From 76ebf01b8ccd0b65df750ee225d5d4cd8cbff88a Mon Sep 17 00:00:00 2001 From: Endi Merkuri Date: Sun, 2 Jun 2024 08:50:36 +0200 Subject: [PATCH] fix book --- fly.toml | 2 +- src/ports/ports.service.ts | 20 ++++++++++++++- src/stations/dto/book-port.dto.ts | 16 ++++++++++++ src/stations/stations.controller.ts | 39 +++++++++++------------------ 4 files changed, 51 insertions(+), 26 deletions(-) create mode 100644 src/stations/dto/book-port.dto.ts diff --git a/fly.toml b/fly.toml index 32083ff..ad02aa0 100644 --- a/fly.toml +++ b/fly.toml @@ -22,4 +22,4 @@ primary_region = 'otp' cpus = 1 [env] - JWT_EXPIRES_IN = '60s' + JWT_EXPIRES_IN = '60000s' diff --git a/src/ports/ports.service.ts b/src/ports/ports.service.ts index d42743e..4f23530 100644 --- a/src/ports/ports.service.ts +++ b/src/ports/ports.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { Port } from './port.entity'; +import { Port, PortStatus, PortType } from './port.entity'; import { Repository } from 'typeorm'; import { CreatePortDto } from 'src/merchants/dto/create-port.dto'; @@ -15,6 +15,24 @@ export class PortsService { return this.portRepository.findOne({ where: { id } }); } + async findByTypeAndStationId( + type: PortType, + stationId: string, + ): Promise { + return this.portRepository.findOne({ + where: { type, stationId, status: PortStatus.FREE }, + }); + } + + async findByOccupiedByIdAndStationId( + occupiedBy: string, + stationId: string, + ): Promise { + return this.portRepository.findOne({ + where: { occupiedBy, stationId, status: PortStatus.OCCUPIED }, + }); + } + async findByIdAndStationId(id: string, stationId: string): Promise { return this.portRepository.findOne({ where: { id, stationId } }); } diff --git a/src/stations/dto/book-port.dto.ts b/src/stations/dto/book-port.dto.ts new file mode 100644 index 0000000..4b20791 --- /dev/null +++ b/src/stations/dto/book-port.dto.ts @@ -0,0 +1,16 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { Transform } from 'class-transformer'; +import { IsEnum, IsNotEmpty } from 'class-validator'; + +enum PortType { + FAST = 'fast', + NORMAL = 'normal', +} + +export class BookPortDto { + @ApiProperty() + @IsEnum(PortType) + @IsNotEmpty() + @Transform(({ value }) => ('' + value).toLowerCase()) + type: PortType; +} diff --git a/src/stations/stations.controller.ts b/src/stations/stations.controller.ts index a33e7f8..f47c22c 100644 --- a/src/stations/stations.controller.ts +++ b/src/stations/stations.controller.ts @@ -1,5 +1,4 @@ import { - ConflictException, Request, Controller, Get, @@ -8,6 +7,7 @@ import { Post, UseGuards, Query, + Body, } from '@nestjs/common'; import { StationsService } from './stations.service'; import { normalizeResponse } from 'src/util/helpers/response.helpers'; @@ -20,6 +20,7 @@ import { StationStatus } from './station.entity'; import { PortsService } from 'src/ports/ports.service'; import { CardsService } from 'src/cards/cards.service'; import { StationQueryDto } from './dto/station-query.dto'; +import { BookPortDto } from './dto/book-port.dto'; @Controller('stations') export class StationsController { @@ -58,28 +59,25 @@ export class StationsController { return normalizeResponse({ stations, _message: 'success' }); } - @Post(':id/ports/:portId/book') + @Post(':id/book') @Roles(UserType.CLIENT) @UseGuards(JwtAuthGuard, RolesGuard) async bookPort( @Request() req, @Param('id') id: string, - @Param('portId') portId: string, + @Body() payload: BookPortDto, ) { const { user: { user }, } = req; const { id: userId, card } = user; - let port = await this.portsService.findByIdAndStationId(portId, id); + const { type } = payload; + let port = await this.portsService.findByTypeAndStationId(type, id); if (!port) { throw new NotFoundException('Port not found'); } - if (port.status === PortStatus.OCCUPIED) { - throw new ConflictException('Port is already occupied'); - } - port.occupiedBy = userId; port = await this.portsService.update(port, { status: PortStatus.OCCUPIED, @@ -89,29 +87,22 @@ export class StationsController { return normalizeResponse({ port, _message: 'success' }); } - @Post(':id/ports/:portId/unbook') + @Post(':id/unbook') @Roles(UserType.CLIENT) @UseGuards(JwtAuthGuard, RolesGuard) - async unbookPort( - @Request() req, - @Param('id') id: string, - @Param('portId') portId: string, - ) { - const { user } = req; - let port = await this.portsService.findByIdAndStationId(portId, id); + async unbookPort(@Request() req, @Param('id') id: string) { + const { + user: { user }, + } = req; + let port = await this.portsService.findByOccupiedByIdAndStationId( + user.id, + id, + ); if (!port) { throw new NotFoundException('Port not found'); } - if (port.status === PortStatus.FREE) { - throw new ConflictException('Port is already free'); - } - - if (port.occupiedBy !== user.user.id) { - throw new ConflictException('Port is not occupied by you'); - } - port.occupiedBy = null; port = await this.portsService.update(port, { status: PortStatus.FREE,