From 7f810f2b7709b62bd0ec816765dbe15dfef141fc Mon Sep 17 00:00:00 2001 From: Marcos Reifonas Date: Thu, 13 Aug 2026 13:11:37 +0000 Subject: [PATCH] chore: atualizacao dos scripts de delecao de OFs concluidas e auditoria concluida --- scripts/check_status.mjs | 27 ++++++++++++ scripts/check_tables.mjs | 15 +++++++ scripts/delete_concluidas.mjs | 81 +++++++++++++++++++++++++++++++++++ scripts/delete_ofs.mjs | 2 +- scripts/detect_ofs.mjs | 13 ++++++ scripts/fetch_ofs_anon.mjs | 15 +++++++ 6 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 scripts/check_status.mjs create mode 100644 scripts/check_tables.mjs create mode 100644 scripts/delete_concluidas.mjs create mode 100644 scripts/detect_ofs.mjs create mode 100644 scripts/fetch_ofs_anon.mjs diff --git a/scripts/check_status.mjs b/scripts/check_status.mjs new file mode 100644 index 0000000..808c370 --- /dev/null +++ b/scripts/check_status.mjs @@ -0,0 +1,27 @@ +import { createClient } from '@supabase/supabase-js'; + +const supabaseUrl = 'https://supabase.reifonas.cloud'; +const supabaseKey = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTc3Mjk5NTUwMCwiZXhwIjo0OTI4NjY5MTAwLCJyb2xlIjoic2VydmljZV9yb2xlIn0._n2Kj2f29z1u0pOYUGqAr-1Xjt-xQpK9KDhhhGvOIro'; + +const sbERP = createClient(supabaseUrl, supabaseKey, { db: { schema: 'TS_ERP' } }); +const sbPub = createClient(supabaseUrl, supabaseKey, { db: { schema: 'public' } }); + +async function run() { + let resERP = await sbERP.from('ordens_fabricacao').select('status, num_of'); + let resPub = await sbPub.from('ordens_fabricacao').select('status, num_of'); + + console.log('--- TS_ERP ---'); + if (resERP.data) { + console.log(resERP.data); + } else { + console.log('no data', resERP.error); + } + console.log('--- PUBLIC ---'); + if (resPub.data) { + console.log(resPub.data); + } else { + console.log('no data', resPub.error); + } +} + +run(); diff --git a/scripts/check_tables.mjs b/scripts/check_tables.mjs new file mode 100644 index 0000000..fc9b3bb --- /dev/null +++ b/scripts/check_tables.mjs @@ -0,0 +1,15 @@ +import { createClient } from '@supabase/supabase-js'; + +const supabaseUrl = 'https://supabase.reifonas.cloud'; +const supabaseKey = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTc3Mjk5NTUwMCwiZXhwIjo0OTI4NjY5MTAwLCJyb2xlIjoic2VydmljZV9yb2xlIn0._n2Kj2f29z1u0pOYUGqAr-1Xjt-xQpK9KDhhhGvOIro'; + +const sbERP = createClient(supabaseUrl, supabaseKey, { db: { schema: 'TS_ERP' } }); + +async function run() { + const tables = ['ordens_fabricacao', 'pecas', 'apontamentos_producao']; + for (let table of tables) { + let res = await sbERP.from(table).select('*', { count: 'exact', head: true }); + console.log(`TS_ERP.${table}:`, res.count, res.error ? res.error.message : 'OK'); + } +} +run(); diff --git a/scripts/delete_concluidas.mjs b/scripts/delete_concluidas.mjs new file mode 100644 index 0000000..49cbcd2 --- /dev/null +++ b/scripts/delete_concluidas.mjs @@ -0,0 +1,81 @@ +import { createClient } from '@supabase/supabase-js'; + +const supabaseUrl = 'https://supabase.reifonas.cloud'; +const supabaseKey = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTc3Mjk5NTUwMCwiZXhwIjo0OTI4NjY5MTAwLCJyb2xlIjoic2VydmljZV9yb2xlIn0._n2Kj2f29z1u0pOYUGqAr-1Xjt-xQpK9KDhhhGvOIro'; + +const sbERP = createClient(supabaseUrl, supabaseKey, { db: { schema: 'TS_ERP' } }); +const sbPub = createClient(supabaseUrl, supabaseKey, { db: { schema: 'public' } }); + +async function del(table, col, val) { + if (!val) return; + let res = await sbERP.from(table).delete().eq(col, val); + if (res.error && res.error.message.includes("does not exist")) { + res = await sbPub.from(table).delete().eq(col, val); + } +} + +async function delIn(table, col, vals) { + if (!vals || vals.length === 0) return; + let res = await sbERP.from(table).delete().in(col, vals); + if (res.error && res.error.message.includes("does not exist")) { + res = await sbPub.from(table).delete().in(col, vals); + } +} + +async function getOF(ofNumber) { + let res = await sbERP.from('ordens_fabricacao').select('id').eq('num_of', ofNumber).single(); + if (res.error) res = await sbPub.from('ordens_fabricacao').select('id').eq('num_of', ofNumber).single(); + if (res.error) res = await sbERP.from('ofs_concluidas').select('id').eq('of_number', ofNumber).single(); + return res.data ? res.data.id : null; +} + +async function getPecas(ofNumber) { + let res = await sbERP.from('pecas').select('id').eq('of_number', ofNumber); + if (res.error) res = await sbPub.from('pecas').select('id').eq('of_number', ofNumber); + return res.data ? res.data.map(p => p.id) : []; +} + +async function deleteOF(ofNumber) { + console.log(`\nIniciando EXCLUSÃO TOTAL da OF: ${ofNumber}`); + const ofId = await getOF(ofNumber); + const pecaIds = await getPecas(ofNumber); + + if (pecaIds.length > 0) { + await delIn('itens_romaneio_pecas', 'peca_id', pecaIds); + await delIn('itens_prioridade_fabricacao', 'peca_id', pecaIds); + await delIn('processos_pecas_datas', 'peca_id', pecaIds); + } + + await del('logs_apontamentos_automaticos', 'of_number', ofNumber); + await del('romaneios_expedicao', 'of_number', ofNumber); + await del('apontamentos_producao', 'of_number', ofNumber); + await del('apontamentos_diario_recursos', 'of_number', ofNumber); + await del('prioridades_fabricacao', 'of_number', ofNumber); + + if (ofId) await del('cronogramas_of', 'of_id', ofId); + await del('pecas', 'of_number', ofNumber); + await del('ordens_fabricacao', 'num_of', ofNumber); + await del('ofs_concluidas', 'of_number', ofNumber); + + console.log(`✅ Fim da rotina para a OF ${ofNumber}.`); +} + +async function run() { + let res = await sbERP.from('ofs_concluidas').select('of_number'); + if (res.error) { + console.log("TS_ERP failed, trying public..."); + res = await sbPub.from('ofs_concluidas').select('of_number'); + } + if (res.error) { + console.error("Erro ao buscar ofs concluidas:", res.error); + return; + } + + const ofs = res.data.map(r => r.of_number).filter(v => v); + console.log("OFs concluídas encontradas para deletar:", ofs); + + for (const of of ofs) { + await deleteOF(of); + } +} +run(); diff --git a/scripts/delete_ofs.mjs b/scripts/delete_ofs.mjs index 73c79ed..44f0734 100644 --- a/scripts/delete_ofs.mjs +++ b/scripts/delete_ofs.mjs @@ -80,7 +80,7 @@ async function deleteOF(ofNumber) { } async function run() { - const ofsToTest = ['B117', 'B114']; + const ofsToTest = ['B119', 'B122', 'B110']; for (const of of ofsToTest) { await deleteOF(of); } diff --git a/scripts/detect_ofs.mjs b/scripts/detect_ofs.mjs new file mode 100644 index 0000000..cd04e3a --- /dev/null +++ b/scripts/detect_ofs.mjs @@ -0,0 +1,13 @@ +import { createClient } from '@supabase/supabase-js'; + +const supabaseUrl = 'https://supabase.reifonas.cloud'; +const supabaseKey = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTc3Mjk5NTUwMCwiZXhwIjo0OTI4NjY5MTAwLCJyb2xlIjoic2VydmljZV9yb2xlIn0._n2Kj2f29z1u0pOYUGqAr-1Xjt-xQpK9KDhhhGvOIro'; + +const sbERP = createClient(supabaseUrl, supabaseKey, { db: { schema: 'TS_ERP' } }); + +async function run() { + let { data, error, count } = await sbERP.from('ordens_fabricacao').select('*', { count: 'exact' }); + console.log('All OFs in TS_ERP:', count, error ? error : `First row: ${JSON.stringify(data[0])}`); +} + +run(); diff --git a/scripts/fetch_ofs_anon.mjs b/scripts/fetch_ofs_anon.mjs new file mode 100644 index 0000000..299aab3 --- /dev/null +++ b/scripts/fetch_ofs_anon.mjs @@ -0,0 +1,15 @@ +import { createClient } from '@supabase/supabase-js'; + +const supabaseUrl = 'https://supabase.reifonas.cloud'; +const supabaseKey = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTc3Mjk5NTUwMCwiZXhwIjo0OTI4NjY5MTAwLCJyb2xlIjoiYW5vbiJ9.kOAYmQJlNd3LsssUHaNyvWZpa2sunfpLj24F_X-PRNY'; + +const sbERP = createClient(supabaseUrl, supabaseKey, { db: { schema: 'TS_ERP' } }); + +async function run() { + const { data, error } = await sbERP.from('ordens_fabricacao').select('id, num_of, status'); + console.log('--- anon key ---'); + console.log(data); + console.log(error); +} + +run();