3 Commits

6 changed files with 40 additions and 17 deletions

View File

@@ -0,0 +1,12 @@
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
export async function getAnalysisResponseAdmin(analysisOrderId: number) {
const { data } = await getSupabaseServerAdminClient()
.schema('medreport')
.from('analysis_responses')
.select('*')
.eq('analysis_order_id', analysisOrderId)
.single()
.throwOnError();
return data;
}

View File

@@ -12,7 +12,7 @@ import { createBillingEventHandlerService } from './billing-event-handler.servic
type ClientProvider = () => SupabaseClient<Database>; type ClientProvider = () => SupabaseClient<Database>;
// the billing provider from the database // the billing provider from the database
type BillingProvider = Enums<'billing_provider'>; type BillingProvider = Enums<{ schema: 'medreport' }, 'billing_provider'>;
/** /**
* @name getBillingEventHandlerService * @name getBillingEventHandlerService

View File

@@ -19,10 +19,10 @@ import { initializeEmailI18n } from '../lib/i18n';
export async function renderAllResultsReceivedEmail({ export async function renderAllResultsReceivedEmail({
language, language,
analysisOrderId, analysisResponseId,
}: { }: {
language: string; language: string;
analysisOrderId: number; analysisResponseId: number;
}) { }) {
const namespace = 'all-results-received-email'; const namespace = 'all-results-received-email';
@@ -57,13 +57,13 @@ export async function renderAllResultsReceivedEmail({
{t(`${namespace}:openOrdersHeading`)} {t(`${namespace}:openOrdersHeading`)}
</Text> </Text>
<EmailButton <EmailButton
href={`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisOrderId}`} href={`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisResponseId}`}
> >
{t(`${namespace}:linkText`)} {t(`${namespace}:linkText`)}
</EmailButton> </EmailButton>
<Text> <Text>
{t(`${namespace}:ifLinksDisabled`)}{' '} {t(`${namespace}:ifLinksDisabled`)}{' '}
{`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisOrderId}`} {`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisResponseId}`}
</Text> </Text>
<CommonFooter t={t} /> <CommonFooter t={t} />
</EmailContent> </EmailContent>

View File

@@ -19,10 +19,10 @@ import { initializeEmailI18n } from '../lib/i18n';
export async function renderFirstResultsReceivedEmail({ export async function renderFirstResultsReceivedEmail({
language, language,
analysisOrderId, analysisResponseId,
}: { }: {
language: string; language: string;
analysisOrderId: number; analysisResponseId: number;
}) { }) {
const namespace = 'first-results-received-email'; const namespace = 'first-results-received-email';
@@ -61,14 +61,14 @@ export async function renderFirstResultsReceivedEmail({
</Text> </Text>
<EmailButton <EmailButton
href={`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisOrderId}`} href={`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisResponseId}`}
> >
{t(`${namespace}:linkText`)} {t(`${namespace}:linkText`)}
</EmailButton> </EmailButton>
<Text> <Text>
{t(`${namespace}:ifLinksDisabled`)}{' '} {t(`${namespace}:ifLinksDisabled`)}{' '}
{`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisOrderId}`} {`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisResponseId}`}
</Text> </Text>
<CommonFooter t={t} /> <CommonFooter t={t} />
</EmailContent> </EmailContent>

View File

@@ -404,8 +404,14 @@ export async function getAnalysisResultsForDoctor(
const medusaOrderId = const medusaOrderId =
firstAnalysisResponse?.analysis_responses?.analysis_order?.medusa_order_id; firstAnalysisResponse?.analysis_responses?.analysis_order?.medusa_order_id;
if (!analysisResponsesData?.length || !userId || !medusaOrderId) { if (!analysisResponsesData?.length) {
throw new Error('Failed to retrieve full analysis data.'); throw new Error('No analysis responses data found.');
}
if (!userId) {
throw new Error('No user id found.');
}
if (!medusaOrderId) {
throw new Error('No medusa order id found.');
} }
const responseElementAnalysisElementOriginalIds = analysisResponsesData.map( const responseElementAnalysisElementOriginalIds = analysisResponsesData.map(

View File

@@ -16,6 +16,7 @@ import {
getDoctorAccounts, getDoctorAccounts,
getUserContactAdmin, getUserContactAdmin,
} from '~/lib/services/account.service'; } from '~/lib/services/account.service';
import { getAnalysisResponseAdmin } from '~/lib/services/analysis-response.service';
import { import {
NotificationAction, NotificationAction,
createNotificationLog, createNotificationLog,
@@ -74,14 +75,14 @@ class OrderWebhooksService {
} }
logger.info(ctx, 'Status change processed. No notifications to send.'); logger.info(ctx, 'Status change processed. No notifications to send.');
} catch (e: any) { } catch (e) {
if (actions.length) if (actions.length)
await Promise.all( await Promise.all(
actions.map((action) => actions.map((action) =>
createNotificationLog({ createNotificationLog({
action, action,
status: 'FAIL', status: 'FAIL',
comment: e?.message, comment: e instanceof Error ? e.message : 'Unknown error',
relatedRecordId: analysisOrder.id, relatedRecordId: analysisOrder.id,
}), }),
), ),
@@ -201,11 +202,11 @@ class OrderWebhooksService {
status: 'SUCCESS', status: 'SUCCESS',
relatedRecordId: orderCart.order_id, relatedRecordId: orderCart.order_id,
}); });
} catch (e: any) { } catch (e) {
createNotificationLog({ createNotificationLog({
action: NotificationAction.TTO_ORDER_CONFIRMATION, action: NotificationAction.TTO_ORDER_CONFIRMATION,
status: 'FAIL', status: 'FAIL',
comment: e?.message, comment: e instanceof Error ? e.message : 'Unknown error',
relatedRecordId: ttoReservation.id, relatedRecordId: ttoReservation.id,
}); });
logger.error( logger.error(
@@ -345,10 +346,12 @@ class OrderWebhooksService {
.map(({ email }) => email) .map(({ email }) => email)
.filter((email): email is string => !!email); .filter((email): email is string => !!email);
const analysisResponse = await getAnalysisResponseAdmin(analysisOrder.id);
await sendEmailFromTemplate( await sendEmailFromTemplate(
renderFirstResultsReceivedEmail, renderFirstResultsReceivedEmail,
{ {
analysisOrderId: analysisOrder.id, analysisResponseId: analysisResponse.id,
language: 'et', language: 'et',
}, },
doctorEmails, doctorEmails,
@@ -380,10 +383,12 @@ class OrderWebhooksService {
return; return;
} }
const analysisResponse = await getAnalysisResponseAdmin(analysisOrder.id);
await sendEmailFromTemplate( await sendEmailFromTemplate(
renderAllResultsReceivedEmail, renderAllResultsReceivedEmail,
{ {
analysisOrderId: analysisOrder.id, analysisResponseId: analysisResponse.id,
language: 'et', language: 'et',
}, },
assignedDoctorEmail, assignedDoctorEmail,