-
Notifications
You must be signed in to change notification settings - Fork 0
/
slots.js
68 lines (57 loc) · 1.38 KB
/
slots.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
import {zipObject, map} from 'lodash'
// create slots
const createSlot = async (slot: Slot, client: Client) => {
const {name, xws} = slot
const result = await client.mutate(`{
slot: createSlot(
name: "${name}",
xws: "${xws}"
){
id
}
}`)
if(!result) {
console.log(`Error adding ${slot}:`)
console.log(slot)
}
// else {
// console.log(`Added ${name} slot`)
// }
return result.slot.id
}
const findSlot = async (slot: Slot, client: Client) => {
const {name} = slot
const result = await client.query(`{
allSlots(
filter: {
name: "${name}"
}
) {
id
}
}`)
return result
}
export const slotListToObj = (allSlots: string[]) => {
const toObj = (item) => {
const xws = item.replace(/\W/g,'').toLowerCase()
return {
'name': item,
'xws': xws
}
}
return map(allSlots, toObj)
}
export const createSlots = async (rawSlots: Slot[], client: Client): Promise<IdMap> => {
const slotIds = await Promise.all(rawSlots.map( async(slot) => {
// check if slot already exists
const existingSlot = await findSlot(slot, client)
// if slot doesn't exist, create it
if (existingSlot.allSlots.length === 0) {
return await createSlot(slot, client)
} else {
return existingSlot.allSlots[0].id
}
}))
return zipObject(rawSlots.map(slot => slot.name), slotIds)
}