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.
Files changed (129) hide show
  1. package/README.md +130 -0
  2. package/index.js +350 -0
  3. package/package.json +52 -0
  4. package/src/auth/handler.js +3 -0
  5. package/src/common/MondelezCastOrder.js +449 -0
  6. package/src/common/utils/AuthSecurity.js +46 -0
  7. package/src/common/utils/account-error-handler.js +279 -0
  8. package/src/common/utils/account-error-helper.js +231 -0
  9. package/src/common/utils/account-properties-handler.js +355 -0
  10. package/src/common/utils/api-response.js +62 -0
  11. package/src/common/utils/aws-services.js +186 -0
  12. package/src/common/utils/constants/account-error-codes.json +801 -0
  13. package/src/common/utils/constants.js +37 -0
  14. package/src/common/utils/convert/MondelezClientsItemsCast.js +52 -0
  15. package/src/common/utils/convert/MondelezInventoryItemsCast.js +15 -0
  16. package/src/common/utils/convert/MondelezOrderStatusCast.js +34 -0
  17. package/src/common/utils/convert/MondelezPricesItemsCast.js +37 -0
  18. package/src/common/utils/cron-ftp-get.js +143 -0
  19. package/src/common/utils/data-tables-helper.js +213 -0
  20. package/src/common/utils/date-range-calculator.js +113 -0
  21. package/src/common/utils/delay.js +17 -0
  22. package/src/common/utils/ftp-sftp.js +320 -0
  23. package/src/common/utils/logger.js +126 -0
  24. package/src/common/utils/nodemailerLib.js +61 -0
  25. package/src/common/utils/product-unit-converter.js +168 -0
  26. package/src/common/utils/schemas-utils.js +101 -0
  27. package/src/common/utils/seller-email-sharing-service.js +441 -0
  28. package/src/common/utils/sftp-utils.js +202 -0
  29. package/src/common/utils/status.js +15 -0
  30. package/src/common/utils/util.js +236 -0
  31. package/src/common/utils/validate-state-order.js +35 -0
  32. package/src/common/utils/validateProviders.js +67 -0
  33. package/src/common/utils/validation-data.js +45 -0
  34. package/src/common/utils/vtex/save-hooks.js +65 -0
  35. package/src/common/utils/vtex/save-schemas.js +65 -0
  36. package/src/common/utils/vtex-hook-handler.js +71 -0
  37. package/src/common/validation/AccountCoordinatesValidation.js +350 -0
  38. package/src/common/validation/GeneralErrorValidation.js +11 -0
  39. package/src/common/validation/MainErrorValidation.js +8 -0
  40. package/src/entities/account.js +639 -0
  41. package/src/entities/clients.js +104 -0
  42. package/src/entities/controlprice.js +196 -0
  43. package/src/entities/controlstock.js +206 -0
  44. package/src/entities/cron.js +77 -0
  45. package/src/entities/cronjob.js +71 -0
  46. package/src/entities/orders.js +195 -0
  47. package/src/entities/sftp-inbound.js +88 -0
  48. package/src/entities/sku.js +220 -0
  49. package/src/entities/taxpromotion.js +249 -0
  50. package/src/functions/account/account-get.js +262 -0
  51. package/src/functions/account/account-handler.js +299 -0
  52. package/src/functions/account/clients.js +10 -0
  53. package/src/functions/account/index.js +208 -0
  54. package/src/functions/actions/save-promotions-order-history.js +324 -0
  55. package/src/functions/affiliates/affiliates-hook-consumer.js +87 -0
  56. package/src/functions/affiliates/affiliates-hook-producer.js +45 -0
  57. package/src/functions/clients/clients-audience.js +62 -0
  58. package/src/functions/clients/clients-consumer.js +648 -0
  59. package/src/functions/clients/clients-producer.js +362 -0
  60. package/src/functions/clients/clients-suggested-product-consumer.js +166 -0
  61. package/src/functions/clients/helpers/suggested-product-mdlz.js +233 -0
  62. package/src/functions/clients_peru/email.html +129 -0
  63. package/src/functions/clients_peru/splitfile.js +357 -0
  64. package/src/functions/clients_peru/updateClients.js +1334 -0
  65. package/src/functions/clients_peru/utils.js +243 -0
  66. package/src/functions/cronjobs/cron-jobs-manager.js +40 -0
  67. package/src/functions/cronjobs/cron-jobs.js +171 -0
  68. package/src/functions/crons/cron.js +39 -0
  69. package/src/functions/distributors/distributor-handler.js +81 -0
  70. package/src/functions/distributors/distributor.js +535 -0
  71. package/src/functions/distributors/index.js +60 -0
  72. package/src/functions/financialpolicy/assign-financialpolicy.js +111 -0
  73. package/src/functions/financialpolicy/get-financialpolicy.js +91 -0
  74. package/src/functions/financialpolicy/index.js +28 -0
  75. package/src/functions/inventory/catalog-sync-consumer.js +17 -0
  76. package/src/functions/inventory/catalog-sync-handler.js +311 -0
  77. package/src/functions/inventory/inventory-consumer.js +119 -0
  78. package/src/functions/inventory/inventory-producer.js +197 -0
  79. package/src/functions/multiPresentation/multipre-queue.js +155 -0
  80. package/src/functions/multiPresentation/multipres.js +459 -0
  81. package/src/functions/nodeflow/index.js +83 -0
  82. package/src/functions/nodeflow/nodeflow-cron.js +200 -0
  83. package/src/functions/nodeflow/nodeflow-pub.js +203 -0
  84. package/src/functions/nodeflow/nodeflow-pvt.js +266 -0
  85. package/src/functions/notifications/download-leads-handler.js +67 -0
  86. package/src/functions/notifications/new-leads-notification-consumer.js +17 -0
  87. package/src/functions/notifications/new-leads-notification-handler.js +359 -0
  88. package/src/functions/notifications/order-status-notification-handler.js +482 -0
  89. package/src/functions/notifications/promotion-notification-handler.js +193 -0
  90. package/src/functions/orders/index.js +32 -0
  91. package/src/functions/orders/orders-cancel-handler.js +74 -0
  92. package/src/functions/orders/orders-handler.js +280 -0
  93. package/src/functions/orders/orders-hook-consumer.js +137 -0
  94. package/src/functions/orders/orders-hook-producer.js +170 -0
  95. package/src/functions/orders/orders-notifications-handler.js +137 -0
  96. package/src/functions/orders/orders-status-consumer.js +461 -0
  97. package/src/functions/orders/orders-status-producer.js +443 -0
  98. package/src/functions/prices/index.js +75 -0
  99. package/src/functions/prices/prices-consumer.js +236 -0
  100. package/src/functions/prices/prices-producer.js +323 -0
  101. package/src/functions/prices/promotion-and-tax.js +1284 -0
  102. package/src/functions/routesflow/assign-routeflow-queue.js +77 -0
  103. package/src/functions/schemas/vtex/handle-schemas.js +102 -0
  104. package/src/functions/security/process_gas.js +221 -0
  105. package/src/functions/security/security-handler.js +950 -0
  106. package/src/functions/sftp/sftp-consumer.js +453 -0
  107. package/src/functions/sftpIntegrations/processes/redirectServices.js +184 -0
  108. package/src/functions/sftpIntegrations/processes/validateFileSchema.js +226 -0
  109. package/src/functions/sftpIntegrations/schemas/credential-schema.js +123 -0
  110. package/src/functions/sftpIntegrations/schemas/record-schema.js +131 -0
  111. package/src/functions/sftpIntegrations/schemas/sftp_required_fields.json +3 -0
  112. package/src/functions/sftpIntegrations/sftp-config-producer.js +112 -0
  113. package/src/functions/sftpIntegrations/sftp-consumer.js +700 -0
  114. package/src/functions/sftpIntegrations/test/validateFile.test.js +122 -0
  115. package/src/functions/sftpIntegrations/utils/connect-dynamo.js +29 -0
  116. package/src/functions/sftpIntegrations/utils/split-data.js +25 -0
  117. package/src/functions/utils/index.js +130 -0
  118. package/src/functions/vtex/vtex-helpers.js +694 -0
  119. package/src/integrations/accountErrors/AccountErrorManager.js +437 -0
  120. package/src/integrations/audience/Audience.js +70 -0
  121. package/src/integrations/financialPolicy/FinancialPolicyApi.js +377 -0
  122. package/src/integrations/index.js +0 -0
  123. package/src/integrations/mobilvendor/MobilvendorApi.js +405 -0
  124. package/src/integrations/productmultipresentation/ProductMultiPresentation.js +200 -0
  125. package/src/mdlz/auth/SecretManagerApi.js +77 -0
  126. package/src/mdlz/client/MdlzApi.js +70 -0
  127. package/src/vtex/clients/ProvidersApi.js +51 -0
  128. package/src/vtex/clients/VtexApi.js +511 -0
  129. package/src/vtex/models/VtexOrder.js +87 -0
