Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/business.js
1129 views
1
"use strict";
2
Object.defineProperty(exports, "__esModule", { value: true });
3
exports.uploadingNecessaryImages = exports.uploadingNecessaryImagesOfProduct = exports.parseProductNode = exports.toProductNode = exports.parseOrderDetailsNode = exports.parseCollectionsNode = exports.parseCatalogNode = void 0;
4
const boom_1 = require("@hapi/boom");
5
const crypto_1 = require("crypto");
6
const WABinary_1 = require("../WABinary");
7
const messages_media_1 = require("./messages-media");
8
const parseCatalogNode = (node) => {
9
const catalogNode = (0, WABinary_1.getBinaryNodeChild)(node, 'product_catalog');
10
const products = (0, WABinary_1.getBinaryNodeChildren)(catalogNode, 'product').map(exports.parseProductNode);
11
return { products };
12
};
13
exports.parseCatalogNode = parseCatalogNode;
14
const parseCollectionsNode = (node) => {
15
const collectionsNode = (0, WABinary_1.getBinaryNodeChild)(node, 'collections');
16
const collections = (0, WABinary_1.getBinaryNodeChildren)(collectionsNode, 'collection').map(collectionNode => {
17
const id = (0, WABinary_1.getBinaryNodeChildString)(collectionNode, 'id');
18
const name = (0, WABinary_1.getBinaryNodeChildString)(collectionNode, 'name');
19
const products = (0, WABinary_1.getBinaryNodeChildren)(collectionNode, 'product').map(exports.parseProductNode);
20
return {
21
id,
22
name,
23
products,
24
status: parseStatusInfo(collectionNode)
25
};
26
});
27
return {
28
collections
29
};
30
};
31
exports.parseCollectionsNode = parseCollectionsNode;
32
const parseOrderDetailsNode = (node) => {
33
const orderNode = (0, WABinary_1.getBinaryNodeChild)(node, 'order');
34
const products = (0, WABinary_1.getBinaryNodeChildren)(orderNode, 'product').map(productNode => {
35
const imageNode = (0, WABinary_1.getBinaryNodeChild)(productNode, 'image');
36
return {
37
id: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'id'),
38
name: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'name'),
39
imageUrl: (0, WABinary_1.getBinaryNodeChildString)(imageNode, 'url'),
40
price: +(0, WABinary_1.getBinaryNodeChildString)(productNode, 'price'),
41
currency: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'currency'),
42
quantity: +(0, WABinary_1.getBinaryNodeChildString)(productNode, 'quantity')
43
};
44
});
45
const priceNode = (0, WABinary_1.getBinaryNodeChild)(orderNode, 'price');
46
const orderDetails = {
47
price: {
48
total: +(0, WABinary_1.getBinaryNodeChildString)(priceNode, 'total'),
49
currency: (0, WABinary_1.getBinaryNodeChildString)(priceNode, 'currency'),
50
},
51
products
52
};
53
return orderDetails;
54
};
55
exports.parseOrderDetailsNode = parseOrderDetailsNode;
56
const toProductNode = (productId, product) => {
57
const attrs = {};
58
const content = [];
59
if (typeof productId !== 'undefined') {
60
content.push({
61
tag: 'id',
62
attrs: {},
63
content: Buffer.from(productId)
64
});
65
}
66
if (typeof product.name !== 'undefined') {
67
content.push({
68
tag: 'name',
69
attrs: {},
70
content: Buffer.from(product.name)
71
});
72
}
73
if (typeof product.description !== 'undefined') {
74
content.push({
75
tag: 'description',
76
attrs: {},
77
content: Buffer.from(product.description)
78
});
79
}
80
if (typeof product.retailerId !== 'undefined') {
81
content.push({
82
tag: 'retailer_id',
83
attrs: {},
84
content: Buffer.from(product.retailerId)
85
});
86
}
87
if (product.images.length) {
88
content.push({
89
tag: 'media',
90
attrs: {},
91
content: product.images.map(img => {
92
if (!('url' in img)) {
93
throw new boom_1.Boom('Expected img for product to already be uploaded', { statusCode: 400 });
94
}
95
return {
96
tag: 'image',
97
attrs: {},
98
content: [
99
{
100
tag: 'url',
101
attrs: {},
102
content: Buffer.from(img.url.toString())
103
}
104
]
105
};
106
})
107
});
108
}
109
if (typeof product.price !== 'undefined') {
110
content.push({
111
tag: 'price',
112
attrs: {},
113
content: Buffer.from(product.price.toString())
114
});
115
}
116
if (typeof product.currency !== 'undefined') {
117
content.push({
118
tag: 'currency',
119
attrs: {},
120
content: Buffer.from(product.currency)
121
});
122
}
123
if ('originCountryCode' in product) {
124
if (typeof product.originCountryCode === 'undefined') {
125
attrs.compliance_category = 'COUNTRY_ORIGIN_EXEMPT';
126
}
127
else {
128
content.push({
129
tag: 'compliance_info',
130
attrs: {},
131
content: [
132
{
133
tag: 'country_code_origin',
134
attrs: {},
135
content: Buffer.from(product.originCountryCode)
136
}
137
]
138
});
139
}
140
}
141
if (typeof product.isHidden !== 'undefined') {
142
attrs.is_hidden = product.isHidden.toString();
143
}
144
const node = {
145
tag: 'product',
146
attrs,
147
content
148
};
149
return node;
150
};
151
exports.toProductNode = toProductNode;
152
const parseProductNode = (productNode) => {
153
const isHidden = productNode.attrs.is_hidden === 'true';
154
const id = (0, WABinary_1.getBinaryNodeChildString)(productNode, 'id');
155
const mediaNode = (0, WABinary_1.getBinaryNodeChild)(productNode, 'media');
156
const statusInfoNode = (0, WABinary_1.getBinaryNodeChild)(productNode, 'status_info');
157
const product = {
158
id,
159
imageUrls: parseImageUrls(mediaNode),
160
reviewStatus: {
161
whatsapp: (0, WABinary_1.getBinaryNodeChildString)(statusInfoNode, 'status'),
162
},
163
availability: 'in stock',
164
name: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'name'),
165
retailerId: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'retailer_id'),
166
url: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'url'),
167
description: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'description'),
168
price: +(0, WABinary_1.getBinaryNodeChildString)(productNode, 'price'),
169
currency: (0, WABinary_1.getBinaryNodeChildString)(productNode, 'currency'),
170
isHidden,
171
};
172
return product;
173
};
174
exports.parseProductNode = parseProductNode;
175
/**
176
* Uploads images not already uploaded to WA's servers
177
*/
178
async function uploadingNecessaryImagesOfProduct(product, waUploadToServer, timeoutMs = 30000) {
179
product = {
180
...product,
181
images: product.images ? await (0, exports.uploadingNecessaryImages)(product.images, waUploadToServer, timeoutMs) : product.images
182
};
183
return product;
184
}
185
exports.uploadingNecessaryImagesOfProduct = uploadingNecessaryImagesOfProduct;
186
/**
187
* Uploads images not already uploaded to WA's servers
188
*/
189
const uploadingNecessaryImages = async (images, waUploadToServer, timeoutMs = 30000) => {
190
const results = await Promise.all(images.map(async (img) => {
191
if ('url' in img) {
192
const url = img.url.toString();
193
if (url.includes('.whatsapp.net')) {
194
return { url };
195
}
196
}
197
const { stream } = await (0, messages_media_1.getStream)(img);
198
const hasher = (0, crypto_1.createHash)('sha256');
199
const contentBlocks = [];
200
for await (const block of stream) {
201
hasher.update(block);
202
contentBlocks.push(block);
203
}
204
const sha = hasher.digest('base64');
205
const { mediaUrl } = await waUploadToServer((0, messages_media_1.toReadable)(Buffer.concat(contentBlocks)), { mediaType: 'image', fileEncSha256B64: sha, timeoutMs });
206
return { url: mediaUrl };
207
}));
208
return results;
209
};
210
exports.uploadingNecessaryImages = uploadingNecessaryImages;
211
const parseImageUrls = (mediaNode) => {
212
const imgNode = (0, WABinary_1.getBinaryNodeChild)(mediaNode, 'image');
213
return {
214
requested: (0, WABinary_1.getBinaryNodeChildString)(imgNode, 'request_image_url'),
215
original: (0, WABinary_1.getBinaryNodeChildString)(imgNode, 'original_image_url')
216
};
217
};
218
const parseStatusInfo = (mediaNode) => {
219
const node = (0, WABinary_1.getBinaryNodeChild)(mediaNode, 'status_info');
220
return {
221
status: (0, WABinary_1.getBinaryNodeChildString)(node, 'status'),
222
canAppeal: (0, WABinary_1.getBinaryNodeChildString)(node, 'can_appeal') === 'true',
223
};
224
};
225
226