-
Notifications
You must be signed in to change notification settings - Fork 1
/
initial-data.js
209 lines (194 loc) · 5.87 KB
/
initial-data.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* eslint-disable no-console */
const crypto = require('crypto');
const axios = require('axios');
const { map, flatten, omit } = require('lodash');
const randomString = () => crypto.randomBytes(6).hexSlice();
const addInitialUser = async (keystone, context) => {
// Count existing users
const {
data: {
_allUsersMeta: { count },
},
} = await keystone.executeGraphQL({
context,
query: `query {
_allUsersMeta {
count
}
}`,
});
if (count === 0) {
const password = randomString();
const email = '[email protected]';
const { errors } = await keystone.executeGraphQL({
context,
query: `mutation initialUser($password: String, $email: String) {
createUser(data: {name: "Admin", email: $email, isAdmin: true, password: $password}) {
id
}
}`,
variables: { password, email },
});
if (errors) {
console.log('failed to create initial user:');
console.log(errors);
} else {
console.log(`
User created:
email: ${email}
password: ${password}
Please change these details after initial login.
`);
}
}
};
const populateLayers = async (keystone, context) => {
// Populate layers
const {
data: { allLayers },
} = await keystone.executeGraphQL({
context,
query: `query {
allLayers {
id
layerId
}
}`,
});
const allLayerNames = map(allLayers, 'layerId');
let { data: layers } = await axios.get(`${process.env.NEXT_PUBLIC_SEARCH_API}/layers`);
layers = layers.map(layer => ({
layerId: layer.name.toLowerCase(),
title: layer.title,
remoteId: layer.id,
}));
await Promise.all(
layers.map(layer => {
if (!allLayerNames.includes(layer.layerId)) {
return keystone.executeGraphQL({
context,
query: `
mutation InitLayer($layerId: String, $title: String, $remoteId: Int) {
createLayer(data: { layerId: $layerId, title: $title, remoteId: $remoteId }) {
id
}
}`,
variables: layer,
});
}
const existingLayer = allLayers.find(l => l.layerId === layer.layerId);
if (!existingLayer) return Promise.resolve();
const { id } = existingLayer;
return keystone.executeGraphQL({
context,
query: `
mutation UpdateLayer($id: ID!, $layerId: String, $title: String, $remoteId: Int) {
updateLayer(id: $id, data: { layerId: $layerId, title: $title, remoteId: $remoteId }) {
id
}
}`,
variables: { id, ...layer },
});
})
);
};
const populateBasemaps = async (keystone, context) => {
// Populate basemaps
const {
data: { allBasemaps },
} = await keystone.executeGraphQL({
context,
query: `query {
allBasemaps {
id
ssid
}
}`,
});
const allBasemapIds = map(allBasemaps, 'ssid');
let { data } = await axios.get(`${process.env.NEXT_PUBLIC_SEARCH_API}/documents`);
data = data.filter(d => d.title !== 'Views');
const documents = flatten(map(data, 'Documents'));
const documentReqs = documents.map(m => {
const variables = {
...m,
firstYear: m.firstyear,
lastYear: m.lastyear,
};
if (!allBasemapIds.includes(`${m.ssid}`) && !allBasemapIds.includes(`SSID${m.ssid}`)) {
return keystone.executeGraphQL({
context,
query: `
mutation InitBasemap($ssid: String, $title: String, $firstYear: Int, $lastYear: Int, $longitude: Float, $latitude: Float, $thumbnail: String, $creator: String) {
createBasemap(data: { ssid: $ssid, title: $title, firstYear: $firstYear, lastYear: $lastYear, longitude: $longitude, latitude: $latitude, thumbnail: $thumbnail, creator: $creator }) {
id
}
}`,
variables,
});
}
const existingBasemap = allBasemaps.find(l => l.ssid === m.ssid || l.ssid === `SSID${m.ssid}`);
if (!existingBasemap) return Promise.resolve();
variables.id = existingBasemap.id;
return keystone.executeGraphQL({
context,
query: `
mutation UpdateBasemap($id: ID!, $ssid: String, $title: String, $firstYear: Int, $lastYear: Int, $longitude: Float, $latitude: Float, $thumbnail: String, $creator: String) {
updateBasemap(id: $id, data: { ssid: $ssid, title: $title, firstYear: $firstYear, lastYear: $lastYear, longitude: $longitude, latitude: $latitude, thumbnail: $thumbnail, creator: $creator }) {
id
}
}`,
variables,
});
});
return Promise.all(documentReqs);
};
const migrateImages = async (keystone, context) => {
const {
data: { allSlides },
} = await keystone.executeGraphQL({
context,
query: `query {
allSlides {
id
imageTitle
image {
imageTitle: title
creator
source
date
url
}
}
}`,
});
const imageUpdateRequests = allSlides
.filter(slide => slide.image && !slide.imageTitle)
.map(slide => {
const { url } = slide.image;
const imageTitle = Object.values(omit(slide.image, 'url'))
.filter(v => v)
.join(', ');
return keystone.executeGraphQL({
context,
query: `mutation UpdateSlideImage($id: ID!, $imageTitle: String, $url: String) {
updateSlide(id: $id, data: { imageTitle: $imageTitle, url: $url }) {
id
}
}`,
variables: {
id: slide.id,
url,
imageTitle,
},
});
});
return Promise.all(imageUpdateRequests);
};
module.exports = async keystone => {
const context = keystone.createContext({ skipAccessControl: true });
await addInitialUser(keystone, context);
await populateLayers(keystone, context);
await populateBasemaps(keystone, context);
await migrateImages(keystone, context);
};