|
| 1 | +import { db } from "../../services/mongo.service"; |
| 2 | + |
| 3 | +import currentCategoryQuery from "../../api/commons/queries/current-category.query"; |
| 4 | +import currentEmailsQuery from "../../api/commons/queries/current-emails.query"; |
| 5 | +import currentIdentifiersQuery from "../../api/commons/queries/current-identifiers.query"; |
| 6 | +import currentLegalCategoryQuery from "../../api/commons/queries/current-legal-category.query"; |
| 7 | +import currentLocalisationQuery from "../../api/commons/queries/current-localisation.query"; |
| 8 | +import currentNameQuery from "../../api/commons/queries/current-name.query"; |
| 9 | +import currentWebsitesQuery from "../../api/commons/queries/current-websites.query"; |
| 10 | +import currentSocialsQuery from "../../api/commons/queries/current-socials.query"; |
| 11 | +import relationTypesLightQuery from '../../api/commons/queries/relation-types.light.query'; |
| 12 | +import categoryLightQuery from '../../api/commons/queries/categories.light.query'; |
| 13 | +import legalCategoryLightQuery from '../../api/commons/queries/legal-categories.light.query'; |
| 14 | +import personLightQuery from '../../api/commons/queries/persons.light.query'; |
| 15 | +import prizeLightQuery from '../../api/commons/queries/prizes.light.query'; |
| 16 | +import structuresDumpQuery from '../../api/commons/queries/structures.dump.query'; |
| 17 | +import supervisingMinistersLightQuery from '../../api/commons/queries/supervising-ministers.light.query'; |
| 18 | +import termsLightQuery from '../../api/commons/queries/terms.light.query'; |
| 19 | + |
| 20 | +function getRelatedObject(localField) { |
| 21 | + return ([ |
| 22 | + { |
| 23 | + $lookup: { |
| 24 | + from: 'categories', |
| 25 | + localField, |
| 26 | + foreignField: 'id', |
| 27 | + pipeline: categoryLightQuery, |
| 28 | + as: 'relatedCategories', |
| 29 | + }, |
| 30 | + }, |
| 31 | + { |
| 32 | + $lookup: { |
| 33 | + from: 'legalcategories', |
| 34 | + localField, |
| 35 | + foreignField: 'id', |
| 36 | + pipeline: legalCategoryLightQuery, |
| 37 | + as: 'relatedLegalCategories', |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + $lookup: { |
| 42 | + from: 'terms', |
| 43 | + localField, |
| 44 | + foreignField: 'id', |
| 45 | + pipeline: termsLightQuery, |
| 46 | + as: 'relatedTerms', |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + $lookup: { |
| 51 | + from: 'persons', |
| 52 | + localField, |
| 53 | + foreignField: 'id', |
| 54 | + pipeline: personLightQuery, |
| 55 | + as: 'relatedPersons', |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + $lookup: { |
| 60 | + from: 'prizes', |
| 61 | + localField, |
| 62 | + foreignField: 'id', |
| 63 | + pipeline: prizeLightQuery, |
| 64 | + as: 'relatedPrizes', |
| 65 | + }, |
| 66 | + }, |
| 67 | + { |
| 68 | + $lookup: { |
| 69 | + from: 'structures', |
| 70 | + localField, |
| 71 | + foreignField: 'id', |
| 72 | + pipeline: structuresDumpQuery, |
| 73 | + as: 'relatedStructures', |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + $lookup: { |
| 78 | + from: 'supervisingministers', |
| 79 | + localField, |
| 80 | + foreignField: 'id', |
| 81 | + pipeline: supervisingMinistersLightQuery, |
| 82 | + as: 'relatedMinisters', |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + $set: { |
| 87 | + related: { |
| 88 | + $concatArrays: [ |
| 89 | + '$relatedLegalCategories', |
| 90 | + '$relatedStructures', |
| 91 | + '$relatedPrizes', |
| 92 | + '$relatedPersons', |
| 93 | + '$relatedTerms', |
| 94 | + '$relatedCategories', |
| 95 | + '$relatedMinisters', |
| 96 | + ], |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + ]); |
| 101 | +} |
| 102 | + |
| 103 | +export const relatedObjectLookup = [ |
| 104 | + ...getRelatedObject('relatedObjectId'), |
| 105 | + { $set: { relatedObject: { $arrayElemAt: ['$related', 0] } } }, |
| 106 | +]; |
| 107 | +export const resourceLookup = [ |
| 108 | + ...getRelatedObject('resourceId'), |
| 109 | + { $set: { resource: { $arrayElemAt: ['$related', 0] } } }, |
| 110 | +]; |
| 111 | + |
| 112 | +const relationTypeQuery = [ |
| 113 | + { |
| 114 | + $lookup: { |
| 115 | + from: 'relationtypes', |
| 116 | + localField: 'relationTypeId', |
| 117 | + foreignField: 'id', |
| 118 | + pipeline: relationTypesLightQuery, |
| 119 | + as: 'relationType', |
| 120 | + }, |
| 121 | + }, |
| 122 | + { $set: { relationType: { $arrayElemAt: ['$relationType', 0] } } }, |
| 123 | +] |
| 124 | +const relationGroupQuery = [ |
| 125 | + { |
| 126 | + $lookup: { |
| 127 | + from: 'relationsgroups', |
| 128 | + localField: 'relationsGroupId', |
| 129 | + foreignField: 'id', |
| 130 | + pipeline: [ |
| 131 | + { |
| 132 | + $project: { |
| 133 | + _id: 0, |
| 134 | + id: 1, |
| 135 | + resourceId: 1, |
| 136 | + name: 1, |
| 137 | + accepts: 1, |
| 138 | + priority: 1, |
| 139 | + }, |
| 140 | + }, |
| 141 | + ], |
| 142 | + as: 'relationGroup', |
| 143 | + }, |
| 144 | + }, |
| 145 | + { $set: { relationGroup: { $arrayElemAt: ['$relationGroup', 0] } } }, |
| 146 | +] |
| 147 | + |
| 148 | +const projection = { |
| 149 | + _id: 0, |
| 150 | + id: 1, |
| 151 | + resourceId: 1, |
| 152 | + relatedObjectId: 1, |
| 153 | + relationGroup: { $ifNull: ['$relationGroup', null] }, |
| 154 | + relationType: { $ifNull: ['$relationType', { priority: 99 }] }, |
| 155 | + relationTag: { $ifNull: ['$relationTag', null] }, |
| 156 | + startDate: { $ifNull: ['$startDate', null] }, |
| 157 | + endDate: { $ifNull: ['$endDate', null] }, |
| 158 | + endDatePrevisional: { $ifNull: ['$endDatePrevisional', null] }, |
| 159 | + mandatePosition: { $ifNull: ['$mandatePosition', null] }, |
| 160 | + mandateReason: { $ifNull: ['$mandateReason', null] }, |
| 161 | + mandateEmail: { $ifNull: ['$mandateEmail', null] }, |
| 162 | + personalEmail: { $ifNull: ['$personalEmail', null] }, |
| 163 | + mandatePhonenumber: { $ifNull: ['$mandatePhonenumber', null] }, |
| 164 | + mandateTemporary: { $ifNull: ['$mandateTemporary', null] }, |
| 165 | + mandatePrecision: { $ifNull: ['$mandatePrecision', null] }, |
| 166 | + laureatePrecision: { $ifNull: ['$laureatePrecision', null] }, |
| 167 | + active: { $ifNull: ['$active', null] }, |
| 168 | +} |
| 169 | + |
| 170 | +const relationRelatedQuery = [ |
| 171 | + ...relatedObjectLookup, |
| 172 | + ...relationTypeQuery, |
| 173 | + ...relationGroupQuery, |
| 174 | + { |
| 175 | + $project: {...projection, relatedObject: 1 }, |
| 176 | + }, |
| 177 | +]; |
| 178 | + |
| 179 | +const relationResourceQuery = [ |
| 180 | + ...resourceLookup, |
| 181 | + ...relationTypeQuery, |
| 182 | + ...relationGroupQuery, |
| 183 | + { |
| 184 | + $project: {...projection, resource: 1 }, |
| 185 | + }, |
| 186 | +]; |
| 187 | + |
| 188 | + |
| 189 | + |
| 190 | +const structureDumpQuery = [ |
| 191 | + ...currentCategoryQuery, |
| 192 | + ...currentEmailsQuery, |
| 193 | + ...currentIdentifiersQuery, |
| 194 | + ...currentLegalCategoryQuery, |
| 195 | + ...currentLocalisationQuery, |
| 196 | + ...currentNameQuery, |
| 197 | + ...currentSocialsQuery, |
| 198 | + ...currentWebsitesQuery, |
| 199 | + { |
| 200 | + $lookup: { |
| 201 | + from: 'relationships', |
| 202 | + localField: 'id', |
| 203 | + foreignField: 'resourceId', |
| 204 | + pipeline: [...relationRelatedQuery], |
| 205 | + as: 'rel1' |
| 206 | + } |
| 207 | + }, |
| 208 | + { |
| 209 | + $lookup: { |
| 210 | + from: 'relationships', |
| 211 | + localField: 'id', |
| 212 | + foreignField: 'relatedObjectId', |
| 213 | + pipeline: [...relationResourceQuery], |
| 214 | + as: 'rel2' |
| 215 | + } |
| 216 | + }, |
| 217 | + { |
| 218 | + $set: { |
| 219 | + relations: { |
| 220 | + $concatArrays: ['$rel1', '$rel2'] |
| 221 | + } |
| 222 | + } |
| 223 | + }, |
| 224 | + { |
| 225 | + $project: { |
| 226 | + _id: 0, |
| 227 | + id: 1, |
| 228 | + object: 'structures', |
| 229 | + status: { $ifNull: ['$structureStatus', null] }, |
| 230 | + displayName: '$currentName.usualName', |
| 231 | + currentName: { $ifNull: ['$currentName', {}] }, |
| 232 | + descriptionEn: { $ifNull: ['$descriptionEn', null] }, |
| 233 | + descriptionFr: { $ifNull: ['$descriptionFr', null] }, |
| 234 | + currentLocalisation: { $ifNull: ['$currentLocalisation', {}] }, |
| 235 | + localisations: 1, |
| 236 | + category: { $ifNull: ['$category', {}] }, |
| 237 | + legalcategory: { $ifNull: ['$legalcategory', {}] }, |
| 238 | + identifiers: { $ifNull: ['$identifiers', []] }, |
| 239 | + relations: { $ifNull: ['$relations', []] }, |
| 240 | + socialmedias: { $ifNull: ['$socialmedias', []] }, |
| 241 | + categories: { $ifNull: ['$categories', []] }, |
| 242 | + closureDate: { $ifNull: ['$closureDate', null] }, |
| 243 | + createdAt: 1, |
| 244 | + creationDate: { $ifNull: ['$creationDate', null] }, |
| 245 | + emails: { $ifNull: ['$emails', []] }, |
| 246 | + websites: { $ifNull: ['$websites', []] }, |
| 247 | + dumpedAt: new Date() |
| 248 | + }, |
| 249 | + }, |
| 250 | + { |
| 251 | + $out: 'structures-dump', |
| 252 | + }, |
| 253 | +]; |
| 254 | + |
| 255 | + |
| 256 | +export default async function createStructuresDump() { |
| 257 | + console.log('--------------------------------------'); |
| 258 | + console.log('Creating structures dump...'); |
| 259 | + console.log('--------------------------------------'); |
| 260 | + try { |
| 261 | + const res = await db.collection('structures').aggregate(structureDumpQuery, { allowDiskUse: true }).toArray(); |
| 262 | + console.log('--------------------------------------'); |
| 263 | + console.log('Structures dump created successfully', res); |
| 264 | + console.log('--------------------------------------'); |
| 265 | + return {status: 'success', data: res}; |
| 266 | + } catch (error) { |
| 267 | + console.log('--------------------------------------'); |
| 268 | + console.log('--------------------------------------'); |
| 269 | + console.log('--------------------------------------'); |
| 270 | + console.error('Error creating structures dump:', error); |
| 271 | + console.log('--------------------------------------'); |
| 272 | + console.log('--------------------------------------'); |
| 273 | + console.log('--------------------------------------'); |
| 274 | + throw error; |
| 275 | + } |
| 276 | +} |
0 commit comments