diff --git a/app/api/job/handler/sync-connected-online.ts b/app/api/job/handler/sync-connected-online.ts index 2d0d9fb..829ba54 100644 --- a/app/api/job/handler/sync-connected-online.ts +++ b/app/api/job/handler/sync-connected-online.ts @@ -1,27 +1,19 @@ -import { createClient as createCustomClient } from '@supabase/supabase-js'; - import axios from 'axios'; -import type { IConnectedOnlineResponse_Search_Load } from './types'; +import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client'; + +import type { ISearchLoadResponse } from '~/lib/types/connected-online'; export default async function syncConnectedOnline() { const isProd = process.env.NODE_ENV === 'production'; const baseUrl = process.env.CONNECTED_ONLINE_URL; - const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; - const supabaseServiceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY; - if (!baseUrl || !supabaseUrl || !supabaseServiceRoleKey) { + if (!baseUrl) { throw new Error('Could not access all necessary environment variables'); } - const supabase = createCustomClient(supabaseUrl, supabaseServiceRoleKey, { - auth: { - persistSession: false, - autoRefreshToken: false, - detectSessionInUrl: false, - }, - }); + const supabase = getSupabaseServerAdminClient(); try { const response = await axios.post<{ d: string }>(`${baseUrl}/Search_Load`, { @@ -31,7 +23,7 @@ export default async function syncConnectedOnline() { param: "{'Value':'|et|-1'}", // get all available services in Estonian }); - const responseData: IConnectedOnlineResponse_Search_Load = JSON.parse(response.data.d); + const responseData: ISearchLoadResponse = JSON.parse(response.data.d); if (responseData?.ErrorCode !== 0) { throw new Error('Failed to get Connected Online data'); diff --git a/app/api/job/handler/types.ts b/app/api/job/handler/types.ts deleted file mode 100644 index 0f32e9f..0000000 --- a/app/api/job/handler/types.ts +++ /dev/null @@ -1,39 +0,0 @@ -export interface IMedipostResponse_GetPublicMessageList { - code: number; - messages: { - messageId: string; - }[]; -} - -export interface IConnectedOnlineResponse_Search_Load { - Value: string; - Data: { - T_Lic: { - ID: number; - Name: string; - OnlineCanSelectWorker: boolean; - Email: string | null; - PersonalCodeRequired: boolean; - Phone: string | null; - }[]; - T_Service: { - ID: number; - ClinicID: number; - Code: string; - Description: string | null; - Display: string; - Duration: number; - HasFreeCodes: boolean; - Name: string; - NetoDuration: number; - OnlineHideDuration: number; - OnlineHidePrice: number; - Price: number; - PricePeriods: string | null; - RequiresPayment: boolean; - SyncID: string; - }[]; - }; - ErrorCode: number; - ErrorMessage: string; -} diff --git a/lib/services/codes.service.ts b/lib/services/codes.service.ts new file mode 100644 index 0000000..f3b19d3 --- /dev/null +++ b/lib/services/codes.service.ts @@ -0,0 +1,8 @@ +import { getSupabaseServerAdminClient } from "@kit/supabase/server-admin-client"; +import type { ICode } from "~/lib/types/code"; + +export const createCodes = async (codes: ICode[]) => { + await getSupabaseServerAdminClient() + .schema('medreport').from('codes') + .upsert(codes); +} diff --git a/lib/types/code.ts b/lib/types/code.ts new file mode 100644 index 0000000..a60ca8b --- /dev/null +++ b/lib/types/code.ts @@ -0,0 +1,9 @@ +export interface ICode { + hk_code: string; + hk_code_multiplier: number; + coefficient: number; + price: number; + analysis_group_id: number | null; + analysis_element_id: number | null; + analysis_id: number | null; +} diff --git a/lib/types/connected-online.ts b/lib/types/connected-online.ts index db6205d..57e95d4 100644 --- a/lib/types/connected-online.ts +++ b/lib/types/connected-online.ts @@ -224,3 +224,36 @@ export const ConfirmedLoadResponseSchema = z.object({ ErrorMessage: z.union([z.string(), z.null()]), }); export type ConfirmedLoadResponse = z.infer; + +export interface ISearchLoadResponse { + Value: string; + Data: { + T_Lic: { + ID: number; + Name: string; + OnlineCanSelectWorker: boolean; + Email: string | null; + PersonalCodeRequired: boolean; + Phone: string | null; + }[]; + T_Service: { + ID: number; + ClinicID: number; + Code: string; + Description: string | null; + Display: string; + Duration: number; + HasFreeCodes: boolean; + Name: string; + NetoDuration: number; + OnlineHideDuration: number; + OnlineHidePrice: number; + Price: number; + PricePeriods: string | null; + RequiresPayment: boolean; + SyncID: string; + }[]; + }; + ErrorCode: number; + ErrorMessage: string; +}