Skip to content
Snippets Groups Projects
Commit 736bb96f authored by Antonin COQUET's avatar Antonin COQUET
Browse files

Merge branch 'feat/update-data' into 'dev'

Feat/update data

See merge request web-et-numerique/pamn_plateforme-des-acteurs-de-la-mediation-numerique/pamn_server!63
parents 66ae0e8a fd5ebaf2
No related branches found
No related tags found
3 merge requests!96release V1.10.0,!73Dev,!63Feat/update data
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;
}
......@@ -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);
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