From b665d1f55c2457b2388966ca0816a258a587f45a Mon Sep 17 00:00:00 2001 From: G Date: Sun, 8 Sep 2024 01:04:38 +0330 Subject: [PATCH] optimising the tickets --- src/features/gateway/gatway.controller.ts | 120 +++++++++--------- src/features/ticket/ticket.controller.ts | 35 +++-- .../dtos/ticket/CreateTicketContent.dto.ts | 2 - src/utils/helpers.ts | 15 +-- 4 files changed, 85 insertions(+), 87 deletions(-) diff --git a/src/features/gateway/gatway.controller.ts b/src/features/gateway/gatway.controller.ts index 6680cc6bb..8d11cb33a 100644 --- a/src/features/gateway/gatway.controller.ts +++ b/src/features/gateway/gatway.controller.ts @@ -110,68 +110,68 @@ export class GateWayController implements OnModuleInit { } } - @UseFilters(new BadRequestTransformationFilter()) - @UsePipes(new ValidationPipe()) - @SubscribeMessage('new:ticket:message') - async onNewTicketContent( - @MessageBody(ValidateGatewayPipe) body: CreateTicketContentDto, - ) { - this.checkConnection(); - const { ticket } = await this.ticketService.getTicketById( - body.ticketId, - body.from, - ); - console.log('\x1b[36m%s\x1b[0m', 'Socket Creating Ticket Content ...\n'); - const content = await this.ticketService.createTicketContent( - { - message: body.message, - from: body.from, - announcement: AnnouncementEnum.NONE, - }, - ticket, - ); - await this.ticketService.updateTicketTime(ticket, body.from); + // @UseFilters(new BadRequestTransformationFilter()) + // @UsePipes(new ValidationPipe()) + // @SubscribeMessage('new:ticket:message') + // async onNewTicketContent( + // @MessageBody(ValidateGatewayPipe) body: CreateTicketContentDto, + // ) { + // this.checkConnection(); + // const { ticket } = await this.ticketService.getTicketById( + // body.ticketId, + // body.from, + // ); + // console.log('\x1b[36m%s\x1b[0m', 'Socket Creating Ticket Content ...\n'); + // const content = await this.ticketService.createTicketContent( + // { + // message: body.message, + // from: body.from, + // announcement: AnnouncementEnum.NONE, + // }, + // ticket, + // ); + // await this.ticketService.updateTicketTime(ticket, body.from); - if (this.socket.connected) { - console.log( - '\x1b[36m%s\x1b[0m', - `Sending back the content for ticket: ${body.ticketId}...`, - ); - // send message to the room - this.server - .to(`room:${ticket.id}`) - .emit(`onTicketMessage${body.ticketId}`, { - socketId: this.socket.id, - content: content, - }); - return content; - } - } + // if (this.socket.connected) { + // console.log( + // '\x1b[36m%s\x1b[0m', + // `Sending back the content for ticket: ${body.ticketId}...`, + // ); + // // send message to the room + // this.server + // .to(`room:${ticket.id}`) + // .emit(`onTicketMessage${body.ticketId}`, { + // socketId: this.socket.id, + // content: content, + // }); + // return content; + // } + // } - @UseFilters(new BadRequestTransformationFilter()) - @UsePipes(new ValidationPipe()) - @SubscribeMessage('new:ticket:view') - async onNewTicketView( - @MessageBody(ValidateGatewayPipe) body: CreateTicketViewDto, - @ConnectedSocket() client: Socket, - ) { - console.log('\x1b[36m%s\x1b[0m', 'Socket Updating Views...\n'); - const { ticket, myView } = await this.ticketService.getTicketById( - body.ticketId, - body.flaskUserId, - ); - console.log('\x1b[36m%s\x1b[0m', 'Updated my view ...\n'); - if (this.socket.connected) { - console.log('\x1b[36m%s\x1b[0m', 'Sending back view details ...\n'); - client.emit(`onViewMessage${myView.flaskUserId}`, { - ticketId: ticket.id, - viewId: myView.id, - flaskUserId: myView.flaskUserId, - lastView: myView.viewed, - socketId: this.socket.id, - }); - } - } + // @UseFilters(new BadRequestTransformationFilter()) + // @UsePipes(new ValidationPipe()) + // @SubscribeMessage('new:ticket:view') + // async onNewTicketView( + // @MessageBody(ValidateGatewayPipe) body: CreateTicketViewDto, + // @ConnectedSocket() client: Socket, + // ) { + // console.log('\x1b[36m%s\x1b[0m', 'Socket Updating Views...\n'); + // const { ticket, myView } = await this.ticketService.getTicketById( + // body.ticketId, + // body.flaskUserId, + // ); + // console.log('\x1b[36m%s\x1b[0m', 'Updated my view ...\n'); + // if (this.socket.connected) { + // console.log('\x1b[36m%s\x1b[0m', 'Sending back view details ...\n'); + // client.emit(`onViewMessage${myView.flaskUserId}`, { + // ticketId: ticket.id, + // viewId: myView.id, + // flaskUserId: myView.flaskUserId, + // lastView: myView.viewed, + // socketId: this.socket.id, + // }); + // } + // } @UseFilters(new BadRequestTransformationFilter()) @UsePipes(new ValidationPipe()) diff --git a/src/features/ticket/ticket.controller.ts b/src/features/ticket/ticket.controller.ts index 85a286e35..1e0821005 100644 --- a/src/features/ticket/ticket.controller.ts +++ b/src/features/ticket/ticket.controller.ts @@ -93,7 +93,7 @@ export class TicketController { return ticket; } - @Post('messages/add') + @Post('content/add') @UsePipes(new ValidationPipe()) // validation for dto files async createTicketMsg( @Req() req: Request, @@ -104,19 +104,28 @@ export class TicketController { if (!isAuthenticated(panelFlaskUserId, panelFlaskTypeId)) { throw new ForbiddenException('You Are not authorized!'); } - const { ticket } = await this.ticketService.getTicketById( - body.ticketId, - body.from, - ); - const msg = body.message; - const from = body.from; + try { + const { ticket } = await this.ticketService.getTicketById( + body.ticketId, + panelFlaskUserId, + ); + const msg = body.message; + const from = panelFlaskUserId; - const contentDetails = { - message: msg, - from, - announcement: AnnouncementEnum.NONE, - }; - return await this.ticketService.createTicketContent(contentDetails, ticket); + const contentDetails = { + message: msg, + from, + announcement: AnnouncementEnum.NONE, + }; + + await this.ticketService.updateTicketTime(ticket, panelFlaskUserId); + return await this.ticketService.createTicketContent( + contentDetails, + ticket, + ); + } catch (e) { + throw new ServerError(e.msg, 500); + } } @UseInterceptors(AddTicketInterceptor) diff --git a/src/types/dtos/ticket/CreateTicketContent.dto.ts b/src/types/dtos/ticket/CreateTicketContent.dto.ts index f7b79124f..6c2b82f19 100644 --- a/src/types/dtos/ticket/CreateTicketContent.dto.ts +++ b/src/types/dtos/ticket/CreateTicketContent.dto.ts @@ -5,7 +5,5 @@ export class CreateTicketContentDto { ticketId: string @IsNotEmpty() message: string - @IsNotEmpty() - from: number } diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index e75caaf6a..2f39c643c 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -29,9 +29,9 @@ const PARENTS_DELIVERED_RANGE = 1; const RELETIVES_DELIVERED_RANGE = 3; export function sleep(time) { - console.log('Sleeping...'); - return new Promise(resolve => setTimeout(resolve, time)); -} + console.log('Sleeping...'); + return new Promise((resolve) => setTimeout(resolve, time)); +} export function getAllFilesFromFolder(dir: string) { let results = []; if (checkIfFileOrDirectoryExists(dir)) { @@ -537,14 +537,6 @@ export function ticketNotifications( t.lastAnnouncement !== AnnouncementEnum.NGO_RECEIVED_MONEY, ) .filter((t) => { - // console.log(new Date(t.updatedAt)); - console.log( - new Date( - t.views.find((v) => v.flaskUserId === flaskUserId) && - t.views.find((v) => v.flaskUserId === flaskUserId).viewed, - ), - ); - // when a user creates a ticket, the participants won't have a view assigned to them const myView = t.views.find((v) => v.flaskUserId === flaskUserId); const latestView = t.views.find( @@ -552,7 +544,6 @@ export function ticketNotifications( Date.parse(v.viewed.toUTCString()) === Math.max(...t.views.map((t) => Date.parse(t.viewed.toUTCString()))), ); - // if (myView) { // const { diff } = timeDifference( // new Date(