Skip to content

Commit 163529f

Browse files
committed
fix: support both templateReferenceId and internal db id lookup
1. First try: Filter by templateReferenceId (Document Service) 2. Fallback: Try internal database id via entityService This ensures backward compatibility with frontends that send internal database IDs (5, 117) instead of templateReferenceId.
1 parent eafd8b2 commit 163529f

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

server/src/services/email-designer.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,28 +101,38 @@ module.exports = ({ strapi }) => ({
101101
},
102102

103103
/**
104-
* Get template by numeric templateReferenceId
105-
* The templateReferenceId is a unique integer field used to identify templates
104+
* Get template by numeric ID (supports both templateReferenceId and internal db id)
105+
* First tries templateReferenceId, then falls back to internal database id via entityService
106106
*/
107107
async findById(id) {
108-
strapi.log.info(`[magic-mail] [LOOKUP] Finding template by templateReferenceId: ${id}`);
108+
const numericId = Number(id);
109+
strapi.log.info(`[magic-mail] [LOOKUP] Finding template by numeric ID: ${numericId}`);
109110

110-
// Filter by templateReferenceId (unique integer field)
111-
const results = await strapi.documents(EMAIL_TEMPLATE_UID).findMany({
112-
filters: { templateReferenceId: Number(id) },
111+
// 1. First try: Filter by templateReferenceId (unique integer field)
112+
const byRefId = await strapi.documents(EMAIL_TEMPLATE_UID).findMany({
113+
filters: { templateReferenceId: numericId },
113114
limit: 1,
114115
populate: ['versions'],
115116
});
116117

117-
const result = results.length > 0 ? results[0] : null;
118+
if (byRefId.length > 0) {
119+
strapi.log.info(`[magic-mail] [SUCCESS] Found template by templateReferenceId ${numericId}: documentId=${byRefId[0].documentId}, name="${byRefId[0].name}"`);
120+
return byRefId[0];
121+
}
118122

119-
if (result) {
120-
strapi.log.info(`[magic-mail] [SUCCESS] Found template by templateReferenceId ${id}: documentId=${result.documentId}, name="${result.name}"`);
121-
} else {
122-
strapi.log.warn(`[magic-mail] [WARNING] Template with templateReferenceId ${id} not found`);
123+
// 2. Fallback: Try internal database id via entityService (for backward compatibility)
124+
strapi.log.info(`[magic-mail] [FALLBACK] templateReferenceId not found, trying internal db id: ${numericId}`);
125+
const byInternalId = await strapi.entityService.findOne(EMAIL_TEMPLATE_UID, numericId, {
126+
populate: ['versions'],
127+
});
128+
129+
if (byInternalId) {
130+
strapi.log.info(`[magic-mail] [SUCCESS] Found template by internal id ${numericId}: documentId=${byInternalId.documentId}, name="${byInternalId.name}"`);
131+
return byInternalId;
123132
}
124133

125-
return result;
134+
strapi.log.warn(`[magic-mail] [WARNING] Template with ID ${numericId} not found (tried templateReferenceId and internal id)`);
135+
return null;
126136
},
127137

128138
/**

0 commit comments

Comments
 (0)