-
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: nettoyage des organismes inconnus
- Loading branch information
Showing
2 changed files
with
33 additions
and
116 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
server/src/db/migrations/20241025115945-clean-organismes.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,21 @@ | ||
import { Db, type ObjectId } from "mongodb"; | ||
|
||
import { organismesDb, usersMigrationDb } from "@/common/model/collections"; | ||
|
||
const countOrganismeUsers = async (organismeId: ObjectId) => { | ||
return await usersMigrationDb().countDocuments({ organismes: organismeId }); | ||
}; | ||
|
||
export const up = async (db: Db) => { | ||
const organismes = organismesDb().find({ effectifs_count: 0 }); | ||
for await (const organisme of organismes) { | ||
const usersCount = await countOrganismeUsers(organisme._id); | ||
if (usersCount === 0) { | ||
await db.collection("migration-20241025115945-clean-organismes").insertOne({ | ||
op: "delete-organisme", | ||
organisme, | ||
}); | ||
await organismesDb().deleteOne({ _id: organisme._id }); | ||
} | ||
} | ||
}; |
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