Skip to content

Commit

Permalink
feat(api): optimize fonjep bop migration
Browse files Browse the repository at this point in the history
  • Loading branch information
mxmeunier committed Oct 17, 2023
1 parent 5d7c3b5 commit 2f12f2a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
const { default: fonjepService } = require("../build/src/modules/providers/fonjep/fonjep.service");
const { default: fonjepJoiner } = require("../build/src/modules/providers/fonjep/joiners/fonjepJoiner");

module.exports = {
async up(db) {
const cursor = await db.collection("fonjepVersement").find({});
const fullGrants = await fonjepJoiner.getAllFullGrants();

let nbVersementUpdated = 0;
while (await cursor.hasNext()) {
const versement = await cursor.next();
if (versement.indexedInformations.bop) continue;
const subvention = (
await db
.collection("fonjepSubvention")
.find({ "indexedInformations.code_poste": versement.indexedInformations.code_poste })
.toArray()
)[0];
versement.indexedInformations.bop = fonjepService.getBopFromFounderCode(
subvention.data["FinanceurPrincipalCode"],
);
await db.collection("fonjepVersement").updateOne({ _id: versement._id }, { $set: versement });
nbVersementUpdated++;
if (nbVersementUpdated % 100 === 0) console.log(`100 bop ajoutés`);
const versementsGroupByFinanceurCode = fullGrants.reduce((acc, curr) => {
const financeurCode = curr.data.Dispositif.FinanceurCode;
if (!acc[financeurCode]) acc[financeurCode] = curr.payments;
else acc[financeurCode].push(...curr.payments);
return acc;
}, {});

for (const [versements, code] of versementsGroupByFinanceurCode) {
const bop = fonjepService.getBopFromFounderCode(code);
console.log(`start update versements with bop ${bop}`);
await db
.collection("fonjepVersement")
.updateMany(
{ _id: { $in: versements.map(versement => versement._id) } },
{ $set: { "indexedInformations.bop": bop } },
);
console.log(`end update versements with bop ${bop}`);
}

throw new Error("end mig");
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export class FonjepJoiner {
];
}

public getAllFullGrants() {
return this.applicationCollection.aggregate([{ $match: {} }, ...this.joinPipeline]).toArray();
}

public getFullFonjepGrantsBySiret(siret: Siret) {
return this.applicationCollection
.aggregate([{ $match: { "legalInformations.siret": siret } }, ...this.joinPipeline])
Expand Down

0 comments on commit 2f12f2a

Please sign in to comment.