From 1d24d339160dd3132db0ff50ad67469628d4a471 Mon Sep 17 00:00:00 2001
From: Augustin LECONTE <["ext.sopra.aleconte@grandlyon.com"]>
Date: Mon, 20 Dec 2021 08:58:11 +0000
Subject: [PATCH] feat(structure): add service venteMaterielPrixSolidaire

---
 .../1639062602934-solidarity-sell-service.ts  | 49 +++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 src/migrations/scripts/1639062602934-solidarity-sell-service.ts

diff --git a/src/migrations/scripts/1639062602934-solidarity-sell-service.ts b/src/migrations/scripts/1639062602934-solidarity-sell-service.ts
new file mode 100644
index 000000000..5ca327c19
--- /dev/null
+++ b/src/migrations/scripts/1639062602934-solidarity-sell-service.ts
@@ -0,0 +1,49 @@
+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 == 'equipmentsAndServices') {
+      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 == 'equipmentsAndServices') {
+      const newDoc = downgradeLabel(document);
+      await db.collection('categoriesothers').updateOne({ _id: document._id }, [{ $set: newDoc }]);
+    }
+  }
+  console.log(`Update done`);
+};
+
+function updateLabel(doc) {
+  if (doc.modules) {
+    doc.modules.push({
+      id: 'venteMaterielPrixSolidaire',
+      text: 'Vente de matériel à prix solidaire',
+    });
+  }
+  return doc;
+}
+
+function downgradeLabel(doc) {
+  if (doc.modules) {
+    doc.modules = doc.modules.filter(function (elem) {
+      return elem.id != 'venteMaterielPrixSolidaire';
+    });
+  }
+  return doc;
+}
-- 
GitLab