-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.ts
More file actions
136 lines (116 loc) · 3.56 KB
/
event.ts
File metadata and controls
136 lines (116 loc) · 3.56 KB
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
import { asyncHandler, BadRequestError, checkAbility } from "@api/common";
import express from "express";
import { FilterQuery } from "mongoose";
import { EventModel, Event } from "../models/event";
export const eventRoutes = express.Router();
eventRoutes.route("/").get(
checkAbility("read", "Event"),
asyncHandler(async (req, res) => {
const filter: FilterQuery<Event> = {};
if (req.query.hexathon) {
filter.hexathon = String(req.query.hexathon);
}
if (req.query.search) {
const searchLength = (req.query.search as string).length;
const search =
searchLength > 75
? (req.query.search as string).slice(0, 75)
: (req.query.search as string);
filter.$or = [
{ name: { $regex: new RegExp(search, "i") } },
{ type: { $regex: new RegExp(search, "i") } },
{ description: { $regex: new RegExp(search, "i") } },
];
}
const events = await EventModel.accessibleBy(req.ability).find(filter);
return res.send(events);
})
);
eventRoutes.route("/:id").get(
checkAbility("read", "Event"),
asyncHandler(async (req, res) => {
const event = await EventModel.findById(req.params.id).accessibleBy(req.ability);
return res.send(event);
})
);
eventRoutes.route("/").post(
checkAbility("create", "Event"),
asyncHandler(async (req, res) => {
const existingEvent = await EventModel.findOne({
hexathon: req.body.hexathon,
name: req.body.name,
});
if (existingEvent) {
throw new BadRequestError(
`Event with name ${req.body.name} already exists for this hexathon`
);
}
const event: Event = await EventModel.create({
hexathon: req.body.hexathon,
name: req.body.name,
type: req.body.type,
description: req.body.description || " ",
startDate: req.body.startDate,
endDate: req.body.endDate,
location: req.body.location,
tags: req.body.tags,
checkIns: req.body.checkIns || 0,
});
return res.send(event);
})
);
eventRoutes.route("/:id").patch(
checkAbility("update", "Event"),
asyncHandler(async (req, res) => {
const currentEvent = await EventModel.findById(req.params.id);
const existingEvent = await EventModel.findOne({
hexathon: currentEvent?.hexathon,
name: req.body.name,
});
if (existingEvent && existingEvent.id !== req.params.id) {
throw new BadRequestError(
`Event with name ${req.body.name} already exists for this hexathon`
);
}
const event = await EventModel.findByIdAndUpdate(
req.params.id,
{
$set: {
hexathon: req.body.hexathon,
name: req.body.name,
type: req.body.type,
description: req.body.description,
startDate: req.body.startDate,
endDate: req.body.endDate,
location: req.body.location,
tags: req.body.tags,
},
},
{ new: true }
);
res.send(event);
})
);
eventRoutes.route("/:id").delete(
checkAbility("delete", "Event"),
asyncHandler(async (req, res) => {
await EventModel.findByIdAndDelete(req.params.id);
return res.sendStatus(204);
})
);
eventRoutes.route("/add-check-in/:id").patch(
checkAbility("update", "Event"),
asyncHandler(async (req, res) => {
const currentEvent = await EventModel.findById(req.params.id);
const event = await EventModel.findByIdAndUpdate(
req.params.id,
{
$set: {
checkIns: currentEvent?.checkIns ? currentEvent.checkIns + 1 : 1,
},
},
{ new: true }
);
res.send(event);
})
);