File size: 8,799 Bytes
4c34106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * This file is part of WPPConnect.
 *
 * WPPConnect is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WPPConnect is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with WPPConnect.  If not, see <https://www.gnu.org/licenses/>.
 */

import { Page } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
import { evaluateAndReturn } from '../helpers';
import { HostLayer } from './host.layer';
import { CommunityLayer } from './community.layer';

export class CatalogLayer extends CommunityLayer {
  constructor(public page: Page, session?: string, options?: CreateConfig) {
    super(page, session, options);
  }

  /**
   * Create a product on catalog
   * @param name Product name
   * @param image Product image
   * @param description Product description
   * @param price Product price
   * @param isHidden Product visibility
   * @param url Product url
   * @param retailerId Product own ID system
   * @param currency Product currency
   * @example
   * ```javascript
   * client.createtProduct(
   *    'Product name',
   *    'image in base64',
   *    'product description',
   *    '89.90',
   *    true,
   *    'https://wppconnect.io',
   *    'AKA001',
   *   );
   * ```
   */
  public async createProduct(
    name: string,
    image: string,
    description: string,
    price: number,
    isHidden: boolean,
    url: string,
    retailerId: string,
    currency: string
  ) {
    return evaluateAndReturn(
      this.page,
      ({
        name,
        image,
        description,
        price,
        isHidden,
        url,
        retailerId,
        currency,
      }) =>
        WPP.catalog.createProduct({
          name,
          image,
          description,
          price,
          isHidden,
          url,
          retailerId,
          currency,
        }),
      { name, image, description, price, isHidden, url, retailerId, currency }
    );
  }
  /**
   * Querys all products
   * @param id Buisness profile id ('00000@c.us')
   * @param qnt limit to load products - Default: 10
   */
  public async getProducts(id: string, qnt: number) {
    return evaluateAndReturn(
      this.page,
      ({ id, qnt }) => WPP.catalog.getProducts(id, qnt),
      { id, qnt }
    );
  }

  /**
   * Create a new product on catalog
   * @param id Buisness profile id ('00000@c.us')
   * @param productId ID of Product
   */
  public async getProductById(id: string, productId: string) {
    return evaluateAndReturn(
      this.page,
      ({ id, productId }) => WPP.catalog.getProductById(id, productId),
      { id, productId }
    );
  }

  /**
   * Edit product on catalog
   * @param productId Product ID
   * @param options Object with options
   * @example
   * ```javascript
   * client.editProduct('56989897878' {
   *    name: 'Product name',
   *     description: 'product description',
   *     price: '89.90',
   *     isHidden: true,
   *     url: 'https://wppconnect.io',
   *     retailerId: 'AKA001',
   *   });
   * ```
   */
  public async editProduct(productId: string, options: string) {
    return evaluateAndReturn(
      this.page,
      ({ productId, options }) => WPP.catalog.editProduct(productId, options),
      { productId, options }
    );
  }
  /**
   * Delete product(s) on catalog
   * @param productsId Products ID
   * @example
   * ```javascript
   * //Delete one product
   * client.delProducts(['56989897878']);
   *
   * // Delete various products
   * client.delProducts(['56989897878','565657878']);
   * ```
   */
  public async delProducts(productsId: string[]) {
    return evaluateAndReturn(
      this.page,
      ({ productsId }) => WPP.catalog.delProducts(productsId),
      { productsId }
    );
  }

  /**
   * Add image on product This function change main image of product, for change additional images use client.addImage
   * @param productId Product ID
   * @param image Image in base64
   * @example
   * ```javascript
   * client.changeProductImage('56989897878', 'base64/string');
   * ```
   */
  public async changeProductImage(productId: string, image: string) {
    return evaluateAndReturn(
      this.page,
      ({ productId, image }) =>
        WPP.catalog.changeProductImage(productId, image),
      { productId, image }
    );
  }

  /**
   * Add image on product This function include additional images on product for change main image use client.changeProductImage
   * @param productId Product ID
   * @param image Image in base64
   * @example
   * ```javascript
   * client.addProductImage('56989897878', 'base64/string');
   * ```
   */
  public async addProductImage(productId: string, image: string) {
    return evaluateAndReturn(
      this.page,
      ({ productId, image }) => WPP.catalog.addProductImage(productId, image),
      { productId, image }
    );
  }

  /**
   * Remove image on product This function remove additional images of product for change main image use client.changeProductImage
   * @param productId Product ID
   * @param index Index array of additional imagens
   * @example
   * ```javascript
   * client.removeProductImage('56989897878', '1');
   * ```
   */
  public async removeProductImage(productId: string, index: string) {
    return evaluateAndReturn(
      this.page,
      ({ productId, index }) =>
        WPP.catalog.removeProductImage(productId, index),
      { productId, index }
    );
  }

  /**
   * Query all collections
   * @param id Product ID
   * @param qnt Max qnt collections - Default 10
   * @param maxProducts Max products in array products of collection - Default 10
   * @example
   * ```javascript
   * client.getCollections('5521988556558@c.us', '10','20');
   * ```
   */
  public async getCollections(id: string, qnt: string, maxProducts: string) {
    return evaluateAndReturn(
      this.page,
      ({ id, qnt, maxProducts }) =>
        WPP.catalog.getCollections(id, qnt, maxProducts),
      { id, qnt, maxProducts }
    );
  }

  /**
   * Create new collection
   * @param collectionName Product ID
   * @param productsId Index array of additional imagens
   * @example
   * ```javascript
   * client.createCollection('Name of collection', ['655632565','5689859898']);
   * ```
   */
  public async createCollection(collectionName: string, productsId: string) {
    return evaluateAndReturn(
      this.page,
      ({ collectionName, productsId }) =>
        WPP.catalog.createCollection(collectionName, productsId),
      { collectionName, productsId }
    );
  }

  /**
   * Edit a collection
   * @param collectionId Collection id
   * @param options Options arguments
   * @example
   * ```javascript
   * client.editCollection('656565898', {
   * collectionName: 'New Name for collection',
   * productsToAdd: ['5656523223'],
   * productsToRemove: ['5656523232']
   * });
   * ```
   */
  public async editCollection(collectionId: string, options: string) {
    return evaluateAndReturn(
      this.page,
      ({ collectionId, options }) =>
        WPP.catalog.editCollection(collectionId, options),
      { collectionId, options }
    );
  }

  /**
   * Delete a collection
   * @param collectionId Collection id
   * @param options Options arguments
   * @example
   * ```javascript
   * client.deleteCollection('65666565898');
   * ```
   */
  public async deleteCollection(collectionId: string) {
    return evaluateAndReturn(
      this.page,
      ({ collectionId }) => WPP.catalog.deleteCollection(collectionId),
      { collectionId }
    );
  }

  /**
   * Set product visibility
   * @param productId Product id
   * @param value True for visibility, false for non visible
   * @example
   * ```javascript
   * client.setProductVisibility('65666565898', false);
   * ```
   */
  public async setProductVisibility(productId: string, value: boolean) {
    return evaluateAndReturn(
      this.page,
      ({ productId, value }) =>
        WPP.catalog.setProductVisibility(productId, value),
      { productId, value }
    );
  }

  /**
   * Update options to customer cart your products
   * @param value True for enabled, false for non enabled
   * @example
   * ```javascript
   * client.updateCartEnabled(false);
   * ```
   */
  public async updateCartEnabled(value: boolean) {
    return evaluateAndReturn(
      this.page,
      ({ value }) => WPP.catalog.updateCartEnabled(value),
      { value }
    );
  }
}