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,279 @@
|
|
|
1
|
+
const ApiResponse = require("./api-response");
|
|
2
|
+
const AccountErrorManager = require("../../integrations/accountErrors/AccountErrorManager");
|
|
3
|
+
const Logger = require("./logger");
|
|
4
|
+
|
|
5
|
+
const handler = async (event) => {
|
|
6
|
+
const { httpMethod, resource, pathParameters, queryStringParameters } = event;
|
|
7
|
+
|
|
8
|
+
try {
|
|
9
|
+
switch (resource) {
|
|
10
|
+
case "/errors":
|
|
11
|
+
if (httpMethod === "GET") {
|
|
12
|
+
return await listErrors(queryStringParameters);
|
|
13
|
+
} else if (httpMethod === "POST") {
|
|
14
|
+
return await createErrors(event.body);
|
|
15
|
+
}
|
|
16
|
+
break;
|
|
17
|
+
|
|
18
|
+
case "/errors/{errorCode}":
|
|
19
|
+
const { errorCode } = pathParameters || {};
|
|
20
|
+
if (!errorCode) {
|
|
21
|
+
return ApiResponse.response(400, { error: "Error code is required" });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (httpMethod === "GET") {
|
|
25
|
+
return await getError(errorCode);
|
|
26
|
+
} else if (httpMethod === "PUT") {
|
|
27
|
+
return await updateError(errorCode, event.body);
|
|
28
|
+
} else if (httpMethod === "DELETE") {
|
|
29
|
+
return await deleteError(errorCode);
|
|
30
|
+
}
|
|
31
|
+
break;
|
|
32
|
+
|
|
33
|
+
default:
|
|
34
|
+
return ApiResponse.response(404, { error: "Resource not found" });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return ApiResponse.response(405, { error: "Method not allowed" });
|
|
38
|
+
|
|
39
|
+
} catch (error) {
|
|
40
|
+
Logger.error("Error in account errors handler:", error);
|
|
41
|
+
return ApiResponse.response(500, { error: "Internal server error", details: error.message });
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const listErrors = async (queryParams = {}) => {
|
|
46
|
+
try {
|
|
47
|
+
const safeQueryParams = queryParams || {};
|
|
48
|
+
|
|
49
|
+
const filters = {
|
|
50
|
+
limit: safeQueryParams.limit ? parseInt(safeQueryParams.limit) : 100,
|
|
51
|
+
category: safeQueryParams.category,
|
|
52
|
+
severity: safeQueryParams.severity,
|
|
53
|
+
lastEvaluatedKey: safeQueryParams.lastEvaluatedKey
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const result = await AccountErrorManager.listErrors(filters);
|
|
57
|
+
|
|
58
|
+
return ApiResponse.response(200, {
|
|
59
|
+
message: "Errors retrieved successfully",
|
|
60
|
+
data: result.items,
|
|
61
|
+
pagination: {
|
|
62
|
+
count: result.count,
|
|
63
|
+
lastEvaluatedKey: result.lastEvaluatedKey ? JSON.stringify(result.lastEvaluatedKey) : null,
|
|
64
|
+
hasMore: !!result.lastEvaluatedKey
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
} catch (error) {
|
|
69
|
+
Logger.error("Error listing errors:", error);
|
|
70
|
+
return ApiResponse.response(500, { error: "Error retrieving errors", details: error.message });
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const getError = async (errorCode) => {
|
|
75
|
+
try {
|
|
76
|
+
const error = await AccountErrorManager.getError(errorCode);
|
|
77
|
+
|
|
78
|
+
if (!error) {
|
|
79
|
+
return ApiResponse.response(404, { error: "Error code not found" });
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return ApiResponse.response(200, {
|
|
83
|
+
message: "Error retrieved successfully",
|
|
84
|
+
data: error
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
} catch (error) {
|
|
88
|
+
Logger.error(`Error getting error ${errorCode}:`, error);
|
|
89
|
+
return ApiResponse.response(500, { error: "Error retrieving error", details: error.message });
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const createErrors = async (body) => {
|
|
94
|
+
try {
|
|
95
|
+
if (!body) {
|
|
96
|
+
return ApiResponse.response(400, { error: "Request body is required" });
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const requestData = JSON.parse(body);
|
|
100
|
+
const isArray = Array.isArray(requestData);
|
|
101
|
+
const errorsToCreate = isArray ? requestData : [requestData];
|
|
102
|
+
|
|
103
|
+
Logger.info(`Creating ${errorsToCreate.length} error(s)`, {
|
|
104
|
+
isArray,
|
|
105
|
+
count: errorsToCreate.length
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const validationErrors = [];
|
|
109
|
+
errorsToCreate.forEach((errorData, index) => {
|
|
110
|
+
const prefix = isArray ? `Error[${index}]` : 'Error';
|
|
111
|
+
|
|
112
|
+
if (!errorData.ErrorCode) {
|
|
113
|
+
validationErrors.push(`${prefix}: ErrorCode is required`);
|
|
114
|
+
}
|
|
115
|
+
if (!errorData.HttpStatus || typeof errorData.HttpStatus !== 'number') {
|
|
116
|
+
validationErrors.push(`${prefix}: HttpStatus must be a valid number`);
|
|
117
|
+
}
|
|
118
|
+
if (!errorData.Message) {
|
|
119
|
+
validationErrors.push(`${prefix}: Message is required`);
|
|
120
|
+
}
|
|
121
|
+
if (!errorData.Category) {
|
|
122
|
+
validationErrors.push(`${prefix}: Category is required`);
|
|
123
|
+
}
|
|
124
|
+
if (!errorData.Severity) {
|
|
125
|
+
validationErrors.push(`${prefix}: Severity is required`);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
if (validationErrors.length > 0) {
|
|
130
|
+
return ApiResponse.response(400, {
|
|
131
|
+
error: "Validation failed",
|
|
132
|
+
validationErrors: validationErrors
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const successfulCreations = [];
|
|
137
|
+
const failedCreations = [];
|
|
138
|
+
|
|
139
|
+
for (let i = 0; i < errorsToCreate.length; i++) {
|
|
140
|
+
const errorData = errorsToCreate[i];
|
|
141
|
+
const prefix = isArray ? `[${i}]` : '';
|
|
142
|
+
|
|
143
|
+
try {
|
|
144
|
+
const createdError = await AccountErrorManager.createError(errorData);
|
|
145
|
+
|
|
146
|
+
successfulCreations.push({
|
|
147
|
+
index: isArray ? i : null,
|
|
148
|
+
errorCode: errorData.ErrorCode,
|
|
149
|
+
status: 'created',
|
|
150
|
+
data: createdError
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
Logger.debug(`Error ${prefix} created successfully:`, errorData.ErrorCode);
|
|
154
|
+
|
|
155
|
+
} catch (error) {
|
|
156
|
+
failedCreations.push({
|
|
157
|
+
index: isArray ? i : null,
|
|
158
|
+
errorCode: errorData.ErrorCode,
|
|
159
|
+
status: 'failed',
|
|
160
|
+
error: error.message,
|
|
161
|
+
details: errorData
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
Logger.error(`Error ${prefix} creation failed:`, {
|
|
165
|
+
errorCode: errorData.ErrorCode,
|
|
166
|
+
error: error.message
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const totalCount = errorsToCreate.length;
|
|
172
|
+
const successCount = successfulCreations.length;
|
|
173
|
+
const failureCount = failedCreations.length;
|
|
174
|
+
|
|
175
|
+
let statusCode;
|
|
176
|
+
let message;
|
|
177
|
+
|
|
178
|
+
if (failureCount === 0) {
|
|
179
|
+
statusCode = 201;
|
|
180
|
+
message = isArray
|
|
181
|
+
? `All ${totalCount} errors created successfully`
|
|
182
|
+
: "Error created successfully";
|
|
183
|
+
} else if (successCount === 0) {
|
|
184
|
+
statusCode = 400;
|
|
185
|
+
message = isArray
|
|
186
|
+
? `All ${totalCount} errors failed to create`
|
|
187
|
+
: "Error creation failed";
|
|
188
|
+
} else {
|
|
189
|
+
statusCode = 207;
|
|
190
|
+
message = `${successCount} of ${totalCount} errors created successfully`;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const responseData = {
|
|
194
|
+
message: message,
|
|
195
|
+
summary: {
|
|
196
|
+
total: totalCount,
|
|
197
|
+
successful: successCount,
|
|
198
|
+
failed: failureCount,
|
|
199
|
+
is_array: isArray
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
if (isArray || failureCount > 0) {
|
|
204
|
+
responseData.results = {
|
|
205
|
+
successful: successfulCreations,
|
|
206
|
+
failed: failedCreations
|
|
207
|
+
};
|
|
208
|
+
} else {
|
|
209
|
+
responseData.data = successfulCreations[0]?.data;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return ApiResponse.response(statusCode, responseData);
|
|
213
|
+
|
|
214
|
+
} catch (error) {
|
|
215
|
+
Logger.error("Error in createErrors:", error);
|
|
216
|
+
|
|
217
|
+
if (error.message && error.message.includes("already exists")) {
|
|
218
|
+
return ApiResponse.response(409, { error: error.message });
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return ApiResponse.response(500, { error: "Error creating errors", details: error.message });
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const updateError = async (errorCode, body) => {
|
|
226
|
+
try {
|
|
227
|
+
if (!body) {
|
|
228
|
+
return ApiResponse.response(400, { error: "Request body is required" });
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const errorData = JSON.parse(body);
|
|
232
|
+
|
|
233
|
+
if (errorData.HttpStatus && typeof errorData.HttpStatus !== 'number') {
|
|
234
|
+
return ApiResponse.response(400, { error: "HttpStatus must be a valid number" });
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const updatedError = await AccountErrorManager.updateError(errorCode, errorData);
|
|
238
|
+
|
|
239
|
+
return ApiResponse.response(200, {
|
|
240
|
+
message: "Error updated successfully",
|
|
241
|
+
data: updatedError
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
} catch (error) {
|
|
245
|
+
Logger.error(`Error updating error ${errorCode}:`, error);
|
|
246
|
+
|
|
247
|
+
if (error.message.includes("not found")) {
|
|
248
|
+
return ApiResponse.response(404, { error: error.message });
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (error.message.includes("No fields to update")) {
|
|
252
|
+
return ApiResponse.response(400, { error: error.message });
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return ApiResponse.response(500, { error: "Error updating error", details: error.message });
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
const deleteError = async (errorCode) => {
|
|
260
|
+
try {
|
|
261
|
+
await AccountErrorManager.deleteError(errorCode);
|
|
262
|
+
|
|
263
|
+
return ApiResponse.response(200, {
|
|
264
|
+
message: "Error deleted successfully",
|
|
265
|
+
deleted: true
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
} catch (error) {
|
|
269
|
+
Logger.error(`Error deleting error ${errorCode}:`, error);
|
|
270
|
+
|
|
271
|
+
if (error.message.includes("not found")) {
|
|
272
|
+
return ApiResponse.response(404, { error: error.message });
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return ApiResponse.response(500, { error: "Error deleting error", details: error.message });
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
module.exports = { handler };
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
const AccountErrorManager = require("../../integrations/accountErrors/AccountErrorManager");
|
|
2
|
+
const Logger = require("./logger");
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Clase helper moderna para gestión de errores de Account
|
|
6
|
+
* 100% basada en DynamoDB, con fallbacks robustos
|
|
7
|
+
*/
|
|
8
|
+
class AccountErrors {
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Crea una respuesta de error
|
|
12
|
+
* @param {string} errorCode - Código de error
|
|
13
|
+
* @param {Object} replacements - Variables para reemplazar
|
|
14
|
+
* @returns {Promise<Object>} Respuesta de error
|
|
15
|
+
*/
|
|
16
|
+
static async createError(errorCode, replacements = {}) {
|
|
17
|
+
try {
|
|
18
|
+
return await AccountErrorManager.createResponse(errorCode, replacements);
|
|
19
|
+
} catch (error) {
|
|
20
|
+
Logger.error(`Critical error in createError for ${errorCode}:`, error);
|
|
21
|
+
|
|
22
|
+
// Fallback de emergencia
|
|
23
|
+
return this._emergencyFallback(errorCode, error);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Manejo de errores con contexto específico
|
|
29
|
+
* @param {string} errorCode - Código de error
|
|
30
|
+
* @param {Object} context - Contexto adicional
|
|
31
|
+
* @returns {Promise<Object>} Respuesta de error
|
|
32
|
+
*/
|
|
33
|
+
static async handleError(errorCode, context = {}) {
|
|
34
|
+
const { variables = {}, logDetails = null } = context;
|
|
35
|
+
|
|
36
|
+
// Log del error si se proporcionan detalles
|
|
37
|
+
if (logDetails) {
|
|
38
|
+
Logger.error(`Account Error ${errorCode}:`, logDetails);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return await this.createError(errorCode, variables);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Crea error con logging automático
|
|
46
|
+
* @param {string} errorCode - Código de error
|
|
47
|
+
* @param {Error} originalError - Error original para logging
|
|
48
|
+
* @param {Object} variables - Variables para reemplazar
|
|
49
|
+
* @returns {Promise<Object>} Respuesta de error
|
|
50
|
+
*/
|
|
51
|
+
static async createWithLogging(errorCode, originalError = null, variables = {}) {
|
|
52
|
+
if (originalError) {
|
|
53
|
+
Logger.error(`Account Error ${errorCode} - Original:`, {
|
|
54
|
+
message: originalError.message,
|
|
55
|
+
stack: originalError.stack,
|
|
56
|
+
name: originalError.name
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return await this.createError(errorCode, variables);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Valida y crea error con fallback
|
|
65
|
+
* @param {string} errorCode - Código de error
|
|
66
|
+
* @param {Object} variables - Variables para reemplazar
|
|
67
|
+
* @param {string} fallbackMessage - Mensaje de fallback
|
|
68
|
+
* @returns {Promise<Object>} Respuesta de error
|
|
69
|
+
*/
|
|
70
|
+
static async createSafe(errorCode, variables = {}, fallbackMessage = "An error occurred") {
|
|
71
|
+
try {
|
|
72
|
+
return await this.createError(errorCode, variables);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
Logger.error(`Failed to create error ${errorCode}:`, error);
|
|
75
|
+
|
|
76
|
+
return this._manualFallback(errorCode, fallbackMessage, variables);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* ✅ NUEVO: Inicializa errores básicos del sistema
|
|
82
|
+
* @returns {Promise<Object>} Resultado de la inicialización
|
|
83
|
+
*/
|
|
84
|
+
static async initializeSystemErrors() {
|
|
85
|
+
try {
|
|
86
|
+
const results = await AccountErrorManager.initializeBasicErrors();
|
|
87
|
+
return {
|
|
88
|
+
success: true,
|
|
89
|
+
message: "System errors initialized successfully",
|
|
90
|
+
results: results
|
|
91
|
+
};
|
|
92
|
+
} catch (error) {
|
|
93
|
+
Logger.error("Failed to initialize system errors:", error);
|
|
94
|
+
return {
|
|
95
|
+
success: false,
|
|
96
|
+
message: "Failed to initialize system errors",
|
|
97
|
+
error: error.message
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* ✅ NUEVO: Verifica la salud del sistema de errores
|
|
104
|
+
* @returns {Promise<Object>} Estado del sistema
|
|
105
|
+
*/
|
|
106
|
+
static async healthCheck() {
|
|
107
|
+
try {
|
|
108
|
+
// Intentar obtener un error básico
|
|
109
|
+
const testError = await AccountErrorManager.getError('ACCOUNT_001');
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
status: 'healthy',
|
|
113
|
+
dynamodb_connection: 'ok',
|
|
114
|
+
basic_errors_available: !!testError,
|
|
115
|
+
timestamp: new Date().toISOString()
|
|
116
|
+
};
|
|
117
|
+
} catch (error) {
|
|
118
|
+
return {
|
|
119
|
+
status: 'unhealthy',
|
|
120
|
+
dynamodb_connection: 'failed',
|
|
121
|
+
error: error.message,
|
|
122
|
+
timestamp: new Date().toISOString()
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Fallback de emergencia cuando todo falla
|
|
129
|
+
* @private
|
|
130
|
+
*/
|
|
131
|
+
static _emergencyFallback(errorCode, originalError) {
|
|
132
|
+
return {
|
|
133
|
+
statusCode: 500,
|
|
134
|
+
body: JSON.stringify({
|
|
135
|
+
error: errorCode || 'CRITICAL_SYSTEM_ERROR',
|
|
136
|
+
message: "A critical system error occurred. Please contact support immediately.",
|
|
137
|
+
emergency_fallback: true,
|
|
138
|
+
timestamp: new Date().toISOString(),
|
|
139
|
+
trace_id: Math.random().toString(36).substring(2, 15)
|
|
140
|
+
}, null, 2),
|
|
141
|
+
headers: {
|
|
142
|
+
"Content-Type": "application/json"
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Fallback manual con mensaje personalizado
|
|
149
|
+
* @private
|
|
150
|
+
*/
|
|
151
|
+
static _manualFallback(errorCode, fallbackMessage, variables = {}) {
|
|
152
|
+
// Intentar reemplazar variables en el mensaje de fallback
|
|
153
|
+
let message = fallbackMessage;
|
|
154
|
+
Object.keys(variables).forEach(key => {
|
|
155
|
+
const placeholder = `{${key}}`;
|
|
156
|
+
message = message.replace(new RegExp(placeholder.replace(/[{}]/g, '\\$&'), 'g'), variables[key]);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
statusCode: 500,
|
|
161
|
+
body: JSON.stringify({
|
|
162
|
+
error: errorCode,
|
|
163
|
+
message: message,
|
|
164
|
+
manual_fallback: true,
|
|
165
|
+
timestamp: new Date().toISOString()
|
|
166
|
+
}, null, 2),
|
|
167
|
+
headers: {
|
|
168
|
+
"Content-Type": "application/json"
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Métodos helper rápidos para errores comunes
|
|
175
|
+
*/
|
|
176
|
+
|
|
177
|
+
// Errores críticos del sistema
|
|
178
|
+
static async systemError(details = null) {
|
|
179
|
+
return await this.createWithLogging('ACCOUNT_026', details);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Error de entrada básico
|
|
183
|
+
static async invalidInput(field = 'data') {
|
|
184
|
+
return await this.createError('ACCOUNT_001');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Error de cuenta no encontrada (genérico)
|
|
188
|
+
static async notFound(accountName = null) {
|
|
189
|
+
return await this.createError('ACCOUNT_028', { accountName: accountName || 'Unknown' });
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Error de validación de coordenadas
|
|
193
|
+
static async coordinatesError(accountName, coordinates = 'N/A') {
|
|
194
|
+
return await this.createError('ACCOUNT_010', {
|
|
195
|
+
AccountName: accountName,
|
|
196
|
+
context: 'coordinates',
|
|
197
|
+
latitude: 'N/A',
|
|
198
|
+
longitude: 'N/A'
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Error de seller en marketplace
|
|
203
|
+
static async sellerError(sellerId) {
|
|
204
|
+
return await this.createError('ACCOUNT_014', { SellerId: sellerId });
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* ✅ NUEVO: Helper para migrar desde JSON (útil para desarrollo)
|
|
209
|
+
* @param {Object} jsonErrors - Errores del JSON original
|
|
210
|
+
* @returns {Promise<Object>} Resultado de la migración
|
|
211
|
+
*/
|
|
212
|
+
static async migrateFromJson(jsonErrors) {
|
|
213
|
+
try {
|
|
214
|
+
const results = await AccountErrorManager.migrateFromJson(jsonErrors);
|
|
215
|
+
return {
|
|
216
|
+
success: true,
|
|
217
|
+
message: "Migration completed",
|
|
218
|
+
results: results
|
|
219
|
+
};
|
|
220
|
+
} catch (error) {
|
|
221
|
+
Logger.error("Migration failed:", error);
|
|
222
|
+
return {
|
|
223
|
+
success: false,
|
|
224
|
+
message: "Migration failed",
|
|
225
|
+
error: error.message
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
module.exports = AccountErrors;
|