diff --git a/src/migrations/scripts/1621433347542-pix-label.ts b/src/migrations/scripts/1621433347542-pix-label.ts new file mode 100644 index 0000000000000000000000000000000000000000..bb5757cf420f0e2d12e074acbfa66bf0cd0eb69d --- /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; +}