-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditions.js
60 lines (50 loc) · 1.46 KB
/
conditions.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
import {zipObject} from 'lodash'
// create conditions
const createCondition = async (condition: Condition, client: Client) => {
const {id: oldId, name, unique = false, text='', image, xws} = condition
const result = await client.mutate(`{
condition: createCondition(
oldId: ${oldId},
name: "${name.replace(/"/g, '"e;')}",
unique: ${unique},
text: "${text.replace(/"/g, '"e;')}",
image: "${image}",
xws: "${xws}"
){
id
}
}`)
if(!result) {
console.log(condition)
}
// else {
// console.log(`Added ${name}`)
// }
return result.condition.id
}
const findCondition = async (condition: Condition, client: Client) => {
const {id: oldId} = condition
const result = await client.query(`{
allConditions(
filter: {
oldId: ${oldId}
}
) {
id
}
}`)
return result
}
export const createConditions = async (rawConditions: Condition[], client: Client): Promise<IdMap> => {
const conditionIds = await Promise.all(rawConditions.map( async(condition) => {
// check if condition already exists
const existingCondition = await findCondition(condition, client)
// if condition doesn't exist, create it
if (existingCondition.allConditions.length === 0) {
return await createCondition(condition, client)
} else {
return existingCondition.allConditions[0].id
}
}))
return zipObject(rawConditions.map(condition => condition.id), conditionIds)
}