-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
218 lines (192 loc) · 5.28 KB
/
database.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
210
211
212
213
214
215
216
217
218
var MongoClient = require('mongodb').MongoClient;
/* Takes the group and adds to database
* input: {object} : data as fields
* {
* teamName : {string} : name of slack team
* channelName : {string} : name of channel aka study-group
* topic : {string} : topic for that study-group
* creator: {string} : Id of creator
* }
*/
exports.setTopic = (input) => {
let data = {
"slack-team": input.teamName,
"channel-name": input.channelName,
"channel-id": input.channelId,
"topic": input.topic,
"auth-key": input.authKey
}
MongoClient.connect(process.env.MONGO_URL, (err, db) => {
db.collection('study-groups').updateOne({
"slack-team": input.teamName,
"channel-name": input.channelName
}, data, {upsert:true, w: 1}, (err, result) => {
if(err != null){
console.log("Error happened :(", err);
}
db.close();
})
});
}
/* Takes the topic and finds the groups related to it
* teamName : {string} : name of slack team
* topic : {string} : topic to query
* callback : {function (array)} : handle the query result
*/
exports.queryTopic = (teamName, topic, callback) => {
MongoClient.connect(process.env.MONGO_URL, (err, db) => {
db.collection('study-groups').createIndex(
{
"topic":"text"
});
db.collection('study-groups').find({
"slack-team": teamName,
"topic": {
$regex : topic,
$options: "$i"
}
}).toArray((err, docs) => {
if(err == null){
callback(docs);
}
else{
console.log(err);
}
db.close();
});
})
}
/* Returns all groups in the slack-team
* teamName : {string} : name of slack team
* callback : {function (array)} : handle the query result
*/
exports.queryAll = (teamName, callback) => {
MongoClient.connect(process.env.MONGO_URL, (err, db) => {
db.collection('study-groups').find({
"slack-team": teamName,
}).toArray((err, docs) => {
if(err == null){
callback(docs);
}
else{
console.log(err);
}
db.close();
});
});
}
/* Add the access token to database
* teamName : {string} : Name of team
* access_token : {string} : Code to get access for team
*/
exports.storeToken = (teamName, teamid, userid, access_token) => {
let data = {
"slack-team": teamName,
"team-id": teamid,
"user-id": userid,
"token": access_token
}
MongoClient.connect(process.env.MONGO_URL, (err, db) => {
db.collection('slack_user_tokens').updateOne({
"slack-team": teamName
}, data, {upsert:true, w: 1}, (err, result)=>{
if(err != null){
console.log("Error happened :(", err);
}
db.close();
})
});
}
/* Get the token from database given the team domain
* team_id : {string} : Team domain name
* user_id : {string} : User Id to find
* callback : {function (err, string)} : Function to handle the token
*/
exports.getToken = (team_id, user_id, callback) => {
MongoClient.connect(process.env.MONGO_URL, (err, db) => {
db.collection('slack_user_tokens').findOne({
"team-id": team_id,
"user-id": user_id
}).then((data)=>{
if(!data){
callback(404, null);
return;
}
callback(null, data.token);
})
});
}
/* Gets the specified channel data
* team_id : {string} : Team domain name
* channel_id : {string} : Channel id to find
* callback : {function(err, data)} : function to handle the channel data
*/
exports.getChannel = (team_name, channel_id, callback) => {
MongoClient.connect(process.env.MONGO_URL, (err, db) => {
db.collection('study-groups').findOne({
"slack-team": team_name,
"channel-id": channel_id
}
).then((data)=>{
if(!data){
callback(404 , null);
return;
}
callback(null, data);
});
});
}
/* Gets the specified channel data from study-groups collection, i.e. :
* channel-id
* auth-key
*/
exports.getChannelInfo = (team_name, channel_name, callback) => {
MongoClient.connect(process.env.MONGO_URL, (err, db) => {
db.collection('study-groups').findOne({
"slack-team": team_name,
"channel-name": channel_name
}).then((data) => {
if(!data){
callback(404, null);
}
else{
callback(null,data);
}
db.close();
});
});
}
/* Check team exists
* team_id : {string} : Team domain name
* callback : {function (err, string)} : Function to handle the token
*/
exports.checkTeam = (team_id, callback) => {
MongoClient.connect(process.env.MONGO_URL, (err, db) => {
db.collection('slack_user_tokens').findOne({
"team-id": team_id
}).then((data)=>{
if(!data){
callback(404, null);
return;
}
callback(null, data.token);
})
});
}
exports.removeChannel = (input) => {
let data = {
"slack-team": input.teamName,
"channel-id": input.channelId
}
MongoClient.connect(process.env.MONGO_URL, (err, db) => {
db.collection('study-groups').updateOne({
"slack-team": input.teamName,
"channel-name": input.channelName
}, data, {upsert:true, w: 1}, (err, result) => {
if(err != null){
console.log("Error happened :(", err);
}
db.close();
})
});
}