forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-affiliation-by-key.js
More file actions
41 lines (37 loc) · 1.29 KB
/
load-affiliation-by-key.js
File metadata and controls
41 lines (37 loc) · 1.29 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
import DataLoader from 'dataloader'
import { t } from '@lingui/macro'
export const loadAffiliationByKey = ({ query, userKey, i18n }) =>
new DataLoader(async (ids) => {
let cursor
try {
cursor = await query`
WITH affiliations, organizations, users
FOR affiliation IN affiliations
FILTER affiliation._key IN ${ids}
LET orgKey = PARSE_IDENTIFIER(affiliation._from).key
LET userKey = PARSE_IDENTIFIER(affiliation._to).key
RETURN MERGE(affiliation, { id: affiliation._key, orgKey: orgKey, userKey: userKey, _type: "affiliation" })
`
} catch (err) {
console.error(
`Database error occurred when user: ${userKey} running loadAffiliationByKey: ${err}`,
)
throw new Error(
i18n._(t`Unable to find user affiliation(s). Please try again.`),
)
}
const affiliationMap = {}
try {
await cursor.forEach((affiliation) => {
affiliationMap[affiliation._key] = affiliation
})
} catch (err) {
console.error(
`Cursor error occurred when user: ${userKey} running loadAffiliationByKey: ${err}`,
)
throw new Error(
i18n._(t`Unable to find user affiliation(s). Please try again.`),
)
}
return ids.map((id) => affiliationMap[id])
})