@solution25/n8n-nodes-shopware 1.4.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/README.md +4 -1
  2. package/dist/credentials/ShopwareOAuth2Api.credentials.js +4 -3
  3. package/dist/credentials/ShopwareOAuth2Api.credentials.js.map +1 -1
  4. package/dist/nodes/Shopware/Shopware.node.js.map +1 -1
  5. package/dist/nodes/Shopware/test/v1/helpers/payloadBuilders.test.js +213 -0
  6. package/dist/nodes/Shopware/test/v1/helpers/payloadBuilders.test.js.map +1 -0
  7. package/dist/nodes/Shopware/test/v1/helpers/pricing.test.js +75 -0
  8. package/dist/nodes/Shopware/test/v1/helpers/pricing.test.js.map +1 -0
  9. package/dist/nodes/Shopware/test/v1/helpers/utils.test.js +153 -0
  10. package/dist/nodes/Shopware/test/v1/helpers/utils.test.js.map +1 -0
  11. package/dist/nodes/Shopware/test/v1/helpers/validation.test.d.ts +1 -0
  12. package/dist/nodes/Shopware/test/v1/helpers/validation.test.js +16 -0
  13. package/dist/nodes/Shopware/test/v1/helpers/validation.test.js.map +1 -0
  14. package/dist/nodes/Shopware/v1/actions/category/create.operation.js +17 -39
  15. package/dist/nodes/Shopware/v1/actions/category/create.operation.js.map +1 -1
  16. package/dist/nodes/Shopware/v1/actions/category/deleteCategory.operation.js +2 -1
  17. package/dist/nodes/Shopware/v1/actions/category/deleteCategory.operation.js.map +1 -1
  18. package/dist/nodes/Shopware/v1/actions/category/fields.js +3 -1
  19. package/dist/nodes/Shopware/v1/actions/category/fields.js.map +1 -1
  20. package/dist/nodes/Shopware/v1/actions/category/get.operation.js +4 -0
  21. package/dist/nodes/Shopware/v1/actions/category/get.operation.js.map +1 -1
  22. package/dist/nodes/Shopware/v1/actions/category/getMany.operation.js +1 -1
  23. package/dist/nodes/Shopware/v1/actions/category/getMany.operation.js.map +1 -1
  24. package/dist/nodes/Shopware/v1/actions/category/types.d.ts +3 -2
  25. package/dist/nodes/Shopware/v1/actions/category/update.operation.js +15 -19
  26. package/dist/nodes/Shopware/v1/actions/category/update.operation.js.map +1 -1
  27. package/dist/nodes/Shopware/v1/actions/customer/create.operation.js +49 -64
  28. package/dist/nodes/Shopware/v1/actions/customer/create.operation.js.map +1 -1
  29. package/dist/nodes/Shopware/v1/actions/customer/deleteCustomer.operation.js +2 -1
  30. package/dist/nodes/Shopware/v1/actions/customer/deleteCustomer.operation.js.map +1 -1
  31. package/dist/nodes/Shopware/v1/actions/customer/fields.d.ts +0 -3
  32. package/dist/nodes/Shopware/v1/actions/customer/fields.js +4 -4
  33. package/dist/nodes/Shopware/v1/actions/customer/fields.js.map +1 -1
  34. package/dist/nodes/Shopware/v1/actions/customer/get.operation.js +5 -0
  35. package/dist/nodes/Shopware/v1/actions/customer/get.operation.js.map +1 -1
  36. package/dist/nodes/Shopware/v1/actions/customer/getMany.operation.js +1 -1
  37. package/dist/nodes/Shopware/v1/actions/customer/getMany.operation.js.map +1 -1
  38. package/dist/nodes/Shopware/v1/actions/customer/types.d.ts +7 -14
  39. package/dist/nodes/Shopware/v1/actions/customer/update.operation.js +36 -58
  40. package/dist/nodes/Shopware/v1/actions/customer/update.operation.js.map +1 -1
  41. package/dist/nodes/Shopware/v1/actions/fields.js +4 -1
  42. package/dist/nodes/Shopware/v1/actions/fields.js.map +1 -1
  43. package/dist/nodes/Shopware/v1/actions/order/create.operation.js +192 -276
  44. package/dist/nodes/Shopware/v1/actions/order/create.operation.js.map +1 -1
  45. package/dist/nodes/Shopware/v1/actions/order/deleteOrder.operation.js +2 -1
  46. package/dist/nodes/Shopware/v1/actions/order/deleteOrder.operation.js.map +1 -1
  47. package/dist/nodes/Shopware/v1/actions/order/getMany.operation.js +0 -1
  48. package/dist/nodes/Shopware/v1/actions/order/getMany.operation.js.map +1 -1
  49. package/dist/nodes/Shopware/v1/actions/order/types.d.ts +30 -7
  50. package/dist/nodes/Shopware/v1/actions/order/update.operation.js +216 -245
  51. package/dist/nodes/Shopware/v1/actions/order/update.operation.js.map +1 -1
  52. package/dist/nodes/Shopware/v1/actions/product/create.operation.js +68 -60
  53. package/dist/nodes/Shopware/v1/actions/product/create.operation.js.map +1 -1
  54. package/dist/nodes/Shopware/v1/actions/product/deleteProduct.operation.js +2 -1
  55. package/dist/nodes/Shopware/v1/actions/product/deleteProduct.operation.js.map +1 -1
  56. package/dist/nodes/Shopware/v1/actions/product/fields.d.ts +0 -3
  57. package/dist/nodes/Shopware/v1/actions/product/fields.js +6 -4
  58. package/dist/nodes/Shopware/v1/actions/product/fields.js.map +1 -1
  59. package/dist/nodes/Shopware/v1/actions/product/get.operation.js +6 -0
  60. package/dist/nodes/Shopware/v1/actions/product/get.operation.js.map +1 -1
  61. package/dist/nodes/Shopware/v1/actions/product/getMany.operation.js +3 -3
  62. package/dist/nodes/Shopware/v1/actions/product/getMany.operation.js.map +1 -1
  63. package/dist/nodes/Shopware/v1/actions/product/types.d.ts +12 -14
  64. package/dist/nodes/Shopware/v1/actions/product/update.operation.js +70 -51
  65. package/dist/nodes/Shopware/v1/actions/product/update.operation.js.map +1 -1
  66. package/dist/nodes/Shopware/v1/actions/router.js +2 -1
  67. package/dist/nodes/Shopware/v1/actions/router.js.map +1 -1
  68. package/dist/nodes/Shopware/v1/actions/types.d.ts +14 -0
  69. package/dist/nodes/Shopware/v1/actions/versionDescription.js.map +1 -1
  70. package/dist/nodes/Shopware/v1/helpers/handlers.js +4 -4
  71. package/dist/nodes/Shopware/v1/helpers/handlers.js.map +1 -1
  72. package/dist/nodes/Shopware/v1/helpers/media.d.ts +12 -0
  73. package/dist/nodes/Shopware/v1/helpers/media.js +35 -0
  74. package/dist/nodes/Shopware/v1/helpers/media.js.map +1 -0
  75. package/dist/nodes/Shopware/v1/helpers/params.d.ts +90 -0
  76. package/dist/nodes/Shopware/v1/helpers/params.js +154 -0
  77. package/dist/nodes/Shopware/v1/helpers/params.js.map +1 -0
  78. package/dist/nodes/Shopware/v1/helpers/payloadBuilders.d.ts +143 -0
  79. package/dist/nodes/Shopware/v1/helpers/payloadBuilders.js +441 -0
  80. package/dist/nodes/Shopware/v1/helpers/payloadBuilders.js.map +1 -0
  81. package/dist/nodes/Shopware/v1/helpers/pricing.d.ts +29 -0
  82. package/dist/nodes/Shopware/v1/helpers/pricing.js +108 -0
  83. package/dist/nodes/Shopware/v1/helpers/pricing.js.map +1 -0
  84. package/dist/nodes/Shopware/v1/helpers/utils.d.ts +4 -1
  85. package/dist/nodes/Shopware/v1/helpers/utils.js +260 -21
  86. package/dist/nodes/Shopware/v1/helpers/utils.js.map +1 -1
  87. package/dist/nodes/Shopware/v1/helpers/validation.d.ts +3 -0
  88. package/dist/nodes/Shopware/v1/helpers/validation.js +29 -0
  89. package/dist/nodes/Shopware/v1/helpers/validation.js.map +1 -0
  90. package/dist/nodes/Shopware/v1/methods/loadOptions.js +14 -21
  91. package/dist/nodes/Shopware/v1/methods/loadOptions.js.map +1 -1
  92. package/dist/nodes/Shopware/v1/transport/index.d.ts +1 -1
  93. package/dist/nodes/Shopware/v1/transport/index.js +28 -4
  94. package/dist/nodes/Shopware/v1/transport/index.js.map +1 -1
  95. package/dist/package.json +2 -2
  96. package/dist/tsconfig.tsbuildinfo +1 -1
  97. package/package.json +2 -2
  98. package/dist/nodes/Shopware/test/v1/node/customer/getMany.test.js +0 -98
  99. package/dist/nodes/Shopware/test/v1/node/customer/getMany.test.js.map +0 -1
  100. package/dist/nodes/Shopware/test/v1/node/helpers.d.ts +0 -6
  101. package/dist/nodes/Shopware/test/v1/node/helpers.js +0 -44
  102. package/dist/nodes/Shopware/test/v1/node/helpers.js.map +0 -1
  103. package/dist/nodes/Shopware/test/v1/node/order/getMany.test.js +0 -111
  104. package/dist/nodes/Shopware/test/v1/node/order/getMany.test.js.map +0 -1
  105. package/dist/nodes/Shopware/test/v1/node/product/getMany.test.js +0 -96
  106. package/dist/nodes/Shopware/test/v1/node/product/getMany.test.js.map +0 -1
  107. /package/dist/nodes/Shopware/test/v1/{node/customer/getMany.test.d.ts → helpers/payloadBuilders.test.d.ts} +0 -0
  108. /package/dist/nodes/Shopware/test/v1/{node/order/getMany.test.d.ts → helpers/pricing.test.d.ts} +0 -0
  109. /package/dist/nodes/Shopware/test/v1/{node/product/getMany.test.d.ts → helpers/utils.test.d.ts} +0 -0
