Skip to content
Snippets Groups Projects
Commit c112c287 authored by Hugo SUBTIL's avatar Hugo SUBTIL
Browse files

Merge branch 'fix/update-structure-formated' into 'dev'

Fix/update structure formated

See merge request web-et-numerique/pamn_plateforme-des-acteurs-de-la-mediation-numerique/pamn_server!71
parents a03a3c3e 2253581b
Branches
Tags
3 merge requests!96release V1.10.0,!73Dev,!71Fix/update structure formated
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) {
return updateHours(doc);
}
function downgradeStructure(doc) {
return restoreHours(doc);
}
function updateHours(doc) {
if (doc.hours) {
Object.keys(doc.hours).forEach((key) => {
doc.hours[key].time.forEach((timeRange) => {
timeRange.opening = timeRange.openning;
delete timeRange.openning;
});
});
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 = timeRange.opening;
delete timeRange.opening;
});
}
});
return doc;
} else {
console.warn(`No hours on doc ${doc._id}`);
return doc;
}
}
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) {
return updateTimestamp(doc);
}
function downgradeStructure(doc) {
return restoreTimestamp(doc);
}
function updateTimestamp(doc) {
doc.createdAt = new Date(doc.createdAt).toISOString();
doc.updatedAt = new Date(doc.updatedAt).toISOString();
return doc;
}
function restoreTimestamp(doc) {
doc.createdAt = new Date(doc.createdAt).toString();
doc.updatedAt = new Date(doc.updatedAt).toISOString();
return doc;
}
......@@ -4,7 +4,7 @@ import { Document } from 'mongoose';
export type TimeDocument = Time & Document;
export class Time {
openning: string;
opening: string;
closing: string;
}
......
......@@ -185,57 +185,59 @@ export class StructuresService {
}
})
);
return (await this.structureModel.find({ deletedAt: { $exists: false }, accountVerified: true }).exec()).map(
(structure) => {
structure.proceduresAccompaniment = this.mapModules(
structure.proceduresAccompaniment,
accompagnementCategories.find((category) => category.name === 'Accompagnement des démarches').modules
);
structure.labelsQualifications = this.mapModules(
structure.labelsQualifications,
otherCategories.find((category) => category.name === 'Labels et qualifications').modules
);
structure.publics = this.mapModules(
structure.publics,
otherCategories.find((category) => category.name === 'Publics acceptés').modules
);
structure.accessModality = this.mapModules(
structure.accessModality,
otherCategories.find((category) => category.name === "Modalités d'accès").modules
);
structure.publicsAccompaniment = this.mapModules(
structure.publicsAccompaniment,
otherCategories.find((category) => category.name === 'Accompagnement des publics spécifique').modules
);
structure.equipmentsAndServices = this.mapModules(
structure.equipmentsAndServices,
otherCategories.find((category) => category.name === 'Équipements et services proposés').modules
);
structure.baseSkills = this.mapFormationModules(
structure.baseSkills,
formationCategories.find((category) => category.name === 'Les compétences de base').modules
);
structure.accessRight = this.mapFormationModules(
structure.accessRight,
formationCategories.find((category) => category.name === 'Accès aux droits').modules
);
structure.socialAndProfessional = this.mapFormationModules(
structure.socialAndProfessional,
formationCategories.find((category) => category.name === 'Insertion sociale et professionnelle').modules
);
structure.parentingHelp = this.mapFormationModules(
structure.parentingHelp,
formationCategories.find((category) => category.name === 'Aide à la parentalité').modules
);
structure.digitalCultureSecurity = this.mapFormationModules(
structure.digitalCultureSecurity,
formationCategories.find((category) => category.name === 'Culture et sécurité numérique').modules
);
return structure;
}
);
return (
await this.structureModel
.find({ deletedAt: { $exists: false }, accountVerified: true })
.select('-_id -accountVerified -otherDescription')
.exec()
).map((structure) => {
structure.proceduresAccompaniment = this.mapModules(
structure.proceduresAccompaniment,
accompagnementCategories.find((category) => category.name === 'Accompagnement des démarches').modules
);
structure.labelsQualifications = this.mapModules(
structure.labelsQualifications,
otherCategories.find((category) => category.name === 'Labels et qualifications').modules
);
structure.publics = this.mapModules(
structure.publics,
otherCategories.find((category) => category.name === 'Publics acceptés').modules
);
structure.accessModality = this.mapModules(
structure.accessModality,
otherCategories.find((category) => category.name === "Modalités d'accès").modules
);
structure.publicsAccompaniment = this.mapModules(
structure.publicsAccompaniment,
otherCategories.find((category) => category.name === 'Accompagnement des publics spécifique').modules
);
structure.equipmentsAndServices = this.mapModules(
structure.equipmentsAndServices,
otherCategories.find((category) => category.name === 'Équipements et services proposés').modules
);
structure.baseSkills = this.mapFormationModules(
structure.baseSkills,
formationCategories.find((category) => category.name === 'Les compétences de base').modules
);
structure.accessRight = this.mapFormationModules(
structure.accessRight,
formationCategories.find((category) => category.name === 'Accès aux droits').modules
);
structure.socialAndProfessional = this.mapFormationModules(
structure.socialAndProfessional,
formationCategories.find((category) => category.name === 'Insertion sociale et professionnelle').modules
);
structure.parentingHelp = this.mapFormationModules(
structure.parentingHelp,
formationCategories.find((category) => category.name === 'Aide à la parentalité').modules
);
structure.digitalCultureSecurity = this.mapFormationModules(
structure.digitalCultureSecurity,
formationCategories.find((category) => category.name === 'Culture et sécurité numérique').modules
);
return structure;
});
}
public mapFormationModules(structureModule: string[], baseModule: CategoriesFormationsModule[]): string[] {
if (structureModule == []) {
return [];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment