Skip to content

Commit

Permalink
optimising the tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan-g committed Sep 7, 2024
1 parent c9f199a commit b665d1f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 87 deletions.
120 changes: 60 additions & 60 deletions src/features/gateway/gatway.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
35 changes: 22 additions & 13 deletions src/features/ticket/ticket.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions src/types/dtos/ticket/CreateTicketContent.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ export class CreateTicketContentDto {
ticketId: string
@IsNotEmpty()
message: string
@IsNotEmpty()
from: number
}

15 changes: 3 additions & 12 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -537,22 +537,13 @@ 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(
(v) =>
Date.parse(v.viewed.toUTCString()) ===
Math.max(...t.views.map((t) => Date.parse(t.viewed.toUTCString()))),
);

// if (myView) {
// const { diff } = timeDifference(
// new Date(
Expand Down

0 comments on commit b665d1f

Please sign in to comment.