Skip to content

Commit

Permalink
feat(api) 121 - add coherence check for missing IN_PROGRESS status fo…
Browse files Browse the repository at this point in the history
…r youngs
  • Loading branch information
Johannbr committed Jan 23, 2025
1 parent 2d7d2d2 commit c35f785
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
77 changes: 77 additions & 0 deletions api/src/crons/checkMissingInProgress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { YoungDocument, YoungModel } from "../models";
import { logger } from "../logger";
import { capture } from "../sentry";
import slack from "../slack";

export const handler = async () => {
try {
const youngs: (YoungDocument & { patches: any })[] = await YoungModel.aggregate([
{
$match: {
status: "VALIDATED",
},
},
{
$project: {
status: 1,
},
},
{
$lookup: {
from: "young_patches",
localField: "_id",
foreignField: "ref",
as: "patches",
},
},
]);

const checkMissingInProgressWhenValidated = (youngs: (YoungDocument & { patches: any })[]): string[] => {
logger.info("checkMissingInProgressWhenValidated.youngs.length : " + youngs.length);
const youngsWithStatusValidatedWihtoutInProgress: string[] = [];
let counter = 0;
for (const young of youngs) {
counter++;
if (counter % 1000 === 0) {
logger.info("checkMissingInProgressWhenValidated.counter : " + counter);
}
let hasStatusValidated = false;
let hasStatusInProgress = false;

for (const patch of young.patches) {
for (const op of patch.ops) {
if (op.path === "/status" && op.value === "VALIDATED") {
hasStatusValidated = true;
}
if (op.path === "/status" && op.value === "IN_PROGRESS") {
hasStatusInProgress = true;
}
}
}

if (hasStatusValidated === true && hasStatusInProgress === false) {
youngsWithStatusValidatedWihtoutInProgress.push(young._id);
}
}

return youngsWithStatusValidatedWihtoutInProgress;
};

const youngsWithStatusValidatedWihtoutInProgress = checkMissingInProgressWhenValidated(youngs);

if (youngsWithStatusValidatedWihtoutInProgress.length > 0) {
logger.info("checkMissingInProgress: Youngs with status VALIDATED without IN_PROGRESS: " + youngsWithStatusValidatedWihtoutInProgress.length);
logger.info("checkMissingInProgress: Youngs with status VALIDATED without IN_PROGRESS: " + JSON.stringify(youngsWithStatusValidatedWihtoutInProgress));
await slack.error({
title: "checkMissingInProgress",
text: `Young with incoherent cohortId: ${youngsWithStatusValidatedWihtoutInProgress.length}, cohorts: ${JSON.stringify(youngsWithStatusValidatedWihtoutInProgress)}`,
});
} else {
logger.debug("checkMissingInProgress: No youngs with incoherent cohortId");
}
} catch (e) {
capture(e);
slack.error({ title: "checkMissingInProgress", text: JSON.stringify(e) });
throw e;
}
};
2 changes: 2 additions & 0 deletions api/src/crons/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const classesStatusUpdate = require("./classesStatusUpdate");
const monitorCertificats = require("./monitorCertificats");
const checkCoherence = require("./checkCoherence");
const autoValidatePhase1 = require("./autoValidatePhase1");
const checkMissingInProgressWhenValidated = require("./checkMissingInProgress");

// doubt ? -> https://crontab.guru/

Expand Down Expand Up @@ -115,6 +116,7 @@ const CRONS = [
cron("classesStatusUpdate", "2 */1 * * *", classesStatusUpdate.handler),
cron("monitorCertificats", "0 3 1 * *", monitorCertificats.handler),
cron("checkCoherence", "30 7,12,16 * * *", checkCoherence.handler),
cron("checkMissingInProgressWhenValidated", "42 1 * * *", checkMissingInProgressWhenValidated.handler),
cron("autoValidatePhase1", "20 1 * * *", autoValidatePhase1.handler),
];

Expand Down

0 comments on commit c35f785

Please sign in to comment.