Skip to content

Commit

Permalink
fix(structures): Current name can to be in the future /7
Browse files Browse the repository at this point in the history
  • Loading branch information
annelhote committed Dec 24, 2024
1 parent 1af55fb commit 15466fa
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/api/commons/queries/current-name.query.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export default [
input: '$filteredNames',
initialValue: null,
in: { $cond: [
{ $ifNull: ['$$this.startDate', 0] },
{ $cond: [{ $ifNull: ['$$value.startDate', 0] }, {}, '$$value'] },
{ $eq: ['$$this.startDate', null] },
{ $cond: [{ $eq: ['$$value.startDate', null] }, {}, '$$value'] },
{ $cond: [{ $gt: ['$$this.startDate', '$$value.startDate'] }, '$$this', '$$value'] },
] },
},
Expand Down
108 changes: 96 additions & 12 deletions src/api/structures/__tests__/names.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,39 +190,123 @@ describe('API > structures > names > list', () => {
});

describe('API > structures > names > currentName', () => {
beforeAll(async () => {
const { startDate, ...structure } = structureName;
it('should return the last created name if multiple names without startDate', async () => {
const response = await global.superapp
.post(`/${resource}`)
.set('Authorization', authorization)
.send({
structureStatus: 'active',
creationDate: '2021-02',
usualName: 'Université',
usualName: 'name_01_01',
}).expect(201);
resourceId = response.body.id;
await global.superapp
.post(`/${resource}/${resourceId}/${subresource}/`)
.post(`/${resource}/${resourceId}/names`)
.set('Authorization', authorization)
.send({ ...structure })
.send({ usualName: 'name_01_02' })
.expect(201);

const { body } = await global.superapp
.get(`/${resource}/${resourceId}`)
.set('Authorization', authorization)
.expect(200);
expect(body.currentName.usualName).toBe('name_01_02');
});

it('should return the name with a startDate if multiple names and one has a startDate', async () => {
const response = await global.superapp
.post(`/${resource}`)
.set('Authorization', authorization)
.send({
structureStatus: 'active',
usualName: 'name_02_01',
}).expect(201);
resourceId = response.body.id;
await global.superapp
.post(`/${resource}/${resourceId}/${subresource}/`)
.post(`/${resource}/${resourceId}/names`)
.set('Authorization', authorization)
.send({ startDate: '2020-10-08', usualName: 'name_02_02' })
.expect(201);

const { body } = await global.superapp
.get(`/${resource}/${resourceId}`)
.set('Authorization', authorization)
.expect(200);
expect(body.currentName.usualName).toBe('name_02_02');
});

it('should return the most recent startDate', async () => {
const response = await global.superapp
.post(`/${resource}`)
.set('Authorization', authorization)
.send({ ...structure, usualName: 'string2' })
.send({
structureStatus: 'active',
usualName: 'name_03_01',
}).expect(201);
resourceId = response.body.id;
await global.superapp
.post(`/${resource}/${resourceId}/names`)
.set('Authorization', authorization)
.send({ startDate: '2020-10-08', usualName: 'name_03_02' })
.expect(201);
await global.superapp
.post(`/${resource}/${resourceId}/${subresource}/`)
.post(`/${resource}/${resourceId}/names`)
.set('Authorization', authorization)
.send({ ...structure, usualName: 'string3' })
.send({ startDate: '2012-02-05', usualName: 'name_03_03' })
.expect(201);

const { body } = await global.superapp
.get(`/${resource}/${resourceId}`)
.set('Authorization', authorization)
.expect(200);
expect(body.currentName.usualName).toBe('name_03_02');
});

it('returns currentName as last created name', async () => {
it('should fill partial date with YYY-01-01 and then compare dates and return the most recent startDate', async () => {
const response = await global.superapp
.post(`/${resource}`)
.set('Authorization', authorization)
.send({
structureStatus: 'active',
usualName: 'name_04_01',
}).expect(201);
resourceId = response.body.id;
await global.superapp
.post(`/${resource}/${resourceId}/names`)
.set('Authorization', authorization)
.send({ startDate: '2020-10-08', usualName: 'name_04_02' })
.expect(201);
await global.superapp
.post(`/${resource}/${resourceId}/names`)
.set('Authorization', authorization)
.send({ startDate: '2020', usualName: 'name_04_03' })
.expect(201);

const { body } = await global.superapp
.get(`/${resource}/${resourceId}`)
.set('Authorization', authorization)
.expect(200);
expect(body.currentName.usualName).toBe('name_04_02');
});

it('should return empty startDate rather than future ones', async () => {
const response = await global.superapp
.post(`/${resource}`)
.set('Authorization', authorization)
.send({
structureStatus: 'active',
usualName: 'name_05_01',
}).expect(201);
resourceId = response.body.id;
await global.superapp
.post(`/${resource}/${resourceId}/names`)
.set('Authorization', authorization)
.send({ startDate: '2042-02-05', usualName: 'name_05_02' })
.expect(201);

const { body } = await global.superapp
.get(`/${resource}/${resourceId}`)
.set('Authorization', authorization)
.expect(200);
expect(body.currentName.usualName).toBe('string3');
expect(body.currentName.usualName).toBe('name_05_01');
});
});

0 comments on commit 15466fa

Please sign in to comment.