Skip to content

Commit

Permalink
feat: Get Orders and Products (pedroslopez#612)
Browse files Browse the repository at this point in the history
* - Get products and orders

* - Get products and orders

* - Eslint fixes

* - Eslint fixes

* allow downloading media for products

* products and orders work on normal accounts

Co-authored-by: Renato Jop <renato.jop@consystec-corp.com>
Co-authored-by: Pedro Lopez <pedroslopez@me.com>
Co-authored-by: Pedro S. Lopez <pslamoros@hotmail.com>
  • Loading branch information
4 people authored Jun 1, 2021
1 parent f506c17 commit 5177a25
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 5 deletions.
100 changes: 98 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ declare namespace WAWebJS {
CONTACT_CARD = 'vcard',
CONTACT_CARD_MULTI = 'multi_vcard',
REVOKED = 'revoked',
ORDER = 'order',
PRODUCT = 'product',
UNKNOWN = 'unknown',
}

Expand Down Expand Up @@ -521,7 +523,16 @@ declare namespace WAWebJS {
type: MessageTypes,
/** Links included in the message. */
links: string[],

/** Order ID */
orderId: string,
/** title */
title?: string,
/** description*/
description?: string,
/** Business Owner JID */
businessOwnerJid?: string,
/** Product JID */
productId?: string,
/** Deletes the message from the chat */
delete: (everyone?: boolean) => Promise<void>,
/** Downloads and returns the attatched message media */
Expand Down Expand Up @@ -549,7 +560,11 @@ declare namespace WAWebJS {
/** Unstar this message */
unstar: () => Promise<void>,
/** Get information about message delivery statuso */
getInfo: () => Promise<MessageInfo | null>
getInfo: () => Promise<MessageInfo | null>,
/**
* Gets the order associated with a given message
*/
getOrder: () => Order,
}

/** ID that represents a message */
Expand Down Expand Up @@ -917,6 +932,87 @@ declare namespace WAWebJS {
/** Makes the bot leave the group */
leave: () => Promise<void>;
}

/**
* Represents the metadata associated with a given product
*
*/
export interface ProductMetadata {
/** Product Id */
id: string,
/** Product Name */
name: string,
/** Product Description */
description: string,
/** Retailer ID */
retailer_id?: string
}

/**
* Represents a Product on Whatsapp
* @example
* {
* "id": "123456789",
* "price": "150000",
* "thumbnailId": "123456789",
* "thumbnailUrl": "https://mmg.whatsapp.net",
* "currency": "GTQ",
* "name": "Store Name",
* "quantity": 1
* }
*/
export interface Product {
/** Product Id */
id: string,
/** Price */
price?: string,
/** Product Thumbnail*/
thumbnailUrl: string,
/** Currency */
currency: string,
/** Product Name */
name: string,
/** Product Quantity*/
quantity: number,
/** Gets the Product metadata */
getData: () => Promise<ProductMetadata>
}

/**
* Represents a Order on WhatsApp
*
* @example
* {
* "products": [
* {
* "id": "123456789",
* "price": "150000",
* "thumbnailId": "123456789",
* "thumbnailUrl": "https://mmg.whatsapp.net",
* "currency": "GTQ",
* "name": "Store Name",
* "quantity": 1
* }
* ],
* "subtotal": "150000",
* "total": "150000",
* "currency": "GTQ",
* "createdAt": 1610136796,
* "sellerJid": "55555555@s.whatsapp.net"
* }
*/
export interface Order {
/** List of products*/
products: Array<Product>,
/** Order Subtotal */
subtotal: string,
/** Order Total */
total: string,
/** Order Currency */
currency: string,
/** Order Created At*/
createdAt: number;
}
}

export = WAWebJS
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ module.exports = {
BusinessContact: require('./src/structures/BusinessContact'),
ClientInfo: require('./src/structures/ClientInfo'),
Location: require('./src/structures/Location'),

ProductMetadata: require('./src/structures/ProductMetadata'),
...Constants
};
49 changes: 48 additions & 1 deletion src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const Base = require('./Base');
const MessageMedia = require('./MessageMedia');
const Location = require('./Location');
const Order = require('./Order');
const { MessageTypes } = require('../util/Constants');

/**
Expand Down Expand Up @@ -40,7 +41,7 @@ class Message extends Base {
* Indicates if the message has media available for download
* @type {boolean}
*/
this.hasMedia = data.clientUrl || data.deprecatedMms3Url ? true : false;
this.hasMedia = data.clientUrl || data.deprecatedMms3Url;

/**
* Message content
Expand Down Expand Up @@ -139,6 +140,37 @@ class Message extends Base {
this.mentionedIds = data.mentionedJidList;
}

/**
* Order ID for message type ORDER
* @type {string}
*/
this.orderId = data.orderId ? data.orderId : undefined;
/**
* Order Token for message type ORDER
* @type {string}
*/
this.token = data.token ? data.token : undefined;

/** Title */
if (data.title) {
this.title = data.title;
}

/** Description */
if (data.description) {
this.description = data.description;
}

/** Business Owner JID */
if (data.businessOwnerJid) {
this.businessOwnerJid = data.businessOwnerJid;
}

/** Product ID */
if (data.productId) {
this.productId = data.productId;
}

/**
* Links included in the message.
* @type {Array<string>}
Expand Down Expand Up @@ -342,6 +374,21 @@ class Message extends Base {

return info;
}

/**
* Gets the order associated with a given message
* @return {Promise<Order>}
*/
async getOrder() {
if (this.type === MessageTypes.ORDER) {
const result = await this.client.pupPage.evaluate((orderId, token) => {
return window.WWebJS.getOrderDetail(orderId, token);
}, this.orderId, this.token);
if (!result) return undefined;
return new Order(this.client, result);
}
return undefined;
}
}

