forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupsert-ownership.js
More file actions
42 lines (40 loc) · 1.22 KB
/
upsert-ownership.js
File metadata and controls
42 lines (40 loc) · 1.22 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
const upsertOwnership = async ({ ownership, key, query }) => {
try {
await query`
LET givenDomains = ${ownership}
LET orgAcronym = ${String(key)}
FOR domain IN givenDomains
LET domainId = FIRST(
FOR d IN domains
FILTER d.domain == domain
RETURN d._id
)
FILTER LENGTH(domainId) > 0
LET ownershipKey = (
FOR v, e IN 1..1 ANY domainId ownership
RETURN e._key
)
LET orgId = FIRST(
FOR org IN organizations
FILTER (org.orgDetails.en.acronym == orgAcronym)
OR (org.orgDetails.fr.acronym == orgAcronym)
RETURN org._id
)
FILTER LENGTH(orgId) > 0
UPSERT { _to: domainId }
INSERT { _from: orgId, _to: domainId }
UPDATE { _from: orgId, _to: domainId }
IN ownership OPTIONS { waitForSync: true }
RETURN { doc: NEW }
`
} catch (err) {
console.error(
`Error occurred while inserting/updating ownerships for ${String(
key,
)}: ${err}`,
)
}
}
module.exports = {
upsertOwnership,
}