From ff69726ec46520f25dde8940b4c5b52a90428775 Mon Sep 17 00:00:00 2001 From: Rajeh Taher <52720753+PurpShell@users.noreply.github.com> Date: Fri, 4 Jun 2021 20:35:48 +0300 Subject: [PATCH] Edit Business Profile (#499) * :sparkles: Added updateBusinessProfile * :art: parity of output between getBusinessProfile and updateBusinessProfile * :label: Added the Interfaces * :label: improved the type declarations * :goal_net: Fixed fatal error & Removed semicolon * :goal_net: Fixed one more error, and cleaned the code to be ready for release --- src/WAConnection/5.User.ts | 20 +++++++++++++++++--- src/WAConnection/Constants.ts | 29 ++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/WAConnection/5.User.ts b/src/WAConnection/5.User.ts index fc47951..d8d4036 100644 --- a/src/WAConnection/5.User.ts +++ b/src/WAConnection/5.User.ts @@ -80,6 +80,20 @@ export class WAConnection extends Base { this.emit ('contact-update', { jid: this.user.jid, status }) return response } + /** Updates business profile. */ + async updateBusinessProfile(profile: WABusinessProfile) { + if (profile.business_hours?.config) { + profile.business_hours.business_config = profile.business_hours.config + delete profile.business_hours.config + } + const json = ['action', "editBusinessProfile", {...profile, v: 2}] + try { + const response = await this.query({ json, expect200: true, requiresPhoneConnection: true }).catch(_ => throw new Error(_)) + } catch (_) { + return {status: 400} + } + return {status: response.status} + } async updateProfileName (name: string) { const response = (await this.setQuery ( [ @@ -214,7 +228,7 @@ export class WAConnection extends Base { /** * Query Business Profile (Useful for VCards) * @param jid Business Jid - * @returns profile object or undefined if not business account + * @returns {WABusinessProfile} profile object or undefined if not business account */ async getBusinessProfile(jid: string) { jid = whatsappID(jid) @@ -233,8 +247,8 @@ export class WAConnection extends Base { requiresPhoneConnection: false, }) return { - profile, - jid: whatsappID(wid), + ...profile, + wid: whatsappID(wid) } } } diff --git a/src/WAConnection/Constants.ts b/src/WAConnection/Constants.ts index 6594f95..4e3881f 100644 --- a/src/WAConnection/Constants.ts +++ b/src/WAConnection/Constants.ts @@ -31,6 +31,33 @@ export type WAInitResponse = { status: 200 } +export interface WABusinessProfile { + description: string + email: string + business_hours: WABusinessHours + website: string[] + categories: WABusinessCategories[] + wid?: string +} + +export type WABusinessCategories = { + id: string + localized_display_name: string +} + +export type WABusinessHours = { + timezone: string + config?: WABusinessHoursConfig[] + business_config?: WABusinessHoursConfig[] +} + +export type WABusinessHoursConfig = { + day_of_week: string + mode: string + open_time?: number + close_time?: number +} + export interface WALocationMessage { degreesLatitude: number degreesLongitude: number @@ -472,4 +499,4 @@ export type BaileysEvent = 'group-update' | 'received-pong' | 'blocklist-update' | - 'contact-update' \ No newline at end of file + 'contact-update'