forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-affiliation-connections-by-org-id.js
More file actions
300 lines (275 loc) · 10.8 KB
/
Copy pathload-affiliation-connections-by-org-id.js
File metadata and controls
300 lines (275 loc) · 10.8 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { aql } from 'arangojs'
import { fromGlobalId, toGlobalId } from 'graphql-relay'
import { t } from '@lingui/macro'
export const loadAffiliationConnectionsByOrgId =
({ query, userKey, cleanseInput, i18n }) =>
async ({ orgId, after, before, first, last, orderBy, search }) => {
let afterTemplate = aql``
if (typeof after !== 'undefined') {
const { id: afterId } = fromGlobalId(cleanseInput(after))
if (typeof orderBy === 'undefined') {
afterTemplate = aql`FILTER TO_NUMBER(affiliation._key) > TO_NUMBER(${afterId})`
} else {
let afterTemplateDirection
if (orderBy.direction === 'ASC') {
afterTemplateDirection = aql`>`
} else {
afterTemplateDirection = aql`<`
}
let affiliationField, documentField
/* istanbul ignore else */
if (orderBy.field === 'user-username') {
affiliationField = aql`DOCUMENT(users, PARSE_IDENTIFIER(affiliation._to).key).userName`
documentField = aql`DOCUMENT(users, PARSE_IDENTIFIER(DOCUMENT(affiliations, ${afterId})._to).key).userName`
}
afterTemplate = aql`
FILTER ${affiliationField} ${afterTemplateDirection} ${documentField}
OR (${affiliationField} == ${documentField}
AND TO_NUMBER(affiliation._key) > TO_NUMBER(${afterId}))
`
}
}
let beforeTemplate = aql``
if (typeof before !== 'undefined') {
const { id: beforeId } = fromGlobalId(cleanseInput(before))
if (typeof orderBy === 'undefined') {
beforeTemplate = aql`FILTER TO_NUMBER(affiliation._key) < TO_NUMBER(${beforeId})`
} else {
let beforeTemplateDirection = aql``
if (orderBy.direction === 'ASC') {
beforeTemplateDirection = aql`<`
} else {
beforeTemplateDirection = aql`>`
}
let affiliationField, documentField
/* istanbul ignore else */
if (orderBy.field === 'user-username') {
affiliationField = aql`DOCUMENT(users, PARSE_IDENTIFIER(affiliation._to).key).userName`
documentField = aql`DOCUMENT(users, PARSE_IDENTIFIER(DOCUMENT(affiliations, ${beforeId})._to).key).userName`
}
beforeTemplate = aql`
FILTER ${affiliationField} ${beforeTemplateDirection} ${documentField}
OR (${affiliationField} == ${documentField}
AND TO_NUMBER(affiliation._key) < TO_NUMBER(${beforeId}))
`
}
}
let limitTemplate = aql``
if (typeof first === 'undefined' && typeof last === 'undefined') {
console.warn(
`User: ${userKey} did not have either \`first\` or \`last\` arguments set for: loadAffiliationConnectionsByOrgId.`,
)
throw new Error(
i18n._(
t`You must provide a \`first\` or \`last\` value to properly paginate the \`Affiliation\` connection.`,
),
)
} else if (typeof first !== 'undefined' && typeof last !== 'undefined') {
console.warn(
`User: ${userKey} attempted to have \`first\` and \`last\` arguments set for: loadAffiliationConnectionsByOrgId.`,
)
throw new Error(
i18n._(
t`Passing both \`first\` and \`last\` to paginate the \`Affiliation\` connection is not supported.`,
),
)
} else if (typeof first === 'number' || typeof last === 'number') {
/* istanbul ignore else */
if (first < 0 || last < 0) {
const argSet = typeof first !== 'undefined' ? 'first' : 'last'
console.warn(
`User: ${userKey} attempted to have \`${argSet}\` set below zero for: loadAffiliationConnectionsByOrgId.`,
)
throw new Error(
i18n._(
t`\`${argSet}\` on the \`Affiliation\` connection cannot be less than zero.`,
),
)
} else if (first > 100 || last > 100) {
const argSet = typeof first !== 'undefined' ? 'first' : 'last'
const amount = typeof first !== 'undefined' ? first : last
console.warn(
`User: ${userKey} attempted to have \`${argSet}\` set to ${amount} for: loadAffiliationConnectionsByOrgId.`,
)
throw new Error(
i18n._(
t`Requesting \`${amount}\` records on the \`Affiliation\` connection exceeds the \`${argSet}\` limit of 100 records.`,
),
)
} else if (typeof first !== 'undefined' && typeof last === 'undefined') {
limitTemplate = aql`TO_NUMBER(affiliation._key) ASC LIMIT TO_NUMBER(${first})`
} else if (typeof first === 'undefined' && typeof last !== 'undefined') {
limitTemplate = aql`TO_NUMBER(affiliation._key) DESC LIMIT TO_NUMBER(${last})`
}
} else {
const argSet = typeof first !== 'undefined' ? 'first' : 'last'
const typeSet = typeof first !== 'undefined' ? typeof first : typeof last
console.warn(
`User: ${userKey} attempted to have \`${argSet}\` set as a ${typeSet} for: loadAffiliationConnectionsByOrgId.`,
)
throw new Error(
i18n._(t`\`${argSet}\` must be of type \`number\` not \`${typeSet}\`.`),
)
}
let hasNextPageFilter = aql`FILTER TO_NUMBER(affiliation._key) > TO_NUMBER(LAST(retrievedAffiliations)._key)`
let hasPreviousPageFilter = aql`FILTER TO_NUMBER(affiliation._key) < TO_NUMBER(FIRST(retrievedAffiliations)._key)`
if (typeof orderBy !== 'undefined') {
let hasNextPageDirection = aql``
let hasPreviousPageDirection = aql``
if (orderBy.direction === 'ASC') {
hasNextPageDirection = aql`>`
hasPreviousPageDirection = aql`<`
} else {
hasNextPageDirection = aql`<`
hasPreviousPageDirection = aql`>`
}
let affField, hasNextPageDocument, hasPreviousPageDocument
/* istanbul ignore else */
if (orderBy.field === 'user-username') {
affField = aql`DOCUMENT(users, PARSE_IDENTIFIER(affiliation._to).key).userName`
hasNextPageDocument = aql`DOCUMENT(users, PARSE_IDENTIFIER(LAST(retrievedAffiliations)._to).key).userName`
hasPreviousPageDocument = aql`DOCUMENT(users, PARSE_IDENTIFIER(FIRST(retrievedAffiliations)._to).key).userName`
}
hasNextPageFilter = aql`
FILTER ${affField} ${hasNextPageDirection} ${hasNextPageDocument}
OR (${affField} == ${hasNextPageDocument}
AND TO_NUMBER(affiliation._key) > TO_NUMBER(LAST(retrievedAffiliations)._key))
`
hasPreviousPageFilter = aql`
FILTER ${affField} ${hasPreviousPageDirection} ${hasPreviousPageDocument}
OR (${affField} == ${hasPreviousPageDocument}
AND TO_NUMBER(affiliation._key) < TO_NUMBER(FIRST(retrievedAffiliations)._key))
`
}
let sortByField = aql``
if (typeof orderBy !== 'undefined') {
/* istanbul ignore else */
if (orderBy.field === 'user-username') {
sortByField = aql`DOCUMENT(users, PARSE_IDENTIFIER(affiliation._to).key).userName ${orderBy.direction},`
}
}
let sortString
if (typeof last !== 'undefined') {
sortString = aql`DESC`
} else {
sortString = aql`ASC`
}
let userSearchQuery = aql``
let userIdFilter = aql``
if (typeof search !== 'undefined' && search !== '') {
search = cleanseInput(search)
userSearchQuery = aql`
LET tokenArr = TOKENS(${search}, "text_en")
LET userIds = UNIQUE(
FOR token IN tokenArr
FOR user IN userSearch
SEARCH ANALYZER(
user.displayName LIKE CONCAT("%", token, "%")
OR user.userName LIKE CONCAT("%", token, "%")
, "text_en")
RETURN user._id
)
`
userIdFilter = aql`FILTER e._to IN userIds`
}
let filteredAffiliationCursor
try {
filteredAffiliationCursor = await query`
WITH affiliations, organizations, users, userSearch
${userSearchQuery}
LET affiliationKeys = (
FOR v, e IN 1..1 OUTBOUND ${orgId} affiliations
${userIdFilter}
RETURN e._key
)
LET retrievedAffiliations = (
FOR affiliation IN affiliations
FILTER affiliation._key IN affiliationKeys
LET orgKey = PARSE_IDENTIFIER(affiliation._from).key
LET userKey = PARSE_IDENTIFIER(affiliation._to).key
${afterTemplate}
${beforeTemplate}
SORT
${sortByField}
${limitTemplate}
RETURN MERGE(
{
id: affiliation._key,
orgKey: orgKey,
userKey: userKey,
_type: "affiliation"
},
affiliation
)
)
LET hasNextPage = (LENGTH(
FOR affiliation IN affiliations
FILTER affiliation._key IN affiliationKeys
${hasNextPageFilter}
SORT ${sortByField} TO_NUMBER(affiliation._key) ${sortString} LIMIT 1
RETURN affiliation
) > 0 ? true : false)
LET hasPreviousPage = (LENGTH(
FOR affiliation IN affiliations
FILTER affiliation._key IN affiliationKeys
${hasPreviousPageFilter}
SORT ${sortByField} TO_NUMBER(affiliation._key) ${sortString} LIMIT 1
RETURN affiliation
) > 0 ? true : false)
RETURN {
"affiliations": retrievedAffiliations,
"totalCount": LENGTH(affiliationKeys),
"hasNextPage": hasNextPage,
"hasPreviousPage": hasPreviousPage,
"startKey": FIRST(retrievedAffiliations)._key,
"endKey": LAST(retrievedAffiliations)._key
}
`
} catch (err) {
console.error(
`Database error occurred while user: ${userKey} was trying to query affiliations in loadAffiliationConnectionsByOrgId, error: ${err}`,
)
throw new Error(
i18n._(t`Unable to query affiliation(s). Please try again.`),
)
}
let filteredAffiliations
try {
filteredAffiliations = await filteredAffiliationCursor.next()
} catch (err) {
console.error(
`Cursor error occurred while user: ${userKey} was trying to gather affiliations in loadAffiliationConnectionsByOrgId, error: ${err}`,
)
throw new Error(
i18n._(t`Unable to load affiliation(s). Please try again.`),
)
}
if (filteredAffiliations.affiliations.length === 0) {
return {
edges: [],
totalCount: 0,
pageInfo: {
hasNextPage: false,
hasPreviousPage: false,
startCursor: '',
endCursor: '',
},
}
}
const edges = filteredAffiliations.affiliations.map((affiliation) => {
return {
cursor: toGlobalId('affiliation', affiliation._key),
node: affiliation,
}
})
return {
edges,
totalCount: filteredAffiliations.totalCount,
pageInfo: {
hasNextPage: filteredAffiliations.hasNextPage,
hasPreviousPage: filteredAffiliations.hasPreviousPage,
startCursor: toGlobalId('affiliation', filteredAffiliations.startKey),
endCursor: toGlobalId('affiliation', filteredAffiliations.endKey),
},
}
}