Skip to content

Commit

Permalink
feat: progress
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlr committed Jun 26, 2024
1 parent 1b14f2a commit 1abf2e6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ async function saveHistory(originalDocument, newDocument) {
const oDiff = diff(originalDocument, newDocument);
updatedFields = deepFlattenToObject(oDiff);

console.log("updatedFields : ", updatedFields);

if (!updatedFields) return;

for (const key of Object.keys(updatedFields)) {
Expand Down
45 changes: 26 additions & 19 deletions server/src/modules/jobs/deca/hydrate-deca.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import parentLogger from "@/common/logger";

import { asyncForEach } from "../../../common/utils/asyncUtils";
import { getDbCollection } from "../../../common/utils/mongodbUtils";
import { saveHistory } from "./watcher";
import { saveHistory } from "./hydrate-deca-history";

const logger = parentLogger.child({ module: "job:hydrate:deca" });
const DATE_DEBUT_CONTRATS_DISPONIBLES = new Date("2022-06-07T00:00:00.000Z"); // Date de début de disponibilité des données dans l'API Deca
Expand Down Expand Up @@ -71,7 +71,7 @@ export const hydrateDeca = async ({ from, to, chunk = 1 }: { from?: string; to?:
// Récupération des périodes (liste dateDebut/fin) à fetch dans l'API
const periods = buildPeriodsToFetch(dateDebutToFetch, dateFinToFetch, chunk);

//console.log("PERDIODS : ", periods);
console.log("PERDIODS : ", periods);

await asyncForEach(periods, async ({ dateDebut, dateFin }: { dateDebut: string; dateFin: string }) => {
try {
Expand Down Expand Up @@ -159,7 +159,7 @@ export const hydrateDeca = async ({ from, to, chunk = 1 }: { from?: string; to?:
},
no_contrat: contrat.detailsContrat.noContrat, // TDB, LBA
...ifDefined("type_contrat", "" + contrat.detailsContrat.typeContrat), // TDB, LBA
...ifDefined("date_effet_rupture", contrat.rupture?.dateEffetRupture), // TDB, LBA
...ifDefined("date_effet_rupture", contrat.rupture?.dateEffetRupture, parseDate), // TDB, LBA
...ifDefined("dispositif", contrat.detailsContrat.dispositif), // LBA
...ifDefined("date_debut_contrat", contrat.detailsContrat.dateDebutContrat, parseDate), // TDB, LBA
...ifDefined("date_fin_contrat", contrat.detailsContrat.dateFinContrat, parseDate), // TDB, LBA
Expand All @@ -180,30 +180,37 @@ export const hydrateDeca = async ({ from, to, chunk = 1 }: { from?: string; to?:
return acc;
}, [] as any[]);

await asyncForEach(decaContratsForPeriod, async (currentContrat: any) => {
console.log("AAAAA ", currentContrat);
console.log("nb_contrat_après_merge ", decaContratsForPeriod.length);

await asyncForEach(decaContratsForPeriod, async (currentContrat: any) => {
const oldContrat = await getDbCollection("deca").findOne({
no_contrat: currentContrat.no_contrat,
"alternant.date_naissance": currentContrat.alternant.date_naissance,
});

console.log("ICI : ", oldContrat);
const now = new Date();
const newContrat = await getDbCollection("deca").findOneAndUpdate(
{
no_contrat: currentContrat.no_contrat,
"alternant.date_naissance": currentContrat.alternant.date_naissance,
},
{
$set: { ...currentContrat, created_at: oldContrat ? oldContrat.created_at : now, updated_at: now },
},
{ upsert: true, returnDocument: "after" }
);
console.log("LA ", newContrat);

if (oldContrat) {
await saveHistory(oldContrat, newContrat);
/*
decaHistory contient les modifs lorsque modif sur numéro de contrat + nom + type contrat identique
*/
if (oldContrat && oldContrat.type_contrat !== currentContrat.type_contrat) {
await getDbCollection("deca").insertOne({ ...currentContrat, created_at: now, updated_at: now });
} else {
const newContrat = await getDbCollection("deca").findOneAndUpdate(
{
no_contrat: currentContrat.no_contrat,
"alternant.date_naissance": currentContrat.alternant.date_naissance,
},
{
$set: { ...currentContrat, created_at: oldContrat ? oldContrat.created_at : now, updated_at: now },
},
{ upsert: true, returnDocument: "after" }
);

if (oldContrat) {
await saveHistory(oldContrat, newContrat.value);
}
}
});
} catch (err: any) {
Expand Down
13 changes: 0 additions & 13 deletions server/src/modules/jobs/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,6 @@ export async function setupJobProcessor() {
const { from, to, chunk } = job.payload as any;
await hydrateDeca({ from, to, chunk });
},
/*
ALAN:
faire tourner une fenêtre glissante de 1 jour tous les jours
en cas d'échec récupérer la last update et partir de là sur une fenêtre élargie pour se repositionner
supprimer le watcher sur la collection deca et mesurer / enregistrer les diff de manière séquentielle (findOne -> buildDiff -> createHistory)
les documents dans decaHistory concernent un seul champ (ex: update de 3 champs --> 3 documents enregistrés )
decaHistory contient les modifs lorsque modif sur numéro de contrat + nom + type contrat identique
une modif sur contrat + nom identiques mais type différent implique un nouveau document dans deca (et pas dans history en première entrée)
!!!! L'api n'est fonctionnelle qu'après 20h00.
L'api est fragile et ne doit pas être sur sollicitée
*/
},
"import:catalogue": {
handler: () => runCatalogueImporter(),
Expand Down
2 changes: 1 addition & 1 deletion shared/models/deca.model/parts/deca.apprenant.part.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const ZDecaApprenant = z
})
.strict()
.optional(),
derniere_classe: z.number().optional().describe("La dernière classe de l'apprenant"),
derniere_classe: z.string().optional().describe("La dernière classe de l'apprenant"),
})
.strict();

Expand Down

0 comments on commit 1abf2e6

Please sign in to comment.