Spaces:
Runtime error
Runtime error
| import { isLidUser } from '@adiwajshing/baileys/lib/WABinary/jid-utils'; | |
| import { | |
| Controller, | |
| Get, | |
| Param, | |
| Query, | |
| UnprocessableEntityException, | |
| UsePipes, | |
| ValidationPipe, | |
| } from '@nestjs/common'; | |
| import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger'; | |
| import { WhatsappSession } from '@waha/core/abc/session.abc'; | |
| import { | |
| SessionApiParam, | |
| WorkingSessionParam, | |
| } from '@waha/nestjs/params/SessionApiParam'; | |
| import { CountResponse } from '@waha/structures/base.dto'; | |
| import { | |
| LidsListQueryParams, | |
| LidToPhoneNumber, | |
| } from '@waha/structures/lids.dto'; | |
| import { PaginationParams, SortOrder } from '@waha/structures/pagination.dto'; | |
| import { SessionManager } from '../core/abc/manager.abc'; | |
| ('api_key') | |
| ('api/:session/lids') | |
| ('👤 Contacts') | |
| export class LidsController { | |
| constructor(private manager: SessionManager) {} | |
| ('/') | |
| ({ summary: 'Get all known lids to phone number mapping' }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| async getAll( | |
| session: WhatsappSession, | |
| () params: LidsListQueryParams, | |
| ): Promise<Array<LidToPhoneNumber>> { | |
| // Always lid | |
| const pagination: PaginationParams = params; | |
| pagination.sortBy = 'lid'; | |
| pagination.sortOrder = SortOrder.ASC; | |
| const lids = await session.getAllLids(pagination); | |
| return lids; | |
| } | |
| ('/count') | |
| ({ summary: 'Get the number of known lids' }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| async getLidsCount( | |
| session: WhatsappSession, | |
| ): Promise<CountResponse> { | |
| const count = await session.getLidsCount(); | |
| return { | |
| count: count, | |
| }; | |
| } | |
| ('/:lid') | |
| ({ summary: 'Get phone number by lid' }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| async findPNByLid( | |
| session: WhatsappSession, | |
| ('lid') lid: string, | |
| ): Promise<LidToPhoneNumber> { | |
| if (!lid.includes('@')) { | |
| lid = lid + '@lid'; | |
| } | |
| if (!isLidUser(lid)) { | |
| throw new UnprocessableEntityException( | |
| 'Invalid LID - it must end with @lid', | |
| ); | |
| } | |
| const result = await session.findPNByLid(lid); | |
| result.pn = result.pn || null; | |
| return result; | |
| } | |
| ('/pn/:phoneNumber') | |
| ({ summary: 'Get lid by phone number (chat id)' }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| async findLIDByPhoneNumber( | |
| session: WhatsappSession, | |
| ('phoneNumber') phoneNumber: string, | |
| ): Promise<LidToPhoneNumber> { | |
| if (isLidUser(phoneNumber)) { | |
| return { | |
| lid: phoneNumber, | |
| pn: null, | |
| }; | |
| } | |
| const result = await session.findLIDByPhoneNumber(phoneNumber); | |
| result.lid = result.lid || null; | |
| return result; | |
| } | |
| } | |