@@ -10,9 +10,107 @@ import currentWebsitesQuery from "../../api/commons/queries/current-websites.que
1010import currentSocialsQuery from "../../api/commons/queries/current-socials.query" ;
1111import { relatedObjectLookup , resourceLookup } from '../../api/commons/queries/related-object.query' ;
1212import relationTypesLightQuery from '../../api/commons/queries/relation-types.light.query' ;
13+ import categoryLightQuery from '../../api/commons/queries/categories.light.query' ;
14+ import legalCategoryLightQuery from '../../api/commons/queries/legal-categories.light.query' ;
15+ import personLightQuery from '../../api/commons/queries/persons.light.query' ;
16+ import prizeLightQuery from '../../api/commons/queries/prizes.light.query' ;
17+ import structuresDumpQuery from '../../api/commons/queries/structures.dump.query' ;
18+ import supervisingMinistersLightQuery from '../../api/commons/queries/supervising-ministers.light.query' ;
19+ import termsLightQuery from '../../api/commons/queries/terms.light.query' ;
1320
14- const relationRelatedQuery = [
15- ...relatedObjectLookup ,
21+ function getRelatedObject ( localField ) {
22+ return ( [
23+ {
24+ $lookup : {
25+ from : 'categories' ,
26+ localField,
27+ foreignField : 'id' ,
28+ pipeline : categoryLightQuery ,
29+ as : 'relatedCategories' ,
30+ } ,
31+ } ,
32+ {
33+ $lookup : {
34+ from : 'legalcategories' ,
35+ localField,
36+ foreignField : 'id' ,
37+ pipeline : legalCategoryLightQuery ,
38+ as : 'relatedLegalCategories' ,
39+ } ,
40+ } ,
41+ {
42+ $lookup : {
43+ from : 'terms' ,
44+ localField,
45+ foreignField : 'id' ,
46+ pipeline : termsLightQuery ,
47+ as : 'relatedTerms' ,
48+ } ,
49+ } ,
50+ {
51+ $lookup : {
52+ from : 'persons' ,
53+ localField,
54+ foreignField : 'id' ,
55+ pipeline : personLightQuery ,
56+ as : 'relatedPersons' ,
57+ } ,
58+ } ,
59+ {
60+ $lookup : {
61+ from : 'prizes' ,
62+ localField,
63+ foreignField : 'id' ,
64+ pipeline : prizeLightQuery ,
65+ as : 'relatedPrizes' ,
66+ } ,
67+ } ,
68+ {
69+ $lookup : {
70+ from : 'structures' ,
71+ localField,
72+ foreignField : 'id' ,
73+ pipeline : structuresDumpQuery ,
74+ as : 'relatedStructures' ,
75+ } ,
76+ } ,
77+ {
78+ $lookup : {
79+ from : 'supervisingministers' ,
80+ localField,
81+ foreignField : 'id' ,
82+ pipeline : supervisingMinistersLightQuery ,
83+ as : 'relatedMinisters' ,
84+ } ,
85+ } ,
86+ {
87+ $set : {
88+ related : {
89+ $concatArrays : [
90+ '$relatedLegalCategories' ,
91+ '$relatedStructures' ,
92+ '$relatedPrizes' ,
93+ '$relatedPersons' ,
94+ '$relatedTerms' ,
95+ '$relatedCategories' ,
96+ '$relatedMinisters' ,
97+ ] ,
98+ } ,
99+ } ,
100+ } ,
101+ ] ) ;
102+ }
103+
104+ export const relatedObjectLookup = [
105+ ...getRelatedObject ( 'relatedObjectId' ) ,
106+ { $set : { relatedObject : { $arrayElemAt : [ '$related' , 0 ] } } } ,
107+ ] ;
108+ export const resourceLookup = [
109+ ...getRelatedObject ( 'resourceId' ) ,
110+ { $set : { resource : { $arrayElemAt : [ '$related' , 0 ] } } } ,
111+ ] ;
112+
113+ const relationTypeQuery = [
16114 {
17115 $lookup : {
18116 from : 'relationtypes' ,
@@ -23,34 +121,66 @@ const relationRelatedQuery = [
23121 } ,
24122 } ,
25123 { $set : { relationType : { $arrayElemAt : [ '$relationType' , 0 ] } } } ,
124+ ]
125+ const relationGroupQuery = [
26126 {
27- $project : {
28- _id : 0 ,
29- id : 1 ,
30- structureId : "$resourceId" ,
31- relationsGroupId : { $ifNull : [ '$relationsGroupId' , null ] } ,
32- relatedObject : 1 ,
33- relatedObjectId : 1 ,
34- relationType : { $ifNull : [ '$relationType' , { priority : 99 } ] } ,
35- relationTag : { $ifNull : [ '$relationTag' , null ] } ,
36- startDate : { $ifNull : [ '$startDate' , null ] } ,
37- endDate : { $ifNull : [ '$endDate' , null ] } ,
38- endDatePrevisional : { $ifNull : [ '$endDatePrevisional' , null ] } ,
39- mandatePosition : { $ifNull : [ '$mandatePosition' , null ] } ,
40- mandateReason : { $ifNull : [ '$mandateReason' , null ] } ,
41- mandateEmail : { $ifNull : [ '$mandateEmail' , null ] } ,
42- personalEmail : { $ifNull : [ '$personalEmail' , null ] } ,
43- mandatePhonenumber : { $ifNull : [ '$mandatePhonenumber' , null ] } ,
44- mandateTemporary : { $ifNull : [ '$mandateTemporary' , null ] } ,
45- mandatePrecision : { $ifNull : [ '$mandatePrecision' , null ] } ,
46- laureatePrecision : { $ifNull : [ '$laureatePrecision' , null ] } ,
47- active : { $ifNull : [ '$active' , null ] } ,
127+ $lookup : {
128+ from : 'relationgroups' ,
129+ localField : '$relationsGroupId' ,
130+ foreignField : 'id' ,
131+ pipeline : [
132+ {
133+ $project : {
134+ _id : 0 ,
135+ id : 1 ,
136+ resourceId : 1 ,
137+ name : 1 ,
138+ accepts : 1 ,
139+ priority : 1 ,
140+ } ,
141+ } ,
142+ ] ,
143+ as : 'relationGroup' ,
48144 } ,
49145 } ,
146+ { $set : { relationGroup : { $arrayElemAt : [ '$relationGroup' , 0 ] } } } ,
147+ ]
148+
149+ const projection = {
150+ _id : 0 ,
151+ id : 1 ,
152+ resourceId : 1 ,
153+ relatedObjectId : 1 ,
154+ relationGroup : { $ifNull : [ '$relationGroup' , null ] } ,
155+ relationType : { $ifNull : [ '$relationType' , { priority : 99 } ] } ,
156+ relationTag : { $ifNull : [ '$relationTag' , null ] } ,
157+ startDate : { $ifNull : [ '$startDate' , null ] } ,
158+ endDate : { $ifNull : [ '$endDate' , null ] } ,
159+ endDatePrevisional : { $ifNull : [ '$endDatePrevisional' , null ] } ,
160+ mandatePosition : { $ifNull : [ '$mandatePosition' , null ] } ,
161+ mandateReason : { $ifNull : [ '$mandateReason' , null ] } ,
162+ mandateEmail : { $ifNull : [ '$mandateEmail' , null ] } ,
163+ personalEmail : { $ifNull : [ '$personalEmail' , null ] } ,
164+ mandatePhonenumber : { $ifNull : [ '$mandatePhonenumber' , null ] } ,
165+ mandateTemporary : { $ifNull : [ '$mandateTemporary' , null ] } ,
166+ mandatePrecision : { $ifNull : [ '$mandatePrecision' , null ] } ,
167+ laureatePrecision : { $ifNull : [ '$laureatePrecision' , null ] } ,
168+ active : { $ifNull : [ '$active' , null ] } ,
169+ }
170+
171+ const relationRelatedQuery = [
172+ ...relatedObjectLookup ,
173+ ...relationTypeQuery ,
174+ ...relationGroupQuery ,
175+ {
176+ $project : { ...projection , relatedObject : 1 } ,
177+ } ,
50178] ;
51179
52180const relationResourceQuery = [
53181 ...resourceLookup ,
182+ ...relationTypeQuery ,
183+ ...relationGroupQuery ,
54184 {
55185 $lookup : {
56186 from : 'relationtypes' ,
@@ -62,28 +192,7 @@ const relationResourceQuery = [
62192 } ,
63193 { $set : { relationType : { $arrayElemAt : [ '$relationType' , 0 ] } } } ,
64194 {
65- $project : {
66- _id : 0 ,
67- id : 1 ,
68- structureId : "$relatedObjectId" ,
69- relationsGroupId : { $ifNull : [ '$relationsGroupId' , null ] } ,
70- relatedObject : "$resource" ,
71- relatedObjectId : "$resourceId" ,
72- relationType : { $ifNull : [ '$relationType' , { priority : 99 } ] } ,
73- relationTag : { $ifNull : [ '$relationTag' , null ] } ,
74- startDate : { $ifNull : [ '$startDate' , null ] } ,
75- endDate : { $ifNull : [ '$endDate' , null ] } ,
76- endDatePrevisional : { $ifNull : [ '$endDatePrevisional' , null ] } ,
77- mandatePosition : { $ifNull : [ '$mandatePosition' , null ] } ,
78- mandateReason : { $ifNull : [ '$mandateReason' , null ] } ,
79- mandateEmail : { $ifNull : [ '$mandateEmail' , null ] } ,
80- personalEmail : { $ifNull : [ '$personalEmail' , null ] } ,
81- mandatePhonenumber : { $ifNull : [ '$mandatePhonenumber' , null ] } ,
82- mandateTemporary : { $ifNull : [ '$mandateTemporary' , null ] } ,
83- mandatePrecision : { $ifNull : [ '$mandatePrecision' , null ] } ,
84- laureatePrecision : { $ifNull : [ '$laureatePrecision' , null ] } ,
85- active : { $ifNull : [ '$active' , null ] } ,
86- } ,
195+ $project : { ...projection , resource : 1 } ,
87196 } ,
88197] ;
89198
0 commit comments