From b310d9d2b84f54cdc72679e10ea7f85efe01f036 Mon Sep 17 00:00:00 2001
From: Hugo <hnouts@grandlyon.com>
Date: Mon, 27 Sep 2021 16:50:31 +0200
Subject: [PATCH] feat (migrations) : add label conseiller numerique france
 service

---
 .../scripts/1621433347542-pix-label.ts        |  2 +-
 .../1632750776916-conseillernum-label.ts      | 59 +++++++++++++++++++
 2 files changed, 60 insertions(+), 1 deletion(-)
 create mode 100644 src/migrations/scripts/1632750776916-conseillernum-label.ts

diff --git a/src/migrations/scripts/1621433347542-pix-label.ts b/src/migrations/scripts/1621433347542-pix-label.ts
index bb5757cf4..f1823d44e 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 000000000..f81a13c13
--- /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;
+}
-- 
GitLab