dcos-core-monalisav2-latam 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +130 -0
- package/index.js +350 -0
- package/package.json +52 -0
- package/src/auth/handler.js +3 -0
- package/src/common/MondelezCastOrder.js +449 -0
- package/src/common/utils/AuthSecurity.js +46 -0
- package/src/common/utils/account-error-handler.js +279 -0
- package/src/common/utils/account-error-helper.js +231 -0
- package/src/common/utils/account-properties-handler.js +355 -0
- package/src/common/utils/api-response.js +62 -0
- package/src/common/utils/aws-services.js +186 -0
- package/src/common/utils/constants/account-error-codes.json +801 -0
- package/src/common/utils/constants.js +37 -0
- package/src/common/utils/convert/MondelezClientsItemsCast.js +52 -0
- package/src/common/utils/convert/MondelezInventoryItemsCast.js +15 -0
- package/src/common/utils/convert/MondelezOrderStatusCast.js +34 -0
- package/src/common/utils/convert/MondelezPricesItemsCast.js +37 -0
- package/src/common/utils/cron-ftp-get.js +143 -0
- package/src/common/utils/data-tables-helper.js +213 -0
- package/src/common/utils/date-range-calculator.js +113 -0
- package/src/common/utils/delay.js +17 -0
- package/src/common/utils/ftp-sftp.js +320 -0
- package/src/common/utils/logger.js +126 -0
- package/src/common/utils/nodemailerLib.js +61 -0
- package/src/common/utils/product-unit-converter.js +168 -0
- package/src/common/utils/schemas-utils.js +101 -0
- package/src/common/utils/seller-email-sharing-service.js +441 -0
- package/src/common/utils/sftp-utils.js +202 -0
- package/src/common/utils/status.js +15 -0
- package/src/common/utils/util.js +236 -0
- package/src/common/utils/validate-state-order.js +35 -0
- package/src/common/utils/validateProviders.js +67 -0
- package/src/common/utils/validation-data.js +45 -0
- package/src/common/utils/vtex/save-hooks.js +65 -0
- package/src/common/utils/vtex/save-schemas.js +65 -0
- package/src/common/utils/vtex-hook-handler.js +71 -0
- package/src/common/validation/AccountCoordinatesValidation.js +350 -0
- package/src/common/validation/GeneralErrorValidation.js +11 -0
- package/src/common/validation/MainErrorValidation.js +8 -0
- package/src/entities/account.js +639 -0
- package/src/entities/clients.js +104 -0
- package/src/entities/controlprice.js +196 -0
- package/src/entities/controlstock.js +206 -0
- package/src/entities/cron.js +77 -0
- package/src/entities/cronjob.js +71 -0
- package/src/entities/orders.js +195 -0
- package/src/entities/sftp-inbound.js +88 -0
- package/src/entities/sku.js +220 -0
- package/src/entities/taxpromotion.js +249 -0
- package/src/functions/account/account-get.js +262 -0
- package/src/functions/account/account-handler.js +299 -0
- package/src/functions/account/clients.js +10 -0
- package/src/functions/account/index.js +208 -0
- package/src/functions/actions/save-promotions-order-history.js +324 -0
- package/src/functions/affiliates/affiliates-hook-consumer.js +87 -0
- package/src/functions/affiliates/affiliates-hook-producer.js +45 -0
- package/src/functions/clients/clients-audience.js +62 -0
- package/src/functions/clients/clients-consumer.js +648 -0
- package/src/functions/clients/clients-producer.js +362 -0
- package/src/functions/clients/clients-suggested-product-consumer.js +166 -0
- package/src/functions/clients/helpers/suggested-product-mdlz.js +233 -0
- package/src/functions/clients_peru/email.html +129 -0
- package/src/functions/clients_peru/splitfile.js +357 -0
- package/src/functions/clients_peru/updateClients.js +1334 -0
- package/src/functions/clients_peru/utils.js +243 -0
- package/src/functions/cronjobs/cron-jobs-manager.js +40 -0
- package/src/functions/cronjobs/cron-jobs.js +171 -0
- package/src/functions/crons/cron.js +39 -0
- package/src/functions/distributors/distributor-handler.js +81 -0
- package/src/functions/distributors/distributor.js +535 -0
- package/src/functions/distributors/index.js +60 -0
- package/src/functions/financialpolicy/assign-financialpolicy.js +111 -0
- package/src/functions/financialpolicy/get-financialpolicy.js +91 -0
- package/src/functions/financialpolicy/index.js +28 -0
- package/src/functions/inventory/catalog-sync-consumer.js +17 -0
- package/src/functions/inventory/catalog-sync-handler.js +311 -0
- package/src/functions/inventory/inventory-consumer.js +119 -0
- package/src/functions/inventory/inventory-producer.js +197 -0
- package/src/functions/multiPresentation/multipre-queue.js +155 -0
- package/src/functions/multiPresentation/multipres.js +459 -0
- package/src/functions/nodeflow/index.js +83 -0
- package/src/functions/nodeflow/nodeflow-cron.js +200 -0
- package/src/functions/nodeflow/nodeflow-pub.js +203 -0
- package/src/functions/nodeflow/nodeflow-pvt.js +266 -0
- package/src/functions/notifications/download-leads-handler.js +67 -0
- package/src/functions/notifications/new-leads-notification-consumer.js +17 -0
- package/src/functions/notifications/new-leads-notification-handler.js +359 -0
- package/src/functions/notifications/order-status-notification-handler.js +482 -0
- package/src/functions/notifications/promotion-notification-handler.js +193 -0
- package/src/functions/orders/index.js +32 -0
- package/src/functions/orders/orders-cancel-handler.js +74 -0
- package/src/functions/orders/orders-handler.js +280 -0
- package/src/functions/orders/orders-hook-consumer.js +137 -0
- package/src/functions/orders/orders-hook-producer.js +170 -0
- package/src/functions/orders/orders-notifications-handler.js +137 -0
- package/src/functions/orders/orders-status-consumer.js +461 -0
- package/src/functions/orders/orders-status-producer.js +443 -0
- package/src/functions/prices/index.js +75 -0
- package/src/functions/prices/prices-consumer.js +236 -0
- package/src/functions/prices/prices-producer.js +323 -0
- package/src/functions/prices/promotion-and-tax.js +1284 -0
- package/src/functions/routesflow/assign-routeflow-queue.js +77 -0
- package/src/functions/schemas/vtex/handle-schemas.js +102 -0
- package/src/functions/security/process_gas.js +221 -0
- package/src/functions/security/security-handler.js +950 -0
- package/src/functions/sftp/sftp-consumer.js +453 -0
- package/src/functions/sftpIntegrations/processes/redirectServices.js +184 -0
- package/src/functions/sftpIntegrations/processes/validateFileSchema.js +226 -0
- package/src/functions/sftpIntegrations/schemas/credential-schema.js +123 -0
- package/src/functions/sftpIntegrations/schemas/record-schema.js +131 -0
- package/src/functions/sftpIntegrations/schemas/sftp_required_fields.json +3 -0
- package/src/functions/sftpIntegrations/sftp-config-producer.js +112 -0
- package/src/functions/sftpIntegrations/sftp-consumer.js +700 -0
- package/src/functions/sftpIntegrations/test/validateFile.test.js +122 -0
- package/src/functions/sftpIntegrations/utils/connect-dynamo.js +29 -0
- package/src/functions/sftpIntegrations/utils/split-data.js +25 -0
- package/src/functions/utils/index.js +130 -0
- package/src/functions/vtex/vtex-helpers.js +694 -0
- package/src/integrations/accountErrors/AccountErrorManager.js +437 -0
- package/src/integrations/audience/Audience.js +70 -0
- package/src/integrations/financialPolicy/FinancialPolicyApi.js +377 -0
- package/src/integrations/index.js +0 -0
- package/src/integrations/mobilvendor/MobilvendorApi.js +405 -0
- package/src/integrations/productmultipresentation/ProductMultiPresentation.js +200 -0
- package/src/mdlz/auth/SecretManagerApi.js +77 -0
- package/src/mdlz/client/MdlzApi.js +70 -0
- package/src/vtex/clients/ProvidersApi.js +51 -0
- package/src/vtex/clients/VtexApi.js +511 -0
- package/src/vtex/models/VtexOrder.js +87 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
const VtexApi = require("../../vtex/clients/VtexApi")
|
|
2
|
+
const AWSServices = require("../../common/utils/aws-services")
|
|
3
|
+
const SkuData = require("../../entities/sku")
|
|
4
|
+
const { roundPrice } = require("../../common/utils/util");
|
|
5
|
+
const SQS_PRICES_DQL_QUEUE_URL = process.env.SQS_INVENTORY_DQL_QUEUE_URL
|
|
6
|
+
const SQS_PRICES_QUEUE_URL = process.env.SQS_INVENTORY_QUEUE_URL
|
|
7
|
+
const MAX_ATTEMPS = process.env.MAX_ATTEMPS || 3
|
|
8
|
+
const CotrolPrice = require("../../entities/controlprice")
|
|
9
|
+
const Logger = require("../../common/utils/logger");
|
|
10
|
+
|
|
11
|
+
const consumer = async (event) => {
|
|
12
|
+
for (const record of event.Records) {
|
|
13
|
+
Logger.debug("Message Body: ", record.body, ". Type: ", typeof record.body)
|
|
14
|
+
|
|
15
|
+
let { an, key, token, hoursValidStockPrice, ValidateSkuPriceInventoryInProcess, ParentAccountName, SkuRefUsed, RoundUpDecimalPrice } = JSON.parse(record.body)
|
|
16
|
+
let { data } = JSON.parse(record.body)
|
|
17
|
+
|
|
18
|
+
// obtener sku vtex
|
|
19
|
+
let skuInprocess= null
|
|
20
|
+
|
|
21
|
+
if (ValidateSkuPriceInventoryInProcess === true) {
|
|
22
|
+
try{
|
|
23
|
+
skuInprocess= await SkuData.getSkuVtex({ account: ParentAccountName, sku: data?.sku, SkuRefUsed })
|
|
24
|
+
|
|
25
|
+
} catch(error) {
|
|
26
|
+
Logger.error('Error get sku vtex.', error)
|
|
27
|
+
|
|
28
|
+
try{
|
|
29
|
+
await SkuData.saveLogProductsNotFount({ an, key, token, entity: "logpricesnotfound", sku: data?.sku, value: JSON.stringify(data) })
|
|
30
|
+
}catch(e){
|
|
31
|
+
Logger.error('error save log price')
|
|
32
|
+
}
|
|
33
|
+
return false
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
const vtexApi = new VtexApi(an, key, token)
|
|
39
|
+
const skuId = skuInprocess ? skuInprocess : data?.sku
|
|
40
|
+
let priceM = 0
|
|
41
|
+
|
|
42
|
+
delete data.sku
|
|
43
|
+
|
|
44
|
+
// Encontrar precio máximo redondeado
|
|
45
|
+
for (let i = 0; i < data.fixedPrices.length; i++) {
|
|
46
|
+
if(priceM < data.fixedPrices[i]?.value) {
|
|
47
|
+
priceM = data.fixedPrices[i]?.value ?? 0
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if((!data?.basePrice && priceM > 0) || (data?.basePrice === 0 && priceM > 0)){
|
|
52
|
+
data.basePrice = priceM;
|
|
53
|
+
if(data?.costPrice) {
|
|
54
|
+
data.costPrice = priceM;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Validacion valores ya informados
|
|
59
|
+
const validPriceInternal= await CotrolPrice?.validNotificationPriceVtex({
|
|
60
|
+
skuId: skuId,
|
|
61
|
+
account: an,
|
|
62
|
+
hoursUpdate: hoursValidStockPrice ?? 24,
|
|
63
|
+
data:data
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
if(validPriceInternal === false){
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
// fin validacion valores informados
|
|
70
|
+
|
|
71
|
+
let response= { status: 200 }
|
|
72
|
+
|
|
73
|
+
const isGeneralPricing = data?.sc || data?.priceTableId ? false : true;
|
|
74
|
+
|
|
75
|
+
response = isGeneralPricing
|
|
76
|
+
? await setPricesGeneral(data, vtexApi, skuId, RoundUpDecimalPrice)
|
|
77
|
+
: await setPricesPerTableId(data, vtexApi, skuId, RoundUpDecimalPrice);
|
|
78
|
+
|
|
79
|
+
if (response.status >= 200 && response.status < 300) {
|
|
80
|
+
// Fin Proceso de impuestos
|
|
81
|
+
} else {
|
|
82
|
+
const attemps = JSON.parse(record.body)?.attemps || 0;
|
|
83
|
+
Logger.error( "Error updating prices. Status: ", response.status,". Message: ", response?.data?.message, " - Try: ", attemps)
|
|
84
|
+
|
|
85
|
+
if (attemps < MAX_ATTEMPS) {
|
|
86
|
+
await AWSServices.sendSQSMessage(SQS_PRICES_QUEUE_URL,
|
|
87
|
+
JSON.stringify({
|
|
88
|
+
an,
|
|
89
|
+
authKey,
|
|
90
|
+
token,
|
|
91
|
+
data,
|
|
92
|
+
attemps: attemps + 1,
|
|
93
|
+
})
|
|
94
|
+
)
|
|
95
|
+
} else {
|
|
96
|
+
throw new Error("Max attemps reached.");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
} catch (error) {
|
|
100
|
+
Logger.error(error)
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
await AWSServices.sendSQSMessage(
|
|
104
|
+
SQS_PRICES_DQL_QUEUE_URL,
|
|
105
|
+
JSON.stringify({
|
|
106
|
+
body: record.body,
|
|
107
|
+
error: error?.message,
|
|
108
|
+
})
|
|
109
|
+
)
|
|
110
|
+
} catch (error) {
|
|
111
|
+
Logger.error("Sending message to DQL Queue: ", error)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const setPricesGeneral = async (data, vtexApi, skuId, RoundUpDecimalPrice) => {
|
|
118
|
+
let response = { status: 200 }
|
|
119
|
+
|
|
120
|
+
// rounded prices
|
|
121
|
+
applyRoundUpDecimalPrice(data, RoundUpDecimalPrice);
|
|
122
|
+
|
|
123
|
+
if (data?.basePrice > 0) {
|
|
124
|
+
let fixedPricesSave = []
|
|
125
|
+
|
|
126
|
+
for (let x = 0; x < data?.fixedPrices?.length; x++) {
|
|
127
|
+
if (data?.fixedPrices[x]?.value > 0) {
|
|
128
|
+
fixedPricesSave = [...fixedPricesSave, data?.fixedPrices[x]]
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
response = await vtexApi.fetchApiURL(`/pricing/prices/${skuId}/`, {
|
|
134
|
+
method: "PUT",
|
|
135
|
+
data: {
|
|
136
|
+
basePrice: data.basePrice,
|
|
137
|
+
costPrice: data.costPrice ?? data.basePrice,
|
|
138
|
+
fixedPrices: fixedPricesSave,
|
|
139
|
+
},
|
|
140
|
+
})
|
|
141
|
+
return response
|
|
142
|
+
} catch (error) {
|
|
143
|
+
Logger.error('response update price', error)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
} else {
|
|
148
|
+
|
|
149
|
+
try{
|
|
150
|
+
const response = await vtexApi.fetchApiURL(`/pricing/prices/${skuId}/`, {
|
|
151
|
+
method: "DELETE"
|
|
152
|
+
})
|
|
153
|
+
return response
|
|
154
|
+
|
|
155
|
+
} catch (error) {
|
|
156
|
+
Logger.error('response Delete price error', error)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return response
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const setPricesPerTableId = async (data, vtexApi, skuId, RoundUpDecimalPrice) => {
|
|
163
|
+
let response = { status: 200 }
|
|
164
|
+
|
|
165
|
+
// rounded prices
|
|
166
|
+
applyRoundUpDecimalPrice(data, RoundUpDecimalPrice);
|
|
167
|
+
|
|
168
|
+
if (!data?.sc && !data?.priceTableId) {
|
|
169
|
+
Logger.error('No exists sc or priceTableId properties')
|
|
170
|
+
return { status: 404, data: { message: 'No exists sc or priceTableId propertie' } }
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
let priceTableId = ''
|
|
174
|
+
if (data?.sc){
|
|
175
|
+
priceTableId = data?.sc
|
|
176
|
+
}else{
|
|
177
|
+
priceTableId = data?.priceTableId
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (data?.basePrice > 0) {
|
|
181
|
+
try {
|
|
182
|
+
response = await vtexApi.fetchApiURL(`/pricing/prices/${skuId}/fixed/${priceTableId}`, {
|
|
183
|
+
method: "POST",
|
|
184
|
+
data: [{
|
|
185
|
+
value: data?.basePrice,
|
|
186
|
+
listPrice: data?.listPrice ?? data?.basePrice,
|
|
187
|
+
minQuantity: 1,
|
|
188
|
+
}],
|
|
189
|
+
})
|
|
190
|
+
return response
|
|
191
|
+
} catch (error) {
|
|
192
|
+
Logger.error('response error update price by table id', error)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
} else {
|
|
197
|
+
|
|
198
|
+
try {
|
|
199
|
+
response = await vtexApi.fetchApiURL(`/pricing/prices/${skuId}/fixed/${priceTableId}`, {
|
|
200
|
+
method: "delete",
|
|
201
|
+
})
|
|
202
|
+
return response
|
|
203
|
+
} catch (error) {
|
|
204
|
+
Logger.error('response error update price by table id', error)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
}
|
|
208
|
+
return response
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const applyRoundUpDecimalPrice = (data, RoundUpDecimalPrice) => {
|
|
212
|
+
if (RoundUpDecimalPrice && RoundUpDecimalPrice?.isActive) {
|
|
213
|
+
if (data?.basePrice) {
|
|
214
|
+
data.basePrice = roundPrice(data.basePrice, RoundUpDecimalPrice);
|
|
215
|
+
}
|
|
216
|
+
if (data?.listPrice) {
|
|
217
|
+
data.listPrice = roundPrice(data.listPrice, RoundUpDecimalPrice);
|
|
218
|
+
}
|
|
219
|
+
if (data?.costPrice) {
|
|
220
|
+
data.costPrice = roundPrice(data.costPrice, RoundUpDecimalPrice);
|
|
221
|
+
}
|
|
222
|
+
if (data?.fixedPrices) {
|
|
223
|
+
data.fixedPrices = data.fixedPrices.map((fixedPrice) => {
|
|
224
|
+
return {
|
|
225
|
+
...fixedPrice,
|
|
226
|
+
value: roundPrice(fixedPrice.value, RoundUpDecimalPrice),
|
|
227
|
+
listPrice: roundPrice(fixedPrice.listPrice, RoundUpDecimalPrice),
|
|
228
|
+
};
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
module.exports = {
|
|
235
|
+
consumer,
|
|
236
|
+
};
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
const ApiResponse = require("../../common/utils/api-response");
|
|
2
|
+
const AWSServices = require("../../common/utils/aws-services");
|
|
3
|
+
const ClientData = require("../../entities/clients");
|
|
4
|
+
const AccountData = require("../../entities/account");
|
|
5
|
+
const SkuData = require("../../entities/sku");
|
|
6
|
+
const DistributorData = require("../distributors/distributor");
|
|
7
|
+
const Logger = require("../../common/utils/logger");
|
|
8
|
+
const LIMIT_ITEMS_TO_PROCESS = process?.env?.LIMIT_ITEMS_TO_PROCESS || 150;
|
|
9
|
+
const SQS_PRICE_QUEUE_URL = process?.env?.SQS_PRICE_QUEUE_URL;
|
|
10
|
+
const SQS_PROCESSTAX_QUEUE_URL = process?.env?.SQS_PROCESSTAX_QUEUE_URL ?? null;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @param {import("aws-lambda").APIGatewayProxyEvent} event
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
const producer = async (event) => {
|
|
18
|
+
let statusCode = 200;
|
|
19
|
+
let message;
|
|
20
|
+
|
|
21
|
+
if (!event.body) {
|
|
22
|
+
return ApiResponse.response(400, "No body was found");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const clientId = event?.requestContext?.authorizer?.claims?.client_id;
|
|
26
|
+
Logger.log(JSON.stringify(JSON.parse(event.body)), clientId);
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
let request = JSON.parse(event.body);
|
|
30
|
+
const scope = event?.requestContext?.authorizer?.claims?.scope;
|
|
31
|
+
const accountName = request?.an;
|
|
32
|
+
const prices = orderPricesASC(request?.prices);
|
|
33
|
+
let pricesDiscount = [];
|
|
34
|
+
|
|
35
|
+
if (!prices || prices?.length > LIMIT_ITEMS_TO_PROCESS) {
|
|
36
|
+
return ApiResponse.response(404, "Prices limit of items exceeded");
|
|
37
|
+
}
|
|
38
|
+
if (!event?.internalService) {
|
|
39
|
+
if (!scope?.includes("admin")) {
|
|
40
|
+
const isValid = await ClientData.validateAccount(clientId, accountName);
|
|
41
|
+
if (!isValid) {
|
|
42
|
+
Logger.error("Account not found: ", accountName, ". ClientId: ", clientId);
|
|
43
|
+
return ApiResponse.response(404, "Account not found");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// se valida el account para determinar si los precios vienen de un proceso interno, o una ejecucion HTTP
|
|
49
|
+
const account = event?.accountData && event?.internalService === true
|
|
50
|
+
? event?.accountData
|
|
51
|
+
: await AccountData.getAccountDataByAccountName(accountName);
|
|
52
|
+
|
|
53
|
+
if (!account) {
|
|
54
|
+
Logger.error("Account not found: ", accountName, ". ClientId: ", clientId)
|
|
55
|
+
return ApiResponse.response(404, "Account not found")
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const count = Object.keys(account?.PriceList).length
|
|
59
|
+
if (!account || !account?.PriceList || count < 1) {
|
|
60
|
+
return ApiResponse.response(404, "Warehouse not found")
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
let skuRefUsed = account?.SkuRefUsed
|
|
64
|
+
if (typeof skuRefUsed !== "boolean") {
|
|
65
|
+
skuRefUsed = true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let notProcessed = [];
|
|
69
|
+
|
|
70
|
+
for (let item of prices) {
|
|
71
|
+
let copySku = item?.sku
|
|
72
|
+
let copyItem = item
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
let notFoundByEan = false
|
|
76
|
+
let skuData = null
|
|
77
|
+
|
|
78
|
+
//Si esta activado el manejo de sku por código de distribuidor
|
|
79
|
+
try {
|
|
80
|
+
const skuDistributorManagement = account?.SkuDistributorManagement || false
|
|
81
|
+
const refIdVtex = await DistributorData.getDistributorByErpCode(accountName, item?.sku, skuDistributorManagement)
|
|
82
|
+
|
|
83
|
+
item.sku = refIdVtex
|
|
84
|
+
copyItem.sku = refIdVtex
|
|
85
|
+
pricesDiscount = [...pricesDiscount, copyItem]
|
|
86
|
+
|
|
87
|
+
} catch (error) {
|
|
88
|
+
Logger.error("Error al obtener distribuidor por sku:", error);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (skuRefUsed) {
|
|
92
|
+
skuData = account?.ValidateSkuPriceInventoryInProcess === true
|
|
93
|
+
? { SkuId: item?.sku }
|
|
94
|
+
: await SkuData.getSkuByParentAccountNameAndSku(account?.ParentAccountName, item?.sku);
|
|
95
|
+
|
|
96
|
+
} else {
|
|
97
|
+
skuData = account?.ValidateSkuPriceInventoryInProcess === true
|
|
98
|
+
? [{ SkuId: item?.sku }]
|
|
99
|
+
: await SkuData.getSkuByParentAccountNameAndEan(account?.ParentAccountName, item?.sku);
|
|
100
|
+
|
|
101
|
+
if (!skuData || skuData.length < 1) {
|
|
102
|
+
notFoundByEan = true;
|
|
103
|
+
} else {
|
|
104
|
+
skuData = skuData?.[0];
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if ((!skuData || !skuData?.SkuId) && !notFoundByEan) {
|
|
109
|
+
item.message = "SKU not found";
|
|
110
|
+
notProcessed.push(item);
|
|
111
|
+
|
|
112
|
+
} else if (notFoundByEan && !skuRefUsed) {
|
|
113
|
+
item.message = "SKU not found by Ean";
|
|
114
|
+
notProcessed.push(item);
|
|
115
|
+
|
|
116
|
+
} else {
|
|
117
|
+
|
|
118
|
+
let fixedPrices = []
|
|
119
|
+
let namesFixedPrices = []
|
|
120
|
+
let messageError = ""
|
|
121
|
+
item.sku = skuData?.SkuId
|
|
122
|
+
|
|
123
|
+
// Si viene sc se prioriza sobre pricetableId y sobre fixed Prices
|
|
124
|
+
if (item?.sc) {
|
|
125
|
+
delete item.priceTableId // si viene priceTabledId junto con sc se elimina ya que es prioritario sc
|
|
126
|
+
}else if (item?.priceTableId) {
|
|
127
|
+
item.priceTableId = `L${item.priceTableId}` // se asigna un prefijo para la actualizacion de precios basado en un priceTableId
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
for (let fixedPrice of item?.fixedPrices) {
|
|
131
|
+
|
|
132
|
+
let priceList = Object.values(account?.PriceList)?.[0];
|
|
133
|
+
fixedPrice.minQuantity = fixedPrice?.minQuantity || 1;
|
|
134
|
+
|
|
135
|
+
if (fixedPrice?.listId && !namesFixedPrices.includes(fixedPrice?.listId)) {
|
|
136
|
+
namesFixedPrices = [...namesFixedPrices, fixedPrice?.listId];
|
|
137
|
+
priceList = account?.PriceList?.[fixedPrice?.listId];
|
|
138
|
+
if (!priceList) {
|
|
139
|
+
messageError = "Price list" + fixedPrice?.listId + " not found. ";
|
|
140
|
+
|
|
141
|
+
fixedPrice.tradePolicyId = `L${fixedPrice?.listId}`;
|
|
142
|
+
delete fixedPrice.listId;
|
|
143
|
+
fixedPrices.push(fixedPrice);
|
|
144
|
+
} else {
|
|
145
|
+
fixedPrice.tradePolicyId = priceList;
|
|
146
|
+
delete fixedPrice.listId;
|
|
147
|
+
fixedPrices.push(fixedPrice);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (item?.fixedPrices) {
|
|
154
|
+
item.fixedPrices = fixedPrices;
|
|
155
|
+
item.sku = skuData?.SkuId;
|
|
156
|
+
|
|
157
|
+
if (account?.SetValueListPriceAll === true) {
|
|
158
|
+
item = setListPriceAll(item)
|
|
159
|
+
}
|
|
160
|
+
await sendDataToSQS({ accountName, account, skuRefUsed, item })
|
|
161
|
+
|
|
162
|
+
}else {
|
|
163
|
+
item.sku = copySku;
|
|
164
|
+
Logger.error("Not all prices were processed. ", item);
|
|
165
|
+
item.message = messageError;
|
|
166
|
+
notProcessed.push(item);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
} catch (error) {
|
|
170
|
+
item.sku = copySku;
|
|
171
|
+
Logger.error(error);
|
|
172
|
+
notProcessed.push(item);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// Inicio Proceso de impuestos
|
|
176
|
+
request = { ...request, prices: pricesDiscount };
|
|
177
|
+
Logger.debug("request.prices", JSON.stringify(request.prices));
|
|
178
|
+
const listTaxes = getListTaxes(request) ?? [];
|
|
179
|
+
|
|
180
|
+
if (listTaxes && Object.keys(listTaxes).length > 0) {
|
|
181
|
+
const accountParent = await AccountData.getAccountDataByAccountName(account?.ParentAccountName);
|
|
182
|
+
const resSendTax = await sendSQStax({
|
|
183
|
+
data: listTaxes,
|
|
184
|
+
dataAccount: account,
|
|
185
|
+
dataAccountParent: accountParent
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (notProcessed.length > 0) {
|
|
190
|
+
const message = {
|
|
191
|
+
message: "Not all items were processed.",
|
|
192
|
+
notProcessedItems: notProcessed,
|
|
193
|
+
};
|
|
194
|
+
return ApiResponse.response(207, message);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
message = "Message accepted!";
|
|
198
|
+
|
|
199
|
+
} catch (error) {
|
|
200
|
+
Logger.error(error);
|
|
201
|
+
message = error;
|
|
202
|
+
statusCode = 500;
|
|
203
|
+
}
|
|
204
|
+
return ApiResponse.response(statusCode, message);
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Envio de datos a la cola AWS SQS
|
|
209
|
+
* @param data -- objeto con la informacion necesaria de precios para enviar a la cola de SQS
|
|
210
|
+
**/
|
|
211
|
+
const sendDataToSQS = async ({ accountName, account, skuRefUsed, item }) => {
|
|
212
|
+
|
|
213
|
+
const dataSend = {
|
|
214
|
+
an: accountName,
|
|
215
|
+
hoursValidStockPrice:account?.HoursValidStockPrice ?? 24,
|
|
216
|
+
ValidateSkuPriceInventoryInProcess:account?.ValidateSkuPriceInventoryInProcess ?? false,
|
|
217
|
+
ParentAccountName:account?.ParentAccountName,
|
|
218
|
+
SkuRefUsed: skuRefUsed,
|
|
219
|
+
RoundUpDecimalPrice:account?.RoundUpDecimalPrice ?? null,
|
|
220
|
+
key:account?.Credentials?.key,
|
|
221
|
+
token:account?.Credentials?.token,
|
|
222
|
+
data:item,
|
|
223
|
+
};
|
|
224
|
+
await AWSServices.sendSQSMessage(SQS_PRICE_QUEUE_URL, dataSend);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Asignacion de lista de precios en todas las propiedades del item
|
|
229
|
+
* @param item -- objeto item precio
|
|
230
|
+
**/
|
|
231
|
+
const setListPriceAll = (item) => {
|
|
232
|
+
for (const it in item) {
|
|
233
|
+
if (it === 'basePrice') {
|
|
234
|
+
item[it] = item?.listPrice;
|
|
235
|
+
}
|
|
236
|
+
if (it === 'fixedPrices') {
|
|
237
|
+
for (let i = 0; i < item?.fixedPrices?.length; i++) {
|
|
238
|
+
item.fixedPrices[i].value = item.fixedPrices[i]?.listPrice;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return item;
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Funcion para ordenar los precios notificados por el ERP
|
|
248
|
+
**/
|
|
249
|
+
const orderPricesASC = (prices) => {
|
|
250
|
+
let listPrices = [];
|
|
251
|
+
|
|
252
|
+
if (prices && prices?.length > 0) {
|
|
253
|
+
listPrices = prices?.sort((a, b) => (parseFloat(a?.basePrice) - parseFloat(b?.basePrice)));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return listPrices;
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* set list taxes request
|
|
261
|
+
**/
|
|
262
|
+
const getListTaxes = (resq) => {
|
|
263
|
+
if (!resq?.an || !resq?.prices || resq?.prices?.length == 0) {
|
|
264
|
+
return [];
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
let listTaxes = {};
|
|
268
|
+
for (let i = 0; i < resq.prices.length; i++) {
|
|
269
|
+
let itemsTaxes = resq?.prices[i]?.taxes ?? [];
|
|
270
|
+
let skuIt = resq?.prices[i]?.sku ?? null;
|
|
271
|
+
if (itemsTaxes?.length > 0 && skuIt) {
|
|
272
|
+
for (let j = 0; j < itemsTaxes.length; j++) {
|
|
273
|
+
let tax = itemsTaxes[j];
|
|
274
|
+
if (tax?.value > 0) {
|
|
275
|
+
let idTax = `${tax?.type}-${String(tax?.name).replace(/ /g, '').toLocaleLowerCase()}-${tax?.value}-tax-${resq.an}-`;
|
|
276
|
+
if (typeof listTaxes[idTax] !== 'object') {
|
|
277
|
+
listTaxes[idTax] = {
|
|
278
|
+
type: null,
|
|
279
|
+
name: null,
|
|
280
|
+
value: 0,
|
|
281
|
+
type: "percentual",
|
|
282
|
+
skus: [],
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
if (!listTaxes[idTax].skus?.includes(skuIt)) {
|
|
286
|
+
listTaxes[idTax].type = tax?.type ?? "percentual";
|
|
287
|
+
listTaxes[idTax].name = tax?.name ?? null;
|
|
288
|
+
listTaxes[idTax].value = tax?.value ?? null;
|
|
289
|
+
listTaxes[idTax].an = resq?.an ?? null;
|
|
290
|
+
listTaxes[idTax].skus = [...listTaxes[idTax].skus, skuIt];
|
|
291
|
+
} else {
|
|
292
|
+
}
|
|
293
|
+
} else {
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return listTaxes;
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Envio de SQS para salvar dentro de VTEX
|
|
304
|
+
* @param datasend -- Objeto con envio de parametros salvar impuesto
|
|
305
|
+
**/
|
|
306
|
+
const sendSQStax = async (datasend) => {
|
|
307
|
+
if (!SQS_PROCESSTAX_QUEUE_URL || !datasend) {
|
|
308
|
+
return "Error create sqs tax";
|
|
309
|
+
}
|
|
310
|
+
return new Promise(async (res, err) => {
|
|
311
|
+
try {
|
|
312
|
+
await AWSServices.sendSQSMessage(SQS_PROCESSTAX_QUEUE_URL, datasend);
|
|
313
|
+
res("Notification sqs tax successfully");
|
|
314
|
+
} catch (err) {
|
|
315
|
+
Logger.error('error crear SQS tax', err);
|
|
316
|
+
res("Error create sqs tax");
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
module.exports = {
|
|
322
|
+
producer,
|
|
323
|
+
};
|