This commit is contained in:
2025-12-10 11:11:57 +03:30
parent 7ab766a563
commit 30158a7b5f
2 changed files with 311 additions and 0 deletions
+103
View File
@@ -12,6 +12,9 @@ import { Logger, UseGuards } from '@nestjs/common';
import { Pager } from './entities/pager.entity';
import { WsAdminAuthGuard, type AuthenticatedSocket } from './guards/ws-admin-auth.guard';
import { WsRestId } from './decorators/ws-rest-id.decorator';
import { PagerService } from './pager.service';
import { WsAdminId } from './decorators/ws-admin-id.decorator';
import { PagerStatus } from './interface/pager';
@WebSocketGateway({
cors: {
@@ -26,6 +29,8 @@ export class PagerGateway implements OnGatewayConnection, OnGatewayDisconnect {
private readonly logger = new Logger(PagerGateway.name);
constructor(private readonly pagerService: PagerService) {}
handleConnection(client: Socket) {
this.logger.log(`Client connected: ${client.id}`);
}
@@ -78,6 +83,104 @@ export class PagerGateway implements OnGatewayConnection, OnGatewayDisconnect {
}
}
/**
* Get list of pagers for the restaurant (admin only)
* Requires admin authentication via JWT token
* Restaurant ID is extracted from the authenticated admin's token
*/
@UseGuards(WsAdminAuthGuard)
@SubscribeMessage('get:pagers')
async handleGetPagers(@ConnectedSocket() client: AuthenticatedSocket, @WsRestId() restId: string) {
try {
const pagers = await this.pagerService.findAllByRestaurantId(restId);
const pagersData = pagers.map((pager) => ({
id: pager.id,
tableNumber: pager.tableNumber,
message: pager.message,
status: pager.status,
cookieId: pager.cookieId,
createdAt: pager.createdAt,
updatedAt: pager.updatedAt,
user: pager.user
? {
id: pager.user.id,
firstName: pager.user.firstName,
lastName: pager.user.lastName,
}
: null,
staff: pager.staff
? {
id: pager.staff.id,
firstName: pager.staff.firstName,
lastName: pager.staff.lastName,
}
: null,
}));
void client.emit('pagers:list', { pagers: pagersData });
this.logger.log(`Admin ${client.adminId} requested pagers list for restaurant ${restId}`);
} catch (error) {
this.logger.error(`Error getting pagers list: ${error instanceof Error ? error.message : 'Unknown error'}`);
void client.emit('error', {
message: 'Failed to get pagers list',
code: 'GET_PAGERS_ERROR',
details: error instanceof Error ? error.message : 'Unknown error',
});
}
}
/**
* Update pager status (admin only)
* Requires admin authentication via JWT token
* Restaurant ID and Admin ID are extracted from the authenticated admin's token
*/
@UseGuards(WsAdminAuthGuard)
@SubscribeMessage('update:pager:status')
async handleUpdatePagerStatus(
@ConnectedSocket() client: AuthenticatedSocket,
@WsRestId() restId: string,
@WsAdminId() adminId: string,
@MessageBody() data: { pagerId: string; status: PagerStatus },
) {
try {
if (!data.pagerId || !data.status) {
void client.emit('error', {
message: 'Pager ID and status are required',
code: 'INVALID_PAYLOAD',
});
return;
}
const pager = await this.pagerService.updateStatus(data.pagerId, restId, adminId, data.status);
const pagerData = {
id: pager.id,
tableNumber: pager.tableNumber,
message: pager.message,
status: pager.status,
updatedAt: pager.updatedAt,
staff: pager.staff
? {
id: pager.staff.id,
firstName: pager.staff.firstName,
lastName: pager.staff.lastName,
}
: null,
};
void client.emit('pager:status:updated:response', { pager: pagerData });
this.logger.log(`Admin ${adminId} updated pager ${data.pagerId} status to ${data.status}`);
} catch (error) {
this.logger.error(`Error updating pager status: ${error instanceof Error ? error.message : 'Unknown error'}`);
void client.emit('error', {
message: error instanceof Error ? error.message : 'Failed to update pager status',
code: 'UPDATE_PAGER_STATUS_ERROR',
details: error instanceof Error ? error.message : 'Unknown error',
});
}
}
/**
* Emit pager created event to restaurant room
*/