forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-organization-by-key.js
More file actions
52 lines (48 loc) · 1.43 KB
/
load-organization-by-key.js
File metadata and controls
52 lines (48 loc) · 1.43 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
import DataLoader from 'dataloader'
import { t } from '@lingui/macro'
export const loadOrgByKey = ({ query, language, userKey, i18n }) =>
new DataLoader(async (ids) => {
let cursor
try {
cursor = await query`
WITH claims, domains, organizations
FOR org IN organizations
FILTER org._key IN ${ids}
LET orgDomains = (FOR v, e IN 1..1 OUTBOUND org._id claims RETURN e._to)
RETURN MERGE(
{
_id: org._id,
_key: org._key,
_rev: org._rev,
_type: "organization",
id: org._key,
verified: org.verified,
domainCount: COUNT(orgDomains),
summaries: org.summaries
},
TRANSLATE(${language}, org.orgDetails)
)
`
} catch (err) {
console.error(
`Database error occurred when user: ${userKey} running loadOrgByKey: ${err}`,
)
throw new Error(
i18n._(t`Unable to load organization(s). Please try again.`),
)
}
const orgMap = {}
try {
await cursor.forEach((org) => {
orgMap[org._key] = org
})
} catch (err) {
console.error(
`Cursor error occurred when user: ${userKey} during loadOrgByKey: ${err}`,
)
throw new Error(
i18n._(t`Unable to load organization(s). Please try again.`),
)
}
return ids.map((id) => orgMap[id])
})