diff --git a/src/migrations/scripts/1621433347542-pix-label.ts b/src/migrations/scripts/1621433347542-pix-label.ts index bb5757cf420f0e2d12e074acbfa66bf0cd0eb69d..f1823d44ef9e823374493d83d6822af508afbfd6 100644 --- a/src/migrations/scripts/1621433347542-pix-label.ts +++ b/src/migrations/scripts/1621433347542-pix-label.ts @@ -21,7 +21,7 @@ export const down = async () => { const cursor = db.collection('categoriesothers').find({}); let document; while ((document = await cursor.next())) { - if (document.id == 'labelsQualification') { + if (document.id == 'labelsQualifications') { const newDoc = downgradeLabel(document); await db.collection('categoriesothers').updateOne({ _id: document._id }, [{ $set: newDoc }]); } diff --git a/src/migrations/scripts/1632750776916-conseillernum-label.ts b/src/migrations/scripts/1632750776916-conseillernum-label.ts new file mode 100644 index 0000000000000000000000000000000000000000..f81a13c13f7251c0b0d14a71fbe71979b419b5dc --- /dev/null +++ b/src/migrations/scripts/1632750776916-conseillernum-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 == 'labelsQualifications') { + const newDoc = downgradeLabel(document); + await db.collection('categoriesothers').updateOne({ _id: document._id }, [{ $set: newDoc }]); + } + } + console.log(`Update done`); +}; + +function updateLabel(doc) { + doc = addCNFS(doc); + return doc; +} + +function downgradeLabel(doc) { + doc = removeCNFS(doc); + return doc; +} + +function removeCNFS(doc) { + if (doc.modules) { + doc.modules = doc.modules.filter(function (elem) { + return elem.id != 'conseillerNumFranceServices'; + }); + } + return doc; +} + +function addCNFS(doc) { + if (doc.modules) { + doc.modules.push({ + id: 'conseillerNumFranceServices', + text: 'Conseiller numérique France services', + }); + } + return doc; +}