MED-145: send notification to patient when summary completed (#61)
* MED-145: send notification to patient when summary completed * MED-145: send notification to patient when summary completed * use aliased imports where possible, revert cart service urls * save language preference to local db * remove unnecessary optional chaning
This commit is contained in:
@@ -5,6 +5,10 @@ import { revalidatePath } from 'next/cache';
|
||||
import { enhanceAction } from '@kit/next/actions';
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
|
||||
import {
|
||||
NotificationAction,
|
||||
createNotificationLog,
|
||||
} from '../../../../../../../lib/services/audit/notificationEntries.service';
|
||||
import {
|
||||
DoctorAnalysisFeedbackTable,
|
||||
DoctorJobSelect,
|
||||
@@ -107,6 +111,7 @@ export const giveFeedbackAction = doctorAction(
|
||||
status: DoctorAnalysisFeedbackTable['status'];
|
||||
}) => {
|
||||
const logger = await getLogger();
|
||||
const isCompleted = status === 'COMPLETED';
|
||||
|
||||
try {
|
||||
logger.info(
|
||||
@@ -118,8 +123,25 @@ export const giveFeedbackAction = doctorAction(
|
||||
logger.info({ analysisOrderId }, `Successfully submitted feedback`);
|
||||
|
||||
revalidateDoctorAnalysis();
|
||||
|
||||
if (isCompleted) {
|
||||
await createNotificationLog({
|
||||
action: NotificationAction.DOCTOR_FEEDBACK_RECEIVED,
|
||||
status: 'SUCCESS',
|
||||
relatedRecordId: analysisOrderId,
|
||||
});
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
} catch (e) {
|
||||
} catch (e: any) {
|
||||
if (isCompleted) {
|
||||
await createNotificationLog({
|
||||
action: NotificationAction.DOCTOR_FEEDBACK_RECEIVED,
|
||||
status: 'FAIL',
|
||||
comment: e?.message,
|
||||
relatedRecordId: analysisOrderId,
|
||||
});
|
||||
}
|
||||
logger.error('Failed to give feedback', e);
|
||||
return { success: false, reason: ErrorReason.UNKNOWN };
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@ import 'server-only';
|
||||
|
||||
import { isBefore } from 'date-fns';
|
||||
|
||||
import { getFullName } from '@kit/shared/utils';
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
import { sendDoctorSummaryCompletedEmail } from '../../../../../../../lib/services/mailer.service';
|
||||
import { AnalysisResultDetails } from '../schema/doctor-analysis-detail-view.schema';
|
||||
import {
|
||||
AnalysisResponseBase,
|
||||
@@ -635,5 +637,42 @@ export async function submitFeedback(
|
||||
throw new Error('Something went wrong');
|
||||
}
|
||||
|
||||
if (status === 'COMPLETED') {
|
||||
const [{ data: recipient }, { data: medusaOrderIds }] = await Promise.all([
|
||||
supabase
|
||||
.schema('medreport')
|
||||
.from('accounts')
|
||||
.select('name, last_name, email, preferred_locale')
|
||||
.eq('is_personal_account', true)
|
||||
.eq('primary_owner_user_id', userId)
|
||||
.throwOnError(),
|
||||
supabase
|
||||
.schema('medreport')
|
||||
.from('analysis_orders')
|
||||
.select('medusa_order_id, id')
|
||||
.eq('id', analysisOrderId)
|
||||
.limit(1)
|
||||
.throwOnError(),
|
||||
]);
|
||||
|
||||
if (!recipient?.[0]?.email) {
|
||||
throw new Error('Could not find user email.');
|
||||
}
|
||||
|
||||
if (!medusaOrderIds?.[0]?.id) {
|
||||
throw new Error('Could not retrieve order.');
|
||||
}
|
||||
|
||||
const { preferred_locale, name, last_name, email } = recipient[0];
|
||||
|
||||
await sendDoctorSummaryCompletedEmail(
|
||||
preferred_locale ?? 'et',
|
||||
getFullName(name, last_name),
|
||||
email,
|
||||
medusaOrderIds?.[0]?.medusa_order_id ?? '',
|
||||
medusaOrderIds[0].id,
|
||||
);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user