File size: 2,767 Bytes
46252cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

declare(strict_types=1);

namespace OpenWA\Resources;

use OpenWA\Http\HttpExecutor;

/**
 * Catalog resource — WhatsApp Business catalog, products, and product/catalog sends.
 *
 * Backed by src/modules/catalog/catalog.controller.ts (@Controller('sessions/:sessionId')).
 * NOTE: the catalog controller is mounted under the session root, so catalog
 * reads are /catalog... while product/catalog SENDS share the messages namespace
 * (/messages/send-product, /messages/send-catalog).
 */
class CatalogResource
{
    private HttpExecutor $http;

    public function __construct(HttpExecutor $http)
    {
        $this->http = $http;
    }

    /** @return array<string,mixed> */
    public function info(string $sessionId): array
    {
        return $this->http->request('GET', "/api/sessions/{$this->http->encodeSegment($sessionId)}/catalog");
    }

    /**
     * List catalog products (paginated). Returns a page shaped
     * {products: list, pagination: {page, limit, total, totalPages}} — iterate over the `products`
     * field, not the result itself. Consistent with the JavaScript, Python, and Java SDKs.
     *
     * @param array<string,mixed> $query  e.g. ['page' => 1, 'limit' => 20].
     * @return array{products: array<int,array<string,mixed>>, pagination: array<string,mixed>}
     */
    public function products(string $sessionId, array $query = []): array
    {
        return $this->http->request('GET', "/api/sessions/{$this->http->encodeSegment($sessionId)}/catalog/products", $query)
            ?? ['products' => [], 'pagination' => []];
    }

    /** @return array<string,mixed> */
    public function product(string $sessionId, string $productId): array
    {
        return $this->http->request('GET', "/api/sessions/{$this->http->encodeSegment($sessionId)}/catalog/products/{$this->http->encodeSegment($productId)}");
    }

    /**
     * Send a product message. Requires an OPERATOR-level key. Shares the messages path.
     *
     * @param array<string,mixed> $body  chatId + productId required; body optional.
     * @return array<string,mixed>
     */
    public function sendProduct(string $sessionId, array $body): array
    {
        return $this->http->request('POST', "/api/sessions/{$this->http->encodeSegment($sessionId)}/messages/send-product", [], $body);
    }

    /**
     * Send a catalog link message. Requires an OPERATOR-level key. Shares the messages path.
     *
     * @param array<string,mixed> $body  chatId required; body optional.
     * @return array<string,mixed>
     */
    public function sendCatalog(string $sessionId, array $body): array
    {
        return $this->http->request('POST', "/api/sessions/{$this->http->encodeSegment($sessionId)}/messages/send-catalog", [], $body);
    }
}