Skip to content

Commit

Permalink
fix: correction de l'api de mise a jour de reseau + migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomarom committed Mar 5, 2025
1 parent 48331e3 commit d19c627
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 29 additions & 0 deletions server/src/db/migrations/20250305140803-change-key-name-reseau.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { addJob } from "job-processor";

import { organismesDb, reseauxDb } from "@/common/model/collections";

export const up = async () => {
// Récupération de tous les reseaux des organismes, et vérification si c'est le nom ou le key qui est dans la liste
// Puis mise à jour en conséquence

const allReseaux = await reseauxDb().find().toArray();

const reseauxMap = allReseaux.reduce((acc, curr) => {
return {
...acc,
[curr.nom]: curr.key,
};
}, {});

const cursor = organismesDb().find({ reseaux: { $exists: true, $not: { $size: 0 } } });
while (await cursor.hasNext()) {
const org = await cursor.next();
if (org) {
// Remplacer le nom par la key si existe, sinon on garde la valeur dans la liste
const newReseaux = org?.reseaux?.map((n) => reseauxMap[n] ?? n);
await organismesDb().updateOne({ _id: org._id }, { $set: { reseaux: newReseaux } });
}
}
// Mise a jour des computed des organismes
await addJob({ name: "computed:update", queued: true });
};
4 changes: 2 additions & 2 deletions server/src/http/routes/admin.routes/reseaux.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default () => {
throw Boom.internal("Failed to update the organismes_ids array.");
}

await organismesDb().updateOne({ _id: organismeObjectId }, { $addToSet: { reseaux: reseau.nom } });
await organismesDb().updateOne({ _id: organismeObjectId }, { $addToSet: { reseaux: reseau.key } });

const updatedOrganisme = await organismesDb().findOne({ _id: organismeObjectId });
if (updatedOrganisme) {
Expand Down Expand Up @@ -142,7 +142,7 @@ export default () => {
throw Boom.notFound(`No reseau found with id ${id}`);
}

await organismesDb().updateOne({ _id: organismeId as ObjectId }, { $pull: { reseaux: reseau.nom } });
await organismesDb().updateOne({ _id: organismeId as ObjectId }, { $pull: { reseaux: reseau.key } });

const updatedOrganisme = await organismesDb().findOne({ _id: organismeId as ObjectId });
if (updatedOrganisme) {
Expand Down

0 comments on commit d19c627

Please sign in to comment.