Skip to content

Commit

Permalink
feat(api): add get ongoing or live alert for challenge method
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrecoin authored Nov 13, 2024
1 parent 5ecabfb commit 541031d
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ const getOngoingByChallengeIdAndAssessmentId = async ({ challengeId, assessmentI
return _toDomain(certificationChallengeLiveAlertDto);
};

const getOngoingOrValidatedByChallengeIdAndAssessmentId = async ({ challengeId, assessmentId }) => {
const certificationChallengeLiveAlertDto = await knex('certification-challenge-live-alerts')
.where({
'certification-challenge-live-alerts.challengeId': challengeId,
'certification-challenge-live-alerts.assessmentId': assessmentId,
'certification-challenge-live-alerts.status': CertificationChallengeLiveAlertStatus.ONGOING,
})
.orWhere({
'certification-challenge-live-alerts.challengeId': challengeId,
'certification-challenge-live-alerts.assessmentId': assessmentId,
'certification-challenge-live-alerts.status': CertificationChallengeLiveAlertStatus.VALIDATED,
})
.first();

return _toDomain(certificationChallengeLiveAlertDto);
};

const _toDomain = (certificationChallengeLiveAlertDto) => {
if (!certificationChallengeLiveAlertDto) {
return null;
Expand All @@ -73,5 +90,6 @@ export {
getLiveAlertValidatedChallengeIdsByAssessmentId,
getOngoingByChallengeIdAndAssessmentId,
getOngoingBySessionIdAndUserId,
getOngoingOrValidatedByChallengeIdAndAssessmentId,
save,
};
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,91 @@ describe('Integration | Repository | Certification Challenge Live Alert', functi
});
});
});

describe('getOngoingOrValidatedByChallengeIdAndAssessmentId', function () {
const challengeId = 'rec123';
const assessmentId = 456;

describe('when there are no matching live alerts', function () {
it('should return null', async function () {
// given / when
const liveAlert =
await certificationChallengeLiveAlertRepository.getOngoingOrValidatedByChallengeIdAndAssessmentId({
challengeId,
assessmentId,
});

// then
expect(liveAlert).to.be.null;
});
});

describe('when there is a matching validated live alert', function () {
it('should return the live alert', async function () {
// given
const certificationCourse = databaseBuilder.factory.buildCertificationCourse();

const assessment = databaseBuilder.factory.buildAssessment({
certificationCourseId: certificationCourse.id,
userId: certificationCourse.userId,
});

databaseBuilder.factory.buildCertificationChallengeLiveAlert({
assessmentId: assessment.id,
status: CertificationChallengeLiveAlertStatus.DISMISSED,
});

const certificationChallengeLiveAlert = databaseBuilder.factory.buildCertificationChallengeLiveAlert({
assessmentId: assessment.id,
status: CertificationChallengeLiveAlertStatus.VALIDATED,
});

await databaseBuilder.commit();

// when
const liveAlert =
await certificationChallengeLiveAlertRepository.getOngoingOrValidatedByChallengeIdAndAssessmentId({
challengeId: certificationChallengeLiveAlert.challengeId,
assessmentId: assessment.id,
});

// then
expect(liveAlert).to.deep.equal(certificationChallengeLiveAlert);
});
});

describe('when there is a matching ongoing validated alert', function () {
it('should return the live alert', async function () {
// given
const certificationCourse = databaseBuilder.factory.buildCertificationCourse();

const assessment = databaseBuilder.factory.buildAssessment({
certificationCourseId: certificationCourse.id,
userId: certificationCourse.userId,
});

databaseBuilder.factory.buildCertificationChallengeLiveAlert({
assessmentId: assessment.id,
status: CertificationChallengeLiveAlertStatus.DISMISSED,
});

const certificationChallengeLiveAlert = databaseBuilder.factory.buildCertificationChallengeLiveAlert({
assessmentId: assessment.id,
status: CertificationChallengeLiveAlertStatus.ONGOING,
});

await databaseBuilder.commit();

// when
const liveAlert =
await certificationChallengeLiveAlertRepository.getOngoingOrValidatedByChallengeIdAndAssessmentId({
challengeId: certificationChallengeLiveAlert.challengeId,
assessmentId: assessment.id,
});

// then
expect(liveAlert).to.deep.equal(certificationChallengeLiveAlert);
});
});
});
});

0 comments on commit 541031d

Please sign in to comment.