Spaces:
Runtime error
Runtime error
| import { | |
| Body, | |
| Controller, | |
| Delete, | |
| Get, | |
| NotFoundException, | |
| Param, | |
| Post, | |
| Put, | |
| UnprocessableEntityException, | |
| UsePipes, | |
| ValidationPipe, | |
| } from '@nestjs/common'; | |
| import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger'; | |
| import { WhatsappSession } from '@waha/core/abc/session.abc'; | |
| import { ChatIdApiParam } from '@waha/nestjs/params/ChatIdApiParam'; | |
| import { | |
| SessionApiParam, | |
| WorkingSessionParam, | |
| } from '@waha/nestjs/params/SessionApiParam'; | |
| import { | |
| Label, | |
| LabelBody, | |
| SetLabelsRequest, | |
| } from '@waha/structures/labels.dto'; | |
| import * as lodash from 'lodash'; | |
| import { SessionManager } from '../core/abc/manager.abc'; | |
| ('api_key') | |
| ('api/:session/labels') | |
| ('🏷️ Labels') | |
| export class LabelsController { | |
| constructor(private manager: SessionManager) {} | |
| ('/') | |
| ({ summary: 'Get all labels' }) | |
| getAll( session: WhatsappSession): Promise<Label[]> { | |
| return session.getLabels(); | |
| } | |
| ('/') | |
| ({ summary: 'Create a new label' }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| async create( | |
| session: WhatsappSession, | |
| () body: LabelBody, | |
| ): Promise<Label> { | |
| const labelDto = body.toDTO(); | |
| const labels = await session.getLabels(); | |
| if (labels.length >= 20) { | |
| throw new UnprocessableEntityException('Maximum 20 labels allowed'); | |
| } | |
| const labelByName = lodash.find(labels, { name: labelDto.name }); | |
| if (labelByName) { | |
| throw new UnprocessableEntityException( | |
| `Label with '${labelByName.name}' already exists, id: ${labelByName.id}`, | |
| ); | |
| } | |
| return session.createLabel(labelDto); | |
| } | |
| ('/:labelId') | |
| ({ summary: 'Update a label' }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| async update( | |
| session: WhatsappSession, | |
| ('labelId') labelId: string, | |
| () body: LabelBody, | |
| ): Promise<Label> { | |
| const label = await session.getLabel(labelId); | |
| if (!label) { | |
| throw new NotFoundException('Label not found'); | |
| } | |
| const labelDto = body.toDTO(); | |
| const labels = await session.getLabels(); | |
| const labelByName = lodash.find(labels, { name: labelDto.name }); | |
| if (labelByName) { | |
| throw new UnprocessableEntityException( | |
| `Label with '${labelByName.name}' already exists, id: ${labelByName.id}`, | |
| ); | |
| } | |
| label.name = labelDto.name; | |
| label.color = labelDto.color; | |
| label.colorHex = Label.toHex(label.color); | |
| await session.updateLabel(label); | |
| return label; | |
| } | |
| ('/:labelId') | |
| ({ summary: 'Delete a label' }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| async delete( | |
| session: WhatsappSession, | |
| ('labelId') labelId: string, | |
| ): Promise<any> { | |
| const label = await session.getLabel(labelId); | |
| if (!label) { | |
| throw new NotFoundException('Label not found'); | |
| } | |
| await session.deleteLabel(label); | |
| return { result: true }; | |
| } | |
| ('/chats/:chatId') | |
| ({ summary: 'Get labels for the chat' }) | |
| getChatLabels( | |
| session: WhatsappSession, | |
| ('chatId') chatId: string, | |
| ): Promise<Label[]> { | |
| return session.getChatLabels(chatId); | |
| } | |
| ('/chats/:chatId') | |
| ({ summary: 'Save labels for the chat' }) | |
| putChatLabels( | |
| session: WhatsappSession, | |
| ('chatId') chatId: string, | |
| () request: SetLabelsRequest, | |
| ) { | |
| return session.putLabelsToChat(chatId, request.labels); | |
| } | |
| ('/:labelId/chats') | |
| ({ summary: 'Get chats by label' }) | |
| getChatsByLabel( | |
| session: WhatsappSession, | |
| ('labelId') labelId: string, | |
| ) { | |
| return session.getChatsByLabelId(labelId); | |
| } | |
| } | |