Files
medreport_mrb2b/lib/services/mailer.service.ts
2025-08-28 18:03:28 +03:00

78 lines
1.7 KiB
TypeScript

'use server';
import { CompanySubmitData } from '@/lib/types/company';
import { emailSchema } from '@/lib/validations/email.schema';
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,
});
await sendEmail({
subject,
html,
to: recipientEmail,
});
};
export const sendCompanyOfferEmail = async (
data: CompanySubmitData,
language: string,
) => {
const { renderCompanyOfferEmail } = await import('@kit/email-templates');
const { html, subject } = await renderCompanyOfferEmail({
language,
companyData: data,
});
await sendEmail({
subject,
html,
to: process.env.CONTACT_EMAIL || '',
});
};
export const sendEmail = enhanceAction(
async ({ subject, html, to }) => {
const mailer = await getMailer();
const log = await getLogger();
if (!process.env.EMAIL_USER) {
log.error('Sending email failed, as no sender found in env.')
throw new Error('No email user configured');
}
const result = await mailer.sendEmail({
from: process.env.EMAIL_USER,
to,
subject,
html,
});
log.info(
`Sent email with subject "${subject}", result: ${JSON.stringify(result)}`,
);
return {};
},
{
schema: emailSchema,
auth: false,
},
);