This repository has been archived by the owner on Apr 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
titres-activites-props-update.ts
95 lines (76 loc) · 2.4 KB
/
titres-activites-props-update.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
import dateFormat from 'dateformat'
import { ITitreActivite } from '../../types'
import { titresActivitesUpsert } from '../../database/queries/titres-activites'
import { titresGet } from '../../database/queries/titres'
import { titreValideCheck } from '../utils/titre-valide-check'
import { userSuper } from '../../database/user-super'
const titresActivitesPropsUpdate = async (titresIds?: string[]) => {
console.info()
console.info('propriétés des activités de titres…')
const titres = await titresGet(
{ ids: titresIds },
{
fields: {
demarches: { phase: { id: {} }, etapes: { id: {} }, type: { id: {} } },
activites: {
type: {
frequence: {
mois: { id: {} },
trimestres: { id: {} },
annees: { id: {} }
}
}
}
}
},
userSuper
)
const titresActivitesUpdated = titres.reduce(
(acc: ITitreActivite[], titre) => {
if (!titre.activites?.length) return acc
return titre.activites.reduce((acc, titreActivite) => {
const activiteType = titreActivite.type!
const periodes =
activiteType.frequence![activiteType.frequence!.periodesNom!]!
const periodeMonths = 12 / periodes.length
const dateDebut = dateFormat(
new Date(
titreActivite.annee,
(titreActivite.periodeId - 1) * periodeMonths,
1
),
'yyyy-mm-dd'
)
const titreIsValide =
titre.demarches?.length &&
titreValideCheck(
titre.demarches!,
dateDebut,
titreActivite.date,
titre.typeId,
true
)
if (titreIsValide && titreActivite.suppression) {
titreActivite.suppression = null
acc.push(titreActivite)
}
if (!titreIsValide && !titreActivite.suppression) {
titreActivite.suppression = true
acc.push(titreActivite)
}
return acc
}, acc)
},
[]
)
if (titresActivitesUpdated.length) {
await titresActivitesUpsert(titresActivitesUpdated)
const log = {
type: 'titre / activités / propriétés (mise à jour) ->',
value: titresActivitesUpdated.map(ta => ta.id).join(', ')
}
console.info(log.type, log.value)
}
return titresActivitesUpdated.map(ta => ta.id)
}
export { titresActivitesPropsUpdate }