forked from PCreations/dixitonline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (88 loc) · 3.02 KB
/
index.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
import { makeSchema, queryType, mutationType } from 'nexus';
import express from 'express';
import cors from 'cors';
import { ApolloServer, AuthenticationError } from 'apollo-server-express';
import { initialize as initializeDecks } from '@dixit/decks';
import { GameTypes, initialize as initializeGame } from '@dixit/game';
import { makeGameRepository } from '@dixit/game/src/repos/game-repository';
import { makeRemoveInactivePlayers as makeRemoveInactivePlayersUseCase } from '@dixit/game/src/useCases/remove-inactive-players';
import { TurnTypes, initialize as initializeTurn, turnReducer } from '@dixit/turn';
import { makeGraphqlExpressAuthorizationService } from '@dixit/users';
import { SentryPlugin } from './sentry-plugin';
const Query = queryType({
definition() {},
});
const Mutation = mutationType({
definition() {},
});
export default ({ firestore, firebaseAuth, dispatchDomainEvents, subscribeToDomainEvent }) => {
const gameRepository = makeGameRepository({ firestore });
const removeInactivePlayers = makeRemoveInactivePlayersUseCase({ gameRepository });
const authorizationService = makeGraphqlExpressAuthorizationService({ firebaseAuth });
initializeDecks({ firestore, dispatchDomainEvents, subscribeToDomainEvent });
const { getContext: getGameContext, getDataSources: getGameDataSources } = initializeGame({
firestore,
dispatchDomainEvents,
subscribeToDomainEvent,
authorizationService,
});
const { getContext: getTurnContext, getDataSources: getTurnDataSources } = initializeTurn({
firestore,
firebaseAuth,
authorizationService,
dispatchDomainEvents,
subscribeToDomainEvent,
});
const schema = makeSchema({
types: { Query, Mutation, ...GameTypes, ...TurnTypes },
});
const server = new ApolloServer({
schema,
plugins: [SentryPlugin],
dataSources: () => ({
...getGameDataSources(),
...getTurnDataSources(),
}),
context: async (...args) => {
const [context] = args;
if (context.req?.body.operationName !== 'IntrospectionQuery') {
try {
return {
...(await getGameContext(...args)),
...(await getTurnContext(...args)),
};
} catch (e) {
if (e.message === 'unauthorized') {
throw new AuthenticationError();
}
}
}
},
});
const app = express();
app.use(async (_, __, next) => {
try {
await firestore
.collection('lobby-games')
.where('status', '==', 'WAITING_FOR_PLAYERS')
.where('isPrivate', '==', false)
.get()
.then(snp => {
const ids = [];
snp.forEach(doc => ids.push(doc.data().id));
console.log(`Handling ${ids.length} games`, ids);
return Promise.all(ids.map(id => removeInactivePlayers({ gameId: id, now: new Date() })));
});
} catch (err) {
console.error(err);
}
next();
});
app.use(cors({ origin: true }));
server.applyMiddleware({ app });
return {
turnReducer,
app,
removeInactivePlayers,
};
};