1
- import { getTestApp , signInWith , YOLO_DODO } from '@/backend/test' ;
1
+ import { GetIndicateursValeursResponseType } from '@/backend/indicateurs/shared/models/get-indicateurs.response' ;
2
+ import { UpsertIndicateursValeursRequest } from '@/backend/indicateurs/shared/models/upsert-indicateurs-valeurs.request' ;
3
+ import { CalculTrajectoireResultatMode } from '@/backend/indicateurs/trajectoires/calcul-trajectoire.request' ;
4
+ import { CalculTrajectoireResponse } from '@/backend/indicateurs/trajectoires/calcul-trajectoire.response' ;
5
+ import {
6
+ VerificationTrajectoireResponseType ,
7
+ VerificationTrajectoireStatus ,
8
+ } from '@/backend/indicateurs/trajectoires/verification-trajectoire.response' ;
9
+ import {
10
+ getAuthUser ,
11
+ getTestApp ,
12
+ getTestRouter ,
13
+ signInWith ,
14
+ YOLO_DODO ,
15
+ } from '@/backend/test' ;
16
+ import { AuthenticatedUser } from '@/backend/users/models/auth.models' ;
17
+ import { sleep } from '@/backend/utils/sleep.utils' ;
18
+ import { TrpcRouter } from '@/backend/utils/trpc/trpc.router' ;
2
19
import { INestApplication } from '@nestjs/common' ;
3
20
import request from 'supertest' ;
4
21
import { expect } from 'vitest' ;
5
22
6
23
describe ( 'Téléchargement de la trajectoire SNBC' , ( ) => {
7
24
let app : INestApplication ;
25
+ let router : TrpcRouter ;
8
26
let yoloDodoToken : string ;
27
+ let yoloDodoUser : AuthenticatedUser ;
9
28
10
29
beforeAll ( async ( ) => {
11
30
app = await getTestApp ( ) ;
12
-
31
+ router = await getTestRouter ( app ) ;
13
32
const yoloDodo = await signInWith ( YOLO_DODO ) ;
14
33
yoloDodoToken = yoloDodo . data . session ?. access_token || '' ;
34
+ yoloDodoUser = await getAuthUser ( YOLO_DODO ) ;
15
35
} ) ;
16
36
17
37
test ( `Telechargement du modele` , ( ) => {
@@ -35,6 +55,317 @@ describe('Téléchargement de la trajectoire SNBC', () => {
35
55
) ;
36
56
} , 30000 ) ;
37
57
58
+ test ( `Verification, calcul avec donnees completes et gestion de la mise à jour` , async ( ) => {
59
+ const caller = router . createCaller ( { user : yoloDodoUser } ) ;
60
+ const collectiviteId = 4936 ;
61
+
62
+ // Restauration de la valeur d'indicateur
63
+ const response = await request ( app . getHttpServer ( ) )
64
+ . get (
65
+ `/indicateurs?identifiantsReferentiel=cae_1.e&collectiviteId=4936&sources=rare`
66
+ )
67
+ . set ( 'Authorization' , `Bearer ${ yoloDodoToken } ` )
68
+ . expect ( 200 ) ;
69
+ const indicateurExistingValeurs =
70
+ response . body as GetIndicateursValeursResponseType ;
71
+ const indicateurCae1eId =
72
+ indicateurExistingValeurs . indicateurs [ 0 ] . definition . id ;
73
+ const indicateurCae1eMetadataId =
74
+ indicateurExistingValeurs . indicateurs [ 0 ] . sources [ 'rare' ] . metadonnees [ 0 ]
75
+ . id ;
76
+ const restaureIndicateurValeurPayload : UpsertIndicateursValeursRequest = {
77
+ valeurs : [
78
+ {
79
+ collectiviteId : collectiviteId ,
80
+ indicateurId : indicateurCae1eId ,
81
+ dateValeur : '2015-01-01' ,
82
+ metadonneeId : indicateurCae1eMetadataId ,
83
+ resultat : 653.598 ,
84
+ } ,
85
+ ] ,
86
+ } ;
87
+ await request ( app . getHttpServer ( ) )
88
+ . post ( '/indicateurs' )
89
+ . set ( 'Authorization' , `Bearer ${ yoloDodoToken } ` )
90
+ . send ( restaureIndicateurValeurPayload )
91
+ . expect ( 201 ) ;
92
+
93
+ // Suppression de la trajectoire snbc existante si le test est joué plusieurs fois
94
+ await caller . indicateurs . trajectoires . snbc . delete ( {
95
+ collectiviteId : collectiviteId ,
96
+ } ) ;
97
+
98
+ const verifcationReponseAttendue : VerificationTrajectoireResponseType = {
99
+ status : VerificationTrajectoireStatus . PRET_A_CALCULER ,
100
+ epci : {
101
+ id : collectiviteId ,
102
+ nom : 'Eurométropole de Strasbourg' ,
103
+ siren : '246700488' ,
104
+ natureInsee : 'METRO' ,
105
+ } ,
106
+ donneesEntree : {
107
+ sources : [ 'rare' , 'aldo' ] ,
108
+ emissionsGes : {
109
+ valeurs : [
110
+ {
111
+ identifiantsReferentiel : [ 'cae_1.c' ] ,
112
+ valeur : 447.868 ,
113
+ dateMin : '2015-01-01' ,
114
+ dateMax : '2015-01-01' ,
115
+ } ,
116
+ {
117
+ identifiantsReferentiel : [ 'cae_1.d' ] ,
118
+ valeur : 471.107 ,
119
+ dateMin : '2015-01-01' ,
120
+ dateMax : '2015-01-01' ,
121
+ } ,
122
+ {
123
+ identifiantsReferentiel : [ 'cae_1.i' ] ,
124
+ valeur : 348.525 ,
125
+ dateMin : '2015-01-01' ,
126
+ dateMax : '2015-01-01' ,
127
+ } ,
128
+ {
129
+ identifiantsReferentiel : [ 'cae_1.g' ] ,
130
+ valeur : 28.839 ,
131
+ dateMin : '2015-01-01' ,
132
+ dateMax : '2015-01-01' ,
133
+ } ,
134
+ {
135
+ identifiantsReferentiel : [ 'cae_1.e' ] ,
136
+ valeur : 653.6 ,
137
+ dateMin : '2015-01-01' ,
138
+ dateMax : '2015-01-01' ,
139
+ } ,
140
+ {
141
+ identifiantsReferentiel : [ 'cae_1.f' ] ,
142
+ valeur : 21.492 ,
143
+ dateMin : '2015-01-01' ,
144
+ dateMax : '2015-01-01' ,
145
+ } ,
146
+ {
147
+ identifiantsReferentiel : [ 'cae_1.h' ] ,
148
+ valeur : 39.791 ,
149
+ dateMin : '2015-01-01' ,
150
+ dateMax : '2015-01-01' ,
151
+ } ,
152
+ {
153
+ identifiantsReferentiel : [ 'cae_1.j' ] ,
154
+ valeur : 13.5 ,
155
+ dateMin : '2015-01-01' ,
156
+ dateMax : '2015-01-01' ,
157
+ } ,
158
+ ] ,
159
+ identifiantsReferentielManquants : [ ] ,
160
+ } ,
161
+ consommationsFinales : {
162
+ valeurs : [
163
+ {
164
+ identifiantsReferentiel : [ 'cae_2.e' ] ,
165
+ valeur : 3092.7 ,
166
+ dateMin : '2015-01-01' ,
167
+ dateMax : '2015-01-01' ,
168
+ } ,
169
+ {
170
+ identifiantsReferentiel : [ 'cae_2.f' ] ,
171
+ valeur : 3295.15 ,
172
+ dateMin : '2015-01-01' ,
173
+ dateMax : '2015-01-01' ,
174
+ } ,
175
+ {
176
+ identifiantsReferentiel : [ 'cae_2.k' ] ,
177
+ valeur : null ,
178
+ dateMin : null ,
179
+ dateMax : null ,
180
+ } ,
181
+ {
182
+ identifiantsReferentiel : [ 'cae_2.i' ] ,
183
+ valeur : 61 ,
184
+ dateMin : '2015-01-01' ,
185
+ dateMax : '2015-01-01' ,
186
+ } ,
187
+ {
188
+ identifiantsReferentiel : [ 'cae_2.g' , 'cae_2.h' ] ,
189
+ valeur : 2668.6499999999996 ,
190
+ dateMin : '2015-01-01' ,
191
+ dateMax : '2015-01-01' ,
192
+ } ,
193
+ {
194
+ identifiantsReferentiel : [ 'cae_2.j' ] ,
195
+ valeur : 0 ,
196
+ dateMin : '2015-01-01' ,
197
+ dateMax : '2015-01-01' ,
198
+ } ,
199
+ {
200
+ identifiantsReferentiel : [ 'cae_2.l_pcaet' ] ,
201
+ valeur : null ,
202
+ dateMin : null ,
203
+ dateMax : null ,
204
+ } ,
205
+ ] ,
206
+ identifiantsReferentielManquants : [ 'cae_2.k' , 'cae_2.l_pcaet' ] ,
207
+ } ,
208
+ sequestrations : {
209
+ valeurs : [
210
+ {
211
+ identifiantsReferentiel : [ 'cae_63.ca' ] ,
212
+ valeur : - 0.13844 ,
213
+ dateMin : '2018-01-01' ,
214
+ dateMax : '2018-01-01' ,
215
+ } ,
216
+ {
217
+ identifiantsReferentiel : [ 'cae_63.cb' ] ,
218
+ valeur : null ,
219
+ dateMin : null ,
220
+ dateMax : null ,
221
+ } ,
222
+ {
223
+ identifiantsReferentiel : [ 'cae_63.da' ] ,
224
+ valeur : null ,
225
+ dateMin : null ,
226
+ dateMax : null ,
227
+ } ,
228
+ {
229
+ identifiantsReferentiel : [ 'cae_63.cd' ] ,
230
+ valeur : null ,
231
+ dateMin : null ,
232
+ dateMax : null ,
233
+ } ,
234
+ {
235
+ identifiantsReferentiel : [ 'cae_63.cc' ] ,
236
+ valeur : null ,
237
+ dateMin : null ,
238
+ dateMax : null ,
239
+ } ,
240
+ {
241
+ identifiantsReferentiel : [ 'cae_63.db' ] ,
242
+ valeur : - 0.2279 ,
243
+ dateMin : '2018-01-01' ,
244
+ dateMax : '2018-01-01' ,
245
+ } ,
246
+ {
247
+ identifiantsReferentiel : [ 'cae_63.b' ] ,
248
+ valeur : 7.81264 ,
249
+ dateMin : '2018-01-01' ,
250
+ dateMax : '2018-01-01' ,
251
+ } ,
252
+ {
253
+ identifiantsReferentiel : [ 'cae_63.e' ] ,
254
+ valeur : 0.62713 ,
255
+ dateMin : '2018-01-01' ,
256
+ dateMax : '2018-01-01' ,
257
+ } ,
258
+ ] ,
259
+ identifiantsReferentielManquants : [
260
+ 'cae_63.cb' ,
261
+ 'cae_63.da' ,
262
+ 'cae_63.cd' ,
263
+ 'cae_63.cc' ,
264
+ ] ,
265
+ } ,
266
+ } ,
267
+ } ;
268
+ const verificationResponse =
269
+ await caller . indicateurs . trajectoires . snbc . checkStatus ( {
270
+ collectiviteId : collectiviteId ,
271
+ } ) ;
272
+ expect ( verificationResponse ) . toMatchObject ( verifcationReponseAttendue ) ;
273
+
274
+ // Calcul de la trajectoire
275
+ const responseCalcul = await request ( app . getHttpServer ( ) )
276
+ . get ( `/trajectoires/snbc?collectiviteId=${ collectiviteId } ` )
277
+ . set ( 'Authorization' , `Bearer ${ yoloDodoToken } ` )
278
+ . expect ( 200 ) ;
279
+ expect ( ( responseCalcul . body as CalculTrajectoireResponse ) . mode ) . toEqual (
280
+ CalculTrajectoireResultatMode . MAJ_SPREADSHEET_EXISTANT
281
+ ) ;
282
+
283
+ // La vérification doit maintenant retourner "calculé"
284
+ const verificationReponseAttendueApresCalcul : VerificationTrajectoireResponseType =
285
+ {
286
+ status : VerificationTrajectoireStatus . DEJA_CALCULE ,
287
+ sourcesDonneesEntree : [ 'rare' , 'aldo' ] ,
288
+ indentifiantsReferentielManquantsDonneesEntree : [
289
+ 'cae_2.k' ,
290
+ 'cae_2.l_pcaet' ,
291
+ 'cae_63.cb' ,
292
+ 'cae_63.da' ,
293
+ 'cae_63.cd' ,
294
+ 'cae_63.cc' ,
295
+ ] ,
296
+ } ;
297
+ const verificationApresCalculResponse =
298
+ await caller . indicateurs . trajectoires . snbc . checkStatus ( {
299
+ collectiviteId : collectiviteId ,
300
+ } ) ;
301
+
302
+ expect ( verificationApresCalculResponse ) . toMatchObject (
303
+ verificationReponseAttendueApresCalcul
304
+ ) ;
305
+
306
+ // Si on requête de nouveau le calcul, il doit provenir de la base de données
307
+ const responseRecalcul = await request ( app . getHttpServer ( ) )
308
+ . get ( `/trajectoires/snbc?collectiviteId=${ collectiviteId } ` )
309
+ . set ( 'Authorization' , `Bearer ${ yoloDodoToken } ` )
310
+ . expect ( 200 ) ;
311
+ expect ( ( responseRecalcul . body as CalculTrajectoireResponse ) . mode ) . toEqual (
312
+ CalculTrajectoireResultatMode . DONNEES_EN_BDD
313
+ ) ;
314
+
315
+ // Maintenant on met à jour une valeur d'indicateur
316
+ const indicateurValeurPayload : UpsertIndicateursValeursRequest = {
317
+ valeurs : [
318
+ {
319
+ collectiviteId : 4936 ,
320
+ indicateurId : indicateurCae1eId ,
321
+ dateValeur : '2015-01-01' ,
322
+ metadonneeId : indicateurCae1eMetadataId ,
323
+ resultat : 663 ,
324
+ } ,
325
+ ] ,
326
+ } ;
327
+ const upsertIndicateurResponse = await request ( app . getHttpServer ( ) )
328
+ . post ( '/indicateurs' )
329
+ . set ( 'Authorization' , `Bearer ${ yoloDodoToken } ` )
330
+ . send ( indicateurValeurPayload )
331
+ . expect ( 201 ) ;
332
+
333
+ await sleep ( 1000 ) ;
334
+
335
+ // La verification doit indiquer qu'il y a une mise à jour disponible
336
+ const verificationApresMajResponse =
337
+ await caller . indicateurs . trajectoires . snbc . checkStatus ( {
338
+ collectiviteId : collectiviteId ,
339
+ forceRecuperationDonnees : true ,
340
+ } ) ;
341
+ expect ( verificationApresMajResponse . status ) . toEqual (
342
+ VerificationTrajectoireStatus . MISE_A_JOUR_DISPONIBLE
343
+ ) ;
344
+
345
+ // On force un recalcul
346
+ const responseApresMajEtRecalcul = await request ( app . getHttpServer ( ) )
347
+ . get (
348
+ `/trajectoires/snbc?collectiviteId=${ collectiviteId } &mode=${ CalculTrajectoireResultatMode . MAJ_SPREADSHEET_EXISTANT } `
349
+ )
350
+ . set ( 'Authorization' , `Bearer ${ yoloDodoToken } ` )
351
+ . expect ( 200 ) ;
352
+ const calculTrajectoireResponse =
353
+ responseApresMajEtRecalcul . body as CalculTrajectoireResponse ;
354
+ expect ( calculTrajectoireResponse . mode ) . toEqual (
355
+ CalculTrajectoireResultatMode . MAJ_SPREADSHEET_EXISTANT
356
+ ) ;
357
+
358
+ // La vérification doit maintenant envoyer 'deja_calcule'
359
+ const verificationApresMajEtRecalculResponse =
360
+ await caller . indicateurs . trajectoires . snbc . checkStatus ( {
361
+ collectiviteId : collectiviteId ,
362
+ forceRecuperationDonnees : true ,
363
+ } ) ;
364
+ expect ( verificationApresMajEtRecalculResponse . status ) . toEqual (
365
+ VerificationTrajectoireStatus . DEJA_CALCULE
366
+ ) ;
367
+ } , 30000 ) ;
368
+
38
369
afterAll ( async ( ) => {
39
370
await app . close ( ) ;
40
371
} ) ;
0 commit comments