forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-organization-by-slug.js
More file actions
56 lines (52 loc) · 1.63 KB
/
load-organization-by-slug.js
File metadata and controls
56 lines (52 loc) · 1.63 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
import DataLoader from 'dataloader'
import { t } from '@lingui/macro'
export const loadOrgBySlug = ({ query, language, userKey, i18n }) =>
new DataLoader(async (slugs) => {
let cursor
try {
cursor = await query`
WITH claims, domains, organizations
FOR org IN organizations
FILTER org.orgDetails.en.slug IN ${slugs}
OR org.orgDetails.fr.slug IN ${slugs}
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,
slugEN: org.orgDetails.en.slug,
slugFR: org.orgDetails.fr.slug
},
TRANSLATE(${language}, org.orgDetails)
)
`
} catch (err) {
console.error(
`Database error occurred when user: ${userKey} running loadOrgBySlug: ${err}`,
)
throw new Error(
i18n._(t`Unable to load organization(s). Please try again.`),
)
}
const orgMap = {}
try {
await cursor.forEach((org) => {
orgMap[org.slugEN] = org
orgMap[org.slugFR] = org
})
} catch (err) {
console.error(
`Cursor error occurred when user: ${userKey} running loadOrgBySlug: ${err}`,
)
throw new Error(
i18n._(t`Unable to load organization(s). Please try again.`),
)
}
return slugs.map((slug) => orgMap[slug])
})