File size: 5,879 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
/*
 * 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 { ControlsLayer } from './controls.layer';
import { CreateConfig } from '../../config/create-config';
import { evaluateAndReturn } from '../helpers';
import { BusinessProfileModel } from '@wppconnect/wa-js/dist/whatsapp';
import { Chat } from '../model';

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

  /**
   * Querys product catalog
   * @param id Buisness profile id ('00000@c.us')
   */
  public async getBusinessProfilesProducts(id: string) {
    return evaluateAndReturn(
      this.page,
      ({ id }) => WAPI.getBusinessProfilesProducts(id),
      { id }
    );
  }
  /**
   * Get Business Profile
   * @param id Buisness profile id ('00000@c.us')
   */
  public async getBusinessProfile(id: string) {
    return evaluateAndReturn(
      this.page,
      async ({ id }) => {
        return JSON.parse(
          JSON.stringify(await WPP.contact.getBusinessProfile(id))
        );
      },
      { id }
    );
  }
  /**
   * Querys order catalog
   * @param messageId string
   * @returns Message object
   */
  public async getOrderbyMsg(messageId: string) {
    return evaluateAndReturn(
      this.page,
      ({ messageId }) => WAPI.getOrderbyMsg(messageId),
      { messageId }
    );
  }

  /**
 * Update your business profile
 *
 * @example
 * ```javascript
 * await client.editBusinessProfile({description: 'New description for profile'});
 * ```
 *
 * ```javascript
 * await client.editBusinessProfile({categories: {
    id: "133436743388217",
    localized_display_name: "Artes e entretenimento",
    not_a_biz: false,
  }});
 * ```
 *
 * ```javascript
 * await client.editBusinessProfile({address: 'Street 01, New York'});
 * ```
 *
 * ```javascript
 * await client.editBusinessProfile({email: 'test@test.com.br'});
 * ```
 *
 * Change website of profile (max 2 sites)
 * ```javascript
 * await client.editBusinessProfile({website: [
  "https://www.wppconnect.io",
  "https://www.teste2.com.br",
]});
 * ```
 *
 * Change businessHours for Specific Hours
 * ```javascript
 * await client.editBusinessProfile({ businessHours: {
 * {
      tue: {
        mode: "specific_hours",
        hours: [
          [
            540,
            1080,
          ],
        ],
      },
      wed: {
        mode: "specific_hours",
        hours: [
          [
            540,
            1080,
          ],
        ],
      },
      thu: {
        mode: "specific_hours",
        hours: [
          [
            540,
            1080,
          ],
        ],
      },
      fri: {
        mode: "specific_hours",
        hours: [
          [
            540,
            1080,
          ],
        ],
      },
      sat: {
        mode: "specific_hours",
        hours: [
          [
            540,
            1080,
          ],
        ],
      },
      sun: {
        mode: "specific_hours",
        hours: [
          [
            540,
            1080,
          ],
        ],
      },
    }
  },
  timezone: "America/Sao_Paulo"
  });
 *
 * Change businessHours for Always Opened
 * ```javascript
 * await client.editBusinessProfile({ businessHours: {
    {
      mon: {
        mode: "open_24h",
      },
      tue: {
        mode: "open_24h",
      },
      wed: {
        mode: "open_24h",
      },
      thu: {
        mode: "open_24h",
      },
      fri: {
        mode: "open_24h",
      },
      sat: {
        mode: "open_24h",
      },
      sun: {
        mode: "open_24h",
      },
    }
    timezone: "America/Sao_Paulo"
  });
 *
 * Change businessHours for Appointment Only
 * ```javascript
 * await client.editBusinessProfile({ businessHours: { {
    mon: {
      mode: "appointment_only",
    },
    tue: {
      mode: "appointment_only",
    },
    wed: {
      mode: "appointment_only",
    },
    thu: {
      mode: "appointment_only",
    },
    fri: {
      mode: "appointment_only",
    },
    sat: {
      mode: "appointment_only",
    },
    sun: {
      mode: "appointment_only",
    },
  }
    timezone: "America/Sao_Paulo"
  });
 *
 *
 * ```
 */
  public async editBusinessProfile(options: any) {
    return await evaluateAndReturn(
      this.page,
      async ({ options }) => {
        return JSON.parse(
          JSON.stringify(await WPP.profile.editBusinessProfile(options))
        );
      },
      { options }
    );
  }

  /**
   * Sends product with product image to given chat id
   * @param to Chat id
   * @param base64 Base64 image data
   * @param caption Message body
   * @param businessId Business id number that owns the product ('0000@c.us')
   * @param productId Product id, see method getBusinessProfilesProducts for more info
   */
  public async sendImageWithProduct(
    to: string,
    base64: string,
    caption: string,
    businessId: string,
    productId: string
  ) {
    return evaluateAndReturn(
      this.page,
      ({ to, base64, businessId, caption, productId }) => {
        WAPI.sendImageWithProduct(base64, to, caption, businessId, productId);
      },
      { to, base64, businessId, caption, productId }
    );
  }
}