From aaf0363e7b0b65162050217ab4de44bd2495812a Mon Sep 17 00:00:00 2001 From: Etienne LOUPIAS <eloupias@grandlyon.com> Date: Wed, 29 May 2024 16:32:55 +0200 Subject: [PATCH] fix(filter): fix freeWorkshop id --- .../1716971590976-rename-freeworkshop-id.ts | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/migrations/scripts/1716971590976-rename-freeworkshop-id.ts diff --git a/src/migrations/scripts/1716971590976-rename-freeworkshop-id.ts b/src/migrations/scripts/1716971590976-rename-freeworkshop-id.ts new file mode 100644 index 000000000..00e99189b --- /dev/null +++ b/src/migrations/scripts/1716971590976-rename-freeworkshop-id.ts @@ -0,0 +1,118 @@ +import { Db } from 'mongodb'; +import { getDb } from '../migrations-utils/db'; + +export const up = async () => { + try { + const db: Db = await getDb(); + + // Change ids of the freeWorkShop category + const categories = await db.collection('categories').find().toArray(); + + const cursor = db.collection('categories').find({ id: 'freeWorkShop' }); + let category; + while ((category = await cursor.next())) { + const modules = [ + { + id: 'yes', + name: 'Accompagnements gratuits', + }, + { + id: 'underCondition', + name: 'Accompagnements gratuits sous conditions', + }, + { + id: 'no', + name: 'Accompagnements payants', + }, + ]; + + await db.collection('categories').updateOne( + { _id: category._id }, + { + $set: { modules: modules }, + } + ); + } + + // Change freeWorkShop id of structures + const structures = await db.collection('structures').find().toArray(); + + for (const structure of structures) { + let freeWorkShopValue: string; + + if (structure.categories.freeWorkShop === 'Oui') { + freeWorkShopValue = 'yes'; + } else if (structure.categories.freeWorkShop === 'Oui, sous condition') { + freeWorkShopValue = 'underCondition'; + } else if (structure.categories.freeWorkShop === 'Non') { + freeWorkShopValue = 'no'; + } + + await db + .collection('structures') + .updateOne({ _id: structure._id }, { $set: { 'categories.freeWorkShop': freeWorkShopValue } }); + } + + console.log(`Update done: freeWorkShops ids fixed.`); + } catch (error) { + console.error('Error while fixing freeWorkShops ids:', error); + } +}; + +export const down = async () => { + try { + const db: Db = await getDb(); + + // Change ids of the freeWorkShop category + const categories = await db.collection('categories').find().toArray(); + + const cursor = db.collection('categories').find({ id: 'freeWorkShop' }); + let category; + while ((category = await cursor.next())) { + const modules = [ + { + id: 'Oui', + name: 'Accompagnements gratuits', + }, + { + id: 'Oui, sous condition', + name: 'Accompagnements gratuits sous conditions', + }, + { + id: 'Non', + name: 'Accompagnements payants', + }, + ]; + + await db.collection('categories').updateOne( + { _id: category._id }, + { + $set: { modules: modules }, + } + ); + } + + // Change freeWorkShop id of structures + const structures = await db.collection('structures').find().toArray(); + + for (const structure of structures) { + let freeWorkShopValue: string; + + if (structure.categories.freeWorkShop === 'yes') { + freeWorkShopValue = 'Oui'; + } else if (structure.categories.freeWorkShop === 'underCondition') { + freeWorkShopValue = 'Oui, sous condition'; + } else if (structure.categories.freeWorkShop === 'no') { + freeWorkShopValue = 'Non'; + } + + await db + .collection('structures') + .updateOne({ _id: structure._id }, { $set: { 'categories.freeWorkShop': freeWorkShopValue } }); + } + + console.log(`Downgrade done: freeWorkShops ids reverted.`); + } catch (error) { + console.error('Error reverting freeWorkShop ids:', error); + } +}; -- GitLab