Spaces:
Sleeping
Sleeping
File size: 3,675 Bytes
525d39f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | import { Controller, Get, Post, Delete, Param, Body, BadRequestException, NotFoundException } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger';
import { SessionService } from '../session/session.service';
@ApiTags('labels')
@Controller('sessions/:sessionId/labels')
export class LabelController {
constructor(private readonly sessionService: SessionService) {}
private getEngine(sessionId: string) {
const engine = this.sessionService.getEngine(sessionId);
if (!engine) {
throw new BadRequestException('Session is not started');
}
return engine;
}
@Get()
@ApiOperation({ summary: 'Get all labels (WhatsApp Business only)' })
@ApiParam({ name: 'sessionId', description: 'Session ID' })
@ApiResponse({
status: 200,
description: 'List of labels',
})
@ApiResponse({ status: 400, description: 'Session not ready or not a business account' })
@ApiResponse({ status: 404, description: 'Session not found' })
async findAll(@Param('sessionId') sessionId: string) {
const engine = this.getEngine(sessionId);
return engine.getLabels();
}
@Get(':labelId')
@ApiOperation({ summary: 'Get a specific label by ID' })
@ApiParam({ name: 'sessionId', description: 'Session ID' })
@ApiParam({ name: 'labelId', description: 'Label ID' })
@ApiResponse({
status: 200,
description: 'Label details',
})
@ApiResponse({ status: 404, description: 'Label not found' })
async findOne(@Param('sessionId') sessionId: string, @Param('labelId') labelId: string) {
const engine = this.getEngine(sessionId);
const label = await engine.getLabelById(labelId);
if (!label) {
throw new NotFoundException(`Label ${labelId} not found`);
}
return label;
}
@Get('chat/:chatId')
@ApiOperation({ summary: 'Get labels for a specific chat' })
@ApiParam({ name: 'sessionId', description: 'Session ID' })
@ApiParam({ name: 'chatId', description: 'Chat ID' })
@ApiResponse({
status: 200,
description: 'List of labels for the chat',
})
async getChatLabels(@Param('sessionId') sessionId: string, @Param('chatId') chatId: string) {
const engine = this.getEngine(sessionId);
return engine.getChatLabels(chatId);
}
@Post('chat/:chatId')
@ApiOperation({ summary: 'Add a label to a chat' })
@ApiParam({ name: 'sessionId', description: 'Session ID' })
@ApiParam({ name: 'chatId', description: 'Chat ID' })
@ApiBody({
schema: {
type: 'object',
properties: {
labelId: { type: 'string', description: 'Label ID to add' },
},
required: ['labelId'],
},
})
@ApiResponse({
status: 200,
description: 'Label added to chat',
})
async addLabelToChat(
@Param('sessionId') sessionId: string,
@Param('chatId') chatId: string,
@Body() body: { labelId: string },
) {
const engine = this.getEngine(sessionId);
await engine.addLabelToChat(chatId, body.labelId);
return { success: true };
}
@Delete('chat/:chatId/:labelId')
@ApiOperation({ summary: 'Remove a label from a chat' })
@ApiParam({ name: 'sessionId', description: 'Session ID' })
@ApiParam({ name: 'chatId', description: 'Chat ID' })
@ApiParam({ name: 'labelId', description: 'Label ID to remove' })
@ApiResponse({
status: 200,
description: 'Label removed from chat',
})
async removeLabelFromChat(
@Param('sessionId') sessionId: string,
@Param('chatId') chatId: string,
@Param('labelId') labelId: string,
) {
const engine = this.getEngine(sessionId);
await engine.removeLabelFromChat(chatId, labelId);
return { success: true };
}
}
|