From 08aa0fd94f199980ddaf27b10a41b6aab446f508 Mon Sep 17 00:00:00 2001 From: Stefano Di Leo Date: Mon, 14 Oct 2024 12:27:12 +0200 Subject: [PATCH] Plug notificationController in ticketController and update tests --- server/src/controllers/notificationController.ts | 9 +++++++++ server/src/controllers/ticketController.ts | 7 +++++-- server/test_integration/notificationIntegration.test.ts | 5 +++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/server/src/controllers/notificationController.ts b/server/src/controllers/notificationController.ts index 94a1861..e3a4c1b 100644 --- a/server/src/controllers/notificationController.ts +++ b/server/src/controllers/notificationController.ts @@ -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() diff --git a/server/src/controllers/ticketController.ts b/server/src/controllers/ticketController.ts index c454330..69c0c30 100644 --- a/server/src/controllers/ticketController.ts +++ b/server/src/controllers/ticketController.ts @@ -1,5 +1,6 @@ import { Request, Response } from 'express'; import TicketDAO from '../dao/ticketDao'; +import notificationController from './notificationController'; class TicketController { private dao: TicketDAO; @@ -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 }); @@ -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; diff --git a/server/test_integration/notificationIntegration.test.ts b/server/test_integration/notificationIntegration.test.ts index b6e60ba..31b44cc 100644 --- a/server/test_integration/notificationIntegration.test.ts +++ b/server/test_integration/notificationIntegration.test.ts @@ -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) => {