Skip to content

Commit 7e6a977

Browse files
committed
feat: pr feedback
1 parent 2e74fa3 commit 7e6a977

File tree

11 files changed

+21
-45
lines changed

11 files changed

+21
-45
lines changed

README.md

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,28 +115,6 @@ npm run watch
115115
```
116116

117117
The server will be available at `http://localhost:3000`
118-
119-
### 6. WebSocket (Socket.IO) authentication
120-
121-
The backend exposes a Socket.IO server (default `WEBSOCKET_PORT=8080` in development).
122-
123-
- **Connect with token (preferred)**: provide the JWT in `handshake.auth.token`.
124-
- **Room subscriptions**: clients must `emit('subscribe', roomName)` to join rooms. Rooms that require authorization will respond with an `error` event if the client is unauthenticated/unauthorized.
125-
126-
Example:
127-
128-
```ts
129-
import { io } from 'socket.io-client';
130-
131-
const socket = io('http://localhost:8080', {
132-
auth: { token: '<jwt>' },
133-
});
134-
135-
socket.on('error', (err) => console.error(err));
136-
socket.emit('subscribe', 'pos:123:transactions');
137-
socket.on('transaction:created', (tx) => console.log(tx));
138-
```
139-
140118
### 6. Access API Documentation
141119

142120
Visit `http://localhost:3000/api-docs` to access the Swagger UI for API documentation.

