Skip to content

Commit 2596cd8

Browse files
Implement websocket connection validation and add environment management routes for notifications
1 parent 6324a93 commit 2596cd8

5 files changed

Lines changed: 126 additions & 33 deletions

File tree

app/handlers/notifications/websocketRouteHandler.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,24 @@ export const websocketRouteHandler=(
1313
connection: WebSocket
1414
)=>{
1515
const connectionId = Math.random().toString(36).substring(2, 15);
16-
const connectionData: ActiveConnection = {
17-
socket: connection,
18-
user_id: Number(req.params.user_id),
19-
user_type: req.params.user_type,
20-
topics: []
21-
};
22-
activeConnections.set(connectionId, connectionData);
23-
console.log(activeConnections)
24-
connection.on("message", (message) => {
25-
try {
16+
//verify that userID and userType doesn't exist in activeConnections already
17+
for (const [id, connData] of activeConnections.entries()) {
18+
if (connData.user_id === req.params.user_id && connData.user_type === req.params.user_type) {
19+
console.log(`User with ID ${req.params.user_id} and type ${req.params.user_type} is already connected.`);
20+
connection.close(1000, "User already connected");
21+
return;
22+
}
23+
}
24+
const connectionData: ActiveConnection = {
25+
socket: connection,
26+
user_id: Number(req.params.user_id),
27+
user_type: req.params.user_type,
28+
topics: []
29+
};
30+
activeConnections.set(connectionId, connectionData);
31+
console.log(activeConnections)
32+
connection.on("message", (message) => {
33+
try {
2634
const parsed = JSON.parse(message.toString());
2735
console.log("Received:", parsed);
2836

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { FastifyInstance, FastifyRequest } from 'fastify';
2+
import { appEmitter } from '../../services/notifications/event';
3+
4+
export async function environmentManagementRoutes(fastify: FastifyInstance) {
5+
fastify.post('/environment-created', async (request: FastifyRequest<{ Body: { name: string } }>, reply) => {
6+
appEmitter.emit('environment.created', {
7+
name: request.body.name
8+
});
9+
return { success: true, event: 'environment.created' };
10+
});
11+
12+
fastify.post('/zone-created', async (request: FastifyRequest<{ Body: { name: string } }>, reply) => {
13+
appEmitter.emit('zone.created', {
14+
zoneName: request.body.name
15+
});
16+
return { success: true, event: 'zone.created' };
17+
});
18+
19+
fastify.post('/zone-updated', async (request: FastifyRequest<{ Body: { name: string } }>, reply) => {
20+
appEmitter.emit('zone.updated', {
21+
zoneName: request.body.name
22+
});
23+
return { success: true, event: 'zone.updated' };
24+
});
25+
26+
fastify.post('/zone-deleted', async (request: FastifyRequest<{ Body: { id: string } }>, reply) => {
27+
appEmitter.emit('zone.deleted', {
28+
zoneId: request.body.id,
29+
});
30+
return { success: true, event: 'zone.deleted' };
31+
});
32+
33+
fastify.post('/floor-created', async (request: FastifyRequest<{ Body: { floorName: string, environmentId: string } }>, reply) => {
34+
appEmitter.emit('floor.created', {
35+
floorName: request.body.floorName,
36+
environmentId: request.body.environmentId,
37+
});
38+
return { success: true, event: 'floor.created' };
39+
});
40+
41+
fastify.post('/floor-updated', async (request: FastifyRequest<{ Body: { id: string } }>, reply) => {
42+
appEmitter.emit('floor.updated', {
43+
floorId: request.body.id,
44+
});
45+
return { success: true, event: 'floor.updated' };
46+
});
47+
48+
fastify.post('/floor-deleted', async (request: FastifyRequest<{ Body: { id: string } }>, reply) => {
49+
appEmitter.emit('floor.deleted', {
50+
floorId: request.body.id,
51+
});
52+
return { success: true, event: 'floor.deleted' };
53+
});
54+
55+
fastify.post('/poi-created', async (request: FastifyRequest<{ Body: { name: string } }>, reply) => {
56+
appEmitter.emit('poi.created', {
57+
poiName: request.body.name
58+
});
59+
return { success: true, event: 'poi.created' };
60+
});
61+
62+
fastify.post('/poi-updated', async (request: FastifyRequest<{ Body: { name: string } }>, reply) => {
63+
appEmitter.emit('poi.updated', {
64+
poiName: request.body.name
65+
});
66+
return { success: true, event: 'poi.updated' };
67+
});
68+
69+
fastify.post('/poi-deleted', async (request: FastifyRequest<{ Body: { id: string } }>, reply) => {
70+
appEmitter.emit('poi.deleted', {
71+
poiId: request.body.id
72+
});
73+
return { success: true, event: 'poi.deleted' };
74+
});
75+
}

app/routers/notifications/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { FastifyInstance } from 'fastify';
22
import websocketRoute from './websocketRoute';
33
import {registerNotificationRoutes} from './notificationRoutes';
44
import { pushRoutes } from './pushRoutes';
5+
import { environmentManagementRoutes } from './cartographyNorifsRoutes';
56
export default async function registerRoutesNotifications(fastify: FastifyInstance) {
67
fastify.register(websocketRoute, { prefix: '/websocket' });
78
fastify.register((fastify) => { registerNotificationRoutes(fastify); }, { prefix: '/' });
89
fastify.register((fastify) => { pushRoutes(fastify); }, { prefix: '/push' });
10+
fastify.register(environmentManagementRoutes, { prefix: '/notify' });
911
}

app/services/notifications/inAppChannelService.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ import { WebSocket } from "ws";
77
export const inAppChannelService={
88
async sendNotification(notification: NotificationPayload) {
99
const notificationMessage = JSON.stringify({
10-
type: "notification",
11-
data: notification,
12-
timestamp: new Date().toISOString()
10+
id: notification.requestId,
11+
user_id: notification.recipient?.[0]?.userId ?? null,
12+
user_type: notification.recipient?.[0]?.userType ?? null,
13+
type: notification.notificationType,
14+
title: notification.message.pushNotification?.title || notification.message.body,
15+
message: notification.message.pushNotification?.body || notification.message.body,
16+
metadata: notification.metadata,
17+
is_read: false,
18+
created_at: notification.timestamp,
19+
sent_at: notification.schedule?.sendAt ?? notification.timestamp,
20+
read_at: null
1321
});
1422
activeConnections.forEach((connection, connectionId) => {
1523
// Skip if connection is not open

app/services/notifications/notificationsListners.ts/environmentManagement.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ export default function setupNotificationListenersEnvironment() {
2323
recipient: adminRecipients,
2424
message: {
2525
subject: "New Environment Created",
26-
body: `A new environment "${data.name}" has been created by ${data.createdBy || 'unknown'}.`,
26+
body: `A new environment "${data.name}" has been created.`,
2727
attachments: [],
2828
pushNotification: {
2929
title: "New Environment",
30-
body: `${data.name} has been created by ${data.createdBy || 'unknown'}`,
30+
body: `${data.name} has been created.`,
3131
},
3232
},
3333
schedule: undefined,
@@ -54,11 +54,11 @@ export default function setupNotificationListenersEnvironment() {
5454
recipient: adminRecipients,
5555
message: {
5656
subject: "New Zone Created",
57-
body: `A new zone "${data.zoneName}" has been created by ${data.createdBy || 'unknown'}.`,
57+
body: `A new zone "${data.zoneName}" has been created.`,
5858
attachments: [],
5959
pushNotification: {
6060
title: "New Zone",
61-
body: `${data.zoneName} has been created by ${data.createdBy || 'unknown'}`,
61+
body: `${data.zoneName} has been created.`,
6262
},
6363
},
6464
schedule: undefined,
@@ -85,11 +85,11 @@ export default function setupNotificationListenersEnvironment() {
8585
recipient: adminRecipients,
8686
message: {
8787
subject: "Zone Updated",
88-
body: `The zone "${data.zoneName}" has been updated by ${data.updatedBy || 'unknown'}.`,
88+
body: `The zone "${data.zoneName}" has been updated.`,
8989
attachments: [],
9090
pushNotification: {
9191
title: "Zone Updated",
92-
body: `${data.zoneName} has been updated by ${data.updatedBy || 'unknown'}`,
92+
body: `${data.zoneName} has been updated.`,
9393
},
9494
},
9595
schedule: undefined,
@@ -116,11 +116,11 @@ export default function setupNotificationListenersEnvironment() {
116116
recipient: adminRecipients,
117117
message: {
118118
subject: "Zone Deleted",
119-
body: `The zone "${data.zoneId}" has been deleted by ${data.deletedBy || 'unknown'}.`,
119+
body: `The zone "${data.zoneId}" has been deleted.`,
120120
attachments: [],
121121
pushNotification: {
122122
title: "Zone Deleted",
123-
body: `${data.zoneId} has been deleted by ${data.deletedBy || 'unknown'}`,
123+
body: `${data.zoneId} has been deleted.`,
124124
},
125125
},
126126
schedule: undefined,
@@ -148,11 +148,11 @@ export default function setupNotificationListenersEnvironment() {
148148
recipient: adminRecipients,
149149
message: {
150150
subject: "New POI Created",
151-
body: `A new POI "${data.poiName}" has been created by ${data.createdBy || 'unknown'}.`,
151+
body: `A new POI "${data.poiName}" has been created.`,
152152
attachments: [],
153153
pushNotification: {
154154
title: "New POI",
155-
body: `${data.poiName} has been created by ${data.createdBy || 'unknown'}`,
155+
body: `${data.poiName} has been created`,
156156
},
157157
},
158158
schedule: undefined,
@@ -178,11 +178,11 @@ export default function setupNotificationListenersEnvironment() {
178178
recipient: adminRecipients,
179179
message: {
180180
subject: "POI Updated",
181-
body: `The POI "${data.poiName}" has been updated by ${data.updatedBy || 'unknown'}.`,
181+
body: `The POI "${data.poiName}" has been updated.`,
182182
attachments: [],
183183
pushNotification: {
184184
title: "POI Updated",
185-
body: `${data.poiName} has been updated by ${data.updatedBy || 'unknown'}`,
185+
body: `${data.poiName} has been updated.`,
186186
},
187187
},
188188
schedule: undefined,
@@ -208,11 +208,11 @@ export default function setupNotificationListenersEnvironment() {
208208
recipient: adminRecipients,
209209
message: {
210210
subject: "POI Deleted",
211-
body: `The POI "${data.poiId}" has been deleted by ${data.deletedBy || 'unknown'}.`,
211+
body: `The POI "${data.poiId}" has been deleted.`,
212212
attachments: [],
213213
pushNotification: {
214214
title: "POI Deleted",
215-
body: `${data.poiId} has been deleted by ${data.deletedBy || 'unknown'}`,
215+
body: `${data.poiId} has been deleted.`,
216216
},
217217
},
218218
schedule: undefined,
@@ -239,11 +239,11 @@ export default function setupNotificationListenersEnvironment() {
239239
recipient: adminRecipients,
240240
message: {
241241
subject: "New Floor Created",
242-
body: `A new floor "${data.floorName}" has been created by ${data.createdBy || 'unknown'}.`,
242+
body: `A new floor "${data.floorName}" has been created.`,
243243
attachments: [],
244244
pushNotification: {
245245
title: "New Floor",
246-
body: `${data.floorName} has been created at environment ${data.environmentId} by ${data.createdBy || 'unknown'}`,
246+
body: `${data.floorName} has been created at environment ${data.environmentId}.`,
247247
},
248248
},
249249
schedule: undefined,
@@ -269,11 +269,11 @@ export default function setupNotificationListenersEnvironment() {
269269
recipient: adminRecipients,
270270
message: {
271271
subject: "Floor Updated",
272-
body: `The floor "${data.floorId}" has been updated by ${data.updatedBy || 'unknown'}.`,
272+
body: `The floor "${data.floorId}" has been updated.`,
273273
attachments: [],
274274
pushNotification: {
275275
title: "Floor Updated",
276-
body: `${data.floorId} has been updated by ${data.updatedBy || 'unknown'}`,
276+
body: `${data.floorId} has been updated.`,
277277
},
278278
},
279279
schedule: undefined,
@@ -298,11 +298,11 @@ export default function setupNotificationListenersEnvironment() {
298298
recipient: adminRecipients,
299299
message: {
300300
subject: "Floor Deleted",
301-
body: `The floor "${data.floorId}" has been deleted by ${data.deletedBy || 'unknown'}.`,
301+
body: `The floor "${data.floorId}" has been deleted.`,
302302
attachments: [],
303303
pushNotification: {
304304
title: "Floor Deleted",
305-
body: `${data.floorId} has been deleted by ${data.deletedBy || 'unknown'}`,
305+
body: `${data.floorId} has been deleted.`,
306306
},
307307
},
308308
schedule: undefined,

0 commit comments

Comments
 (0)