MED-88: add doctor email notifications (#65)
* MED-88: add doctor email notifications * add logging, send open jobs notification on partial analysis response * update permissions * fix import, permissions * casing, let email be null * unused import
This commit is contained in:
@@ -1,50 +1,41 @@
|
||||
'use server';
|
||||
|
||||
import { CompanySubmitData } from '@/lib/types/company';
|
||||
import { emailSchema } from '@/lib/validations/email.schema';
|
||||
import { toArray } from '@/lib/utils';
|
||||
|
||||
import { renderDoctorSummaryReceivedEmail } from '@kit/email-templates';
|
||||
import { getMailer } from '@kit/mailers';
|
||||
import { enhanceAction } from '@kit/next/actions';
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
|
||||
export const sendDoctorSummaryCompletedEmail = async (
|
||||
language: string,
|
||||
recipientName: string,
|
||||
recipientEmail: string,
|
||||
orderNr: string,
|
||||
orderId: number,
|
||||
) => {
|
||||
const { html, subject } = await renderDoctorSummaryReceivedEmail({
|
||||
language,
|
||||
recipientName,
|
||||
recipientEmail,
|
||||
orderNr,
|
||||
orderId,
|
||||
});
|
||||
import { emailSchema } from '~/lib/validations/email.schema';
|
||||
|
||||
await sendEmail({
|
||||
subject,
|
||||
html,
|
||||
to: recipientEmail,
|
||||
});
|
||||
type EmailTemplate = {
|
||||
html: string;
|
||||
subject: string;
|
||||
};
|
||||
|
||||
export const sendCompanyOfferEmail = async (
|
||||
data: CompanySubmitData,
|
||||
language: string,
|
||||
) => {
|
||||
const { renderCompanyOfferEmail } = await import('@kit/email-templates');
|
||||
const { html, subject } = await renderCompanyOfferEmail({
|
||||
language,
|
||||
companyData: data,
|
||||
});
|
||||
type EmailRenderer<T = any> = (params: T) => Promise<EmailTemplate>;
|
||||
|
||||
await sendEmail({
|
||||
subject,
|
||||
html,
|
||||
to: process.env.CONTACT_EMAIL || '',
|
||||
});
|
||||
export const sendEmailFromTemplate = async <T>(
|
||||
renderer: EmailRenderer<T>,
|
||||
templateParams: T,
|
||||
recipients: string | string[],
|
||||
) => {
|
||||
const { html, subject } = await renderer(templateParams);
|
||||
|
||||
const recipientsArray = toArray(recipients);
|
||||
if (!recipientsArray.length) {
|
||||
throw new Error('No valid email recipients provided');
|
||||
}
|
||||
|
||||
const emailPromises = recipientsArray.map((email) =>
|
||||
sendEmail({
|
||||
subject,
|
||||
html,
|
||||
to: email,
|
||||
}),
|
||||
);
|
||||
|
||||
await Promise.all(emailPromises);
|
||||
};
|
||||
|
||||
export const sendEmail = enhanceAction(
|
||||
@@ -53,7 +44,7 @@ export const sendEmail = enhanceAction(
|
||||
const log = await getLogger();
|
||||
|
||||
if (!process.env.EMAIL_USER) {
|
||||
log.error('Sending email failed, as no sender found in env.')
|
||||
log.error('Sending email failed, as no sender was found in env.');
|
||||
throw new Error('No email user configured');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user