From 5b2b2036e20587d201eaa7d106409095041db1aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= <80365304+Diego-lvan@users.noreply.github.com> Date: Tue, 12 Nov 2024 21:53:01 -0600 Subject: [PATCH] generar custom ruta --- backend/src/route/dto/create-route-req.ts | 10 ++++ backend/src/route/route.controller.ts | 22 ++++++++- backend/src/route/route.service.ts | 56 +++++++++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/backend/src/route/dto/create-route-req.ts b/backend/src/route/dto/create-route-req.ts index 8e515934..a8a2c405 100644 --- a/backend/src/route/dto/create-route-req.ts +++ b/backend/src/route/dto/create-route-req.ts @@ -7,3 +7,13 @@ export class CreateRouteReq { @ApiProperty({ name: 'end', type: Number }) end: number; } + +export class CreateCustomRouteReq { + @ApiProperty({ name: 'placesIds', type: [Number] }) + placesIds: number[]; + @ApiProperty({ name: 'start', type: Number }) + start: number; + + @ApiProperty({ name: 'end', type: Number }) + end: number; +} diff --git a/backend/src/route/route.controller.ts b/backend/src/route/route.controller.ts index aedd78d9..6a602c3c 100644 --- a/backend/src/route/route.controller.ts +++ b/backend/src/route/route.controller.ts @@ -1,10 +1,10 @@ import { Controller, Param, UseGuards, Req, Query, Body, Post, Get, Patch } from '@nestjs/common'; import { RouteService } from './route.service'; -import { ApiBearerAuth, ApiBody, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger'; +import { ApiBearerAuth, ApiBody, ApiOperation, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger'; import { LANGUAGES } from 'src/shared/enum/languages.enum'; import { AuthUserGuard } from 'src/auth/user/authUser.guard'; import { CustomUserRequest } from 'src/auth/user/interface/customUserReq'; -import { CreateRouteReq } from './dto/create-route-req'; +import { CreateCustomRouteReq, CreateRouteReq } from './dto/create-route-req'; import { RouteStatus } from './entities/route.entity'; import { UpdateRouteStatusDto } from './dto/updateRouteStatus.dto'; @@ -82,4 +82,22 @@ export class RouteController { async updateRoute(@Body() updateRouteStatusDto: UpdateRouteStatusDto, @Param('idRoute') idRoute: number) { return await this.routeService.updateRoute(idRoute, updateRouteStatusDto.status); } + + @Post('custom/:idTown/:lang') + @ApiOperation({ summary: 'Create a custom route' }) + @ApiBearerAuth('jwt') + @UseGuards(AuthUserGuard) + async createCustomRoute( + @Req() req: CustomUserRequest, + @Body() createRouteReq: CreateCustomRouteReq, + @Param('lang') lang: LANGUAGES, + ) { + try { + const { email } = req.user; + return await this.routeService.createCustomRoute(email, lang as LANGUAGES, createRouteReq); + } catch (error) { + console.log(error); + return error; + } + } } diff --git a/backend/src/route/route.service.ts b/backend/src/route/route.service.ts index 38764976..b1264a4c 100644 --- a/backend/src/route/route.service.ts +++ b/backend/src/route/route.service.ts @@ -17,6 +17,7 @@ import { CreateTravelPlaceDto } from 'src/travel-place/dto/create-travel-place.d import { UserService } from 'src/user/user.service'; import { TravelPlace } from 'src/travel-place/entities/travel-place.entity'; import { ServerConstants } from 'src/constants/server.contants'; +import { CreateCustomRouteReq } from './dto/create-route-req'; @Injectable() export class RouteService { @@ -179,4 +180,59 @@ export class RouteService { return res; } + + async createCustomRoute(email: string, lang: LANGUAGES, createRouteReq: CreateCustomRouteReq) { + const { start, end, placesIds } = createRouteReq; + if (placesIds.length === 0) return []; + const place = await this.placeService.findOneAndTradAndAvailable(placesIds[0], lang); + const user = await this.userService.findOne(email); + + const town: Town = await this.townRepository.findOneBy({ townId: place.idTown }); + + const places = []; + for (const idPlace of placesIds) { + const place = await this.placeService.findOneAndTradAndAvailable(idPlace, lang); + places.push(place); + } + const placesNotVisited: RecommendPlace[] = places.map((place: GetPlaceDto) => { + return { + idPlace: place.idPlace, + openAt: place.openAt, + closeAt: place.closeAt, + categories: place.categories.map((category) => category.idCategory), + rating: 0, + }; + }); + + const system = new RecommendationsSystem([], placesNotVisited, start, end); + const chosen: RecommendPlace[] = system.recommend(); + const startDate: Date = new Date(); + startDate.setHours(start); + const endDate = new Date(); + endDate.setHours(end); + const createRouteDto: CreateRouteDto = { startDate, endDate, town, user }; + const idRoute = (await this.routeRepository.save(createRouteDto)).idRoute; + + for (const curRecommended of chosen) { + const place = await this.placeService.findOneAndTradAndAvailable(curRecommended.idPlace, lang); + const endDate = new Date(); + endDate.setHours(curRecommended.closeAt); + endDate.setMinutes(0); + const startDate = new Date(); + startDate.setHours(curRecommended.openAt); + startDate.setMinutes(0); + + const createTravelPlace: CreateTravelPlaceDto = { + ...place, + done: false, + idRoute, + endDate, + startDate, + }; + + await this.travelPlaceService.create(createTravelPlace); + } + + return await this.getRouteById(idRoute, email, lang); + } } -- GitLab