diff --git a/src/migrations/scripts/1618322296327-clean-data.ts b/src/migrations/scripts/1618322296327-clean-data.ts new file mode 100644 index 0000000000000000000000000000000000000000..a0278da0f19684b2a751bcabf253242b31359483 --- /dev/null +++ b/src/migrations/scripts/1618322296327-clean-data.ts @@ -0,0 +1,103 @@ +import { Db } from 'mongodb'; +import { getDb } from '../migrations-utils/db'; + +export const up = async () => { + const db: Db = await getDb(); + + const cursor = db.collection('structures').find({}); + let document; + while ((document = await cursor.next())) { + const newDoc = updateStructure(document); + await db.collection('structures').updateOne({ _id: document._id }, [{ $set: newDoc }]); + } + console.log(`Update done`); +}; + +export const down = async () => { + const db: Db = await getDb(); + + const cursor = db.collection('structures').find({}); + let document; + while ((document = await cursor.next())) { + const newDoc = downgradeStructure(document); + await db.collection('structures').updateOne({ _id: document._id }, [{ $set: newDoc }]); + } + console.log(`Update done`); +}; + +function updateStructure(doc) { + doc = updateHours(doc); + doc = removeUnusedFields(doc); + return doc; +} + +function downgradeStructure(doc) { + doc = restoreHours(doc); + return doc; +} + +function updateHours(doc) { + if (doc.hours) { + Object.keys(doc.hours).forEach((key) => { + if (doc.hours[key].time.length > 0) { + doc.hours[key].time.forEach((timeRange) => { + timeRange.openning = formatHours(timeRange.openning); + timeRange.closing = formatHours(timeRange.closing); + }); + } + }); + return doc; + } else { + console.warn(`No hours on doc ${doc._id}`); + return doc; + } +} + +function restoreHours(doc) { + if (doc.hours) { + Object.keys(doc.hours).forEach((key) => { + if (doc.hours[key].time.length > 0) { + doc.hours[key].time.forEach((timeRange) => { + timeRange.openning = formatBackHours(timeRange.openning); + timeRange.closing = formatBackHours(timeRange.closing); + }); + } + }); + return doc; + } else { + console.warn(`No hours on doc ${doc._id}`); + return doc; + } +} + +function formatHours(hour): string { + const stringifiedHour = hour.toString(); + if (stringifiedHour.length === 3) { + // 930 + return stringifiedHour.slice(0, 1) + ':' + stringifiedHour.slice(1, 3); + } else if (stringifiedHour.length === 4) { + // 1200 + return stringifiedHour.slice(0, 2) + ':' + stringifiedHour.slice(2, 4); + } +} + +function formatBackHours(hour): number { + const splitedHour = hour.split(':'); + return parseInt(''.concat(...splitedHour), 10); +} + +function removeUnusedFields(doc) { + if (doc['equipmentDetails']) { + delete doc['equipmentDetails']; + } + if (doc['nomDeLusager']) { + delete doc['nomDeLusager']; + } + if (doc['statutJuridique']) { + delete doc['statutJuridique']; + } + if (doc['documentsMeeting']) { + delete doc['documentsMeeting']; + } + return doc; +} diff --git a/src/structures/schemas/time.schema.ts b/src/structures/schemas/time.schema.ts index 986b7773a38ace7f052d28519321bbb2e37efeab..65109f63a40c61e04044ddb49d8ba95c145fa772 100644 --- a/src/structures/schemas/time.schema.ts +++ b/src/structures/schemas/time.schema.ts @@ -4,8 +4,8 @@ import { Document } from 'mongoose'; export type TimeDocument = Time & Document; export class Time { - openning: number; - closing: number; + openning: string; + closing: string; } export const TimeSchema = SchemaFactory.createForClass(Time);