From ab52515ce0c86a14541486c8fbb1aa580828b9c0 Mon Sep 17 00:00:00 2001 From: Antonin Coquet <ext.sopra.acoquet@grandlyon.com> Date: Wed, 19 May 2021 17:29:59 +0200 Subject: [PATCH] feat: add migration for pix label --- .../scripts/1621433347542-pix-label.ts | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/migrations/scripts/1621433347542-pix-label.ts diff --git a/src/migrations/scripts/1621433347542-pix-label.ts b/src/migrations/scripts/1621433347542-pix-label.ts new file mode 100644 index 000000000..bb5757cf4 --- /dev/null +++ b/src/migrations/scripts/1621433347542-pix-label.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 == 'labelsQualifications') { + const newDoc = updateLabel(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 == 'labelsQualification') { + const newDoc = downgradeLabel(document); + await db.collection('categoriesothers').updateOne({ _id: document._id }, [{ $set: newDoc }]); + } + } + console.log(`Update done`); +}; + +function updateLabel(doc) { + doc = addPix(doc); + return doc; +} + +function downgradeLabel(doc) { + doc = removePix(doc); + return doc; +} + +function removePix(doc) { + if (doc.modules) { + doc.modules = doc.modules.filter(function (elem) { + return elem.id != 'pix'; + }); + } + return doc; +} + +function addPix(doc) { + if (doc.modules) { + doc.modules.push({ + id: 'pix', + text: 'Évaluation des compétences numériques', + }); + } + return doc; +} -- GitLab