Spaces:
Runtime error
Runtime error
File size: 4,085 Bytes
4327358 |
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
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';
@ApiSecurity('api_key')
@Controller('api/:session/labels')
@ApiTags('🏷️ Labels')
export class LabelsController {
constructor(private manager: SessionManager) {}
@Get('/')
@SessionApiParam
@ApiOperation({ summary: 'Get all labels' })
getAll(@WorkingSessionParam session: WhatsappSession): Promise<Label[]> {
return session.getLabels();
}
@Post('/')
@SessionApiParam
@ApiOperation({ summary: 'Create a new label' })
@UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
async create(
@WorkingSessionParam session: WhatsappSession,
@Body() 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);
}
@Put('/:labelId')
@SessionApiParam
@ApiOperation({ summary: 'Update a label' })
@UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
async update(
@WorkingSessionParam session: WhatsappSession,
@Param('labelId') labelId: string,
@Body() 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;
}
@Delete('/:labelId')
@SessionApiParam
@ApiOperation({ summary: 'Delete a label' })
@UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
async delete(
@WorkingSessionParam session: WhatsappSession,
@Param('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 };
}
@Get('/chats/:chatId')
@SessionApiParam
@ChatIdApiParam
@ApiOperation({ summary: 'Get labels for the chat' })
getChatLabels(
@WorkingSessionParam session: WhatsappSession,
@Param('chatId') chatId: string,
): Promise<Label[]> {
return session.getChatLabels(chatId);
}
@Put('/chats/:chatId')
@SessionApiParam
@ChatIdApiParam
@ApiOperation({ summary: 'Save labels for the chat' })
putChatLabels(
@WorkingSessionParam session: WhatsappSession,
@Param('chatId') chatId: string,
@Body() request: SetLabelsRequest,
) {
return session.putLabelsToChat(chatId, request.labels);
}
@Get('/:labelId/chats')
@SessionApiParam
@ApiOperation({ summary: 'Get chats by label' })
getChatsByLabel(
@WorkingSessionParam session: WhatsappSession,
@Param('labelId') labelId: string,
) {
return session.getChatsByLabelId(labelId);
}
}
|