-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolvers.js
467 lines (426 loc) · 13.3 KB
/
resolvers.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
const emailValidator = require('email-validator');
const { assertDirective } = require('graphql');
const PG_UNIQUE_VIOLATION = '23505';
const GQL_UNKNOWN_ERROR = 'ERR_UNKNOWN'
const GQL_UNIQUE_VIOLATION = 'ERR_DUPLICATE';
const GQL_INVALID_INPUT = 'ERR_INVALID_INPUT'
const BYE = '-1';
const BYE_LIMIT = 4;
const FINAL_WEEK = 18;
const resolvers = {
Query: {
async user(parent, { email }, { dataSources }, info) {
try {
const result = await dataSources.pg.getUserByEmail(email.toLowerCase());
return result;
} catch (err) {
console.log(err.stack);
}
},
async league(parent, { leagueID }, context, info) {
let [league, users, teams] = await Promise.all([
context.dataSources.pg.getLeagueById(leagueID),
context.dataSources.pg.getLeagueMembers(leagueID),
context.dataSources.pg.getTeams()
]);
let picks;
if (league.season === process.env.CURRENT_SEASON) {
picks = await context.dataSources.pg.getPicksForLeague(leagueID);
} else {
picks = await context.dataSources.pg.getPicksForLeague(leagueID, true);
}
context.picks = picks;
context.users = users;
context.teams = teams;
return leagueFromRow(league);
},
async leagues(parent, { userID }, { dataSources }, info) {
try {
let result;
if (userID) {
result = await dataSources.pg.getLeaguesForUser(userID);
} else {
result = await dataSources.pg.getAllLeagues();
}
return result.map(function(row) {
return leagueFromRow(row);
});
} catch (err) {
console.log(err.stack);
}
},
async sportsTeams(parent, args, { dataSources }, info) {
try {
const result = await dataSources.pg.getTeams();
return result.map(function(row) {
return teamFromRow(row);
});
} catch (err) {
console.log(err.stack);
}
},
async sportsGames(parent, { season }, { dataSources }, info) {
try {
let [result, allTeams] = await Promise.all([
dataSources.pg.getSportsGames(season ? season : process.env.CURRENT_SEASON),
dataSources.pg.getTeams()
]);
return gamesFromRows(result, allTeams);
} catch (err) {
console.log(err.stack);
}
},
async currentPick(parent, {leagueID, userID}, { dataSources }, info) {
try {
const result = await dataSources.pg.getCurrentPick(leagueID, userID, parseInt(process.env.CURRENT_WEEK));
return result.map(function(row) {
return pickFromRow(row);
});
} catch (err) {
console.log(err.stack);
}
},
async currentSeason(parent, {league}) {
return process.env.CURRENT_SEASON;
}
},
Mutation: {
async submitPick(parent, { request }, context, info) {
const dataSources = context.dataSources;
const validPick = await validatePick(request, dataSources.pg, context);
if (validPick) {
const picks = await registerPick(request, dataSources.pg);
if (!picks) {
return {
pick: null,
errors: [{
code: GQL_UNKNOWN_ERROR,
message: 'Storing the pick failed. Please retry.'
}]
};
} else { // Success!
return {
pick: picks
};
}
} else {
return {
pick: null,
errors: [{
code: GQL_INVALID_INPUT,
message: context.errorMessage
}]
};
}
}
},
SportsGame: {
async result(game, args) {
if (game.awayTeamScore === null || game.homeTeamScore === null) {
return {
complete: false
};
}
return {
complete: true,
awayTeamScore: game.awayTeamScore,
homeTeamScore: game.homeTeamScore
};
}
},
FantasyLeague: {
async owner(league, args, { dataSources }, info) {
try {
const result = await dataSources.pg.getLeagueOwner(league.id, league.ownerID);
return userFromRow(result);
} catch (err) {
console.log(err.stack);
}
},
async users(league, args, context, info) {
if (context.users) {
return context.users.map(function(row) {
return userFromRow(row);
});
} else {
throw new Error('User list not found');
}
},
async picks(league, args, context, info) {
if (context.picks) {
return context.picks.map(function(row) {
return pickFromRow(row);
});
} else {
throw new Error('Pick list not found');
}
}
},
Pick: {
async user(pick, args, context, info) {
if (context.users) {
return userFromRow(context.users.find(user => user.user_id === pick.userID));
} else {
try {
const result = await context.dataSources.pg.getLeagueMembers(pick.leagueID);
return userFromRow(result.find(user => user.id === pick.userID));
} catch (err) {
console.log(err.stack);
}
}
},
async league(pick, args, { dataSources }, info) {
try {
const result = await dataSources.pg.getLeagueById(pick.leagueID);
return leagueFromRow(result);
} catch (err) {
console.log(err.stack);
}
},
async team(pick, args, context, info) {
if (pick.teamID === -1) {
return {
id: 'bye',
name: 'BYE',
shortName: 'BYE',
sportsLeague: 'NFL'
};
}
if (context.teams) {
return teamFromRow(context.teams.find(row => row.id === pick.teamID));
} else {
try {
const result = await context.dataSources.pg.getTeams();
return teamFromRow(result.find(row => row.id === pick.teamID));
} catch (err) {
console.log(err.stack);
}
}
},
},
User: {
async fantasyLeagues(user, args, { dataSources }, info) {
try {
const result = await dataSources.pg.getLeaguesForUser(user.id);
return result.map(function(row) {
return leagueFromRow(row);
});
} catch (err) {
console.log(err.stack);
}
},
async displayName(user, { leagueID }, { dataSources }, info) {
// Don't bother with db query if we already have the data
if (user.displayName) {
return user.displayName;
}
try {
const result = await dataSources.pg.getUserDisplayNameForLeague(user.id, leagueID);
return result.display_name;
} catch (err) {
console.log(err.stack);
}
},
}
};
async function registerPick(pickRequest, pg) {
const { leagueID } = pickRequest;
try {
const result = await pg.getLeagueById(leagueID);
if (!result) {
// Fantasy league not found
return false;
} else if (result.game_mode === 'PICK_TWO') {
return registerPickTwoPick(pickRequest, pg);
} else {
// Unrecognized game mode
return false;
}
} catch (err) {
console.log(err.stack);
return false;
}
}
async function registerPickTwoPick(pickRequest, pg) {
const { userID, leagueID, teamIDs, week } = pickRequest;
try {
const result = await pg.submitPicks(userID, leagueID, teamIDs, week);
return result.map(function(row) {
return pickFromRow(row);
});
} catch (err) {
console.log(err.stack);
}
}
async function validatePick(pickRequest, pg, context) {
const { leagueID } = pickRequest;
try {
const result = await pg.getLeagueById(leagueID);
if (!result) {
return false;
} else if (result.game_mode === 'PICK_TWO') {
return await validatePickTwoPick(pickRequest, pg, context);
} else {
return false;
}
} catch (err) {
console.log(err.stack);
return false;
}
}
async function validatePickTwoPick(pickRequest, pg, context) {
const { userID, leagueID, teamIDs, week } = pickRequest;
// Need exactly two teams
if (teamIDs.length !== 2) {
context.errorMessage = 'Must select exactly two teams';
return false;
}
// Can't double-pick a team (unless BYE)
if (teamIDs[0] === teamIDs[1] && teamIDs[0] !== BYE) {
context.errorMessage = 'Must select two different teams (unless BYE)';
return false;
}
// Can't pick BYE plus an actual team
if (teamIDs[0] !== teamIDs[1] && teamIDs.includes(BYE)) {
context.errorMessage = "Can't select one team and BYE";
return false;
}
// Get all games for this week
const weekGames = await pg.getSportsGamesForWeek(process.env.CURRENT_SEASON, week);
const allTeams = await pg.getTeams();
// Make sure both picked teams have games this week
let pickedGames = [];
for (const teamID of teamIDs) {
if (teamID === BYE) break;
const currentTeam = allTeams.find(team => team.id === parseInt(teamID));
const hasGame = weekGames.find(game => (game.away_team_short_name === currentTeam.short_name || game.home_team_short_name === currentTeam.short_name));
if (hasGame) {
pickedGames.push(hasGame);
} else {
context.errorMessage = `Team ${currentTeam.short_name} does not appear to have a game this week! If this is incorrect, please email Stephen to make your pick.`
return false;
}
}
// Make sure the teams aren't playing each other (unless week 18)
if (pickedGames.length) {
if (pickedGames[0].id === pickedGames[1].id && week < FINAL_WEEK) {
context.errorMessage = `Can't select two teams playing each other (except in week ${FINAL_WEEK}).`
return false;
}
}
// Make sure neither game has already started
const now = new Date();
for (const game of pickedGames) {
const gameDate = new Date(game.start_time);
if (gameDate < now) {
context.errorMessage = 'At least one selected game appears to have already started! If this is incorrect, please email Stephen to make your pick.'
return false;
}
}
const pastPicks = await pg.getPicksForMember(userID, leagueID);
// Check if any picked team has been picked before
let bye_count = 0;
for (const pick of pastPicks) {
if (teamIDs.includes(pick.team_id.toString()) && pick.week !== week) {
// Check if BYE limit is already reached
if (teamIDs.includes(BYE)){
bye_count += 1;
if (bye_count === BYE_LIMIT) {
context.errorMessage = 'You have already used all byes this season!'
return false;
}
} else {
// At least one of these teams has already been picked by this player
context.errorMessage = 'You have already picked at least one of these teams! If this is incorrect, please email Stephen to make your pick.'
return false;
}
}
}
// Check if this player previously picked a team
// this week that already started their game
const thisWeekPicks = pastPicks.filter(pick => (pick.week === week));
if (thisWeekPicks) {
let pickedTeams = [];
for (const pick of thisWeekPicks) {
pickedTeams.push(allTeams.find(team => team.id === pick.team_id));
}
// Get the two games for the two teams from this
// player's previous submission
for (const pickedTeam of pickedTeams) {
// This player's previous pick was to take a bye
if (!pickedTeam) {
break;
}
const pickedGame = weekGames.find(game => (game.away_team_short_name === pickedTeam.short_name || game.home_team_short_name === pickedTeam.short_name));
if (pickedGame) {
const gameDate = new Date(pickedGame.start_time);
if (gameDate < now) {
context.errorMessage = 'At least one team from your PREVIOUS submission has already started their game. If this is incorrect, please email Stephen to make your pick.'
return false;
}
}
}
}
// Looks like a valid pick
return true;
}
function pickFromRow(row) {
return {
id: row.id,
week: row.week,
isInvalidated: !(row.invalidated_at === null),
// Not schema fields, but used by subresolvers
userID: row.user_id,
leagueID: row.league_id,
teamID: row.team_id
}
}
function userFromRow(row) {
return {
id: row.user_id,
email: row.email,
displayName: row.display_name,
}
}
function leagueFromRow(row) {
return {
// Sometimes the row is a MEMBERSHIP JOIN,
// not a LEAGUE
id: (row.league_id ? row.league_id : row.id),
name: row.name,
gameMode: row.game_mode,
season: row.season,
currentWeek: parseInt(process.env.CURRENT_WEEK) || 1,
revealedWeek: parseInt(process.env.REVEALED_WEEK) || 0,
// Not schema fields, but used by subresolvers
ownerID: row.owner_id
};
}
function teamFromRow(row) {
return {
id: row.id,
name: row.name,
shortName: row.short_name,
sportsLeague: row.sports_league
};
}
function gamesFromRows(rows, allTeams) {
let games = [];
for (const row of rows) {
const awayTeam = allTeams.find(team => row.away_team_short_name === team.short_name);
const homeTeam = allTeams.find(team => row.home_team_short_name === team.short_name);
games.push({
id: row.id,
sportsLeague: row.sports_league,
startsAt: row.start_time,
week: row.week,
awayTeam: teamFromRow(awayTeam),
homeTeam: teamFromRow(homeTeam),
// Not schema fields, but used by subresolvers
awayTeamScore: row.away_team_score,
homeTeamScore: row.home_team_score
});
}
return games;
}
exports.resolvers = resolvers;