-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: api: migrate publish related methods from repository
- Loading branch information
1 parent
5fa5c71
commit b9e6029
Showing
7 changed files
with
190 additions
and
143 deletions.
There are no files selected for viewing
38 changes: 1 addition & 37 deletions
38
api/lib/infrastructure/repositories/certification-repository.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,7 @@ | ||
import { knex } from '../../../db/knex-database-connection.js'; | ||
|
||
const publishCertificationCourses = async function (certificationStatuses) { | ||
const certificationDataToUpdate = certificationStatuses.map(({ certificationCourseId }) => ({ | ||
id: certificationCourseId, | ||
isPublished: true, | ||
updatedAt: new Date(), | ||
version: -1, // Version number used to meet requirements regarding the version column non-null constraint in the insert request below | ||
})); | ||
|
||
// Trick to .batchUpdate(), which does not exist in knex per say | ||
await knex('certification-courses') | ||
.insert(certificationDataToUpdate) | ||
.onConflict('id') | ||
.merge(['isPublished', 'updatedAt']); | ||
}; | ||
|
||
const getStatusesBySessionId = async function (sessionId) { | ||
return knex('certification-courses') | ||
.select({ | ||
certificationCourseId: 'certification-courses.id', | ||
isCancelled: 'certification-courses.isCancelled', | ||
pixCertificationStatus: 'assessment-results.status', | ||
}) | ||
.where('certification-courses.sessionId', sessionId) | ||
.join('assessments', 'assessments.certificationCourseId', 'certification-courses.id') | ||
.leftJoin( | ||
'certification-courses-last-assessment-results', | ||
'certification-courses.id', | ||
'certification-courses-last-assessment-results.certificationCourseId', | ||
) | ||
.leftJoin( | ||
'assessment-results', | ||
'assessment-results.id', | ||
'certification-courses-last-assessment-results.lastAssessmentResultId', | ||
); | ||
}; | ||
|
||
const unpublishCertificationCoursesBySessionId = async function (sessionId) { | ||
await knex('certification-courses').where({ sessionId }).update({ isPublished: false, updatedAt: new Date() }); | ||
}; | ||
|
||
export { getStatusesBySessionId, publishCertificationCourses, unpublishCertificationCoursesBySessionId }; | ||
export { unpublishCertificationCoursesBySessionId }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
api/src/certification/session-management/domain/usecases/publish-session.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
.../certification/session-management/infrastructure/repositories/certification-repository.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { knex } from '../../../../../db/knex-database-connection.js'; | ||
|
||
const getStatusesBySessionId = async function (sessionId) { | ||
return knex('certification-courses') | ||
.select({ | ||
certificationCourseId: 'certification-courses.id', | ||
isCancelled: 'certification-courses.isCancelled', | ||
pixCertificationStatus: 'assessment-results.status', | ||
}) | ||
.where('certification-courses.sessionId', sessionId) | ||
.join('assessments', 'assessments.certificationCourseId', 'certification-courses.id') | ||
.leftJoin( | ||
'certification-courses-last-assessment-results', | ||
'certification-courses.id', | ||
'certification-courses-last-assessment-results.certificationCourseId', | ||
) | ||
.leftJoin( | ||
'assessment-results', | ||
'assessment-results.id', | ||
'certification-courses-last-assessment-results.lastAssessmentResultId', | ||
); | ||
}; | ||
|
||
const publishCertificationCourses = async function (certificationStatuses) { | ||
const certificationDataToUpdate = certificationStatuses.map(({ certificationCourseId }) => ({ | ||
id: certificationCourseId, | ||
isPublished: true, | ||
updatedAt: new Date(), | ||
version: -1, // Version number used to meet requirements regarding the version column non-null constraint in the insert request below | ||
})); | ||
|
||
// Trick to .batchUpdate(), which does not exist in knex per say | ||
await knex('certification-courses') | ||
.insert(certificationDataToUpdate) | ||
.onConflict('id') | ||
.merge(['isPublished', 'updatedAt']); | ||
}; | ||
|
||
export { getStatusesBySessionId, publishCertificationCourses }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...ssion-management/integration/infrastructure/repositories/certification-repository_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import * as certificationRepository from '../../../../../../src/certification/session-management/infrastructure/repositories/certification-repository.js'; | ||
import { AssessmentResult, status } from '../../../../../../src/shared/domain/models/AssessmentResult.js'; | ||
import { databaseBuilder, expect, knex, sinon } from '../../../../../test-helper.js'; | ||
|
||
describe('Integration | Repository | Certification', function () { | ||
describe('#getStatusesBySessionId', function () { | ||
it('should get status information', async function () { | ||
// given | ||
const sessionId = 200; | ||
databaseBuilder.factory.buildSession({ id: sessionId + 1 }); | ||
databaseBuilder.factory.buildSession({ id: sessionId }); | ||
_buildValidatedCertification({ id: 1, sessionId, isPublished: false }); | ||
_buildValidatedCertification({ id: 2, sessionId: sessionId + 1, isPublished: false }); | ||
_buildRejectedCertification({ id: 3, sessionId, isPublished: false }); | ||
_buildCancelledCertification({ id: 4, sessionId, isPublished: false }); | ||
await databaseBuilder.commit(); | ||
|
||
// when | ||
const statuses = await certificationRepository.getStatusesBySessionId(sessionId); | ||
|
||
// then | ||
|
||
expect(statuses).to.have.length(3); | ||
expect(statuses).to.deep.equal([ | ||
{ | ||
certificationCourseId: 1, | ||
isCancelled: false, | ||
pixCertificationStatus: AssessmentResult.status.VALIDATED, | ||
}, | ||
{ | ||
certificationCourseId: 3, | ||
isCancelled: false, | ||
pixCertificationStatus: AssessmentResult.status.REJECTED, | ||
}, | ||
{ | ||
certificationCourseId: 4, | ||
isCancelled: true, | ||
pixCertificationStatus: null, | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('#publishCertificationCoursesBySessionId', function () { | ||
const sessionId = 200; | ||
|
||
beforeEach(function () { | ||
databaseBuilder.factory.buildSession({ id: sessionId }); | ||
_buildValidatedCertification({ id: 1, sessionId, isPublished: false }); | ||
_buildRejectedCertification({ id: 2, sessionId, isPublished: false }); | ||
_buildCancelledCertification({ id: 3, sessionId, isPublished: false }); | ||
return databaseBuilder.commit(); | ||
}); | ||
|
||
context( | ||
'when all certification latest assessment result are validated, rejected or certification is cancelled', | ||
function () { | ||
let clock; | ||
const now = new Date('2022-12-25'); | ||
|
||
beforeEach(function () { | ||
clock = sinon.useFakeTimers({ | ||
now, | ||
toFake: ['Date'], | ||
}); | ||
}); | ||
|
||
afterEach(async function () { | ||
clock.restore(); | ||
}); | ||
|
||
it('should set certifications as published within the session and update pixCertificationStatus according to assessment result status', async function () { | ||
// when | ||
await certificationRepository.publishCertificationCourses([ | ||
{ certificationCourseId: 1 }, | ||
{ certificationCourseId: 2 }, | ||
{ certificationCourseId: 3 }, | ||
]); | ||
|
||
// then | ||
const certifications = await knex('certification-courses') | ||
.select('id', 'isPublished', 'updatedAt', 'version') | ||
.where({ sessionId }); | ||
expect(certifications).to.deep.equal([ | ||
{ | ||
id: 1, | ||
isPublished: true, | ||
updatedAt: now, | ||
version: 2, | ||
}, | ||
{ | ||
id: 2, | ||
isPublished: true, | ||
updatedAt: now, | ||
version: 2, | ||
}, | ||
{ | ||
id: 3, | ||
isPublished: true, | ||
updatedAt: now, | ||
version: 2, | ||
}, | ||
]); | ||
}); | ||
}, | ||
); | ||
}); | ||
}); | ||
|
||
function _buildValidatedCertification({ id, sessionId, isPublished }) { | ||
_buildCertification({ id, sessionId, isPublished, status: status.VALIDATED }); | ||
} | ||
|
||
function _buildRejectedCertification({ id, sessionId, isPublished }) { | ||
_buildCertification({ id, sessionId, isPublished, status: status.REJECTED }); | ||
} | ||
|
||
function _buildCancelledCertification({ id, sessionId, isPublished }) { | ||
_buildCertification({ id, sessionId, isPublished, isCancelled: true, status: null }); | ||
} | ||
|
||
function _buildCertification({ id, sessionId, status, isPublished, isCancelled = false }) { | ||
databaseBuilder.factory.buildCertificationCourse({ id, sessionId, isPublished, isCancelled }); | ||
databaseBuilder.factory.buildAssessment({ id, certificationCourseId: id }); | ||
if (status) { | ||
// not the latest | ||
databaseBuilder.factory.buildAssessmentResult({ | ||
assessmentId: id, | ||
createdAt: new Date('2020-01-01'), | ||
status, | ||
}); | ||
// the latest | ||
databaseBuilder.factory.buildAssessmentResult.last({ | ||
certificationCourseId: id, | ||
assessmentId: id, | ||
createdAt: new Date('2021-01-01'), | ||
status, | ||
}); | ||
} | ||
} |
Oops, something went wrong.