mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-18 03:29:14 +00:00
feat: Get Orders and Products (#612)
* - 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>
This commit is contained in:
@@ -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');
|
||||
|
||||
/**
|
||||
@@ -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
|
||||
@@ -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>}
|
||||
@@ -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
src/structures/Order.js
Normal file
52
src/structures/Order.js
Normal file
@@ -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
src/structures/Product.js
Normal file
68
src/structures/Product.js
Normal file
@@ -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
src/structures/ProductMetadata.js
Normal file
25
src/structures/ProductMetadata.js
Normal file
@@ -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;
|
||||
@@ -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')
|
||||
};
|
||||
@@ -68,7 +68,9 @@ exports.MessageTypes = {
|
||||
LOCATION: 'location',
|
||||
CONTACT_CARD: 'vcard',
|
||||
CONTACT_CARD_MULTI: 'multi_vcard',
|
||||
ORDER: 'order',
|
||||
REVOKED: 'revoked',
|
||||
PRODUCT: 'product',
|
||||
UNKNOWN: 'unknown'
|
||||
};
|
||||
|
||||
|
||||
@@ -32,6 +32,9 @@ exports.ExposeStore = (moduleRaidStr) => {
|
||||
window.Store.Sticker = window.mR.findModule('Sticker')[0].default.Sticker;
|
||||
window.Store.UploadUtils = window.mR.findModule((module) => (module.default && module.default.encryptAndUpload) ? module.default : null)[0].default;
|
||||
window.Store.Label = window.mR.findModule('LabelCollection')[0].default;
|
||||
window.Store.QueryOrder = window.mR.findModule('queryOrder')[0];
|
||||
window.Store.QueryProduct = window.mR.findModule('queryProduct')[0];
|
||||
|
||||
};
|
||||
|
||||
exports.LoadUtils = () => {
|
||||
@@ -458,6 +461,20 @@ exports.LoadUtils = () => {
|
||||
const chat = await window.WWebJS.getChat(chatId);
|
||||
return (chat.labels || []).map(id => window.WWebJS.getLabel(id));
|
||||
};
|
||||
|
||||
window.WWebJS.getOrderDetail = async (orderId, token) => {
|
||||
return window.Store.QueryOrder.queryOrder(orderId, 80, 80, token);
|
||||
};
|
||||
|
||||
window.WWebJS.getProductMetadata = async (productId) => {
|
||||
let sellerId = window.Store.Conn.wid;
|
||||
let product = await window.Store.QueryProduct.queryProduct(sellerId, productId);
|
||||
if (product && product.data) {
|
||||
return product.data;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
};
|
||||
};
|
||||
|
||||
exports.MarkAllRead = () => {
|
||||
|
||||
Reference in New Issue
Block a user