refactor: desativa conexao com postgres
Comenta integracao com WMS/leadtime e remove DataSource postgres do bootstrap e repositories.
This commit is contained in:
@@ -2,7 +2,6 @@ import { Module, MiddlewareConsumer, NestModule } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { createOracleConfig } from './core/configs/typeorm.oracle.config';
|
||||
import { createPostgresConfig } from './core/configs/typeorm.postgres.config';
|
||||
import { LogisticModule } from './logistic/logistic.module';
|
||||
import { AuthModule } from './auth/auth/auth.module';
|
||||
import { DataConsultModule } from './data-consult/data-consult.module';
|
||||
@@ -27,11 +26,12 @@ import { PartnersModule } from './partners/partners.module';
|
||||
inject: [ConfigService],
|
||||
useFactory: createOracleConfig,
|
||||
}),
|
||||
TypeOrmModule.forRootAsync({
|
||||
name: 'postgres',
|
||||
inject: [ConfigService],
|
||||
useFactory: createPostgresConfig,
|
||||
}),
|
||||
// Postgres desativado: conexao removida e trechos de uso comentados no codigo.
|
||||
// TypeOrmModule.forRootAsync({
|
||||
// name: 'postgres',
|
||||
// inject: [ConfigService],
|
||||
// useFactory: createPostgresConfig,
|
||||
// }),
|
||||
ThrottlerModule.forRootAsync({
|
||||
imports: [ConfigModule],
|
||||
inject: [ConfigService],
|
||||
|
||||
@@ -6,11 +6,11 @@ export const databaseConfig = registerAs('database', () => ({
|
||||
username: process.env.ORACLE_USER,
|
||||
password: process.env.ORACLE_PASSWORD,
|
||||
},
|
||||
postgres: {
|
||||
host: process.env.POSTGRES_HOST,
|
||||
port: parseInt(process.env.POSTGRES_PORT || '5432', 10),
|
||||
username: process.env.POSTGRES_USER,
|
||||
password: process.env.POSTGRES_PASSWORD,
|
||||
database: process.env.POSTGRES_DB,
|
||||
},
|
||||
// postgres: {
|
||||
// host: process.env.POSTGRES_HOST,
|
||||
// port: parseInt(process.env.POSTGRES_PORT || '5432', 10),
|
||||
// username: process.env.POSTGRES_USER,
|
||||
// password: process.env.POSTGRES_PASSWORD,
|
||||
// database: process.env.POSTGRES_DB,
|
||||
// },
|
||||
}));
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { createOracleConfig } from '../core/configs/typeorm.oracle.config';
|
||||
import { createPostgresConfig } from '../core/configs/typeorm.postgres.config';
|
||||
import { CarOutDelivery } from '../core/models/car-out-delivery.model';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { CarInDelivery } from '../core/models/car-in-delivery.model';
|
||||
@@ -11,21 +10,29 @@ export class LogisticService {
|
||||
constructor(private readonly configService: ConfigService) {}
|
||||
|
||||
async getExpedicao() {
|
||||
// Postgres desativado: este endpoint dependia do WMS (Postgres).
|
||||
// Mantido como referencia, mas sem executar queries.
|
||||
throw new HttpException(
|
||||
'Integracao com WMS (Postgres) desativada. Conexao com Postgres removida.',
|
||||
HttpStatus.SERVICE_UNAVAILABLE,
|
||||
);
|
||||
|
||||
/*
|
||||
const dataSource = new DataSource(createPostgresConfig(this.configService));
|
||||
await dataSource.initialize();
|
||||
const queryRunner = dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
try {
|
||||
const sqlWMS = `select dados.*,
|
||||
( select count(distinct v.numero_carga) quantidade_cargas_embarcadas
|
||||
from volume v, carga c2
|
||||
where v.numero_carga = c2.numero
|
||||
and c2.data_integracao >= TO_DATE('01/02/2025', 'DD/MM/YYYY')
|
||||
and TO_DATE(RIGHT(c2.observacao, 10), 'DD/MM/YYYY') = dados.dataHoje
|
||||
and v.embarcado = 'S' ) quantidade_cargas_embarcadas
|
||||
FROM ( select date_trunc('day', (CURRENT_DATE + INTERVAL '1 day'))::date data_saida, --TO_DATE(RIGHT(c.observacao, 10), 'DD/MM/YYYY') data_saida,
|
||||
date_trunc('day', (CURRENT_DATE + INTERVAL '1 day'))::date dataHoje,
|
||||
SUM(c.qt_itens_conferidos) total_itens_conferidos,
|
||||
( select count(distinct v.numero_carga) quantidade_cargas_embarcadas
|
||||
from volume v, carga c2
|
||||
where v.numero_carga = c2.numero
|
||||
and c2.data_integracao >= TO_DATE('01/02/2025', 'DD/MM/YYYY')
|
||||
and TO_DATE(RIGHT(c2.observacao, 10), 'DD/MM/YYYY') = dados.dataHoje
|
||||
and v.embarcado = 'S' ) quantidade_cargas_embarcadas
|
||||
FROM ( select date_trunc('day', (CURRENT_DATE + INTERVAL '1 day'))::date data_saida, --TO_DATE(RIGHT(c.observacao, 10), 'DD/MM/YYYY') data_saida,
|
||||
date_trunc('day', (CURRENT_DATE + INTERVAL '1 day'))::date dataHoje,
|
||||
SUM(c.qt_itens_conferidos) total_itens_conferidos,
|
||||
SUM(c.qt_itens_separados) total_itens_separados,
|
||||
SUM(c.qt_total_itens) quantidade_total_itens,
|
||||
SUM(c.qt_total_pedidos) quantidade_total,
|
||||
@@ -97,6 +104,7 @@ export class LogisticService {
|
||||
await queryRunner.release();
|
||||
await dataSource.destroy();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
async getDeliveries(_placa: string) {
|
||||
@@ -142,7 +150,8 @@ export class LogisticService {
|
||||
}
|
||||
|
||||
async getStatusCar(placa: string) {
|
||||
const dataSource = new DataSource(createPostgresConfig(this.configService));
|
||||
// Postgres desativado: este metodo deve usar Oracle.
|
||||
const dataSource = new DataSource(createOracleConfig(this.configService));
|
||||
await dataSource.initialize();
|
||||
const queryRunner = dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
@@ -186,7 +195,8 @@ export class LogisticService {
|
||||
}
|
||||
|
||||
async createCarOut(data: CarOutDelivery) {
|
||||
const dataSource = new DataSource(createPostgresConfig(this.configService));
|
||||
// Postgres desativado: este fluxo usa SQL Oracle (DUAL/sequence).
|
||||
const dataSource = new DataSource(createOracleConfig(this.configService));
|
||||
await dataSource.initialize();
|
||||
const queryRunner = dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
@@ -257,7 +267,8 @@ export class LogisticService {
|
||||
}
|
||||
|
||||
async createCarIn(data: CarInDelivery) {
|
||||
const dataSource = new DataSource(createPostgresConfig(this.configService));
|
||||
// Postgres desativado: este fluxo usa SQL Oracle (DUAL/sequence).
|
||||
const dataSource = new DataSource(createOracleConfig(this.configService));
|
||||
await dataSource.initialize();
|
||||
const queryRunner = dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
|
||||
@@ -21,9 +21,10 @@ import { DeliveryCompleted } from '../dto/delivery-completed.dto';
|
||||
export class OrdersRepository {
|
||||
constructor(
|
||||
@InjectDataSource('oracle') private readonly oracleDataSource: DataSource,
|
||||
@InjectDataSource('postgres')
|
||||
private readonly postgresDataSource: DataSource,
|
||||
) {}
|
||||
) // Postgres desativado: conexao removida e trechos de uso comentados.
|
||||
// @InjectDataSource('postgres')
|
||||
// private readonly postgresDataSource: DataSource,
|
||||
{}
|
||||
|
||||
/**
|
||||
* Busca log de transferência por ID do pedido
|
||||
@@ -1365,10 +1366,8 @@ WHERE
|
||||
}
|
||||
async getOrderDeliveries(orderId: string) {
|
||||
const queryRunnerOracle = this.oracleDataSource.createQueryRunner();
|
||||
const queryRunnerPostgres = this.postgresDataSource.createQueryRunner();
|
||||
|
||||
await queryRunnerOracle.connect();
|
||||
await queryRunnerPostgres.connect();
|
||||
|
||||
try {
|
||||
const sqlOracle = `SELECT PCPEDC.CODFILIAL as "storeId"
|
||||
@@ -1407,75 +1406,8 @@ WHERE
|
||||
|
||||
const orders = await queryRunnerOracle.manager.query(sqlOracle);
|
||||
|
||||
// Consulta no WMS (Postgres) - Modificada para buscar pelo número do pedido
|
||||
const sqlWMS = `
|
||||
SELECT p.numero,
|
||||
p.posicao,
|
||||
CASE
|
||||
WHEN p.posicao = 'F' THEN 'FATURADO'
|
||||
WHEN p.posicao = 'C' THEN 'CONCLUIDO'
|
||||
WHEN p.posicao = 'L' THEN 'LIBERADO'
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM movimentacao m2 WHERE m2.numero_pedido = p.numero
|
||||
AND m2.data_inicio_separacao IS NULL
|
||||
) AND p.posicao = 'M' THEN 'MONTADO'
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM movimentacao m2 WHERE m2.numero_pedido = p.numero
|
||||
AND m2.data_inicio_separacao IS NOT NULL
|
||||
AND m2.data_fim_separacao IS NULL
|
||||
) AND p.posicao = 'M' THEN 'EM SEPARACAO'
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM movimentacao m2 WHERE m2.numero_pedido = p.numero
|
||||
AND m2.data_fim_separacao IS NOT NULL
|
||||
AND m2.data_inicio_conferencia IS NULL
|
||||
) AND p.posicao = 'M' THEN 'SEPARACAO FINALIZADA'
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM movimentacao m2 WHERE m2.numero_pedido = p.numero
|
||||
AND m2.data_inicio_conferencia IS NOT NULL
|
||||
AND m2.data_fim_conferencia IS NULL
|
||||
) AND p.posicao = 'M' THEN 'EM CONFERENCIA'
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM movimentacao m2 WHERE m2.numero_pedido = p.numero
|
||||
AND m2.data_fim_conferencia IS NOT NULL
|
||||
AND (SELECT COUNT(1) FROM volume v WHERE v.numero_pedido = m2.numero_pedido AND v.data_embarque IS NULL) = 0
|
||||
) AND p.posicao = 'M' THEN 'CONFERENCIA FINALIZADA'
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM movimentacao m2 WHERE m2.numero_pedido = p.numero
|
||||
AND m2.data_fim_conferencia IS NOT NULL
|
||||
AND (SELECT COUNT(1) FROM volume v WHERE v.numero_pedido = m2.numero_pedido AND v.data_embarque IS NULL) > 0
|
||||
) AND p.posicao = 'M' THEN 'EMBARCADO'
|
||||
END as "situacaoPedido"
|
||||
FROM pedido p
|
||||
WHERE p.numero IN (
|
||||
SELECT DISTINCT m.numero_pedido
|
||||
FROM movimentacao m
|
||||
WHERE m.numero_pedido = $1::bigint
|
||||
OR m.numero_pedido IN (
|
||||
SELECT CAST(o.orderId AS bigint) FROM UNNEST($2::text[]) o(orderId)
|
||||
WHERE o.orderId ~ '^[0-9]+$'
|
||||
)
|
||||
)
|
||||
`;
|
||||
|
||||
// Criar array com os IDs de pedidos obtidos do Oracle
|
||||
const orderIds = orders.map((o) => o.orderId?.toString() || '');
|
||||
|
||||
// Converter orderId para número para evitar erro de tipo
|
||||
const numericOrderId = parseInt(orderId, 10);
|
||||
const ordersWMS = await queryRunnerPostgres.manager.query(sqlWMS, [
|
||||
numericOrderId,
|
||||
orderIds,
|
||||
]);
|
||||
|
||||
// Atualizar status baseado no WMS
|
||||
for (const order of orders) {
|
||||
const orderWMS = ordersWMS.find(
|
||||
(o) => Number(o.numero) === Number(order.orderId),
|
||||
);
|
||||
if (orderWMS && !order.deliveryConfirmationDate) {
|
||||
order.status = orderWMS.situacaoPedido;
|
||||
}
|
||||
}
|
||||
// Postgres desativado: consulta no WMS (Postgres) comentada.
|
||||
// O status retornado aqui fica apenas com base no Oracle.
|
||||
|
||||
return orders;
|
||||
} catch (_error) {
|
||||
@@ -1485,15 +1417,12 @@ WHERE
|
||||
);
|
||||
} finally {
|
||||
await queryRunnerOracle.release();
|
||||
await queryRunnerPostgres.release();
|
||||
}
|
||||
}
|
||||
|
||||
async getLeadtimeWMS(orderId: string): Promise<LeadtimeDto[]> {
|
||||
const queryRunnerPostgres = this.postgresDataSource.createQueryRunner();
|
||||
const queryRunnerOracle = this.oracleDataSource.createQueryRunner();
|
||||
|
||||
await queryRunnerPostgres.connect();
|
||||
await queryRunnerOracle.connect();
|
||||
|
||||
try {
|
||||
@@ -1512,84 +1441,14 @@ WHERE
|
||||
`;
|
||||
const dataOracle = await queryRunnerOracle.manager.query(sqlOracle);
|
||||
|
||||
// Consulta no Postgres
|
||||
const sqlPostgres = `
|
||||
SELECT DADOS.ETAPA as "etapa",
|
||||
DADOS.DESCRICAO_ETAPA as "descricaoEtapa",
|
||||
DADOS.DATA as "data",
|
||||
DADOS.CODIGO_FUNCIONARIO as "codigoFuncionario",
|
||||
DADOS.NOME_FUNCIONARIO as "nomeFuncionario",
|
||||
DADOS.NUMERO_PEDIDO as "numeroPedido"
|
||||
FROM (
|
||||
SELECT 3 AS ETAPA, 'Inicio Separação' AS DESCRICAO_ETAPA,
|
||||
MIN(m.data_inicio_separacao) AS DATA,
|
||||
MAX(m.codigo_separador) AS CODIGO_FUNCIONARIO,
|
||||
(SELECT u.nome FROM usuario u WHERE u.id = MAX(m.codigo_separador)) AS NOME_FUNCIONARIO,
|
||||
m.numero_pedido
|
||||
FROM movimentacao m
|
||||
WHERE m.data_inicio_separacao >= '2025-01-01'
|
||||
AND m.numero_pedido > 0
|
||||
AND m.data_inicio_separacao IS NOT NULL
|
||||
GROUP BY m.numero_pedido
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT 4, 'Separado', MIN(m.data_fim_separacao), MAX(m.codigo_separador),
|
||||
(SELECT u.nome FROM usuario u WHERE u.id = MAX(m.codigo_separador)), m.numero_pedido
|
||||
FROM movimentacao m
|
||||
WHERE m.data_inicio_separacao >= '2025-01-01'
|
||||
AND m.numero_pedido > 0
|
||||
AND m.data_fim_separacao IS NOT NULL
|
||||
GROUP BY m.numero_pedido
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT 5, 'Inicio Conferência', MIN(m.data_inicio_conferencia), MAX(m.codigo_conferente),
|
||||
(SELECT u.nome FROM usuario u WHERE u.id = MAX(m.codigo_conferente)), m.numero_pedido
|
||||
FROM movimentacao m
|
||||
WHERE m.data_inicio_conferencia IS NOT NULL
|
||||
AND m.numero_pedido > 0
|
||||
GROUP BY m.numero_pedido
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT 6, 'Fim Conferência', MIN(m.data_fim_conferencia), MAX(m.codigo_conferente),
|
||||
(SELECT u.nome FROM usuario u WHERE u.id = MAX(m.codigo_conferente)), m.numero_pedido
|
||||
FROM movimentacao m
|
||||
WHERE m.data_fim_conferencia IS NOT NULL
|
||||
AND m.numero_pedido > 0
|
||||
GROUP BY m.numero_pedido
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT 7, 'Embarcado', MAX(v.data_embarque), v.usuario_embarque_id,
|
||||
(SELECT u.nome FROM usuario u WHERE u.id = v.usuario_embarque_id), m.numero_pedido
|
||||
FROM movimentacao m
|
||||
JOIN volume v ON m.numero_pedido = v.numero_pedido
|
||||
WHERE v.data_embarque IS NOT NULL
|
||||
AND m.numero_pedido > 0
|
||||
GROUP BY v.usuario_embarque_id, m.numero_pedido
|
||||
) DADOS
|
||||
WHERE DADOS.numero_pedido = $1
|
||||
ORDER BY DADOS.numero_pedido, DADOS.ETAPA;
|
||||
`;
|
||||
const dataPostgres = await queryRunnerPostgres.manager.query(
|
||||
sqlPostgres,
|
||||
[orderId],
|
||||
);
|
||||
|
||||
// Junta os dados Oracle + Postgres
|
||||
const leadtime = [...dataOracle, ...dataPostgres];
|
||||
|
||||
// Ordena pela etapa (opcional, para garantir ordem)
|
||||
return leadtime.sort((a, b) => a.etapa - b.etapa);
|
||||
// Postgres desativado: consulta do leadtime no WMS (Postgres) comentada.
|
||||
return dataOracle;
|
||||
} catch (_error) {
|
||||
throw new HttpException(
|
||||
'Erro ao buscar dados de leadtime do WMS',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
} finally {
|
||||
await queryRunnerPostgres.release();
|
||||
await queryRunnerOracle.release();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user