Refactor email attachment handling and improve error management
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good

This commit refines the `resolveEmailAttachments` function by enhancing error handling for unknown object types and removing unnecessary console logs. It also updates the test suite to mock additional database functions and ensure proper object retrieval during email message processing, improving the robustness of email management features.
This commit is contained in:
Tom Butcher 2026-09-12 21:52:21 +01:00
parent 624a2aefdd
commit 1d473cc755
2 changed files with 18 additions and 11 deletions

View File

@ -30,7 +30,10 @@ jest.unstable_mockModule('../../../database/schemas/misc/usernotifier.schema.js'
userNotifierModel: { create: notifierCreate },
}));
jest.unstable_mockModule('../../../database/database.js', () => ({
aggregateRollups: jest.fn(),
aggregateRollupsHistory: jest.fn(),
createFileThumbnails: jest.fn(),
deleteObject: jest.fn(),
editObject: jest.fn(),
getObject: jest.fn(),
getModelHistory: jest.fn(),
@ -95,7 +98,7 @@ const {
newEmailMessageRouteHandler,
processEmailMessage,
} = await import('../emailmessages.js');
const { createFileThumbnails, editObject, listObjectsByProperties, newObject } =
const { createFileThumbnails, editObject, getObject, listObjectsByProperties, newObject } =
await import('../../../database/database.js');
const { downloadFile, uploadFile } = await import('../../../database/ceph.js');
const { distributeNew } = await import('../../../utils.js');
@ -215,10 +218,12 @@ describe('email message creation', () => {
it('renders document template attachments into files before sending', async () => {
const user = { _id: 'user-1' };
const pdfBytes = Buffer.from('pdf-bytes');
const invoice = { _id: 'object-1', name: 'INV-1' };
emailMessageModel.findById.mockReturnValue({
lean: jest.fn().mockResolvedValue({
_id: 'message-1',
emailTemplate: 'template-1',
objectType: 'invoice',
object: 'object-1',
recipientEmail: 'client@example.com',
}),
@ -233,6 +238,7 @@ describe('email message creation', () => {
},
],
});
getObject.mockResolvedValue(invoice);
templateManager.renderEmailTemplate.mockResolvedValue({
subject: 'Invoice',
html: '<p>Hi</p>',
@ -253,10 +259,15 @@ describe('email message creation', () => {
user
);
expect(getObject).toHaveBeenCalledWith(
expect.objectContaining({
id: 'object-1',
})
);
expect(templateManager.renderDownload).toHaveBeenCalledWith(
'doc-1',
undefined,
'object-1',
invoice,
'pdf'
);
expect(newObject).toHaveBeenCalledWith(

View File

@ -162,25 +162,24 @@ async function resolveEmailAttachments(emailMessage, user) {
for (const attachment of template.attachments || []) {
if (attachment.type === 'documentTemplate') {
const templateId = attachment.file?._id ?? attachment.file;
console.log(templateId);
if (!templateId) throw new Error('Document template attachment is missing a file.');
const model = getModelByName(emailMessage.objectType);
const modelEntry = getModelByName(emailMessage.objectType);
if (!modelEntry?.model) {
throw new Error(`Unknown object type for email attachment: ${emailMessage.objectType}`);
}
const object = await getObject({
model: getModelByName(emailMessage.objectType).model,
model: modelEntry.model,
id: emailMessage.object?._id || emailMessage.object,
});
console.log(object);
const rendered = await templateManager.renderDownload(
templateId,
undefined,
object,
attachment.fileType || 'pdf'
);
console.log(rendered);
if (rendered?.error) throw new Error(rendered.error);
const buffers = rendered.buffers || [];
if (!buffers.length) throw new Error('Document template attachment produced no files.');
console.log(buffers);
for (let index = 0; index < buffers.length; index += 1) {
const buffer = Buffer.isBuffer(buffers[index])
? buffers[index]
@ -275,9 +274,6 @@ export const processEmailMessage = async (id, account, user) => {
const { fileIds, mailAttachments } = await resolveEmailAttachments(emailMessage, user);
console.log(fileIds);
console.log(mailAttachments);
await updateEmailMessage(
id,
{