-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicyActionTypes.mjs
83 lines (71 loc) · 1.98 KB
/
policyActionTypes.mjs
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
/**
*
* importing and then exporting policy action types
*/
import '@babel/polyfill'
import dotenv from 'dotenv'
import sanityClient from '@sanity/client'
dotenv.config()
const client = sanityClient({
apiVersion: process.env.SANITY_API_VERSION,
dataset: 'production',
projectId: process.env.SANITY_PROJECT_ID,
token: process.env.SANITY_ACCESS_TOKEN,
useCdn: false // We can't use the CDN for writing
})
const query = `*[_type == "policy_action"] { country, type, title }`
const downloadTypes = async () => {
const actions = await client.fetch(query)
const types = []
const govts = []
actions.map(action => {
types.push(action.type)
govts.push(action.country)
})
//dedup the array and
const deduped = {
types: [...new Set(types)].map(type => {
return {
_type: "dropdown_option",
entry: {
type: 'actionType',
value: type
}
}
}),
govts: [...new Set(govts)].map(govt => {
return {
_type: "dropdown_option",
entry: {
type: 'actionGovt',
value: govt
}
}
})
}
return deduped
}
const main = async () => {
const toUpload = await downloadTypes()
toUpload.types.forEach(type => {
client.create(type)
.then((doc) => {
console.log("File updated! New document: ");
console.log(doc);
})
.catch((err) => {
console.error("Update failed: ", err.message);
})
})
toUpload.govts.forEach(govt => {
client.create(govt)
.then((doc) => {
console.log("File updated! New document: ");
console.log(doc);
})
.catch((err) => {
console.error("Update failed: ", err.message);
})
})
}
main()