-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.js
75 lines (75 loc) · 2.56 KB
/
methods.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
Meteor.methods({
updateSchedule: function(obj, id) {
if (!Roles.userIsInRole(Meteor.userId(), ["admin"])) {
throw new Meteor.Error("not-authorized");
}
else {
if (Events.findOne({event: id})) { // if a schedule exists, update it.
Events.update({event: id}, {$set: {'sched': obj}});
}
else {
Events.insert({ // create a new document
event: id,
sched: obj
});
}
}
},
makeQueuer: function(id) {
if (!Roles.userIsInRole(Meteor.userId(), ["admin"])) {
throw new Meteor.Error("not-authorized");
}
else {
Roles.addUsersToRoles(id, ["queuer"]);
}
},
demoteQueuer: function(id){
if (!Roles.userIsInRole(Meteor.userId(), ["admin"])) {
throw new Meteor.Error("not-authorized");
}
else {
Roles.removeUsersFromRoles(id, ["queuer"]);
}
},
followTeam: function(teamNum, userId) {
if (Teams.findOne({number: teamNum})) {
if (Teams.findOne({number: teamNum}).followers.indexOf(userId) < 0) {
Teams.update({number: teamNum}, {$push: {"followers": userId}});
}
}
else {
Teams.insert({
number: teamNum,
followers: [userId]
})
}
},
unfollowTeam: function(teamNum, userId) {
if (Teams.findOne({number: teamNum})) { // if the team exists
Teams.update({number: teamNum}, {$pull: {followers: userId}}); // pull the user from the followers array
}
},
textFollowers: function(d, roundNum) {
var labels = ['red1', 'red2', 'blue1', 'blue2'];
for (var i = 0; i < labels.length; i++) {
var tid = d[labels[i]]; // the team number for one of the teams in the match.
if (Teams.findOne({number: tid})) { // if the team exists in the database
var users = Teams.findOne({number: tid}).followers; // get the array of their followers
for (var i = 0; i < users.length; i++) { // loop thru their followers
var number = Meteor.users.findOne(users[i]).profile.number;
if (number) { // if they have a phone number, send them a text.
HTTP.post("https://api.twilio.com/2010-04-01/Accounts/"+twilioKey.sid+"/Messages.json",{
params: {
From: "+12018856228",
To: number,
Body: "Hello! Your team #"+tm+" is ready to queue on field " + (roundNum % 2 == 0 ? "2" : "1")+" for round #"+roundNum+"."
// TODO: Have a variable number of fields (1,2,3 etc).
},
auth: twilioKey.sid +":"+ twilioKey.token
});
}
}
}
}
}
});