Skip to content

Commit

Permalink
Plug notificationController in ticketController and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dlste committed Oct 15, 2024
1 parent 0d448ea commit 5a0c303
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
9 changes: 9 additions & 0 deletions server/src/controllers/notificationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ class NotificationController {

return true;
}

removeAll(): void {
this.active.forEach((value) => {
if (value && value.readyState !== value.CLOSED && value.readyState !== value.CLOSING) {
value.close();
}
});
this.active.clear();
}
}

const notificationController = new NotificationController()
Expand Down
7 changes: 5 additions & 2 deletions server/src/controllers/ticketController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Request, Response } from 'express';
import TicketDAO from '../dao/ticketDao';
import notificationController from './notificationController';

class TicketController {
private dao: TicketDAO;
Expand All @@ -17,8 +18,9 @@ class TicketController {
}

try {
const tickets = await this.dao.getTicket(Number(service_id));
res.status(200).json("S"+ service_id + "-" + tickets);
const ticket = await this.dao.getTicket(Number(service_id));
notificationController.addTicket(ticket)
res.status(200).json("S"+ service_id + "-" + ticket);
} catch (error) {
const status = (error as any).status || 500;
res.status(status).json({ message: (error as Error).message });
Expand All @@ -29,6 +31,7 @@ class TicketController {

try {
await this.dao.resetServiceCounters();
notificationController.removeAll();
res.status(200).json({ message: 'Service counters reset successfully' });
} catch (error) {
const status = (error as any).status || 500;
Expand Down
5 changes: 3 additions & 2 deletions server/test_integration/notificationIntegration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ describe('Notification controller API tests', () => {
});

it('1 - correct api usage', async () => {
const ticketNumber = 3;
const serviceId = 3;
const counter = 3;

notificationController.addTicket(ticketNumber);
const response = await request(server).get(`/api/tickets/${serviceId}`);
const ticketNumber = Number((response.body as string).split('-')[1]);
const ws = await request(server).ws(`/api/tickets/notification/${ticketNumber}`);

ws.on('message', (data: WebSocket.RawData, isBinary: boolean) => {
Expand Down

0 comments on commit 5a0c303

Please sign in to comment.