Skip to content

Commit

Permalink
✨ api: receive pb-boss job into handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Steph0 committed Jul 19, 2024
1 parent 1a792bb commit 1f6767f
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 73 deletions.
12 changes: 12 additions & 0 deletions api/lib/domain/events/CertificationRescoredByScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { assertNotNullOrUndefined } from '../../../src/shared/domain/models/asserts.js';

export default class CertificationRescoredByScript {
/**
* @param {Object} params
* @param {number} params.certificationCourseId - certification course that will be rescored
*/
constructor({ certificationCourseId }) {
assertNotNullOrUndefined(certificationCourseId);
this.certificationCourseId = certificationCourseId;
}
}
4 changes: 3 additions & 1 deletion api/lib/domain/events/handle-certification-rescoring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CertificationResult } from '../models/CertificationResult.js';
import { CertificationCourseRejected } from './CertificationCourseRejected.js';
import { CertificationCourseUnrejected } from './CertificationCourseUnrejected.js';
import { CertificationJuryDone } from './CertificationJuryDone.js';
import CertificationRescoredByScript from './CertificationRescoredByScript.js';
import { CertificationRescoringCompleted } from './CertificationRescoringCompleted.js';
import { ChallengeDeneutralized } from './ChallengeDeneutralized.js';
import { ChallengeNeutralized } from './ChallengeNeutralized.js';
Expand All @@ -17,6 +18,7 @@ const eventTypes = [
CertificationJuryDone,
CertificationCourseRejected,
CertificationCourseUnrejected,
CertificationRescoredByScript,
];

