-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: consolidation des effectifs autour du RNCP/CFD
- Loading branch information
Showing
6 changed files
with
189 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
server/src/db/migrations/20241211154816-deduplication-effectifs-auto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import type { ObjectId } from "mongodb"; | ||
import { MOTIF_SUPPRESSION } from "shared/constants"; | ||
|
||
import { softDeleteEffectif } from "@/common/actions/effectifs.actions"; | ||
import { effectifsDb, effectifsDECADb } from "@/common/model/collections"; | ||
|
||
export const up = async () => { | ||
const effectifDuplicats = await effectifsDb() | ||
.aggregate<{ | ||
_id: { | ||
annee_scolaire: string; | ||
cfd: string; | ||
rncp: string; | ||
id_erp_apprenant: string; | ||
organisme_id: string; | ||
}; | ||
count: number; | ||
docs: { | ||
id: ObjectId; | ||
updated_at: Date; | ||
}[]; | ||
}>([ | ||
{ | ||
$group: { | ||
_id: { | ||
annee_scolaire: "$annee_scolaire", | ||
cfd: "$formation.cfd", | ||
rncp: "$formation.rncp", | ||
id_erp_apprenant: "$id_erp_apprenant", | ||
organisme_id: "$organisme_id", | ||
}, | ||
count: { | ||
$sum: 1, | ||
}, | ||
docs: { | ||
$addToSet: { | ||
id: "$_id", | ||
updated_at: "$updated_at", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
$match: { count: { $gt: 1 } }, | ||
}, | ||
]) | ||
.toArray(); | ||
|
||
for (const effectifDuplicat of effectifDuplicats) { | ||
const { docs } = effectifDuplicat; | ||
const lastUpdatedDoc = docs.reduce((acc, doc) => { | ||
if (doc.updated_at.getTime() > acc.updated_at.getTime()) { | ||
return doc; | ||
} | ||
|
||
return acc; | ||
}, docs[0]); | ||
|
||
await softDeleteEffectif(lastUpdatedDoc.id, null, { | ||
motif: MOTIF_SUPPRESSION.Doublon, | ||
description: "Suppression du doublon suite à la migration index unique sur effectifs", | ||
}); | ||
} | ||
|
||
const effectifDecaDuplicats = await effectifsDECADb() | ||
.aggregate<{ | ||
_id: { | ||
annee_scolaire: string; | ||
cfd: string; | ||
rncp: string; | ||
id_erp_apprenant: string; | ||
organisme_id: string; | ||
}; | ||
count: number; | ||
docs: { | ||
id: ObjectId; | ||
updated_at: Date; | ||
}[]; | ||
}>([ | ||
{ | ||
$group: { | ||
_id: { | ||
annee_scolaire: "$annee_scolaire", | ||
cfd: "$formation.cfd", | ||
rncp: "$formation.rncp", | ||
id_erp_apprenant: "$id_erp_apprenant", | ||
organisme_id: "$organisme_id", | ||
}, | ||
count: { | ||
$sum: 1, | ||
}, | ||
docs: { | ||
$addToSet: { | ||
id: "$_id", | ||
updated_at: "$updated_at", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
$match: { count: { $gt: 1 } }, | ||
}, | ||
]) | ||
.toArray(); | ||
|
||
for (const effectifDuplicat of effectifDecaDuplicats) { | ||
const { docs } = effectifDuplicat; | ||
const lastUpdatedDoc = docs.reduce((acc, doc) => { | ||
if (doc.updated_at.getTime() > acc.updated_at.getTime()) { | ||
return doc; | ||
} | ||
|
||
return acc; | ||
}, docs[0]); | ||
|
||
await effectifsDECADb().deleteMany({ _id: { $ne: lastUpdatedDoc.id } }); | ||
} | ||
|
||
// DROP unique index | ||
await effectifsDb().dropIndex( | ||
"organisme_id_1_annee_scolaire_1_id_erp_apprenant_1_apprenant.nom_1_apprenant.prenom_1_formation.cfd_1_formation.annee_1" | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters