diff --git a/src/services/management/__tests__/emailmessages.test.js b/src/services/management/__tests__/emailmessages.test.js index 33080b1..4df08f7 100644 --- a/src/services/management/__tests__/emailmessages.test.js +++ b/src/services/management/__tests__/emailmessages.test.js @@ -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: '

Hi

', @@ -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( diff --git a/src/services/management/emailmessages.js b/src/services/management/emailmessages.js index 5c38cfb..9f9c449 100644 --- a/src/services/management/emailmessages.js +++ b/src/services/management/emailmessages.js @@ -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, {