-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEventController.ts
131 lines (118 loc) · 5.26 KB
/
EventController.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
import {
JsonController, Get, Patch, Delete, Post, UseBefore, Params, ForbiddenError, QueryParams, UploadedFile, Body,
} from 'routing-controllers';
import { Service } from 'typedi';
import EventService from '../../services/EventService';
import { UserAuthentication, OptionalUserAuthentication } from '../middleware/UserAuthentication';
import { AuthenticatedUser } from '../decorators/AuthenticatedUser';
import { UserModel } from '../../models/UserModel';
import PermissionsService from '../../services/PermissionsService';
import StorageService from '../../services/StorageService';
import AttendanceService from '../../services/AttendanceService';
import {
MediaType,
File,
CreateEventResponse,
PatchEventResponse,
DeleteEventResponse,
GetOneEventResponse,
UpdateEventCoverResponse,
GetFutureEventsResponse,
GetAllEventsResponse,
GetPastEventsResponse,
} from '../../types';
import { UuidParam } from '../validators/GenericRequests';
import {
EventSearchOptions,
PatchEventRequest,
CreateEventRequest,
SubmitEventFeedbackRequest,
} from '../validators/EventControllerRequests';
@Service()
@JsonController('/event')
export class EventController {
private eventService: EventService;
private storageService: StorageService;
private attendanceService: AttendanceService;
constructor(eventService: EventService, storageService: StorageService, attendanceService: AttendanceService) {
this.eventService = eventService;
this.storageService = storageService;
this.attendanceService = attendanceService;
}
@UseBefore(OptionalUserAuthentication)
@Get('/past')
async getPastEvents(@QueryParams() options: EventSearchOptions,
@AuthenticatedUser() user: UserModel): Promise<GetPastEventsResponse> {
const canSeeAttendanceCode = !!user && PermissionsService.canEditEvents(user);
const events = await this.eventService.getPastEvents(canSeeAttendanceCode, options);
return { error: null, events };
}
@UseBefore(OptionalUserAuthentication)
@Get('/future')
async getFutureEvents(@QueryParams() options: EventSearchOptions,
@AuthenticatedUser() user: UserModel): Promise<GetFutureEventsResponse> {
const canSeeAttendanceCode = !!user && PermissionsService.canEditEvents(user);
const events = await this.eventService.getFutureEvents(canSeeAttendanceCode, options);
return { error: null, events };
}
@UseBefore(UserAuthentication)
@Post('/picture/:uuid')
async updateEventCover(@UploadedFile('image',
{ options: StorageService.getFileOptions(MediaType.EVENT_COVER) }) file: File,
@Params() params: UuidParam,
@AuthenticatedUser() user: UserModel): Promise<UpdateEventCoverResponse> {
if (!PermissionsService.canEditEvents(user)) throw new ForbiddenError();
const cover = await this.storageService.upload(file, MediaType.EVENT_COVER, params.uuid);
const event = await this.eventService.updateByUuid(params.uuid, { cover });
return { error: null, event };
}
@UseBefore(UserAuthentication)
@Post('/:uuid/feedback')
async submitEventFeedback(@Params() params: UuidParam, @Body() submitEventFeedbackRequest: SubmitEventFeedbackRequest,
@AuthenticatedUser() user: UserModel) {
if (!PermissionsService.canSubmitFeedback(user)) throw new ForbiddenError();
await this.attendanceService.submitEventFeedback(submitEventFeedbackRequest.feedback, params.uuid, user);
return { error: null };
}
@UseBefore(OptionalUserAuthentication)
@Get('/:uuid')
async getOneEvent(@Params() params: UuidParam,
@AuthenticatedUser() user: UserModel): Promise<GetOneEventResponse> {
const canSeeAttendanceCode = !!user && PermissionsService.canEditEvents(user);
const event = await this.eventService.findByUuid(params.uuid, canSeeAttendanceCode);
return { error: null, event };
}
@UseBefore(UserAuthentication)
@Patch('/:uuid')
async updateEvent(@Params() params: UuidParam,
@Body() patchEventRequest: PatchEventRequest,
@AuthenticatedUser() user: UserModel): Promise<PatchEventResponse> {
if (!PermissionsService.canEditEvents(user)) throw new ForbiddenError();
const event = await this.eventService.updateByUuid(params.uuid, patchEventRequest.event);
return { error: null, event };
}
@UseBefore(UserAuthentication)
@Delete('/:uuid')
async deleteEvent(@Params() params: UuidParam,
@AuthenticatedUser() user: UserModel): Promise<DeleteEventResponse> {
if (!PermissionsService.canEditEvents(user)) throw new ForbiddenError();
await this.eventService.deleteByUuid(params.uuid);
return { error: null };
}
@UseBefore(OptionalUserAuthentication)
@Get()
async getAllEvents(@QueryParams() options: EventSearchOptions, @AuthenticatedUser() user: UserModel):
Promise<GetAllEventsResponse> {
const canSeeAttendanceCode = !!user && PermissionsService.canEditEvents(user);
const events = await this.eventService.getAllEvents(canSeeAttendanceCode, options);
return { error: null, events };
}
@UseBefore(UserAuthentication)
@Post()
async createEvent(@Body() createEventRequest: CreateEventRequest,
@AuthenticatedUser() user: UserModel): Promise<CreateEventResponse> {
if (!PermissionsService.canEditEvents(user)) throw new ForbiddenError();
const event = await this.eventService.create(createEventRequest.event);
return { error: null, event };
}
}