forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (79 loc) · 2.05 KB
/
index.js
File metadata and controls
92 lines (79 loc) · 2.05 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
require('dotenv-safe').config({
example: './.env.example',
path: './.env',
})
const { DB_PASS: rootPass, DB_URL: url, DB_NAME: databaseName } = process.env
const { ensure } = require('arango-tools')
const { databaseOptions } = require('./database-options')
const {
checkClaimCount,
checkDomain,
checkOrganization,
createClaim,
createDomain,
createOrganization,
slugify,
} = require('./src')
;(async () => {
let data
try {
data = require('./organization-domains.json')
} catch (err) {
console.error(err)
return
}
const { query, collections, transaction } = await ensure({
type: 'database',
name: databaseName,
url,
rootPassword: rootPass,
options: databaseOptions({ rootPass }),
})
// setup transactions
// Generate list of collections names
const collectionStrings = []
for (const property in collections) {
collectionStrings.push(property.toString())
}
// Setup Transaction
const trx = await transaction(collectionStrings)
for (const key in data) {
const org = await checkOrganization({ data, key, query })
if (typeof org === 'undefined') {
createOrganization({ slugify, data, key, trx, query })
} else {
console.info(`\tOrg found.`)
}
for (const domain of data[key].domains) {
const checkedDomain = await checkDomain({ query, domain })
if (typeof checkedDomain === 'undefined') {
const savedDomain = await createDomain({ trx, query, domain })
await createClaim({
trx,
query,
domainId: savedDomain._id,
orgId: org._id,
})
} else {
console.log(`\tDomain already found.`)
const claimCount = await checkClaimCount({
query,
domainId: checkedDomain._id,
})
if (claimCount === 0) {
await createClaim({
trx,
query,
domainId: checkedDomain._id,
orgId: org._id,
})
}
}
}
try {
trx.commit()
} catch (err) {
throw new Error(err)
}
}
})()