async function handleCertificationRescoring({
Expand Down Expand Up @@ -216,7 +218,7 @@ function _getEmitterFromEvent(event) {
emitter = CertificationResult.emitters.PIX_ALGO_NEUTRALIZATION;
}

if (event instanceof CertificationJuryDone) {
if (event instanceof CertificationJuryDone || event instanceof CertificationRescoredByScript) {
emitter = CertificationResult.emitters.PIX_ALGO_AUTO_JURY;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import CertificationRescoredByScript from '../../../../../lib/domain/events/CertificationRescoredByScript.js';
import { CertificationRescoringByScriptJob } from './CertificationRescoringByScriptJob.js';

class CertificationRescoringByScriptJobHandler {
constructor({ logger }) {
this.logger = logger;
constructor({ eventDispatcher }) {
this.eventDispatcher = eventDispatcher;
}

handle(event) {
this.logger.info({
data: event.certificationCourseId,
});
/**
* @param {Object} event
* @param {number} event.certificationCourseId
*/
async handle(event) {
await this.eventDispatcher.dispatch(new CertificationRescoredByScript(event));
}

get name() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import CertificationRescoredByScript from '../../../../../lib/domain/events/CertificationRescoredByScript.js';
import { JobPgBoss } from '../../../../../lib/infrastructure/jobs/JobPgBoss.js';

export class CertificationRescoringByScriptJob extends JobPgBoss {
Expand All @@ -10,8 +11,11 @@ export class CertificationRescoringByScriptJob extends JobPgBoss {
);
}

/** * @param {number} certificationCourseId */
/**
* @param {number} certificationCourseId
*/
async schedule(certificationCourseId) {
return super.schedule({ certificationCourseId, type: 'CertificationRescoringByScript' });
const data = new CertificationRescoredByScript({ certificationCourseId });
return super.schedule(data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import CertificationRescoredByScript from '../../../../../../lib/domain/events/CertificationRescoredByScript.js';
import { CertificationRescoringByScriptJobHandler } from '../../../../../../src/certification/session-management/infrastructure/jobs/CertificationRescoringByScriptHandler.js';
import { expect, sinon } from '../../../../../test-helper.js';

describe('Certification | session-management | Unit | Jobs | CertificationRescoringByScriptHandler', function () {
it('should call usecase with given certificationCourseId', async function () {
// given
const certificationCourseId = 1;
const eventDispatcherStub = {
dispatch: sinon.stub(),
};

const handler = new CertificationRescoringByScriptJobHandler({ eventDispatcher: eventDispatcherStub });

// when
await handler.handle({ certificationCourseId });

// then
expect(eventDispatcherStub.dispatch).to.have.been.calledWithExactly(
new CertificationRescoredByScript({ certificationCourseId }),
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ describe('Integration | Scripts | Certification | rescore-certfication', functio
.orderBy('createdon', 'asc');

expect(job1.data).to.deep.equal({
type: 'CertificationRescoringByScript',
certificationCourseId: 1,
});

expect(job2.data).to.deep.equal({
type: 'CertificationRescoringByScript',
certificationCourseId: 2,
});
});
Expand Down
141 changes: 82 additions & 59 deletions api/tests/unit/worker_test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CertificationRescoringByScriptJobHandler } from '../../src/certification/session-management/infrastructure/jobs/CertificationRescoringByScriptHandler.js';
import { CertificationRescoringByScriptJob } from '../../src/certification/session-management/infrastructure/jobs/CertificationRescoringByScriptJob.js';
import { ImportOrganizationLearnersJob } from '../../src/prescription/learner-management/infrastructure/jobs/ImportOrganizationLearnersJob.js';
import { ImportOrganizationLearnersJobHandler } from '../../src/prescription/learner-management/infrastructure/jobs/ImportOrganizationLearnersJobHandler.js';
import { ValidateOrganizationImportFileJob } from '../../src/prescription/learner-management/infrastructure/jobs/ValidateOrganizationImportFileJob.js';
Expand All @@ -19,81 +21,102 @@ describe('#runjobs', function () {
scheduleCpfJobsStub = sinon.stub();
});

it('should register ImportOrganizationLearnersJob', async function () {
//given
sinon.stub(config.pgBoss, 'importFileJobEnabled').value(true);
describe('Prescription', function () {
it('should register ImportOrganizationLearnersJob', async function () {
//given
sinon.stub(config.pgBoss, 'importFileJobEnabled').value(true);

// when
await runJobs({
startPgBoss: startPgBossStub,
createMonitoredJobQueue: createMonitoredJobQueueStub,
scheduleCpfJobs: scheduleCpfJobsStub,
// when
await runJobs({
startPgBoss: startPgBossStub,
createMonitoredJobQueue: createMonitoredJobQueueStub,
scheduleCpfJobs: scheduleCpfJobsStub,
});

// then
const calls = monitoredJobQueueStub.performJob
.getCalls()
.find(({ args }) => args[0] === ImportOrganizationLearnersJob.name);

expect(calls.args[1]).to.equal(ImportOrganizationLearnersJobHandler);
});

// then
const calls = monitoredJobQueueStub.performJob
.getCalls()
.find(({ args }) => args[0] === ImportOrganizationLearnersJob.name);
it('should register ValidateOrganizationImportFileJob', async function () {
//given
sinon.stub(config.pgBoss, 'validationFileJobEnabled').value(true);

expect(calls.args[1]).to.equal(ImportOrganizationLearnersJobHandler);
});
// when
await runJobs({
startPgBoss: startPgBossStub,
createMonitoredJobQueue: createMonitoredJobQueueStub,
scheduleCpfJobs: scheduleCpfJobsStub,
});

it('should register ValidateOrganizationImportFileJob', async function () {
//given
sinon.stub(config.pgBoss, 'validationFileJobEnabled').value(true);
// then
const calls = monitoredJobQueueStub.performJob
.getCalls()
.find(({ args }) => args[0] === ValidateOrganizationImportFileJob.name);

// when
await runJobs({
startPgBoss: startPgBossStub,
createMonitoredJobQueue: createMonitoredJobQueueStub,
scheduleCpfJobs: scheduleCpfJobsStub,
expect(calls.args[1]).to.equal(ValidateOrganizationImportFileJobHandler);
});

// then
const calls = monitoredJobQueueStub.performJob
.getCalls()
.find(({ args }) => args[0] === ValidateOrganizationImportFileJob.name);
it('should not register validation job if PGBOSS_VALIDATION_FILE_JOB_ENABLED is false', async function () {
//given
sinon.stub(config.pgBoss, 'validationFileJobEnabled').value(false);
// when
await runJobs({
startPgBoss: startPgBossStub,
createMonitoredJobQueue: createMonitoredJobQueueStub,
scheduleCpfJobs: scheduleCpfJobsStub,
});

expect(calls.args[1]).to.equal(ValidateOrganizationImportFileJobHandler);
});
// then
const calls = monitoredJobQueueStub.performJob.getCalls();

const validatationJob = calls.find(({ args }) => args[0] === ValidateOrganizationImportFileJob.name);
const importJob = calls.find(({ args }) => args[0] === ImportOrganizationLearnersJob.name);

it('should not register validation job if PGBOSS_VALIDATION_FILE_JOB_ENABLED is false', async function () {
//given
sinon.stub(config.pgBoss, 'validationFileJobEnabled').value(false);
// when
await runJobs({
startPgBoss: startPgBossStub,
createMonitoredJobQueue: createMonitoredJobQueueStub,
scheduleCpfJobs: scheduleCpfJobsStub,
expect(validatationJob).to.be.undefined;
expect(importJob).to.exist;
});

// then
const calls = monitoredJobQueueStub.performJob.getCalls();
it('should not register import job if PGBOSS_IMPORT_FILE_JOB_ENABLED is false', async function () {
//given
sinon.stub(config.pgBoss, 'importFileJobEnabled').value(false);
// when
await runJobs({
startPgBoss: startPgBossStub,
createMonitoredJobQueue: createMonitoredJobQueueStub,
scheduleCpfJobs: scheduleCpfJobsStub,
});

const validatationJob = calls.find(({ args }) => args[0] === ValidateOrganizationImportFileJob.name);
const importJob = calls.find(({ args }) => args[0] === ImportOrganizationLearnersJob.name);
// then
const calls = monitoredJobQueueStub.performJob.getCalls();

expect(validatationJob).to.be.undefined;
expect(importJob).to.exist;
});
const validatationJob = calls.find(({ args }) => args[0] === ValidateOrganizationImportFileJob.name);
const importJob = calls.find(({ args }) => args[0] === ImportOrganizationLearnersJob.name);

it('should not register import job if PGBOSS_IMPORT_FILE_JOB_ENABLED is false', async function () {
//given
sinon.stub(config.pgBoss, 'importFileJobEnabled').value(false);
// when
await runJobs({
startPgBoss: startPgBossStub,
createMonitoredJobQueue: createMonitoredJobQueueStub,
scheduleCpfJobs: scheduleCpfJobsStub,
expect(validatationJob).to.exist;
expect(importJob).to.be.undefined;
});
});

// then
const calls = monitoredJobQueueStub.performJob.getCalls();

const validatationJob = calls.find(({ args }) => args[0] === ValidateOrganizationImportFileJob.name);
const importJob = calls.find(({ args }) => args[0] === ImportOrganizationLearnersJob.name);

expect(validatationJob).to.exist;
expect(importJob).to.be.undefined;
describe('Certification', function () {
it('should register CertificationRescoringByScriptJob', async function () {
//given
// when
await runJobs({
startPgBoss: startPgBossStub,
createMonitoredJobQueue: createMonitoredJobQueueStub,
scheduleCpfJobs: scheduleCpfJobsStub,
});

// then
const calls = monitoredJobQueueStub.performJob
.getCalls()
.find(({ args }) => args[0] === CertificationRescoringByScriptJob.name);

expect(calls.args[1]).to.equal(CertificationRescoringByScriptJobHandler);
});
});
});
9 changes: 6 additions & 3 deletions api/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as url from 'node:url';
import _ from 'lodash';
import PgBoss from 'pg-boss';

import { eventDispatcher } from './lib/domain/events/index.js';
import { UserAnonymizedEventLoggingJob } from './lib/infrastructure/jobs/audit-log/UserAnonymizedEventLoggingJob.js';
import { UserAnonymizedEventLoggingJobHandler } from './lib/infrastructure/jobs/audit-log/UserAnonymizedEventLoggingJobHandler.js';
import { ParticipationResultCalculationJob } from './lib/infrastructure/jobs/campaign-result/ParticipationResultCalculationJob.js';
Expand All @@ -22,6 +23,8 @@ import { ScheduleComputeOrganizationLearnersCertificabilityJob } from './lib/inf
import { ScheduleComputeOrganizationLearnersCertificabilityJobHandler } from './lib/infrastructure/jobs/organization-learner/ScheduleComputeOrganizationLearnersCertificabilityJobHandler.js';
import * as organizationLearnerRepository from './lib/infrastructure/repositories/organization-learner-repository.js';
import * as pgBossRepository from './lib/infrastructure/repositories/pgboss-repository.js';
import { CertificationRescoringByScriptJobHandler } from './src/certification/session-management/infrastructure/jobs/CertificationRescoringByScriptHandler.js';
import { CertificationRescoringByScriptJob } from './src/certification/session-management/infrastructure/jobs/CertificationRescoringByScriptJob.js';
import { GarAnonymizedBatchEventsLoggingJob } from './src/identity-access-management/infrastructure/jobs/audit-log/GarAnonymizedBatchEventsLoggingJob.js';
import { GarAnonymizedBatchEventsLoggingJobHandler } from './src/identity-access-management/infrastructure/jobs/audit-log/GarAnonymizedBatchEventsLoggingJobHandler.js';
import { ImportOrganizationLearnersJob } from './src/prescription/learner-management/infrastructure/jobs/ImportOrganizationLearnersJob.js';
Expand All @@ -31,8 +34,6 @@ import { ValidateOrganizationImportFileJobHandler } from './src/prescription/lea
import { config } from './src/shared/config.js';
import * as learningContentDatasource from './src/shared/infrastructure/datasources/learning-content/datasource.js';
import { logger } from './src/shared/infrastructure/utils/logger.js';
import {CertificationRescoringByScriptJob} from './src/certification/session-management/infrastructure/jobs/CertificationRescoringByScriptJob.js';
import {CertificationRescoringByScriptJobHandler} from './src/certification/session-management/infrastructure/jobs/CertificationRescoringByScriptHandler.js';

async function startPgBoss() {
logger.info('Starting pg-boss');
Expand Down Expand Up @@ -100,7 +101,9 @@ export async function runJobs(dependencies = { startPgBoss, createMonitoredJobQu

monitoredJobQueue.performJob(UserAnonymizedEventLoggingJob.name, UserAnonymizedEventLoggingJobHandler);
monitoredJobQueue.performJob(GarAnonymizedBatchEventsLoggingJob.name, GarAnonymizedBatchEventsLoggingJobHandler);
monitoredJobQueue.performJob(CertificationRescoringByScriptJob.name, CertificationRescoringByScriptJobHandler);
monitoredJobQueue.performJob(CertificationRescoringByScriptJob.name, CertificationRescoringByScriptJobHandler, {
eventDispatcher,
});

if (config.pgBoss.validationFileJobEnabled) {
monitoredJobQueue.performJob(ValidateOrganizationImportFileJob.name, ValidateOrganizationImportFileJobHandler);
Expand Down

0 comments on commit 1f6767f

Please sign in to comment.