Skip to content

Commit

Permalink
[BUGFIX] Modification de la valeur CORE en BDD (PIX-12981).
Browse files Browse the repository at this point in the history
  • Loading branch information
pix-service-auto-merge authored Jun 19, 2024
2 parents 1801dbd + 0558edf commit c687595
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
20 changes: 20 additions & 0 deletions api/db/migrations/20240617134714_update-core-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const TABLE_NAME = 'certification-subscriptions';

const up = async function (knex) {
await knex(TABLE_NAME)
.delete()
.where({ type: 'CORE' })
.whereExists(
knex(`${TABLE_NAME} as cs`)
.where({ type: '"CORE"' })
.whereRaw(`cs."certificationCandidateId" = ??."certificationCandidateId"`, TABLE_NAME),
);

await knex(TABLE_NAME).update({ type: 'CORE' }).where({ type: '"CORE"' });
};

const down = function () {
return true;
};

export { down, up };
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { knex } from '../../../db/knex-database-connection.js';
import { SubscriptionTypes } from '../../../src/certification/shared/domain/models/SubscriptionTypes.js';

const addNonEnrolledCandidatesToSession = async function ({ sessionId, scoCertificationCandidates }) {
const organizationLearnerIds = scoCertificationCandidates.map((candidate) => candidate.organizationLearnerId);
Expand All @@ -21,11 +20,7 @@ const addNonEnrolledCandidatesToSession = async function ({ sessionId, scoCertif

const addedCandidateIds = await knex
.batchInsert('certification-candidates', candidatesToBeEnrolledDTOs)
.returning(knex.raw('id as "certificationCandidateId", \'??\' as type', [SubscriptionTypes.CORE]));

// addedCandidateIds.forEach((candidate) => {
// candidate.type = SubscriptionTypes.CORE;
// });
.returning(knex.raw('id as "certificationCandidateId", \'CORE\' as type'));

await knex.batchInsert('certification-subscriptions', addedCandidateIds);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import _ from 'lodash';

import * as scoCertificationCandidateRepository from '../../../../lib/infrastructure/repositories/sco-certification-candidate-repository.js';
import { databaseBuilder, domainBuilder, expect, knex } from '../../../test-helper.js';
import { databaseBuilder, domainBuilder, expect, knex, sinon } from '../../../test-helper.js';

describe('Integration | Repository | SCOCertificationCandidate', function () {
describe('#addNonEnrolledCandidatesToSession', function () {
let sessionId;
let clock;
const now = new Date('2024-06-17T00:00:00Z');

beforeEach(function () {
// given
clock = sinon.useFakeTimers({ now, toFake: ['Date'] });

sessionId = databaseBuilder.factory.buildSession().id;

return databaseBuilder.commit();
});

afterEach(function () {
clock.restore();
});

it('adds only the unenrolled candidates', async function () {
// given
const organizationLearnerId1 = databaseBuilder.factory.buildOrganizationLearner().id;
Expand Down Expand Up @@ -89,7 +97,10 @@ describe('Integration | Repository | SCOCertificationCandidate', function () {
sessionId,
organizationLearnerId: organizationLearnerId1,
});
databaseBuilder.factory.buildCoreSubscription({ certificationCandidateId: scoCandidateAlreadySaved1.id });
databaseBuilder.factory.buildCoreSubscription({
certificationCandidateId: scoCandidateAlreadySaved1.id,
createdAt: new Date('2024-01-01'),
});
const organizationLearnerId2 = databaseBuilder.factory.buildOrganizationLearner().id;
const organizationLearnerId3 = databaseBuilder.factory.buildOrganizationLearner().id;
await databaseBuilder.commit();
Expand Down Expand Up @@ -123,8 +134,20 @@ describe('Integration | Repository | SCOCertificationCandidate', function () {
});

// then
const { count: subscriptionsCount } = await knex('certification-subscriptions').count().first();
expect(subscriptionsCount).to.equal(3);
const subscriptions = await knex('certification-subscriptions');
expect(subscriptions.length).to.equal(3);
sinon.assert.match(subscriptions[0], {
certificationCandidateId: scoCandidateAlreadySaved1.id,
complementaryCertificationId: null,
createdAt: new Date('2024-01-01'),
type: 'CORE',
});
sinon.assert.match(subscriptions[1], {
certificationCandidateId: sinon.match.number,
complementaryCertificationId: null,
createdAt: sinon.match.date,
type: 'CORE',
});
});

it('does nothing when no candidate is given', async function () {
Expand Down

0 comments on commit c687595

Please sign in to comment.