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)