Skip to content

Commit

Permalink
fix(structures): Current name can to be in the future /6
Browse files Browse the repository at this point in the history
  • Loading branch information
annelhote committed Dec 18, 2024
1 parent bd55410 commit 8731d0a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/api/commons/queries/current-name.query.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,29 @@ 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'] },
] },
},
},
},
},
{
$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'] },
},
},
}],
],
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/api/structures/__tests__/names.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8731d0a

Please sign in to comment.