From 084653b5ea641649cac7f4d494f804d7c493d0ac Mon Sep 17 00:00:00 2001 From: Helena <37183360+helenarebane@users.noreply.github.com> Date: Mon, 16 Jun 2025 15:11:40 +0300 Subject: [PATCH] B2B-49: save codes to db in sync script (#16) * save codes to db * save codes to db --------- Co-authored-by: Helena --- jobs/sync-analysis-groups.ts | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/jobs/sync-analysis-groups.ts b/jobs/sync-analysis-groups.ts index a0d977e..e31c76b 100644 --- a/jobs/sync-analysis-groups.ts +++ b/jobs/sync-analysis-groups.ts @@ -1,4 +1,5 @@ import { createClient as createCustomClient } from '@supabase/supabase-js'; + import axios from 'axios'; import { format } from 'date-fns'; import { config } from 'dotenv'; @@ -126,6 +127,7 @@ async function syncData() { }); } + const codes: any[] = []; for (const analysisGroup of analysisGroups) { // SAVE ANALYSIS GROUP const { data: insertedAnalysisGroup, error } = await supabase @@ -148,15 +150,17 @@ async function syncData() { const analysisGroupId = insertedAnalysisGroup[0].id; const analysisGroupCodes = toArray(analysisGroup.Kood); - const codes = analysisGroupCodes.map((kood) => ({ - hk_code: kood.HkKood, - hk_code_multiplier: kood.HkKoodiKordaja, - coefficient: kood.Koefitsient, - price: kood.Hind, - analysis_group_id: analysisGroupId, - analysis_element_id: null, - analysis_id: null, - })); + codes.push( + ...analysisGroupCodes.map((kood) => ({ + hk_code: kood.HkKood, + hk_code_multiplier: kood.HkKoodiKordaja, + coefficient: kood.Koefitsient, + price: kood.Hind, + analysis_group_id: analysisGroupId, + analysis_element_id: null, + analysis_id: null, + })), + ); const analysisGroupItems = toArray(analysisGroup.Uuring); @@ -229,7 +233,7 @@ async function syncData() { } const insertedAnalysisId = insertedAnalysis[0].id; - if (analysisElement.Kood) { + if (analysis.Kood) { const analysisCodes = toArray(analysis.Kood); codes.push( @@ -249,6 +253,8 @@ async function syncData() { } } + await supabase.from('codes').upsert(codes); + await supabase.schema('audit').from('sync_entries').insert({ operation: 'ANALYSES_SYNC', status: 'SUCCESS',