@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,9 @@ 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 payloadBuilders_1 = require("../../helpers/payloadBuilders");
10
+ const pricing_1 = require("../../helpers/pricing");
11
+ const params_1 = require("../../helpers/params");
9
12
  const properties = [
10
13
  {
11
14
  displayName: 'Order Number',
@@ -159,6 +162,20 @@ const properties = [
159
162
  default: '',
160
163
  description: 'Customer last name',
161
164
  },
165
+ {
166
+ displayName: 'Zip Code',
167
+ name: 'zipcode',
168
+ type: 'string',
169
+ default: '',
170
+ description: 'Billing address postal code',
171
+ },
172
+ {
173
+ displayName: 'State / Province',
174
+ name: 'state',
175
+ type: 'string',
176
+ default: '',
177
+ description: 'Optional state or province name/code for the billing address',
178
+ },
162
179
  {
163
180
  displayName: 'City',
164
181
  name: 'city',
@@ -226,6 +243,20 @@ const properties = [
226
243
  default: '',
227
244
  description: 'Customer last name',
228
245
  },
246
+ {
247
+ displayName: 'Zip Code',
248
+ name: 'zipcode',
249
+ type: 'string',
250
+ default: '',
251
+ description: 'Shipping address postal code',
252
+ },
253
+ {
254
+ displayName: 'State / Province',
255
+ name: 'state',
256
+ type: 'string',
257
+ default: '',
258
+ description: 'Optional state or province name/code for the shipping address',
259
+ },
229
260
  {
230
261
  displayName: 'City',
231
262
  name: 'city',
@@ -304,7 +335,7 @@ const properties = [
304
335
  type: 'number',
305
336
  typeOptions: {
306
337
  maxValue: 999000000,
307
- minValue: 0,
338
+ minValue: 1,
308
339
  },
309
340
  required: true,
310
341
  default: 1,
@@ -447,6 +478,20 @@ const properties = [
447
478
  default: '',
448
479
  description: 'Customer last name',
449
480
  },
481
+ {
482
+ displayName: 'Zip Code',
483
+ name: 'zipcode',
484
+ type: 'string',
485
+ default: '',
486
+ description: 'Shipping address postal code',
487
+ },
488
+ {
489
+ displayName: 'State / Province',
490
+ name: 'state',
491
+ type: 'string',
492
+ default: '',
493
+ description: 'Optional state or province name/code for the shipping address',
494
+ },
450
495
  {
451
496
  displayName: 'City',
452
497
  name: 'city',
@@ -480,182 +525,136 @@ const displayOptions = {
480
525
  };
481
526
  exports.description = (0, n8n_workflow_1.updateDisplayOptions)(displayOptions, properties);
482
527
  async function execute(items) {
528
+ var _a, _b, _c;
483
529
  const returnData = [];
530
+ let defaultLanguageIdPromise;
531
+ let defaultShippingMethodPromise;
532
+ const shippingMethodDataCache = new Map();
533
+ const countryStateCache = new Map();
484
534
  for (let i = 0; i < items.length; i++) {
485
535
  try {
486
536
  const orderId = (0, utils_1.uuidv7)();
537
+ const params = params_1.extractOrderCreateParams.call(this, i);
538
+ const lineItemLookup = await utils_1.getLineItemDataBatch.call(this, ((_a = params.nodeLineItems) !== null && _a !== void 0 ? _a : []).map((lineItem) => lineItem.productNumber), params.currencyData[0], i);
487
539
  const customerData = {};
488
540
  const globalDefaults = {};
489
541
  let orderCustomer;
490
- const customerNumber = !this.getNodeParameter('guestOrder', i)
491
- ? this.getNodeParameter('customerNumber', i)
492
- : null;
493
- if (customerNumber) {
494
- const customer = await utils_1.getCustomerByNumber.call(this, customerNumber, i);
542
+ if (params.customerNumber) {
543
+ const customer = await utils_1.getCustomerByNumber.call(this, params.customerNumber, i);
495
544
  globalDefaults.languageId = customer.languageId;
496
545
  globalDefaults.salesChannelId = customer.salesChannelId;
497
546
  customerData.firstName = customer.firstName;
498
547
  customerData.lastName = customer.lastName;
499
548
  customerData.email = customer.email;
500
549
  customerData.salutationId = customer.salutationId;
501
- customerData.billingAddress = customer.billingAddress;
502
- customerData.shippingAddress = customer.shippingAddress;
550
+ customerData.billingAddress = {
551
+ ...customer.billingAddress,
552
+ id: (0, utils_1.uuidv7)(),
553
+ };
554
+ customerData.shippingAddress = {
555
+ ...customer.shippingAddress,
556
+ id: (0, utils_1.uuidv7)(),
557
+ };
503
558
  orderCustomer = {
504
559
  firstName: customer.firstName,
505
560
  lastName: customer.lastName,
506
561
  email: customer.email,
507
562
  salutationId: customer.salutationId,
508
563
  customerId: customer.id,
509
- customerNumber,
564
+ customerNumber: params.customerNumber,
510
565
  };
511
566
  }
512
567
  else {
513
- const guest = this.getNodeParameter('guestUi', i)
514
- .guestValues;
515
- const guestBillingAddress = this.getNodeParameter('billingAddressUi', i).billingAddressValues;
516
- const guestShippingAddress = this.getNodeParameter('shippingAddressUi', i).shippingAddressValues;
517
- globalDefaults.languageId = await utils_1.getDefaultLanguageId.call(this);
518
- globalDefaults.salesChannelId = this.getNodeParameter('salesChannel', i);
519
- customerData.firstName = guest.firstName;
520
- customerData.lastName = guest.lastName;
521
- customerData.email = guest.email;
522
- customerData.salutationId = guest.salutation;
568
+ defaultLanguageIdPromise !== null && defaultLanguageIdPromise !== void 0 ? defaultLanguageIdPromise : (defaultLanguageIdPromise = utils_1.getDefaultLanguageId.call(this));
569
+ globalDefaults.languageId = await defaultLanguageIdPromise;
570
+ globalDefaults.salesChannelId = params.salesChannel;
571
+ customerData.firstName = params.guest.firstName;
572
+ customerData.lastName = params.guest.lastName;
573
+ customerData.email = params.guest.email;
574
+ customerData.salutationId = params.guest.salutation;
523
575
  customerData.billingAddress = {
524
576
  id: (0, utils_1.uuidv7)(),
525
- countryId: guestBillingAddress.country,
526
- firstName: guestBillingAddress.firstName,
527
- lastName: guestBillingAddress.lastName,
528
- city: guestBillingAddress.city,
529
- street: guestBillingAddress.street,
577
+ countryId: params.guestBillingAddress.country,
578
+ countryStateId: await utils_1.resolveCountryStateId.call(this, params.guestBillingAddress.country, params.guestBillingAddress.state, countryStateCache, i, 'Billing state / province'),
579
+ salutationId: params.guest.salutation,
580
+ firstName: params.guestBillingAddress.firstName,
581
+ lastName: params.guestBillingAddress.lastName,
582
+ zipcode: (_b = params.guestBillingAddress.zipcode) !== null && _b !== void 0 ? _b : '',
583
+ city: params.guestBillingAddress.city,
584
+ street: params.guestBillingAddress.street,
530
585
  };
531
586
  customerData.shippingAddress = {
532
587
  id: (0, utils_1.uuidv7)(),
533
- countryId: guestShippingAddress.country,
534
- firstName: guestShippingAddress.firstName,
535
- lastName: guestShippingAddress.lastName,
536
- city: guestShippingAddress.city,
537
- street: guestShippingAddress.street,
588
+ countryId: params.guestShippingAddress.country,
589
+ countryStateId: await utils_1.resolveCountryStateId.call(this, params.guestShippingAddress.country, params.guestShippingAddress.state, countryStateCache, i, 'Shipping state / province'),
590
+ salutationId: params.guest.salutation,
591
+ firstName: params.guestShippingAddress.firstName,
592
+ lastName: params.guestShippingAddress.lastName,
593
+ zipcode: (_c = params.guestShippingAddress.zipcode) !== null && _c !== void 0 ? _c : '',
594
+ city: params.guestShippingAddress.city,
595
+ street: params.guestShippingAddress.street,
538
596
  };
539
597
  orderCustomer = {
540
- firstName: guest.firstName,
541
- lastName: guest.lastName,
542
- email: guest.email,
543
- salutationId: guest.salutation,
598
+ firstName: params.guest.firstName,
599
+ lastName: params.guest.lastName,
600
+ email: params.guest.email,
601
+ salutationId: params.guest.salutation,
544
602
  customerNumber: 'guest-' + (0, n8n_workflow_1.randomString)(6),
545
603
  };
546
604
  }
547
- const currencyData = this.getNodeParameter('currency', i).split('-');
548
- const nodeLineItems = this.getNodeParameter('lineItems', i).lineItem;
549
- if (!nodeLineItems || nodeLineItems.length === 0) {
605
+ if (!params.nodeLineItems || params.nodeLineItems.length === 0) {
550
606
  throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Missing order line items', {
551
607
  description: 'At least one line item must be provided',
552
608
  itemIndex: i,
553
609
  });
554
610
  }
555
- const lineItems = await Promise.all(nodeLineItems.map(async (lineItem) => {
556
- const { identifier, productId, label, states, unitPrice, taxRate } = await utils_1.getLineItemData.call(this, lineItem.productNumber, currencyData[0], i);
557
- const quantity = lineItem.quantity;
558
- const totalPrice = unitPrice * quantity;
559
- const tax = totalPrice * (taxRate / 100);
560
- const taxPrice = totalPrice + tax;
561
- const price = {
562
- unitPrice,
563
- totalPrice,
564
- quantity,
565
- calculatedTaxes: [
566
- {
567
- tax,
568
- taxRate,
569
- price: taxPrice,
570
- },
571
- ],
572
- taxRules: [
573
- {
574
- taxRate,
575
- percentage: 100,
576
- },
577
- ],
578
- };
579
- return {
580
- identifier,
581
- productId,
582
- quantity,
583
- label,
584
- states,
585
- price,
586
- };
611
+ const lineItems = await Promise.all(params.nodeLineItems.map(async (lineItem) => {
612
+ const itemData = lineItemLookup.get(lineItem.productNumber);
613
+ if (!itemData) {
614
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No product found', {
615
+ description: 'There is no product associated with product number ' + lineItem.productNumber,
616
+ itemIndex: i,
617
+ });
618
+ }
619
+ return (0, payloadBuilders_1.buildLineItemPayload)({ ...itemData, quantity: lineItem.quantity });
587
620
  }));
588
- const defaultTaxRate = await utils_1.getDefaultTaxRate.call(this);
589
- const totalPrice = lineItems.reduce((acc, item) => acc + item.price.totalPrice, 0);
590
- const orderTax = totalPrice * (defaultTaxRate / 100);
591
- const taxPrice = totalPrice + orderTax;
592
- const quantity = lineItems.reduce((acc, item) => acc + item.quantity, 0);
593
- const price = {
594
- netPrice: totalPrice,
595
- totalPrice: taxPrice,
596
- calculatedTaxes: [
597
- {
598
- tax: orderTax,
599
- taxRate: defaultTaxRate,
600
- price: taxPrice,
601
- },
602
- ],
603
- taxRules: [
604
- {
605
- taxRate: defaultTaxRate,
606
- percentage: 100,
607
- },
608
- ],
609
- positionPrice: totalPrice,
610
- rawTotal: totalPrice,
611
- taxStatus: 'gross',
612
- };
621
+ const orderTotals = (0, pricing_1.calculateOrderTotals)(lineItems);
622
+ const price = (0, payloadBuilders_1.buildOrderPrice)({
623
+ netPrice: orderTotals.netPrice,
624
+ totalPrice: orderTotals.totalPrice,
625
+ calculatedTaxes: orderTotals.calculatedTaxes,
626
+ taxRules: orderTotals.taxRules,
627
+ });
613
628
  let transactions = [];
614
- const nodeTransactions = this.getNodeParameter('transactions', i).transaction;
615
- if (nodeTransactions && nodeTransactions.length > 0) {
616
- transactions = nodeTransactions.map((transaction) => {
617
- const amount = {
618
- unitPrice: totalPrice,
619
- totalPrice: taxPrice,
620
- quantity,
621
- calculatedTaxes: [
622
- {
623
- tax: orderTax,
624
- taxRate: defaultTaxRate,
625
- price: taxPrice,
626
- },
627
- ],
628
- taxRules: [
629
- {
630
- taxRate: defaultTaxRate,
631
- percentage: 100,
632
- },
633
- ],
634
- };
635
- return {
636
- paymentMethodId: transaction.paymentMethod,
637
- stateId: transaction.state,
638
- amount,
639
- };
640
- });
629
+ if (params.nodeTransactions && params.nodeTransactions.length > 0) {
630
+ transactions = params.nodeTransactions.map((transaction) => (0, payloadBuilders_1.buildTransactionPayload)({
631
+ paymentMethodId: transaction.paymentMethod,
632
+ stateId: transaction.state,
633
+ netPrice: orderTotals.netPrice,
634
+ totalPrice: orderTotals.totalPrice,
635
+ quantity: orderTotals.quantity,
636
+ calculatedTaxes: orderTotals.calculatedTaxes,
637
+ taxRules: orderTotals.taxRules,
638
+ }));
641
639
  }
642
- const addresses = [
643
- {
644
- id: customerData.shippingAddress.id,
645
- countryId: customerData.shippingAddress.countryId,
646
- firstName: customerData.shippingAddress.firstName,
647
- lastName: customerData.shippingAddress.lastName,
648
- city: customerData.shippingAddress.city,
649
- street: customerData.shippingAddress.street,
650
- },
651
- ];
640
+ const customerShippingAddress = (0, payloadBuilders_1.buildOrderAddressPayload)({
641
+ id: customerData.shippingAddress.id,
642
+ countryId: customerData.shippingAddress.countryId,
643
+ countryStateId: customerData.shippingAddress.countryStateId,
644
+ salutationId: customerData.shippingAddress.salutationId,
645
+ firstName: customerData.shippingAddress.firstName,
646
+ lastName: customerData.shippingAddress.lastName,
647
+ zipcode: customerData.shippingAddress.zipcode,
648
+ city: customerData.shippingAddress.city,
649
+ street: customerData.shippingAddress.street,
650
+ });
652
651
  let deliveries = [];
653
- const nodeDeliveries = this.getNodeParameter('deliveries', i).delivery;
654
- if (nodeDeliveries && nodeDeliveries.length > 0) {
655
- deliveries = await Promise.all(nodeDeliveries.map(async (delivery) => {
656
- let shippingOrderAddressId;
652
+ if (params.nodeDeliveries && params.nodeDeliveries.length > 0) {
653
+ deliveries = await Promise.all(params.nodeDeliveries.map(async (delivery) => {
654
+ var _a;
655
+ let shippingAddress;
657
656
  if (delivery.customerShippingAddress) {
658
- shippingOrderAddressId = addresses[0].id;
657
+ shippingAddress = { ...customerShippingAddress, id: (0, utils_1.uuidv7)() };
659
658
  }
660
659
  else {
661
660
  const address = delivery.addressUi.addressValues;
@@ -665,7 +664,7 @@ async function execute(items) {
665
664
  itemIndex: i,
666
665
  });
667
666
  }
668
- for (const key in address) {
667
+ for (const key of ['country', 'firstName', 'lastName', 'city', 'street']) {
669
668
  if (address[key] === '') {
670
669
  throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Missing required value for shipping address', {
671
670
  description: `Shipping address ${key} must be a valid value.`,
@@ -673,172 +672,89 @@ async function execute(items) {
673
672
  });
674
673
  }
675
674
  }
676
- shippingOrderAddressId = (0, utils_1.uuidv7)();
677
- addresses.push({
678
- id: shippingOrderAddressId,
675
+ shippingAddress = (0, payloadBuilders_1.buildOrderAddressPayload)({
676
+ id: (0, utils_1.uuidv7)(),
679
677
  countryId: address.country,
678
+ countryStateId: await utils_1.resolveCountryStateId.call(this, address.country, address.state, countryStateCache, i, 'Delivery state / province'),
679
+ salutationId: customerData.salutationId,
680
680
  firstName: address.firstName,
681
681
  lastName: address.lastName,
682
+ zipcode: (_a = address.zipcode) !== null && _a !== void 0 ? _a : '',
682
683
  city: address.city,
683
684
  street: address.street,
684
685
  });
685
686
  }
686
- const { ['unitPrice']: shippingPrice, ['taxRate']: shippingTaxRate } = await utils_1.getShippingMethodData.call(this, delivery.shippingMethod, currencyData[0]);
687
- const deliveryTime = await utils_1.getShippingDeliveryTime.call(this, delivery.shippingMethod);
688
- const shippingTax = shippingPrice * (shippingTaxRate / 100);
689
- const shippingTaxPrice = shippingPrice + shippingTax;
690
- const shippingCosts = {
691
- unitPrice: shippingPrice,
692
- totalPrice: shippingPrice,
693
- quantity: 1,
694
- calculatedTaxes: [
695
- {
696
- tax: shippingTax,
697
- taxRate: shippingTaxRate,
698
- price: shippingTaxPrice,
699
- },
700
- ],
701
- taxRules: [
702
- {
703
- taxRate: shippingTaxRate,
704
- percentage: 100,
705
- },
706
- ],
707
- };
708
- const shippingDateEarliest = new Date();
709
- const shippingDateLatest = new Date();
710
- switch (deliveryTime.unit) {
711
- case 'hour':
712
- shippingDateEarliest.setHours(shippingDateEarliest.getHours() + deliveryTime.min);
713
- shippingDateLatest.setHours(shippingDateLatest.getHours() + deliveryTime.max);
714
- break;
715
- case 'day':
716
- shippingDateEarliest.setDate(shippingDateEarliest.getDate() + deliveryTime.min);
717
- shippingDateLatest.setDate(shippingDateLatest.getDate() + deliveryTime.max);
718
- break;
719
- case 'week':
720
- shippingDateEarliest.setDate(shippingDateEarliest.getDate() + deliveryTime.min * 7);
721
- shippingDateLatest.setDate(shippingDateLatest.getDate() + deliveryTime.max * 7);
722
- break;
687
+ const shippingMethodDataKey = `${delivery.shippingMethod}:${params.currencyData[0]}`;
688
+ if (!shippingMethodDataCache.has(shippingMethodDataKey)) {
689
+ shippingMethodDataCache.set(shippingMethodDataKey, Promise.resolve(utils_1.getShippingMethodFullData.call(this, delivery.shippingMethod, params.currencyData[0])));
723
690
  }
724
- return {
725
- shippingOrderAddressId,
691
+ const shippingMethodData = await shippingMethodDataCache.get(shippingMethodDataKey);
692
+ return (0, payloadBuilders_1.buildDeliveryPayload)({
693
+ shippingAddress,
726
694
  shippingMethodId: delivery.shippingMethod,
727
695
  stateId: delivery.state,
728
- shippingDateEarliest,
729
- shippingDateLatest,
730
- shippingCosts,
731
- };
696
+ shippingPrice: shippingMethodData.unitPrice,
697
+ shippingTaxRate: shippingMethodData.taxRate,
698
+ deliveryTime: shippingMethodData.deliveryTime,
699
+ lineItems,
700
+ uuidFn: utils_1.uuidv7,
701
+ });
732
702
  }));
733
703
  }
734
704
  let shippingCosts;
735
705
  if (deliveries.length === 0) {
736
- const defaultShippingMethod = await utils_1.getDefaultShippingMethod.call(this);
737
- const { ['unitPrice']: shippingPrice, ['taxRate']: shippingTaxRate } = await utils_1.getShippingMethodData.call(this, defaultShippingMethod, currencyData[0]);
738
- const shippingTax = shippingPrice * (shippingTaxRate / 100);
739
- const shippingTaxPrice = shippingPrice + shippingTax;
740
- shippingCosts = {
741
- unitPrice: shippingPrice,
742
- totalPrice: shippingPrice,
743
- quantity: 1,
744
- calculatedTaxes: [
745
- {
746
- tax: shippingTax,
747
- taxRate: shippingTaxRate,
748
- price: shippingTaxPrice,
749
- },
750
- ],
751
- taxRules: [
752
- {
753
- taxRate: shippingTaxRate,
754
- percentage: 100,
755
- },
756
- ],
757
- };
706
+ defaultShippingMethodPromise !== null && defaultShippingMethodPromise !== void 0 ? defaultShippingMethodPromise : (defaultShippingMethodPromise = utils_1.getDefaultShippingMethod.call(this));
707
+ const defaultShippingMethod = await defaultShippingMethodPromise;
708
+ const shippingMethodDataKey = `${defaultShippingMethod}:${params.currencyData[0]}`;
709
+ if (!shippingMethodDataCache.has(shippingMethodDataKey)) {
710
+ shippingMethodDataCache.set(shippingMethodDataKey, Promise.resolve(utils_1.getShippingMethodFullData.call(this, defaultShippingMethod, params.currencyData[0])));
711
+ }
712
+ const shippingMethodData = await shippingMethodDataCache.get(shippingMethodDataKey);
713
+ deliveries = [
714
+ (0, payloadBuilders_1.buildDeliveryPayload)({
715
+ shippingAddress: { ...customerShippingAddress, id: (0, utils_1.uuidv7)() },
716
+ shippingMethodId: defaultShippingMethod,
717
+ stateId: params.stateId,
718
+ shippingPrice: shippingMethodData.unitPrice,
719
+ shippingTaxRate: shippingMethodData.taxRate,
720
+ deliveryTime: shippingMethodData.deliveryTime,
721
+ lineItems,
722
+ uuidFn: utils_1.uuidv7,
723
+ }),
724
+ ];
725
+ shippingCosts = (0, payloadBuilders_1.aggregateDeliveryShippingCosts)(deliveries);
758
726
  }
759
727
  else {
760
- const deliveriesUnitPrice = deliveries.reduce((acc, delivery) => acc + delivery.shippingCosts.unitPrice, 0);
761
- const deliveriesTax = deliveries.reduce((acc, delivery) => acc + delivery.shippingCosts.calculatedTaxes[0].tax, 0);
762
- const deliveriesTaxRate = deliveries[0].shippingCosts.taxRules[0].taxRate;
763
- const deliveriesTaxPrice = deliveriesUnitPrice + deliveriesTax;
764
- shippingCosts = {
765
- unitPrice: deliveriesUnitPrice,
766
- totalPrice: deliveriesUnitPrice,
767
- quantity: 1,
768
- calculatedTaxes: [
769
- {
770
- tax: deliveriesTax,
771
- taxRate: deliveriesTaxRate,
772
- price: deliveriesTaxPrice,
773
- },
774
- ],
775
- taxRules: [
776
- {
777
- taxRate: deliveriesTaxRate,
778
- percentage: 100,
779
- },
780
- ],
781
- };
728
+ shippingCosts = (0, payloadBuilders_1.aggregateDeliveryShippingCosts)(deliveries);
782
729
  }
783
- const parsedItemRounding = JSON.parse(currencyData[3]);
784
- const itemRounding = {
785
- decimals: parsedItemRounding.decimals,
786
- interval: parsedItemRounding.interval,
787
- roundForNet: parsedItemRounding.roundForNet,
788
- };
789
- const parsedTotalRounding = JSON.parse(currencyData[4]);
790
- const totalRounding = {
791
- decimals: parsedTotalRounding.decimals,
792
- interval: parsedTotalRounding.interval,
793
- roundForNet: parsedTotalRounding.roundForNet,
794
- };
795
- const serializedBillingAddress = {
796
- id: customerData.billingAddress.id,
797
- countryId: customerData.billingAddress.countryId,
798
- firstName: customerData.billingAddress.firstName,
799
- lastName: customerData.billingAddress.lastName,
800
- city: customerData.billingAddress.city,
801
- street: customerData.billingAddress.street,
802
- };
803
- const createBody = {
804
- id: orderId,
805
- currencyId: currencyData[0],
806
- languageId: globalDefaults.languageId,
807
- salesChannelId: globalDefaults.salesChannelId,
808
- billingAddressId: customerData.billingAddress.id,
809
- orderNumber: this.getNodeParameter('orderNumber', i),
810
- orderDateTime: this.getNodeParameter('dateAndTime', i),
811
- stateId: this.getNodeParameter('state', i),
812
- currencyFactor: parseInt(currencyData[2]),
813
- itemRounding,
814
- totalRounding,
730
+ const createBody = (0, payloadBuilders_1.buildOrderCreatePayload)({
731
+ orderId,
732
+ currencyData: params.currencyData,
733
+ globalDefaults,
734
+ customerData,
815
735
  orderCustomer,
816
- billingAddress: serializedBillingAddress,
817
736
  lineItems,
818
737
  price,
819
738
  shippingCosts,
820
739
  transactions,
821
740
  deliveries,
822
- addresses,
823
- };
741
+ orderNumber: params.orderNumber,
742
+ dateAndTime: params.dateAndTime,
743
+ stateId: params.stateId,
744
+ });
745
+ (0, payloadBuilders_1.cleanPayload)(createBody);
746
+ await transport_1.apiRequest.call(this, 'POST', `/order`, createBody);
824
747
  const searchBody = {
825
748
  fields: fields_1.orderFields,
826
- includes: {
827
- order: fields_1.orderFields,
828
- },
749
+ includes: { order: fields_1.orderFields },
829
750
  filter: [{ type: 'equals', field: 'id', value: orderId }],
751
+ associations: {
752
+ currency: {},
753
+ deliveries: {},
754
+ transactions: {},
755
+ lineItems: {},
756
+ },
830
757
  };
831
- for (const key in createBody) {
832
- const typedKey = key;
833
- if (Array.isArray(createBody[typedKey]) &&
834
- createBody[typedKey].length === 0) {
835
- delete createBody[typedKey];
836
- }
837
- else if (createBody[typedKey] === '') {
838
- delete createBody[typedKey];
839
- }
840
- }
841
- await transport_1.apiRequest.call(this, 'POST', `/order`, createBody);
842
758
  const response = await transport_1.apiRequest.call(this, 'POST', `/search/order`, searchBody);
843
759
  const executionData = this.helpers.constructExecutionMetaData((0, utils_1.wrapData)(response.data), {
844
760
  itemData: { item: i },
@@ -850,7 +766,7 @@ async function execute(items) {
850
766
  returnData.push({ json: { error: error.message } });
851
767
  continue;
852
768
  }
853
- if (error instanceof n8n_workflow_1.NodeOperationError) {
769
+ if (error instanceof n8n_workflow_1.NodeOperationError || error instanceof n8n_workflow_1.NodeApiError) {
854
770
  throw error;
855
771
  }
856
772
  throw new n8n_workflow_1.NodeApiError(this.getNode(), error);