From 1ad7385e4b79adfbd217c5ba6b83c4e88e835824 Mon Sep 17 00:00:00 2001 From: omar-bear Date: Thu, 16 May 2024 11:13:19 +0100 Subject: [PATCH] fix develop clever chaging template / mails --- packages/server/mailing/mailing.schema.js | 16 ++++++++++++++-- packages/server/template/template.schema.js | 18 +++++++++++++----- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/packages/server/mailing/mailing.schema.js b/packages/server/mailing/mailing.schema.js index e3dcc3bd..8211a994 100644 --- a/packages/server/mailing/mailing.schema.js +++ b/packages/server/mailing/mailing.schema.js @@ -256,7 +256,6 @@ MailingSchema.statics.findForApiWithPagination = async function findForApiWithPa author: 1, userId: '$_user', tags: 1, - hasHtmlPreview: { $ne: ['$previewHtml', null] }, _workspace: 1, espIds: 1, updatedAt: 1, @@ -268,7 +267,20 @@ MailingSchema.statics.findForApiWithPagination = async function findForApiWithPa const { docs, ...restPaginationProperties } = result; - const convertedResultMailingDocs = docs?.map( + const ids = docs.map((doc) => doc._id); + const mailingsWithHtmlPreview = await this.find( + { _id: { $in: ids }, previewHtml: { $exists: true } }, + { _id: 1 } + ).lean(); + const mailingsWithHtmlPreviewSet = new Set( + mailingsWithHtmlPreview.map((mailing) => mailing._id.toString()) + ); + const finalDocs = docs.map((doc) => ({ + ...doc, + hasHtmlPreview: mailingsWithHtmlPreviewSet.has(doc._id.toString()), + })); + + const convertedResultMailingDocs = finalDocs.map( ({ wireframe, author, ...doc }) => ({ templateName: wireframe, userName: author, diff --git a/packages/server/template/template.schema.js b/packages/server/template/template.schema.js index e54a1247..bc37aec3 100644 --- a/packages/server/template/template.schema.js +++ b/packages/server/template/template.schema.js @@ -110,19 +110,27 @@ TemplateSchema.statics.findForApi = async function findForApi(query = {}) { updatedAt: 1, _company: 1, assets: 1, - markup: 1, }) .populate({ path: '_company', select: 'id name' }) .sort({ name: 1 }) .lean(); + // Second query to check existence of 'markup' for each template + const ids = templates.map((template) => template._id); + // find markup exist or not without charging the markup + const templatesWithMarkup = await this.find( + { _id: { $in: ids }, markup: { $exists: true } }, + { _id: 1 } + ).lean(); - const finalTemplates = templates.map(({ assets, markup, ...template }) => ({ + const templatesWithMarkupSet = new Set( + templatesWithMarkup.map((t) => t._id.toString()) + ); + + const finalTemplates = templates.map(({ assets, ...template }) => ({ ...template, - hasMarkup: markup !== null, + hasMarkup: templatesWithMarkupSet.has(template._id.toString()), coverImage: JSON.parse(assets)?.['_full.png'] || null, })); - return finalTemplates; }; - module.exports = TemplateSchema;