1
1
import { Injectable } from '@nestjs/common'
2
2
import { Op } from 'sequelize'
3
+ import { FavoriOffreEmploiSqlModel } from 'src/infrastructure/sequelize/models/favori-offre-emploi.sql-model'
4
+ import { FavoriOffreEngagementSqlModel } from 'src/infrastructure/sequelize/models/favori-offre-engagement.sql-model'
5
+ import { FavoriOffreImmersionSqlModel } from 'src/infrastructure/sequelize/models/favori-offre-immersion.sql-model'
3
6
import { Query } from '../../building-blocks/types/query'
4
7
import { QueryHandler } from '../../building-blocks/types/query-handler'
5
8
import { Result , success } from '../../building-blocks/types/result'
6
9
import { Action } from '../../domain/action/action'
7
10
import { Authentification } from '../../domain/authentification'
8
- import { Evenement } from '../../domain/evenement'
9
11
import { ActionSqlModel } from '../../infrastructure/sequelize/models/action.sql-model'
10
- import { EvenementEngagementHebdoSqlModel } from '../../infrastructure/sequelize/models/evenement-engagement-hebdo.sql-model'
11
12
import { JeuneSqlModel } from '../../infrastructure/sequelize/models/jeune.sql-model'
12
13
import { RendezVousSqlModel } from '../../infrastructure/sequelize/models/rendez-vous.sql-model'
13
14
import { DateService } from '../../utils/date-service'
@@ -19,11 +20,6 @@ export interface GetIndicateursPourConseillerQuery extends Query {
19
20
idJeune : string
20
21
dateDebut : Date
21
22
dateFin : Date
22
- exclure ?: GetIndicateursPourConseillerExclusionQuery
23
- }
24
-
25
- export interface GetIndicateursPourConseillerExclusionQuery {
26
- offresEtFavoris : boolean
27
23
}
28
24
29
25
@Injectable ( )
@@ -56,221 +52,31 @@ export class GetIndicateursPourConseillerQueryHandler extends QueryHandler<
56
52
query : GetIndicateursPourConseillerQuery
57
53
) : Promise < Result < IndicateursPourConseillerQueryModel > > {
58
54
const maintenant = this . dateService . nowJs ( )
59
- const exclureFavoris = query . exclure ? query . exclure . offresEtFavoris : false
60
55
61
- const [
62
- actionsSqlDuJeune ,
63
- rendezVousSqlDuJeune ,
64
- offresEtFavorisEvenementsSql
65
- ] = await Promise . all ( [
66
- findAllActions ( query ) ,
67
- findAllRendezVous ( query ) ,
68
- exclureFavoris ? Promise . resolve ( [ ] ) : findAllFavoris ( query )
69
- ] )
56
+ const [ actionsSqlDuJeune , rendezVousSqlDuJeune , favorisSql ] =
57
+ await Promise . all ( [
58
+ findAllActions ( query ) ,
59
+ findAllRendezVous ( query ) ,
60
+ findAllFavorisPostulesOuCreesPendantPeriode ( query )
61
+ ] )
70
62
71
- const indicateursActions = this . getIndicateursActions (
63
+ const indicateursActions = getIndicateursActions (
72
64
actionsSqlDuJeune ,
73
- query . dateDebut ,
74
- query . dateFin ,
65
+ { debut : query . dateDebut , fin : query . dateFin } ,
75
66
maintenant
76
67
)
77
68
78
- const nombreRendezVousPlanifies = rendezVousSqlDuJeune . length
69
+ const indicateursSuiviOffres = getIndicateursSuiviOffres ( favorisSql , {
70
+ debut : query . dateDebut ,
71
+ fin : query . dateFin
72
+ } )
79
73
80
- const indicateursOffresEtFavoris = this . getIndicateursOffresEtFavoris (
81
- offresEtFavorisEvenementsSql
82
- )
83
74
return success ( {
84
75
actions : indicateursActions ,
85
- rendezVous : {
86
- planifies : nombreRendezVousPlanifies
87
- } ,
88
- offres : indicateursOffresEtFavoris . offres ,
89
- favoris : indicateursOffresEtFavoris . favoris
76
+ rendezVous : { planifies : rendezVousSqlDuJeune . length } ,
77
+ offres : indicateursSuiviOffres
90
78
} )
91
79
}
92
-
93
- private getIndicateursActions (
94
- actionsSqlDuJeune : ActionSqlModel [ ] ,
95
- dateDebut : Date ,
96
- dateFin : Date ,
97
- maintenant : Date
98
- ) : {
99
- creees : number
100
- enRetard : number
101
- terminees : number
102
- aEcheance : number
103
- } {
104
- return actionsSqlDuJeune
105
- . map ( actionSql => {
106
- return {
107
- creees : this . lActionEstCreeeEntreLesDeuxDates (
108
- actionSql ,
109
- dateDebut ,
110
- dateFin
111
- )
112
- ? 1
113
- : 0 ,
114
- enRetard : this . lActionEstEnRetard ( actionSql , maintenant ) ? 1 : 0 ,
115
- terminees : this . lActionEstTermineeEntreLesDeuxDates (
116
- actionSql ,
117
- dateDebut ,
118
- dateFin
119
- )
120
- ? 1
121
- : 0 ,
122
- aEcheance : this . lActionEstAEcheanceEntreLesDeuxDates (
123
- actionSql ,
124
- dateDebut ,
125
- dateFin
126
- )
127
- ? 1
128
- : 0
129
- }
130
- } )
131
- . reduce (
132
- ( indicateursActionsAccumulateur , indicateursAction ) => {
133
- indicateursActionsAccumulateur . creees += indicateursAction . creees
134
- indicateursActionsAccumulateur . enRetard += indicateursAction . enRetard
135
- indicateursActionsAccumulateur . terminees +=
136
- indicateursAction . terminees
137
- indicateursActionsAccumulateur . aEcheance +=
138
- indicateursAction . aEcheance
139
- return indicateursActionsAccumulateur
140
- } ,
141
- {
142
- creees : 0 ,
143
- enRetard : 0 ,
144
- terminees : 0 ,
145
- aEcheance : 0
146
- }
147
- )
148
- }
149
-
150
- private getIndicateursOffresEtFavoris (
151
- evenementsSql : EvenementEngagementHebdoSqlModel [ ]
152
- ) : {
153
- offres : { consultees : number ; partagees : number }
154
- favoris : { offresSauvegardees : number ; recherchesSauvegardees : number }
155
- } {
156
- const codesOffreConsultee : string [ ] = [
157
- Evenement . Code . OFFRE_ALTERNANCE_AFFICHEE ,
158
- Evenement . Code . OFFRE_EMPLOI_AFFICHEE ,
159
- Evenement . Code . OFFRE_IMMERSION_AFFICHEE ,
160
- Evenement . Code . OFFRE_SERVICE_CIVIQUE_AFFICHE ,
161
- Evenement . Code . OFFRE_SERVICE_CIVIQUE_AFFICHEE
162
- ]
163
- const codesOffreSauvegardee : string [ ] = [
164
- Evenement . Code . OFFRE_ALTERNANCE_SAUVEGARDEE ,
165
- Evenement . Code . OFFRE_EMPLOI_SAUVEGARDEE ,
166
- Evenement . Code . OFFRE_IMMERSION_SAUVEGARDEE ,
167
- Evenement . Code . OFFRE_SERVICE_CIVIQUE_SAUVEGARDEE
168
- ]
169
- const codesRechercheSauvegardee : string [ ] = [
170
- Evenement . Code . RECHERCHE_ALTERNANCE_SAUVEGARDEE ,
171
- Evenement . Code . RECHERCHE_IMMERSION_SAUVEGARDEE ,
172
- Evenement . Code . RECHERCHE_OFFRE_EMPLOI_SAUVEGARDEE ,
173
- Evenement . Code . RECHERCHE_SERVICE_CIVIQUE_SAUVEGARDEE
174
- ]
175
-
176
- return evenementsSql
177
- . map ( evenementSql => {
178
- return {
179
- offres : {
180
- consultees : codesOffreConsultee . includes ( evenementSql . code ) ? 1 : 0 ,
181
- partagees :
182
- evenementSql . code === Evenement . Code . MESSAGE_OFFRE_PARTAGEE
183
- ? 1
184
- : 0
185
- } ,
186
- favoris : {
187
- offresSauvegardees : codesOffreSauvegardee . includes (
188
- evenementSql . code
189
- )
190
- ? 1
191
- : 0 ,
192
- recherchesSauvegardees : codesRechercheSauvegardee . includes (
193
- evenementSql . code
194
- )
195
- ? 1
196
- : 0
197
- }
198
- }
199
- } )
200
- . reduce (
201
- (
202
- indicateursOffresEtFavorisAccumulateur ,
203
- indicateursOffresEtFavoris
204
- ) => {
205
- indicateursOffresEtFavorisAccumulateur . offres . consultees +=
206
- indicateursOffresEtFavoris . offres . consultees
207
- indicateursOffresEtFavorisAccumulateur . offres . partagees +=
208
- indicateursOffresEtFavoris . offres . partagees
209
- indicateursOffresEtFavorisAccumulateur . favoris . offresSauvegardees +=
210
- indicateursOffresEtFavoris . favoris . offresSauvegardees
211
- indicateursOffresEtFavorisAccumulateur . favoris . recherchesSauvegardees +=
212
- indicateursOffresEtFavoris . favoris . recherchesSauvegardees
213
- return indicateursOffresEtFavorisAccumulateur
214
- } ,
215
- {
216
- offres : {
217
- consultees : 0 ,
218
- partagees : 0
219
- } ,
220
- favoris : {
221
- offresSauvegardees : 0 ,
222
- recherchesSauvegardees : 0
223
- }
224
- }
225
- )
226
- }
227
-
228
- private lActionEstTermineeEntreLesDeuxDates (
229
- actionSql : ActionSqlModel ,
230
- dateDebut : Date ,
231
- dateFin : Date
232
- ) : boolean {
233
- return (
234
- actionSql . statut === Action . Statut . TERMINEE &&
235
- actionSql . dateFinReelle !== null &&
236
- DateService . isBetweenDates ( actionSql . dateFinReelle , dateDebut , dateFin )
237
- )
238
- }
239
-
240
- private lActionEstEnRetard (
241
- actionSql : ActionSqlModel ,
242
- maintenant : Date
243
- ) : boolean {
244
- return (
245
- actionSql . dateEcheance < maintenant &&
246
- actionSql . statut !== Action . Statut . TERMINEE &&
247
- actionSql . statut !== Action . Statut . ANNULEE
248
- )
249
- }
250
-
251
- private lActionEstCreeeEntreLesDeuxDates (
252
- actionSql : ActionSqlModel ,
253
- dateDebut : Date ,
254
- dateFin : Date
255
- ) : boolean {
256
- return DateService . isBetweenDates (
257
- actionSql . dateCreation ,
258
- dateDebut ,
259
- dateFin
260
- )
261
- }
262
-
263
- private lActionEstAEcheanceEntreLesDeuxDates (
264
- actionSql : ActionSqlModel ,
265
- dateDebut : Date ,
266
- dateFin : Date
267
- ) : boolean {
268
- return DateService . isBetweenDates (
269
- actionSql . dateEcheance ,
270
- dateDebut ,
271
- dateFin
272
- )
273
- }
274
80
}
275
81
276
82
function findAllActions (
@@ -299,14 +105,105 @@ function findAllRendezVous(
299
105
} )
300
106
}
301
107
302
- function findAllFavoris (
108
+ async function findAllFavorisPostulesOuCreesPendantPeriode (
303
109
query : GetIndicateursPourConseillerQuery
304
- ) : Promise < EvenementEngagementHebdoSqlModel [ ] > {
305
- return EvenementEngagementHebdoSqlModel . findAll ( {
110
+ ) : Promise < Array < { dateCreation : Date ; dateCandidature : Date | null } > > {
111
+ const options = {
112
+ attributes : [ 'dateCreation' , 'dateCandidature' ] ,
306
113
where : {
307
- typeUtilisateur : Authentification . Type . JEUNE ,
308
- idUtilisateur : query . idJeune ,
309
- dateEvenement : { [ Op . between ] : [ query . dateDebut , query . dateFin ] }
114
+ idJeune : query . idJeune ,
115
+ [ Op . or ] : {
116
+ dateCreation : { [ Op . between ] : [ query . dateDebut , query . dateFin ] } ,
117
+ dateCandidature : { [ Op . between ] : [ query . dateDebut , query . dateFin ] }
118
+ }
310
119
}
120
+ }
121
+ const [ offresEmploi , offresServiceCivique , offresImmersion ] =
122
+ await Promise . all ( [
123
+ FavoriOffreEmploiSqlModel . findAll ( options ) ,
124
+ FavoriOffreEngagementSqlModel . findAll ( options ) ,
125
+ FavoriOffreImmersionSqlModel . findAll ( options )
126
+ ] )
127
+
128
+ return [ ...offresEmploi , ...offresServiceCivique , ...offresImmersion ]
129
+ }
130
+
131
+ function getIndicateursActions (
132
+ actionsSqlDuJeune : ActionSqlModel [ ] ,
133
+ periode : { debut : Date ; fin : Date } ,
134
+ maintenant : Date
135
+ ) : {
136
+ creees : number
137
+ enRetard : number
138
+ terminees : number
139
+ } {
140
+ const indicateurs = { creees : 0 , enRetard : 0 , terminees : 0 }
141
+ actionsSqlDuJeune . forEach ( action => {
142
+ indicateurs . creees += + actionCreeePendantPeriode ( action , periode )
143
+ if ( actionEnRetard ( action , maintenant ) ) indicateurs . enRetard += 1
144
+ else if ( actionTermineePendantPeriode ( action , periode ) )
145
+ indicateurs . terminees += 1
311
146
} )
147
+
148
+ return indicateurs
149
+ }
150
+
151
+ function getIndicateursSuiviOffres (
152
+ favorisSql : Array < { dateCreation : Date ; dateCandidature : Date | null } > ,
153
+ periode : { debut : Date ; fin : Date }
154
+ ) : {
155
+ sauvegardees : number
156
+ postulees : number
157
+ } {
158
+ const indicateurs = { sauvegardees : 0 , postulees : 0 }
159
+ favorisSql . forEach ( ( { dateCreation, dateCandidature } ) => {
160
+ indicateurs . sauvegardees += + offreSauvegardeePendantPeriode (
161
+ dateCreation ,
162
+ periode
163
+ )
164
+ indicateurs . postulees += + ( dateCandidature !== null )
165
+ } )
166
+
167
+ return indicateurs
168
+ }
169
+
170
+ function actionCreeePendantPeriode (
171
+ actionSql : ActionSqlModel ,
172
+ periode : { debut : Date ; fin : Date }
173
+ ) : boolean {
174
+ return DateService . isBetweenDates (
175
+ actionSql . dateCreation ,
176
+ periode . debut ,
177
+ periode . fin
178
+ )
179
+ }
180
+
181
+ function actionEnRetard ( actionSql : ActionSqlModel , maintenant : Date ) : boolean {
182
+ return (
183
+ actionSql . dateEcheance < maintenant &&
184
+ actionSql . statut !== Action . Statut . TERMINEE &&
185
+ actionSql . statut !== Action . Statut . ANNULEE
186
+ )
187
+ }
188
+
189
+ function actionTermineePendantPeriode (
190
+ actionSql : ActionSqlModel ,
191
+ periode : { debut : Date ; fin : Date }
192
+ ) : boolean {
193
+ return (
194
+ actionSql . statut === Action . Statut . TERMINEE &&
195
+ actionSql . dateFinReelle !== null &&
196
+ DateService . isBetweenDates (
197
+ actionSql . dateFinReelle ,
198
+ periode . debut ,
199
+ periode . fin
200
+ )
201
+ )
202
+ }
203
+
204
+ function offreSauvegardeePendantPeriode (
205
+ dateCreation : Date ,
206
+ periode : { debut : Date ; fin : Date }
207
+ ) : boolean {
208
+ return DateService . isBetweenDates ( dateCreation , periode . debut , periode . fin )
312
209
}
0 commit comments