diff --git a/src/order/dto/order-delivery.dto.ts b/src/order/dto/order-delivery.dto.ts index c0156e5..365dc67 100644 --- a/src/order/dto/order-delivery.dto.ts +++ b/src/order/dto/order-delivery.dto.ts @@ -3,7 +3,14 @@ import { ApiPropertyOptional, IntersectionType, } from '@nestjs/swagger'; -import { IsDateString, IsOptional, IsString, IsUUID } from 'class-validator'; +import { + IsDateString, + IsLatitude, + IsLongitude, + IsOptional, + IsString, + IsUUID, +} from 'class-validator'; import { PaginationQueryDTO } from 'src/utils/dto/pagination.dto'; import { Expose, Transform, Type } from 'class-transformer'; import { BaseUserDTO } from 'src/user/dto/user.dto'; @@ -180,3 +187,17 @@ export class UpdateDeliveryWsDTO { @IsOptional() employeeId?: string; } + +export class UpdateCoordinatesWsDTO { + @ApiProperty({ description: 'ID of the order delivery' }) + @IsUUID() + orderId: string; + + @ApiProperty({ description: 'Latitude of the delivery location' }) + @IsLatitude() + latitude: number; + + @ApiProperty({ description: 'Longitude of the delivery location' }) + @IsLongitude() + longitude: number; +} diff --git a/src/order/order.gateway.ts b/src/order/order.gateway.ts index f9f19ae..ec6fbbb 100644 --- a/src/order/order.gateway.ts +++ b/src/order/order.gateway.ts @@ -23,7 +23,10 @@ import { RolesGuardWs } from 'src/auth/roles.guard'; import { Roles } from 'src/auth/roles.decorador'; import { UserRole } from 'src/user/entities/user.entity'; import { WebsocketExceptionsFilter } from './ws.filters'; -import { UpdateDeliveryWsDTO } from './dto/order-delivery.dto'; +import { + UpdateCoordinatesWsDTO, + UpdateDeliveryWsDTO, +} from './dto/order-delivery.dto'; import { OrderDeliveryService } from './services/order-delivery.controller'; @WebSocketGateway({ @@ -122,4 +125,32 @@ export class OrderGateway implements OnGatewayConnection, OnGatewayDisconnect { client.to(client.id).emit('error', error); }); } + + @UseFilters(new WebsocketExceptionsFilter()) + @UsePipes( + new ValidationPipe({ + exceptionFactory: (errors) => new WsException(errors), + }), + ) + @UseGuards(AuthGuardWs, RolesGuardWs) + @Roles(UserRole.DELIVERY) + @SubscribeMessage('updateCoordinates') + updateCoordinates( + @ConnectedSocket() client: Socket, + @MessageBody() data: UpdateCoordinatesWsDTO, + ) { + this.orderService + .findOneWithUser(data.orderId) + .then((order) => { + if (order.user.wsId) { + client.to(order.user.wsId).emit('coordinatesUpdated', { + latitude: data.latitude, + longitude: data.longitude, + }); + } + }) + .catch((error) => { + client.to(client.id).emit('error', error); + }); + } }