Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@adiwajshing/baileys/lib/LegacySocket/business.js
1129 views
1
"use strict";
2
var __importDefault = (this && this.__importDefault) || function (mod) {
3
return (mod && mod.__esModule) ? mod : { "default": mod };
4
};
5
Object.defineProperty(exports, "__esModule", { value: true });
6
const business_1 = require("../Utils/business");
7
const groups_1 = __importDefault(require("./groups"));
8
const makeBusinessSocket = (config) => {
9
const sock = (0, groups_1.default)(config);
10
const { query, generateMessageTag, waUploadToServer, state } = sock;
11
const getCatalog = async (jid, limit = 10) => {
12
var _a, _b;
13
jid = jid || ((_b = (_a = state.legacy) === null || _a === void 0 ? void 0 : _a.user) === null || _b === void 0 ? void 0 : _b.id);
14
const result = await query({
15
expect200: true,
16
json: [
17
'query',
18
'bizCatalog',
19
{
20
allowShopSource: false,
21
catalogWid: jid,
22
height: 100,
23
width: 100,
24
limit,
25
stanza_id: generateMessageTag(true),
26
type: 'get_product_catalog_reh',
27
}
28
]
29
});
30
const products = result.data.data.map(mapProduct);
31
return {
32
beforeCursor: result.data.paging.cursors.before,
33
products
34
};
35
};
36
const productCreate = async (product) => {
37
const result = await query({
38
expect200: true,
39
json: [
40
'action',
41
'addProduct_reh',
42
await mapProductCreate(product)
43
]
44
});
45
return mapProduct(result.data.product);
46
};
47
const productDelete = async (productIds) => {
48
const result = await query({
49
expect200: true,
50
json: [
51
'action',
52
'deleteProduct_reh',
53
{
54
product_ids: productIds,
55
stanza_id: generateMessageTag(true),
56
}
57
]
58
});
59
return {
60
deleted: result.data.deleted_count
61
};
62
};
63
const productUpdate = async (productId, update) => {
64
const productCreate = await mapProductCreate({ ...update, originCountryCode: undefined }, false);
65
const result = await query({
66
expect200: true,
67
json: [
68
'action',
69
'editProduct_reh',
70
{
71
product_id: productId,
72
...productCreate
73
}
74
]
75
});
76
return mapProduct(result.data.product);
77
};
78
const getOrderDetails = async (orderId, tokenBase64) => {
79
const result = await query({
80
expect200: true,
81
json: [
82
'query',
83
'order',
84
{
85
id: generateMessageTag(true),
86
orderId,
87
imageWidth: '80',
88
imageHeight: '80',
89
token: tokenBase64
90
}
91
]
92
});
93
const data = result.data;
94
const order = {
95
price: {
96
currency: data.price.currency,
97
total: data.price.total,
98
},
99
products: data.products.map(p => {
100
var _a;
101
return ({
102
id: p.id,
103
imageUrl: (_a = p.image) === null || _a === void 0 ? void 0 : _a.url,
104
name: p.name,
105
quantity: +p.quantity,
106
currency: p.currency,
107
price: +p.price
108
});
109
})
110
};
111
return order;
112
};
113
// maps product create to send to WA
114
const mapProductCreate = async (product, mapCompliance = true) => {
115
const imgs = (await (0, business_1.uploadingNecessaryImages)(product.images, waUploadToServer)).map(img => img.url);
116
const result = {
117
name: product.name,
118
description: product.description,
119
image_url: imgs[0],
120
url: product.url || '',
121
additional_image_urls: imgs.slice(1),
122
retailer_id: product.retailerId || '',
123
width: '100',
124
height: '100',
125
stanza_id: generateMessageTag(true),
126
price: product.price.toString(),
127
currency: product.currency
128
};
129
if (mapCompliance) {
130
Object.assign(result, {
131
compliance_category: product.originCountryCode
132
? undefined :
133
'COUNTRY_ORIGIN_EXEMPT',
134
compliance_info: product.originCountryCode
135
? { country_code_origin: product.originCountryCode }
136
: undefined
137
});
138
}
139
return result;
140
};
141
return {
142
...sock,
143
getOrderDetails,
144
getCatalog,
145
productCreate,
146
productDelete,
147
productUpdate
148
};
149
};
150
const mapProduct = (item) => ({
151
id: item.id,
152
name: item.name,
153
retailerId: item.retailer_id,
154
price: +item.price,
155
description: item.description,
156
currency: item.currency,
157
imageUrls: item.image_cdn_urls.reduce((dict, { key, value }) => {
158
dict[key] = value;
159
return dict;
160
}, {}),
161
reviewStatus: item.capability_to_review_status.reduce((dict, { key, value }) => {
162
dict[key] = value;
163
return dict;
164
}, {}),
165
isHidden: item.is_hidden,
166
availability: item.availability
167
});
168
exports.default = makeBusinessSocket;
169
170