forked from joystickinteractive/slack-vote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
persist.js
168 lines (153 loc) · 2.82 KB
/
persist.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
const { request } = require('graphql-request')
const assert = require('assert').strict;
var endpoint = 'http://localhost:8080/graphql'
const get_query = `
query($inp: String!) {
queryPoll(filter:{id:{eq:$inp}}) {
created
pollName
active
answers {
answerName
id
votes {
created
userName
userID
}
}
}
}
`
const update_query = `
mutation($inp: PollPatch, $id:String!) {
updatePoll(input:{
filter: {id: {eq: $id}},
set:$inp
}) {
poll {
created
pollName
active
answers {
answerName
id
votes {
created
userName
userID
}
}
}
}
}
`
const add_query = `
mutation($inp: AddPollInput!) {
addPoll(input: [$inp]) {
poll {
created
pollName
active
answers {
answerName
id
votes {
created
userName
userID
}
}
}
}
}
`
const delete_poll_query = `
mutation($id: String) {
deletePoll(filter:{id:{eq: $id}}) {
msg
}
}
`
const delete_query = `
mutation($id: ID!){
deleteAnswer(filter:{id:[$id]}) {
msg
}
}
`
var redis = require('redis')
, pollnameText = ''
, triggerWord = ''
, pollnameText = ''
, slackRes = ''
, client = ''
, rtg = ''
, operationComplete = false
, ts = Math.floor(Date.now() / 1000);
/*
* Set correct environment for redis.
*
* Lines 19-20 are for using Heroku Redis
* If not using Heroku Redis, uncomment lines 22-23 and comment out lines 19-20
*
*/
var getPoll = async function(pollId, callback) {
try {
console.log("****GET POLL****", pollId);
assert(pollId != null)
const data = await request(endpoint, get_query, {inp: pollId})
var reply = JSON.stringify(data["queryPoll"][0], undefined, 2)
if (data["queryPoll"].length > 0) {
callback(reply);
} else {
callback(null);
}
} catch(err) {
console.log("here1", err);
callback(null);
}
}
var dbActions = {
deletePoll: async function(pollKey, callback) {
try {
const data = await request(endpoint, delete_poll_query, {id: pollKey})
callback(data)
} catch (err) {
console.log("here2", err);
callback(null);
}
},
deleteAnswer: async function(answerKey, callback) {
try {
const data = await request(endpoint, delete_query, {id: answerKey})
callback(data)
} catch (err) {
console.log("here2", err);
callback(null);
}
},
/*
* Set poll data.
*/
setPoll: async function(pollKey, pollData, callbacki) {
getPoll(pollKey, async (reply) => {
assert(pollKey != null);
console.log("Setting to:", pollKey)
if (reply) {
const vars = {inp: JSON.parse(pollData), id: pollKey}
const data = await request(endpoint, update_query, vars)
callbacki(pollData);
} else {
var temp = Object.assign({id: pollKey}, JSON.parse(pollData))
const data = await request(endpoint, add_query, {inp: temp})
callbacki(pollData);
}
})
},
/*
* Get poll from id.
*/
getPoll: getPoll
};
module.exports = dbActions;