src/controller/server-settings-controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export default class ServerSettingsController extends BaseController {
109109
await store.setSetting('maintenanceMode', body.enabled);
110110

111111
// Send websocket message to POS
112-
WebSocketService.sendMaintenanceMode(body.enabled);
112+
WebSocketService.emitMaintenanceMode(body.enabled);
113113

114114
res.status(204).send();
115115
} catch (error) {

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,9 @@ export default async function createApp(): Promise<Application> {
247247
try {
248248
const existingInstance = WebSocketService.getInstance();
249249
if (existingInstance.server.listening) {
250-
const l = log4js.getLogger('index');
251-
l.info('Closing existing WebSocket server before creating new instance');
250+
application.logger.info('Closing existing WebSocket server before creating new instance');
252251
existingInstance.server.close(() => {
253-
l.info('Existing WebSocket server closed');
252+
application.logger.info('Existing WebSocket server closed');
254253
});
255254
}
256255
} catch {

src/service/websocket-service.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import TokenHandler from '../authentication/token-handler';
3030
import JsonWebToken from '../authentication/json-web-token';
3131
import User from '../entity/user/user';
3232
import { TransactionResponse } from '../controller/response/transaction-response';
33-
import { parseRoom } from './websocket/room-authorization';
33+
import { parseRoom } from './websocket/room-parser';
3434
import {
3535
RoomPolicyRegistry,
3636
RoomRegistration,
@@ -193,7 +193,7 @@ export default class WebSocketService {
193193
},
194194
});
195195

196-
// Register global transaction rooms
196+
// Register global transaction room
197197
this.registerRoom({
198198
pattern: 'transactions:all',
199199
policy: async (context) => this.roleManager.can(
@@ -326,7 +326,7 @@ export default class WebSocketService {
326326

327327
const tokenString = tokenFromAuth ?? tokenFromQuery;
328328

329-
if (!tokenString || typeof tokenString !== 'string') {
329+
if (!tokenString) {
330330
this.logger.trace(`Client ${client.id} connected without authentication`);
331331
return;
332332
}
@@ -339,7 +339,6 @@ export default class WebSocketService {
339339
const token = await this.tokenHandler.verifyToken(tokenString);
340340
const user = await User.findOne({
341341
where: { id: token.user.id },
342-
relations: { pointOfSale: true },
343342
});
344343

345344
if (user) {
@@ -550,7 +549,7 @@ export default class WebSocketService {
550549
* Delegates to the singleton instance.
551550
* @throws Error if WebSocketService has not been initialized.
552551
*/
553-
public static sendMaintenanceMode(enabled: boolean): void {
552+
public static emitMaintenanceMode(enabled: boolean): void {
554553
this.getInstance().sendMaintenanceMode(enabled);
555554
}
556555

src/service/websocket/event-guards.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @license
1919
*/
2020

21-
import { ParsedRoom } from './room-authorization';
21+
import { ParsedRoom } from './room-parser';
2222

2323
/**
2424
* Guard function that determines if an event should be emitted to a room.
File renamed without changes.

src/service/websocket/room-policy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import User from '../../entity/user/user';
2222
import JsonWebToken from '../../authentication/json-web-token';
23-
import { ParsedRoom, matchesRoomPattern } from './room-authorization';
23+
import { ParsedRoom, matchesRoomPattern } from './room-parser';
2424

2525
/**
2626
* WebSocket request context similar to RequestWithToken but for WebSocket connections.

test/unit/controller/server-settings-controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('ServerSettingsController', () => {
8080
tokenHandler: mockTokenHandler,
8181
roleManager: mockRoleManager,
8282
});
83-
// Mock sendMaintenanceMode to avoid actual WebSocket operations in tests
83+
// Mock emitMaintenanceMode to avoid actual WebSocket operations in tests
8484
sinon.stub(webSocketService, 'sendMaintenanceMode').returns(undefined);
8585
sinon.stub(WebSocketService, 'getInstance').returns(webSocketService);
8686

test/unit/service/websocket-service.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ describe('WebSocketService', () => {
200200

201201
// Send maintenance mode and verify we don't receive it after unsubscribing
202202
setTimeout(() => {
203-
WebSocketService.sendMaintenanceMode(true);
203+
WebSocketService.emitMaintenanceMode(true);
204204

205205
setTimeout(() => {
206206
expect(receivedAfterUnsubscribe).to.be.false;
@@ -242,12 +242,12 @@ describe('WebSocketService', () => {
242242
clientSocket.on('maintenance-mode', () => {
243243
done();
244244
});
245-
WebSocketService.sendMaintenanceMode(true);
245+
WebSocketService.emitMaintenanceMode(true);
246246
}, 200);
247247
});
248248
});
249249

250-
describe('sendMaintenanceMode function', () => {
250+
describe('emitMaintenanceMode function', () => {
251251
it('should emit maintenance-mode event to subscribed clients', (done) => {
252252
// Configure ServerSettingsStore mock to return 'true' for maintenance mode
253253
serverSettingsMock.getSettingFromDatabase.withArgs('maintenanceMode').resolves(true);
@@ -264,7 +264,7 @@ describe('WebSocketService', () => {
264264

265265
// Wait for subscription to complete, then send maintenance mode
266266
setTimeout(() => {
267-
WebSocketService.sendMaintenanceMode(true);
267+
WebSocketService.emitMaintenanceMode(true);
268268
}, 100);
269269
});
270270

@@ -284,7 +284,7 @@ describe('WebSocketService', () => {
284284

285285
// Also send maintenance mode manually to ensure test completes
286286
setTimeout(() => {
287-
WebSocketService.sendMaintenanceMode(false);
287+
WebSocketService.emitMaintenanceMode(false);
288288
}, 300);
289289
});
290290
});
@@ -567,10 +567,10 @@ describe('WebSocketService', () => {
567567
expect(webSocketService.emitQRConfirmed).to.be.a('function');
568568
});
569569

570-
it('should delegate sendMaintenanceMode to instance', () => {
570+
it('should delegate emitMaintenanceMode to instance', () => {
571571
// Should complete without error
572-
WebSocketService.sendMaintenanceMode(true);
573-
WebSocketService.sendMaintenanceMode(false);
572+
WebSocketService.emitMaintenanceMode(true);
573+
WebSocketService.emitMaintenanceMode(false);
574574

575575
// Verify method exists
576576
expect(webSocketService.sendMaintenanceMode).to.be.a('function');

test/unit/service/websocket/event-guards.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import { expect } from 'chai';
2222
import { InPosGuard, ForUserGuard } from '../../../../src/service/websocket/event-guards';
23-
import { ParsedRoom } from '../../../../src/service/websocket/room-authorization';
23+
import { ParsedRoom } from '../../../../src/service/websocket/room-parser';
2424

2525
describe('Event Guards', () => {
2626
describe('InPosGuard', () => {

0 commit comments

Comments
 (0)