Skip to content

Commit

Permalink
fix book
Browse files Browse the repository at this point in the history
  • Loading branch information
endimerkuri committed Jun 2, 2024
1 parent 7498f58 commit 76ebf01
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 26 deletions.
2 changes: 1 addition & 1 deletion fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ primary_region = 'otp'
cpus = 1

[env]
JWT_EXPIRES_IN = '60s'
JWT_EXPIRES_IN = '60000s'
20 changes: 19 additions & 1 deletion src/ports/ports.service.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -15,6 +15,24 @@ export class PortsService {
return this.portRepository.findOne({ where: { id } });
}

async findByTypeAndStationId(
type: PortType,
stationId: string,
): Promise<Port> {
return this.portRepository.findOne({
where: { type, stationId, status: PortStatus.FREE },
});
}

async findByOccupiedByIdAndStationId(
occupiedBy: string,
stationId: string,
): Promise<Port> {
return this.portRepository.findOne({
where: { occupiedBy, stationId, status: PortStatus.OCCUPIED },
});
}

async findByIdAndStationId(id: string, stationId: string): Promise<Port> {
return this.portRepository.findOne({ where: { id, stationId } });
}
Expand Down
16 changes: 16 additions & 0 deletions src/stations/dto/book-port.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
39 changes: 15 additions & 24 deletions src/stations/stations.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
ConflictException,
Request,
Controller,
Get,
Expand All @@ -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';
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 76ebf01

Please sign in to comment.