@@ -0,0 +1,104 @@
1
+ const DynamoAWS = require("aws-sdk/clients/dynamodb");
2
+ const Logger = require("../common/utils/logger");
3
+
4
+ const CLIENT_DATA_CONFIG_TABLE = process.env.CLIENT_DATA_CONFIG_TABLE;
5
+
6
+ const validateAccount = async (clientId, accountName) => {
7
+ try {
8
+ const dynamodb = new DynamoAWS.DocumentClient();
9
+ const params = {
10
+ TableName: CLIENT_DATA_CONFIG_TABLE,
11
+ KeyConditionExpression: "ClientId = :clientId",
12
+ FilterExpression: "contains(AccountNames, :accountName)",
13
+ ExpressionAttributeValues: {
14
+ ":clientId": clientId,
15
+ ":accountName": accountName,
16
+ },
17
+ };
18
+ const data = await dynamodb.query(params).promise();
19
+ return data && data?.Items?.length > 0 && data?.Count > 0;
20
+ } catch (err) {
21
+ Logger.error("Error validating account: ", err);
22
+ return false;
23
+ }
24
+ };
25
+
26
+ const getClient = async (clientId) => {
27
+ try {
28
+ const dynamodb = new DynamoAWS.DocumentClient();
29
+ const params = {
30
+ TableName: CLIENT_DATA_CONFIG_TABLE,
31
+ KeyConditionExpression: "ClientId = :clientId",
32
+ ExpressionAttributeValues: {
33
+ ":clientId": clientId
34
+ },
35
+ };
36
+ const data = await dynamodb.query(params).promise();
37
+ return data && data?.Items?.length > 0 ? data?.Items[0] : null;
38
+ } catch (err) {
39
+ Logger.error("Error validating account: ", err);
40
+ return false;
41
+ }
42
+ };
43
+
44
+ const getListAccountByClient = async (clientId) => {
45
+ try {
46
+ const dynamodb = new DynamoAWS.DocumentClient();
47
+ const params = {
48
+ TableName: CLIENT_DATA_CONFIG_TABLE,
49
+ Key: { ClientId: clientId },
50
+ };
51
+ const data = await dynamodb.get(params).promise();
52
+ return data && data?.Item ? data?.Item : null;
53
+ } catch (err) {
54
+ Logger.error("Error validating account: ", err);
55
+ return false;
56
+ }
57
+ };
58
+
59
+ const addClient = async (clientId, accountName) => {
60
+ try {
61
+ const dynamoDB = new DynamoAWS.DocumentClient();
62
+ const params = {
63
+ TableName: CLIENT_DATA_CONFIG_TABLE,
64
+ Key: { ClientId: clientId },
65
+ UpdateExpression: "ADD AccountNames :newAccountName",
66
+ ExpressionAttributeValues: {
67
+ ":newAccountName": dynamoDB.createSet([accountName]),
68
+ },
69
+ ConditionExpression: "attribute_exists(ClientId)",
70
+ ReturnValues: "UPDATED_NEW",
71
+ };
72
+ try {
73
+ const data = await dynamoDB.update(params).promise();
74
+ return data;
75
+ } catch (err) {
76
+ Logger.error("Error adding client: ", err);
77
+ if (err.code === "ConditionalCheckFailedException") {
78
+ Logger.error("Client does not exist, creating new client");
79
+ const createParams = {
80
+ TableName: CLIENT_DATA_CONFIG_TABLE,
81
+ Item: {
82
+ ClientId: clientId,
83
+ AccountNames: dynamoDB.createSet([accountName]),
84
+ },
85
+ };
86
+ const dataNew = await dynamoDB.put(createParams).promise();
87
+ return clientId;
88
+ } else {
89
+ Logger.error("Error adding new client: ", err);
90
+ throw err;
91
+ }
92
+ }
93
+ } catch (error) {
94
+ Logger.error("Error adding client: ", error);
95
+ return;
96
+ }
97
+ };
98
+
99
+ module.exports = {
100
+ validateAccount,
101
+ addClient,
102
+ getClient,
103
+ getListAccountByClient
104
+ };
@@ -0,0 +1,196 @@
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
+ const TABLE_NAME = process?.env?.CONTROL_PRICE_TABLE;
6
+ const TABLE_KEY= "SkuIdAccount";
7
+ /**
8
+ * Obtner registro
9
+ * @param Id -- id registro
10
+ **/
11
+ const getPerId = async (Id= null) => {
12
+ if(!Id){
13
+ return null;
14
+ }
15
+ try {
16
+ const dynamodb = new DynamoAWS.DocumentClient();
17
+ const params = {
18
+ TableName: TABLE_NAME,
19
+ Key: {
20
+ [TABLE_KEY]: Id
21
+ }
22
+ };
23
+ const response = await dynamodb.get(params).promise();
24
+ return response?.Item ? response?.Item : null;
25
+ } catch (err) {
26
+ Logger.error("Error getting account data: ", err);
27
+ return null;
28
+ }
29
+ };
30
+
31
+ /**
32
+ * CREAR O ACTULALIZA
33
+ * @param dataSave -- json con datos para almacenar
34
+ **/
35
+ const createOrUpdate = async (dataSave) => {
36
+ const dynamodb = new DynamoAWS.DocumentClient();
37
+ let data = Validation.transformFirstKeysToUpperCaseValue(dataSave);
38
+ try {
39
+ const params = {
40
+ TableName: TABLE_NAME,
41
+ Item: data,
42
+ ConditionExpression: `attribute_not_exists(${TABLE_KEY})`,
43
+ };
44
+ await dynamodb.put(params).promise();
45
+ return dataSave;
46
+ } catch (err) {
47
+ if (err.code === "ConditionalCheckFailedException") {
48
+ try{
49
+ let dataUpdateDynamo= setDataUpdateDynamo(data, 60);
50
+ for (var i = 0; i < dataUpdateDynamo.length; i++) {
51
+ let updateExpression='set';
52
+ let ExpressionAttributeNames={};
53
+ let ExpressionAttributeValues = {};
54
+ for (const property in dataUpdateDynamo[i]) {
55
+ if(property != TABLE_KEY && property != TABLE_KEY){
56
+ updateExpression += ` #${property} = :${property} ,`;
57
+ ExpressionAttributeNames['#'+property] = property ;
58
+ ExpressionAttributeValues[':'+property]=dataUpdateDynamo[i][property];
59
+ }
60
+ }
61
+ updateExpression= updateExpression.slice(0, -1);
62
+ let res2= await dynamodb.update({
63
+ TableName: TABLE_NAME,
64
+ Key:{
65
+ [TABLE_KEY]: data[TABLE_KEY]
66
+ },
67
+ UpdateExpression: updateExpression,
68
+ ExpressionAttributeNames: ExpressionAttributeNames,
69
+ ExpressionAttributeValues: ExpressionAttributeValues
70
+ }).promise();
71
+ }
72
+
73
+ return dataSave;
74
+ }catch(err2){
75
+ if(err2.code === "ConditionalCheckFailedException"){
76
+ return dataSave;
77
+ }
78
+ Logger.error('err2err2',err2);
79
+ return null
80
+ }
81
+ }
82
+ Logger.error('error create', err);
83
+ return null;
84
+ }
85
+ };
86
+
87
+ /**
88
+ * Ajuste la cantidad de propiedades validar dentro de dynamo
89
+ * @param data -- json promotion
90
+ * @param limit -- limite propiedades dynamo
91
+ **/
92
+ const setDataUpdateDynamo= (data, limit=100)=>{
93
+ limit= limit > 100 ? 100 : limit;
94
+ let arraData= [], objInternal={}, countItems=0, lengthData=Object.keys(data).length;
95
+ for (let item in data) {
96
+ countItems++;
97
+ if(Object.keys(objInternal).length < limit){
98
+ objInternal[item]= data[item];
99
+ }else{
100
+ arraData=[...arraData,objInternal];
101
+ objInternal={};
102
+ objInternal[item]= data[item];
103
+ }
104
+ if(countItems === lengthData){
105
+ arraData=[...arraData,objInternal];
106
+ }
107
+ }
108
+
109
+ return arraData;
110
+ };
111
+
112
+ const validNotificationPriceVtex= async ({
113
+ skuId= null,
114
+ account= null,
115
+ data= null,
116
+ hoursUpdate= 24
117
+ })=>{
118
+ if(!skuId || !account || !data){
119
+ return false;
120
+ }
121
+
122
+ return new Promise(async (res,err)=>{
123
+ let idReg= `${skuId}${account}`;
124
+ let register= await getPerId(idReg);
125
+ let resp= true;
126
+ let validNotification= false;
127
+ let dataSave= {
128
+ [TABLE_KEY]: idReg,
129
+ SkuId: skuId,
130
+ AccountName: account,
131
+ JsonPrice: JSON.stringify(data),
132
+ JsonPricePrev: register?.JsonPrice ? register?.JsonPrice : "",
133
+ LastNotificadionDateERP: (new Date()).toISOString(),
134
+ LastNotificadionDateVtex: (new Date()).toISOString(),
135
+ CreatedIn: (new Date()).toISOString()
136
+ };
137
+ //validacion de actualizacion de inventario
138
+ if(register){
139
+ validNotification= validNotificacionVTEX({
140
+ dateUpdate: register?.LastNotificadionDateVtex ?? (new Date()).toISOString(),
141
+ diferentsHours: hoursUpdate ?? 24,
142
+ dataNotification: data,
143
+ dataSaved: JSON.parse(register?.JsonPrice)
144
+ });
145
+ }
146
+ let registerUpdate= {};
147
+ if(!register){
148
+ registerUpdate= await createOrUpdate(dataSave);
149
+ }else if(validNotification===true){
150
+ delete dataSave.CreatedIn;
151
+ registerUpdate= await createOrUpdate(dataSave);
152
+ }else{
153
+ delete dataSave.CreatedIn;
154
+ delete dataSave.LastNotificadionDateVtex;
155
+ resp= false;
156
+ registerUpdate= await createOrUpdate(dataSave);
157
+ }
158
+ res(resp);
159
+ });
160
+ };
161
+
162
+ /**
163
+ * Validador de diferencias horas notificacion a vtex
164
+ **/
165
+ const validNotificacionVTEX= ({dateUpdate= null, diferentsHours=24, dataNotification=null, dataSaved= null})=>{
166
+ if(!dateUpdate || !diferentsHours || !dataNotification || !dataSaved){
167
+ Logger.error("Error validNotificacionVTEX: ", {
168
+ dateUpdate,
169
+ diferentsHours,
170
+ dataNotification,
171
+ dataSaved
172
+ });
173
+ return false;
174
+ }
175
+ if(JSON.stringify(dataNotification) === JSON.stringify(dataSaved) ){
176
+ const dateNow= (new Date()).getTime();
177
+ const dateLastInteraction= (new Date(dateUpdate)).getTime();
178
+ const diff = dateNow - dateLastInteraction;
179
+ const hours = diff / (1000 * 60 * 60);
180
+ if(hours >= diferentsHours){
181
+ return true;
182
+ }else{
183
+ return false;
184
+ }
185
+
186
+ }else{
187
+ return true;
188
+ }
189
+
190
+ };
191
+
192
+ module.exports = {
193
+ createOrUpdate,
194
+ getPerId,
195
+ validNotificationPriceVtex
196
+ };
@@ -0,0 +1,206 @@
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
+ const TABLE_NAME = process?.env?.CONTROL_STOCK_TABLE;
6
+ const TABLE_KEY= "SkuIdWarehouseAccount";
7
+ /**
8
+ * Obtner registro
9
+ * @param Id -- id registro
10
+ **/
11
+ const getPerId = async (Id= null) => {
12
+ if(!Id){
13
+ return null;
14
+ }
15
+
16
+ try {
17
+ const dynamodb = new DynamoAWS.DocumentClient();
18
+ const params = {
19
+ TableName: TABLE_NAME,
20
+ Key: {
21
+ [TABLE_KEY]: Id
22
+ }
23
+ };
24
+ const response = await dynamodb.get(params).promise();
25
+ return response?.Item ? response?.Item : null;
26
+ } catch (err) {
27
+ Logger.error("Error getting account data: ", err);
28
+ return null;
29
+ }
30
+ };
31
+
32
+ /**
33
+ * CREAR O ACTULALIZA
34
+ * @param dataSave -- json con datos para almacenar
35
+ **/
36
+ const createOrUpdate = async (dataSave) => {
37
+ const dynamodb = new DynamoAWS.DocumentClient();
38
+ let data = Validation.transformFirstKeysToUpperCaseValue(dataSave);
39
+ try {
40
+ const params = {
41
+ TableName: TABLE_NAME,
42
+ Item: data,
43
+ ConditionExpression: `attribute_not_exists(${TABLE_KEY})`,
44
+ };
45
+ await dynamodb.put(params).promise();
46
+ return dataSave;
47
+ } catch (err) {
48
+ if (err.code === "ConditionalCheckFailedException") {
49
+ try{
50
+ let dataUpdateDynamo= setDataUpdateDynamo(data, 60);
51
+ for (var i = 0; i < dataUpdateDynamo.length; i++) {
52
+ let updateExpression='set';
53
+ let ExpressionAttributeNames={};
54
+ let ExpressionAttributeValues = {};
55
+ for (const property in dataUpdateDynamo[i]) {
56
+ if(property != TABLE_KEY && property != TABLE_KEY){
57
+ updateExpression += ` #${property} = :${property} ,`;
58
+ ExpressionAttributeNames['#'+property] = property ;
59
+ ExpressionAttributeValues[':'+property]=dataUpdateDynamo[i][property];
60
+ }
61
+ }
62
+ updateExpression= updateExpression.slice(0, -1);
63
+ let res2= await dynamodb.update({
64
+ TableName: TABLE_NAME,
65
+ Key:{
66
+ [TABLE_KEY]: data[TABLE_KEY]
67
+ },
68
+ UpdateExpression: updateExpression,
69
+ ExpressionAttributeNames: ExpressionAttributeNames,
70
+ ExpressionAttributeValues: ExpressionAttributeValues
71
+ }).promise();
72
+ }
73
+
74
+ return dataSave;
75
+ }catch(err2){
76
+ if(err2.code === "ConditionalCheckFailedException"){
77
+ return dataSave;
78
+ }
79
+ Logger.error('err2err2',err2);
80
+ return null
81
+ }
82
+
83
+ }
84
+ Logger.error('error create', err);
85
+ return null;
86
+ }
87
+ };
88
+
89
+ /**
90
+ * Ajuste la cantidad de propiedades validar dentro de dynamo
91
+ * @param data -- json promotion
92
+ * @param limit -- limite propiedades dynamo
93
+ **/
94
+ const setDataUpdateDynamo= (data, limit=100)=>{
95
+ limit= limit > 100 ? 100 : limit;
96
+ let arraData= [], objInternal={}, countItems=0, lengthData=Object.keys(data).length;
97
+ for (let item in data) {
98
+ countItems++;
99
+ if(Object.keys(objInternal).length < limit){
100
+ objInternal[item]= data[item];
101
+ }else{
102
+ arraData=[...arraData,objInternal];
103
+ objInternal={};
104
+ objInternal[item]= data[item];
105
+ }
106
+ if(countItems === lengthData){
107
+ arraData=[...arraData,objInternal];
108
+ }
109
+ }
110
+
111
+ return arraData;
112
+ };
113
+
114
+ const validNotificationStockVtex= async ({
115
+ skuId= null,
116
+ account= null,
117
+ warehouse= null,
118
+ quantity= null,
119
+ hoursUpdate= 24
120
+ })=>{
121
+ if(!skuId || !account || quantity===null || !warehouse){
122
+ Logger.error('Error validNotificationStockVtex: ', 'skuId, account, quantity, warehouse');
123
+ return false;
124
+ }
125
+
126
+ return new Promise(async (res,err)=>{
127
+ let idReg= `${skuId}${warehouse}${account}`;
128
+ let register= await getPerId(idReg);
129
+ let resp= true;
130
+ let validNotification= false;
131
+ let dataSave= {
132
+ [TABLE_KEY]: idReg,
133
+ SkuId: skuId,
134
+ AccountName: account,
135
+ Quantity: quantity,
136
+ QuantityPrev: register?.Quantity ?? 0,
137
+ Warehouse: warehouse,
138
+ LastNotificadionDateERP: (new Date()).toISOString(),
139
+ LastNotificadionDateVtex: (new Date()).toISOString(),
140
+ CreatedIn: (new Date()).toISOString()
141
+ };
142
+
143
+ //validacion de actualizacion de inventario
144
+ if(register){
145
+ validNotification= validNotificacionVTEX({
146
+ dateUpdate: register?.LastNotificadionDateVtex ?? (new Date()).toISOString(),
147
+ diferentsHours: hoursUpdate ?? 24,
148
+ quantityNotification: quantity ?? 0,
149
+ quantitySaved: register?.Quantity ?? 0
150
+ });
151
+ }
152
+
153
+ let registerUpdate= {};
154
+ if(!register){
155
+ registerUpdate= await createOrUpdate(dataSave);
156
+ }else if(validNotification===true){
157
+ delete dataSave.CreatedIn;
158
+ registerUpdate= await createOrUpdate(dataSave);
159
+ }else{
160
+ delete dataSave.CreatedIn;
161
+ delete dataSave.LastNotificadionDateVtex;
162
+ resp= false;
163
+ registerUpdate= await createOrUpdate(dataSave);
164
+ }
165
+
166
+ res(resp);
167
+ });
168
+ };
169
+
170
+ /**
171
+ * Validador de diferencias horas notificacion a vtex
172
+ **/
173
+ const validNotificacionVTEX= ({dateUpdate= null, diferentsHours=24, quantityNotification=null, quantitySaved= null})=>{
174
+ if(!dateUpdate || diferentsHours===null || quantityNotification===null || quantitySaved===null){
175
+ Logger.error("Error validNotificacionVTEX: ", {
176
+ dateUpdate,
177
+ diferentsHours,
178
+ quantityNotification,
179
+ quantitySaved
180
+ });
181
+ return false;
182
+ }
183
+
184
+ if(quantityNotification === quantitySaved){
185
+ const dateNow= (new Date()).getTime();
186
+ const dateLastInteraction= (new Date(dateUpdate)).getTime();
187
+ const diff = dateNow - dateLastInteraction;
188
+ const hours = diff / (1000 * 60 * 60);
189
+
190
+ if(hours >= diferentsHours){
191
+ return true;
192
+ }else{
193
+ return false;
194
+ }
195
+
196
+ }else{
197
+ return true;
198
+ }
199
+
200
+ };
201
+
202
+ module.exports = {
203
+ createOrUpdate,
204
+ getPerId,
205
+ validNotificationStockVtex
206
+ };
@@ -0,0 +1,77 @@
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
+ const CRONS_TABLE = process.env.CRONS_TABLE;
6
+
7
+ const getPerId = async (cronId) => {
8
+ try {
9
+ const dynamodb = new DynamoAWS.DocumentClient();
10
+ const params = {
11
+ TableName: CRONS_TABLE,
12
+ Key: {
13
+ "CronId": cronId
14
+ }
15
+ };
16
+ const response = await dynamodb.get(params).promise();
17
+ return response?.Item ? response?.Item : null;
18
+ } catch (err) {
19
+ Logger.error("Error getting account data: ", err);
20
+ return null;
21
+ }
22
+ };
23
+
24
+ const addRegisterPerId = async (dataSave, CronId) => {
25
+ const dynamodb = new DynamoAWS.DocumentClient();
26
+ let data = Validation.transformFirstKeysToUpperCaseValue(dataSave);
27
+ try {
28
+ const params = {
29
+ TableName: CRONS_TABLE,
30
+ Item: data,
31
+ ConditionExpression: "attribute_not_exists(CronId)",
32
+ };
33
+ await dynamodb.put(params).promise();
34
+ return dataSave;
35
+ } catch (err) {
36
+ Logger.error("Error saving account data config: ", err);
37
+ if (err.code === "ConditionalCheckFailedException") {
38
+ try{
39
+ let updateExpression='set';
40
+ let ExpressionAttributeNames={};
41
+ let ExpressionAttributeValues = {};
42
+ for (const property in data) {
43
+ if(property != 'CronId'){
44
+ updateExpression += ` #${property} = :${property} ,`;
45
+ ExpressionAttributeNames['#'+property] = property ;
46
+ ExpressionAttributeValues[':'+property]=data[property];
47
+ }
48
+ }
49
+ updateExpression= updateExpression.slice(0, -1);
50
+ const params2 = {
51
+ TableName: CRONS_TABLE,
52
+ Key:{
53
+ CronId: CronId
54
+ },
55
+ UpdateExpression: updateExpression,
56
+ ExpressionAttributeNames: ExpressionAttributeNames,
57
+ ExpressionAttributeValues: ExpressionAttributeValues
58
+ };
59
+ let res2= await dynamodb.update(params2).promise();
60
+ return dataSave;
61
+ }catch(err2){
62
+ Logger.error('err2err2',err2);
63
+ if(err2.code === "ConditionalCheckFailedException"){
64
+ return dataSave;
65
+ }
66
+ return null
67
+ }
68
+
69
+ }
70
+ return null;
71
+ }
72
+ };
73
+
74
+ module.exports = {
75
+ getPerId,
76
+ addRegisterPerId
77
+ };
@@ -0,0 +1,71 @@
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
+ const { CRONS_TABLE } = process?.env ?? {};
6
+
7
+ module.exports.getPerId = async (cronId) => {
8
+ try {
9
+ const dynamodb = new DynamoAWS.DocumentClient();
10
+ const params = {
11
+ TableName: CRONS_TABLE,
12
+ Key: { 'CronId': cronId }
13
+ };
14
+ const response = await dynamodb.get(params).promise();
15
+ return response?.Item ?? null;
16
+ } catch (ex) {
17
+ Logger.error('Error in getPerId:', ex);
18
+ return null;
19
+ }
20
+ }
21
+
22
+ module.exports.addRegisterPerId = async (cronId, cronData) => {
23
+ const dynamodb = new DynamoAWS.DocumentClient();
24
+ let data = Validation.transformFirstKeysToUpperCaseValue(cronData);
25
+ try {
26
+ const params = {
27
+ TableName: CRONS_TABLE,
28
+ Item: data,
29
+ ConditionExpression: 'attribute_not_exists(CronId)',
30
+ };
31
+ await dynamodb.put(params).promise();
32
+ return data;
33
+ } catch (ex) {
34
+ if (ex.code === 'ConditionalCheckFailedException') {
35
+ return updateDynamoRecord(dynamodb, cronId, data);
36
+ } else {
37
+ Logger.error('Error in addRegisterPerId: ', ex);
38
+ return null;
39
+ }
40
+ }
41
+ }
42
+
43
+ async function updateDynamoRecord(dynamodb, cronId, data) {
44
+ try {
45
+ let updateExpression = '';
46
+ let expressionAttributeNames = {}, expressionAttributeValues = {};
47
+ for (const property in data) {
48
+ if (property != 'CronId') {
49
+ if (updateExpression) {
50
+ updateExpression += ', ';
51
+ }
52
+ const key = `#${property}`, value = `:${property}`;
53
+ updateExpression += `${key} = ${value}`;
54
+ expressionAttributeNames[key] = property;
55
+ expressionAttributeValues[value] = data[property];
56
+ }
57
+ }
58
+ const params = {
59
+ TableName: CRONS_TABLE,
60
+ Key: { CronId: cronId },
61
+ UpdateExpression: `set ${updateExpression}`,
62
+ ExpressionAttributeNames: expressionAttributeNames,
63
+ ExpressionAttributeValues: expressionAttributeValues
64
+ };
65
+ await dynamodb.update(params).promise();
66
+ return data;
67
+ } catch (ex) {
68
+ Logger.error('Error in updateDynamoRecord: ', ex);
69
+ return null;
70
+ }
71
+ }