Skip to content
Snippets Groups Projects
Commit aaf0363e authored by Etienne LOUPIAS's avatar Etienne LOUPIAS
Browse files

fix(filter): fix freeWorkshop id

parent 26978f06
2 merge requests!404V3.1.0 (sans impression),!402fix(filter): fix freeWorkshop id
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);
}
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment