From 8239f757dbc28db4ce8a0fed8821aeab9eceb02d Mon Sep 17 00:00:00 2001 From: Emmanuelle Bonnemay Date: Wed, 8 Jan 2025 15:11:46 +0100 Subject: [PATCH] feat(api): set updatedAt to disabledAt value if membership is disabled --- .../build-certification-center-membership.js | 1 + ...tification-center-membership.repository.js | 5 +-- ...ation-center-membership.repository.test.js | 33 +++++++++++-------- .../build-certification-center-membership.js | 6 +++- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/api/db/database-builder/factory/build-certification-center-membership.js b/api/db/database-builder/factory/build-certification-center-membership.js index 846b39c1101..e0e0e4b74fe 100644 --- a/api/db/database-builder/factory/build-certification-center-membership.js +++ b/api/db/database-builder/factory/build-certification-center-membership.js @@ -17,6 +17,7 @@ const buildCertificationCenterMembership = function ({ } = {}) { userId = _.isUndefined(userId) ? buildUser().id : userId; certificationCenterId = _.isUndefined(certificationCenterId) ? buildCertificationCenter().id : certificationCenterId; + updatedAt = disabledAt !== undefined ? disabledAt : updatedAt; const values = { id, diff --git a/api/src/team/infrastructure/repositories/certification-center-membership.repository.js b/api/src/team/infrastructure/repositories/certification-center-membership.repository.js index 49c208d307d..efff5ad8f9c 100644 --- a/api/src/team/infrastructure/repositories/certification-center-membership.repository.js +++ b/api/src/team/infrastructure/repositories/certification-center-membership.repository.js @@ -219,7 +219,7 @@ const disableById = async function ({ certificationCenterMembershipId, updatedBy const now = new Date(); const result = await knex(CERTIFICATION_CENTER_MEMBERSHIP_TABLE_NAME) .where({ id: certificationCenterMembershipId }) - .update({ disabledAt: now, updatedByUserId }) + .update({ disabledAt: now, updatedAt: now, updatedByUserId }) .returning('*'); if (result.length === 0) { @@ -268,11 +268,12 @@ const getRefererByCertificationCenterId = async function ({ certificationCenterI }; const disableMembershipsByUserId = async function ({ userId, updatedByUserId }) { + const now = new Date(); const knexConn = DomainTransaction.getConnection(); await knexConn(CERTIFICATION_CENTER_MEMBERSHIP_TABLE_NAME) .whereNull('disabledAt') .andWhere({ userId }) - .update({ disabledAt: new Date(), updatedByUserId }); + .update({ disabledAt: now, updatedAt: now, updatedByUserId }); }; const update = async function (certificationCenterMembership) { diff --git a/api/tests/team/integration/infrastructure/repositories/certification-center-membership.repository.test.js b/api/tests/team/integration/infrastructure/repositories/certification-center-membership.repository.test.js index 5a52efae0ff..9414cc4d720 100644 --- a/api/tests/team/integration/infrastructure/repositories/certification-center-membership.repository.test.js +++ b/api/tests/team/integration/infrastructure/repositories/certification-center-membership.repository.test.js @@ -859,7 +859,8 @@ describe('Integration | Team | Infrastructure | Repository | Certification Cente }); describe('#disableMembershipsByUserId', function () { - const now = new Date('2022-12-05'); + const creationDate = new Date('2022-12-05'); + const now = new Date('2024-10-04'); let clock; beforeEach(function () { @@ -880,17 +881,20 @@ describe('Integration | Team | Infrastructure | Repository | Certification Cente const firstMembership = databaseBuilder.factory.buildCertificationCenterMembership({ userId, certificationCenterId: firstCertificationCenterId, - createdAt: now, + createdAt: creationDate, + updatedAt: creationDate, }); const secondMembership = databaseBuilder.factory.buildCertificationCenterMembership({ userId, certificationCenterId: secondCertificationCenterId, - createdAt: now, + createdAt: creationDate, + updatedAt: creationDate, }); databaseBuilder.factory.buildCertificationCenterMembership({ userId: databaseBuilder.factory.buildUser().id, certificationCenterId: secondCertificationCenterId, - createdAt: now, + createdAt: creationDate, + updatedAt: creationDate, }); await databaseBuilder.commit(); @@ -902,14 +906,16 @@ describe('Integration | Team | Infrastructure | Repository | Certification Cente const expectedMemberships = [ { ...firstMembership, - createdAt: now, + createdAt: creationDate, disabledAt: now, + updatedAt: now, updatedByUserId, }, { ...secondMembership, - createdAt: now, + createdAt: creationDate, disabledAt: now, + updatedAt: now, updatedByUserId, }, ]; @@ -930,19 +936,19 @@ describe('Integration | Team | Infrastructure | Repository | Certification Cente const firstMembership = databaseBuilder.factory.buildCertificationCenterMembership({ userId, certificationCenterId: firstCertificationCenterId, - createdAt: now, + createdAt: creationDate, }); const secondMembership = databaseBuilder.factory.buildCertificationCenterMembership({ userId, updatedByUserId, certificationCenterId: secondCertificationCenterId, - createdAt: now, - disabledAt: now, + createdAt: creationDate, + disabledAt: creationDate, }); databaseBuilder.factory.buildCertificationCenterMembership({ userId: databaseBuilder.factory.buildUser().id, certificationCenterId: secondCertificationCenterId, - createdAt: now, + createdAt: creationDate, }); await databaseBuilder.commit(); @@ -958,6 +964,7 @@ describe('Integration | Team | Infrastructure | Repository | Certification Cente { ...firstMembership, disabledAt: now, + updatedAt: now, updatedByUserId, }, ]; @@ -981,20 +988,20 @@ describe('Integration | Team | Infrastructure | Repository | Certification Cente userId, updatedByUserId, certificationCenterId: firstCertificationCenterId, - createdAt: now, + createdAt: creationDate, disabledAt: now, }); databaseBuilder.factory.buildCertificationCenterMembership({ userId, updatedByUserId, certificationCenterId: secondCertificationCenterId, - createdAt: now, + createdAt: creationDate, disabledAt: now, }); databaseBuilder.factory.buildCertificationCenterMembership({ userId: databaseBuilder.factory.buildUser().id, certificationCenterId: secondCertificationCenterId, - createdAt: now, + createdAt: creationDate, }); await databaseBuilder.commit(); diff --git a/api/tests/tooling/domain-builder/factory/build-certification-center-membership.js b/api/tests/tooling/domain-builder/factory/build-certification-center-membership.js index 867fe08f9b6..6b7e9842226 100644 --- a/api/tests/tooling/domain-builder/factory/build-certification-center-membership.js +++ b/api/tests/tooling/domain-builder/factory/build-certification-center-membership.js @@ -22,7 +22,7 @@ const buildCertificationCenterMembership = function ({ updatedByUserId = 1, updatedAt = new Date(), } = {}) { - return new CertificationCenterMembership({ + const certificationCenterMembership = new CertificationCenterMembership({ id, certificationCenter, user, @@ -33,6 +33,10 @@ const buildCertificationCenterMembership = function ({ updatedByUserId, updatedAt, }); + if (disabledAt !== undefined) { + certificationCenterMembership.updatedAt = disabledAt; + } + return certificationCenterMembership; }; export { buildCertificationCenterMembership };