From 8731d0a9f6f9de572e3747118a08fdc308d8a4aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anne=20L=27H=C3=B4te?= Date: Wed, 18 Dec 2024 16:00:34 +0100 Subject: [PATCH] fix(structures): Current name can to be in the future /6 --- src/api/commons/queries/current-name.query.js | 22 +++++++++++++------ src/api/structures/__tests__/names.test.js | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/api/commons/queries/current-name.query.js b/src/api/commons/queries/current-name.query.js index d31b2930..941f8b17 100644 --- a/src/api/commons/queries/current-name.query.js +++ b/src/api/commons/queries/current-name.query.js @@ -41,7 +41,11 @@ export default [ $reduce: { input: '$filteredNames', initialValue: null, - in: { $cond: [{ $gt: ['$$this.startDate', '$$value.startDate'] }, '$$this', '$$value'] }, + in: { $cond: [ + { $ifNull: ['$$this.startDate', 0] }, + { $cond: [{ $ifNull: ['$$value.startDate', 0] }, {}, '$$value'] }, + { $cond: [{ $gt: ['$$this.startDate', '$$value.startDate'] }, '$$this', '$$value'] }, + ] }, }, }, }, @@ -49,13 +53,17 @@ export default [ { $set: { currentName: { - $ifNull: ['$currentName', { - $reduce: { - input: '$filteredNames', - initialValue: null, - in: { $cond: [{ $gt: ['$$this.createdAt', '$$value.createdAt'] }, '$$this', '$$value'] }, + $cond: [ + { $ifNull: ['$currentName.startDate', 0] }, + '$currentName', + { + $reduce: { + input: '$filteredNames', + initialValue: null, + in: { $cond: [{ $gt: ['$$this.createdAt', '$$value.createdAt'] }, '$$this', '$$value'] }, + }, }, - }], + ], }, }, }, diff --git a/src/api/structures/__tests__/names.test.js b/src/api/structures/__tests__/names.test.js index abd0b639..eb88eb69 100644 --- a/src/api/structures/__tests__/names.test.js +++ b/src/api/structures/__tests__/names.test.js @@ -218,7 +218,7 @@ describe('API > structures > names > currentName', () => { .expect(201); }); - it('returns currentName successfully', async () => { + it('returns currentName as last created name', async () => { const { body } = await global.superapp .get(`/${resource}/${resourceId}`) .set('Authorization', authorization)