Skip to content

Commit

Permalink
add AdminTaskAffectationSelectorService for AdminTask Consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
achorein committed Jan 27, 2025
1 parent 8205cc6 commit 6792a44
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ describe("ValiderAffectationCLE", () => {
}),
);

console.log(result);
expect(result.analytics.errors).toEqual(0);
expect(result.analytics.jeunesAffected).toEqual(1);
expect(result.rapportData[0]).toEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ export class AffectationController {
TaskName.AFFECTATION_CLE_SIMULATION_VALIDER,
);

console.log("status", status, "lastCompletedAt", lastCompletedAt);
if (
[TaskStatus.IN_PROGRESS, TaskStatus.PENDING].includes(status) ||
(lastCompletedAt && simulationTask.createdAt <= lastCompletedAt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class SejourMapper {
status: sejourModel.status,
hasTimeSchedule: sejourModel.hasTimeSchedule,
hasPedagoProject: sejourModel.hasPedagoProject,
cohort: sejourModel.sessionName,
cohort: sejourModel.sessionNom,
cohortId: sejourModel.sessionId,
};
}
Expand Down
103 changes: 9 additions & 94 deletions apiv2/src/admin/infra/task/AdminTask.consumer.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
import { ReferentielImportTaskModel } from "@admin/core/referentiel/routes/ReferentielImportTask.model";
import { Processor, WorkerHost } from "@nestjs/bullmq";
import { Logger } from "@nestjs/common";
import { Job } from "bullmq";
import { ClsService } from "nestjs-cls";

import { TaskName } from "snu-lib";

import { ConsumerResponse } from "@shared/infra/ConsumerResponse";
import { QueueName, TaskQueue } from "@shared/infra/Queue";
import { Job } from "bullmq";
import { TaskName, ValiderAffectationCLETaskResult, ValiderAffectationHTSTaskResult } from "snu-lib";
import { AdminTaskRepository } from "./AdminTaskMongo.repository";
import { SimulationAffectationCLE } from "@admin/core/sejours/phase1/affectation/SimulationAffectationCLE";
import { SimulationAffectationHTS } from "@admin/core/sejours/phase1/affectation/SimulationAffectationHTS";
import {
SimulationAffectationHTSTaskModel,
SimulationAffectationHTSTaskResult,
} from "@admin/core/sejours/phase1/affectation/SimulationAffectationHTSTask.model";
import { ValiderAffectationHTS } from "@admin/core/sejours/phase1/affectation/ValiderAffectationHTS";
import { ValiderAffectationHTSTaskModel } from "@admin/core/sejours/phase1/affectation/ValiderAffectationHTSTask.model";
import { ReferentielImportTaskModel } from "@admin/core/referentiel/routes/ReferentielImportTask.model";
import { AdminTaskImportReferentielSelectorService } from "./AdminTaskImportReferentielSelector.service";
import { ClsService } from "nestjs-cls";
import {
SimulationAffectationCLETaskModel,
SimulationAffectationCLETaskResult,
} from "@admin/core/sejours/phase1/affectation/SimulationAffectationCLETask.model";
import { ValiderAffectationCLETaskModel } from "@admin/core/sejours/phase1/affectation/ValiderAffectationCLETask.model";
import { ValiderAffectationCLE } from "@admin/core/sejours/phase1/affectation/ValiderAffectationCLE";
import { AdminTaskAffectationSelectorService } from "./AdminTaskAffectationSelector.service";

@Processor(QueueName.ADMIN_TASK)
export class AdminTaskConsumer extends WorkerHost {
constructor(
private readonly logger: Logger,
private readonly adminTaskRepository: AdminTaskRepository,
private readonly simulationAffectationCle: SimulationAffectationCLE,
private readonly simulationAffectationHts: SimulationAffectationHTS,
private readonly validerAffectationHts: ValiderAffectationHTS,
private readonly validerAffectationCle: ValiderAffectationCLE,
private readonly adminTaskAffectationSelectorService: AdminTaskAffectationSelectorService,
private readonly referentielTaskService: AdminTaskImportReferentielSelectorService,
private readonly cls: ClsService,
) {
Expand All @@ -51,82 +37,11 @@ export class AdminTaskConsumer extends WorkerHost {
this.cls.set("user", { id: "", firstName: job.name, lastName: task.metadata?.parameters?.type });
switch (job.name) {
case TaskName.AFFECTATION_HTS_SIMULATION:
const simulationHtsTask = task as SimulationAffectationHTSTaskModel; // pour l'onglet historique (patches)
const simulationhts = await this.simulationAffectationHts.execute(
simulationHtsTask.metadata!.parameters!,
);
results = {
rapportKey: simulationhts.rapportFile.Key,
selectedCost: simulationhts.analytics.selectedCost,
jeunesNouvellementAffected: simulationhts.analytics.jeunesNouvellementAffected,
jeuneAttenteAffectation: simulationhts.analytics.jeuneAttenteAffectation,
jeunesDejaAffected: simulationhts.analytics.jeunesDejaAffected,
} as SimulationAffectationHTSTaskResult;
break;

case TaskName.AFFECTATION_HTS_SIMULATION_VALIDER:
const validationHtsTask = task as ValiderAffectationHTSTaskModel;
const validationResult = await this.validerAffectationHts.execute({
...validationHtsTask.metadata!.parameters!,
dateAffectation: validationHtsTask.createdAt,
});
results = {
rapportKey: validationResult.rapportFile.Key,
jeunesAffected: validationResult.analytics.jeunesAffected,
errors: validationResult.analytics.errors,
} as ValiderAffectationHTSTaskResult;
// TODO: handle errors with partial results for all tasks
if (validationResult.analytics.jeunesAffected === 0) {
await this.adminTaskRepository.update(task.id, {
...task,
metadata: {
...task.metadata,
results,
},
});
await this.adminTaskRepository.toFailed(job.data.id, "Aucun jeune n'a été affecté");
return ConsumerResponse.FAILURE;
}
break;

case TaskName.AFFECTATION_CLE_SIMULATION:
const simulationCleTask = task as SimulationAffectationCLETaskModel; // pour l'onglet historique (patches)
const simulationCle = await this.simulationAffectationCle.execute(
simulationCleTask.metadata!.parameters!,
);
results = {
rapportKey: simulationCle.rapportFile.Key,
jeunesAffected: simulationCle.analytics.jeunesAffected,
erreurs: simulationCle.analytics.erreurs,
classes: simulationCle.analytics.classes,
} as SimulationAffectationCLETaskResult;
break;

case TaskName.AFFECTATION_CLE_SIMULATION_VALIDER:
const validationCleTask = task as ValiderAffectationCLETaskModel;
const validationCleResult = await this.validerAffectationCle.execute({
...validationCleTask.metadata!.parameters!,
dateAffectation: validationCleTask.createdAt,
});
results = {
rapportKey: validationCleResult.rapportFile.Key,
jeunesAffected: validationCleResult.analytics.jeunesAffected,
errors: validationCleResult.analytics.errors,
} as ValiderAffectationCLETaskResult;
// TODO: handle errors with partial results for all tasks
if (validationCleResult.analytics.jeunesAffected === 0) {
await this.adminTaskRepository.update(task.id, {
...task,
metadata: {
...task.metadata,
results,
},
});
await this.adminTaskRepository.toFailed(job.data.id, "Aucun jeune n'a été affecté");
return ConsumerResponse.FAILURE;
}
results = await this.adminTaskAffectationSelectorService.handleAffectation(job, task);
break;

case TaskName.REFERENTIEL_IMPORT:
const importTask = task as ReferentielImportTaskModel;
this.logger.log(
Expand Down
113 changes: 113 additions & 0 deletions apiv2/src/admin/infra/task/AdminTaskAffectationSelector.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { Injectable } from "@nestjs/common";
import { TaskQueue } from "@shared/infra/Queue";
import {
SimulationAffectationCLETaskResult,
SimulationAffectationHTSTaskResult,
TaskName,
ValiderAffectationCLETaskResult,
ValiderAffectationHTSTaskResult,
} from "snu-lib";
import { Job } from "bullmq";
import { SimulationAffectationHTSTaskModel } from "@admin/core/sejours/phase1/affectation/SimulationAffectationHTSTask.model";
import { ValiderAffectationHTSTaskModel } from "@admin/core/sejours/phase1/affectation/ValiderAffectationHTSTask.model";
import { SimulationAffectationCLETaskModel } from "@admin/core/sejours/phase1/affectation/SimulationAffectationCLETask.model";
import { ValiderAffectationCLETaskModel } from "@admin/core/sejours/phase1/affectation/ValiderAffectationCLETask.model";
import { TaskModel } from "@task/core/Task.model";
import { ValiderAffectationHTS } from "@admin/core/sejours/phase1/affectation/ValiderAffectationHTS";
import { SimulationAffectationHTS } from "@admin/core/sejours/phase1/affectation/SimulationAffectationHTS";
import { AdminTaskRepository } from "./AdminTaskMongo.repository";
import { SimulationAffectationCLE } from "@admin/core/sejours/phase1/affectation/SimulationAffectationCLE";
import { ValiderAffectationCLE } from "@admin/core/sejours/phase1/affectation/ValiderAffectationCLE";

@Injectable()
export class AdminTaskAffectationSelectorService {
constructor(
private readonly simulationAffectationHts: SimulationAffectationHTS,
private readonly validerAffectationHts: ValiderAffectationHTS,
private readonly adminTaskRepository: AdminTaskRepository,
private readonly simulationAffectationCle: SimulationAffectationCLE,
private readonly validerAffectationCle: ValiderAffectationCLE,
) {}
async handleAffectation(job: Job<TaskQueue, any, TaskName>, task: TaskModel): Promise<Record<string, any>> {
let results = {} as Record<string, any>;
switch (job.name) {
case TaskName.AFFECTATION_HTS_SIMULATION:
const simulationHtsTask = task as SimulationAffectationHTSTaskModel; // pour l'onglet historique (patches)
const simulationhts = await this.simulationAffectationHts.execute(
simulationHtsTask.metadata!.parameters!,
);
results = {
rapportKey: simulationhts.rapportFile.Key,
selectedCost: simulationhts.analytics.selectedCost,
jeunesNouvellementAffected: simulationhts.analytics.jeunesNouvellementAffected,
jeuneAttenteAffectation: simulationhts.analytics.jeuneAttenteAffectation,
jeunesDejaAffected: simulationhts.analytics.jeunesDejaAffected,
} as SimulationAffectationHTSTaskResult;
break;

case TaskName.AFFECTATION_HTS_SIMULATION_VALIDER:
const validationHtsTask = task as ValiderAffectationHTSTaskModel;
const validationResult = await this.validerAffectationHts.execute({
...validationHtsTask.metadata!.parameters!,
dateAffectation: validationHtsTask.createdAt,
});
results = {
rapportKey: validationResult.rapportFile.Key,
jeunesAffected: validationResult.analytics.jeunesAffected,
errors: validationResult.analytics.errors,
} as ValiderAffectationHTSTaskResult;
// TODO: handle errors with partial results for all tasks
if (validationResult.analytics.jeunesAffected === 0) {
await this.adminTaskRepository.update(task.id, {
...task,
metadata: {
...task.metadata,
results,
},
});
throw new Error("Aucun jeune n'a été affecté");
}
break;

case TaskName.AFFECTATION_CLE_SIMULATION:
const simulationCleTask = task as SimulationAffectationCLETaskModel; // pour l'onglet historique (patches)
const simulationCle = await this.simulationAffectationCle.execute(
simulationCleTask.metadata!.parameters!,
);
results = {
rapportKey: simulationCle.rapportFile.Key,
jeunesAffected: simulationCle.analytics.jeunesAffected,
erreurs: simulationCle.analytics.erreurs,
classes: simulationCle.analytics.classes,
} as SimulationAffectationCLETaskResult;
break;

case TaskName.AFFECTATION_CLE_SIMULATION_VALIDER:
const validationCleTask = task as ValiderAffectationCLETaskModel;
const validationCleResult = await this.validerAffectationCle.execute({
...validationCleTask.metadata!.parameters!,
dateAffectation: validationCleTask.createdAt,
});
results = {
rapportKey: validationCleResult.rapportFile.Key,
jeunesAffected: validationCleResult.analytics.jeunesAffected,
errors: validationCleResult.analytics.errors,
} as ValiderAffectationCLETaskResult;
// TODO: handle errors with partial results for all tasks
if (validationCleResult.analytics.jeunesAffected === 0) {
await this.adminTaskRepository.update(task.id, {
...task,
metadata: {
...task.metadata,
results,
},
});
throw new Error("Aucun jeune n'a été affecté");
}
break;
default:
throw new Error(`Task of type ${job.name} not handle yet for affectation`);
}
return results;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,16 @@ import { addHours } from "date-fns";
import { INestApplication } from "@nestjs/common";
import { TestingModule } from "@nestjs/testing";

import {
departmentList,
GRADES,
region2department,
RegionsMetropole,
RegionsMetropoleAndCorse,
TaskName,
TaskStatus,
} from "snu-lib";
import { departmentList, region2department, RegionsMetropoleAndCorse, TaskName, TaskStatus } from "snu-lib";

import { AffectationController } from "@admin/infra/sejours/phase1/affectation/api/Affectation.controller";
import { TaskGateway } from "@task/core/Task.gateway";

import { createTask } from "../../../TaskHelper";
import { setupAdminTest } from "../../../setUpAdminTest";
import { createSession } from "../SessionHelper";

import { AffectationService } from "@admin/core/sejours/phase1/affectation/Affectation.service";
import { createSession } from "../helper/SessionHelper";

describe("AffectationController - CLE", () => {
let app: INestApplication;
Expand Down

0 comments on commit 6792a44

Please sign in to comment.