From 3bb52d0a2718c7a42d9084c225b4e82e0e27a9f4 Mon Sep 17 00:00:00 2001 From: Antonin Coquet <ext.sopra.acoquet@grandlyon.com> Date: Thu, 6 May 2021 15:53:56 +0200 Subject: [PATCH] feat: add women to public list --- .../scripts/1620308186636-public-women.ts | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/migrations/scripts/1620308186636-public-women.ts diff --git a/src/migrations/scripts/1620308186636-public-women.ts b/src/migrations/scripts/1620308186636-public-women.ts new file mode 100644 index 000000000..16bfe2fc6 --- /dev/null +++ b/src/migrations/scripts/1620308186636-public-women.ts @@ -0,0 +1,59 @@ +import { Db } from 'mongodb'; +import { getDb } from '../migrations-utils/db'; + +export const up = async () => { + const db: Db = await getDb(); + + const cursor = db.collection('categoriesothers').find({}); + let document; + while ((document = await cursor.next())) { + if (document.id == 'publics') { + const newDoc = updatePublic(document); + await db.collection('categoriesothers').updateOne({ _id: document._id }, [{ $set: newDoc }]); + } + } + console.log(`Update done`); +}; + +export const down = async () => { + const db: Db = await getDb(); + + const cursor = db.collection('categoriesothers').find({}); + let document; + while ((document = await cursor.next())) { + if (document.id == 'publics') { + const newDoc = downgradePublic(document); + await db.collection('categoriesothers').updateOne({ _id: document._id }, [{ $set: newDoc }]); + } + } + console.log(`Update done`); +}; + +function updatePublic(doc) { + doc = addWomen(doc); + return doc; +} + +function downgradePublic(doc) { + doc = removeWomen(doc); + return doc; +} + +function removeWomen(doc) { + if (doc.modules) { + doc.modules = doc.modules.filter(function (elem) { + return elem.id != 'women'; + }); + } + return doc; +} + +function addWomen(doc) { + if (doc.modules) { + doc.modules.push({ + id: 'women', + text: 'Femmes', + }); + } + return doc; +} -- GitLab