diff --git a/src/migrations/scripts/1620308186636-public-women.ts b/src/migrations/scripts/1620308186636-public-women.ts new file mode 100644 index 0000000000000000000000000000000000000000..16bfe2fc6d79370bcf76970e99d524559df326c2 --- /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; +}