This repository has been archived by the owner on Apr 7, 2022. It is now read-only.
/
fields-format.ts
236 lines (196 loc) · 5.83 KB
/
fields-format.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const fieldsOrderDesc = ['etablissements', 'demarches', 'etapes', 'activites']
const fieldsOrderAsc = [
'domaines',
'points',
'substances',
'references',
'etapesTypes',
'etapesStatuts',
'titresTypes',
'titresTypesEtapesTypes',
'titresTypesTitresStatuts'
]
const fieldsToRemove = ['coordonnees', 'incertitudes', 'heritageProps']
const titreFieldsToRemove = ['geojsonCentre'] as string[]
const geoFieldsToReplace = ['geojsonPoints', 'geojsonMultiPolygon']
const titrePropsEtapesFields = ['surface']
interface IFields {
[key: string]: IFields
}
const graphTitreAdministrationsFormat = (fields: IFields, type: string) => {
if (!fields.administrations) return
fields[`administrations${type}`] = {
...fields.administrations
}
if (
!fields.administrations.type &&
Object.keys(fields.administrations).length !== 0
) {
fields[`administrations${type}`].type = { id: {} }
}
}
// ajoute des propriétés requises par /database/queries/_format
const fieldsFormat = (fields: IFields, parent: string) => {
const isParentTitre = [
'titres',
'titre',
'amodiataireTitres',
'titulaireTitres'
].includes(parent)
// ajoute la propriété `titreType` sur les démarches
if (fields.demarches && !fields.demarches.titreType) {
fields.demarches.titreType = { id: {} }
}
// ajoute la propriété `type` sur les démarches
// pour pouvoir récupérer les types d'étapes spécifiques
if (fields.demarches && !fields.demarches.type) {
fields.demarches.type = { id: {} }
}
// ajoute la propriété `etapesTypes` sur les démarches
// pour pouvoir récupérer les types spécifiques
if (
fields.demarches &&
fields.demarches.type &&
!fields.demarches.type.etapesTypes
) {
fields.demarches.type.etapesTypes = { id: {} }
}
// ajoute la propriété `type` sur les activités
// pour savoir si une activité est liée à une administration
if (fields.activites && !fields.activites.type) {
fields.activites.type = { id: {}, administrations: { id: {} } }
}
// si `geojsonPoints` ou `geojsonMultiPolygon` sont présentes
// - ajoute la propriété `points`
// - supprime les propriété `geojsonPoints` ou `geojsonMultiPolygon`
geoFieldsToReplace.forEach(key => {
if (fields[key]) {
if (!fields.points) {
fields.points = { id: {} }
}
delete fields[key]
}
})
// supprime la propriété `coordonnees`
fieldsToRemove.forEach(key => {
if (fields[key]) {
delete fields[key]
}
})
if (fields.pays && (isParentTitre || parent === 'etapes')) {
fields.communes = { departement: { region: { pays: { id: {} } } } }
delete fields.pays
}
// ajoute `(orderDesc)` à certaine propriétés
if (fieldsOrderDesc.includes(parent)) {
// TODO: est ce qu'on peut faire un typage plus propre ?
fields.$modifier = 'orderDesc' as unknown as IFields
}
// ajoute `(orderAsc)` à certaine propriétés
if (fieldsOrderAsc.includes(parent)) {
fields.$modifier = 'orderAsc' as unknown as IFields
}
if (fields.administrations) {
if (isParentTitre) {
// ajoute la propriété `type` sur les administrations
graphTitreAdministrationsFormat(fields, 'Locales')
graphTitreAdministrationsFormat(fields, 'Gestionnaires')
delete fields.administrations
} else if (
!fields.administrations.type &&
Object.keys(fields.administrations).length !== 0
) {
fields.administrations.type = { id: {} }
}
}
// sur les titres
if (isParentTitre) {
if (fields.references && !fields.references.type) {
fields.references.type = { id: {} }
}
// si la propriété `surface` est présente
// - la remplace par `surfaceEtape`
titrePropsEtapesFields.forEach(key => {
if (fields[key]) {
fields[`${key}Etape`] = { id: {} }
delete fields[key]
}
})
// supprime certaines propriétés
titreFieldsToRemove.forEach(key => {
if (fields[key]) {
delete fields[key]
}
})
// trie les types de titres
if (fields.type) {
fields.type.$modifier = 'orderAsc' as unknown as IFields
}
// ajouter titulaires et amodiataires
}
if (['activites', 'activite'].includes(parent)) {
if (!fields.type) {
fields.type = { id: {} }
}
if (!fields.type.administrations) {
fields.type.administrations = { id: {} }
}
if (!fields.type.documentsTypes) {
fields.type.documentsTypes = { id: {} }
}
if (!fields.documents) {
fields.documents = { id: {} }
}
}
if (['utilisateurs', 'utilisateur'].includes(parent)) {
if (fields.sections) {
delete fields.sections
}
}
// on a besoin des activités si elles sont absentes
// pour calculer le nombre d'activités par type
if (!fields.activites) {
if (
fields.activitesDeposees ||
fields.activitesEnConstruction ||
fields.activitesAbsentes
) {
fields.activites = { id: {} }
}
}
// pour calculer la propriété « déposable » sur les étapes
if (['etapes', 'etape'].includes(parent)) {
if (!fields.documents) {
fields.documents = { id: {} }
}
if (!fields.justificatifs) {
fields.justificatifs = { id: {} }
}
if (!fields.type) {
fields.type = { id: {} }
}
if (!fields.type.documentsTypes) {
fields.type.documentsTypes = { id: {} }
}
if (!fields.type.justificatifsTypes) {
fields.type.justificatifsTypes = { id: {} }
}
if (!fields.demarche) {
fields.demarche = { id: {} }
}
if (!fields.demarche.titre) {
fields.demarche.titre = { id: {} }
}
if (!fields.points) {
fields.points = { id: {} }
}
if (!fields.substances) {
fields.substances = { id: {} }
}
if (!fields.sdomZones) {
fields.sdomZones = { id: {} }
}
}
return fields
}
export { fieldsFormat }