|
| 1 | +import currentCategoryQuery from "../../api/commons/queries/current-category.query"; |
| 2 | +import currentEmailsQuery from "../../api/commons/queries/current-emails.query"; |
| 3 | +import currentIdentifiersQuery from "../../api/commons/queries/current-identifiers.query"; |
| 4 | +import currentLegalCategoryQuery from "../../api/commons/queries/current-legal-category.query"; |
| 5 | +import currentLocalisationQuery from "../../api/commons/queries/current-localisation.query"; |
| 6 | +import currentNameQuery from "../../api/commons/queries/current-name.query"; |
| 7 | +import currentWebsitesQuery from "../../api/commons/queries/current-websites.query"; |
| 8 | +import currentSocialsQuery from "../../api/commons/queries/current-socials.query"; |
| 9 | +import relationsQuery from "../../api/commons/queries/relations.query"; |
| 10 | + |
| 11 | + |
| 12 | +const structureDumpQuery = [ |
| 13 | + ...metas, |
| 14 | + ...currentCategoryQuery, |
| 15 | + ...currentEmailsQuery, |
| 16 | + ...currentIdentifiersQuery, |
| 17 | + ...currentLegalCategoryQuery, |
| 18 | + ...currentLocalisationQuery, |
| 19 | + ...currentNameQuery, |
| 20 | + ...currentSocialsQuery, |
| 21 | + ...currentWebsitesQuery, |
| 22 | + { |
| 23 | + $lookup: { |
| 24 | + from: 'relationships', |
| 25 | + localField: 'id', |
| 26 | + foreignField: 'resourceId', |
| 27 | + pipeline: [...relationsQuery], |
| 28 | + as: 'rel1' |
| 29 | + } |
| 30 | + }, |
| 31 | + { |
| 32 | + $lookup: { |
| 33 | + from: 'relationships', |
| 34 | + localField: 'id', |
| 35 | + foreignField: 'relatedObjectId', |
| 36 | + pipeline: [...relationsQuery], |
| 37 | + as: 'rel2' |
| 38 | + } |
| 39 | + }, |
| 40 | + { |
| 41 | + $set: { |
| 42 | + relations: { |
| 43 | + $concatArrays: ['$rel1', '$rel2'] |
| 44 | + } |
| 45 | + } |
| 46 | + }, |
| 47 | + { |
| 48 | + $project: { |
| 49 | + _id: 0, |
| 50 | + id: 1, |
| 51 | + object: 'structures', |
| 52 | + status: { $ifNull: ['$structureStatus', null] }, |
| 53 | + displayName: '$currentName.usualName', |
| 54 | + currentName: { $ifNull: ['$currentName', {}] }, |
| 55 | + descriptionEn: { $ifNull: ['$descriptionEn', null] }, |
| 56 | + descriptionFr: { $ifNull: ['$descriptionFr', null] }, |
| 57 | + currentLocalisation: { $ifNull: ['$currentLocalisation', {}] }, |
| 58 | + localisations: 1, |
| 59 | + category: { $ifNull: ['$category', {}] }, |
| 60 | + legalcategory: { $ifNull: ['$legalcategory', {}] }, |
| 61 | + identifiers: { $ifNull: ['$identifiers', []] }, |
| 62 | + relations: { $ifNull: ['$relations', []] }, |
| 63 | + socialmedias: { $ifNull: ['$socialmedias', []] }, |
| 64 | + categories: { $ifNull: ['$categories', []] }, |
| 65 | + closureDate: { $ifNull: ['$closureDate', null] }, |
| 66 | + createdAt: 1, |
| 67 | + creationDate: { $ifNull: ['$creationDate', null] }, |
| 68 | + emails: { $ifNull: ['$emails', []] }, |
| 69 | + websites: { $ifNull: ['$websites', []] }, |
| 70 | + dumpedAt: new Date() |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + $out: 'structures-dump', |
| 75 | + }, |
| 76 | +]; |
| 77 | + |
| 78 | + |
| 79 | +export default async function createStructuresDump() { |
| 80 | + try { |
| 81 | + const res = await db.collection('structures').aggregate(structureDumpQuery, { allowDiskUse: true }).toArray(); |
| 82 | + return {status: 'success', data: res}; |
| 83 | + } catch (error) { |
| 84 | + console.error('Error creating structures dump:', error); |
| 85 | + throw error; |
| 86 | + } |
| 87 | +} |
0 commit comments