@medusajs/medusa 1.7.2 → 1.7.3-alpha.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 (302) hide show
  1. package/.turbo/turbo-test.log +0 -75
  2. package/CHANGELOG.md +50 -0
  3. package/dist/api/index.d.ts +1 -0
  4. package/dist/api/index.js +1 -0
  5. package/dist/api/index.js.map +1 -1
  6. package/dist/api/middlewares/check-registered-modules.d.ts +4 -0
  7. package/dist/api/middlewares/check-registered-modules.js +78 -0
  8. package/dist/api/middlewares/check-registered-modules.js.map +1 -0
  9. package/dist/api/routes/admin/discounts/add-resources-to-condition-batch.d.ts +1 -1
  10. package/dist/api/routes/admin/discounts/add-resources-to-condition-batch.js +1 -1
  11. package/dist/api/routes/admin/discounts/delete-resources-from-condition-batch.d.ts +1 -1
  12. package/dist/api/routes/admin/discounts/delete-resources-from-condition-batch.js +1 -1
  13. package/dist/api/routes/admin/discounts/list-discounts.d.ts +1 -1
  14. package/dist/api/routes/admin/index.js +4 -0
  15. package/dist/api/routes/admin/index.js.map +1 -1
  16. package/dist/api/routes/admin/order-edits/add-line-item.d.ts +1 -1
  17. package/dist/api/routes/admin/order-edits/add-line-item.js +1 -1
  18. package/dist/api/routes/admin/order-edits/delete-line-item.d.ts +1 -1
  19. package/dist/api/routes/admin/order-edits/delete-line-item.js +1 -1
  20. package/dist/api/routes/admin/order-edits/delete-order-edit-item-change.d.ts +1 -1
  21. package/dist/api/routes/admin/order-edits/delete-order-edit-item-change.js +1 -1
  22. package/dist/api/routes/admin/order-edits/delete-order-edit.d.ts +1 -1
  23. package/dist/api/routes/admin/order-edits/delete-order-edit.js +1 -1
  24. package/dist/api/routes/admin/order-edits/get-order-edit.d.ts +1 -1
  25. package/dist/api/routes/admin/order-edits/get-order-edit.js +1 -1
  26. package/dist/api/routes/admin/order-edits/list-order-edit.d.ts +1 -1
  27. package/dist/api/routes/admin/order-edits/request-confirmation.d.ts +1 -1
  28. package/dist/api/routes/admin/order-edits/request-confirmation.js +1 -1
  29. package/dist/api/routes/admin/order-edits/update-order-edit-line-item.d.ts +1 -1
  30. package/dist/api/routes/admin/order-edits/update-order-edit-line-item.js +1 -1
  31. package/dist/api/routes/admin/order-edits/update-order-edit.d.ts +1 -1
  32. package/dist/api/routes/admin/order-edits/update-order-edit.js +1 -1
  33. package/dist/api/routes/admin/orders/cancel-fulfillment.d.ts +5 -0
  34. package/dist/api/routes/admin/orders/cancel-fulfillment.js +46 -2
  35. package/dist/api/routes/admin/orders/cancel-fulfillment.js.map +1 -1
  36. package/dist/api/routes/admin/orders/create-fulfillment.d.ts +1 -0
  37. package/dist/api/routes/admin/orders/create-fulfillment.js +87 -5
  38. package/dist/api/routes/admin/orders/create-fulfillment.js.map +1 -1
  39. package/dist/api/routes/admin/orders/index.js +3 -7
  40. package/dist/api/routes/admin/orders/index.js.map +1 -1
  41. package/dist/api/routes/admin/payment-collections/delete-payment-collection.d.ts +1 -1
  42. package/dist/api/routes/admin/payment-collections/delete-payment-collection.js +1 -1
  43. package/dist/api/routes/admin/payment-collections/get-payment-collection.d.ts +1 -1
  44. package/dist/api/routes/admin/payment-collections/get-payment-collection.js +1 -1
  45. package/dist/api/routes/admin/payment-collections/mark-authorized-payment-collection.d.ts +1 -1
  46. package/dist/api/routes/admin/payment-collections/mark-authorized-payment-collection.js +1 -1
  47. package/dist/api/routes/admin/payment-collections/update-payment-collection.d.ts +1 -1
  48. package/dist/api/routes/admin/payment-collections/update-payment-collection.js +1 -1
  49. package/dist/api/routes/admin/price-lists/list-price-list-products.d.ts +2 -2
  50. package/dist/api/routes/admin/price-lists/list-price-list-products.js +1 -1
  51. package/dist/api/routes/admin/product-categories/delete-product-category.d.ts +64 -0
  52. package/dist/api/routes/admin/product-categories/delete-product-category.js +129 -0
  53. package/dist/api/routes/admin/product-categories/delete-product-category.js.map +1 -0
  54. package/dist/api/routes/admin/product-categories/get-product-category.d.ts +58 -0
  55. package/dist/api/routes/admin/product-categories/get-product-category.js +134 -0
  56. package/dist/api/routes/admin/product-categories/get-product-category.js.map +1 -0
  57. package/dist/api/routes/admin/product-categories/index.d.ts +7 -0
  58. package/dist/api/routes/admin/product-categories/index.js +68 -0
  59. package/dist/api/routes/admin/product-categories/index.js.map +1 -0
  60. package/dist/api/routes/admin/product-categories/list-product-categories.d.ts +78 -0
  61. package/dist/api/routes/admin/product-categories/list-product-categories.js +206 -0
  62. package/dist/api/routes/admin/product-categories/list-product-categories.js.map +1 -0
  63. package/dist/api/routes/admin/publishable-api-keys/add-channels-batch.d.ts +1 -1
  64. package/dist/api/routes/admin/publishable-api-keys/add-channels-batch.js +1 -1
  65. package/dist/api/routes/admin/publishable-api-keys/create-publishable-api-key.d.ts +1 -1
  66. package/dist/api/routes/admin/publishable-api-keys/create-publishable-api-key.js +1 -1
  67. package/dist/api/routes/admin/publishable-api-keys/delete-channels-batch.d.ts +1 -1
  68. package/dist/api/routes/admin/publishable-api-keys/delete-channels-batch.js +1 -1
  69. package/dist/api/routes/admin/publishable-api-keys/delete-publishable-api-key.d.ts +1 -1
  70. package/dist/api/routes/admin/publishable-api-keys/delete-publishable-api-key.js +1 -1
  71. package/dist/api/routes/admin/publishable-api-keys/get-publishable-api-key.d.ts +1 -1
  72. package/dist/api/routes/admin/publishable-api-keys/get-publishable-api-key.js +1 -1
  73. package/dist/api/routes/admin/publishable-api-keys/list-publishable-api-key-sales-channels.d.ts +2 -2
  74. package/dist/api/routes/admin/publishable-api-keys/list-publishable-api-key-sales-channels.js +1 -1
  75. package/dist/api/routes/admin/publishable-api-keys/list-publishable-api-keys.d.ts +1 -1
  76. package/dist/api/routes/admin/publishable-api-keys/revoke-publishable-api-key.d.ts +1 -1
  77. package/dist/api/routes/admin/publishable-api-keys/revoke-publishable-api-key.js +1 -1
  78. package/dist/api/routes/admin/publishable-api-keys/update-publishable-api-key.d.ts +1 -1
  79. package/dist/api/routes/admin/publishable-api-keys/update-publishable-api-key.js +1 -1
  80. package/dist/api/routes/admin/returns/cancel-return.js.map +1 -1
  81. package/dist/api/routes/admin/returns/receive-return.d.ts +1 -0
  82. package/dist/api/routes/admin/returns/receive-return.js +8 -1
  83. package/dist/api/routes/admin/returns/receive-return.js.map +1 -1
  84. package/dist/api/routes/admin/sales-channels/associate-stock-location.d.ts +79 -0
  85. package/dist/api/routes/admin/sales-channels/associate-stock-location.js +163 -0
  86. package/dist/api/routes/admin/sales-channels/associate-stock-location.js.map +1 -0
  87. package/dist/api/routes/admin/sales-channels/index.d.ts +2 -0
  88. package/dist/api/routes/admin/sales-channels/index.js +6 -0
  89. package/dist/api/routes/admin/sales-channels/index.js.map +1 -1
  90. package/dist/api/routes/admin/sales-channels/list-sales-channels.d.ts +1 -1
  91. package/dist/api/routes/admin/sales-channels/remove-stock-location.d.ts +88 -0
  92. package/dist/api/routes/admin/sales-channels/remove-stock-location.js +174 -0
  93. package/dist/api/routes/admin/sales-channels/remove-stock-location.js.map +1 -0
  94. package/dist/api/routes/admin/stock-locations/create-stock-location.d.ts +105 -0
  95. package/dist/api/routes/admin/stock-locations/create-stock-location.js +240 -0
  96. package/dist/api/routes/admin/stock-locations/create-stock-location.js.map +1 -0
  97. package/dist/api/routes/admin/stock-locations/get-stock-location.d.ts +48 -0
  98. package/dist/api/routes/admin/stock-locations/get-stock-location.js +121 -0
  99. package/dist/api/routes/admin/stock-locations/get-stock-location.js.map +1 -0
  100. package/dist/api/routes/admin/stock-locations/index.d.ts +18 -0
  101. package/dist/api/routes/admin/stock-locations/index.js +78 -0
  102. package/dist/api/routes/admin/stock-locations/index.js.map +1 -0
  103. package/dist/api/routes/admin/stock-locations/list-stock-locations.d.ts +144 -0
  104. package/dist/api/routes/admin/stock-locations/list-stock-locations.js +265 -0
  105. package/dist/api/routes/admin/stock-locations/list-stock-locations.js.map +1 -0
  106. package/dist/api/routes/admin/stock-locations/update-stock-location.d.ts +103 -0
  107. package/dist/api/routes/admin/stock-locations/update-stock-location.js +240 -0
  108. package/dist/api/routes/admin/stock-locations/update-stock-location.js.map +1 -0
  109. package/dist/api/routes/admin/uploads/create-protected-upload.d.ts +2 -2
  110. package/dist/api/routes/admin/uploads/create-protected-upload.js +2 -2
  111. package/dist/api/routes/admin/uploads/delete-upload.d.ts +1 -1
  112. package/dist/api/routes/admin/uploads/delete-upload.js +1 -1
  113. package/dist/api/routes/admin/uploads/get-download-url.d.ts +1 -1
  114. package/dist/api/routes/admin/uploads/get-download-url.js +1 -1
  115. package/dist/api/routes/store/order-edits/complete-order-edit.js +35 -34
  116. package/dist/api/routes/store/order-edits/complete-order-edit.js.map +1 -1
  117. package/dist/api/routes/store/orders/confirm-order-request.d.ts +2 -2
  118. package/dist/api/routes/store/orders/confirm-order-request.js +2 -2
  119. package/dist/api/routes/store/orders/request-order.d.ts +1 -1
  120. package/dist/api/routes/store/orders/request-order.js +1 -1
  121. package/dist/api/routes/store/payment-collections/authorize-batch-payment-sessions.d.ts +2 -2
  122. package/dist/api/routes/store/payment-collections/authorize-batch-payment-sessions.js +2 -2
  123. package/dist/api/routes/store/payment-collections/authorize-payment-session.d.ts +2 -2
  124. package/dist/api/routes/store/payment-collections/authorize-payment-session.js +2 -2
  125. package/dist/api/routes/store/payment-collections/get-payment-collection.d.ts +2 -2
  126. package/dist/api/routes/store/payment-collections/get-payment-collection.js +2 -2
  127. package/dist/api/routes/store/payment-collections/manage-batch-payment-sessions.d.ts +2 -2
  128. package/dist/api/routes/store/payment-collections/manage-batch-payment-sessions.js +2 -2
  129. package/dist/api/routes/store/payment-collections/manage-payment-session.d.ts +2 -2
  130. package/dist/api/routes/store/payment-collections/manage-payment-session.js +2 -2
  131. package/dist/api/routes/store/payment-collections/refresh-payment-session.d.ts +1 -1
  132. package/dist/api/routes/store/payment-collections/refresh-payment-session.js +1 -1
  133. package/dist/commands/develop.js +24 -0
  134. package/dist/commands/develop.js.map +1 -1
  135. package/dist/index.d.ts +6 -2
  136. package/dist/index.js +6 -2
  137. package/dist/index.js.map +1 -1
  138. package/dist/interfaces/payment-processor.d.ts +102 -0
  139. package/dist/interfaces/payment-processor.js +31 -0
  140. package/dist/interfaces/payment-processor.js.map +1 -0
  141. package/dist/interfaces/payment-service.d.ts +92 -1
  142. package/dist/interfaces/payment-service.js +10 -0
  143. package/dist/interfaces/payment-service.js.map +1 -1
  144. package/dist/interfaces/services/event-bus.d.ts +3 -0
  145. package/dist/interfaces/services/event-bus.js +3 -0
  146. package/dist/interfaces/services/event-bus.js.map +1 -0
  147. package/dist/interfaces/services/index.d.ts +3 -0
  148. package/dist/interfaces/services/index.js +3 -0
  149. package/dist/interfaces/services/index.js.map +1 -1
  150. package/dist/interfaces/services/inventory.d.ts +24 -0
  151. package/dist/interfaces/services/inventory.js +3 -0
  152. package/dist/interfaces/services/inventory.js.map +1 -0
  153. package/dist/interfaces/services/stock-location.d.ts +9 -0
  154. package/dist/interfaces/services/stock-location.js +3 -0
  155. package/dist/interfaces/services/stock-location.js.map +1 -0
  156. package/dist/loaders/feature-flags/product-categories.d.ts +3 -0
  157. package/dist/loaders/feature-flags/product-categories.js +10 -0
  158. package/dist/loaders/feature-flags/product-categories.js.map +1 -0
  159. package/dist/loaders/feature-flags/publishable-api-keys.js +1 -1
  160. package/dist/loaders/feature-flags/publishable-api-keys.js.map +1 -1
  161. package/dist/loaders/feature-flags/sales-channels.js +1 -1
  162. package/dist/loaders/feature-flags/sales-channels.js.map +1 -1
  163. package/dist/loaders/plugins.d.ts +1 -0
  164. package/dist/loaders/plugins.js +3 -1
  165. package/dist/loaders/plugins.js.map +1 -1
  166. package/dist/migrations/1671711415179-multi_location.d.ts +6 -0
  167. package/dist/migrations/1671711415179-multi_location.js +119 -0
  168. package/dist/migrations/1671711415179-multi_location.js.map +1 -0
  169. package/dist/migrations/1672906846559-product-category.d.ts +6 -0
  170. package/dist/migrations/1672906846559-product-category.js +83 -0
  171. package/dist/migrations/1672906846559-product-category.js.map +1 -0
  172. package/dist/migrations/1672906846560-payment-session-is-initiated.d.ts +6 -0
  173. package/dist/migrations/1672906846560-payment-session-is-initiated.js +81 -0
  174. package/dist/migrations/1672906846560-payment-session-is-initiated.js.map +1 -0
  175. package/dist/migrations/1673003729870-staged_job_options.d.ts +6 -0
  176. package/dist/migrations/1673003729870-staged_job_options.js +71 -0
  177. package/dist/migrations/1673003729870-staged_job_options.js.map +1 -0
  178. package/dist/models/fulfillment.d.ts +5 -0
  179. package/dist/models/fulfillment.js +8 -0
  180. package/dist/models/fulfillment.js.map +1 -1
  181. package/dist/models/index.d.ts +4 -0
  182. package/dist/models/index.js +4 -0
  183. package/dist/models/index.js.map +1 -1
  184. package/dist/models/line-item.d.ts +6 -6
  185. package/dist/models/line-item.js +9 -9
  186. package/dist/models/line-item.js.map +1 -1
  187. package/dist/models/payment-session.d.ts +6 -0
  188. package/dist/models/payment-session.js +9 -0
  189. package/dist/models/payment-session.js.map +1 -1
  190. package/dist/models/product-category.d.ts +67 -0
  191. package/dist/models/product-category.js +144 -0
  192. package/dist/models/product-category.js.map +1 -0
  193. package/dist/models/product-variant-inventory-item.d.ts +7 -0
  194. package/dist/models/product-variant-inventory-item.js +66 -0
  195. package/dist/models/product-variant-inventory-item.js.map +1 -0
  196. package/dist/models/return.d.ts +7 -6
  197. package/dist/models/return.js +14 -9
  198. package/dist/models/return.js.map +1 -1
  199. package/dist/models/sales-channel-location.d.ts +6 -0
  200. package/dist/models/sales-channel-location.js +62 -0
  201. package/dist/models/sales-channel-location.js.map +1 -0
  202. package/dist/models/staged-job.d.ts +1 -0
  203. package/dist/models/staged-job.js +4 -0
  204. package/dist/models/staged-job.js.map +1 -1
  205. package/dist/models/store.d.ts +6 -5
  206. package/dist/models/store.js +11 -7
  207. package/dist/models/store.js.map +1 -1
  208. package/dist/repositories/customer-group.js +18 -2
  209. package/dist/repositories/customer-group.js.map +1 -1
  210. package/dist/repositories/product-category.d.ts +6 -0
  211. package/dist/repositories/product-category.js +125 -0
  212. package/dist/repositories/product-category.js.map +1 -0
  213. package/dist/repositories/product-variant.js +30 -5
  214. package/dist/repositories/product-variant.js.map +1 -1
  215. package/dist/repositories/product.js +7 -2
  216. package/dist/repositories/product.js.map +1 -1
  217. package/dist/scripts/migrate-inventory-items.d.ts +1 -0
  218. package/dist/scripts/migrate-inventory-items.js +197 -0
  219. package/dist/scripts/migrate-inventory-items.js.map +1 -0
  220. package/dist/services/cache.js +2 -5
  221. package/dist/services/cache.js.map +1 -1
  222. package/dist/services/cart.d.ts +9 -26
  223. package/dist/services/cart.js +276 -182
  224. package/dist/services/cart.js.map +1 -1
  225. package/dist/services/claim-item.js +3 -0
  226. package/dist/services/claim-item.js.map +1 -1
  227. package/dist/services/claim.d.ts +4 -4
  228. package/dist/services/claim.js +132 -166
  229. package/dist/services/claim.js.map +1 -1
  230. package/dist/services/event-bus.d.ts +23 -10
  231. package/dist/services/event-bus.js +106 -42
  232. package/dist/services/event-bus.js.map +1 -1
  233. package/dist/services/fulfillment.d.ts +7 -3
  234. package/dist/services/fulfillment.js +79 -90
  235. package/dist/services/fulfillment.js.map +1 -1
  236. package/dist/services/index.d.ts +4 -1
  237. package/dist/services/index.js +10 -4
  238. package/dist/services/index.js.map +1 -1
  239. package/dist/services/line-item.d.ts +5 -5
  240. package/dist/services/line-item.js +2 -1
  241. package/dist/services/line-item.js.map +1 -1
  242. package/dist/services/order.d.ts +5 -19
  243. package/dist/services/order.js +146 -181
  244. package/dist/services/order.js.map +1 -1
  245. package/dist/services/payment-collection.d.ts +2 -2
  246. package/dist/services/payment-collection.js +88 -128
  247. package/dist/services/payment-collection.js.map +1 -1
  248. package/dist/services/payment-provider.d.ts +2 -1
  249. package/dist/services/payment-provider.js +32 -27
  250. package/dist/services/payment-provider.js.map +1 -1
  251. package/dist/services/product-category.d.ts +41 -0
  252. package/dist/services/product-category.js +190 -0
  253. package/dist/services/product-category.js.map +1 -0
  254. package/dist/services/product-variant-inventory.d.ts +115 -0
  255. package/dist/services/product-variant-inventory.js +747 -0
  256. package/dist/services/product-variant-inventory.js.map +1 -0
  257. package/dist/services/return.d.ts +7 -12
  258. package/dist/services/return.js +12 -9
  259. package/dist/services/return.js.map +1 -1
  260. package/dist/services/sales-channel-inventory.d.ts +24 -0
  261. package/dist/services/sales-channel-inventory.js +70 -0
  262. package/dist/services/sales-channel-inventory.js.map +1 -0
  263. package/dist/services/sales-channel-location.d.ts +41 -0
  264. package/dist/services/sales-channel-location.js +159 -0
  265. package/dist/services/sales-channel-location.js.map +1 -0
  266. package/dist/services/swap.d.ts +4 -4
  267. package/dist/services/swap.js +78 -127
  268. package/dist/services/swap.js.map +1 -1
  269. package/dist/services/totals.d.ts +2 -2
  270. package/dist/services/totals.js +2 -2
  271. package/dist/services/totals.js.map +1 -1
  272. package/dist/strategies/batch-jobs/product/import.d.ts +1 -1
  273. package/dist/strategies/batch-jobs/product/import.js +4 -3
  274. package/dist/strategies/batch-jobs/product/import.js.map +1 -1
  275. package/dist/strategies/cart-completion.d.ts +14 -9
  276. package/dist/strategies/cart-completion.js +111 -48
  277. package/dist/strategies/cart-completion.js.map +1 -1
  278. package/dist/subscribers/cart.d.ts +2 -4
  279. package/dist/subscribers/cart.js +8 -21
  280. package/dist/subscribers/cart.js.map +1 -1
  281. package/dist/subscribers/product.d.ts +1 -5
  282. package/dist/subscribers/product.js +15 -5
  283. package/dist/subscribers/product.js.map +1 -1
  284. package/dist/types/inventory.d.ts +106 -0
  285. package/dist/types/inventory.js +3 -0
  286. package/dist/types/inventory.js.map +1 -0
  287. package/dist/types/payment.d.ts +1 -0
  288. package/dist/types/product.d.ts +1 -0
  289. package/dist/types/product.js.map +1 -1
  290. package/dist/types/stock-location.d.ts +245 -0
  291. package/dist/types/stock-location.js +3 -0
  292. package/dist/types/stock-location.js.map +1 -0
  293. package/dist/utils/index.d.ts +1 -0
  294. package/dist/utils/index.js +1 -0
  295. package/dist/utils/index.js.map +1 -1
  296. package/dist/utils/repository.d.ts +4 -3
  297. package/dist/utils/repository.js +43 -23
  298. package/dist/utils/repository.js.map +1 -1
  299. package/package.json +2 -2
  300. package/dist/services/inventory.d.ts +0 -31
  301. package/dist/services/inventory.js +0 -151
  302. package/dist/services/inventory.js.map +0 -1
