-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.ts
159 lines (135 loc) · 4.42 KB
/
events.ts
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
import { and, eq, getTableColumns, gte, lte, or, sql } from 'drizzle-orm';
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
import type { Request, Response } from 'express';
import * as schema from '../db/schema';
import { logger } from '../utils/logger';
export function getEventCyclesHandler(dbPool: NodePgDatabase<typeof schema>) {
return async function (req: Request, res: Response) {
const { eventId } = req.params;
if (!eventId) {
return res.status(400).json({ error: 'Missing eventId' });
}
const eventCycles = await dbPool.query.cycles.findMany({
where: eq(schema.cycles.eventId, eventId),
with: {
questions: {
with: {
options: {
columns: {
voteScore: false,
},
where: eq(schema.options.show, true),
},
},
},
},
});
return res.json({ data: eventCycles });
};
}
export function getEventGroupCategoriesHandler(dbPool: NodePgDatabase<typeof schema>) {
return async function (req: Request, res: Response) {
const { eventId } = req.params;
if (!eventId) {
return res.status(400).json({ error: 'Missing eventId' });
}
const eventGroupCategories = await dbPool.query.groupCategories.findMany({
where: eq(schema.groupCategories.eventId, eventId),
});
return res.json({ data: eventGroupCategories });
};
}
export function getEventsHandler(dbPool: NodePgDatabase<typeof schema>) {
return async function (req: Request, res: Response) {
const columns = getTableColumns(schema.events);
const events = await dbPool
.select({
...columns,
status: sql`CASE
WHEN EXISTS (
SELECT 1
FROM ${schema.cycles}
WHERE ${schema.cycles.eventId} = events.id
AND ${schema.cycles.status} = 'OPEN'
) THEN 'OPEN'
WHEN EXISTS (
SELECT 1
FROM ${schema.cycles}
WHERE ${schema.cycles.eventId} = events.id
AND ${schema.cycles.status} = 'UPCOMING'
) THEN 'UPCOMING'
ELSE 'CLOSED'
END`,
})
.from(schema.events);
return res.json({ data: events });
};
}
export function getEventHandler(dbPool: NodePgDatabase<typeof schema>) {
return async function (req: Request, res: Response) {
const { eventId } = req.params;
if (!eventId) {
return res.status(400).json({ error: 'Missing eventId' });
}
const event = await dbPool.query.events.findFirst({
where: eq(schema.events.id, eventId),
});
return res.json({ data: event });
};
}
export function getEventRegistrationFieldsHandler(dbPool: NodePgDatabase<typeof schema>) {
return async function (req: Request, res: Response) {
const eventId = req.params.eventId;
if (!eventId) {
return res.status(400).json({ errors: ['eventId is required'] });
}
const event = await dbPool.query.events.findFirst({
with: {
registrationFields: {
with: {
registrationFieldOptions: true,
},
},
},
where: eq(schema.events.id, eventId),
});
return res.json({ data: event?.registrationFields });
};
}
export function getEventRegistrationsHandler(dbPool: NodePgDatabase<typeof schema>) {
return async function (req: Request, res: Response) {
// parse input
const eventId = req.params.eventId ?? '';
const userId = req.session.userId;
try {
const out = await dbPool.query.registrations.findMany({
where: and(
eq(schema.registrations.userId, userId),
eq(schema.registrations.eventId, eventId),
),
});
return res.json({ data: out });
} catch (e) {
logger.error('error getting registration ' + e);
return res.sendStatus(500);
}
};
}
export function getEventNavLinksHandler(dbPool: NodePgDatabase<typeof schema>) {
return async function (req: Request, res: Response) {
const { eventId } = req.params;
if (!eventId) {
return res.status(400).json({ error: 'Missing eventId' });
}
const eventNavLinks = await dbPool.query.navLinks.findMany({
where: and(
eq(schema.navLinks.eventId, eventId),
or(
eq(schema.navLinks.active, true),
and(lte(schema.navLinks.startAt, new Date()), gte(schema.navLinks.endAt, new Date())),
),
),
});
return res.json({ data: eventNavLinks });
};
}