From 1af55fb16563b8bbeba31717d54136ec423268a5 Mon Sep 17 00:00:00 2001 From: folland87 Date: Fri, 20 Dec 2024 10:26:03 +0100 Subject: [PATCH] rework sirene query --- .../commons/queries/sirene-updates.query.js | 86 ++++++++++++++++--- 1 file changed, 76 insertions(+), 10 deletions(-) diff --git a/src/api/commons/queries/sirene-updates.query.js b/src/api/commons/queries/sirene-updates.query.js index f007e0e0..0952fa71 100644 --- a/src/api/commons/queries/sirene-updates.query.js +++ b/src/api/commons/queries/sirene-updates.query.js @@ -1,13 +1,79 @@ -import structuresLightQuery from "./structures.light.query"; +import currentLegalCategoryQuery from './current-legal-category.query'; +import currentLocalisationQuery from './current-localisation.query'; +import currentNameQuery from './current-name.query'; +const structQuery = [ + ...currentLegalCategoryQuery, + ...currentLocalisationQuery, + ...currentNameQuery, + { + $project: { + _id: 0, + id: 1, + closureDate: { $ifNull: ['$closureDate', null] }, + creationDate: { $ifNull: ['$creationDate', null] }, + currentLocalisation: { $ifNull: ['$currentLocalisation', {}] }, + currentName: { $ifNull: ['$currentName', {}] }, + displayName: '$currentName.usualName', + emails: { $ifNull: ['$emails', []] }, + href: { $concat: ['/structures/', '$id'] }, + legalcategory: { $ifNull: ['$legalcategory', {}] }, + structureStatus: { $ifNull: ['$structureStatus', null] }, + }, + }, +]; export default [ - { - $lookup: { - from: "structures", - localField: "paysage", - foreignField: "id", - pipeline: structuresLightQuery, - as: "paysageData", - }, - }, + { + $group: { + _id: "$paysage", + siren: { $first: "$siren" }, + siret: { $first: "$siret" }, + id: { $first: "$paysage" }, + type: { $first: "$type" }, + lastModificationDate: { $max: "$lastChecked" }, + updates: { + $push: { + $cond: { + if: { $eq: ["$changeType", "change"] }, + then: "$$ROOT", + else: "$$REMOVE" + } + } + }, + checks: { + $push: { + $cond: { + if: { $eq: ["$changeType", "check"] }, + then: "$$ROOT", + else: "$$REMOVE" + } + } + } + } + }, + { + $lookup: { + from: "structures", + localField: "_id", + foreignField: "id", + pipeline: structQuery, + as: "paysageData", + }, + }, + { + $project: { + _id: 0, + id: 1, + type: 1, + lastModificationDate: 1, + siren: 1, + siret: 1, + paysageData: { $arrayElemAt: ["$paysageData", 0] }, + updates: 1, + checks: 1, + } + }, + { + $sort: { lastModificationDate: -1 } + } ];