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
/
entreprises.ts
142 lines (118 loc) · 3.88 KB
/
entreprises.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
import { raw, QueryBuilder } from 'objection'
import { IUtilisateur } from '../../../types'
import { knex } from '../../../knex'
import { permissionCheck } from '../../../business/permission'
import Entreprises from '../../models/entreprises'
import Utilisateurs from '../../models/utilisateurs'
import Titres from '../../models/titres'
import Documents from '../../models/documents'
import { titresQueryModify } from './titres'
import { utilisateursQueryModify } from './utilisateurs'
import { documentsQueryModify } from './documents'
// import fileCreate from '../../../tools/file-create'
// import { format } from 'sql-formatter'
const entreprisesQueryModify = (
q: QueryBuilder<Entreprises, Entreprises | Entreprises[]>,
user: IUtilisateur | null | undefined
) => {
q.select('entreprises.*')
if (permissionCheck(user?.permissionId, ['super', 'admin', 'editeur'])) {
q.select(raw('true').as('modification'))
} else if (
permissionCheck(user?.permissionId, ['entreprise']) &&
user?.entreprises?.length
) {
const utilisateurEntreprise = Utilisateurs.query().leftJoin(
'utilisateurs__entreprises as u_e',
b => {
b.on(knex.raw('?? = ??', ['u_e.entrepriseId', 'entreprises.id']))
b.andOn(knex.raw('?? = ?', ['u_e.utilisateurId', user.id]))
}
)
q.select(raw('exists (?)', [utilisateurEntreprise]).as('modification'))
} else {
q.select(raw('false').as('modification'))
}
q.modifyGraph('titulaireTitres', a =>
titresQueryModify(a as QueryBuilder<Titres, Titres | Titres[]>, user)
// on group by entrepriseId au cas où il y a une aggrégation
// dans la requête de titre (ex : calc activités)
.groupBy('titres.id', 'titresTitulaires.entrepriseId')
)
q.modifyGraph('amodiataireTitres', a =>
titresQueryModify(a as QueryBuilder<Titres, Titres | Titres[]>, user)
// on group by entrepriseId au cas où il y a une aggrégation
// dans la requête de titre (ex : calc activités)
.groupBy('titres.id', 'titresAmodiataires.entrepriseId')
)
q.modifyGraph('utilisateurs', b => {
utilisateursQueryModify(
b as QueryBuilder<Utilisateurs, Utilisateurs | Utilisateurs[]>,
user
)
})
q.modifyGraph('documents', b => {
documentsQueryModify(
b as QueryBuilder<Documents, Documents | Documents[]>,
user
)
})
// fileCreate('test.sql', format(q.toKnexQuery().toString()))
return q
}
const entreprisesTitresQuery = (
entreprisesIds: string[],
titreAlias: string,
{
isTitulaire,
isAmodiataire
}: { isTitulaire?: boolean; isAmodiataire?: boolean } = {}
) => {
const q = Entreprises.query().whereIn('entreprises.id', entreprisesIds)
if (isTitulaire) {
q.modify(entreprisesTitulairesModify, entreprisesIds, titreAlias)
}
if (isAmodiataire) {
q.modify(entreprisesAmodiatairesModify, entreprisesIds, titreAlias)
}
q.where(c => {
if (isTitulaire) {
c.orWhereNotNull('t_t.entrepriseId')
}
if (isAmodiataire) {
c.orWhereNotNull('t_a.entrepriseId')
}
})
return q
}
const entreprisesTitulairesModify = (
q: QueryBuilder<Entreprises, Entreprises | Entreprises[]>,
entreprisesIds: string[],
titreAlias: string
) => {
q.leftJoin('titresTitulaires as t_t', b => {
b.on(
knex.raw('?? ->> ? = ??', [
`${titreAlias}.propsTitreEtapesIds`,
'titulaires',
't_t.titreEtapeId'
])
).onIn('t_t.entrepriseId', entreprisesIds)
})
}
const entreprisesAmodiatairesModify = (
q: QueryBuilder<Entreprises, Entreprises | Entreprises[]>,
entreprisesIds: string[],
titreAlias: string
) => {
q.leftJoin('titresAmodiataires as t_a', b => {
b.on(
knex.raw('?? ->> ? = ??', [
`${titreAlias}.propsTitreEtapesIds`,
'amodiataires',
't_a.titreEtapeId'
])
).onIn('t_a.entrepriseId', entreprisesIds)
})
}
export { entreprisesQueryModify, entreprisesTitresQuery }