@@ -6,6 +6,10 @@ const n8n_workflow_1 = require("n8n-workflow");
6
6
  const transport_1 = require("../../transport");
7
7
  const fields_1 = require("./fields");
8
8
  const utils_1 = require("../../helpers/utils");
9
+ const params_1 = require("../../helpers/params");
10
+ const payloadBuilders_1 = require("../../helpers/payloadBuilders");
11
+ const pricing_1 = require("../../helpers/pricing");
12
+ const validation_1 = require("../../helpers/validation");
9
13
  const properties = [
10
14
  {
11
15
  displayName: 'Order ID',
@@ -65,6 +69,20 @@ const properties = [
65
69
  default: '',
66
70
  description: 'Customer last name',
67
71
  },
72
+ {
73
+ displayName: 'Zip Code',
74
+ name: 'zipcode',
75
+ type: 'string',
76
+ default: '',
77
+ description: 'Billing address postal code',
78
+ },
79
+ {
80
+ displayName: 'State / Province',
81
+ name: 'state',
82
+ type: 'string',
83
+ default: '',
84
+ description: 'Optional state or province name/code for the billing address',
85
+ },
68
86
  {
69
87
  displayName: 'City',
70
88
  name: 'city',
@@ -111,17 +129,6 @@ const properties = [
111
129
  default: '',
112
130
  description: 'Delivery shipping method',
113
131
  },
114
- {
115
- displayName: 'State',
116
- name: 'state',
117
- type: 'options',
118
- typeOptions: {
119
- loadOptionsMethod: 'getDeliveryStates',
120
- },
121
- required: true,
122
- default: '',
123
- description: 'State of the delivery (e.g. Shipped)',
124
- },
125
132
  {
126
133
  displayName: 'Use Customer Shipping Address',
127
134
  name: 'customerShippingAddress',
@@ -176,6 +183,20 @@ const properties = [
176
183
  default: '',
177
184
  description: 'Customer last name',
178
185
  },
186
+ {
187
+ displayName: 'Zip Code',
188
+ name: 'zipcode',
189
+ type: 'string',
190
+ default: '',
191
+ description: 'Shipping address postal code',
192
+ },
193
+ {
194
+ displayName: 'State / Province',
195
+ name: 'state',
196
+ type: 'string',
197
+ default: '',
198
+ description: 'Optional state or province name/code for the shipping address',
199
+ },
179
200
  {
180
201
  displayName: 'City',
181
202
  name: 'city',
@@ -230,7 +251,7 @@ const properties = [
230
251
  type: 'number',
231
252
  typeOptions: {
232
253
  maxValue: 999000000,
233
- minValue: 0,
254
+ minValue: 1,
234
255
  },
235
256
  required: true,
236
257
  default: 1,
@@ -283,6 +304,20 @@ const properties = [
283
304
  default: '',
284
305
  description: 'Customer last name',
285
306
  },
307
+ {
308
+ displayName: 'Zip Code',
309
+ name: 'zipcode',
310
+ type: 'string',
311
+ default: '',
312
+ description: 'Shipping address postal code',
313
+ },
314
+ {
315
+ displayName: 'State / Province',
316
+ name: 'state',
317
+ type: 'string',
318
+ default: '',
319
+ description: 'Optional state or province name/code for the shipping address',
320
+ },
286
321
  {
287
322
  displayName: 'City',
288
323
  name: 'city',
@@ -384,174 +419,164 @@ const displayOptions = {
384
419
  };
385
420
  exports.description = (0, n8n_workflow_1.updateDisplayOptions)(displayOptions, properties);
386
421
  async function execute(items) {
387
- var _a, _b, _c, _d, _e;
422
+ var _a, _b, _c, _d;
388
423
  const returnData = [];
424
+ let prePaymentOrderStatesPromise;
425
+ const shippingMethodDataCache = new Map();
426
+ const countryStateCache = new Map();
389
427
  for (let i = 0; i < items.length; i++) {
390
428
  try {
391
- const id = this.getNodeParameter('id', i);
429
+ const params = params_1.extractOrderUpdateParams.call(this, i);
430
+ const orderId = (0, validation_1.validateShopwareId)(this.getNode(), params.id, i, 'Order ID');
392
431
  const searchPrevOrderBody = {
393
- filter: [{ type: 'equals', field: 'id', value: id }],
432
+ filter: [{ type: 'equals', field: 'id', value: orderId }],
394
433
  associations: {
395
434
  currency: {},
435
+ lineItems: {},
436
+ deliveries: {
437
+ associations: {
438
+ shippingOrderAddress: {},
439
+ positions: {},
440
+ },
441
+ },
396
442
  },
397
443
  };
398
444
  const prevOrder = (await transport_1.apiRequest.call(this, 'POST', `/search/order`, searchPrevOrderBody)).data[0];
399
445
  if (!prevOrder) {
400
446
  throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Order does not exist', {
401
- description: 'There is no order with id ' + id,
447
+ description: 'There is no order with id ' + orderId,
402
448
  itemIndex: i,
403
449
  });
404
450
  }
405
- const { state: orderState, ...updateFields } = this.getNodeParameter('updateFields', i);
406
- const { currency: currencyData, shippingCosts: shippingData, orderCustomer: prevOrderCustomer, ...previousOrderData } = prevOrder;
451
+ const { currency: currencyData, orderCustomer: prevOrderCustomer, ...previousOrderData } = prevOrder;
407
452
  const customerData = {};
408
- const customerBillingAddress = (_a = updateFields.billingAddressUi) === null || _a === void 0 ? void 0 : _a.billingAddressValues;
409
- const customerShippingAddress = (_b = updateFields.shippingAddressUi) === null || _b === void 0 ? void 0 : _b.shippingAddressValues;
410
- if (customerBillingAddress) {
411
- const prePaymentStates = await utils_1.getPrePaymentOrderStates.call(this);
453
+ if (params.billingAddress) {
454
+ prePaymentOrderStatesPromise !== null && prePaymentOrderStatesPromise !== void 0 ? prePaymentOrderStatesPromise : (prePaymentOrderStatesPromise = utils_1.getPrePaymentOrderStates.call(this));
455
+ const prePaymentStates = await prePaymentOrderStatesPromise;
412
456
  if (prePaymentStates.includes(previousOrderData.stateId)) {
413
457
  customerData.billingAddress = {
414
458
  id: (0, utils_1.uuidv7)(),
415
- countryId: customerBillingAddress.country,
416
- firstName: customerBillingAddress.firstName,
417
- lastName: customerBillingAddress.lastName,
418
- city: customerBillingAddress.city,
419
- street: customerBillingAddress.street,
459
+ countryId: params.billingAddress.country,
460
+ countryStateId: await utils_1.resolveCountryStateId.call(this, params.billingAddress.country, params.billingAddress.state, countryStateCache, i, 'Billing state / province'),
461
+ salutationId: prevOrderCustomer.salutationId,
462
+ firstName: params.billingAddress.firstName,
463
+ lastName: params.billingAddress.lastName,
464
+ zipcode: (_a = params.billingAddress.zipcode) !== null && _a !== void 0 ? _a : '',
465
+ city: params.billingAddress.city,
466
+ street: params.billingAddress.street,
420
467
  };
421
468
  }
422
469
  }
423
- if (customerShippingAddress) {
470
+ if (params.shippingAddress) {
424
471
  customerData.shippingAddress = {
425
472
  id: (0, utils_1.uuidv7)(),
426
- countryId: customerShippingAddress.country,
427
- firstName: customerShippingAddress.firstName,
428
- lastName: customerShippingAddress.lastName,
429
- city: customerShippingAddress.city,
430
- street: customerShippingAddress.street,
473
+ countryId: params.shippingAddress.country,
474
+ countryStateId: await utils_1.resolveCountryStateId.call(this, params.shippingAddress.country, params.shippingAddress.state, countryStateCache, i, 'Shipping state / province'),
475
+ salutationId: prevOrderCustomer.salutationId,
476
+ firstName: params.shippingAddress.firstName,
477
+ lastName: params.shippingAddress.lastName,
478
+ zipcode: (_b = params.shippingAddress.zipcode) !== null && _b !== void 0 ? _b : '',
479
+ city: params.shippingAddress.city,
480
+ street: params.shippingAddress.street,
431
481
  };
432
482
  }
433
- const nodeLineItems = (_c = updateFields.lineItems) === null || _c === void 0 ? void 0 : _c.lineItem;
434
- let transactions = [];
435
- const nodeTransactions = (_d = updateFields.transactions) === null || _d === void 0 ? void 0 : _d.transaction;
436
483
  let lineItems = [];
437
484
  let price = null;
438
- if (nodeLineItems) {
439
- lineItems = await Promise.all(nodeLineItems.map(async (lineItem) => {
440
- const { identifier, productId, label, states, unitPrice, taxRate } = await utils_1.getLineItemData.call(this, lineItem.productNumber, currencyData.id, i);
441
- const quantity = lineItem.quantity;
442
- const totalPrice = unitPrice * quantity;
443
- const tax = totalPrice * (taxRate / 100);
444
- const taxPrice = totalPrice + tax;
445
- const price = {
446
- unitPrice,
447
- totalPrice,
448
- quantity,
449
- calculatedTaxes: [
450
- {
451
- tax,
452
- taxRate,
453
- price: taxPrice,
454
- },
455
- ],
456
- taxRules: [
457
- {
458
- taxRate,
459
- percentage: 100,
460
- },
461
- ],
462
- };
463
- return {
464
- identifier,
465
- productId,
466
- quantity,
467
- label,
468
- states,
469
- price,
470
- };
485
+ let transactions = [];
486
+ if (params.nodeLineItems) {
487
+ const lineItemLookup = await utils_1.getLineItemDataBatch.call(this, params.nodeLineItems.map((lineItem) => lineItem.productNumber), currencyData.id, i);
488
+ lineItems = await Promise.all(params.nodeLineItems.map(async (lineItem) => {
489
+ const itemData = lineItemLookup.get(lineItem.productNumber);
490
+ if (!itemData) {
491
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No product found', {
492
+ description: 'There is no product associated with product number ' + lineItem.productNumber,
493
+ itemIndex: i,
494
+ });
495
+ }
496
+ return (0, payloadBuilders_1.buildLineItemPayload)({ ...itemData, quantity: lineItem.quantity });
471
497
  }));
472
- const defaultTaxRate = await utils_1.getDefaultTaxRate.call(this);
473
- const totalPrice = lineItems.reduce((acc, item) => acc + item.price.totalPrice, 0) +
474
- previousOrderData.amountNet;
475
- const orderTax = totalPrice * (defaultTaxRate / 100);
476
- const taxPrice = totalPrice + orderTax;
477
- const quantity = lineItems.reduce((acc, item) => acc + item.quantity, 0);
478
- price = {
479
- netPrice: totalPrice,
480
- totalPrice: taxPrice,
481
- calculatedTaxes: [
482
- {
483
- tax: orderTax,
484
- taxRate: defaultTaxRate,
485
- price: taxPrice,
486
- },
487
- ],
488
- taxRules: [
489
- {
490
- taxRate: defaultTaxRate,
491
- percentage: 100,
492
- },
493
- ],
494
- positionPrice: totalPrice,
495
- rawTotal: totalPrice,
496
- taxStatus: 'gross',
497
- };
498
- if (nodeTransactions && nodeTransactions.length > 0) {
499
- transactions = nodeTransactions.map((transaction) => {
500
- const amount = {
501
- unitPrice: totalPrice,
502
- totalPrice: taxPrice,
503
- quantity: quantity,
504
- calculatedTaxes: [
505
- {
506
- tax: orderTax,
507
- taxRate: defaultTaxRate,
508
- price: taxPrice,
509
- },
510
- ],
511
- taxRules: [
512
- {
513
- taxRate: defaultTaxRate,
514
- percentage: 100,
515
- },
516
- ],
517
- };
518
- return {
519
- paymentMethodId: transaction.paymentMethod,
520
- stateId: transaction.state,
521
- amount,
522
- };
523
- });
498
+ const orderTotals = (0, pricing_1.calculateOrderTotals)(lineItems);
499
+ price = (0, payloadBuilders_1.buildOrderPrice)({
500
+ netPrice: orderTotals.netPrice,
501
+ totalPrice: orderTotals.totalPrice,
502
+ calculatedTaxes: orderTotals.calculatedTaxes,
503
+ taxRules: orderTotals.taxRules,
504
+ });
505
+ if (params.nodeTransactions && params.nodeTransactions.length > 0) {
506
+ transactions = params.nodeTransactions.map((transaction) => (0, payloadBuilders_1.buildTransactionPayload)({
507
+ paymentMethodId: transaction.paymentMethod,
508
+ stateId: transaction.state,
509
+ netPrice: orderTotals.netPrice,
510
+ totalPrice: orderTotals.totalPrice,
511
+ quantity: orderTotals.quantity,
512
+ calculatedTaxes: orderTotals.calculatedTaxes,
513
+ taxRules: orderTotals.taxRules,
514
+ }));
524
515
  }
525
516
  }
526
- if (!nodeLineItems && nodeTransactions) {
517
+ if (!params.nodeLineItems && params.nodeTransactions) {
527
518
  throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Missing line items for transaction', {
528
519
  description: 'Line items are required for order transactions',
529
520
  itemIndex: i,
530
521
  });
531
522
  }
532
- const addresses = [];
533
- if (customerShippingAddress && customerData.shippingAddress) {
534
- addresses.push({
523
+ const effectiveLineItems = lineItems.length > 0 ? lineItems : ((_c = prevOrder.lineItems) !== null && _c !== void 0 ? _c : []);
524
+ let customerShippingAddress = null;
525
+ if (params.shippingAddress && customerData.shippingAddress) {
526
+ customerShippingAddress = (0, payloadBuilders_1.buildOrderAddressPayload)({
535
527
  id: customerData.shippingAddress.id,
536
528
  countryId: customerData.shippingAddress.countryId,
529
+ countryStateId: customerData.shippingAddress.countryStateId,
530
+ salutationId: customerData.shippingAddress.salutationId,
537
531
  firstName: customerData.shippingAddress.firstName,
538
532
  lastName: customerData.shippingAddress.lastName,
533
+ zipcode: customerData.shippingAddress.zipcode,
539
534
  city: customerData.shippingAddress.city,
540
535
  street: customerData.shippingAddress.street,
541
536
  });
542
537
  }
543
- let deliveries = [];
544
- const nodeDeliveries = (_e = updateFields.deliveries) === null || _e === void 0 ? void 0 : _e.delivery;
545
- let shippingCosts = null;
546
- if (nodeDeliveries && nodeDeliveries.length > 0) {
547
- deliveries = await Promise.all(nodeDeliveries.map(async (delivery) => {
548
- let shippingOrderAddressId;
538
+ const deliveries = [];
539
+ const shippingCosts = null;
540
+ if (params.nodeDeliveries && params.nodeDeliveries.length > 0) {
541
+ if (effectiveLineItems.length === 0) {
542
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Missing order line items for delivery update', {
543
+ description: 'The order does not contain line items that can be used to rebuild delivery positions.',
544
+ itemIndex: i,
545
+ });
546
+ }
547
+ if (!prevOrder.deliveries || prevOrder.deliveries.length < params.nodeDeliveries.length) {
548
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Delivery update is invalid', {
549
+ description: 'The order does not contain enough existing deliveries to update.',
550
+ itemIndex: i,
551
+ });
552
+ }
553
+ const existingCustomer = customerShippingAddress === null
554
+ && params.nodeDeliveries.some((delivery) => delivery.customerShippingAddress)
555
+ ? await utils_1.getCustomerByNumber.call(this, prevOrderCustomer.customerNumber, i)
556
+ : null;
557
+ for (let deliveryIndex = 0; deliveryIndex < params.nodeDeliveries.length; deliveryIndex++) {
558
+ const delivery = params.nodeDeliveries[deliveryIndex];
559
+ const existingDelivery = prevOrder.deliveries[deliveryIndex];
560
+ let shippingAddress;
549
561
  if (delivery.customerShippingAddress) {
550
- if (addresses.length > 0) {
551
- shippingOrderAddressId = addresses[0].id;
562
+ if (customerShippingAddress) {
563
+ shippingAddress = {
564
+ ...customerShippingAddress,
565
+ id: existingDelivery.shippingOrderAddress.id,
566
+ };
552
567
  }
553
568
  else {
554
- shippingOrderAddressId = (await utils_1.getCustomerByNumber.call(this, prevOrderCustomer.customerNumber, i)).defaultShippingAddressId;
569
+ shippingAddress = {
570
+ id: existingDelivery.shippingOrderAddress.id,
571
+ countryId: existingCustomer.shippingAddress.countryId,
572
+ countryStateId: existingCustomer.shippingAddress.countryStateId,
573
+ salutationId: existingCustomer.shippingAddress.salutationId,
574
+ firstName: existingCustomer.shippingAddress.firstName,
575
+ lastName: existingCustomer.shippingAddress.lastName,
576
+ zipcode: existingCustomer.shippingAddress.zipcode,
577
+ street: existingCustomer.shippingAddress.street,
578
+ city: existingCustomer.shippingAddress.city,
579
+ };
555
580
  }
556
581
  }
557
582
  else {
@@ -562,7 +587,7 @@ async function execute(items) {
562
587
  itemIndex: i,
563
588
  });
564
589
  }
565
- for (const key in address) {
590
+ for (const key of ['country', 'firstName', 'lastName', 'city', 'street']) {
566
591
  if (address[key] === '') {
567
592
  throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Missing required value for shipping address', {
568
593
  description: `Shipping address ${key} must be a valid value.`,
@@ -570,137 +595,83 @@ async function execute(items) {
570
595
  });
571
596
  }
572
597
  }
573
- shippingOrderAddressId = (0, utils_1.uuidv7)();
574
- addresses.push({
575
- id: shippingOrderAddressId,
598
+ shippingAddress = (0, payloadBuilders_1.buildOrderAddressPayload)({
599
+ id: existingDelivery.shippingOrderAddress.id,
576
600
  countryId: address.country,
601
+ countryStateId: await utils_1.resolveCountryStateId.call(this, address.country, address.state, countryStateCache, i, 'Delivery state / province'),
602
+ salutationId: prevOrderCustomer.salutationId,
577
603
  firstName: address.firstName,
578
604
  lastName: address.lastName,
605
+ zipcode: (_d = address.zipcode) !== null && _d !== void 0 ? _d : '',
579
606
  city: address.city,
580
607
  street: address.street,
581
608
  });
582
609
  }
583
- const { ['unitPrice']: shippingPrice, ['taxRate']: shippingTaxRate } = await utils_1.getShippingMethodData.call(this, delivery.shippingMethod, currencyData.id);
584
- const deliveryTime = await utils_1.getShippingDeliveryTime.call(this, delivery.shippingMethod);
585
- const shippingTax = shippingPrice * (shippingTaxRate / 100);
586
- const shippingTaxPrice = shippingPrice + shippingTax;
587
- const shippingCosts = {
588
- unitPrice: shippingPrice,
589
- totalPrice: shippingPrice,
590
- quantity: 1,
591
- calculatedTaxes: [
592
- {
593
- tax: shippingTax,
594
- taxRate: shippingTaxRate,
595
- price: shippingTaxPrice,
596
- },
597
- ],
598
- taxRules: [
599
- {
600
- taxRate: shippingTaxRate,
601
- percentage: 100,
602
- },
603
- ],
604
- };
605
- const shippingDateEarliest = new Date();
606
- const shippingDateLatest = new Date();
607
- switch (deliveryTime.unit) {
608
- case 'hour':
609
- shippingDateEarliest.setHours(shippingDateEarliest.getHours() + deliveryTime.min);
610
- shippingDateLatest.setHours(shippingDateLatest.getHours() + deliveryTime.max);
611
- break;
612
- case 'day':
613
- shippingDateEarliest.setDate(shippingDateEarliest.getDate() + deliveryTime.min);
614
- shippingDateLatest.setDate(shippingDateLatest.getDate() + deliveryTime.max);
615
- break;
616
- case 'week':
617
- shippingDateEarliest.setDate(shippingDateEarliest.getDate() + deliveryTime.min * 7);
618
- shippingDateLatest.setDate(shippingDateLatest.getDate() + deliveryTime.max * 7);
619
- break;
610
+ await transport_1.apiRequest.call(this, 'PATCH', `/order-address/${existingDelivery.shippingOrderAddress.id}`, shippingAddress);
611
+ const shippingMethodDataKey = `${delivery.shippingMethod}:${currencyData.id}`;
612
+ if (!shippingMethodDataCache.has(shippingMethodDataKey)) {
613
+ shippingMethodDataCache.set(shippingMethodDataKey, utils_1.getShippingMethodFullData.call(this, delivery.shippingMethod, currencyData.id));
620
614
  }
621
- return {
622
- shippingOrderAddressId,
615
+ const shippingMethodData = await shippingMethodDataCache.get(shippingMethodDataKey);
616
+ const patchedDelivery = (0, payloadBuilders_1.buildDeliveryPayload)({
617
+ deliveryId: existingDelivery.id,
618
+ shippingAddress,
623
619
  shippingMethodId: delivery.shippingMethod,
624
- stateId: delivery.state,
625
- shippingDateEarliest,
626
- shippingDateLatest,
627
- shippingCosts,
628
- };
629
- }));
630
- const deliveriesUnitPrice = deliveries.reduce((acc, delivery) => acc + delivery.shippingCosts.unitPrice, 0) +
631
- shippingData.unitPrice;
632
- const deliveriesTax = deliveries.reduce((acc, delivery) => acc + delivery.shippingCosts.calculatedTaxes[0].tax, 0) + shippingData.calculatedTaxes[0].tax;
633
- const deliveriesTaxRate = deliveries[0].shippingCosts.taxRules[0].taxRate;
634
- const deliveriesTaxPrice = deliveriesUnitPrice + deliveriesTax;
635
- shippingCosts = {
636
- unitPrice: deliveriesUnitPrice,
637
- totalPrice: deliveriesUnitPrice,
638
- quantity: 1,
639
- calculatedTaxes: [
640
- {
641
- tax: deliveriesTax,
642
- taxRate: deliveriesTaxRate,
643
- price: deliveriesTaxPrice,
644
- },
645
- ],
646
- taxRules: [
647
- {
648
- taxRate: deliveriesTaxRate,
649
- percentage: 100,
650
- },
651
- ],
652
- };
620
+ stateId: existingDelivery.stateId,
621
+ shippingPrice: shippingMethodData.unitPrice,
622
+ shippingTaxRate: shippingMethodData.taxRate,
623
+ deliveryTime: shippingMethodData.deliveryTime,
624
+ lineItems: effectiveLineItems,
625
+ uuidFn: utils_1.uuidv7,
626
+ });
627
+ await transport_1.apiRequest.call(this, 'PATCH', `/order-delivery/${existingDelivery.id}`, {
628
+ shippingMethodId: patchedDelivery.shippingMethodId,
629
+ shippingCosts: patchedDelivery.shippingCosts,
630
+ shippingDateEarliest: patchedDelivery.shippingDateEarliest,
631
+ shippingDateLatest: patchedDelivery.shippingDateLatest,
632
+ });
633
+ }
653
634
  }
654
- let serializedBillingAddress = null;
655
- if (customerData.billingAddress) {
656
- serializedBillingAddress = {
657
- id: customerData.billingAddress.id,
658
- countryId: customerData.billingAddress.countryId,
659
- firstName: customerData.billingAddress.firstName,
660
- lastName: customerData.billingAddress.lastName,
661
- city: customerData.billingAddress.city,
662
- street: customerData.billingAddress.street,
663
- };
635
+ else if (customerShippingAddress && prevOrder.deliveries && prevOrder.deliveries.length > 0) {
636
+ for (const delivery of prevOrder.deliveries) {
637
+ await transport_1.apiRequest.call(this, 'PATCH', `/order-address/${delivery.shippingOrderAddress.id}`, {
638
+ ...customerShippingAddress,
639
+ id: delivery.shippingOrderAddress.id,
640
+ });
641
+ }
664
642
  }
665
- const updateBody = {
666
- billingAddressId: customerData.billingAddress ? customerData.billingAddress.id : null,
667
- billingAddress: serializedBillingAddress,
643
+ const updateBody = (0, payloadBuilders_1.buildOrderUpdatePayload)({
644
+ customerData,
668
645
  lineItems,
669
646
  price,
670
647
  shippingCosts,
671
648
  transactions,
672
649
  deliveries,
673
- addresses,
674
- };
675
- const searchBody = {
676
- fields: fields_1.orderFields,
677
- includes: {
678
- order: fields_1.orderFields,
679
- },
680
- filter: [{ type: 'equals', field: 'id', value: id }],
681
- };
682
- for (const key in updateBody) {
683
- const typedKey = key;
684
- if (Array.isArray(updateBody[typedKey]) &&
685
- updateBody[typedKey].length === 0) {
686
- delete updateBody[typedKey];
687
- }
688
- else if (updateBody[typedKey] === '' || updateBody[typedKey] === null) {
689
- delete updateBody[typedKey];
690
- }
691
- }
692
- if (orderState) {
693
- await transport_1.apiRequest.call(this, 'POST', `/_action/order/${previousOrderData.id}/state/${orderState}`);
694
- }
695
- if (orderState === '') {
650
+ });
651
+ (0, payloadBuilders_1.cleanPayload)(updateBody, true);
652
+ if (params.orderState === '') {
696
653
  throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid order state', {
697
654
  description: 'Please specify a valid order state from the list',
698
655
  itemIndex: i,
699
656
  });
700
657
  }
701
- if (Object.keys(updateFields).length !== 0) {
702
- await transport_1.apiRequest.call(this, 'PATCH', `/order/${id}`, updateBody);
658
+ if (params.orderState) {
659
+ await transport_1.apiRequest.call(this, 'POST', `/_action/order/${orderId}/state/${params.orderState}`);
660
+ }
661
+ if (Object.keys(updateBody).length !== 0) {
662
+ await transport_1.apiRequest.call(this, 'PATCH', `/order/${orderId}`, updateBody);
703
663
  }
664
+ const searchBody = {
665
+ fields: fields_1.orderFields,
666
+ includes: { order: fields_1.orderFields },
667
+ filter: [{ type: 'equals', field: 'id', value: orderId }],
668
+ associations: {
669
+ currency: {},
670
+ deliveries: {},
671
+ transactions: {},
672
+ lineItems: {},
673
+ },
674
+ };
704
675
  const response = await transport_1.apiRequest.call(this, 'POST', `/search/order`, searchBody);
705
676
  const executionData = this.helpers.constructExecutionMetaData((0, utils_1.wrapData)(response.data), {
706
677
  itemData: { item: i },
@@ -712,7 +683,7 @@ async function execute(items) {
712
683
  returnData.push({ json: { error: error.message } });
713
684
  continue;
714
685
  }
715
- if (error instanceof n8n_workflow_1.NodeOperationError) {
686
+ if (error instanceof n8n_workflow_1.NodeOperationError || error instanceof n8n_workflow_1.NodeApiError) {
716
687
  throw error;
717
688
  }
718
689
  throw new n8n_workflow_1.NodeApiError(this.getNode(), error);