feat(MED-131): update analyses sync to medusa store
This commit is contained in:
99
lib/services/analyses.service.ts
Normal file
99
lib/services/analyses.service.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||
import type { IUuringElement } from "./medipost.types";
|
||||
|
||||
export const createAnalysis = async (
|
||||
analysis: IUuringElement,
|
||||
insertedAnalysisElementId: number,
|
||||
) => {
|
||||
const { data: insertedAnalysis, error } = await getSupabaseServerAdminClient()
|
||||
.schema('medreport')
|
||||
.from('analyses')
|
||||
.upsert(
|
||||
{
|
||||
analysis_id_oid: analysis.UuringIdOID,
|
||||
analysis_id_original: analysis.UuringId,
|
||||
tehik_short_loinc: analysis.TLyhend,
|
||||
tehik_loinc_name: analysis.KNimetus,
|
||||
analysis_name_lab: analysis.UuringNimi,
|
||||
order: analysis.Jarjekord,
|
||||
parent_analysis_element_id: insertedAnalysisElementId,
|
||||
},
|
||||
{ onConflict: 'analysis_id_original', ignoreDuplicates: false },
|
||||
)
|
||||
.select('id');
|
||||
const insertedAnalysisId = insertedAnalysis?.[0]?.id as number;
|
||||
|
||||
if (error || !insertedAnalysisId) {
|
||||
throw new Error(
|
||||
`Failed to insert analysis (id: ${analysis.UuringId}) error: ${error?.message}`,
|
||||
);
|
||||
}
|
||||
|
||||
return insertedAnalysisId;
|
||||
}
|
||||
|
||||
const createSyncEntry = async ({
|
||||
operation,
|
||||
status,
|
||||
comment,
|
||||
}: {
|
||||
operation: 'ANALYSES_SYNC' | 'ANALYSIS_GROUPS_SYNC' | 'ANALYSES_MEDUSA_SYNC';
|
||||
status: 'SUCCESS' | 'FAIL';
|
||||
comment?: string;
|
||||
}) => {
|
||||
await getSupabaseServerAdminClient()
|
||||
.schema('audit').from('sync_entries')
|
||||
.insert({
|
||||
operation,
|
||||
status,
|
||||
changed_by_role: 'service_role',
|
||||
comment,
|
||||
});
|
||||
}
|
||||
|
||||
export const createNoNewDataReceivedEntry = async () => {
|
||||
await createSyncEntry({
|
||||
operation: 'ANALYSES_SYNC',
|
||||
status: 'SUCCESS',
|
||||
comment: 'No new data received',
|
||||
});
|
||||
}
|
||||
|
||||
export const createNoDataReceivedEntry = async () => {
|
||||
await createSyncEntry({
|
||||
operation: 'ANALYSES_SYNC',
|
||||
status: 'SUCCESS',
|
||||
comment: 'No data received',
|
||||
});
|
||||
}
|
||||
|
||||
export const createSyncFailEntry = async (error: string) => {
|
||||
await createSyncEntry({
|
||||
operation: 'ANALYSES_SYNC',
|
||||
status: 'FAIL',
|
||||
comment: error,
|
||||
});
|
||||
}
|
||||
|
||||
export const createSyncSuccessEntry = async () => {
|
||||
await createSyncEntry({
|
||||
operation: 'ANALYSES_SYNC',
|
||||
status: 'SUCCESS',
|
||||
});
|
||||
}
|
||||
|
||||
export const createMedusaSyncFailEntry = async (error: string) => {
|
||||
await createSyncEntry({
|
||||
operation: 'ANALYSES_MEDUSA_SYNC',
|
||||
status: 'FAIL',
|
||||
comment: error,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export const createMedusaSyncSuccessEntry = async () => {
|
||||
await createSyncEntry({
|
||||
operation: 'ANALYSES_MEDUSA_SYNC',
|
||||
status: 'SUCCESS',
|
||||
});
|
||||
}
|
||||
@@ -7,18 +7,21 @@ export type AnalysisElement = Tables<{ schema: 'medreport' }, 'analysis_elements
|
||||
analysis_groups: Tables<{ schema: 'medreport' }, 'analysis_groups'>;
|
||||
};
|
||||
|
||||
export async function getAnalysisElements({
|
||||
originalIds,
|
||||
}: {
|
||||
originalIds: string[]
|
||||
}) {
|
||||
const { data: analysisElements } = await getSupabaseServerClient()
|
||||
export async function getAnalysisElements({ originalIds }: {
|
||||
originalIds?: string[]
|
||||
} = {}) {
|
||||
const query = getSupabaseServerClient()
|
||||
.schema('medreport')
|
||||
.from('analysis_elements')
|
||||
.select(`*, analysis_groups(*)`)
|
||||
.in('analysis_id_original', [...new Set(originalIds)])
|
||||
.order('order', { ascending: true });
|
||||
|
||||
if (Array.isArray(originalIds)) {
|
||||
query.in('analysis_id_original', [...new Set(originalIds)]);
|
||||
}
|
||||
|
||||
const { data: analysisElements } = await query;
|
||||
|
||||
return analysisElements ?? [];
|
||||
}
|
||||
|
||||
|
||||
40
lib/services/analysis-group.service.ts
Normal file
40
lib/services/analysis-group.service.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { getSupabaseServerAdminClient } from "@kit/supabase/server-admin-client";
|
||||
|
||||
export const createAnalysisGroup = async (
|
||||
analysisGroup: {
|
||||
id: string;
|
||||
name: string;
|
||||
order: number;
|
||||
}
|
||||
) => {
|
||||
const { data: insertedAnalysisGroup, error } = await getSupabaseServerAdminClient()
|
||||
.schema('medreport')
|
||||
.from('analysis_groups')
|
||||
.upsert(
|
||||
{
|
||||
original_id: analysisGroup.id,
|
||||
name: analysisGroup.name,
|
||||
order: analysisGroup.order,
|
||||
},
|
||||
{ onConflict: 'original_id', ignoreDuplicates: false },
|
||||
)
|
||||
.select('id');
|
||||
const analysisGroupId = insertedAnalysisGroup?.[0]?.id as number;
|
||||
|
||||
if (error || !analysisGroupId) {
|
||||
throw new Error(
|
||||
`Failed to insert analysis group (id: ${analysisGroup.id}), error: ${error?.message}`,
|
||||
);
|
||||
}
|
||||
|
||||
return analysisGroupId;
|
||||
}
|
||||
|
||||
export const getAnalysisGroups = async () => {
|
||||
const { data: analysisGroups } = await getSupabaseServerAdminClient()
|
||||
.schema('medreport')
|
||||
.from('analysis_groups')
|
||||
.select('*');
|
||||
|
||||
return analysisGroups;
|
||||
}
|
||||
@@ -33,6 +33,7 @@ import { XMLParser } from 'fast-xml-parser';
|
||||
import { uniqBy } from 'lodash';
|
||||
|
||||
import { Tables } from '@kit/supabase/database';
|
||||
import { createAnalysisGroup } from './analysis-group.service';
|
||||
|
||||
const BASE_URL = process.env.MEDIPOST_URL!;
|
||||
const USER = process.env.MEDIPOST_USER!;
|
||||
@@ -196,25 +197,11 @@ async function saveAnalysisGroup(
|
||||
analysisGroup: UuringuGrupp,
|
||||
supabase: SupabaseClient,
|
||||
) {
|
||||
const { data: insertedAnalysisGroup, error } = await supabase
|
||||
.schema('medreport')
|
||||
.from('analysis_groups')
|
||||
.upsert(
|
||||
{
|
||||
original_id: analysisGroup.UuringuGruppId,
|
||||
name: analysisGroup.UuringuGruppNimi,
|
||||
order: analysisGroup.UuringuGruppJarjekord,
|
||||
},
|
||||
{ onConflict: 'original_id', ignoreDuplicates: false },
|
||||
)
|
||||
.select('id');
|
||||
|
||||
if (error || !insertedAnalysisGroup[0]?.id) {
|
||||
throw new Error(
|
||||
`Failed to insert analysis group (id: ${analysisGroup.UuringuGruppId}), error: ${error?.message}`,
|
||||
);
|
||||
}
|
||||
const analysisGroupId = insertedAnalysisGroup[0].id;
|
||||
const analysisGroupId = await createAnalysisGroup({
|
||||
id: analysisGroup.UuringuGruppId,
|
||||
name: analysisGroup.UuringuGruppNimi,
|
||||
order: analysisGroup.UuringuGruppJarjekord,
|
||||
});
|
||||
|
||||
const analysisGroupCodes = toArray(analysisGroup.Kood);
|
||||
const codes: Partial<Tables<{ schema: 'medreport' }, 'codes'>>[] =
|
||||
|
||||
18
lib/services/sync-entries.service.ts
Normal file
18
lib/services/sync-entries.service.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { format } from 'date-fns';
|
||||
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||
|
||||
export const getLastCheckedDate = async () => {
|
||||
const { data: lastChecked } = await getSupabaseServerAdminClient()
|
||||
.schema('audit')
|
||||
.from('sync_entries')
|
||||
.select('created_at')
|
||||
.eq('status', 'SUCCESS')
|
||||
.order('created_at')
|
||||
.limit(1);
|
||||
const lastEntry = lastChecked?.[0];
|
||||
const lastCheckedDate = lastEntry
|
||||
? format(lastEntry.created_at, 'yyyy-MM-dd HH:mm:ss')
|
||||
: null;
|
||||
|
||||
return lastCheckedDate;
|
||||
}
|
||||
Reference in New Issue
Block a user