-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtripnotify.controller.ts
More file actions
122 lines (108 loc) · 4.33 KB
/
tripnotify.controller.ts
File metadata and controls
122 lines (108 loc) · 4.33 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
import { BadRequestException, Body, Controller, Delete, ForbiddenException, Get, InternalServerErrorException, NotFoundException, Param, Post, UseGuards } from "@nestjs/common";
import { ApiSecurity, ApiTags } from "@nestjs/swagger";
import { TripNotifyService } from "./tripnotify.service";
import { TripDto } from "./dto/trip.dto";
import { AuthGuard } from "src/account/account.guard";
import { Guest } from "src/account/decorators/guest.decorator";
import { CreateTripDto } from "./dto/create-trip.dto";
import { TripNotExistsException } from "./exceptions/trip-not-exists.exception";
import { TripCreationException } from "./exceptions/trip-creation.exception";
import { LogsService } from "src/logs/logs.service";
import { HISTORY_TYPE } from "src/logs/entities/history-type.entity";
import { STATUS } from "src/logs/entities/status.entity";
import { LogWithHistoryDto } from "./dto/log-with-history.dto";
import { Premium } from "src/premium/decorators/premium.decorator";
import { PremiumGuard } from "src/premium/premium.guard";
import { TripNotStartedException } from "./exceptions/trip-not-started.exception";
import { TripMissingPremiumException } from "./exceptions/trip-missing-premium.exception";
import { TripInformationDto } from "./dto/trip-information.dto";
@Controller('trips')
@UseGuards(AuthGuard)
@UseGuards(PremiumGuard)
@ApiSecurity('custom-auth')
@ApiTags('TripNotify')
export class TripNotifyController {
constructor(
private readonly logsService: LogsService,
private readonly tripNotifyService: TripNotifyService,
) {}
@Get(':akey/list')
async getActiveTripsByAKey(
@Param('akey') akey: string,
): Promise<TripDto[]> {
try {
return (await this.tripNotifyService.findAccessibleByAkey(akey)).map((trip) => new TripDto(trip));
} catch(_) {
throw new InternalServerErrorException();
}
}
@Get(':code')
@Guest()
async getActiveTripByCode(
@Param('code') code: string,
): Promise<TripDto> {
try {
return new TripDto(await this.tripNotifyService.findAccessibleByCode(code));
} catch (error) {
if (error instanceof TripNotExistsException) {
throw new NotFoundException(error.message);
} else if (error instanceof TripNotStartedException || error instanceof TripMissingPremiumException) {
throw new ForbiddenException(error.message);
}
throw new InternalServerErrorException();
}
}
@Post(':akey')
@Premium()
async createTrip(
@Param('akey') akey: string,
@Body() createTripDto: CreateTripDto,
): Promise<TripDto> {
try {
return new TripDto(await this.tripNotifyService.create(akey, createTripDto));
} catch (error) {
if (error instanceof TripCreationException) {
throw new BadRequestException(error.message);
}
throw new InternalServerErrorException();
}
}
@Get(':code/info')
@Guest()
async tripInformation(
@Param('code') code: string,
): Promise<TripInformationDto> {
try {
const trip = await this.tripNotifyService.findAccessibleByCode(code);
const logs = await this.logsService.findLogsWithinDateRange(trip.akey, trip.startDate, trip.endDate);
for(const log of logs) {
log.history = await this.logsService.findOneWithHistory(log.akey, log._id.toString(), trip.locationEnabled ? HISTORY_TYPE.ALL : HISTORY_TYPE.BATTERY_DATA);
if (log.startDate < trip.startDate || log.endDate > trip.endDate || log.status === STATUS.RUNNING) {
log.history = log.history.filter((entry) => new Date(entry.timestamp) <= trip.endDate);
}
}
return new TripInformationDto(logs.map((log) => new LogWithHistoryDto(log)));
} catch (error) {
if (error instanceof TripNotExistsException) {
throw new NotFoundException(error.message);
} else if (error instanceof TripNotStartedException || error instanceof TripMissingPremiumException) {
throw new ForbiddenException(error.message);
}
throw new InternalServerErrorException();
}
}
@Delete(':akey/:code')
async deleteTrip(
@Param('akey') akey: string,
@Param('code') code: string,
) {
try {
return await this.tripNotifyService.deleteTrip(akey, code);
} catch(error) {
if (error instanceof TripNotExistsException) {
throw new NotFoundException(error.message);
}
throw new InternalServerErrorException();
}
}
}