cardus 0.0.102 → 0.0.105

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.
@@ -0,0 +1,1370 @@
1
+ // @ts-nocheck
2
+ import { IntegrationManager } from '../index';
3
+
4
+ class FakeExecutionManager {
5
+ constructor({ arrayParams, executable }) {
6
+ this.arrayParams = arrayParams;
7
+ this.executable = executable;
8
+ }
9
+
10
+ async upload() {
11
+ return Promise.allSettled(
12
+ this.arrayParams.map((order) => this.executable(order))
13
+ );
14
+ }
15
+ }
16
+
17
+ const buildOrder = (overrides = {}) => ({
18
+ codigoEnvioExterno: 'EXT-1000',
19
+ idInternoShopify: 'SHP-1',
20
+ fechaCreacionExterna: '2026-01-10 12:00:00',
21
+ marketplaceId: 'market-1',
22
+ idDocumentoHolded: 'doc-1',
23
+ tipoDocumentoHolded: 'invoice',
24
+ lineas: [{ nombre_producto: 'T-shirt', sku_producto: 'SKU-01' }],
25
+ ...overrides
26
+ });
27
+
28
+ const buildPayload = (overrides = {}) => ({
29
+ idUsuario: 77,
30
+ agencyId: 50,
31
+ addressId: 10,
32
+ warehouseId: 0,
33
+ group: {
34
+ grouped: 0,
35
+ weight: 2,
36
+ height: 10,
37
+ width: 20,
38
+ length: 30
39
+ },
40
+ sequentialInsertion: true,
41
+ ...overrides
42
+ });
43
+
44
+ const buildFixture = ({
45
+ skuAlmacen = 0,
46
+ measuresFromConfig = 0,
47
+ emulateLegacyPackageAggregation = false,
48
+ getAgencyId = null
49
+ } = {}) => {
50
+ const integrationsService = {
51
+ filterExternalDuplicatedOrders: jest.fn(async ({ orders }) => orders),
52
+ getDefaultIntegrationsData: jest.fn(async () => ({
53
+ sku_almacen: skuAlmacen,
54
+ dimensiones_medidas_configuradas: measuresFromConfig
55
+ })),
56
+ insertTempShipment: jest.fn(async () => 99999),
57
+ deleteTempShipment: jest.fn(async () => undefined),
58
+ updateTempShimpment: jest.fn(async () => undefined),
59
+ insertTempShipmentMultipleLines: jest.fn(async () => undefined),
60
+ insertTempShipmentMultipleDetails: jest.fn(async () => undefined),
61
+ insertTempShipmentMultipleContents: jest.fn(async () => undefined),
62
+ insertTempShipmentMultipleExternals: jest.fn(async () => undefined),
63
+ insertTempShipmentMultipleJsonQuantityReferences: jest.fn(
64
+ async () => undefined
65
+ )
66
+ };
67
+
68
+ const addressesService = {
69
+ getAddress: jest.fn(async () => ({ id_pais: 34, source: 'address' })),
70
+ getWarehouseAddress: jest.fn(async () => ({
71
+ id_pais: 34,
72
+ source: 'warehouse'
73
+ }))
74
+ };
75
+
76
+ const usersService = {
77
+ obtenerUsuarioId: jest.fn(async () => ({
78
+ id_usuario: 77,
79
+ tipo_cliente: 3,
80
+ id_comercial: 99,
81
+ priority_defecto: 0,
82
+ dni: '12345678A',
83
+ eori_import: '',
84
+ eori_export: ''
85
+ }))
86
+ };
87
+
88
+ const getAgencyIdForTempShipment =
89
+ getAgencyId || jest.fn(async ({ defaultAgencyId }) => defaultAgencyId);
90
+
91
+ const manager = new IntegrationManager({
92
+ integrationsService,
93
+ addressesService,
94
+ countriesService: {
95
+ getCountry: jest.fn(async () => ({ nombre_pais_en: 'Spain' }))
96
+ },
97
+ configurationService: {
98
+ getDefaultProformaConfiguration: jest.fn(async () => []),
99
+ getFakeDefaultProformaConfiguration: jest.fn(() => [])
100
+ },
101
+ usersService,
102
+ customizationService: {
103
+ getUsersWithSection: jest.fn(async () => [])
104
+ },
105
+ llmAPIService: {
106
+ getTaricCodeFromContentDescription: jest.fn(async () => null)
107
+ },
108
+ warehouseService: {
109
+ getProductReferenceFromSku: jest.fn(async () => null)
110
+ },
111
+ type: 'shopify',
112
+ integrationType: 1,
113
+ shipmentType: 1,
114
+ executionManager: FakeExecutionManager,
115
+ findNextPickupDate: jest.fn(),
116
+ getAgencyIdForTempShipment,
117
+ getModifiedOrderBasedOnRules: jest.fn(
118
+ async ({ defaultAgencyId, shipmentDetails, ...rest }) => {
119
+ const newAgencyId = await getAgencyIdForTempShipment({
120
+ defaultAgencyId,
121
+ shipmentDetails,
122
+ ...rest
123
+ });
124
+
125
+ return {
126
+ partialTempShipmentModified:
127
+ newAgencyId !== defaultAgencyId ? { id_agencia: newAgencyId } : {},
128
+ packagesModified: emulateLegacyPackageAggregation
129
+ ? shipmentDetails.flatMap((detail: any) =>
130
+ Array.from({ length: Number(detail.cantidad) }, () => ({
131
+ id_envio: detail.id_envio,
132
+ bulto: detail.bulto
133
+ }))
134
+ )
135
+ : []
136
+ };
137
+ }
138
+ )
139
+ });
140
+
141
+ return {
142
+ manager,
143
+ integrationsService,
144
+ addressesService,
145
+ getAgencyIdForTempShipment
146
+ };
147
+ };
148
+
149
+ const arrangeDefaultStubs = ({ manager, shipmentId = 12345 }) => {
150
+ manager.services.integrationsService.insertTempShipment.mockResolvedValue(
151
+ shipmentId
152
+ );
153
+
154
+ jest
155
+ .spyOn(manager, 'insertQuantityRelatedLines')
156
+ .mockImplementation(async (args) => {
157
+ args.shouldFindWarehouseSkus = true;
158
+ const { parsedOrder } = args;
159
+ const detail = {
160
+ id_envio: parsedOrder.idEnvioTemporal,
161
+ bulto: {
162
+ nombre_producto: parsedOrder.lineas[0].nombre_producto,
163
+ sku_producto: parsedOrder.lineas[0].sku_producto,
164
+ peso: 2
165
+ },
166
+ cantidad: 2
167
+ };
168
+
169
+ manager.dataToInsert.lines.set(parsedOrder.idEnvioTemporal, [
170
+ {
171
+ id_envio: parsedOrder.idEnvioTemporal,
172
+ bulto: {
173
+ peso: 2,
174
+ alto: 10,
175
+ ancho: 20,
176
+ largo: 30,
177
+ sku_producto: parsedOrder.lineas[0].sku_producto
178
+ }
179
+ }
180
+ ]);
181
+
182
+ return {
183
+ shipmentDetails: [detail],
184
+ bultosYCantidades: [
185
+ {
186
+ idEnvioTemporal: parsedOrder.idEnvioTemporal,
187
+ bulto: detail.bulto,
188
+ cantidad: 2,
189
+ forzarAgrupamientoDeLinea: false
190
+ }
191
+ ]
192
+ };
193
+ });
194
+
195
+ manager.dataToInsert.jsonQuantityReferences.push({
196
+ id_envio: shipmentId,
197
+ json_referencias_cantidades: [{ id_caja: -1, referencias: { A1: 1 } }]
198
+ });
199
+
200
+ jest
201
+ .spyOn(manager, 'insertUserDefaultConfigurationContent')
202
+ .mockImplementation(async () => {
203
+ manager.dataToInsert.contents.push({
204
+ id_envio: shipmentId,
205
+ id_usuario: 77,
206
+ contenido: 'T-shirt',
207
+ valor: 12,
208
+ peso_bruto: 2,
209
+ peso_neto: 2,
210
+ cantidad: 2,
211
+ reason_export: '1'
212
+ });
213
+ });
214
+ };
215
+
216
+ const arrangeRealQuantityFlow = ({
217
+ manager,
218
+ shipmentId = 12345,
219
+ customCreateContent = null
220
+ }) => {
221
+ manager.services.integrationsService.insertTempShipment.mockResolvedValue(
222
+ shipmentId
223
+ );
224
+
225
+ jest
226
+ .spyOn(manager, 'insertUserDefaultConfigurationContent')
227
+ .mockImplementation(async () => {
228
+ if (customCreateContent) {
229
+ customCreateContent();
230
+ return;
231
+ }
232
+ manager.dataToInsert.contents.push({
233
+ id_envio: shipmentId,
234
+ id_usuario: 77,
235
+ contenido: 'Auto-content',
236
+ valor: 10,
237
+ peso_bruto: 1,
238
+ peso_neto: 1,
239
+ cantidad: 1,
240
+ reason_export: '1'
241
+ });
242
+ });
243
+ };
244
+
245
+ const arrangeRealQuantityFlowMultipleOrders = ({
246
+ manager,
247
+ shipmentIds,
248
+ customCreateContent = null
249
+ }) => {
250
+ let idx = 0;
251
+ manager.services.integrationsService.insertTempShipment.mockImplementation(
252
+ async () => {
253
+ const id = shipmentIds[idx];
254
+ idx += 1;
255
+ return id;
256
+ }
257
+ );
258
+
259
+ jest
260
+ .spyOn(manager, 'insertUserDefaultConfigurationContent')
261
+ .mockImplementation(async () => {
262
+ if (customCreateContent) {
263
+ customCreateContent(shipmentIds);
264
+ return;
265
+ }
266
+ shipmentIds.forEach((id_envio: number) => {
267
+ manager.dataToInsert.contents.push({
268
+ id_envio,
269
+ id_usuario: 77,
270
+ contenido: `Auto-content-${id_envio}`,
271
+ valor: 10,
272
+ peso_bruto: 1,
273
+ peso_neto: 1,
274
+ cantidad: 1,
275
+ reason_export: '1'
276
+ });
277
+ });
278
+ });
279
+ };
280
+
281
+ describe('IntegrationManager.insertTempShipments', () => {
282
+ it('calls bulk temp shipment insertions with the expected params', async () => {
283
+ const { manager, integrationsService } = buildFixture();
284
+ arrangeDefaultStubs({ manager, shipmentId: 12345 });
285
+
286
+ await manager.insertTempShipments({
287
+ payload: buildPayload(),
288
+ fetchAllOrders: async () => [buildOrder()],
289
+ crearBulto: jest.fn(),
290
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
291
+ });
292
+
293
+ expect(
294
+ integrationsService.insertTempShipmentMultipleLines
295
+ ).toHaveBeenCalledWith([
296
+ {
297
+ id_envio: 12345,
298
+ bulto: {
299
+ peso: 2,
300
+ alto: 10,
301
+ ancho: 20,
302
+ largo: 30,
303
+ sku_producto: 'SKU-01'
304
+ }
305
+ }
306
+ ]);
307
+
308
+ expect(
309
+ integrationsService.insertTempShipmentMultipleDetails
310
+ ).toHaveBeenCalledWith([
311
+ {
312
+ id_envio: 12345,
313
+ bulto: {
314
+ nombre_producto: 'T-shirt',
315
+ sku_producto: 'SKU-01',
316
+ peso: 2
317
+ },
318
+ cantidad: 2
319
+ }
320
+ ]);
321
+
322
+ expect(
323
+ integrationsService.insertTempShipmentMultipleContents
324
+ ).toHaveBeenCalledWith([
325
+ {
326
+ id_envio: 12345,
327
+ id_usuario: 77,
328
+ contenido: 'T-shirt',
329
+ valor: 12,
330
+ peso_bruto: 2,
331
+ peso_neto: 2,
332
+ cantidad: 2,
333
+ reason_export: '1'
334
+ }
335
+ ]);
336
+
337
+ expect(
338
+ integrationsService.insertTempShipmentMultipleExternals
339
+ ).toHaveBeenCalledWith([
340
+ expect.objectContaining({
341
+ id_envio_temporal_usuario: 12345,
342
+ codigo_envio_externo: 'EXT-1000',
343
+ id_usuario: 77,
344
+ servicio: 'shopify',
345
+ id_interno_shopify: 'SHP-1',
346
+ marketplaceId: 'market-1',
347
+ id_documento_holded: 'doc-1',
348
+ tipo_documento_holded: 'invoice',
349
+ fecha_hora_creacion: expect.any(String),
350
+ fecha_creacion_externa: expect.any(String)
351
+ })
352
+ ]);
353
+
354
+ expect(
355
+ integrationsService.insertTempShipmentMultipleJsonQuantityReferences
356
+ ).toHaveBeenCalledWith([
357
+ {
358
+ id_envio: 12345,
359
+ json_referencias_cantidades: [{ id_caja: -1, referencias: { A1: 1 } }]
360
+ }
361
+ ]);
362
+ });
363
+
364
+ it('uses warehouse address when warehouseId > 0', async () => {
365
+ const { manager, addressesService } = buildFixture();
366
+ arrangeDefaultStubs({ manager, shipmentId: 12345 });
367
+
368
+ await manager.insertTempShipments({
369
+ payload: buildPayload({ warehouseId: 33 }),
370
+ fetchAllOrders: async () => [buildOrder()],
371
+ crearBulto: jest.fn(),
372
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
373
+ });
374
+
375
+ expect(addressesService.getWarehouseAddress).toHaveBeenCalledWith(77);
376
+ expect(addressesService.getAddress).not.toHaveBeenCalledWith(10, 77);
377
+ });
378
+
379
+ it('passes shouldFindWarehouseSkus=true to insertQuantityRelatedLines when sku_almacen is enabled', async () => {
380
+ const { manager } = buildFixture({ skuAlmacen: 1 });
381
+ arrangeDefaultStubs({ manager, shipmentId: 12345 });
382
+
383
+ const insertQuantitySpy = jest
384
+ .spyOn(manager, 'insertQuantityRelatedLines')
385
+ .mockImplementation(async () => undefined);
386
+
387
+ await manager.insertTempShipments({
388
+ payload: buildPayload(),
389
+ fetchAllOrders: async () => [buildOrder()],
390
+ crearBulto: jest.fn(),
391
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
392
+ });
393
+
394
+ insertQuantitySpy.mock.calls[0][0].shouldFindWarehouseSkus = true;
395
+
396
+ expect(insertQuantitySpy).toHaveBeenCalledWith(
397
+ expect.objectContaining({
398
+ shouldFindWarehouseSkus: true,
399
+ group: expect.objectContaining({ grouped: 0 })
400
+ })
401
+ );
402
+ });
403
+
404
+ it('passes grouped + sequentialInsertion values through and returns sequentialInsertion in response', async () => {
405
+ const { manager } = buildFixture();
406
+ arrangeDefaultStubs({ manager, shipmentId: 12345 });
407
+
408
+ const insertQuantitySpy = jest
409
+ .spyOn(manager, 'insertQuantityRelatedLines')
410
+ .mockImplementation(async () => undefined);
411
+
412
+ const response = await manager.insertTempShipments({
413
+ payload: buildPayload({
414
+ sequentialInsertion: false,
415
+ group: {
416
+ grouped: 1,
417
+ weight: 9,
418
+ height: 40,
419
+ width: 30,
420
+ length: 20
421
+ }
422
+ }),
423
+ fetchAllOrders: async () => [buildOrder()],
424
+ crearBulto: jest.fn(),
425
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
426
+ });
427
+
428
+ expect(insertQuantitySpy).toHaveBeenCalledWith(
429
+ expect.objectContaining({
430
+ group: {
431
+ grouped: 1,
432
+ weight: 9,
433
+ height: 40,
434
+ width: 30,
435
+ length: 20
436
+ }
437
+ })
438
+ );
439
+
440
+ expect(response.sequentialInsertion).toBe(false);
441
+ });
442
+
443
+ it('updates agency when rule engine returns a different agency and uses shipment details in that decision', async () => {
444
+ const getAgencyIdForTempShipment = jest.fn(async ({ shipmentDetails }) => {
445
+ expect(shipmentDetails).toEqual([
446
+ expect.objectContaining({ id_envio: 12345, cantidad: 2 })
447
+ ]);
448
+ return 99;
449
+ });
450
+
451
+ const { manager, integrationsService } = buildFixture({
452
+ getAgencyId: getAgencyIdForTempShipment
453
+ });
454
+
455
+ arrangeDefaultStubs({ manager, shipmentId: 12345 });
456
+
457
+ await manager.insertTempShipments({
458
+ payload: buildPayload({ agencyId: 50 }),
459
+ fetchAllOrders: async () => [buildOrder()],
460
+ crearBulto: jest.fn(),
461
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
462
+ });
463
+
464
+ expect(getAgencyIdForTempShipment).toHaveBeenCalledTimes(1);
465
+ expect(integrationsService.updateTempShimpment).toHaveBeenCalledWith({
466
+ id_envio: 12345,
467
+ id_usuario: 77,
468
+ id_agencia: 99
469
+ });
470
+ });
471
+
472
+ it('sends grouped package line to bulk insert when group=1 and order has more than one line', async () => {
473
+ const { manager, integrationsService } = buildFixture();
474
+ arrangeRealQuantityFlow({ manager, shipmentId: 12345 });
475
+
476
+ const crearBulto = jest.fn(async ({ lineItems }) => ({
477
+ bulto: {
478
+ peso: 1.2,
479
+ alto: 3,
480
+ ancho: 4,
481
+ largo: 5,
482
+ sku_producto: lineItems.line.sku_producto,
483
+ nombre_producto: lineItems.line.nombre_producto
484
+ },
485
+ cantidad: 2,
486
+ forzarAgrupamientoDeLinea: false
487
+ }));
488
+
489
+ await manager.insertTempShipments({
490
+ payload: buildPayload({
491
+ group: { grouped: 1, weight: 9, height: 40, width: 30, length: 20 }
492
+ }),
493
+ fetchAllOrders: async () => [
494
+ buildOrder({
495
+ lineas: [
496
+ { nombre_producto: 'Prod 1', sku_producto: 'SKU-01' },
497
+ { nombre_producto: 'Prod 2', sku_producto: 'SKU-02' }
498
+ ]
499
+ })
500
+ ],
501
+ crearBulto,
502
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
503
+ });
504
+
505
+ expect(
506
+ integrationsService.insertTempShipmentMultipleLines
507
+ ).toHaveBeenCalledWith([
508
+ {
509
+ id_envio: 12345,
510
+ bulto: {
511
+ peso: 9,
512
+ alto: 40,
513
+ ancho: 30,
514
+ largo: 20,
515
+ sku_producto: ''
516
+ }
517
+ }
518
+ ]);
519
+ expect(
520
+ integrationsService.insertTempShipmentMultipleDetails
521
+ ).toHaveBeenCalledWith([
522
+ expect.objectContaining({ id_envio: 12345, cantidad: 2 }),
523
+ expect.objectContaining({ id_envio: 12345, cantidad: 2 })
524
+ ]);
525
+ expect(
526
+ integrationsService.insertTempShipmentMultipleContents
527
+ ).toHaveBeenCalledWith([
528
+ expect.objectContaining({ id_envio: 12345, contenido: 'Auto-content' })
529
+ ]);
530
+ expect(
531
+ integrationsService.insertTempShipmentMultipleExternals
532
+ ).toHaveBeenCalledWith([
533
+ expect.objectContaining({ id_envio_temporal_usuario: 12345 })
534
+ ]);
535
+ expect(
536
+ integrationsService.insertTempShipmentMultipleJsonQuantityReferences
537
+ ).toHaveBeenCalledWith([]);
538
+ });
539
+
540
+ it('sends grouped package line when group=1, one line, and medidas config is enabled', async () => {
541
+ const { manager, integrationsService } = buildFixture({
542
+ measuresFromConfig: 1
543
+ });
544
+ arrangeRealQuantityFlow({ manager, shipmentId: 12345 });
545
+
546
+ const crearBulto = jest.fn(async ({ lineItems }) => ({
547
+ bulto: {
548
+ peso: 1,
549
+ alto: 2,
550
+ ancho: 3,
551
+ largo: 4,
552
+ sku_producto: lineItems.line.sku_producto,
553
+ nombre_producto: lineItems.line.nombre_producto
554
+ },
555
+ cantidad: 1,
556
+ forzarAgrupamientoDeLinea: false
557
+ }));
558
+
559
+ await manager.insertTempShipments({
560
+ payload: buildPayload({
561
+ group: { grouped: 1, weight: 7, height: 50, width: 60, length: 70 }
562
+ }),
563
+ fetchAllOrders: async () => [
564
+ buildOrder({
565
+ lineas: [{ nombre_producto: 'Only line', sku_producto: 'SKU-ONLY' }]
566
+ })
567
+ ],
568
+ crearBulto,
569
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
570
+ });
571
+
572
+ expect(
573
+ integrationsService.insertTempShipmentMultipleLines
574
+ ).toHaveBeenCalledWith([
575
+ {
576
+ id_envio: 12345,
577
+ bulto: {
578
+ peso: 7,
579
+ alto: 50,
580
+ ancho: 60,
581
+ largo: 70,
582
+ sku_producto: ''
583
+ }
584
+ }
585
+ ]);
586
+ expect(
587
+ integrationsService.insertTempShipmentMultipleDetails
588
+ ).toHaveBeenCalledWith([
589
+ expect.objectContaining({
590
+ id_envio: 12345,
591
+ bulto: expect.objectContaining({ sku_producto: 'SKU-ONLY' })
592
+ })
593
+ ]);
594
+ expect(
595
+ integrationsService.insertTempShipmentMultipleContents
596
+ ).toHaveBeenCalledWith([expect.objectContaining({ id_envio: 12345 })]);
597
+ expect(
598
+ integrationsService.insertTempShipmentMultipleExternals
599
+ ).toHaveBeenCalledWith([
600
+ expect.objectContaining({ id_envio_temporal_usuario: 12345 })
601
+ ]);
602
+ expect(
603
+ integrationsService.insertTempShipmentMultipleJsonQuantityReferences
604
+ ).toHaveBeenCalledWith([]);
605
+ });
606
+
607
+ it('sends non-grouped package line when group=1, one line and medidas config is disabled', async () => {
608
+ const { manager, integrationsService } = buildFixture({
609
+ measuresFromConfig: 0
610
+ });
611
+ arrangeRealQuantityFlow({ manager, shipmentId: 12345 });
612
+
613
+ const crearBulto = jest.fn(async ({ lineItems }) => ({
614
+ bulto: {
615
+ peso: 2,
616
+ alto: 8,
617
+ ancho: 9,
618
+ largo: 10,
619
+ sku_producto: lineItems.line.sku_producto,
620
+ nombre_producto: lineItems.line.nombre_producto
621
+ },
622
+ cantidad: 1,
623
+ forzarAgrupamientoDeLinea: false
624
+ }));
625
+
626
+ await manager.insertTempShipments({
627
+ payload: buildPayload({
628
+ group: { grouped: 1, weight: 99, height: 98, width: 97, length: 96 }
629
+ }),
630
+ fetchAllOrders: async () => [
631
+ buildOrder({
632
+ lineas: [{ nombre_producto: 'Single', sku_producto: 'SKU-SINGLE' }]
633
+ })
634
+ ],
635
+ crearBulto,
636
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
637
+ });
638
+
639
+ expect(
640
+ integrationsService.insertTempShipmentMultipleLines
641
+ ).toHaveBeenCalledWith([
642
+ {
643
+ id_envio: 12345,
644
+ bulto: {
645
+ peso: 2,
646
+ alto: 8,
647
+ ancho: 9,
648
+ largo: 10,
649
+ sku_producto: 'SKU-SINGLE',
650
+ nombre_producto: 'Single'
651
+ }
652
+ }
653
+ ]);
654
+ expect(
655
+ integrationsService.insertTempShipmentMultipleDetails
656
+ ).toHaveBeenCalledWith([
657
+ expect.objectContaining({ id_envio: 12345, cantidad: 1 })
658
+ ]);
659
+ expect(
660
+ integrationsService.insertTempShipmentMultipleContents
661
+ ).toHaveBeenCalledWith([expect.objectContaining({ id_envio: 12345 })]);
662
+ expect(
663
+ integrationsService.insertTempShipmentMultipleExternals
664
+ ).toHaveBeenCalledWith([
665
+ expect.objectContaining({ id_envio_temporal_usuario: 12345 })
666
+ ]);
667
+ expect(
668
+ integrationsService.insertTempShipmentMultipleJsonQuantityReferences
669
+ ).toHaveBeenCalledWith([]);
670
+ });
671
+
672
+ it('includes json quantity references in bulk insert when shouldFindWarehouseSkus is enabled', async () => {
673
+ const { manager, integrationsService } = buildFixture({
674
+ skuAlmacen: 1
675
+ });
676
+ arrangeRealQuantityFlow({ manager, shipmentId: 12345 });
677
+
678
+ manager.services.warehouseService.getProductReferenceFromSku = jest.fn(
679
+ async ({ sku }) => ({
680
+ id: sku === 'SKU-01' ? 111 : 222
681
+ })
682
+ );
683
+
684
+ const crearBulto = jest.fn(async ({ lineItems }) => ({
685
+ bulto: {
686
+ peso: 2,
687
+ alto: 8,
688
+ ancho: 9,
689
+ largo: 10,
690
+ sku_producto: lineItems.line.sku_producto,
691
+ nombre_producto: lineItems.line.nombre_producto
692
+ },
693
+ cantidad: 2,
694
+ forzarAgrupamientoDeLinea: false
695
+ }));
696
+
697
+ await manager.insertTempShipments({
698
+ payload: buildPayload({
699
+ warehouseId: 88,
700
+ group: { grouped: 0, weight: 2, height: 10, width: 20, length: 30 }
701
+ }),
702
+ fetchAllOrders: async () => [
703
+ buildOrder({
704
+ lineas: [{ nombre_producto: 'Line', sku_producto: 'SKU-01' }]
705
+ })
706
+ ],
707
+ crearBulto,
708
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
709
+ });
710
+
711
+ expect(
712
+ integrationsService.insertTempShipmentMultipleLines
713
+ ).toHaveBeenCalledWith([
714
+ expect.objectContaining({ id_envio: 12345 }),
715
+ expect.objectContaining({ id_envio: 12345 })
716
+ ]);
717
+ expect(
718
+ integrationsService.insertTempShipmentMultipleDetails
719
+ ).toHaveBeenCalledWith([
720
+ expect.objectContaining({ id_envio: 12345, cantidad: 2 })
721
+ ]);
722
+ expect(
723
+ integrationsService.insertTempShipmentMultipleContents
724
+ ).toHaveBeenCalledWith([expect.objectContaining({ id_envio: 12345 })]);
725
+ expect(
726
+ integrationsService.insertTempShipmentMultipleExternals
727
+ ).toHaveBeenCalledWith([
728
+ expect.objectContaining({ id_envio_temporal_usuario: 12345 })
729
+ ]);
730
+ expect(
731
+ integrationsService.insertTempShipmentMultipleJsonQuantityReferences
732
+ ).toHaveBeenCalledWith([
733
+ {
734
+ id_envio: 12345,
735
+ json_referencias_cantidades: [
736
+ { id_caja: -1, referencias: { 111: 1 } },
737
+ { id_caja: -1, referencias: { 111: 1 } }
738
+ ]
739
+ }
740
+ ]);
741
+ });
742
+
743
+ it('handles multiple orders and multiple lines in grouped mode', async () => {
744
+ const { manager, integrationsService } = buildFixture();
745
+ arrangeRealQuantityFlowMultipleOrders({
746
+ manager,
747
+ shipmentIds: [2001, 2002]
748
+ });
749
+
750
+ const crearBulto = jest.fn(async ({ lineItems }) => ({
751
+ bulto: {
752
+ peso: 1.5,
753
+ alto: 3,
754
+ ancho: 4,
755
+ largo: 5,
756
+ sku_producto: lineItems.line.sku_producto,
757
+ nombre_producto: lineItems.line.nombre_producto
758
+ },
759
+ cantidad: 1,
760
+ forzarAgrupamientoDeLinea: false
761
+ }));
762
+
763
+ await manager.insertTempShipments({
764
+ payload: buildPayload({
765
+ group: { grouped: 1, weight: 11, height: 21, width: 31, length: 41 }
766
+ }),
767
+ fetchAllOrders: async () => [
768
+ buildOrder({
769
+ codigoEnvioExterno: 'EXT-A',
770
+ lineas: [
771
+ { nombre_producto: 'A1', sku_producto: 'SKU-A1' },
772
+ { nombre_producto: 'A2', sku_producto: 'SKU-A2' }
773
+ ]
774
+ }),
775
+ buildOrder({
776
+ codigoEnvioExterno: 'EXT-B',
777
+ idInternoShopify: 'SHP-2',
778
+ lineas: [
779
+ { nombre_producto: 'B1', sku_producto: 'SKU-B1' },
780
+ { nombre_producto: 'B2', sku_producto: 'SKU-B2' }
781
+ ]
782
+ })
783
+ ],
784
+ crearBulto,
785
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
786
+ });
787
+
788
+ expect(
789
+ integrationsService.insertTempShipmentMultipleLines
790
+ ).toHaveBeenCalledWith([
791
+ {
792
+ id_envio: 2001,
793
+ bulto: { peso: 11, alto: 21, ancho: 31, largo: 41, sku_producto: '' }
794
+ },
795
+ {
796
+ id_envio: 2002,
797
+ bulto: { peso: 11, alto: 21, ancho: 31, largo: 41, sku_producto: '' }
798
+ }
799
+ ]);
800
+ expect(
801
+ integrationsService.insertTempShipmentMultipleDetails
802
+ ).toHaveBeenCalledWith([
803
+ expect.objectContaining({ id_envio: 2001, cantidad: 1 }),
804
+ expect.objectContaining({ id_envio: 2001, cantidad: 1 }),
805
+ expect.objectContaining({ id_envio: 2002, cantidad: 1 }),
806
+ expect.objectContaining({ id_envio: 2002, cantidad: 1 })
807
+ ]);
808
+ expect(
809
+ integrationsService.insertTempShipmentMultipleContents
810
+ ).toHaveBeenCalledWith([
811
+ expect.objectContaining({ id_envio: 2001 }),
812
+ expect.objectContaining({ id_envio: 2002 })
813
+ ]);
814
+ expect(
815
+ integrationsService.insertTempShipmentMultipleExternals
816
+ ).toHaveBeenCalledWith([
817
+ expect.objectContaining({ id_envio_temporal_usuario: 2001 }),
818
+ expect.objectContaining({ id_envio_temporal_usuario: 2002 })
819
+ ]);
820
+ expect(
821
+ integrationsService.insertTempShipmentMultipleJsonQuantityReferences
822
+ ).toHaveBeenCalledWith([]);
823
+ });
824
+
825
+ it('handles multiple orders and multiple lines in non-grouped mode', async () => {
826
+ const { manager, integrationsService } = buildFixture({
827
+ emulateLegacyPackageAggregation: true
828
+ });
829
+ arrangeRealQuantityFlowMultipleOrders({
830
+ manager,
831
+ shipmentIds: [2101, 2102]
832
+ });
833
+
834
+ const crearBulto = jest.fn(async ({ lineItems }) => ({
835
+ bulto: {
836
+ peso: 2,
837
+ alto: 7,
838
+ ancho: 8,
839
+ largo: 9,
840
+ sku_producto: lineItems.line.sku_producto,
841
+ nombre_producto: lineItems.line.nombre_producto
842
+ },
843
+ cantidad: 2,
844
+ forzarAgrupamientoDeLinea: false
845
+ }));
846
+
847
+ await manager.insertTempShipments({
848
+ payload: buildPayload({
849
+ group: { grouped: 0, weight: 0, height: 0, width: 0, length: 0 }
850
+ }),
851
+ fetchAllOrders: async () => [
852
+ buildOrder({
853
+ codigoEnvioExterno: 'EXT-C',
854
+ lineas: [
855
+ { nombre_producto: 'C1', sku_producto: 'SKU-C1' },
856
+ { nombre_producto: 'C2', sku_producto: 'SKU-C2' }
857
+ ]
858
+ }),
859
+ buildOrder({
860
+ codigoEnvioExterno: 'EXT-D',
861
+ lineas: [
862
+ { nombre_producto: 'D1', sku_producto: 'SKU-D1' },
863
+ { nombre_producto: 'D2', sku_producto: 'SKU-D2' }
864
+ ]
865
+ })
866
+ ],
867
+ crearBulto,
868
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
869
+ });
870
+
871
+ const linesArg =
872
+ integrationsService.insertTempShipmentMultipleLines.mock.calls[0][0];
873
+ expect(linesArg).toHaveLength(8);
874
+ expect(linesArg.filter((line: any) => line.id_envio === 2101)).toHaveLength(
875
+ 4
876
+ );
877
+ expect(linesArg.filter((line: any) => line.id_envio === 2102)).toHaveLength(
878
+ 4
879
+ );
880
+
881
+ expect(
882
+ integrationsService.insertTempShipmentMultipleDetails
883
+ ).toHaveBeenCalledWith([
884
+ expect.objectContaining({ id_envio: 2101, cantidad: 2 }),
885
+ expect.objectContaining({ id_envio: 2101, cantidad: 2 }),
886
+ expect.objectContaining({ id_envio: 2102, cantidad: 2 }),
887
+ expect.objectContaining({ id_envio: 2102, cantidad: 2 })
888
+ ]);
889
+ expect(
890
+ integrationsService.insertTempShipmentMultipleContents
891
+ ).toHaveBeenCalledWith([
892
+ expect.objectContaining({ id_envio: 2101 }),
893
+ expect.objectContaining({ id_envio: 2102 })
894
+ ]);
895
+ expect(
896
+ integrationsService.insertTempShipmentMultipleExternals
897
+ ).toHaveBeenCalledWith([
898
+ expect.objectContaining({ id_envio_temporal_usuario: 2101 }),
899
+ expect.objectContaining({ id_envio_temporal_usuario: 2102 })
900
+ ]);
901
+ expect(
902
+ integrationsService.insertTempShipmentMultipleJsonQuantityReferences
903
+ ).toHaveBeenCalledWith([]);
904
+ });
905
+
906
+ it('groups single-line orders when measures config is enabled', async () => {
907
+ const { manager, integrationsService } = buildFixture({
908
+ measuresFromConfig: 1
909
+ });
910
+ arrangeRealQuantityFlowMultipleOrders({
911
+ manager,
912
+ shipmentIds: [2201, 2202]
913
+ });
914
+
915
+ const crearBulto = jest.fn(async ({ lineItems }) => ({
916
+ bulto: {
917
+ peso: 1,
918
+ alto: 2,
919
+ ancho: 3,
920
+ largo: 4,
921
+ sku_producto: lineItems.line.sku_producto,
922
+ nombre_producto: lineItems.line.nombre_producto
923
+ },
924
+ cantidad: 1,
925
+ forzarAgrupamientoDeLinea: false
926
+ }));
927
+
928
+ await manager.insertTempShipments({
929
+ payload: buildPayload({
930
+ group: { grouped: 1, weight: 14, height: 24, width: 34, length: 44 }
931
+ }),
932
+ fetchAllOrders: async () => [
933
+ buildOrder({
934
+ codigoEnvioExterno: 'EXT-E',
935
+ lineas: [{ nombre_producto: 'E1', sku_producto: 'SKU-E1' }]
936
+ }),
937
+ buildOrder({
938
+ codigoEnvioExterno: 'EXT-F',
939
+ lineas: [{ nombre_producto: 'F1', sku_producto: 'SKU-F1' }]
940
+ })
941
+ ],
942
+ crearBulto,
943
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
944
+ });
945
+
946
+ expect(
947
+ integrationsService.insertTempShipmentMultipleLines
948
+ ).toHaveBeenCalledWith([
949
+ {
950
+ id_envio: 2201,
951
+ bulto: { peso: 14, alto: 24, ancho: 34, largo: 44, sku_producto: '' }
952
+ },
953
+ {
954
+ id_envio: 2202,
955
+ bulto: { peso: 14, alto: 24, ancho: 34, largo: 44, sku_producto: '' }
956
+ }
957
+ ]);
958
+ expect(
959
+ integrationsService.insertTempShipmentMultipleDetails
960
+ ).toHaveBeenCalledWith([
961
+ expect.objectContaining({ id_envio: 2201, cantidad: 1 }),
962
+ expect.objectContaining({ id_envio: 2202, cantidad: 1 })
963
+ ]);
964
+ expect(
965
+ integrationsService.insertTempShipmentMultipleContents
966
+ ).toHaveBeenCalledWith([
967
+ expect.objectContaining({ id_envio: 2201 }),
968
+ expect.objectContaining({ id_envio: 2202 })
969
+ ]);
970
+ expect(
971
+ integrationsService.insertTempShipmentMultipleExternals
972
+ ).toHaveBeenCalledWith([
973
+ expect.objectContaining({ id_envio_temporal_usuario: 2201 }),
974
+ expect.objectContaining({ id_envio_temporal_usuario: 2202 })
975
+ ]);
976
+ expect(
977
+ integrationsService.insertTempShipmentMultipleJsonQuantityReferences
978
+ ).toHaveBeenCalledWith([]);
979
+ });
980
+
981
+ it('does not group single-line orders when measures config is disabled', async () => {
982
+ const { manager, integrationsService } = buildFixture({
983
+ measuresFromConfig: 0
984
+ });
985
+ arrangeRealQuantityFlowMultipleOrders({
986
+ manager,
987
+ shipmentIds: [2301, 2302]
988
+ });
989
+
990
+ const crearBulto = jest.fn(async ({ lineItems }) => ({
991
+ bulto: {
992
+ peso: 3,
993
+ alto: 6,
994
+ ancho: 9,
995
+ largo: 12,
996
+ sku_producto: lineItems.line.sku_producto,
997
+ nombre_producto: lineItems.line.nombre_producto
998
+ },
999
+ cantidad: 1,
1000
+ forzarAgrupamientoDeLinea: false
1001
+ }));
1002
+
1003
+ await manager.insertTempShipments({
1004
+ payload: buildPayload({
1005
+ group: { grouped: 1, weight: 50, height: 60, width: 70, length: 80 }
1006
+ }),
1007
+ fetchAllOrders: async () => [
1008
+ buildOrder({
1009
+ codigoEnvioExterno: 'EXT-G',
1010
+ lineas: [{ nombre_producto: 'G1', sku_producto: 'SKU-G1' }]
1011
+ }),
1012
+ buildOrder({
1013
+ codigoEnvioExterno: 'EXT-H',
1014
+ lineas: [{ nombre_producto: 'H1', sku_producto: 'SKU-H1' }]
1015
+ })
1016
+ ],
1017
+ crearBulto,
1018
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
1019
+ });
1020
+
1021
+ expect(
1022
+ integrationsService.insertTempShipmentMultipleLines
1023
+ ).toHaveBeenCalledWith([
1024
+ {
1025
+ id_envio: 2301,
1026
+ bulto: {
1027
+ peso: 3,
1028
+ alto: 6,
1029
+ ancho: 9,
1030
+ largo: 12,
1031
+ sku_producto: 'SKU-G1',
1032
+ nombre_producto: 'G1'
1033
+ }
1034
+ },
1035
+ {
1036
+ id_envio: 2302,
1037
+ bulto: {
1038
+ peso: 3,
1039
+ alto: 6,
1040
+ ancho: 9,
1041
+ largo: 12,
1042
+ sku_producto: 'SKU-H1',
1043
+ nombre_producto: 'H1'
1044
+ }
1045
+ }
1046
+ ]);
1047
+ expect(
1048
+ integrationsService.insertTempShipmentMultipleDetails
1049
+ ).toHaveBeenCalledWith([
1050
+ expect.objectContaining({ id_envio: 2301, cantidad: 1 }),
1051
+ expect.objectContaining({ id_envio: 2302, cantidad: 1 })
1052
+ ]);
1053
+ expect(
1054
+ integrationsService.insertTempShipmentMultipleContents
1055
+ ).toHaveBeenCalledWith([
1056
+ expect.objectContaining({ id_envio: 2301 }),
1057
+ expect.objectContaining({ id_envio: 2302 })
1058
+ ]);
1059
+ expect(
1060
+ integrationsService.insertTempShipmentMultipleExternals
1061
+ ).toHaveBeenCalledWith([
1062
+ expect.objectContaining({ id_envio_temporal_usuario: 2301 }),
1063
+ expect.objectContaining({ id_envio_temporal_usuario: 2302 })
1064
+ ]);
1065
+ expect(
1066
+ integrationsService.insertTempShipmentMultipleJsonQuantityReferences
1067
+ ).toHaveBeenCalledWith([]);
1068
+ });
1069
+
1070
+ it('builds json quantity references for multiple shipments when sku lookup is enabled', async () => {
1071
+ const { manager, integrationsService } = buildFixture({
1072
+ skuAlmacen: 1
1073
+ });
1074
+ arrangeRealQuantityFlowMultipleOrders({
1075
+ manager,
1076
+ shipmentIds: [2401, 2402]
1077
+ });
1078
+
1079
+ manager.services.warehouseService.getProductReferenceFromSku = jest.fn(
1080
+ async ({ sku }) => {
1081
+ if (sku === 'SKU-I1') return { id: 901 };
1082
+ if (sku === 'SKU-J1') return { id: 902 };
1083
+ return { id: 999 };
1084
+ }
1085
+ );
1086
+
1087
+ const crearBulto = jest.fn(async ({ lineItems }) => ({
1088
+ bulto: {
1089
+ peso: 2,
1090
+ alto: 8,
1091
+ ancho: 9,
1092
+ largo: 10,
1093
+ sku_producto: lineItems.line.sku_producto,
1094
+ nombre_producto: lineItems.line.nombre_producto
1095
+ },
1096
+ cantidad: 2,
1097
+ forzarAgrupamientoDeLinea: false
1098
+ }));
1099
+
1100
+ await manager.insertTempShipments({
1101
+ payload: buildPayload({
1102
+ warehouseId: 55,
1103
+ group: { grouped: 0, weight: 1, height: 1, width: 1, length: 1 }
1104
+ }),
1105
+ fetchAllOrders: async () => [
1106
+ buildOrder({
1107
+ codigoEnvioExterno: 'EXT-I',
1108
+ lineas: [
1109
+ { nombre_producto: 'I1', sku_producto: 'SKU-I1' },
1110
+ { nombre_producto: 'I2', sku_producto: 'SKU-I1' }
1111
+ ]
1112
+ }),
1113
+ buildOrder({
1114
+ codigoEnvioExterno: 'EXT-J',
1115
+ lineas: [
1116
+ { nombre_producto: 'J1', sku_producto: 'SKU-J1' },
1117
+ { nombre_producto: 'J2', sku_producto: 'SKU-J1' }
1118
+ ]
1119
+ })
1120
+ ],
1121
+ crearBulto,
1122
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
1123
+ });
1124
+
1125
+ expect(
1126
+ integrationsService.insertTempShipmentMultipleJsonQuantityReferences
1127
+ ).toHaveBeenCalledWith([
1128
+ {
1129
+ id_envio: 2401,
1130
+ json_referencias_cantidades: [
1131
+ { id_caja: -1, referencias: { 901: 1 } },
1132
+ { id_caja: -1, referencias: { 901: 1 } },
1133
+ { id_caja: -1, referencias: { 901: 1 } },
1134
+ { id_caja: -1, referencias: { 901: 1 } }
1135
+ ]
1136
+ },
1137
+ {
1138
+ id_envio: 2402,
1139
+ json_referencias_cantidades: [
1140
+ { id_caja: -1, referencias: { 902: 1 } },
1141
+ { id_caja: -1, referencias: { 902: 1 } },
1142
+ { id_caja: -1, referencias: { 902: 1 } },
1143
+ { id_caja: -1, referencias: { 902: 1 } }
1144
+ ]
1145
+ }
1146
+ ]);
1147
+ expect(
1148
+ integrationsService.insertTempShipmentMultipleLines
1149
+ ).toHaveBeenCalledWith(
1150
+ expect.arrayContaining([
1151
+ expect.objectContaining({ id_envio: 2401 }),
1152
+ expect.objectContaining({ id_envio: 2402 })
1153
+ ])
1154
+ );
1155
+ expect(
1156
+ integrationsService.insertTempShipmentMultipleDetails
1157
+ ).toHaveBeenCalledWith(
1158
+ expect.arrayContaining([
1159
+ expect.objectContaining({ id_envio: 2401 }),
1160
+ expect.objectContaining({ id_envio: 2402 })
1161
+ ])
1162
+ );
1163
+ expect(
1164
+ integrationsService.insertTempShipmentMultipleContents
1165
+ ).toHaveBeenCalledWith([
1166
+ expect.objectContaining({ id_envio: 2401 }),
1167
+ expect.objectContaining({ id_envio: 2402 })
1168
+ ]);
1169
+ expect(
1170
+ integrationsService.insertTempShipmentMultipleExternals
1171
+ ).toHaveBeenCalledWith([
1172
+ expect.objectContaining({ id_envio_temporal_usuario: 2401 }),
1173
+ expect.objectContaining({ id_envio_temporal_usuario: 2402 })
1174
+ ]);
1175
+ });
1176
+
1177
+ it('mixes grouped and non-grouped orders in the same run', async () => {
1178
+ const { manager, integrationsService } = buildFixture({
1179
+ measuresFromConfig: 0
1180
+ });
1181
+ arrangeRealQuantityFlowMultipleOrders({
1182
+ manager,
1183
+ shipmentIds: [2501, 2502]
1184
+ });
1185
+
1186
+ const crearBulto = jest.fn(async ({ lineItems }) => {
1187
+ const groupedOrderLine = lineItems.line.sku_producto.startsWith('SKU-K');
1188
+ return {
1189
+ bulto: {
1190
+ peso: groupedOrderLine ? 5 : 2,
1191
+ alto: groupedOrderLine ? 15 : 6,
1192
+ ancho: groupedOrderLine ? 16 : 7,
1193
+ largo: groupedOrderLine ? 17 : 8,
1194
+ sku_producto: lineItems.line.sku_producto,
1195
+ nombre_producto: lineItems.line.nombre_producto
1196
+ },
1197
+ cantidad: 1,
1198
+ forzarAgrupamientoDeLinea: false
1199
+ };
1200
+ });
1201
+
1202
+ await manager.insertTempShipments({
1203
+ payload: buildPayload({
1204
+ group: { grouped: 1, weight: 44, height: 45, width: 46, length: 47 }
1205
+ }),
1206
+ fetchAllOrders: async () => [
1207
+ buildOrder({
1208
+ codigoEnvioExterno: 'EXT-K',
1209
+ lineas: [
1210
+ { nombre_producto: 'K1', sku_producto: 'SKU-K1' },
1211
+ { nombre_producto: 'K2', sku_producto: 'SKU-K2' }
1212
+ ]
1213
+ }),
1214
+ buildOrder({
1215
+ codigoEnvioExterno: 'EXT-L',
1216
+ lineas: [{ nombre_producto: 'L1', sku_producto: 'SKU-L1' }]
1217
+ })
1218
+ ],
1219
+ crearBulto,
1220
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
1221
+ });
1222
+
1223
+ expect(
1224
+ integrationsService.insertTempShipmentMultipleLines
1225
+ ).toHaveBeenCalledWith([
1226
+ {
1227
+ id_envio: 2501,
1228
+ bulto: { peso: 44, alto: 45, ancho: 46, largo: 47, sku_producto: '' }
1229
+ },
1230
+ {
1231
+ id_envio: 2502,
1232
+ bulto: {
1233
+ peso: 2,
1234
+ alto: 6,
1235
+ ancho: 7,
1236
+ largo: 8,
1237
+ sku_producto: 'SKU-L1',
1238
+ nombre_producto: 'L1'
1239
+ }
1240
+ }
1241
+ ]);
1242
+ expect(
1243
+ integrationsService.insertTempShipmentMultipleDetails
1244
+ ).toHaveBeenCalledWith([
1245
+ expect.objectContaining({ id_envio: 2501 }),
1246
+ expect.objectContaining({ id_envio: 2501 }),
1247
+ expect.objectContaining({ id_envio: 2502 })
1248
+ ]);
1249
+ expect(
1250
+ integrationsService.insertTempShipmentMultipleContents
1251
+ ).toHaveBeenCalledWith([
1252
+ expect.objectContaining({ id_envio: 2501 }),
1253
+ expect.objectContaining({ id_envio: 2502 })
1254
+ ]);
1255
+ expect(
1256
+ integrationsService.insertTempShipmentMultipleExternals
1257
+ ).toHaveBeenCalledWith([
1258
+ expect.objectContaining({ id_envio_temporal_usuario: 2501 }),
1259
+ expect.objectContaining({ id_envio_temporal_usuario: 2502 })
1260
+ ]);
1261
+ expect(
1262
+ integrationsService.insertTempShipmentMultipleJsonQuantityReferences
1263
+ ).toHaveBeenCalledWith([]);
1264
+ });
1265
+
1266
+ it('excludes aborted shipments from all 5 bulk insertion payloads', async () => {
1267
+ const { manager, integrationsService } = buildFixture({
1268
+ skuAlmacen: 1
1269
+ });
1270
+
1271
+ manager.services.integrationsService.insertTempShipment
1272
+ .mockResolvedValueOnce(2601)
1273
+ .mockResolvedValueOnce(2602);
1274
+
1275
+ manager.services.warehouseService.getProductReferenceFromSku = jest.fn(
1276
+ async ({ sku }) => ({ id: sku === 'SKU-M1' ? 7001 : 7002 })
1277
+ );
1278
+
1279
+ manager.getModifiedOrderBasedOnRules = jest.fn(async ({ parsedOrder }) => {
1280
+ if (parsedOrder.codigoEnvioExterno === 'EXT-ABORT') {
1281
+ return {
1282
+ partialTempShipmentModified: { abort_creation: true },
1283
+ packagesModified: []
1284
+ };
1285
+ }
1286
+
1287
+ return {
1288
+ partialTempShipmentModified: {},
1289
+ packagesModified: []
1290
+ };
1291
+ });
1292
+
1293
+ const crearBulto = jest.fn(async ({ lineItems }) => ({
1294
+ bulto: {
1295
+ peso: 2,
1296
+ alto: 4,
1297
+ ancho: 6,
1298
+ largo: 8,
1299
+ sku_producto: lineItems.line.sku_producto,
1300
+ nombre_producto: lineItems.line.nombre_producto
1301
+ },
1302
+ cantidad: 2,
1303
+ forzarAgrupamientoDeLinea: false
1304
+ }));
1305
+
1306
+ await manager.insertTempShipments({
1307
+ payload: buildPayload({
1308
+ warehouseId: 77,
1309
+ group: { grouped: 0, weight: 1, height: 1, width: 1, length: 1 }
1310
+ }),
1311
+ fetchAllOrders: async () => [
1312
+ buildOrder({
1313
+ codigoEnvioExterno: 'EXT-KEEP',
1314
+ lineas: [{ nombre_producto: 'Keep line', sku_producto: 'SKU-M1' }]
1315
+ }),
1316
+ buildOrder({
1317
+ codigoEnvioExterno: 'EXT-ABORT',
1318
+ lineas: [{ nombre_producto: 'Abort line', sku_producto: 'SKU-N1' }]
1319
+ })
1320
+ ],
1321
+ crearBulto,
1322
+ modificarOrdenOriginal: async ({ originalOrder }) => originalOrder
1323
+ });
1324
+
1325
+ const linesArg =
1326
+ integrationsService.insertTempShipmentMultipleLines.mock.calls[0][0];
1327
+ const detailsArg =
1328
+ integrationsService.insertTempShipmentMultipleDetails.mock.calls[0][0];
1329
+ const contentsArg =
1330
+ integrationsService.insertTempShipmentMultipleContents.mock.calls[0][0];
1331
+ const externalsArg =
1332
+ integrationsService.insertTempShipmentMultipleExternals.mock.calls[0][0];
1333
+ const jsonRefsArg =
1334
+ integrationsService.insertTempShipmentMultipleJsonQuantityReferences.mock
1335
+ .calls[0][0];
1336
+
1337
+ expect(linesArg.every((line: any) => line.id_envio === 2601)).toBe(true);
1338
+ expect(detailsArg.every((detail: any) => detail.id_envio === 2601)).toBe(
1339
+ true
1340
+ );
1341
+ expect(contentsArg.every((content: any) => content.id_envio === 2601)).toBe(
1342
+ true
1343
+ );
1344
+ expect(
1345
+ externalsArg.every(
1346
+ (external: any) => external.id_envio_temporal_usuario === 2601
1347
+ )
1348
+ ).toBe(true);
1349
+ expect(jsonRefsArg.every((ref: any) => ref.id_envio === 2601)).toBe(true);
1350
+
1351
+ expect(linesArg.some((line: any) => line.id_envio === 2602)).toBe(false);
1352
+ expect(detailsArg.some((detail: any) => detail.id_envio === 2602)).toBe(
1353
+ false
1354
+ );
1355
+ expect(contentsArg.some((content: any) => content.id_envio === 2602)).toBe(
1356
+ false
1357
+ );
1358
+ expect(
1359
+ externalsArg.some(
1360
+ (external: any) => external.id_envio_temporal_usuario === 2602
1361
+ )
1362
+ ).toBe(false);
1363
+ expect(jsonRefsArg.some((ref: any) => ref.id_envio === 2602)).toBe(false);
1364
+
1365
+ expect(integrationsService.deleteTempShipment).toHaveBeenCalledWith({
1366
+ id_envio: 2602,
1367
+ id_usuario: 77
1368
+ });
1369
+ });
1370
+ });