-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathevent.controller.ts
271 lines (260 loc) · 8.2 KB
/
event.controller.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
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
import {
ActivationLogDto,
EventPlaceCodeDto,
} from './dto/event-place-code.dto';
import { EventService } from './event.service';
import {
Body,
Controller,
Get,
HttpException,
HttpStatus,
Param,
Post,
Query,
Res,
UploadedFile,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import {
ApiBearerAuth,
ApiBody,
ApiConsumes,
ApiOperation,
ApiParam,
ApiQuery,
ApiResponse,
ApiTags,
} from '@nestjs/swagger';
import { RolesGuard } from '../../roles.guard';
import { UploadTriggerPerLeadTimeDto } from './dto/upload-trigger-per-leadtime.dto';
import { EventSummaryCountry, TriggeredArea } from '../../shared/data.model';
import { DateDto, TriggerPerLeadTimeExampleDto } from './dto/date.dto';
import { Roles } from '../../roles.decorator';
import { UserRole } from '../user/user-role.enum';
import { UserDecorator } from '../user/user.decorator';
import { FileInterceptor } from '@nestjs/platform-express';
import stream from 'stream';
import { Response } from 'express-serve-static-core';
import { IMAGE_UPLOAD_API_FORMAT } from '../../shared/file-upload-api-format';
@ApiBearerAuth()
@ApiTags('event')
@Controller('event')
export class EventController {
private readonly eventService: EventService;
public constructor(eventService: EventService) {
this.eventService = eventService;
}
@UseGuards(RolesGuard)
@ApiOperation({
summary:
'Get summary of active events - if any - for given country and disaster-type',
})
@ApiParam({ name: 'countryCodeISO3', required: true, type: 'string' })
@ApiParam({ name: 'disasterType', required: true, type: 'string' })
@ApiResponse({
status: 200,
description:
'Summary of active events - if any - for given country and disaster-type.',
type: EventSummaryCountry,
})
@Get(':countryCodeISO3/:disasterType')
public async getEventSummaryCountry(
@Param() params,
): Promise<EventSummaryCountry[]> {
return await this.eventService.getEventSummary(
params.countryCodeISO3,
params.disasterType,
);
}
@UseGuards(RolesGuard)
@ApiOperation({
summary:
'Get date of last forecast-data-upload for given country and disaster-type.',
})
@ApiParam({ name: 'countryCodeISO3', required: true, type: 'string' })
@ApiParam({ name: 'disasterType', required: true, type: 'string' })
@ApiResponse({
status: 200,
description:
'Date of last forecast-data-upload for given country and disaster-type.',
type: DateDto,
})
@Get('recent-date/:countryCodeISO3/:disasterType')
public async getRecentDate(@Param() params): Promise<DateDto> {
return await this.eventService.getRecentDate(
params.countryCodeISO3,
params.disasterType,
);
}
@UseGuards(RolesGuard)
@ApiOperation({
summary:
'Get yes/no trigger per lead-time for given country and disaster-type.',
})
@ApiParam({ name: 'countryCodeISO3', required: true, type: 'string' })
@ApiParam({ name: 'disasterType', required: true, type: 'string' })
@ApiQuery({ name: 'eventName', required: false, type: 'string' })
@ApiResponse({
status: 200,
description:
'Yes/no trigger per lead-time for given country and disaster-type.',
type: TriggerPerLeadTimeExampleDto,
})
@Get('triggers/:countryCodeISO3/:disasterType')
public async getTriggerPerLeadtime(
@Param() params,
@Query() query,
): Promise<object> {
return await this.eventService.getTriggerPerLeadtime(
params.countryCodeISO3,
params.disasterType,
query.eventName,
);
}
@UseGuards(RolesGuard)
@ApiOperation({
summary:
'Get triggered admin-areas for given country, disaster-type and lead-time.',
})
@ApiParam({ name: 'countryCodeISO3', required: true, type: 'string' })
@ApiParam({ name: 'disasterType', required: true, type: 'string' })
@ApiParam({ name: 'adminLevel', required: true, type: 'number' })
@ApiQuery({ name: 'leadTime', required: false, type: 'string' })
@ApiQuery({ name: 'eventName', required: false, type: 'string' })
@ApiResponse({
status: 200,
description:
'Triggered admin-areas for given country, disaster-type and lead-time.',
type: [TriggeredArea],
})
@Get('triggered-areas/:countryCodeISO3/:adminLevel/:disasterType')
public async getTriggeredAreas(
@Param() params,
@Query() query,
): Promise<TriggeredArea[]> {
return await this.eventService.getTriggeredAreas(
params.countryCodeISO3,
params.disasterType,
params.adminLevel,
query.leadTime,
query.eventName,
);
}
@UseGuards(RolesGuard)
@ApiOperation({
summary:
'Get past and current trigger activation data per admin-area and disaster-type.',
})
@ApiResponse({
status: 200,
description:
'Past and current trigger activation data per admin-area and disaster-type.',
type: [ActivationLogDto],
})
@ApiQuery({ name: 'countryCodeISO3', required: false, type: 'string' })
@ApiQuery({ name: 'disasterType', required: false, type: 'string' })
@Get('activation-log')
public async getActivationLogData(
@Query() query,
): Promise<ActivationLogDto[]> {
return await this.eventService.getActivationLogData(
query.countryCodeISO3,
query.disasterType,
);
}
@UseGuards(RolesGuard)
@Roles(UserRole.DisasterManager)
@ApiOperation({ summary: 'Stop trigger for given admin-area.' })
@ApiResponse({
status: 201,
description: 'Trigger stopped for given admin-area.',
})
@ApiResponse({
status: 404,
description: 'No admin-area for this event.',
})
@Post('toggle-stopped-trigger')
public async toggleStoppedTrigger(
@UserDecorator('userId') userId: string,
@Body() eventPlaceCodeDto: EventPlaceCodeDto,
): Promise<void> {
return await this.eventService.toggleStoppedTrigger(
userId,
eventPlaceCodeDto,
);
}
@UseGuards(RolesGuard)
@Roles(UserRole.PipelineUser)
@ApiOperation({
summary:
'Upload yes/no trigger data per leadtime for given country and disaster-type',
})
@ApiResponse({
status: 201,
description:
'Uploaded yes/no trigger data per leadtime for given country and disaster-type.',
})
@Post('triggers-per-leadtime')
public async uploadTriggersPerLeadTime(
@Body() uploadTriggerPerLeadTimeDto: UploadTriggerPerLeadTimeDto,
): Promise<void> {
await this.eventService.uploadTriggerPerLeadTime(
uploadTriggerPerLeadTimeDto,
);
}
@UseGuards(RolesGuard)
@ApiOperation({
summary: 'Post event map image (Only .png-files supported)',
})
@ApiParam({ name: 'countryCodeISO3', required: true, type: 'string' })
@ApiParam({ name: 'disasterType', required: true, type: 'string' })
@ApiParam({ name: 'eventName', required: false, type: 'string' })
@ApiConsumes('multipart/form-data')
@ApiBody(IMAGE_UPLOAD_API_FORMAT)
@ApiResponse({ status: 200, description: 'Post event map image' })
@Post('/event-map-image/:countryCodeISO3/:disasterType/:eventName')
@UseInterceptors(FileInterceptor('image'))
public async postEventMapImage(
@UploadedFile() imageFileBlob,
@Param() params,
): Promise<void> {
await this.eventService.postEventMapImage(
params.countryCodeISO3,
params.disasterType,
params.eventName,
imageFileBlob,
);
}
@ApiOperation({
summary: 'Get event map image',
})
@ApiParam({ name: 'countryCodeISO3', required: true, type: 'string' })
@ApiParam({ name: 'disasterType', required: true, type: 'string' })
@ApiParam({ name: 'eventName', required: false, type: 'string' })
@ApiResponse({ status: 200, description: 'Get event map image' })
@Get('/event-map-image/:countryCodeISO3/:disasterType/:eventName')
public async getEventMapImage(
@Res() response: Response,
@Param() params,
): Promise<void> {
const blob = await this.eventService.getEventMapImage(
params.countryCodeISO3,
params.disasterType,
params.eventName,
);
if (!blob) {
throw new HttpException(
'Image not found. Please upload an image using POST and try again.',
HttpStatus.NOT_FOUND,
);
}
const bufferStream = new stream.PassThrough();
bufferStream.end(Buffer.from(blob, 'binary'));
response.writeHead(HttpStatus.OK, {
'Content-Type': 'image/png',
});
bufferStream.pipe(response);
}
}