fix bug pager created notification

This commit is contained in:
2025-12-25 18:54:06 +03:30
parent b78eeb14ac
commit f91b5b8710
2 changed files with 7 additions and 151 deletions
-148
View File
@@ -1,148 +0,0 @@
# Pager Socket.IO Testing Guide
## Quick Start
1. **Start your NestJS server:**
```bash
npm run start:dev
```
2. **Open the test page:**
- Open `test-pager-socket.html` in your web browser
- Or serve it via a simple HTTP server:
```bash
# Using Python
python3 -m http.server 8080
# Then open http://localhost:8080/test-pager-socket.html
# Using Node.js (if you have http-server installed)
npx http-server -p 8080
```
## Testing Scenarios
### Scenario 1: Test as Restaurant Admin/Staff
1. **Get your admin JWT token:**
- Login as admin via your API to get the JWT token
- Example:
```bash
curl -X POST http://localhost:4000/admin/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "admin@example.com",
"password": "password"
}'
```
- Copy the `accessToken` from the response
2. **Connect to the server:**
- Enter server URL: `http://localhost:4000`
- Enter your admin JWT token in the "Admin Token" field
- Click "Connect"
3. **Join restaurant room:**
- Select "Restaurant (Admin/Staff)" from User Type
- Click "Join Room"
- The restaurant ID will be automatically extracted from your token
3. **Create a pager request (via API):**
```bash
curl -X POST http://localhost:4000/public/pager \
-H "Content-Type: application/json" \
-H "X-Slug: your-restaurant-slug" \
-d '{
"tableNumber": "5",
"message": "Need assistance"
}'
```
4. **Observe the event:**
- You should see `pager:created` event in the Events Log
### Scenario 2: Test as Public User
1. **Create a pager request first (to get cookie ID):**
```bash
curl -X POST http://localhost:4000/public/pager \
-H "Content-Type: application/json" \
-H "X-Slug: your-restaurant-slug" \
-d '{
"tableNumber": "3",
"message": "Need water"
}' \
-c cookies.txt
```
2. **Get the cookie ID:**
- Check the response or cookies.txt file for `pagerId` cookie value
3. **Connect and join session:**
- Connect to the server
- Select "Session (Public User)" from User Type
- Enter the cookie ID from step 2
- Click "Join Room"
4. **Update pager status (via API as admin):**
```bash
curl -X PATCH http://localhost:4000/admin/pager/{pagerId}/status \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_ADMIN_TOKEN" \
-d '{
"status": "acknowledged"
}'
```
5. **Observe the event:**
- You should see `pager:status:updated` event in the Events Log
## Socket Events Reference
### Client → Server Events
| Event | Payload | Description |
|-------|---------|-------------|
| `join:restaurant` | `{}` (empty, restId extracted from token) | Join restaurant room for admin/staff. **Requires authentication via JWT token in connection auth** |
| `join:session` | `{ cookieId: string }` | Join session room for public user |
| `leave:room` | `{ room: string }` | Leave a specific room |
**Note:** For `join:restaurant`, the restaurant ID is automatically extracted from the authenticated admin's JWT token. You must provide the token when connecting:
```javascript
const socket = io('http://localhost:4000/pager', {
auth: {
token: 'your-jwt-token-here' // or 'Bearer your-jwt-token-here'
}
});
```
### Server → Client Events
| Event | Payload | Description |
|-------|---------|-------------|
| `pager:created` | `{ pager: {...} }` | New pager request created |
| `pager:status:updated` | `{ pager: {...} }` | Pager status updated |
| `joined` | `{ room: string, message: string }` | Successfully joined a room |
| `left` | `{ room: string, message: string }` | Successfully left a room |
| `error` | `{ message: string }` | Error occurred |
## Room Names
- **Restaurant Room:** `restaurant:{restId}`
- **Session Room:** `cookie:{cookieId}`
## Testing Tips
1. **Open multiple browser tabs/windows** to simulate multiple clients
2. **Use browser DevTools** to inspect WebSocket connections
3. **Check server logs** to see gateway activity
4. **Test reconnection** by disconnecting and reconnecting
5. **Test room isolation** by joining different rooms in different tabs
## Troubleshooting
- **Connection fails:** Check if server is running on the correct port
- **No events received:** Verify you've joined the correct room
- **CORS errors:** Ensure CORS is enabled in your NestJS app (already configured in gateway)
- **Events not showing:** Check browser console for errors
@@ -5,15 +5,19 @@ import { PagerCreatedEvent } from '../events/pager.events';
import { Permission } from 'src/common/enums/permission.enum'; import { Permission } from 'src/common/enums/permission.enum';
import { NotifTitleEnum } from 'src/modules/notifications/interfaces/notification.interface'; import { NotifTitleEnum } from 'src/modules/notifications/interfaces/notification.interface';
import { NotificationService } from 'src/modules/notifications/services/notification.service'; import { NotificationService } from 'src/modules/notifications/services/notification.service';
import { ConfigService } from '@nestjs/config';
@Injectable() @Injectable()
export class PagerListeners { export class PagerListeners {
private readonly logger = new Logger(PagerListeners.name); private readonly logger = new Logger(PagerListeners.name);
private readonly smsPatternPagerCreated: string
constructor( constructor(
private readonly adminService: AdminRepository, private readonly adminService: AdminRepository,
private readonly notificationService: NotificationService, private readonly notificationService: NotificationService,
) {} private readonly configService: ConfigService,
) {
this.smsPatternPagerCreated = this.configService.get<string>('SMS_PATTERN_PAGER_CREATED') ?? '123';
}
@OnEvent(PagerCreatedEvent.name) @OnEvent(PagerCreatedEvent.name)
async handlePagerCreated(event: PagerCreatedEvent) { async handlePagerCreated(event: PagerCreatedEvent) {
@@ -30,7 +34,7 @@ export class PagerListeners {
title: NotifTitleEnum.PAGER_CREATED, title: NotifTitleEnum.PAGER_CREATED,
content: `میز شماره ${event.tableNumber} پیج جدیدی انجام داد`, content: `میز شماره ${event.tableNumber} پیج جدیدی انجام داد`,
sms: { sms: {
templateId: '1234567890', templateId: this.smsPatternPagerCreated,
parameters: { parameters: {
tableNumber: event.tableNumber.toString(), tableNumber: event.tableNumber.toString(),
}, },