module.exports = Message;
52 changes: 52 additions & 0 deletions src/structures/Order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

const Base = require('./Base');
const Product = require('./Product');

/**
* Represents a Order on WhatsApp
* @extends {Base}
*/
class Order extends Base {
constructor(client, data) {
super(client);

if (data) this._patch(data);
}

_patch(data) {
/**
* List of products
* @type {Array<Product>}
*/
if (data.products) {
this.products = data.products.map(product => new Product(this.client, product));
}
/**
* Order Subtotal
* @type {string}
*/
this.subtotal = data.subtotal;
/**
* Order Total
* @type {string}
*/
this.total = data.total;
/**
* Order Currency
* @type {string}
*/
this.currency = data.currency;
/**
* Order Created At
* @type {number}
*/
this.createdAt = data.createdAt;

return super._patch(data);
}


}

module.exports = Order;
68 changes: 68 additions & 0 deletions src/structures/Product.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

const Base = require('./Base');
const ProductMetadata = require('./ProductMetadata');

/**
* Represents a Product on WhatsAppBusiness
* @extends {Base}
*/
class Product extends Base {
constructor(client, data) {
super(client);

if (data) this._patch(data);
}

_patch(data) {
/**
* Product ID
* @type {string}
*/
this.id = data.id;
/**
* Price
* @type {string}
*/
this.price = data.price ? data.price : '';
/**
* Product Thumbnail
* @type {string}
*/
this.thumbnailUrl = data.thumbnailUrl;
/**
* Currency
* @type {string}
*/
this.currency = data.currency;
/**
* Product Name
* @type {string}
*/
this.name = data.name;
/**
* Product Quantity
* @type {number}
*/
this.quantity = data.quantity;
/** Product metadata */
this.data = null;
return super._patch(data);
}

async getData() {
if (this.data === null) {
let result = await this.client.pupPage.evaluate((productId) => {
return window.WWebJS.getProductMetadata(productId);
}, this.id);
if (!result) {
this.data = undefined;
} else {
this.data = new ProductMetadata(this.client, result);
}
}
return this.data;
}
}

module.exports = Product;
25 changes: 25 additions & 0 deletions src/structures/ProductMetadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const Base = require('./Base');

class ProductMetadata extends Base {
constructor(client, data) {
super(client);

if (data) this._patch(data);
}

_patch(data) {
/** Product ID */
this.id = data.id;
/** Retailer ID */
this.retailer_id = data.retailer_id;
/** Product Name */
this.name = data.name;
/** Product Description */
this.description = data.description;

return super._patch(data);
}

}

module.exports = ProductMetadata;
4 changes: 3 additions & 1 deletion src/structures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ module.exports = {
PrivateChat: require('./PrivateChat'),
PrivateContact: require('./PrivateContact'),
GroupNotification: require('./GroupNotification'),
Label: require('./Label.js')
Label: require('./Label.js'),
Order: require('./Order'),
Product: require('./Product')
};
2 changes: 2 additions & 0 deletions src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ exports.MessageTypes = {
LOCATION: 'location',
CONTACT_CARD: 'vcard',
CONTACT_CARD_MULTI: 'multi_vcard',
ORDER: 'order',
REVOKED: 'revoked',
PRODUCT: 'product',
UNKNOWN: 'unknown'
};

Expand Down
Loading

0 comments on commit 5177a25

Please sign in to comment.