Spaces:
Sleeping
Sleeping
Download src/modules/session/session.controller.ts from jinksQspider/openWa: direct link, hf CLI and curl.
- Browser
- Download file 7.63 kB
-
https://huggingface.co/spaces/jinksQspider/openWa/resolve/main/src/modules/session/session.controller.ts
- Command line
-
hf download hf://spaces/jinksQspider/openWa/src/modules/session/session.controller.ts
-
curl -L -o session.controller.ts https://huggingface.co/spaces/jinksQspider/openWa/resolve/main/src/modules/session/session.controller.ts
7.63 kB
| import { Controller, Get, Post, Delete, Param, Body, HttpCode, HttpStatus } from '@nestjs/common'; | |
| import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger'; | |
| import { SessionService } from './session.service'; | |
| import { CreateSessionDto, SessionResponseDto, QRCodeResponseDto, MarkChatReadDto } from './dto'; | |
| import { Session } from './entities/session.entity'; | |
| import { ChatSummary } from '../../engine/interfaces/whatsapp-engine.interface'; | |
| import { AuditService } from '../audit/audit.service'; | |
| import { AuditAction } from '../audit/entities/audit-log.entity'; | |
| import { RequireRole } from '../auth/decorators/auth.decorators'; | |
| import { ApiKeyRole } from '../auth/entities/api-key.entity'; | |
| ('sessions') | |
| ('sessions') | |
| export class SessionController { | |
| constructor( | |
| private readonly sessionService: SessionService, | |
| private readonly auditService: AuditService, | |
| ) {} | |
| // Transform entity to DTO with lastActive field name | |
| private transformSession(session: Session): SessionResponseDto { | |
| return { | |
| id: session.id, | |
| name: session.name, | |
| status: session.status, | |
| phone: session.phone, | |
| pushName: session.pushName, | |
| connectedAt: session.connectedAt, | |
| lastActive: session.lastActiveAt, | |
| createdAt: session.createdAt, | |
| updatedAt: session.updatedAt, | |
| lastError: session.lastError ?? null, | |
| }; | |
| } | |
| () | |
| (ApiKeyRole.OPERATOR) | |
| ({ summary: 'Create a new WhatsApp session' }) | |
| ({ | |
| status: 201, | |
| description: 'Session created', | |
| type: SessionResponseDto, | |
| }) | |
| ({ status: 409, description: 'Session name already exists' }) | |
| async create(() dto: CreateSessionDto): Promise<Session> { | |
| const session = await this.sessionService.create(dto); | |
| await this.auditService.logInfo(AuditAction.SESSION_CREATED, { | |
| sessionId: session.id, | |
| sessionName: session.name, | |
| }); | |
| return session; | |
| } | |
| () | |
| ({ summary: 'List all sessions' }) | |
| ({ | |
| status: 200, | |
| description: 'List of sessions', | |
| type: [SessionResponseDto], | |
| }) | |
| async findAll(): Promise<SessionResponseDto[]> { | |
| const sessions = await this.sessionService.findAll(); | |
| return sessions.map(s => this.transformSession(s)); | |
| } | |
| (':id') | |
| ({ summary: 'Get session by ID' }) | |
| ({ name: 'id', description: 'Session ID' }) | |
| ({ | |
| status: 200, | |
| description: 'Session details', | |
| type: SessionResponseDto, | |
| }) | |
| ({ status: 404, description: 'Session not found' }) | |
| async findOne(('id') id: string): Promise<SessionResponseDto> { | |
| const session = await this.sessionService.findOne(id); | |
| return this.transformSession(session); | |
| } | |
| (':id') | |
| (ApiKeyRole.OPERATOR) | |
| (HttpStatus.NO_CONTENT) | |
| ({ summary: 'Delete a session' }) | |
| ({ name: 'id', description: 'Session ID' }) | |
| ({ status: 204, description: 'Session deleted' }) | |
| ({ status: 404, description: 'Session not found' }) | |
| async delete(('id') id: string): Promise<void> { | |
| const session = await this.sessionService.findOne(id); | |
| await this.sessionService.delete(id); | |
| await this.auditService.logInfo(AuditAction.SESSION_DELETED, { | |
| sessionId: id, | |
| sessionName: session.name, | |
| }); | |
| } | |
| (':id/start') | |
| (ApiKeyRole.OPERATOR) | |
| ({ | |
| summary: 'Start a session and initialize WhatsApp connection', | |
| }) | |
| ({ name: 'id', description: 'Session ID' }) | |
| ({ | |
| status: 200, | |
| description: 'Session started', | |
| type: SessionResponseDto, | |
| }) | |
| ({ status: 400, description: 'Session already started' }) | |
| ({ status: 404, description: 'Session not found' }) | |
| async start(('id') id: string): Promise<SessionResponseDto> { | |
| const session = await this.sessionService.start(id); | |
| await this.auditService.logInfo(AuditAction.SESSION_STARTED, { | |
| sessionId: session.id, | |
| sessionName: session.name, | |
| }); | |
| return this.transformSession(session); | |
| } | |
| (':id/stop') | |
| (ApiKeyRole.OPERATOR) | |
| ({ summary: 'Stop a session and disconnect WhatsApp' }) | |
| ({ name: 'id', description: 'Session ID' }) | |
| ({ | |
| status: 200, | |
| description: 'Session stopped', | |
| type: SessionResponseDto, | |
| }) | |
| ({ status: 404, description: 'Session not found' }) | |
| async stop(('id') id: string): Promise<SessionResponseDto> { | |
| const session = await this.sessionService.stop(id); | |
| await this.auditService.logInfo(AuditAction.SESSION_STOPPED, { | |
| sessionId: session.id, | |
| sessionName: session.name, | |
| }); | |
| return this.transformSession(session); | |
| } | |
| (':id/qr') | |
| (ApiKeyRole.OPERATOR) | |
| ({ summary: 'Get QR code for session authentication' }) | |
| ({ name: 'id', description: 'Session ID' }) | |
| ({ | |
| status: 200, | |
| description: 'QR code data', | |
| type: QRCodeResponseDto, | |
| }) | |
| ({ | |
| status: 400, | |
| description: 'QR code not ready or session already authenticated', | |
| }) | |
| ({ status: 404, description: 'Session not found' }) | |
| async getQRCode(('id') id: string): Promise<QRCodeResponseDto> { | |
| const qrCode = await this.sessionService.getQRCode(id); | |
| await this.auditService.logInfo(AuditAction.SESSION_QR_GENERATED, { | |
| sessionId: id, | |
| }); | |
| return qrCode; | |
| } | |
| (':id/groups') | |
| ({ summary: 'Get all groups for a session' }) | |
| ({ name: 'id', description: 'Session ID' }) | |
| ({ | |
| status: 200, | |
| description: 'List of groups the session is a member of', | |
| }) | |
| ({ status: 400, description: 'Session not ready' }) | |
| ({ status: 404, description: 'Session not found' }) | |
| async getGroups(('id') id: string): Promise<{ id: string; name: string; linkedParentJID?: string | null }[]> { | |
| return this.sessionService.getGroups(id); | |
| } | |
| (':id/chats') | |
| ({ summary: 'Get active chats for a session' }) | |
| ({ name: 'id', description: 'Session ID' }) | |
| ({ status: 200, description: 'List of active chats' }) | |
| ({ status: 400, description: 'Session not ready' }) | |
| ({ status: 404, description: 'Session not found' }) | |
| async getChats(('id') id: string): Promise<ChatSummary[]> { | |
| return this.sessionService.getChats(id); | |
| } | |
| (':id/chats/read') | |
| (ApiKeyRole.OPERATOR) | |
| ({ summary: 'Mark a chat as read/seen' }) | |
| ({ name: 'id', description: 'Session ID' }) | |
| ({ status: 200, description: 'Chat marked as read successfully' }) | |
| ({ status: 400, description: 'Session not ready' }) | |
| ({ status: 404, description: 'Session not found' }) | |
| async markChatRead(('id') id: string, () dto: MarkChatReadDto): Promise<{ success: boolean }> { | |
| const success = await this.sessionService.sendSeen(id, dto.chatId); | |
| return { success }; | |
| } | |
| ('stats/overview') | |
| ({ | |
| summary: 'Get session statistics for multi-session monitoring', | |
| }) | |
| ({ | |
| status: 200, | |
| description: 'Session statistics including counts and memory usage', | |
| }) | |
| async getStats(): Promise<{ | |
| total: number; | |
| active: number; | |
| ready: number; | |
| disconnected: number; | |
| byStatus: Record<string, number>; | |
| memoryUsage: { heapUsed: number; heapTotal: number; rss: number }; | |
| }> { | |
| return this.sessionService.getStats(); | |
| } | |
| } | |