cardus 0.0.99 → 0.0.101
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/dist/index.js +155 -145
- package/index.ts +232 -219
- package/insertTempShipment/index.ts +3 -12
- package/package.json +1 -1
- package/types/index.ts +30 -12
package/dist/index.js
CHANGED
|
@@ -17,20 +17,6 @@ const moment_1 = __importDefault(require("moment"));
|
|
|
17
17
|
const insertTempShipment_1 = require("./insertTempShipment");
|
|
18
18
|
class IntegrationManager {
|
|
19
19
|
constructor(params) {
|
|
20
|
-
this.insertTempShipment = (_a) => __awaiter(this, [_a], void 0, function* ({ order, agencyId, warehouseId, sellerAddress, idUsuario, isPriority }) {
|
|
21
|
-
const tempShipmentData = yield (0, insertTempShipment_1.cookTempShipment)({
|
|
22
|
-
order,
|
|
23
|
-
shipmentType: this.shipmentType,
|
|
24
|
-
agencyId,
|
|
25
|
-
warehouseId,
|
|
26
|
-
sellerAddress,
|
|
27
|
-
idUsuario,
|
|
28
|
-
findNextPickupDate: this.findNextPickupDate,
|
|
29
|
-
countriesService: this.services.countriesService,
|
|
30
|
-
isPriority
|
|
31
|
-
});
|
|
32
|
-
return yield this.services.integrationsService.insertTempShipment(tempShipmentData, false);
|
|
33
|
-
});
|
|
34
20
|
this.insertExternalShipment = ({ idUsuario, order, idEnvioTemporal }) => {
|
|
35
21
|
this.dataToInsert.externals.push({
|
|
36
22
|
id_envio_temporal_usuario: idEnvioTemporal,
|
|
@@ -48,16 +34,19 @@ class IntegrationManager {
|
|
|
48
34
|
});
|
|
49
35
|
};
|
|
50
36
|
this.insertTempShipmentGroupLine = ({ idEnvioTemporal, group }) => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
37
|
+
// si agrupamos, no deberia de haber mas lineas, pero por si acaso, con el set machacamos lo que pudiera haber -que no deberia haber nada, insisto-
|
|
38
|
+
this.dataToInsert.lines.set(idEnvioTemporal, [
|
|
39
|
+
{
|
|
40
|
+
id_envio: idEnvioTemporal,
|
|
41
|
+
bulto: {
|
|
42
|
+
peso: group.weight,
|
|
43
|
+
alto: group.height,
|
|
44
|
+
ancho: group.width,
|
|
45
|
+
largo: group.length,
|
|
46
|
+
sku_producto: ''
|
|
47
|
+
}
|
|
59
48
|
}
|
|
60
|
-
|
|
49
|
+
]);
|
|
61
50
|
};
|
|
62
51
|
this.services = {
|
|
63
52
|
integrationsService: params.integrationsService,
|
|
@@ -70,7 +59,7 @@ class IntegrationManager {
|
|
|
70
59
|
warehouseService: params.warehouseService
|
|
71
60
|
};
|
|
72
61
|
this.dataToInsert = {
|
|
73
|
-
lines:
|
|
62
|
+
lines: new Map(),
|
|
74
63
|
contents: [],
|
|
75
64
|
details: [],
|
|
76
65
|
externals: [],
|
|
@@ -81,7 +70,7 @@ class IntegrationManager {
|
|
|
81
70
|
this.type = params.type;
|
|
82
71
|
this.executionManager = params.executionManager;
|
|
83
72
|
this.findNextPickupDate = params.findNextPickupDate;
|
|
84
|
-
this.
|
|
73
|
+
this.getModifiedOrderBasedOnRules = params.getModifiedOrderBasedOnRules;
|
|
85
74
|
}
|
|
86
75
|
insertTempShipments(_a) {
|
|
87
76
|
return __awaiter(this, arguments, void 0, function* ({ payload: { idUsuario, agencyId, addressId, warehouseId, group, sequentialInsertion = false }, fetchAllOrders, crearBulto, modificarOrdenOriginal }) {
|
|
@@ -92,6 +81,11 @@ class IntegrationManager {
|
|
|
92
81
|
? yield addressesService.getWarehouseAddress(idUsuario)
|
|
93
82
|
: yield addressesService.getAddress(addressId, idUsuario);
|
|
94
83
|
const country = yield this.services.countriesService.getCountry(sellerAddress.id_pais);
|
|
84
|
+
const userIntegrationConfiguration = yield integrationsService.getDefaultIntegrationsData({
|
|
85
|
+
id_usuario: idUsuario,
|
|
86
|
+
id_integracion: this.integrationType
|
|
87
|
+
});
|
|
88
|
+
const shouldFindWarehouseSkus = !!(userIntegrationConfiguration === null || userIntegrationConfiguration === void 0 ? void 0 : userIntegrationConfiguration.sku_almacen);
|
|
95
89
|
const allOrders = yield fetchAllOrders();
|
|
96
90
|
const filteredOrders = yield integrationsService.filterExternalDuplicatedOrders({
|
|
97
91
|
id_usuario: idUsuario,
|
|
@@ -121,17 +115,61 @@ class IntegrationManager {
|
|
|
121
115
|
if (parsedOrder.direccionSalidaId) {
|
|
122
116
|
direccionSalida = yield addressesService.getAddress(parsedOrder.direccionSalidaId, idUsuario);
|
|
123
117
|
}
|
|
124
|
-
const
|
|
118
|
+
const tempShipmentData = yield (0, insertTempShipment_1.cookTempShipment)({
|
|
125
119
|
order: parsedOrder,
|
|
120
|
+
shipmentType: this.shipmentType,
|
|
126
121
|
agencyId,
|
|
127
122
|
warehouseId,
|
|
128
|
-
sellerAddress
|
|
123
|
+
sellerAddress,
|
|
129
124
|
idUsuario,
|
|
125
|
+
findNextPickupDate: this.findNextPickupDate,
|
|
126
|
+
countriesService: this.services.countriesService,
|
|
130
127
|
isPriority: isPriorityByDefault
|
|
131
128
|
});
|
|
129
|
+
const idEnvioTemporal = yield this.services.integrationsService.insertTempShipment(tempShipmentData, false);
|
|
132
130
|
if (!idEnvioTemporal) {
|
|
133
131
|
throw new Error('Temp shipments could not be created');
|
|
134
132
|
}
|
|
133
|
+
yield this.insertQuantityRelatedLines({
|
|
134
|
+
orders: [Object.assign(Object.assign({}, parsedOrder), { idEnvioTemporal })],
|
|
135
|
+
idUsuario,
|
|
136
|
+
crearBulto,
|
|
137
|
+
group,
|
|
138
|
+
warehouseId,
|
|
139
|
+
shouldFindWarehouseSkus
|
|
140
|
+
});
|
|
141
|
+
const shipmentDetails = this.dataToInsert.details.filter(({ id_envio }) => {
|
|
142
|
+
return id_envio === order.idEnvioTemporal;
|
|
143
|
+
});
|
|
144
|
+
const { partialTempShipmentModified, packagesModified } = yield this.getModifiedOrderBasedOnRules({
|
|
145
|
+
defaultAgencyId: agencyId,
|
|
146
|
+
userId: idUsuario,
|
|
147
|
+
integrationId: this.integrationType,
|
|
148
|
+
parsedOrder,
|
|
149
|
+
sellerAddress,
|
|
150
|
+
shipmentDetails,
|
|
151
|
+
tempShipmentData,
|
|
152
|
+
packages: this.dataToInsert.lines.get(idEnvioTemporal) || []
|
|
153
|
+
});
|
|
154
|
+
const isTempShipmentModified = !!partialTempShipmentModified &&
|
|
155
|
+
Object.keys(partialTempShipmentModified).length > 0;
|
|
156
|
+
const arePackagesModified = !!packagesModified && packagesModified.length > 0;
|
|
157
|
+
const shouldAbortCreation = isTempShipmentModified &&
|
|
158
|
+
partialTempShipmentModified.abort_creation === true;
|
|
159
|
+
if (shouldAbortCreation) {
|
|
160
|
+
yield integrationsService.deleteTempShipment({
|
|
161
|
+
id_envio: idEnvioTemporal,
|
|
162
|
+
id_usuario: idUsuario
|
|
163
|
+
});
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (isTempShipmentModified) {
|
|
167
|
+
delete partialTempShipmentModified.abort_creation; // por si acaso
|
|
168
|
+
yield integrationsService.updateTempShimpment(Object.assign({ id_envio: idEnvioTemporal }, tempShipmentData));
|
|
169
|
+
}
|
|
170
|
+
if (arePackagesModified) {
|
|
171
|
+
this.dataToInsert.lines.set(idEnvioTemporal, packagesModified);
|
|
172
|
+
}
|
|
135
173
|
this.insertExternalShipment({
|
|
136
174
|
idUsuario,
|
|
137
175
|
order: parsedOrder,
|
|
@@ -142,56 +180,22 @@ class IntegrationManager {
|
|
|
142
180
|
regularExecutionTime: 8000, // ms
|
|
143
181
|
initialBatchQuantity: sequentialInsertion ? 1 : 5 // si la insercion no es secuencial, de cinco en cinco
|
|
144
182
|
}).upload();
|
|
145
|
-
const successfullShipments = insertOrdersResult
|
|
146
|
-
.filter(Boolean)
|
|
147
|
-
.filter((each) => each.status === 'fulfilled')
|
|
148
|
-
.map((each) => each.value);
|
|
149
|
-
const userIntegrationConfiguration = yield integrationsService.getDefaultIntegrationsData({
|
|
150
|
-
id_usuario: idUsuario,
|
|
151
|
-
id_integracion: this.integrationType
|
|
152
|
-
});
|
|
153
|
-
const shouldFindWarehouseSkus = !!(userIntegrationConfiguration === null || userIntegrationConfiguration === void 0 ? void 0 : userIntegrationConfiguration.sku_almacen);
|
|
154
|
-
yield this.insertQuantityRelatedLines({
|
|
155
|
-
orders: successfullShipments,
|
|
156
|
-
idUsuario,
|
|
157
|
-
crearBulto,
|
|
158
|
-
group,
|
|
159
|
-
warehouseId,
|
|
160
|
-
shouldFindWarehouseSkus
|
|
161
|
-
});
|
|
162
183
|
yield this.insertUserDefaultConfigurationContent({
|
|
163
184
|
user,
|
|
164
|
-
tempShimpmentIds: successfullShipments.map((successfullShipment) => successfullShipment.idEnvioTemporal),
|
|
165
185
|
countryName: country.nombre_pais_en
|
|
166
186
|
});
|
|
167
|
-
|
|
168
|
-
yield Promise.allSettled(successfullShipments.map((order) => __awaiter(this, void 0, void 0, function* () {
|
|
169
|
-
const shipmentDetails = this.dataToInsert.details.filter(({ id_envio }) => {
|
|
170
|
-
return id_envio === order.idEnvioTemporal;
|
|
171
|
-
});
|
|
172
|
-
const newAgencyId = yield this.getAgencyIdForTempShipment({
|
|
173
|
-
defaultAgencyId: agencyId,
|
|
174
|
-
userId: idUsuario,
|
|
175
|
-
integrationId: this.integrationType,
|
|
176
|
-
tempShipmentData: order,
|
|
177
|
-
sellerAddress,
|
|
178
|
-
shipmentDetails
|
|
179
|
-
});
|
|
180
|
-
if (newAgencyId !== order.agencyId) {
|
|
181
|
-
yield integrationsService.updateTempShimpment({
|
|
182
|
-
id_envio: order.idEnvioTemporal,
|
|
183
|
-
id_usuario: idUsuario,
|
|
184
|
-
id_agencia: newAgencyId
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
})));
|
|
187
|
+
const arrayPackages = Array.from(Object.values(this.dataToInsert.lines)).flat();
|
|
188
188
|
yield Promise.allSettled([
|
|
189
|
-
integrationsService.insertTempShipmentMultipleLines(
|
|
189
|
+
integrationsService.insertTempShipmentMultipleLines(arrayPackages),
|
|
190
190
|
integrationsService.insertTempShipmentMultipleDetails(this.dataToInsert.details),
|
|
191
191
|
integrationsService.insertTempShipmentMultipleContents(this.dataToInsert.contents),
|
|
192
192
|
integrationsService.insertTempShipmentMultipleExternals(this.dataToInsert.externals),
|
|
193
193
|
integrationsService.insertTempShipmentMultipleJsonQuantityReferences(this.dataToInsert.jsonQuantityReferences)
|
|
194
194
|
]);
|
|
195
|
+
const successfullShipments = insertOrdersResult
|
|
196
|
+
.filter(Boolean)
|
|
197
|
+
.filter((each) => each.status === 'fulfilled')
|
|
198
|
+
.map((each) => each.value);
|
|
195
199
|
return {
|
|
196
200
|
filteredOrders,
|
|
197
201
|
insertedTempShipments: successfullShipments,
|
|
@@ -200,7 +204,7 @@ class IntegrationManager {
|
|
|
200
204
|
});
|
|
201
205
|
}
|
|
202
206
|
insertUserDefaultConfigurationContent(_a) {
|
|
203
|
-
return __awaiter(this, arguments, void 0, function* ({ user,
|
|
207
|
+
return __awaiter(this, arguments, void 0, function* ({ user, countryName }) {
|
|
204
208
|
const idUsuario = user.id_usuario;
|
|
205
209
|
const { configurationService, llmAPIService } = this.services;
|
|
206
210
|
const insert = (data) => {
|
|
@@ -302,18 +306,22 @@ class IntegrationManager {
|
|
|
302
306
|
insertQuantityRelatedLines(_a) {
|
|
303
307
|
return __awaiter(this, arguments, void 0, function* ({ orders, idUsuario, crearBulto, group, warehouseId, shouldFindWarehouseSkus }) {
|
|
304
308
|
/*
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
+
En esta funcion se calculan los records a incluir en la tabla de detalles y en las de lineas
|
|
310
|
+
Los detalles, es facil, una por cada bulto (aqui no hay ni "agrupar" ni nada de eso, una por cada bulto y ya esta)
|
|
311
|
+
Las lineas, es mas complicado:
|
|
312
|
+
Creamos una linea por cada linea en el pedido.
|
|
313
|
+
Pero hay TRUCO, puede que haya que agrupar, puede que no, puede que haya que coger el peso y medidas del bulto, puede que del "bulto agrupado", hay que tener en cuenta las cantidades...
|
|
314
|
+
Asi que para ver todo esto, la logica esta dentro de la variable "shouldGroup".
|
|
309
315
|
*/
|
|
316
|
+
var _b;
|
|
310
317
|
const { integrationsService } = this.services;
|
|
311
|
-
const
|
|
318
|
+
const shouldGetMeasuresFromGroupBeingOneLine = !!((_b = (yield integrationsService.getDefaultIntegrationsData({
|
|
312
319
|
id_usuario: idUsuario,
|
|
313
320
|
id_integracion: this.integrationType
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
const createBultoFromLine = (
|
|
321
|
+
}))) === null || _b === void 0 ? void 0 : _b.dimensiones_medidas_configuradas);
|
|
322
|
+
// function to create package from line
|
|
323
|
+
const createBultoFromLine = (_c) => __awaiter(this, [_c], void 0, function* ({ line, idEnvioTemporal }) {
|
|
324
|
+
// funcion para crear bulto
|
|
317
325
|
const result = yield crearBulto({
|
|
318
326
|
lineItems: { idEnvioTemporal, line },
|
|
319
327
|
idUsuario
|
|
@@ -324,94 +332,96 @@ class IntegrationManager {
|
|
|
324
332
|
bulto,
|
|
325
333
|
cantidad,
|
|
326
334
|
idEnvioTemporal,
|
|
327
|
-
forzarAgrupamientoDeLinea: !!forzarAgrupamientoDeLinea
|
|
328
|
-
shouldGroup
|
|
335
|
+
forzarAgrupamientoDeLinea: !!forzarAgrupamientoDeLinea
|
|
329
336
|
};
|
|
330
337
|
}
|
|
331
338
|
return null;
|
|
332
339
|
});
|
|
333
|
-
|
|
334
|
-
|
|
340
|
+
yield Promise.allSettled(
|
|
341
|
+
// por cada pedido/envio
|
|
342
|
+
orders.map((_d) => __awaiter(this, [_d], void 0, function* ({ idEnvioTemporal, lineas }) {
|
|
335
343
|
if (!idEnvioTemporal)
|
|
336
344
|
return;
|
|
337
|
-
//
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
const shouldGroup = ((group === null || group === void 0 ? void 0 : group.grouped) === 1 && lineas.length > 1) ||
|
|
341
|
-
((group === null || group === void 0 ? void 0 : group.grouped) === 1 && lineas.length === 1 && measuresFromConfig);
|
|
342
|
-
if (shouldGroup) {
|
|
343
|
-
this.insertTempShipmentGroupLine({ idEnvioTemporal, group });
|
|
344
|
-
}
|
|
345
|
-
lineas.forEach((line) => {
|
|
346
|
-
const bultoPromise = createBultoFromLine({
|
|
345
|
+
// hacemos esto primero, todavia no sabemos si vamos a agrupar (porque necesitamos crear el bulto para eso, ya que la "cantidad" podria influir)
|
|
346
|
+
const bultosYCantidades = (yield Promise.all(lineas.map((line) => __awaiter(this, void 0, void 0, function* () {
|
|
347
|
+
const bultoYCantidad = yield createBultoFromLine({
|
|
347
348
|
line,
|
|
348
|
-
idEnvioTemporal
|
|
349
|
-
shouldGroup
|
|
350
|
-
});
|
|
351
|
-
obtenerBultosYCantidades.push(bultoPromise);
|
|
352
|
-
});
|
|
353
|
-
});
|
|
354
|
-
let bultosYCantidades = yield Promise.all(obtenerBultosYCantidades);
|
|
355
|
-
bultosYCantidades = bultosYCantidades.filter(Boolean);
|
|
356
|
-
if (shouldFindWarehouseSkus) {
|
|
357
|
-
const bultosYCantidadesGroupedByShipment = bultosYCantidades.reduce((acc, curr) => {
|
|
358
|
-
var _a;
|
|
359
|
-
const tempShipmentId = curr === null || curr === void 0 ? void 0 : curr.idEnvioTemporal;
|
|
360
|
-
if (!tempShipmentId)
|
|
361
|
-
return acc;
|
|
362
|
-
(_a = acc[tempShipmentId]) !== null && _a !== void 0 ? _a : (acc[tempShipmentId] = []);
|
|
363
|
-
acc[tempShipmentId].push(curr);
|
|
364
|
-
return acc;
|
|
365
|
-
}, {});
|
|
366
|
-
yield Promise.all(Object.entries(bultosYCantidadesGroupedByShipment).map((_c) => __awaiter(this, [_c], void 0, function* ([tempShipmentId, bultosYCantidades]) {
|
|
367
|
-
const json_referencias_cantidades = yield this.createJsonQuantityReferencesFromLines({
|
|
368
|
-
idUsuario,
|
|
369
|
-
warehouseId,
|
|
370
|
-
bultosYCantidades
|
|
349
|
+
idEnvioTemporal
|
|
371
350
|
});
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
bultosYCantidades.forEach((each) => {
|
|
381
|
-
if (!each)
|
|
382
|
-
return;
|
|
383
|
-
const { bulto, cantidad, idEnvioTemporal, forzarAgrupamientoDeLinea, shouldGroup } = each;
|
|
384
|
-
try {
|
|
351
|
+
return bultoYCantidad;
|
|
352
|
+
})))).filter(Boolean);
|
|
353
|
+
// sacamos todos los bultos (y cantidad)
|
|
354
|
+
yield Promise.allSettled(bultosYCantidades.map((bultoYCantidad) => __awaiter(this, void 0, void 0, function* () {
|
|
355
|
+
if (!bultoYCantidad)
|
|
356
|
+
return;
|
|
357
|
+
const { bulto, cantidad, idEnvioTemporal, forzarAgrupamientoDeLinea } = bultoYCantidad;
|
|
358
|
+
// metemos los detalles siempre
|
|
385
359
|
this.dataToInsert.details.push({
|
|
386
360
|
id_envio: idEnvioTemporal,
|
|
387
361
|
bulto,
|
|
388
362
|
cantidad
|
|
389
363
|
});
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
364
|
+
// Debe agrupar si:
|
|
365
|
+
// 1. grouped es 1 + hay más de una línea
|
|
366
|
+
// 2. grouped es 1 + hay una línea + configuración integraciones tiene el campo dimensiones_medidas_configuradas a 1 (osea, que queremos que coja las dimensiones del bulto, aunque sea una sola linea)
|
|
367
|
+
// 3. grouped es 1 + hay una lines + cantidad es mayor que 1
|
|
368
|
+
const shouldGroup = (group === null || group === void 0 ? void 0 : group.grouped) === 1 &&
|
|
369
|
+
(lineas.length > 1 ||
|
|
370
|
+
(lineas.length === 1 &&
|
|
371
|
+
shouldGetMeasuresFromGroupBeingOneLine) ||
|
|
372
|
+
(lineas.length === 1 && Number(cantidad) > 1));
|
|
373
|
+
// si agrupamos, metemos solo una linea en "lineas" y cogemos las dimensiones y peso de "group" (lo que el usuario tenga por defecto)
|
|
374
|
+
if (shouldGroup) {
|
|
375
|
+
this.insertTempShipmentGroupLine({ idEnvioTemporal, group });
|
|
376
|
+
// si no agrupamos, cogemos el peso y dimensiones del peso mismo
|
|
395
377
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
378
|
+
else {
|
|
379
|
+
// lo de "forzarAgrupamientoDeLinea" se hizo solo para un usuario (Pegaso) que ya ni siquiera esta. Lo que hace es que siempre mete una linea, sin tener en cuenta la cantidad, pero con las dimensiones que les hemos preparado para ese bulto (que es una suma de todos los bultos reales... un chanchullo)
|
|
380
|
+
const tempShipmentLines = this.dataToInsert.lines.get(idEnvioTemporal) || [];
|
|
381
|
+
if (forzarAgrupamientoDeLinea) {
|
|
382
|
+
tempShipmentLines.push({
|
|
383
|
+
id_envio: idEnvioTemporal,
|
|
384
|
+
bulto
|
|
385
|
+
});
|
|
386
|
+
// y esto para todo el resto de usuarios
|
|
399
387
|
}
|
|
400
388
|
else {
|
|
389
|
+
// creamos una linea por cada una de las unidades de "cantidad" (si cantidad es 5, pues 5 lineas -todas iguales, con el peso y medidas del bulto-)
|
|
401
390
|
const arrayFake = Array.from({ length: Number(cantidad) });
|
|
402
|
-
arrayFake.
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
});
|
|
391
|
+
const linesToAdd = arrayFake.map(() => ({
|
|
392
|
+
id_envio: idEnvioTemporal,
|
|
393
|
+
bulto
|
|
394
|
+
}));
|
|
395
|
+
tempShipmentLines.push(...linesToAdd);
|
|
408
396
|
}
|
|
409
397
|
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
398
|
+
// para cuando el usuario quiere mapear su sku con la referencia que tenga en almacen
|
|
399
|
+
if (shouldFindWarehouseSkus) {
|
|
400
|
+
const bultosYCantidadesGroupedByShipment = bultosYCantidades.reduce((acc, curr) => {
|
|
401
|
+
var _a;
|
|
402
|
+
const tempShipmentId = curr === null || curr === void 0 ? void 0 : curr.idEnvioTemporal;
|
|
403
|
+
if (!tempShipmentId)
|
|
404
|
+
return acc;
|
|
405
|
+
(_a = acc[tempShipmentId]) !== null && _a !== void 0 ? _a : (acc[tempShipmentId] = []);
|
|
406
|
+
acc[tempShipmentId].push(curr);
|
|
407
|
+
return acc;
|
|
408
|
+
}, {});
|
|
409
|
+
yield Promise.all(Object.entries(bultosYCantidadesGroupedByShipment).map((_e) => __awaiter(this, [_e], void 0, function* ([tempShipmentId, bultosYCantidades]) {
|
|
410
|
+
const json_referencias_cantidades = yield this.createJsonQuantityReferencesFromLines({
|
|
411
|
+
idUsuario,
|
|
412
|
+
warehouseId,
|
|
413
|
+
bultosYCantidades
|
|
414
|
+
});
|
|
415
|
+
if (json_referencias_cantidades.length > 0) {
|
|
416
|
+
this.dataToInsert.jsonQuantityReferences.push({
|
|
417
|
+
id_envio: Number(tempShipmentId),
|
|
418
|
+
json_referencias_cantidades
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
})));
|
|
422
|
+
}
|
|
423
|
+
})));
|
|
424
|
+
})));
|
|
415
425
|
});
|
|
416
426
|
}
|
|
417
427
|
}
|
package/index.ts
CHANGED
|
@@ -23,14 +23,15 @@ import {
|
|
|
23
23
|
ShipmentType,
|
|
24
24
|
IntegrationType,
|
|
25
25
|
UserDefaultConfigurationContent,
|
|
26
|
-
|
|
26
|
+
GetModifiedOrderBasedOnRules,
|
|
27
27
|
IsPriority,
|
|
28
28
|
CustomizationService,
|
|
29
29
|
InsertContentShipmentLine,
|
|
30
30
|
Bulto,
|
|
31
31
|
DataToInsert,
|
|
32
32
|
BultoYCantidad,
|
|
33
|
-
JsonReferenciasCantidades
|
|
33
|
+
JsonReferenciasCantidades,
|
|
34
|
+
Shipment
|
|
34
35
|
} from './types';
|
|
35
36
|
|
|
36
37
|
interface Services {
|
|
@@ -51,7 +52,7 @@ export class IntegrationManager {
|
|
|
51
52
|
findNextPickupDate: FindNextPickupDate;
|
|
52
53
|
integrationType: IntegrationType;
|
|
53
54
|
shipmentType: ShipmentType;
|
|
54
|
-
|
|
55
|
+
getModifiedOrderBasedOnRules: GetModifiedOrderBasedOnRules;
|
|
55
56
|
|
|
56
57
|
dataToInsert: DataToInsert;
|
|
57
58
|
|
|
@@ -69,7 +70,7 @@ export class IntegrationManager {
|
|
|
69
70
|
shipmentType: ShipmentType;
|
|
70
71
|
executionManager: ExecutionManager;
|
|
71
72
|
findNextPickupDate: FindNextPickupDate;
|
|
72
|
-
|
|
73
|
+
getModifiedOrderBasedOnRules: GetModifiedOrderBasedOnRules;
|
|
73
74
|
}) {
|
|
74
75
|
this.services = {
|
|
75
76
|
integrationsService: params.integrationsService,
|
|
@@ -83,7 +84,7 @@ export class IntegrationManager {
|
|
|
83
84
|
};
|
|
84
85
|
|
|
85
86
|
this.dataToInsert = {
|
|
86
|
-
lines:
|
|
87
|
+
lines: new Map(),
|
|
87
88
|
contents: [],
|
|
88
89
|
details: [],
|
|
89
90
|
externals: [],
|
|
@@ -95,7 +96,7 @@ export class IntegrationManager {
|
|
|
95
96
|
this.type = params.type;
|
|
96
97
|
this.executionManager = params.executionManager;
|
|
97
98
|
this.findNextPickupDate = params.findNextPickupDate;
|
|
98
|
-
this.
|
|
99
|
+
this.getModifiedOrderBasedOnRules = params.getModifiedOrderBasedOnRules;
|
|
99
100
|
}
|
|
100
101
|
|
|
101
102
|
async insertTempShipments({
|
|
@@ -118,10 +119,19 @@ export class IntegrationManager {
|
|
|
118
119
|
warehouseId > 0
|
|
119
120
|
? await addressesService.getWarehouseAddress(idUsuario)
|
|
120
121
|
: await addressesService.getAddress(addressId, idUsuario);
|
|
122
|
+
|
|
121
123
|
const country = await this.services.countriesService.getCountry(
|
|
122
124
|
sellerAddress.id_pais
|
|
123
125
|
);
|
|
124
126
|
|
|
127
|
+
const userIntegrationConfiguration =
|
|
128
|
+
await integrationsService.getDefaultIntegrationsData({
|
|
129
|
+
id_usuario: idUsuario,
|
|
130
|
+
id_integracion: this.integrationType
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const shouldFindWarehouseSkus = !!userIntegrationConfiguration?.sku_almacen;
|
|
134
|
+
|
|
125
135
|
const allOrders = await fetchAllOrders();
|
|
126
136
|
const filteredOrders =
|
|
127
137
|
await integrationsService.filterExternalDuplicatedOrders({
|
|
@@ -167,19 +177,87 @@ export class IntegrationManager {
|
|
|
167
177
|
);
|
|
168
178
|
}
|
|
169
179
|
|
|
170
|
-
const
|
|
180
|
+
const tempShipmentData = await cookTempShipment({
|
|
171
181
|
order: parsedOrder,
|
|
182
|
+
shipmentType: this.shipmentType,
|
|
172
183
|
agencyId,
|
|
173
184
|
warehouseId,
|
|
174
|
-
sellerAddress
|
|
185
|
+
sellerAddress,
|
|
175
186
|
idUsuario,
|
|
187
|
+
findNextPickupDate: this.findNextPickupDate,
|
|
188
|
+
countriesService: this.services.countriesService,
|
|
176
189
|
isPriority: isPriorityByDefault
|
|
177
190
|
});
|
|
178
191
|
|
|
192
|
+
const idEnvioTemporal =
|
|
193
|
+
await this.services.integrationsService.insertTempShipment(
|
|
194
|
+
tempShipmentData,
|
|
195
|
+
false
|
|
196
|
+
);
|
|
197
|
+
|
|
179
198
|
if (!idEnvioTemporal) {
|
|
180
199
|
throw new Error('Temp shipments could not be created');
|
|
181
200
|
}
|
|
182
201
|
|
|
202
|
+
await this.insertQuantityRelatedLines({
|
|
203
|
+
orders: [{ ...parsedOrder, idEnvioTemporal }],
|
|
204
|
+
idUsuario,
|
|
205
|
+
crearBulto,
|
|
206
|
+
group,
|
|
207
|
+
warehouseId,
|
|
208
|
+
shouldFindWarehouseSkus
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const shipmentDetails = this.dataToInsert.details.filter(
|
|
212
|
+
({ id_envio }) => {
|
|
213
|
+
return id_envio === order.idEnvioTemporal;
|
|
214
|
+
}
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
const { partialTempShipmentModified, packagesModified } =
|
|
218
|
+
await this.getModifiedOrderBasedOnRules({
|
|
219
|
+
defaultAgencyId: agencyId,
|
|
220
|
+
userId: idUsuario,
|
|
221
|
+
integrationId: this.integrationType,
|
|
222
|
+
parsedOrder,
|
|
223
|
+
sellerAddress,
|
|
224
|
+
shipmentDetails,
|
|
225
|
+
tempShipmentData,
|
|
226
|
+
packages: this.dataToInsert.lines.get(idEnvioTemporal) || []
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
const isTempShipmentModified =
|
|
230
|
+
!!partialTempShipmentModified &&
|
|
231
|
+
Object.keys(partialTempShipmentModified).length > 0;
|
|
232
|
+
const arePackagesModified =
|
|
233
|
+
!!packagesModified && packagesModified.length > 0;
|
|
234
|
+
|
|
235
|
+
const shouldAbortCreation =
|
|
236
|
+
isTempShipmentModified &&
|
|
237
|
+
partialTempShipmentModified.abort_creation === true;
|
|
238
|
+
|
|
239
|
+
if (shouldAbortCreation) {
|
|
240
|
+
await integrationsService.deleteTempShipment({
|
|
241
|
+
id_envio: idEnvioTemporal,
|
|
242
|
+
id_usuario: idUsuario
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (isTempShipmentModified) {
|
|
249
|
+
delete partialTempShipmentModified.abort_creation; // por si acaso
|
|
250
|
+
|
|
251
|
+
await integrationsService.updateTempShimpment({
|
|
252
|
+
id_envio: idEnvioTemporal,
|
|
253
|
+
...tempShipmentData
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (arePackagesModified) {
|
|
258
|
+
this.dataToInsert.lines.set(idEnvioTemporal, packagesModified);
|
|
259
|
+
}
|
|
260
|
+
|
|
183
261
|
this.insertExternalShipment({
|
|
184
262
|
idUsuario,
|
|
185
263
|
order: parsedOrder,
|
|
@@ -192,70 +270,17 @@ export class IntegrationManager {
|
|
|
192
270
|
initialBatchQuantity: sequentialInsertion ? 1 : 5 // si la insercion no es secuencial, de cinco en cinco
|
|
193
271
|
}).upload();
|
|
194
272
|
|
|
195
|
-
const successfullShipments = insertOrdersResult
|
|
196
|
-
.filter(Boolean)
|
|
197
|
-
.filter((each) => each.status === 'fulfilled')
|
|
198
|
-
.map((each) => each.value) as (ModifiedOrder & {
|
|
199
|
-
idEnvioTemporal: number;
|
|
200
|
-
agencyId: number;
|
|
201
|
-
})[];
|
|
202
|
-
|
|
203
|
-
const userIntegrationConfiguration =
|
|
204
|
-
await integrationsService.getDefaultIntegrationsData({
|
|
205
|
-
id_usuario: idUsuario,
|
|
206
|
-
id_integracion: this.integrationType
|
|
207
|
-
});
|
|
208
|
-
const shouldFindWarehouseSkus = !!userIntegrationConfiguration?.sku_almacen;
|
|
209
|
-
|
|
210
|
-
await this.insertQuantityRelatedLines({
|
|
211
|
-
orders: successfullShipments,
|
|
212
|
-
idUsuario,
|
|
213
|
-
crearBulto,
|
|
214
|
-
group,
|
|
215
|
-
warehouseId,
|
|
216
|
-
shouldFindWarehouseSkus
|
|
217
|
-
});
|
|
218
|
-
|
|
219
273
|
await this.insertUserDefaultConfigurationContent({
|
|
220
274
|
user,
|
|
221
|
-
tempShimpmentIds: successfullShipments.map(
|
|
222
|
-
(successfullShipment) => successfullShipment.idEnvioTemporal
|
|
223
|
-
),
|
|
224
275
|
countryName: country.nombre_pais_en
|
|
225
276
|
});
|
|
226
277
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
const shipmentDetails = this.dataToInsert.details.filter(
|
|
231
|
-
({ id_envio }) => {
|
|
232
|
-
return id_envio === order.idEnvioTemporal;
|
|
233
|
-
}
|
|
234
|
-
);
|
|
235
|
-
|
|
236
|
-
const newAgencyId = await this.getAgencyIdForTempShipment({
|
|
237
|
-
defaultAgencyId: agencyId,
|
|
238
|
-
userId: idUsuario,
|
|
239
|
-
integrationId: this.integrationType,
|
|
240
|
-
tempShipmentData: order,
|
|
241
|
-
sellerAddress,
|
|
242
|
-
shipmentDetails
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
if (newAgencyId !== order.agencyId) {
|
|
246
|
-
await integrationsService.updateTempShimpment({
|
|
247
|
-
id_envio: order.idEnvioTemporal,
|
|
248
|
-
id_usuario: idUsuario,
|
|
249
|
-
id_agencia: newAgencyId
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
})
|
|
253
|
-
);
|
|
278
|
+
const arrayPackages: InsertContentShipmentLine[] = Array.from(
|
|
279
|
+
Object.values(this.dataToInsert.lines)
|
|
280
|
+
).flat();
|
|
254
281
|
|
|
255
282
|
await Promise.allSettled([
|
|
256
|
-
integrationsService.insertTempShipmentMultipleLines(
|
|
257
|
-
this.dataToInsert.lines
|
|
258
|
-
),
|
|
283
|
+
integrationsService.insertTempShipmentMultipleLines(arrayPackages),
|
|
259
284
|
integrationsService.insertTempShipmentMultipleDetails(
|
|
260
285
|
this.dataToInsert.details
|
|
261
286
|
),
|
|
@@ -270,6 +295,14 @@ export class IntegrationManager {
|
|
|
270
295
|
)
|
|
271
296
|
]);
|
|
272
297
|
|
|
298
|
+
const successfullShipments = insertOrdersResult
|
|
299
|
+
.filter(Boolean)
|
|
300
|
+
.filter((each) => each.status === 'fulfilled')
|
|
301
|
+
.map((each) => each.value) as (ModifiedOrder & {
|
|
302
|
+
idEnvioTemporal: number;
|
|
303
|
+
agencyId: number;
|
|
304
|
+
})[];
|
|
305
|
+
|
|
273
306
|
return {
|
|
274
307
|
filteredOrders,
|
|
275
308
|
insertedTempShipments: successfullShipments,
|
|
@@ -279,11 +312,9 @@ export class IntegrationManager {
|
|
|
279
312
|
|
|
280
313
|
async insertUserDefaultConfigurationContent({
|
|
281
314
|
user,
|
|
282
|
-
tempShimpmentIds,
|
|
283
315
|
countryName
|
|
284
316
|
}: {
|
|
285
317
|
user: Record<string, string | number>;
|
|
286
|
-
tempShimpmentIds: number[];
|
|
287
318
|
countryName: string;
|
|
288
319
|
}) {
|
|
289
320
|
const idUsuario = user.id_usuario as number;
|
|
@@ -374,39 +405,6 @@ export class IntegrationManager {
|
|
|
374
405
|
}
|
|
375
406
|
}
|
|
376
407
|
|
|
377
|
-
insertTempShipment = async ({
|
|
378
|
-
order,
|
|
379
|
-
agencyId,
|
|
380
|
-
warehouseId,
|
|
381
|
-
sellerAddress,
|
|
382
|
-
idUsuario,
|
|
383
|
-
isPriority
|
|
384
|
-
}: {
|
|
385
|
-
order: ModifiedOrder;
|
|
386
|
-
agencyId: number;
|
|
387
|
-
warehouseId: number;
|
|
388
|
-
sellerAddress: SellerAddress;
|
|
389
|
-
idUsuario: number;
|
|
390
|
-
isPriority: IsPriority;
|
|
391
|
-
}) => {
|
|
392
|
-
const tempShipmentData = await cookTempShipment({
|
|
393
|
-
order,
|
|
394
|
-
shipmentType: this.shipmentType,
|
|
395
|
-
agencyId,
|
|
396
|
-
warehouseId,
|
|
397
|
-
sellerAddress,
|
|
398
|
-
idUsuario,
|
|
399
|
-
findNextPickupDate: this.findNextPickupDate,
|
|
400
|
-
countriesService: this.services.countriesService,
|
|
401
|
-
isPriority
|
|
402
|
-
});
|
|
403
|
-
|
|
404
|
-
return await this.services.integrationsService.insertTempShipment(
|
|
405
|
-
tempShipmentData,
|
|
406
|
-
false
|
|
407
|
-
);
|
|
408
|
-
};
|
|
409
|
-
|
|
410
408
|
insertExternalShipment = ({
|
|
411
409
|
idUsuario,
|
|
412
410
|
order,
|
|
@@ -439,16 +437,19 @@ export class IntegrationManager {
|
|
|
439
437
|
idEnvioTemporal: number;
|
|
440
438
|
group: Group;
|
|
441
439
|
}) => {
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
440
|
+
// si agrupamos, no deberia de haber mas lineas, pero por si acaso, con el set machacamos lo que pudiera haber -que no deberia haber nada, insisto-
|
|
441
|
+
this.dataToInsert.lines.set(idEnvioTemporal, [
|
|
442
|
+
{
|
|
443
|
+
id_envio: idEnvioTemporal,
|
|
444
|
+
bulto: {
|
|
445
|
+
peso: group.weight,
|
|
446
|
+
alto: group.height,
|
|
447
|
+
ancho: group.width,
|
|
448
|
+
largo: group.length,
|
|
449
|
+
sku_producto: ''
|
|
450
|
+
}
|
|
450
451
|
}
|
|
451
|
-
|
|
452
|
+
]);
|
|
452
453
|
};
|
|
453
454
|
|
|
454
455
|
async createJsonQuantityReferencesFromLines({
|
|
@@ -517,32 +518,32 @@ export class IntegrationManager {
|
|
|
517
518
|
shouldFindWarehouseSkus: boolean;
|
|
518
519
|
}) {
|
|
519
520
|
/*
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
521
|
+
En esta funcion se calculan los records a incluir en la tabla de detalles y en las de lineas
|
|
522
|
+
Los detalles, es facil, una por cada bulto (aqui no hay ni "agrupar" ni nada de eso, una por cada bulto y ya esta)
|
|
523
|
+
Las lineas, es mas complicado:
|
|
524
|
+
Creamos una linea por cada linea en el pedido.
|
|
525
|
+
Pero hay TRUCO, puede que haya que agrupar, puede que no, puede que haya que coger el peso y medidas del bulto, puede que del "bulto agrupado", hay que tener en cuenta las cantidades...
|
|
526
|
+
Asi que para ver todo esto, la logica esta dentro de la variable "shouldGroup".
|
|
524
527
|
*/
|
|
525
528
|
|
|
526
529
|
const { integrationsService } = this.services;
|
|
527
530
|
|
|
528
|
-
const
|
|
531
|
+
const shouldGetMeasuresFromGroupBeingOneLine = !!(
|
|
529
532
|
await integrationsService.getDefaultIntegrationsData({
|
|
530
533
|
id_usuario: idUsuario,
|
|
531
534
|
id_integracion: this.integrationType
|
|
532
|
-
})
|
|
533
|
-
|
|
534
|
-
const measuresFromConfig =
|
|
535
|
-
!!userIntegrationConfiguration?.dimensiones_medidas_configuradas;
|
|
535
|
+
})
|
|
536
|
+
)?.dimensiones_medidas_configuradas;
|
|
536
537
|
|
|
538
|
+
// function to create package from line
|
|
537
539
|
const createBultoFromLine = async ({
|
|
538
540
|
line,
|
|
539
|
-
idEnvioTemporal
|
|
540
|
-
shouldGroup
|
|
541
|
+
idEnvioTemporal
|
|
541
542
|
}: {
|
|
542
543
|
line: InsertContentShipmentLine;
|
|
543
544
|
idEnvioTemporal: number;
|
|
544
|
-
shouldGroup: boolean;
|
|
545
545
|
}): Promise<BultoYCantidad | null> => {
|
|
546
|
+
// funcion para crear bulto
|
|
546
547
|
const result = await crearBulto({
|
|
547
548
|
lineItems: { idEnvioTemporal, line },
|
|
548
549
|
idUsuario
|
|
@@ -554,115 +555,127 @@ export class IntegrationManager {
|
|
|
554
555
|
bulto,
|
|
555
556
|
cantidad,
|
|
556
557
|
idEnvioTemporal,
|
|
557
|
-
forzarAgrupamientoDeLinea: !!forzarAgrupamientoDeLinea
|
|
558
|
-
shouldGroup
|
|
558
|
+
forzarAgrupamientoDeLinea: !!forzarAgrupamientoDeLinea
|
|
559
559
|
};
|
|
560
560
|
}
|
|
561
561
|
return null;
|
|
562
562
|
};
|
|
563
563
|
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
// Debe agrupar si:
|
|
570
|
-
// 1. grouped es 1 + hay más de una línea
|
|
571
|
-
// 2. grouped es 1 + hay una línea + configuración integraciones tiene el campo dimensiones_medidas_configuradas a 1
|
|
572
|
-
const shouldGroup =
|
|
573
|
-
(group?.grouped === 1 && lineas.length > 1) ||
|
|
574
|
-
(group?.grouped === 1 && lineas.length === 1 && measuresFromConfig);
|
|
575
|
-
|
|
576
|
-
if (shouldGroup) {
|
|
577
|
-
this.insertTempShipmentGroupLine({ idEnvioTemporal, group });
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
lineas.forEach((line) => {
|
|
581
|
-
const bultoPromise = createBultoFromLine({
|
|
582
|
-
line,
|
|
583
|
-
idEnvioTemporal,
|
|
584
|
-
shouldGroup
|
|
585
|
-
});
|
|
586
|
-
obtenerBultosYCantidades.push(bultoPromise);
|
|
587
|
-
});
|
|
588
|
-
});
|
|
564
|
+
await Promise.allSettled(
|
|
565
|
+
// por cada pedido/envio
|
|
566
|
+
orders.map(async ({ idEnvioTemporal, lineas }) => {
|
|
567
|
+
if (!idEnvioTemporal) return;
|
|
589
568
|
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
const tempShipmentId = curr?.idEnvioTemporal;
|
|
598
|
-
if (!tempShipmentId) return acc;
|
|
599
|
-
|
|
600
|
-
acc[tempShipmentId] ??= [];
|
|
601
|
-
acc[tempShipmentId].push(curr);
|
|
602
|
-
|
|
603
|
-
return acc;
|
|
604
|
-
}, {});
|
|
605
|
-
|
|
606
|
-
await Promise.all(
|
|
607
|
-
Object.entries(bultosYCantidadesGroupedByShipment).map(
|
|
608
|
-
async ([tempShipmentId, bultosYCantidades]) => {
|
|
609
|
-
const json_referencias_cantidades =
|
|
610
|
-
await this.createJsonQuantityReferencesFromLines({
|
|
611
|
-
idUsuario,
|
|
612
|
-
warehouseId,
|
|
613
|
-
bultosYCantidades
|
|
569
|
+
// hacemos esto primero, todavia no sabemos si vamos a agrupar (porque necesitamos crear el bulto para eso, ya que la "cantidad" podria influir)
|
|
570
|
+
const bultosYCantidades = (
|
|
571
|
+
await Promise.all(
|
|
572
|
+
lineas.map(async (line) => {
|
|
573
|
+
const bultoYCantidad = await createBultoFromLine({
|
|
574
|
+
line,
|
|
575
|
+
idEnvioTemporal
|
|
614
576
|
});
|
|
577
|
+
return bultoYCantidad;
|
|
578
|
+
})
|
|
579
|
+
)
|
|
580
|
+
).filter(Boolean);
|
|
581
|
+
|
|
582
|
+
// sacamos todos los bultos (y cantidad)
|
|
583
|
+
await Promise.allSettled(
|
|
584
|
+
bultosYCantidades.map(async (bultoYCantidad) => {
|
|
585
|
+
if (!bultoYCantidad) return;
|
|
586
|
+
|
|
587
|
+
const {
|
|
588
|
+
bulto,
|
|
589
|
+
cantidad,
|
|
590
|
+
idEnvioTemporal,
|
|
591
|
+
forzarAgrupamientoDeLinea
|
|
592
|
+
} = bultoYCantidad;
|
|
593
|
+
|
|
594
|
+
// metemos los detalles siempre
|
|
595
|
+
this.dataToInsert.details.push({
|
|
596
|
+
id_envio: idEnvioTemporal,
|
|
597
|
+
bulto,
|
|
598
|
+
cantidad
|
|
599
|
+
});
|
|
615
600
|
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
601
|
+
// Debe agrupar si:
|
|
602
|
+
// 1. grouped es 1 + hay más de una línea
|
|
603
|
+
// 2. grouped es 1 + hay una línea + configuración integraciones tiene el campo dimensiones_medidas_configuradas a 1 (osea, que queremos que coja las dimensiones del bulto, aunque sea una sola linea)
|
|
604
|
+
// 3. grouped es 1 + hay una lines + cantidad es mayor que 1
|
|
605
|
+
const shouldGroup =
|
|
606
|
+
group?.grouped === 1 &&
|
|
607
|
+
(lineas.length > 1 ||
|
|
608
|
+
(lineas.length === 1 &&
|
|
609
|
+
shouldGetMeasuresFromGroupBeingOneLine) ||
|
|
610
|
+
(lineas.length === 1 && Number(cantidad) > 1));
|
|
611
|
+
|
|
612
|
+
// si agrupamos, metemos solo una linea en "lineas" y cogemos las dimensiones y peso de "group" (lo que el usuario tenga por defecto)
|
|
613
|
+
if (shouldGroup) {
|
|
614
|
+
this.insertTempShipmentGroupLine({ idEnvioTemporal, group });
|
|
615
|
+
// si no agrupamos, cogemos el peso y dimensiones del peso mismo
|
|
616
|
+
} else {
|
|
617
|
+
// lo de "forzarAgrupamientoDeLinea" se hizo solo para un usuario (Pegaso) que ya ni siquiera esta. Lo que hace es que siempre mete una linea, sin tener en cuenta la cantidad, pero con las dimensiones que les hemos preparado para ese bulto (que es una suma de todos los bultos reales... un chanchullo)
|
|
618
|
+
const tempShipmentLines =
|
|
619
|
+
this.dataToInsert.lines.get(idEnvioTemporal) || [];
|
|
620
|
+
|
|
621
|
+
if (forzarAgrupamientoDeLinea) {
|
|
622
|
+
tempShipmentLines.push({
|
|
623
|
+
id_envio: idEnvioTemporal,
|
|
624
|
+
bulto
|
|
625
|
+
});
|
|
626
|
+
// y esto para todo el resto de usuarios
|
|
627
|
+
} else {
|
|
628
|
+
// creamos una linea por cada una de las unidades de "cantidad" (si cantidad es 5, pues 5 lineas -todas iguales, con el peso y medidas del bulto-)
|
|
629
|
+
|
|
630
|
+
const arrayFake = Array.from({ length: Number(cantidad) });
|
|
631
|
+
const linesToAdd = arrayFake.map(() => ({
|
|
632
|
+
id_envio: idEnvioTemporal,
|
|
633
|
+
bulto
|
|
634
|
+
}));
|
|
635
|
+
|
|
636
|
+
tempShipmentLines.push(...linesToAdd);
|
|
637
|
+
}
|
|
621
638
|
}
|
|
622
|
-
}
|
|
623
|
-
)
|
|
624
|
-
);
|
|
625
|
-
}
|
|
626
639
|
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
shouldGroup
|
|
635
|
-
} = each;
|
|
636
|
-
try {
|
|
637
|
-
this.dataToInsert.details.push({
|
|
638
|
-
id_envio: idEnvioTemporal,
|
|
639
|
-
bulto,
|
|
640
|
-
cantidad
|
|
641
|
-
});
|
|
642
|
-
if (!shouldGroup && forzarAgrupamientoDeLinea) {
|
|
643
|
-
this.dataToInsert.lines.push({
|
|
644
|
-
id_envio: idEnvioTemporal,
|
|
645
|
-
bulto
|
|
646
|
-
});
|
|
647
|
-
}
|
|
640
|
+
// para cuando el usuario quiere mapear su sku con la referencia que tenga en almacen
|
|
641
|
+
if (shouldFindWarehouseSkus) {
|
|
642
|
+
const bultosYCantidadesGroupedByShipment =
|
|
643
|
+
bultosYCantidades.reduce<Record<number, BultoYCantidad[]>>(
|
|
644
|
+
(acc, curr) => {
|
|
645
|
+
const tempShipmentId = curr?.idEnvioTemporal;
|
|
646
|
+
if (!tempShipmentId) return acc;
|
|
648
647
|
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
this.insertTempShipmentGroupLine({ idEnvioTemporal, group });
|
|
652
|
-
} else {
|
|
653
|
-
const arrayFake = Array.from({ length: Number(cantidad) });
|
|
648
|
+
acc[tempShipmentId] ??= [];
|
|
649
|
+
acc[tempShipmentId].push(curr);
|
|
654
650
|
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
651
|
+
return acc;
|
|
652
|
+
},
|
|
653
|
+
{}
|
|
654
|
+
);
|
|
655
|
+
|
|
656
|
+
await Promise.all(
|
|
657
|
+
Object.entries(bultosYCantidadesGroupedByShipment).map(
|
|
658
|
+
async ([tempShipmentId, bultosYCantidades]) => {
|
|
659
|
+
const json_referencias_cantidades =
|
|
660
|
+
await this.createJsonQuantityReferencesFromLines({
|
|
661
|
+
idUsuario,
|
|
662
|
+
warehouseId,
|
|
663
|
+
bultosYCantidades
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
if (json_referencias_cantidades.length > 0) {
|
|
667
|
+
this.dataToInsert.jsonQuantityReferences.push({
|
|
668
|
+
id_envio: Number(tempShipmentId),
|
|
669
|
+
json_referencias_cantidades
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
)
|
|
674
|
+
);
|
|
675
|
+
}
|
|
676
|
+
})
|
|
677
|
+
);
|
|
678
|
+
})
|
|
679
|
+
);
|
|
667
680
|
}
|
|
668
681
|
}
|
|
@@ -6,7 +6,8 @@ import {
|
|
|
6
6
|
CountriesService,
|
|
7
7
|
FindNextPickupDate,
|
|
8
8
|
SellerAddress,
|
|
9
|
-
IsPriority
|
|
9
|
+
IsPriority,
|
|
10
|
+
TempShipmentDataForCooking
|
|
10
11
|
} from 'types';
|
|
11
12
|
|
|
12
13
|
type ZonaLLegada = { nombre_pais: string; id_pais: number };
|
|
@@ -21,17 +22,7 @@ export const cookTempShipment = async ({
|
|
|
21
22
|
findNextPickupDate,
|
|
22
23
|
countriesService,
|
|
23
24
|
isPriority
|
|
24
|
-
}: {
|
|
25
|
-
order: ModifiedOrder;
|
|
26
|
-
shipmentType: ShipmentType;
|
|
27
|
-
agencyId: number;
|
|
28
|
-
warehouseId: number;
|
|
29
|
-
sellerAddress: SellerAddress;
|
|
30
|
-
idUsuario: number;
|
|
31
|
-
findNextPickupDate: FindNextPickupDate;
|
|
32
|
-
countriesService: CountriesService;
|
|
33
|
-
isPriority: IsPriority;
|
|
34
|
-
}): Promise<Shipment> => {
|
|
25
|
+
}: TempShipmentDataForCooking): Promise<Shipment> => {
|
|
35
26
|
let zonaLLegada = {};
|
|
36
27
|
if (order.codigoPaisLLegada)
|
|
37
28
|
zonaLLegada = await countriesService.getCountryFromCountryCode(
|
package/package.json
CHANGED
package/types/index.ts
CHANGED
|
@@ -109,7 +109,6 @@ export type BultoYCantidad = {
|
|
|
109
109
|
cantidad: Bulto['cantidad'];
|
|
110
110
|
idEnvioTemporal: number;
|
|
111
111
|
forzarAgrupamientoDeLinea: boolean;
|
|
112
|
-
shouldGroup: boolean;
|
|
113
112
|
} | null;
|
|
114
113
|
|
|
115
114
|
export type ModifiedOrder = {
|
|
@@ -234,7 +233,7 @@ export type IntegrationsService = {
|
|
|
234
233
|
) => Promise<number | false>;
|
|
235
234
|
|
|
236
235
|
insertTempShipmentMultipleLines: (
|
|
237
|
-
lines:
|
|
236
|
+
lines: InsertContentShipmentLine[]
|
|
238
237
|
) => Promise<number | false>;
|
|
239
238
|
|
|
240
239
|
insertTempShipmentMultipleDetails: (
|
|
@@ -255,12 +254,7 @@ export type IntegrationsService = {
|
|
|
255
254
|
id_envio,
|
|
256
255
|
id_usuario,
|
|
257
256
|
id_agencia
|
|
258
|
-
}: {
|
|
259
|
-
id_envio: number;
|
|
260
|
-
id_usuario: number;
|
|
261
|
-
id_agencia: number;
|
|
262
|
-
}) => Promise<boolean>;
|
|
263
|
-
|
|
257
|
+
}: Partial<Shipment & { id_envio: number }>) => Promise<boolean>;
|
|
264
258
|
getDefaultIntegrationsData: ({
|
|
265
259
|
id_usuario,
|
|
266
260
|
id_integracion
|
|
@@ -271,6 +265,13 @@ export type IntegrationsService = {
|
|
|
271
265
|
sku_almacen: 0 | 1;
|
|
272
266
|
dimensiones_medidas_configuradas: 0 | 1;
|
|
273
267
|
}>;
|
|
268
|
+
deleteTempShipment: ({
|
|
269
|
+
id_envio,
|
|
270
|
+
id_usuario
|
|
271
|
+
}: {
|
|
272
|
+
id_envio: number;
|
|
273
|
+
id_usuario: number;
|
|
274
|
+
}) => Promise<number | false>;
|
|
274
275
|
};
|
|
275
276
|
|
|
276
277
|
export type LlmAPIService = {
|
|
@@ -408,14 +409,19 @@ type ExecutionManagerUploadReturn = {
|
|
|
408
409
|
value: any;
|
|
409
410
|
};
|
|
410
411
|
|
|
411
|
-
export type
|
|
412
|
+
export type GetModifiedOrderBasedOnRules = (params: {
|
|
412
413
|
defaultAgencyId: number;
|
|
413
414
|
userId: number;
|
|
414
415
|
integrationId: number;
|
|
415
|
-
|
|
416
|
+
parsedOrder: ModifiedOrder;
|
|
416
417
|
sellerAddress: SellerAddress;
|
|
417
418
|
shipmentDetails: InsertContentShipmentDetail[];
|
|
418
|
-
|
|
419
|
+
tempShipmentData: Shipment;
|
|
420
|
+
packages: InsertContentShipmentLine[];
|
|
421
|
+
}) => Promise<{
|
|
422
|
+
partialTempShipmentModified: Partial<Shipment & { abort_creation?: boolean }>;
|
|
423
|
+
packagesModified: InsertContentShipmentLine[];
|
|
424
|
+
}>;
|
|
419
425
|
|
|
420
426
|
export type ExecutionManager = {
|
|
421
427
|
upload(): ExecutionManagerUploadReturn[];
|
|
@@ -423,7 +429,7 @@ export type ExecutionManager = {
|
|
|
423
429
|
};
|
|
424
430
|
|
|
425
431
|
export type DataToInsert = {
|
|
426
|
-
lines: InsertContentShipmentLine[]
|
|
432
|
+
lines: Map<number, InsertContentShipmentLine[]>;
|
|
427
433
|
contents: InsertContentShipmentData[];
|
|
428
434
|
details: InsertContentShipmentDetail[];
|
|
429
435
|
externals: InsertContentShipmentExternal[];
|
|
@@ -438,6 +444,18 @@ export type InsertContentShipmentLine = {
|
|
|
438
444
|
>;
|
|
439
445
|
};
|
|
440
446
|
|
|
447
|
+
export type TempShipmentDataForCooking = {
|
|
448
|
+
order: ModifiedOrder;
|
|
449
|
+
shipmentType: ShipmentType;
|
|
450
|
+
agencyId: number;
|
|
451
|
+
warehouseId: number;
|
|
452
|
+
sellerAddress: SellerAddress;
|
|
453
|
+
idUsuario: number;
|
|
454
|
+
findNextPickupDate: FindNextPickupDate;
|
|
455
|
+
countriesService: CountriesService;
|
|
456
|
+
isPriority: IsPriority;
|
|
457
|
+
};
|
|
458
|
+
|
|
441
459
|
type InsertContentShipmentData = UserDefaultConfigurationContent & {
|
|
442
460
|
id_envio: number;
|
|
443
461
|
};
|