Skip to content

Commit

Permalink
better notificationController API and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dlste committed Oct 14, 2024
1 parent 2fb215c commit 7efe791
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
61 changes: 53 additions & 8 deletions server/src/controllers/notificationController.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,72 @@
import { WebSocket } from 'ws';

class NotificationController {
private active: Map<number, WebSocket>;
private active: Map<number, WebSocket | null>;

constructor() {
this.active = new Map();
}

add(ticketNumber: number, ws: WebSocket): void {
this.active.set(ticketNumber, ws);
hasTicket(ticketNumber: number): boolean {
return this.active.has(ticketNumber);
}

remove(ticketNumber: number): boolean {
const ws = this.active.get(ticketNumber);
/**
* To be called when delivering the ticket number to the customer.
* @returns `false` if the ticket already exists, `true` otherwise
*/
addTicket(ticketNumber: number): boolean {
if (this.hasTicket(ticketNumber))
return false;
this.active.set(ticketNumber, null);
return true;
}

/**
* To be called when a the user connects to the endpoint corresponding to the ticket.
* If a connection already exists, it is overridden.
* @returns `false` if the ticket does not exist, `true` otherwise
*/
addConnection(ticketNumber: number, ws: WebSocket): boolean {
if (!this.removeConnection(ticketNumber))
return false;

this.active.set(ticketNumber, ws);
return true;
}

if (!ws)
/**
* Removes a ticket from the active tickets. To be called when an user has been served.
* @returns `false` if the ticket does not exist, `true` otherwise
*/
removeTicket(ticketNumber: number): boolean {
if (!this.removeConnection(ticketNumber))
return false;
if (ws.readyState !== ws.CLOSED && ws.readyState !== ws.CLOSING)
ws.close();

return this.active.delete(ticketNumber);
}

/**
* Closes an existing connection for a specific ticket.
* To be called in case the connection is interrupted.
* @returns `false` if the ticket does not exist, `true` otherwise
*/
removeConnection(ticketNumber: number): boolean {
const curr = this.active.get(ticketNumber);
if (curr === undefined)
return false;
if (curr && curr.readyState !== curr.CLOSED && curr.readyState !== curr.CLOSING) {
curr.close();
}

this.active.set(ticketNumber, null);
return true;
}

/**
* Notifies the user corresponding to the `ticketNumber`
* @returns `false` if the ticket does not exist, `true` otherwise
*/
notify(ticketNumber: number, counter: number): boolean {
const ws = this.active.get(ticketNumber);
if(!ws)
Expand Down
7 changes: 5 additions & 2 deletions server/src/routers/notificationRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ router.ws("/tickets/notification/:ticketId", [
validateWsRequest,
async (req: any, res: WSResponse) => {
const ticketId = Number(req.params.ticketId);
if (!notificationController.hasTicket(ticketId)) {
return res.status(422);
}

const ws = await res.accept();
notificationController.add(ticketId, ws)
notificationController.addConnection(ticketId, ws)
ws.on('close', () => {
notificationController.remove(ticketId);
notificationController.removeConnection(ticketId);
});
}
);
Expand Down
4 changes: 3 additions & 1 deletion server/test_integration/notificationIntegration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ describe('Notification controller API tests', () => {
const ticketNumber = 3;
const counter = 3;

notificationController.addTicket(ticketNumber);
const ws = await request(server).ws(`/api/tickets/notification/${ticketNumber}`);

ws.on('message', (data: WebSocket.RawData, isBinary: boolean) => {
expect(isBinary).toBe(false);
expect(data).toStrictEqual(Buffer.from(`${counter}\n`))
});

notificationController.notify(ticketNumber, counter);
const ret = notificationController.notify(ticketNumber, counter);
expect(ret).toBe(true);
});

it('2 - negative ticket number', async () => {
Expand Down

0 comments on commit 7efe791

Please sign in to comment.