|
|
@@ -0,0 +1,74 @@
|
|
|
+import "dotenv/config";
|
|
|
+import { config } from "../src/config/index.js";
|
|
|
+import { Atendimento } from "../src/models/Atendimento.model.js";
|
|
|
+import { importarProtocolo } from "../src/services/atendimentosService.js";
|
|
|
+
|
|
|
+const limiteIdx = process.argv.indexOf("--limite");
|
|
|
+const LIMITE = limiteIdx > -1 ? Number(process.argv[limiteIdx + 1]) : null;
|
|
|
+const CONCORRENCIA = 4;
|
|
|
+
|
|
|
+async function main() {
|
|
|
+ let query = Atendimento.query()
|
|
|
+ .where("Setor", "SUP")
|
|
|
+ .withGraphFetched("cliente")
|
|
|
+ .orderBy("Abertura", "desc");
|
|
|
+ if (LIMITE) query = query.limit(LIMITE);
|
|
|
+
|
|
|
+ const atendimentos = await query;
|
|
|
+ const total = atendimentos.length;
|
|
|
+ console.log(`[backfill-erp-sup] iniciando: ${total} atendimentos SUP`);
|
|
|
+
|
|
|
+ const baseUrl = config.ifbot.baseUrl.replace(/\/+$/, "");
|
|
|
+ let processados = 0;
|
|
|
+ let comVinculo = 0;
|
|
|
+ let erros = 0;
|
|
|
+ const t0 = Date.now();
|
|
|
+
|
|
|
+ async function processar(a) {
|
|
|
+ try {
|
|
|
+ const item = {
|
|
|
+ Id: a.Id,
|
|
|
+ Codigo: a.Codigo,
|
|
|
+ Abertura: a.Abertura,
|
|
|
+ Cliente: { Id: a.cliente?.Id, Nome: a.cliente?.Nome, Telefone: a.cliente?.Telefone }
|
|
|
+ };
|
|
|
+ await importarProtocolo(item, baseUrl);
|
|
|
+ const atualizado = await Atendimento.query()
|
|
|
+ .findById(a.Id)
|
|
|
+ .withGraphFetched("cliente(soVinculo)")
|
|
|
+ .modifiers({ soVinculo: (q) => q.select("ErpVinculoConfirmadoEm") });
|
|
|
+ if (atualizado?.cliente?.ErpVinculoConfirmadoEm) comVinculo += 1;
|
|
|
+ } catch (err) {
|
|
|
+ erros += 1;
|
|
|
+ console.warn(`[backfill-erp-sup] erro em ${a.Codigo}: ${err.message}`);
|
|
|
+ } finally {
|
|
|
+ processados += 1;
|
|
|
+ if (processados % 50 === 0 || processados === total) {
|
|
|
+ const decorridoS = Math.round((Date.now() - t0) / 1000);
|
|
|
+ console.log(
|
|
|
+ `[backfill-erp-sup] progresso: ${processados}/${total} | com vínculo: ${comVinculo} | erros: ${erros} | ${decorridoS}s decorridos`
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let idx = 0;
|
|
|
+ async function worker() {
|
|
|
+ while (idx < atendimentos.length) {
|
|
|
+ const a = atendimentos[idx++];
|
|
|
+ await processar(a);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ await Promise.all(Array.from({ length: CONCORRENCIA }, worker));
|
|
|
+
|
|
|
+ const totalS = Math.round((Date.now() - t0) / 1000);
|
|
|
+ console.log(
|
|
|
+ `[backfill-erp-sup] concluído: ${processados}/${total} | com vínculo: ${comVinculo} | erros: ${erros} | ${totalS}s total`
|
|
|
+ );
|
|
|
+ process.exit(0);
|
|
|
+}
|
|
|
+
|
|
|
+main().catch((err) => {
|
|
|
+ console.error("[backfill-erp-sup] falha fatal:", err);
|
|
|
+ process.exit(1);
|
|
|
+});
|