From e17ebb6161a05dd68c175fcd341951ce2cc41b32 Mon Sep 17 00:00:00 2001 From: Mathieu Ponton <mponton@grandlyon.com> Date: Mon, 27 May 2024 14:30:32 +0000 Subject: [PATCH] feat(migration): added a new script that add the freeWorkShop field as a... --- .../1709023701437-remove-covid-infos.ts | 38 ++++++------- .../1714481277289-initializefreeworkshop.ts | 53 +++++++++++++++++++ .../1714483632064-addfreeworkshopcategory.ts | 40 ++++++++++++++ .../1715086059103-changedemarchestheme.ts | 30 +++++++++++ 4 files changed, 143 insertions(+), 18 deletions(-) create mode 100644 src/migrations/scripts/1714481277289-initializefreeworkshop.ts create mode 100644 src/migrations/scripts/1714483632064-addfreeworkshopcategory.ts create mode 100644 src/migrations/scripts/1715086059103-changedemarchestheme.ts diff --git a/src/migrations/scripts/1709023701437-remove-covid-infos.ts b/src/migrations/scripts/1709023701437-remove-covid-infos.ts index 6cabe01aa..1ca29871d 100644 --- a/src/migrations/scripts/1709023701437-remove-covid-infos.ts +++ b/src/migrations/scripts/1709023701437-remove-covid-infos.ts @@ -2,25 +2,27 @@ import { Db } from 'mongodb'; import { getDb } from '../migrations-utils/db'; export const up = async () => { - const db: Db = await getDb(); - - await db.collection('structures').updateMany({}, { $unset: { lockdownActivity: '' } }); - - await db.collection('parameters').drop(); - - console.log(`Update done: removed 'lockdownActivity' from 'structures' and deleted the 'parameters' collection`); + try { + const db: Db = await getDb(); + await db.collection('structures').updateMany({}, { $unset: { lockdownActivity: '' } }); + await db.collection('parameters').drop(); + console.log(`Update done: removed 'lockdownActivity' from 'structures' and deleted the 'parameters' collection`); + } catch (error) { + console.error('Error dropping lockdownActivity:', error); + } }; export const down = async () => { - const db: Db = await getDb(); - - const updateResult = await db.collection('structures').updateMany({}, { $set: { lockdownActivity: '' } }); - - await db - .collection('parameters') - .insertOne({ lockdownInfoDisplay: false, createdAt: new Date(), updatedAt: new Date() }); - - console.log( - `Downgrade done: added lockdownActivity to ${updateResult.modifiedCount} structures and added the 'parameters' collection back with its contents` - ); + try { + const db: Db = await getDb(); + const updateResult = await db.collection('structures').updateMany({}, { $set: { lockdownActivity: '' } }); + await db + .collection('parameters') + .insertOne({ lockdownInfoDisplay: false, createdAt: new Date(), updatedAt: new Date() }); + console.log( + `Downgrade done: added lockdownActivity to ${updateResult.modifiedCount} structures and added the 'parameters' collection back with its contents` + ); + } catch (error) { + console.error('Error restoring a lockdownActivity collection:', error); + } }; diff --git a/src/migrations/scripts/1714481277289-initializefreeworkshop.ts b/src/migrations/scripts/1714481277289-initializefreeworkshop.ts new file mode 100644 index 000000000..a47d45b39 --- /dev/null +++ b/src/migrations/scripts/1714481277289-initializefreeworkshop.ts @@ -0,0 +1,53 @@ +import { Db } from 'mongodb'; +import { getDb } from '../migrations-utils/db'; + +export const up = async () => { + try { + const db: Db = await getDb(); + const structures = await db.collection('structures').find().toArray(); + + for (const structure of structures) { + let freeWorkShopValue: string; + if (typeof structure.freeWorkShop === 'boolean') { + freeWorkShopValue = structure.freeWorkShop ? 'Oui' : 'Non'; + } else if (structure.freeWorkShop === 'underCondition') { + freeWorkShopValue = 'Oui, sous condition'; + } else { + freeWorkShopValue = structure.freeWorkShop; + } + + await db + .collection('structures') + .updateOne( + { _id: structure._id }, + { $set: { 'categories.freeWorkShop': freeWorkShopValue }, $unset: { freeWorkShop: '' } } + ); + } + + console.log(`Update done: Fixed invalid freeWorkShops values and move it to categories.`); + } catch (error) { + console.error('Error while fixing moving freeWorkShops to categories:', error); + } +}; + +export const down = async () => { + try { + const db: Db = await getDb(); + const structures = await db.collection('structures').find().toArray(); + + for (const structure of structures) { + const { categories } = structure; + + await db + .collection('structures') + .updateOne( + { _id: structure._id }, + { $set: { freeWorkShop: categories.freeWorkShop }, $unset: { 'categories.freeWorkShop': '' } } + ); + } + + console.log(`Downgrade done: moved freeWorkShop back from categories`); + } catch (error) { + console.error('Error moving back freeWorkShop:', error); + } +}; diff --git a/src/migrations/scripts/1714483632064-addfreeworkshopcategory.ts b/src/migrations/scripts/1714483632064-addfreeworkshopcategory.ts new file mode 100644 index 000000000..c422a90fb --- /dev/null +++ b/src/migrations/scripts/1714483632064-addfreeworkshopcategory.ts @@ -0,0 +1,40 @@ +import { getDb } from '../migrations-utils/db'; + +export const up = async () => { + try { + const db = await getDb(); + const newCategory = { + modules: [ + { + id: 'Oui', + name: 'Accompagnements gratuits', + }, + { + id: 'Oui, sous condition', + name: 'Accompagnements gratuits sous conditions', + }, + { + id: 'Non', + name: 'Accompagnements payants', + }, + ], + name: 'Gratuité', + theme: 'Gratuité', + id: 'freeWorkShop', + }; + await db.collection('categories').insertOne(newCategory); + console.log(`Update done: New category 'freeWorkShop' added successfully.`); + } catch (error) { + console.error(`Error while creating the new category 'freeWorkShop':`, error); + } +}; + +export const down = async () => { + try { + const db = await getDb(); + await db.collection('categories').deleteOne({ id: 'freeWorkShop' }); + console.log(`Downgrade done: removed the 'freeWorkShop' category.`); + } catch (error) { + console.error(`Error while removing the 'freeWorkShop' category:`, error); + } +}; diff --git a/src/migrations/scripts/1715086059103-changedemarchestheme.ts b/src/migrations/scripts/1715086059103-changedemarchestheme.ts new file mode 100644 index 000000000..ac9dc65c7 --- /dev/null +++ b/src/migrations/scripts/1715086059103-changedemarchestheme.ts @@ -0,0 +1,30 @@ +import { Db } from 'mongodb'; +import { getDb } from '../migrations-utils/db'; + +export const up = async () => { + try { + let document; + const db: Db = await getDb(); + const cursor = db.collection('categories').find({ name: 'Démarches en ligne' }); + while ((document = await cursor.next())) { + await db.collection('categories').updateOne({ _id: document._id }, { $set: { theme: 'Compétences numériques' } }); + } + console.log(`Update done: 'Démarches en ligne' theme has been changed to 'Compétences numériques'`); + } catch (error) { + console.error('Error while changing theme:', error); + } +}; + +export const down = async () => { + const db: Db = await getDb(); + try { + let document; + const cursor = db.collection('categories').find({ name: 'Démarches en ligne' }); + while ((document = await cursor.next())) { + await db.collection('categories').updateOne({ _id: document._id }, { $set: { theme: 'Démarches en ligne' } }); + } + console.log(`Downgrade done: 'Compétences numériques' theme has been changed back to 'Démarches en ligne'`); + } catch (error) { + console.error('Error while restoring theme:', error); + } +}; -- GitLab