diff --git a/src/migrations/scripts/1666013714410-structure-new-repository.ts b/src/migrations/scripts/1666013714410-structure-new-repository.ts index d85cf14401c91e4a82f7a409da37b0af2d554995..2b29a53304ce1e4d2925ae669ecaebe2d702d01e 100644 --- a/src/migrations/scripts/1666013714410-structure-new-repository.ts +++ b/src/migrations/scripts/1666013714410-structure-new-repository.ts @@ -14,9 +14,15 @@ export const up = async () => { await db.collection('structures').updateOne({ _id: document._id }, [{ $set: newDoc }]); } console.log(`Update done : new structrure repository ref`); - await db.dropCollection('categoriesaccompagnements'); - await db.dropCollection('categoriesformations'); - await db.dropCollection('categoriesothers'); + try { + await db.dropCollection('categoriesaccompagnements'); + await db.dropCollection('categoriesformations'); + await db.dropCollection('categoriesothers'); + } catch (e) { + if (e.code === 26) { + console.warn('Already deleted collections'); + } + } console.log(`Remove old referential done`); }; @@ -64,15 +70,15 @@ const upgradeStructure = (doc: any): StructureDocument => { }; const mapCategories = (doc: any): StructureDocument => { - doc.categories.accessModality = doc.accessModality; - doc.categories.labelsQualifications = doc.labelsQualifications; + doc.categories.accessModality = doc.accessModality || []; + doc.categories.labelsQualifications = doc.labelsQualifications || []; return doc; }; const mapEquipments = (doc: any): StructureDocument => { const oldMaterial = doc.equipmentsAndServices; const selfServiceMaterial = oldMaterial - .map((el) => { + ?.map((el) => { switch (el) { case 'ordinateurs': return 'computer'; @@ -88,7 +94,7 @@ const mapEquipments = (doc: any): StructureDocument => { }) .filter((el) => el !== ''); const equipmentsServices = oldMaterial - .map((el) => { + ?.map((el) => { switch (el) { case 'donDeMateriels': return 'donDeMateriels'; @@ -101,15 +107,15 @@ const mapEquipments = (doc: any): StructureDocument => { } }) .filter((el) => el !== ''); - doc.categories.selfServiceMaterial = [...new Set(selfServiceMaterial)]; - doc.categories.equipmentsServices = [...new Set(equipmentsServices)]; + doc.categories.selfServiceMaterial = [...new Set(selfServiceMaterial || [])]; + doc.categories.equipmentsServices = [...new Set(equipmentsServices || [])]; return doc; }; const mapOnlineProcedures = (doc: any): StructureDocument => { const oldModality = doc.accessRight; const oldHelping = doc.proceduresAccompaniment; - const newModality = oldModality.map((el) => { + const newModality = oldModality?.map((el) => { switch (el) { case '84': return 'work'; @@ -131,7 +137,7 @@ const mapOnlineProcedures = (doc: any): StructureDocument => { return ''; } }); - const newHelping = oldHelping.map((el) => { + const newHelping = oldHelping?.map((el) => { switch (el) { case 'accompagnantCaf': return 'caf'; @@ -151,7 +157,7 @@ const mapOnlineProcedures = (doc: any): StructureDocument => { return ''; } }); - const onlineProcedures = [...new Set([...newModality, ...newHelping].filter((el) => el !== ''))]; + const onlineProcedures = [...new Set([...(newModality || []), ...(newHelping || [])].filter((el) => el !== ''))]; doc.categories.onlineProcedures = onlineProcedures; return doc; }; @@ -159,7 +165,7 @@ const mapOnlineProcedures = (doc: any): StructureDocument => { const mapWorkInsertion = (doc: any): StructureDocument => { const oldSocialAndPro = doc.socialAndProfessional; const baseSkills = oldSocialAndPro - .map((el) => { + ?.map((el) => { switch (el) { case '6': return 'work'; @@ -184,13 +190,13 @@ const mapWorkInsertion = (doc: any): StructureDocument => { } }) .filter((el) => el !== ''); - doc.categories.baseSkills = [...new Set(baseSkills)]; + doc.categories.baseSkills = [...new Set(baseSkills || [])]; return doc; }; const mapDigitalCultureSecurity = (doc: any): StructureDocument => { const oldData = doc.digitalCultureSecurity; const baseSkills = oldData - .map((el) => { + ?.map((el) => { switch (el) { case '5': return 'communication'; @@ -210,7 +216,7 @@ const mapDigitalCultureSecurity = (doc: any): StructureDocument => { }) .filter((el) => el !== ''); const advancedSkills = oldData - .map((el) => { + ?.map((el) => { switch (el) { case '2': return 'cyberAndConf'; @@ -229,7 +235,7 @@ const mapDigitalCultureSecurity = (doc: any): StructureDocument => { } }) .filter((el) => el !== ''); - doc.categories.baseSkills = [...new Set([...baseSkills, ...doc.categories.baseSkills])]; + doc.categories.baseSkills = [...new Set([...(baseSkills || []), ...doc.categories.baseSkills])]; doc.categories.advancedSkills = [...new Set(advancedSkills)]; return doc; }; @@ -237,7 +243,7 @@ const mapDigitalCultureSecurity = (doc: any): StructureDocument => { const mapbaseComp = (doc: any): StructureDocument => { const oldData = doc.baseSkills; const baseSkills = oldData - .map((el) => { + ?.map((el) => { switch (el) { case '260': return 'computer'; @@ -257,7 +263,7 @@ const mapbaseComp = (doc: any): StructureDocument => { }) .filter((el) => el !== ''); const advancedSkills = oldData - .map((el) => { + ?.map((el) => { switch (el) { case '1': return 'cyberAndConf'; @@ -266,14 +272,14 @@ const mapbaseComp = (doc: any): StructureDocument => { } }) .filter((el) => el !== ''); - doc.categories.baseSkills = [...new Set([...baseSkills, ...doc.categories.baseSkills])]; - doc.categories.advancedSkills = [...new Set([...advancedSkills, ...doc.categories.advancedSkills])]; + doc.categories.baseSkills = [...new Set([...(baseSkills || []), ...doc.categories.baseSkills])]; + doc.categories.advancedSkills = [...new Set([...(advancedSkills || []), ...doc.categories.advancedSkills])]; return doc; }; const mapParentingHelp = (doc: any): StructureDocument => { const oldData = doc.parentingHelp; const baseSkills = oldData - .map((el) => { + ?.map((el) => { switch (el) { case '82': return 'scolarity'; @@ -283,7 +289,7 @@ const mapParentingHelp = (doc: any): StructureDocument => { }) .filter((el) => el !== ''); const advancedSkills = oldData - .map((el) => { + ?.map((el) => { switch (el) { case '3': return 'parenting'; @@ -294,8 +300,8 @@ const mapParentingHelp = (doc: any): StructureDocument => { } }) .filter((el) => el !== ''); - doc.categories.baseSkills = [...new Set([...baseSkills, ...doc.categories.baseSkills])]; - doc.categories.advancedSkills = [...new Set([...advancedSkills, ...doc.categories.advancedSkills])]; + doc.categories.baseSkills = [...new Set([...(baseSkills || []), ...doc.categories.baseSkills])]; + doc.categories.advancedSkills = [...new Set([...(advancedSkills || []), ...doc.categories.advancedSkills])]; return doc; }; const mapPublics = (doc: any): StructureDocument => { @@ -303,7 +309,7 @@ const mapPublics = (doc: any): StructureDocument => { const languageAndIlliteracy = []; const handicaps = []; const publicOthers = []; - doc.publics.forEach((el) => { + doc.publics?.forEach((el) => { switch (el) { case 'toutPublic': age.push('family'); @@ -332,7 +338,7 @@ const mapPublics = (doc: any): StructureDocument => { }); doc.categories.age = [...new Set([...age])]; - doc.publicsAccompaniment.forEach((el) => { + doc.publicsAccompaniment?.forEach((el) => { switch (el) { case "Personnes en situation d'illetrisme": languageAndIlliteracy.push('illettrisme');