File size: 2,979 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
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';

@ApiSecurity('api_key')
@Controller('api/:session/lids')
@ApiTags('👤 Contacts')
export class LidsController {
  constructor(private manager: SessionManager) {}

  @Get('/')
  @SessionApiParam
  @ApiOperation({ summary: 'Get all known lids to phone number mapping' })
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  async getAll(
    @WorkingSessionParam session: WhatsappSession,
    @Query() 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;
  }

  @Get('/count')
  @SessionApiParam
  @ApiOperation({ summary: 'Get the number of known lids' })
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  async getLidsCount(
    @WorkingSessionParam session: WhatsappSession,
  ): Promise<CountResponse> {
    const count = await session.getLidsCount();
    return {
      count: count,
    };
  }

  @Get('/:lid')
  @SessionApiParam
  @ApiOperation({ summary: 'Get phone number by lid' })
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  async findPNByLid(
    @WorkingSessionParam session: WhatsappSession,
    @Param('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;
  }

  @Get('/pn/:phoneNumber')
  @SessionApiParam
  @ApiOperation({ summary: 'Get lid by phone number (chat id)' })
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  async findLIDByPhoneNumber(
    @WorkingSessionParam session: WhatsappSession,
    @Param('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;
  }
}