Spaces:
Runtime error
Runtime error
| import { Controller, Get, Post, Delete, Param, Body, Query } from '@nestjs/common'; | |
| import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody, ApiQuery } from '@nestjs/swagger'; | |
| import { ChannelService } from './channel.service'; | |
| import { SubscribeChannelDto } from './dto/subscribe-channel.dto'; | |
| import { RequireRole } from '../auth/decorators/auth.decorators'; | |
| import { ApiKeyRole } from '../auth/entities/api-key.entity'; | |
| ('channels') | |
| ('sessions/:sessionId/channels') | |
| export class ChannelController { | |
| constructor(private readonly channelService: ChannelService) {} | |
| () | |
| ({ summary: 'Get all subscribed channels/newsletters' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ | |
| status: 200, | |
| description: 'List of subscribed channels', | |
| }) | |
| ({ status: 400, description: 'Session not ready' }) | |
| async findAll(('sessionId') sessionId: string) { | |
| return this.channelService.getSubscribedChannels(sessionId); | |
| } | |
| (':channelId') | |
| ({ summary: 'Get a specific channel by ID' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ name: 'channelId', description: 'Channel ID' }) | |
| ({ | |
| status: 200, | |
| description: 'Channel details', | |
| }) | |
| ({ status: 404, description: 'Channel not found' }) | |
| async findOne(('sessionId') sessionId: string, ('channelId') channelId: string) { | |
| return this.channelService.getChannelById(sessionId, channelId); | |
| } | |
| (':channelId/messages') | |
| ({ summary: 'Get messages from a channel' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ name: 'channelId', description: 'Channel ID' }) | |
| ({ name: 'limit', required: false, type: Number, description: 'Max messages to return (default 50)' }) | |
| ({ | |
| status: 200, | |
| description: 'List of channel messages', | |
| }) | |
| async getMessages( | |
| ('sessionId') sessionId: string, | |
| ('channelId') channelId: string, | |
| ('limit') limit?: string, | |
| ) { | |
| // A non-empty non-numeric ?limit (e.g. `?limit=abc`) is truthy, so a bare `parseInt` would forward | |
| // NaN to the engine (wwjs then returns an unpredictable/empty set). Fall back to the engine default. | |
| const parsed = limit !== undefined ? parseInt(limit, 10) : NaN; | |
| return this.channelService.getChannelMessages(sessionId, channelId, Number.isNaN(parsed) ? undefined : parsed); | |
| } | |
| ('subscribe') | |
| (ApiKeyRole.OPERATOR) | |
| ({ summary: 'Subscribe to a channel using invite code' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ | |
| schema: { | |
| type: 'object', | |
| properties: { | |
| inviteCode: { | |
| type: 'string', | |
| description: 'Channel invite code (from channel link)', | |
| example: 'ABC123xyz', | |
| }, | |
| }, | |
| required: ['inviteCode'], | |
| }, | |
| }) | |
| ({ | |
| status: 201, | |
| description: 'Successfully subscribed to channel', | |
| }) | |
| async subscribe(('sessionId') sessionId: string, () body: SubscribeChannelDto) { | |
| return this.channelService.subscribeToChannel(sessionId, body.inviteCode); | |
| } | |
| (':channelId') | |
| (ApiKeyRole.OPERATOR) | |
| ({ summary: 'Unsubscribe from a channel' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ name: 'channelId', description: 'Channel ID to unsubscribe from' }) | |
| ({ | |
| status: 200, | |
| description: 'Successfully unsubscribed from channel', | |
| }) | |
| async unsubscribe(('sessionId') sessionId: string, ('channelId') channelId: string) { | |
| await this.channelService.unsubscribeFromChannel(sessionId, channelId); | |
| return { success: true }; | |
| } | |
| } | |