Skip to content

Commit

Permalink
fix develop clever chaging template / mails
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-bear committed May 16, 2024
1 parent 7bfe445 commit 1ad7385
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
16 changes: 14 additions & 2 deletions packages/server/mailing/mailing.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
18 changes: 13 additions & 5 deletions packages/server/template/template.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 1ad7385

Please sign in to comment.