Skip to content

Commit

Permalink
feat(api): set updatedAt to disabledAt value if membership is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelleBonnemay committed Jan 8, 2025
1 parent 3e64cda commit 8239f75
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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();
Expand All @@ -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,
},
];
Expand All @@ -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();
Expand All @@ -958,6 +964,7 @@ describe('Integration | Team | Infrastructure | Repository | Certification Cente
{
...firstMembership,
disabledAt: now,
updatedAt: now,
updatedByUserId,
},
];
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const buildCertificationCenterMembership = function ({
updatedByUserId = 1,
updatedAt = new Date(),
} = {}) {
return new CertificationCenterMembership({
const certificationCenterMembership = new CertificationCenterMembership({
id,
certificationCenter,
user,
Expand All @@ -33,6 +33,10 @@ const buildCertificationCenterMembership = function ({
updatedByUserId,
updatedAt,
});
if (disabledAt !== undefined) {
certificationCenterMembership.updatedAt = disabledAt;
}
return certificationCenterMembership;
};

export { buildCertificationCenterMembership };

0 comments on commit 8239f75

Please sign in to comment.