@@ -116,7 +116,7 @@ var is_email_1 = require("../utils/is-email");
116
116
  var CartService = /** @class */ (function (_super) {
117
117
  __extends(CartService, _super);
118
118
  function CartService(_a) {
119
- var manager = _a.manager, cartRepository = _a.cartRepository, shippingMethodRepository = _a.shippingMethodRepository, lineItemRepository = _a.lineItemRepository, eventBusService = _a.eventBusService, paymentProviderService = _a.paymentProviderService, productService = _a.productService, productVariantService = _a.productVariantService, taxProviderService = _a.taxProviderService, regionService = _a.regionService, lineItemService = _a.lineItemService, shippingOptionService = _a.shippingOptionService, customerService = _a.customerService, discountService = _a.discountService, giftCardService = _a.giftCardService, totalsService = _a.totalsService, newTotalsService = _a.newTotalsService, addressRepository = _a.addressRepository, paymentSessionRepository = _a.paymentSessionRepository, inventoryService = _a.inventoryService, customShippingOptionService = _a.customShippingOptionService, lineItemAdjustmentService = _a.lineItemAdjustmentService, priceSelectionStrategy = _a.priceSelectionStrategy, salesChannelService = _a.salesChannelService, featureFlagRouter = _a.featureFlagRouter, storeService = _a.storeService;
119
+ var manager = _a.manager, cartRepository = _a.cartRepository, shippingMethodRepository = _a.shippingMethodRepository, lineItemRepository = _a.lineItemRepository, eventBusService = _a.eventBusService, paymentProviderService = _a.paymentProviderService, productService = _a.productService, productVariantService = _a.productVariantService, taxProviderService = _a.taxProviderService, regionService = _a.regionService, lineItemService = _a.lineItemService, shippingOptionService = _a.shippingOptionService, customerService = _a.customerService, discountService = _a.discountService, giftCardService = _a.giftCardService, totalsService = _a.totalsService, newTotalsService = _a.newTotalsService, addressRepository = _a.addressRepository, paymentSessionRepository = _a.paymentSessionRepository, customShippingOptionService = _a.customShippingOptionService, lineItemAdjustmentService = _a.lineItemAdjustmentService, priceSelectionStrategy = _a.priceSelectionStrategy, salesChannelService = _a.salesChannelService, featureFlagRouter = _a.featureFlagRouter, storeService = _a.storeService, productVariantInventoryService = _a.productVariantInventoryService;
120
120
  var _this =
121
121
  // eslint-disable-next-line prefer-rest-params
122
122
  _super.call(this, arguments[0]) || this;
@@ -138,7 +138,6 @@ var CartService = /** @class */ (function (_super) {
138
138
  _this.newTotalsService_ = newTotalsService;
139
139
  _this.addressRepository_ = addressRepository;
140
140
  _this.paymentSessionRepository_ = paymentSessionRepository;
141
- _this.inventoryService_ = inventoryService;
142
141
  _this.customShippingOptionService_ = customShippingOptionService;
143
142
  _this.taxProviderService_ = taxProviderService;
144
143
  _this.lineItemAdjustmentService_ = lineItemAdjustmentService;
@@ -146,6 +145,7 @@ var CartService = /** @class */ (function (_super) {
146
145
  _this.salesChannelService_ = salesChannelService;
147
146
  _this.featureFlagRouter_ = featureFlagRouter;
148
147
  _this.storeService_ = storeService;
148
+ _this.productVariantInventoryService_ = productVariantInventoryService;
149
149
  return _this;
150
150
  }
151
151
  /**
@@ -568,7 +568,7 @@ var CartService = /** @class */ (function (_super) {
568
568
  return __generator(this, function (_d) {
569
569
  switch (_d.label) {
570
570
  case 0:
571
- if (!sales_channel_id) {
571
+ if (!sales_channel_id || !lineItem.variant_id) {
572
572
  return [2 /*return*/, true];
573
573
  }
574
574
  if (!((_b = lineItem.variant) === null || _b === void 0 ? void 0 : _b.product_id)) return [3 /*break*/, 1];
@@ -613,7 +613,7 @@ var CartService = /** @class */ (function (_super) {
613
613
  select.push("sales_channel_id");
614
614
  }
615
615
  return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
616
- var cart, lineItemServiceTx, currentItem, _a, existingItem, quantity;
616
+ var cart, lineItemIsValid, lineItemServiceTx, currentItem, _a, existingItem, quantity, isCovered;
617
617
  return __generator(this, function (_b) {
618
618
  switch (_b.label) {
619
619
  case 0: return [4 /*yield*/, this.retrieve(cartId, { select: select })];
@@ -621,9 +621,11 @@ var CartService = /** @class */ (function (_super) {
621
621
  cart = _b.sent();
622
622
  if (!this.featureFlagRouter_.isFeatureEnabled("sales_channels")) return [3 /*break*/, 3];
623
623
  if (!config.validateSalesChannels) return [3 /*break*/, 3];
624
+ if (!lineItem.variant_id) return [3 /*break*/, 3];
624
625
  return [4 /*yield*/, this.validateLineItem(cart, lineItem)];
625
626
  case 2:
626
- if (!(_b.sent())) {
627
+ lineItemIsValid = _b.sent();
628
+ if (!lineItemIsValid) {
627
629
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "The product \"".concat(lineItem.title, "\" must belongs to the sales channel on which the cart has been created."));
628
630
  }
629
631
  _b.label = 3;
@@ -646,25 +648,27 @@ var CartService = /** @class */ (function (_super) {
646
648
  quantity = currentItem
647
649
  ? (currentItem.quantity += lineItem.quantity)
648
650
  : lineItem.quantity;
649
- // Confirm inventory or throw error
650
- return [4 /*yield*/, this.inventoryService_
651
- .withTransaction(transactionManager)
652
- .confirmInventory(lineItem.variant_id, quantity)];
651
+ if (!lineItem.variant_id) return [3 /*break*/, 7];
652
+ return [4 /*yield*/, this.productVariantInventoryService_.confirmInventory(lineItem.variant_id, quantity, { salesChannelId: cart.sales_channel_id })];
653
653
  case 6:
654
- // Confirm inventory or throw error
655
- _b.sent();
656
- if (!currentItem) return [3 /*break*/, 8];
654
+ isCovered = _b.sent();
655
+ if (!isCovered) {
656
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Variant with id: ".concat(lineItem.variant_id, " does not have the required inventory"), medusa_core_utils_1.MedusaError.Codes.INSUFFICIENT_INVENTORY);
657
+ }
658
+ _b.label = 7;
659
+ case 7:
660
+ if (!currentItem) return [3 /*break*/, 9];
657
661
  return [4 /*yield*/, lineItemServiceTx.update(currentItem.id, {
658
662
  quantity: currentItem.quantity,
659
663
  })];
660
- case 7:
664
+ case 8:
661
665
  _b.sent();
662
- return [3 /*break*/, 10];
663
- case 8: return [4 /*yield*/, lineItemServiceTx.create(__assign(__assign({}, lineItem), { has_shipping: false, cart_id: cart.id }))];
664
- case 9:
666
+ return [3 /*break*/, 11];
667
+ case 9: return [4 /*yield*/, lineItemServiceTx.create(__assign(__assign({}, lineItem), { has_shipping: false, cart_id: cart.id }))];
668
+ case 10:
665
669
  _b.sent();
666
- _b.label = 10;
667
- case 10: return [4 /*yield*/, lineItemServiceTx
670
+ _b.label = 11;
671
+ case 11: return [4 /*yield*/, lineItemServiceTx
668
672
  .update({ cart_id: cartId, has_shipping: true }, { has_shipping: false })
669
673
  .catch(function (err) {
670
674
  // We only want to catch the errors related to not found items since we don't care if there is not item to update
@@ -673,20 +677,20 @@ var CartService = /** @class */ (function (_super) {
673
677
  }
674
678
  throw err;
675
679
  })];
676
- case 11:
680
+ case 12:
677
681
  _b.sent();
678
682
  return [4 /*yield*/, this.retrieve(cart.id, {
679
683
  relations: ["items", "discounts", "discounts.rule", "region"],
680
684
  })];
681
- case 12:
685
+ case 13:
682
686
  cart = _b.sent();
683
687
  return [4 /*yield*/, this.refreshAdjustments_(cart)];
684
- case 13:
688
+ case 14:
685
689
  _b.sent();
686
690
  return [4 /*yield*/, this.eventBus_
687
691
  .withTransaction(transactionManager)
688
692
  .emit(CartService.Events.UPDATED, { id: cart.id })];
689
- case 14:
693
+ case 15:
690
694
  _b.sent();
691
695
  return [2 /*return*/];
692
696
  }
@@ -721,7 +725,7 @@ var CartService = /** @class */ (function (_super) {
721
725
  select.push("sales_channel_id");
722
726
  }
723
727
  return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
724
- var cart, areValid, invalidProducts, lineItemServiceTx, inventoryServiceTx, existingItems, existingItemsVariantMap, lineItemsToCreate, lineItemsToUpdate, items_1, items_1_1, item, currentItem, existingItem, e_2_1, itemKeysToUpdate;
728
+ var cart, areValid, invalidProducts, lineItemServiceTx, productVariantInventoryServiceTx, existingItems, existingItemsVariantMap, lineItemsToCreate, lineItemsToUpdate, items_1, items_1_1, item, currentItem, existingItem, isSufficient, e_2_1, itemKeysToUpdate;
725
729
  var e_2, _a;
726
730
  var _this = this;
727
731
  return __generator(this, function (_b) {
@@ -734,8 +738,11 @@ var CartService = /** @class */ (function (_super) {
734
738
  return [4 /*yield*/, Promise.all(items.map(function (item) { return __awaiter(_this, void 0, void 0, function () {
735
739
  return __generator(this, function (_a) {
736
740
  switch (_a.label) {
737
- case 0: return [4 /*yield*/, this.validateLineItem(cart, item)];
741
+ case 0:
742
+ if (!item.variant_id) return [3 /*break*/, 2];
743
+ return [4 /*yield*/, this.validateLineItem(cart, item)];
738
744
  case 1: return [2 /*return*/, _a.sent()];
745
+ case 2: return [2 /*return*/, true];
739
746
  }
740
747
  });
741
748
  }); }))];
@@ -754,7 +761,7 @@ var CartService = /** @class */ (function (_super) {
754
761
  _b.label = 3;
755
762
  case 3:
756
763
  lineItemServiceTx = this.lineItemService_.withTransaction(transactionManager);
757
- inventoryServiceTx = this.inventoryService_.withTransaction(transactionManager);
764
+ productVariantInventoryServiceTx = this.productVariantInventoryService_.withTransaction(transactionManager);
758
765
  return [4 /*yield*/, lineItemServiceTx.list({
759
766
  cart_id: cart.id,
760
767
  variant_id: (0, typeorm_1.In)([items.map(function (item) { return item.variant_id; })]),
@@ -770,11 +777,11 @@ var CartService = /** @class */ (function (_super) {
770
777
  lineItemsToUpdate = [];
771
778
  _b.label = 5;
772
779
  case 5:
773
- _b.trys.push([5, 10, 11, 12]);
780
+ _b.trys.push([5, 11, 12, 13]);
774
781
  items_1 = __values(items), items_1_1 = items_1.next();
775
782
  _b.label = 6;
776
783
  case 6:
777
- if (!!items_1_1.done) return [3 /*break*/, 9];
784
+ if (!!items_1_1.done) return [3 /*break*/, 10];
778
785
  item = items_1_1.value;
779
786
  currentItem = void 0;
780
787
  existingItem = existingItemsVariantMap.get(item.variant_id);
@@ -788,9 +795,15 @@ var CartService = /** @class */ (function (_super) {
788
795
  item.quantity = currentItem
789
796
  ? (currentItem.quantity += item.quantity)
790
797
  : item.quantity;
791
- return [4 /*yield*/, inventoryServiceTx.confirmInventory(item.variant_id, item.quantity)];
798
+ if (!item.variant_id) return [3 /*break*/, 8];
799
+ return [4 /*yield*/, productVariantInventoryServiceTx.confirmInventory(item.variant_id, item.quantity, { salesChannelId: cart.sales_channel_id })];
792
800
  case 7:
793
- _b.sent();
801
+ isSufficient = _b.sent();
802
+ if (!isSufficient) {
803
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Variant with id: ".concat(item.variant_id, " does not have the required inventory"), medusa_core_utils_1.MedusaError.Codes.INSUFFICIENT_INVENTORY);
804
+ }
805
+ _b.label = 8;
806
+ case 8:
794
807
  if (currentItem) {
795
808
  lineItemsToUpdate[currentItem.id] = {
796
809
  quantity: item.quantity,
@@ -804,24 +817,24 @@ var CartService = /** @class */ (function (_super) {
804
817
  item.cart_id = cart.id;
805
818
  lineItemsToCreate.push(item);
806
819
  }
807
- _b.label = 8;
808
- case 8:
820
+ _b.label = 9;
821
+ case 9:
809
822
  items_1_1 = items_1.next();
810
823
  return [3 /*break*/, 6];
811
- case 9: return [3 /*break*/, 12];
812
- case 10:
824
+ case 10: return [3 /*break*/, 13];
825
+ case 11:
813
826
  e_2_1 = _b.sent();
814
827
  e_2 = { error: e_2_1 };
815
- return [3 /*break*/, 12];
816
- case 11:
828
+ return [3 /*break*/, 13];
829
+ case 12:
817
830
  try {
818
831
  if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
819
832
  }
820
833
  finally { if (e_2) throw e_2.error; }
821
834
  return [7 /*endfinally*/];
822
- case 12:
835
+ case 13:
823
836
  itemKeysToUpdate = Object.keys(lineItemsToUpdate);
824
- if (!itemKeysToUpdate.length) return [3 /*break*/, 14];
837
+ if (!itemKeysToUpdate.length) return [3 /*break*/, 15];
825
838
  return [4 /*yield*/, Promise.all(itemKeysToUpdate.map(function (id) { return __awaiter(_this, void 0, void 0, function () {
826
839
  return __generator(this, function (_a) {
827
840
  switch (_a.label) {
@@ -830,13 +843,13 @@ var CartService = /** @class */ (function (_super) {
830
843
  }
831
844
  });
832
845
  }); }))];
833
- case 13:
846
+ case 14:
834
847
  _b.sent();
835
- _b.label = 14;
836
- case 14:
848
+ _b.label = 15;
849
+ case 15:
837
850
  // Create all items that needs to be created
838
851
  return [4 /*yield*/, lineItemServiceTx.create(lineItemsToCreate)];
839
- case 15:
852
+ case 16:
840
853
  // Create all items that needs to be created
841
854
  _b.sent();
842
855
  return [4 /*yield*/, lineItemServiceTx
@@ -851,20 +864,20 @@ var CartService = /** @class */ (function (_super) {
851
864
  }
852
865
  throw err;
853
866
  })];
854
- case 16:
867
+ case 17:
855
868
  _b.sent();
856
869
  return [4 /*yield*/, this.retrieve(cart.id, {
857
870
  relations: ["items", "discounts", "discounts.rule", "region"],
858
871
  })];
859
- case 17:
872
+ case 18:
860
873
  cart = _b.sent();
861
874
  return [4 /*yield*/, this.refreshAdjustments_(cart)];
862
- case 18:
875
+ case 19:
863
876
  _b.sent();
864
877
  return [4 /*yield*/, this.eventBus_
865
878
  .withTransaction(transactionManager)
866
879
  .emit(CartService.Events.UPDATED, { id: cart.id })];
867
- case 19:
880
+ case 20:
868
881
  _b.sent();
869
882
  return [2 /*return*/];
870
883
  }
@@ -888,50 +901,54 @@ var CartService = /** @class */ (function (_super) {
888
901
  return __generator(this, function (_a) {
889
902
  switch (_a.label) {
890
903
  case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
891
- var cart, lineItemExists, hasInventory, updatedCart;
904
+ var lineItem, select, cart, hasInventory, updatedCart;
892
905
  return __generator(this, function (_a) {
893
906
  switch (_a.label) {
894
- case 0: return [4 /*yield*/, this.retrieve(cartId, {
895
- relations: ["items", "items.adjustments", "payment_sessions"],
896
- })
897
- // Ensure that the line item exists in the cart
898
- ];
907
+ case 0: return [4 /*yield*/, this.lineItemService_.retrieve(lineItemId, {
908
+ select: ["id", "quantity", "variant_id", "cart_id"],
909
+ })];
899
910
  case 1:
900
- cart = _a.sent();
901
- lineItemExists = cart.items.find(function (i) { return i.id === lineItemId; });
902
- if (!lineItemExists) {
911
+ lineItem = _a.sent();
912
+ if (lineItem.cart_id !== cartId) {
913
+ // Ensure that the line item exists in the cart
903
914
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "A line item with the provided id doesn't exist in the cart");
904
915
  }
905
- if (!lineItemUpdate.quantity) return [3 /*break*/, 3];
906
- return [4 /*yield*/, this.inventoryService_
907
- .withTransaction(transactionManager)
908
- .confirmInventory(lineItemExists.variant_id, lineItemUpdate.quantity)];
916
+ if (!lineItemUpdate.quantity) return [3 /*break*/, 4];
917
+ if (!lineItem.variant_id) return [3 /*break*/, 4];
918
+ select = ["id"];
919
+ if (this.featureFlagRouter_.isFeatureEnabled(sales_channels_1.default.key)) {
920
+ select.push("sales_channel_id");
921
+ }
922
+ return [4 /*yield*/, this.retrieve(cartId, { select: select })];
909
923
  case 2:
924
+ cart = _a.sent();
925
+ return [4 /*yield*/, this.productVariantInventoryService_.confirmInventory(lineItem.variant_id, lineItemUpdate.quantity, { salesChannelId: cart.sales_channel_id })];
926
+ case 3:
910
927
  hasInventory = _a.sent();
911
928
  if (!hasInventory) {
912
929
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Inventory doesn't cover the desired quantity");
913
930
  }
914
- _a.label = 3;
915
- case 3: return [4 /*yield*/, this.lineItemService_
931
+ _a.label = 4;
932
+ case 4: return [4 /*yield*/, this.lineItemService_
916
933
  .withTransaction(transactionManager)
917
934
  .update(lineItemId, lineItemUpdate)];
918
- case 4:
935
+ case 5:
919
936
  _a.sent();
920
937
  return [4 /*yield*/, this.retrieve(cartId, {
921
938
  relations: ["items", "discounts", "discounts.rule", "region"],
922
939
  })];
923
- case 5:
940
+ case 6:
924
941
  updatedCart = _a.sent();
925
942
  return [4 /*yield*/, this.refreshAdjustments_(updatedCart)
926
943
  // Update the line item
927
944
  ];
928
- case 6:
945
+ case 7:
929
946
  _a.sent();
930
947
  // Update the line item
931
948
  return [4 /*yield*/, this.eventBus_
932
949
  .withTransaction(transactionManager)
933
950
  .emit(CartService.Events.UPDATED, updatedCart)];
934
- case 7:
951
+ case 8:
935
952
  // Update the line item
936
953
  _a.sent();
937
954
  return [2 /*return*/, updatedCart];
@@ -1703,10 +1720,9 @@ var CartService = /** @class */ (function (_super) {
1703
1720
  });
1704
1721
  };
1705
1722
  /**
1706
- * Sets a payment method for a cart.
1723
+ * Selects a payment session for a cart and creates a payment object in the external provider system
1707
1724
  * @param cartId - the id of the cart to add payment method to
1708
1725
  * @param providerId - the id of the provider to be set to the cart
1709
- * @return result of update operation
1710
1726
  */
1711
1727
  CartService.prototype.setPaymentSession = function (cartId, providerId) {
1712
1728
  return __awaiter(this, void 0, void 0, function () {
@@ -1714,52 +1730,73 @@ var CartService = /** @class */ (function (_super) {
1714
1730
  return __generator(this, function (_a) {
1715
1731
  switch (_a.label) {
1716
1732
  case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
1717
- var psRepo, cart, paymentSession, updatedCart;
1718
- var _this = this;
1733
+ var psRepo, cart, isProviderPresent, cartPaymentSessionIds, paymentSession, sessionInput;
1719
1734
  return __generator(this, function (_a) {
1720
1735
  switch (_a.label) {
1721
1736
  case 0:
1722
1737
  psRepo = transactionManager.getCustomRepository(this.paymentSessionRepository_);
1723
1738
  return [4 /*yield*/, this.retrieveWithTotals(cartId, {
1724
- relations: ["region", "region.payment_providers", "payment_sessions"],
1725
- })
1726
- // The region must have the provider id in its providers array
1727
- ];
1739
+ relations: [
1740
+ "customer",
1741
+ "region",
1742
+ "region.payment_providers",
1743
+ "payment_sessions",
1744
+ ],
1745
+ })];
1728
1746
  case 1:
1729
1747
  cart = _a.sent();
1730
- // The region must have the provider id in its providers array
1731
- if (providerId !== "system" &&
1732
- !(cart.region.payment_providers.length &&
1733
- cart.region.payment_providers.find(function (_a) {
1734
- var id = _a.id;
1735
- return providerId === id;
1736
- }))) {
1748
+ isProviderPresent = cart.region.payment_providers.find(function (_a) {
1749
+ var id = _a.id;
1750
+ return providerId === id;
1751
+ });
1752
+ if (providerId !== "system" && !isProviderPresent) {
1737
1753
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "The payment method is not available in this region");
1738
1754
  }
1739
- return [4 /*yield*/, Promise.all(cart.payment_sessions.map(function (paymentSession) { return __awaiter(_this, void 0, void 0, function () {
1740
- return __generator(this, function (_a) {
1741
- return [2 /*return*/, psRepo.save(__assign(__assign({}, paymentSession), { is_selected: null }))];
1742
- });
1743
- }); }))];
1755
+ cartPaymentSessionIds = cart.payment_sessions.map(function (p) { return p.id; });
1756
+ return [4 /*yield*/, psRepo.update({ id: (0, typeorm_1.In)(cartPaymentSessionIds) }, {
1757
+ is_selected: null,
1758
+ })];
1744
1759
  case 2:
1745
1760
  _a.sent();
1746
1761
  paymentSession = cart.payment_sessions.find(function (ps) { return ps.provider_id === providerId; });
1747
1762
  if (!paymentSession) {
1748
1763
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Could not find payment session");
1749
1764
  }
1750
- paymentSession.is_selected = true;
1751
- return [4 /*yield*/, psRepo.save(paymentSession)];
1765
+ sessionInput = {
1766
+ cart: cart,
1767
+ customer: cart.customer,
1768
+ amount: cart.total,
1769
+ currency_code: cart.region.currency_code,
1770
+ provider_id: providerId,
1771
+ payment_session_id: paymentSession.id,
1772
+ };
1773
+ if (!paymentSession.is_selected) return [3 /*break*/, 4];
1774
+ // update the session remotely
1775
+ return [4 /*yield*/, this.paymentProviderService_
1776
+ .withTransaction(transactionManager)
1777
+ .updateSession(paymentSession, sessionInput)];
1752
1778
  case 3:
1779
+ // update the session remotely
1753
1780
  _a.sent();
1754
- return [4 /*yield*/, this.retrieve(cartId)];
1781
+ _a.label = 4;
1755
1782
  case 4:
1756
- updatedCart = _a.sent();
1757
- return [4 /*yield*/, this.eventBus_
1783
+ if (!!paymentSession.is_initiated) return [3 /*break*/, 6];
1784
+ return [4 /*yield*/, this.paymentProviderService_
1758
1785
  .withTransaction(transactionManager)
1759
- .emit(CartService.Events.UPDATED, updatedCart)];
1786
+ .createSession(sessionInput)];
1760
1787
  case 5:
1788
+ // Create the session remotely
1789
+ paymentSession = _a.sent();
1790
+ _a.label = 6;
1791
+ case 6: return [4 /*yield*/, psRepo.update(paymentSession.id, { is_selected: true })];
1792
+ case 7:
1761
1793
  _a.sent();
1762
- return [2 /*return*/, updatedCart];
1794
+ return [4 /*yield*/, this.eventBus_
1795
+ .withTransaction(transactionManager)
1796
+ .emit(CartService.Events.UPDATED, { id: cartId })];
1797
+ case 8:
1798
+ _a.sent();
1799
+ return [2 /*return*/];
1763
1800
  }
1764
1801
  });
1765
1802
  }); }, "SERIALIZABLE")];
@@ -1783,13 +1820,13 @@ var CartService = /** @class */ (function (_super) {
1783
1820
  return __generator(this, function (_a) {
1784
1821
  switch (_a.label) {
1785
1822
  case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
1786
- var psRepo, cartId, cart, total, region, partialSessionInput, seen, paymentProvider, paymentSessionInput, paymentSession;
1823
+ var psRepo, paymentProviderServiceTx, cartId, cart, total, region, deleteSessionAppropriately, providerSet, alreadyConsumedProviderIds, partialSessionInput, partialPaymentSessionData, paymentProvider, paymentSessionInput, paymentSession;
1787
1824
  var _this = this;
1788
- var _a;
1789
- return __generator(this, function (_b) {
1790
- switch (_b.label) {
1825
+ return __generator(this, function (_a) {
1826
+ switch (_a.label) {
1791
1827
  case 0:
1792
1828
  psRepo = transactionManager.getCustomRepository(this.paymentSessionRepository_);
1829
+ paymentProviderServiceTx = this.paymentProviderService_.withTransaction(transactionManager);
1793
1830
  cartId = typeof cartOrCartId === "string" ? cartOrCartId : cartOrCartId.id;
1794
1831
  return [4 /*yield*/, this.retrieveWithTotals(cartId, {
1795
1832
  relations: [
@@ -1809,72 +1846,118 @@ var CartService = /** @class */ (function (_super) {
1809
1846
  ],
1810
1847
  }, { force_taxes: true })];
1811
1848
  case 1:
1812
- cart = _b.sent();
1849
+ cart = _a.sent();
1813
1850
  total = cart.total, region = cart.region;
1851
+ deleteSessionAppropriately = function (session) { return __awaiter(_this, void 0, void 0, function () {
1852
+ return __generator(this, function (_a) {
1853
+ if (session.is_selected || session.is_initiated) {
1854
+ return [2 /*return*/, paymentProviderServiceTx.deleteSession(session)];
1855
+ }
1856
+ return [2 /*return*/, psRepo.delete(session)];
1857
+ });
1858
+ }); };
1859
+ if (!(total <= 0)) return [3 /*break*/, 3];
1860
+ return [4 /*yield*/, Promise.all(cart.payment_sessions.map(function (session) { return __awaiter(_this, void 0, void 0, function () {
1861
+ return __generator(this, function (_a) {
1862
+ return [2 /*return*/, deleteSessionAppropriately(session)];
1863
+ });
1864
+ }); }))];
1865
+ case 2:
1866
+ _a.sent();
1867
+ return [2 /*return*/];
1868
+ case 3:
1869
+ providerSet = new Set(region.payment_providers.map(function (p) { return p.id; }));
1870
+ alreadyConsumedProviderIds = new Set();
1814
1871
  partialSessionInput = {
1815
1872
  cart: cart,
1816
1873
  customer: cart.customer,
1817
- amount: cart.total,
1874
+ amount: total,
1818
1875
  currency_code: cart.region.currency_code,
1819
1876
  };
1820
- seen = [];
1821
- if (!((_a = cart.payment_sessions) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 3];
1822
- return [4 /*yield*/, Promise.all(cart.payment_sessions.map(function (paymentSession) { return __awaiter(_this, void 0, void 0, function () {
1823
- var paymentSessionInput;
1877
+ partialPaymentSessionData = {
1878
+ cart_id: cartId,
1879
+ data: {},
1880
+ status: models_1.PaymentSessionStatus.PENDING,
1881
+ amount: total,
1882
+ };
1883
+ return [4 /*yield*/, Promise.all(cart.payment_sessions.map(function (session) { return __awaiter(_this, void 0, void 0, function () {
1884
+ var paymentSessionInput, updatedSession;
1824
1885
  return __generator(this, function (_a) {
1825
- if (total <= 0 ||
1826
- !region.payment_providers.find(function (_a) {
1827
- var id = _a.id;
1828
- return id === paymentSession.provider_id;
1829
- })) {
1830
- return [2 /*return*/, this.paymentProviderService_
1831
- .withTransaction(transactionManager)
1832
- .deleteSession(paymentSession)];
1886
+ switch (_a.label) {
1887
+ case 0:
1888
+ if (!!providerSet.has(session.provider_id)) return [3 /*break*/, 2];
1889
+ return [4 /*yield*/, deleteSessionAppropriately(session)];
1890
+ case 1:
1891
+ /**
1892
+ * if the provider does not belong to the region then delete the session.
1893
+ * The deletion occurs locally if there is no external data or if it is not selected
1894
+ * otherwise the deletion will also occur remotely through the external provider.
1895
+ */
1896
+ return [2 /*return*/, _a.sent()];
1897
+ case 2:
1898
+ /**
1899
+ * if the provider belongs to the region then update or delete the session.
1900
+ * The update occurs locally if it is not selected
1901
+ * otherwise the update will also occur remotely through the external provider.
1902
+ * In case the session is not selected but contains an external provider data, we delete the external provider
1903
+ * session to be in a clean state.
1904
+ */
1905
+ // We are saving the provider id on which the work below will be done. That way,
1906
+ // when handling the providers from the cart region at a later point below, we do not double the work on the sessions that already
1907
+ // exists for the same provider.
1908
+ alreadyConsumedProviderIds.add(session.provider_id);
1909
+ // Update remotely
1910
+ if (session.is_selected) {
1911
+ paymentSessionInput = __assign(__assign({}, partialSessionInput), { provider_id: session.provider_id });
1912
+ return [2 /*return*/, paymentProviderServiceTx.updateSession(session, paymentSessionInput)];
1913
+ }
1914
+ if (!session.is_initiated) return [3 /*break*/, 4];
1915
+ return [4 /*yield*/, paymentProviderServiceTx.deleteSession(session)];
1916
+ case 3:
1917
+ _a.sent();
1918
+ updatedSession = psRepo.create(__assign(__assign({}, partialPaymentSessionData), { provider_id: session.provider_id }));
1919
+ return [3 /*break*/, 5];
1920
+ case 4:
1921
+ updatedSession = __assign(__assign({}, session), { amount: total });
1922
+ _a.label = 5;
1923
+ case 5: return [2 /*return*/, psRepo.save(updatedSession)];
1833
1924
  }
1834
- else {
1835
- seen.push(paymentSession.provider_id);
1836
- paymentSessionInput = __assign(__assign({}, partialSessionInput), { provider_id: paymentSession.provider_id });
1837
- return [2 /*return*/, this.paymentProviderService_
1838
- .withTransaction(transactionManager)
1839
- .updateSession(paymentSession, paymentSessionInput)];
1840
- }
1841
- return [2 /*return*/];
1842
1925
  });
1843
- }); }))];
1844
- case 2:
1845
- _b.sent();
1846
- _b.label = 3;
1847
- case 3:
1848
- if (!(total > 0)) return [3 /*break*/, 8];
1849
- if (!(region.payment_providers.length === 1 && !cart.payment_session)) return [3 /*break*/, 6];
1926
+ }); }))
1927
+ /**
1928
+ * From now on, the sessions have been cleanup. We can now
1929
+ * - Set the provider session as selected if it is the only one existing and there is no payment session on the cart
1930
+ * - Create a session per provider locally if it does not already exists on the cart as per the previous step
1931
+ */
1932
+ // If only one provider exists and there is no session on the cart, create the session and select it.
1933
+ ];
1934
+ case 4:
1935
+ _a.sent();
1936
+ if (!(region.payment_providers.length === 1 && !cart.payment_session)) return [3 /*break*/, 7];
1850
1937
  paymentProvider = region.payment_providers[0];
1851
1938
  paymentSessionInput = __assign(__assign({}, partialSessionInput), { provider_id: paymentProvider.id });
1852
1939
  return [4 /*yield*/, this.paymentProviderService_
1853
1940
  .withTransaction(transactionManager)
1854
1941
  .createSession(paymentSessionInput)];
1855
- case 4:
1856
- paymentSession = _b.sent();
1857
- paymentSession.is_selected = true;
1858
- return [4 /*yield*/, psRepo.save(paymentSession)];
1859
1942
  case 5:
1860
- _b.sent();
1861
- return [3 /*break*/, 8];
1862
- case 6: return [4 /*yield*/, Promise.all(region.payment_providers.map(function (paymentProvider) { return __awaiter(_this, void 0, void 0, function () {
1863
- var paymentSessionInput;
1943
+ paymentSession = _a.sent();
1944
+ return [4 /*yield*/, psRepo.update(paymentSession.id, { is_selected: true })];
1945
+ case 6:
1946
+ _a.sent();
1947
+ return [2 /*return*/];
1948
+ case 7: return [4 /*yield*/, Promise.all(region.payment_providers.map(function (paymentProvider) { return __awaiter(_this, void 0, void 0, function () {
1949
+ var paymentSession;
1864
1950
  return __generator(this, function (_a) {
1865
- if (!seen.includes(paymentProvider.id)) {
1866
- paymentSessionInput = __assign(__assign({}, partialSessionInput), { provider_id: paymentProvider.id });
1867
- return [2 /*return*/, this.paymentProviderService_
1868
- .withTransaction(transactionManager)
1869
- .createSession(paymentSessionInput)];
1951
+ if (alreadyConsumedProviderIds.has(paymentProvider.id)) {
1952
+ return [2 /*return*/];
1870
1953
  }
1871
- return [2 /*return*/];
1954
+ paymentSession = psRepo.create(__assign(__assign({}, partialPaymentSessionData), { provider_id: paymentProvider.id }));
1955
+ return [2 /*return*/, psRepo.save(paymentSession)];
1872
1956
  });
1873
1957
  }); }))];
1874
- case 7:
1875
- _b.sent();
1876
- _b.label = 8;
1877
- case 8: return [2 /*return*/];
1958
+ case 8:
1959
+ _a.sent();
1960
+ return [2 /*return*/];
1878
1961
  }
1879
1962
  });
1880
1963
  }); })];
@@ -1896,7 +1979,7 @@ var CartService = /** @class */ (function (_super) {
1896
1979
  return __generator(this, function (_a) {
1897
1980
  switch (_a.label) {
1898
1981
  case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
1899
- var cart, cartRepo, paymentSession;
1982
+ var cart, cartRepo, paymentSession, psRepo;
1900
1983
  return __generator(this, function (_a) {
1901
1984
  switch (_a.label) {
1902
1985
  case 0: return [4 /*yield*/, this.retrieve(cartId, {
@@ -1905,7 +1988,7 @@ var CartService = /** @class */ (function (_super) {
1905
1988
  case 1:
1906
1989
  cart = _a.sent();
1907
1990
  cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
1908
- if (!cart.payment_sessions) return [3 /*break*/, 3];
1991
+ if (!cart.payment_sessions) return [3 /*break*/, 5];
1909
1992
  paymentSession = cart.payment_sessions.find(function (_a) {
1910
1993
  var provider_id = _a.provider_id;
1911
1994
  return provider_id === providerId;
@@ -1914,24 +1997,28 @@ var CartService = /** @class */ (function (_super) {
1914
1997
  var provider_id = _a.provider_id;
1915
1998
  return provider_id !== providerId;
1916
1999
  });
1917
- if (!paymentSession) return [3 /*break*/, 3];
1918
- // Delete the session with the provider
2000
+ psRepo = transactionManager.getCustomRepository(this.paymentSessionRepository_);
2001
+ if (!paymentSession) return [3 /*break*/, 5];
2002
+ if (!(paymentSession.is_selected || paymentSession.is_initiated)) return [3 /*break*/, 3];
1919
2003
  return [4 /*yield*/, this.paymentProviderService_
1920
2004
  .withTransaction(transactionManager)
1921
2005
  .deleteSession(paymentSession)];
1922
2006
  case 2:
1923
- // Delete the session with the provider
1924
2007
  _a.sent();
1925
- _a.label = 3;
1926
- case 3: return [4 /*yield*/, cartRepo.save(cart)];
2008
+ return [3 /*break*/, 5];
2009
+ case 3: return [4 /*yield*/, psRepo.delete(paymentSession)];
1927
2010
  case 4:
2011
+ _a.sent();
2012
+ _a.label = 5;
2013
+ case 5: return [4 /*yield*/, cartRepo.save(cart)];
2014
+ case 6:
1928
2015
  _a.sent();
1929
2016
  return [4 /*yield*/, this.eventBus_
1930
2017
  .withTransaction(transactionManager)
1931
- .emit(CartService.Events.UPDATED, cart)];
1932
- case 5:
2018
+ .emit(CartService.Events.UPDATED, { id: cart.id })];
2019
+ case 7:
1933
2020
  _a.sent();
1934
- return [2 /*return*/, cart];
2021
+ return [2 /*return*/];
1935
2022
  }
1936
2023
  });
1937
2024
  }); })];
@@ -1945,7 +2032,7 @@ var CartService = /** @class */ (function (_super) {
1945
2032
  * @param cartId - the id of the cart to remove from
1946
2033
  * @param providerId - the id of the provider whoose payment session
1947
2034
  * should be removed.
1948
- * @return {Promise<Cart>} the resulting cart.
2035
+ * @return {Promise<void>} the resulting cart.
1949
2036
  */
1950
2037
  CartService.prototype.refreshPaymentSession = function (cartId, providerId) {
1951
2038
  return __awaiter(this, void 0, void 0, function () {
@@ -1953,7 +2040,7 @@ var CartService = /** @class */ (function (_super) {
1953
2040
  return __generator(this, function (_a) {
1954
2041
  switch (_a.label) {
1955
2042
  case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
1956
- var cart, paymentSession, updatedCart;
2043
+ var cart, paymentSession, psRepo;
1957
2044
  return __generator(this, function (_a) {
1958
2045
  switch (_a.label) {
1959
2046
  case 0: return [4 /*yield*/, this.retrieveWithTotals(cartId, {
@@ -1961,13 +2048,13 @@ var CartService = /** @class */ (function (_super) {
1961
2048
  })];
1962
2049
  case 1:
1963
2050
  cart = _a.sent();
1964
- if (!cart.payment_sessions) return [3 /*break*/, 3];
2051
+ if (!cart.payment_sessions) return [3 /*break*/, 5];
1965
2052
  paymentSession = cart.payment_sessions.find(function (_a) {
1966
2053
  var provider_id = _a.provider_id;
1967
2054
  return provider_id === providerId;
1968
2055
  });
1969
- if (!paymentSession) return [3 /*break*/, 3];
1970
- // Delete the session with the provider
2056
+ if (!paymentSession) return [3 /*break*/, 5];
2057
+ if (!paymentSession.is_selected) return [3 /*break*/, 3];
1971
2058
  return [4 /*yield*/, this.paymentProviderService_
1972
2059
  .withTransaction(transactionManager)
1973
2060
  .refreshSession(paymentSession, {
@@ -1978,18 +2065,22 @@ var CartService = /** @class */ (function (_super) {
1978
2065
  provider_id: providerId,
1979
2066
  })];
1980
2067
  case 2:
1981
- // Delete the session with the provider
1982
2068
  _a.sent();
1983
- _a.label = 3;
1984
- case 3: return [4 /*yield*/, this.retrieve(cartId)];
2069
+ return [3 /*break*/, 5];
2070
+ case 3:
2071
+ psRepo = transactionManager.getCustomRepository(this.paymentSessionRepository_);
2072
+ return [4 /*yield*/, psRepo.update(paymentSession.id, {
2073
+ amount: cart.total,
2074
+ })];
1985
2075
  case 4:
1986
- updatedCart = _a.sent();
1987
- return [4 /*yield*/, this.eventBus_
1988
- .withTransaction(transactionManager)
1989
- .emit(CartService.Events.UPDATED, updatedCart)];
1990
- case 5:
1991
2076
  _a.sent();
1992
- return [2 /*return*/, updatedCart];
2077
+ _a.label = 5;
2078
+ case 5: return [4 /*yield*/, this.eventBus_
2079
+ .withTransaction(transactionManager)
2080
+ .emit(CartService.Events.UPDATED, { id: cartId })];
2081
+ case 6:
2082
+ _a.sent();
2083
+ return [2 /*return*/];
1993
2084
  }
1994
2085
  });
1995
2086
  }); })];
@@ -2166,28 +2257,31 @@ var CartService = /** @class */ (function (_super) {
2166
2257
  var availablePrice;
2167
2258
  return __generator(this, function (_a) {
2168
2259
  switch (_a.label) {
2169
- case 0: return [4 /*yield*/, this.priceSelectionStrategy_
2170
- .withTransaction(transactionManager)
2171
- .calculateVariantPrice(item.variant_id, {
2172
- region_id: region_1.id,
2173
- currency_code: region_1.currency_code,
2174
- quantity: item.quantity,
2175
- customer_id: customer_id || cart.customer_id,
2176
- include_discount_prices: true,
2177
- })
2178
- .catch(function () { return undefined; })];
2260
+ case 0:
2261
+ if (!item.variant_id) {
2262
+ return [2 /*return*/, item];
2263
+ }
2264
+ return [4 /*yield*/, this.priceSelectionStrategy_
2265
+ .withTransaction(transactionManager)
2266
+ .calculateVariantPrice(item.variant_id, {
2267
+ region_id: region_1.id,
2268
+ currency_code: region_1.currency_code,
2269
+ quantity: item.quantity,
2270
+ customer_id: customer_id || cart.customer_id,
2271
+ include_discount_prices: true,
2272
+ })
2273
+ .catch(function () { return undefined; })];
2179
2274
  case 1:
2180
2275
  availablePrice = _a.sent();
2181
2276
  if (!(availablePrice !== undefined &&
2182
- availablePrice.calculatedPrice !== null)) return [3 /*break*/, 2];
2183
- return [2 /*return*/, lineItemServiceTx_2.update(item.id, {
2277
+ availablePrice.calculatedPrice !== null)) return [3 /*break*/, 3];
2278
+ return [4 /*yield*/, lineItemServiceTx_2.update(item.id, {
2184
2279
  has_shipping: false,
2185
2280
  unit_price: availablePrice.calculatedPrice,
2186
2281
  })];
2187
- case 2: return [4 /*yield*/, lineItemServiceTx_2.delete(item.id)];
2188
- case 3:
2189
- _a.sent();
2190
- return [2 /*return*/];
2282
+ case 2: return [2 /*return*/, _a.sent()];
2283
+ case 3: return [4 /*yield*/, lineItemServiceTx_2.delete(item.id)];
2284
+ case 4: return [2 /*return*/, _a.sent()];
2191
2285
  }
2192
2286
  });
2193
2287
  }); }))];