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,195 @@
|
|
|
1
|
+
const uuid = require("uuid");
|
|
2
|
+
const DynamoAWS = require("aws-sdk/clients/dynamodb");
|
|
3
|
+
const Validation = require("../common/utils/validation-data");
|
|
4
|
+
const Logger = require("../common/utils/logger");
|
|
5
|
+
|
|
6
|
+
ORDER_DATA_TABLE = process.env.ORDER_DATA_TABLE;
|
|
7
|
+
|
|
8
|
+
const getOrderDataByOrderId = async (orderId) => {
|
|
9
|
+
const data = {
|
|
10
|
+
TableName: ORDER_DATA_TABLE,
|
|
11
|
+
IndexName: "OrderIdAndAccountNameIndex",
|
|
12
|
+
KeyConditionExpression: "OrderId = :orderId",
|
|
13
|
+
ExpressionAttributeValues: {
|
|
14
|
+
":orderId": orderId,
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
try {
|
|
18
|
+
const dynamodb = new DynamoAWS.DocumentClient();
|
|
19
|
+
const response = await dynamodb.query(data).promise();
|
|
20
|
+
return response.Items;
|
|
21
|
+
} catch (err) {
|
|
22
|
+
Logger.error("Error getting order data: ", err);
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const addOrder = async (order) => {
|
|
28
|
+
order.Id = `${order.AccountNameForOrder}${order.OrderId}${order.OrderState}`;
|
|
29
|
+
let data = {};
|
|
30
|
+
try {
|
|
31
|
+
const dynamodb = new DynamoAWS.DocumentClient();
|
|
32
|
+
data = Validation.transformKeysToUpperCaseValue(order);
|
|
33
|
+
const params = {
|
|
34
|
+
TableName: ORDER_DATA_TABLE,
|
|
35
|
+
Item: data,
|
|
36
|
+
ConditionExpression: "attribute_not_exists(Id)",
|
|
37
|
+
};
|
|
38
|
+
await dynamodb.put(params).promise();
|
|
39
|
+
return order;
|
|
40
|
+
} catch (err) {
|
|
41
|
+
if (err.code === "ConditionalCheckFailedException") {
|
|
42
|
+
Logger.error("Error creating or saving record in order notification. The Id already exists in the entity: ", data);
|
|
43
|
+
return null;
|
|
44
|
+
} else {
|
|
45
|
+
Logger.error("Error creating or saving record in order notification: ", err);
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
async function queryOrdersByAccountNameBetweenDatesWithPagination(accountName, fromDate, toDate, state, lastEvaluatedKey, total) {
|
|
52
|
+
let params = {
|
|
53
|
+
TableName: ORDER_DATA_TABLE,
|
|
54
|
+
IndexName: "AccountNameAndStatusDateIndex",
|
|
55
|
+
KeyConditionExpression: "#an = :accountNameForOrder AND #d BETWEEN :from AND :to",
|
|
56
|
+
ExpressionAttributeNames: {
|
|
57
|
+
"#an": "AccountNameForOrder",
|
|
58
|
+
"#d": "StatusDate",
|
|
59
|
+
"#s": "OrderState",
|
|
60
|
+
"#State": "OrderState",
|
|
61
|
+
"#OId": "OrderId",
|
|
62
|
+
"#Id": "Id"
|
|
63
|
+
},
|
|
64
|
+
ExpressionAttributeValues: {
|
|
65
|
+
":accountNameForOrder": accountName,
|
|
66
|
+
":from": fromDate,
|
|
67
|
+
":to": toDate,
|
|
68
|
+
":state": state,
|
|
69
|
+
},
|
|
70
|
+
FilterExpression: "#s = :state",
|
|
71
|
+
ProjectionExpression: "#Id, #an, #OId, #State, #d",
|
|
72
|
+
Select: "SPECIFIC_ATTRIBUTES"
|
|
73
|
+
};
|
|
74
|
+
if (lastEvaluatedKey) {
|
|
75
|
+
params.ExclusiveStartKey = lastEvaluatedKey;
|
|
76
|
+
}
|
|
77
|
+
if (!state) {
|
|
78
|
+
delete params.ExpressionAttributeValues[":state"];
|
|
79
|
+
delete params.ExpressionAttributeNames["#s"];
|
|
80
|
+
delete params.FilterExpression;
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
let response = await getListCompleteItems(params, total);
|
|
84
|
+
const orderData = response.Items;
|
|
85
|
+
const lastEvaluatedKey = response.LastEvaluatedKey;
|
|
86
|
+
const result = {
|
|
87
|
+
orderData: orderData,
|
|
88
|
+
lastEvaluatedKey: lastEvaluatedKey,
|
|
89
|
+
};
|
|
90
|
+
return result;
|
|
91
|
+
} catch (err) {
|
|
92
|
+
Logger.error("Error getting order data: ", err);
|
|
93
|
+
return [];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const getListCompleteItems = async (params, total) => {
|
|
98
|
+
if (parseInt(total) == 0) {
|
|
99
|
+
return { Items: [], LastEvaluatedKey: null };
|
|
100
|
+
}
|
|
101
|
+
let limitP = total ?? 1, countItems = 0, listItems = [], lastEvaluatedKey = 1;
|
|
102
|
+
return new Promise(async function (resolve, reject) {
|
|
103
|
+
const dynamodb = new DynamoAWS.DocumentClient();
|
|
104
|
+
while (lastEvaluatedKey) {
|
|
105
|
+
let res = await dynamodb.query(params).promise();
|
|
106
|
+
for (let i = 0; i < res.Items.length; i++) {
|
|
107
|
+
listItems = [...listItems, res.Items[i]];
|
|
108
|
+
}
|
|
109
|
+
countItems += parseInt(res.Count);
|
|
110
|
+
params.ExclusiveStartKey = res?.LastEvaluatedKey;
|
|
111
|
+
lastEvaluatedKey = res?.LastEvaluatedKey;
|
|
112
|
+
if (!res?.LastEvaluatedKey) {
|
|
113
|
+
countItems = limitP;
|
|
114
|
+
lastEvaluatedKey = null;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
resolve({ Items: listItems, LastEvaluatedKey: lastEvaluatedKey });
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const getOrdersByAccountNameBetweenDates = async (accountName, startDate, endDate, state, page, total, limitPage = 100) => {
|
|
122
|
+
let evaluate = await queryOrdersByAccountNameBetweenDatesWithPagination(accountName, startDate, endDate, state, null, total);
|
|
123
|
+
let orderData = evaluate.orderData.sort(function (a, b) {
|
|
124
|
+
let dateS = new Date(b.StatusDate).getTime();
|
|
125
|
+
let dateE = new Date(a.StatusDate).getTime();
|
|
126
|
+
return dateS - dateE;
|
|
127
|
+
});
|
|
128
|
+
let startPage = page == 1 || !page ? 0 : (parseInt((page - 1)) * parseInt(limitPage));
|
|
129
|
+
let endPage = page == 1 || !page ? limitPage : (parseInt(page) * parseInt(limitPage));
|
|
130
|
+
let resData = orderData.filter((item, index) => (index >= startPage && index < endPage));
|
|
131
|
+
|
|
132
|
+
return resData;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const getOrdersByAccountNameBetweenDatesCountTotals = async (accountName, startDate, endDate, state) => {
|
|
136
|
+
let data = {
|
|
137
|
+
TableName: ORDER_DATA_TABLE,
|
|
138
|
+
IndexName: "AccountNameAndStatusDateIndex",
|
|
139
|
+
KeyConditionExpression: "#an = :accountNameForOrder AND #d BETWEEN :from AND :to",
|
|
140
|
+
ExpressionAttributeNames: {
|
|
141
|
+
"#an": "AccountNameForOrder",
|
|
142
|
+
"#d": "StatusDate",
|
|
143
|
+
"#s": "OrderState"
|
|
144
|
+
},
|
|
145
|
+
Select: "COUNT",
|
|
146
|
+
ExpressionAttributeValues: {
|
|
147
|
+
":accountNameForOrder": accountName,
|
|
148
|
+
":from": startDate,
|
|
149
|
+
":to": endDate,
|
|
150
|
+
":state": state,
|
|
151
|
+
},
|
|
152
|
+
FilterExpression: "#s = :state"
|
|
153
|
+
};
|
|
154
|
+
if (!state) {
|
|
155
|
+
delete data.ExpressionAttributeValues[":state"];
|
|
156
|
+
delete data.ExpressionAttributeNames["#s"];
|
|
157
|
+
delete data.FilterExpression;
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
const response = await getCount(data);
|
|
161
|
+
return response.Count;
|
|
162
|
+
} catch (err) {
|
|
163
|
+
Logger.error("Error getting order data: ", err);
|
|
164
|
+
return 0;
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Funcion para obtener conteo de registros en dynamo
|
|
170
|
+
* @param params -- son los paraetros de busqueda dynamo
|
|
171
|
+
*/
|
|
172
|
+
const getCount = async (params) => {
|
|
173
|
+
let countItems = 0, listItems = [], lastEvaluatedKey = 1;
|
|
174
|
+
return new Promise(async function (resolve, reject) {
|
|
175
|
+
const dynamodb = new DynamoAWS.DocumentClient();
|
|
176
|
+
while (lastEvaluatedKey) {
|
|
177
|
+
let res = await dynamodb.query(params).promise();
|
|
178
|
+
countItems += parseInt(res?.Count);
|
|
179
|
+
params.ExclusiveStartKey = res?.LastEvaluatedKey;
|
|
180
|
+
lastEvaluatedKey = res?.LastEvaluatedKey;
|
|
181
|
+
if (!res?.LastEvaluatedKey) {
|
|
182
|
+
lastEvaluatedKey = null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
resolve({ Count: countItems });
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
module.exports = {
|
|
191
|
+
getOrderDataByOrderId,
|
|
192
|
+
addOrder,
|
|
193
|
+
getOrdersByAccountNameBetweenDates,
|
|
194
|
+
getOrdersByAccountNameBetweenDatesCountTotals,
|
|
195
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
const DynamoAWS = require("aws-sdk/clients/dynamodb");
|
|
2
|
+
const Validation = require("../common/utils/validation-data");
|
|
3
|
+
const Logger = require("../common/utils/logger");
|
|
4
|
+
|
|
5
|
+
SFTP_INBOUND_DATA_TABLE = process.env.SFTP_INBOUND_DATA_TABLE;
|
|
6
|
+
|
|
7
|
+
const getSftpConfigByDirectoryAndProcessExecute = async (directory, processExecute) => {
|
|
8
|
+
try {
|
|
9
|
+
const params = {
|
|
10
|
+
TableName: SFTP_INBOUND_DATA_TABLE,
|
|
11
|
+
Key: {
|
|
12
|
+
Directory: directory,
|
|
13
|
+
ProcessExecute: processExecute,
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
const dynamodb = new DynamoAWS.DocumentClient();
|
|
17
|
+
const data = await dynamodb.get(params).promise();
|
|
18
|
+
return data.Item;
|
|
19
|
+
} catch (err) {
|
|
20
|
+
Logger.error("Error getting sftp inbound config: ", err);
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const getSftpInboundConfigByAccountName = async (accountName) => {
|
|
26
|
+
try {
|
|
27
|
+
const dynamodb = new DynamoAWS.DocumentClient();
|
|
28
|
+
const params = {
|
|
29
|
+
TableName: SFTP_INBOUND_DATA_TABLE,
|
|
30
|
+
IndexName: "AccountNameIndex",
|
|
31
|
+
KeyConditionExpression: "AccountName = :accountName",
|
|
32
|
+
ExpressionAttributeValues: {
|
|
33
|
+
":accountName": accountName,
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
const response = await dynamodb.query(params).promise();
|
|
37
|
+
return response?.Items[0];
|
|
38
|
+
} catch (err) {
|
|
39
|
+
Logger.error("Error getting sftp inbound data: ", err);
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const addSftpInboundConfig = async (sftpInboundData) => {
|
|
45
|
+
try {
|
|
46
|
+
const dynamodb = new DynamoAWS.DocumentClient();
|
|
47
|
+
const data = Validation.transformFirstKeysToUpperCaseValue(sftpInboundData);
|
|
48
|
+
const params = {
|
|
49
|
+
TableName: SFTP_INBOUND_DATA_TABLE,
|
|
50
|
+
Item: data,
|
|
51
|
+
};
|
|
52
|
+
await dynamodb.put(params).promise();
|
|
53
|
+
return data;
|
|
54
|
+
} catch (err) {
|
|
55
|
+
Logger.error("Error saving sftp inbound data config: ", err);
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const getItemsByDirectory = async (directory) => {
|
|
61
|
+
const dynamodb = new DynamoAWS.DocumentClient();
|
|
62
|
+
const params = {
|
|
63
|
+
TableName: SFTP_INBOUND_DATA_TABLE,
|
|
64
|
+
KeyConditionExpression: "#dir = :directoryValue",
|
|
65
|
+
ExpressionAttributeNames: {
|
|
66
|
+
"#dir": "Directory",
|
|
67
|
+
},
|
|
68
|
+
ExpressionAttributeValues: {
|
|
69
|
+
":directoryValue": directory,
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const response = await dynamodb.query(params).promise();
|
|
75
|
+
const items = response.Items;
|
|
76
|
+
return items;
|
|
77
|
+
} catch (error) {
|
|
78
|
+
Logger.error("Error getting items by directory: ", error);
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
module.exports = {
|
|
84
|
+
getSftpConfigByDirectoryAndProcessExecute,
|
|
85
|
+
getSftpInboundConfigByAccountName,
|
|
86
|
+
addSftpInboundConfig,
|
|
87
|
+
getItemsByDirectory,
|
|
88
|
+
};
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
const DynamoAWS = require("aws-sdk/clients/dynamodb");
|
|
2
|
+
const VtexApi = require("../vtex/clients/VtexApi");
|
|
3
|
+
const AccountData = require("./account");
|
|
4
|
+
const Logger = require("../common/utils/logger");
|
|
5
|
+
|
|
6
|
+
const INVENTORY_SKU_DATA_TABLE = process.env.INVENTORY_SKU_DATA_TABLE;
|
|
7
|
+
|
|
8
|
+
const getSkuByParentAccountNameAndSku = async (accountName, sku, TradePolicyId = null) => {
|
|
9
|
+
try {
|
|
10
|
+
const id = `${accountName}${sku}`;
|
|
11
|
+
const params = {
|
|
12
|
+
TableName: INVENTORY_SKU_DATA_TABLE,
|
|
13
|
+
Key: {
|
|
14
|
+
AccountNameAndSku: id,
|
|
15
|
+
ParentAccountName: accountName,
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
const dynamodb = new DynamoAWS.DocumentClient();
|
|
19
|
+
let data = await dynamodb.get(params).promise();
|
|
20
|
+
if (typeof data.Item != 'object') {
|
|
21
|
+
data.Item = await getItemVtexPerRefId({
|
|
22
|
+
refId: sku,
|
|
23
|
+
parentAccountName: accountName,
|
|
24
|
+
TradePolicyId: TradePolicyId
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return data.Item;
|
|
28
|
+
} catch (err) {
|
|
29
|
+
Logger.error("Error getting sku data config: ", err);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const getSkuByParentAccountNameAndEan = async (accountName, ean, TradePolicyId = null) => {
|
|
35
|
+
try {
|
|
36
|
+
const params = {
|
|
37
|
+
TableName: INVENTORY_SKU_DATA_TABLE,
|
|
38
|
+
IndexName: "AccountNameAndSkuEanIndex",
|
|
39
|
+
KeyConditionExpression: "Ean = :ean AND ParentAccountName = :parentAccount",
|
|
40
|
+
ExpressionAttributeValues: {
|
|
41
|
+
":ean": ean,
|
|
42
|
+
":parentAccount": accountName,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
const dynamodb = new DynamoAWS.DocumentClient();
|
|
46
|
+
let data = await dynamodb.query(params).promise();
|
|
47
|
+
if (typeof data.Items == 'object' && data.Items.length == 0) {
|
|
48
|
+
let itemsearch = await getItemVtexPerEAN({
|
|
49
|
+
ean: ean,
|
|
50
|
+
parentAccountName: accountName,
|
|
51
|
+
TradePolicyId: TradePolicyId
|
|
52
|
+
});
|
|
53
|
+
if (itemsearch && typeof itemsearch == 'object') {
|
|
54
|
+
data.Items = [...data.Items, itemsearch];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return data.Items;
|
|
58
|
+
} catch (err) {
|
|
59
|
+
Logger.error("Error getting sku by ean data config: ", err);
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const addSku = async (sku) => {
|
|
65
|
+
try {
|
|
66
|
+
const dynamodb = new DynamoAWS.DocumentClient();
|
|
67
|
+
const params = {
|
|
68
|
+
TableName: INVENTORY_SKU_DATA_TABLE,
|
|
69
|
+
Item: sku,
|
|
70
|
+
};
|
|
71
|
+
await dynamodb.put(params).promise();
|
|
72
|
+
return sku;
|
|
73
|
+
} catch (err) {
|
|
74
|
+
Logger.error("Error saving account data config: ", err);
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const getItemVtexPerRefId = async ({
|
|
80
|
+
refId = null,
|
|
81
|
+
parentAccountName = null,
|
|
82
|
+
TradePolicyId = null
|
|
83
|
+
}) => {
|
|
84
|
+
if (refId && parentAccountName) {
|
|
85
|
+
try {
|
|
86
|
+
const account = await AccountData.getAccountDataByAccountName(parentAccountName);
|
|
87
|
+
if (account) {
|
|
88
|
+
const an = parentAccountName,
|
|
89
|
+
key = account?.Credentials?.key,
|
|
90
|
+
token = account?.Credentials?.token;
|
|
91
|
+
const vtexApi = new VtexApi(an, key, token);
|
|
92
|
+
const response = await vtexApi.fetch(`/catalog/pvt/stockkeepingunit?RefId=${refId}`, {
|
|
93
|
+
method: "GET",
|
|
94
|
+
data: {},
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
return response?.data && response?.data?.Id ? formatDataItemSku(response?.data, parentAccountName) : null;
|
|
98
|
+
} else {
|
|
99
|
+
Logger.error("account parent not found");
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
} catch (err) {
|
|
103
|
+
Logger.error("not fount item vtex: ", err);
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const getItemVtexPerEAN = async ({
|
|
112
|
+
ean = null,
|
|
113
|
+
parentAccountName = null,
|
|
114
|
+
TradePolicyId = null
|
|
115
|
+
}) => {
|
|
116
|
+
if (ean && parentAccountName) {
|
|
117
|
+
try {
|
|
118
|
+
const account = await AccountData.getAccountDataByAccountName(parentAccountName);
|
|
119
|
+
if (account) {
|
|
120
|
+
const an = parentAccountName,
|
|
121
|
+
key = account?.Credentials?.key,
|
|
122
|
+
token = account?.Credentials?.token;
|
|
123
|
+
const vtexApi = new VtexApi(an, key, token);
|
|
124
|
+
const response = await vtexApi.fetch(`/catalog_system/pvt/sku/stockkeepingunitbyean/${ean}`, {
|
|
125
|
+
method: "GET",
|
|
126
|
+
data: {},
|
|
127
|
+
});
|
|
128
|
+
return response?.data && response?.data?.Id ? formatDataItemSku(response?.data, parentAccountName) : null;
|
|
129
|
+
} else {
|
|
130
|
+
Logger.error("account parent not found");
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
} catch (err) {
|
|
134
|
+
Logger.error("not fount item vtex: ", err);
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
return [];
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const formatDataItemSku = (data, parentAccountName) => {
|
|
143
|
+
if (data && parentAccountName && data?.Id) {
|
|
144
|
+
return {
|
|
145
|
+
Ean: data?.AlternateIds?.Ean ?? data?.RefId,
|
|
146
|
+
ParentAccountName: parentAccountName,
|
|
147
|
+
AccountNameAndSku: `${parentAccountName}${(data?.AlternateIds?.RefId ?? data?.RefId)}`,
|
|
148
|
+
Type: 'SKU',
|
|
149
|
+
Sku: data?.AlternateIds?.RefId ?? data?.RefId,
|
|
150
|
+
SkuId: data?.Id,
|
|
151
|
+
Name: data?.NameComplete ?? data?.Name,
|
|
152
|
+
};
|
|
153
|
+
} else {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Metodo para obtener sku id VTEX
|
|
160
|
+
**/
|
|
161
|
+
const getSkuVtex = async ({
|
|
162
|
+
account = null,
|
|
163
|
+
sku = null,
|
|
164
|
+
SkuRefUsed = true
|
|
165
|
+
}) => {
|
|
166
|
+
return new Promise(async (responseD, errorD) => {
|
|
167
|
+
if (!account || !sku) {
|
|
168
|
+
errorD("account and sku are required!");
|
|
169
|
+
} else {
|
|
170
|
+
let skuData = null;
|
|
171
|
+
if (SkuRefUsed) {
|
|
172
|
+
skuData = await getSkuByParentAccountNameAndSku(account, sku);
|
|
173
|
+
} else {
|
|
174
|
+
skuData = await getSkuByParentAccountNameAndEan(account, sku);
|
|
175
|
+
if (!skuData || skuData.length < 1) {
|
|
176
|
+
errorD("Not found by Ean: ", sku);
|
|
177
|
+
} else {
|
|
178
|
+
skuData = skuData?.[0];
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (skuData && skuData?.SkuId) {
|
|
183
|
+
responseD(skuData?.SkuId);
|
|
184
|
+
} else {
|
|
185
|
+
errorD("Not found by Sku: ", sku);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Save log error search sku
|
|
193
|
+
**/
|
|
194
|
+
const saveLogProductsNotFount = async ({an=null, key=null, token= null, entity=null, sku=null, value= null}) => {
|
|
195
|
+
if (an && key && token && entity && sku && value) {
|
|
196
|
+
try{
|
|
197
|
+
const vtexApi = new VtexApi(an, key, token);
|
|
198
|
+
const response = await vtexApi.fetch(`/dataentities/${entity}/documents/`, {
|
|
199
|
+
method: "PATCH",
|
|
200
|
+
data: {
|
|
201
|
+
id: `${sku}-${an}`,
|
|
202
|
+
sku: sku,
|
|
203
|
+
whitelabel: an,
|
|
204
|
+
value: (typeof value === 'string') ? value : null
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
} catch(errSaveMD2) {
|
|
208
|
+
Logger.error('Error save log sku not found', errSaveMD2);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return true;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
module.exports = {
|
|
215
|
+
getSkuByParentAccountNameAndSku,
|
|
216
|
+
addSku,
|
|
217
|
+
getSkuByParentAccountNameAndEan,
|
|
218
|
+
getSkuVtex,
|
|
219
|
+
saveLogProductsNotFount
|
|
220
|
+
};
|