fix: extraire l'id de la relation avant de la déréférencer
Déploiement API BETA / build (push) Successful in 2m27s
Déploiement API PROD / build (push) Successful in 2m21s
Déploiement API BETA / deploy (push) Successful in 47s
Déploiement API PROD / deploy (push) Successful in 53s

This commit is contained in:
2026-07-05 19:22:29 +04:00
parent 29bdf72640
commit 128c027af1
2 changed files with 36 additions and 3 deletions
@@ -27,11 +27,33 @@ describe('commentaire afterCreate — notification email', () => {
const {afterCreate} = await loadLifecycles(strapiMock); const {afterCreate} = await loadLifecycles(strapiMock);
await afterCreate({params: {data: {user: 1, parole: 7, contenu: '<img src=x onerror=alert(1)>'}}}); await afterCreate({params: {data: {user: {connect: [{id: 1}]}, parole: {connect: [{id: 7}]}, contenu: '<img src=x onerror=alert(1)>'}}});
expect(emailSend).toHaveBeenCalledTimes(1); expect(emailSend).toHaveBeenCalledTimes(1);
const [payload] = emailSend.mock.calls[0]; const [payload] = emailSend.mock.calls[0];
expect(payload.text).toBe('<img src=x onerror=alert(1)>'); expect(payload.text).toBe('<img src=x onerror=alert(1)>');
expect(payload.html).toBeUndefined(); expect(payload.html).toBeUndefined();
}); });
it('extrait l\'id de la relation quelle que soit sa forme (connect, set, id brut)', async () => {
const paroleFindOne = vi.fn(async ({where}) => ({id: where.id, titre: 'Mon titre'}));
const strapiMock = {
db: {
query: vi.fn(uid => {
if (uid === 'plugin::users-permissions.user') return {findOne: vi.fn(async () => null)};
if (uid === 'api::parole.parole') return {findOne: paroleFindOne};
throw new Error(`unexpected uid: ${uid}`);
})
},
plugins: {email: {services: {email: {send: vi.fn()}}}}
};
const {afterCreate} = await loadLifecycles(strapiMock);
await afterCreate({params: {data: {parole: {set: [{id: 7}]}, contenu: 'ok'}}});
expect(paroleFindOne).toHaveBeenCalledWith({where: {id: 7}});
await afterCreate({params: {data: {parole: 7, contenu: 'ok'}}});
expect(paroleFindOne).toHaveBeenCalledWith({where: {id: 7}});
});
}); });
@@ -2,6 +2,17 @@
const { ApplicationError, NotFoundError } = require('@strapi/utils').errors; const { ApplicationError, NotFoundError } = require('@strapi/utils').errors;
// Le Document Service transforme les relations en { connect: [{id}] } ou
// { set: [{id}] } avant que les hooks bas niveau (beforeCreate/afterCreate)
// ne reçoivent `data` : un id brut n'est plus systématiquement garanti ici.
const idRelasyonAn = valè => {
if (valè == null || typeof valè !== 'object') {
return valè;
}
return valè.connect?.[0]?.id ?? valè.set?.[0]?.id ?? null;
};
const jwennUserEpiId = async userId => { const jwennUserEpiId = async userId => {
if (!userId) { if (!userId) {
return null; return null;
@@ -47,8 +58,8 @@ module.exports = {
}, },
afterCreate: async event => { afterCreate: async event => {
const {data} = event.params; const {data} = event.params;
const user = await jwennUserEpiId(data.user); const user = await jwennUserEpiId(idRelasyonAn(data.user));
const parole = await jwennParoleEpiId(data.parole); const parole = await jwennParoleEpiId(idRelasyonAn(data.parole));
if (!parole) { if (!parole) {
throw new NotFoundError('Texte introuvable.'); throw new NotFoundError('Texte introuvable.');