@medusajs/medusa 1.2.2-dev.56 → 1.3.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 (368) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/dist/api/index.d.ts +1 -0
  3. package/dist/api/index.js +1 -0
  4. package/dist/api/index.js.map +1 -1
  5. package/dist/api/middlewares/error-handler.js +1 -1
  6. package/dist/api/routes/admin/auth/create-session.js +5 -5
  7. package/dist/api/routes/admin/auth/create-session.js.map +1 -1
  8. package/dist/api/routes/admin/customer-groups/list-customer-groups.js +2 -2
  9. package/dist/api/routes/admin/customer-groups/list-customer-groups.js.map +1 -1
  10. package/dist/api/routes/admin/discounts/create-discount.d.ts +11 -2
  11. package/dist/api/routes/admin/discounts/create-discount.js +66 -12
  12. package/dist/api/routes/admin/discounts/create-discount.js.map +1 -1
  13. package/dist/api/routes/admin/discounts/create-dynamic-code.d.ts +1 -0
  14. package/dist/api/routes/admin/discounts/create-dynamic-code.js +4 -1
  15. package/dist/api/routes/admin/discounts/create-dynamic-code.js.map +1 -1
  16. package/dist/api/routes/admin/discounts/delete-dynamic-code.js +3 -1
  17. package/dist/api/routes/admin/discounts/delete-dynamic-code.js.map +1 -1
  18. package/dist/api/routes/admin/discounts/get-discount.d.ts +7 -0
  19. package/dist/api/routes/admin/discounts/get-discount.js +43 -9
  20. package/dist/api/routes/admin/discounts/get-discount.js.map +1 -1
  21. package/dist/api/routes/admin/discounts/index.d.ts +1 -3
  22. package/dist/api/routes/admin/discounts/index.js +1 -6
  23. package/dist/api/routes/admin/discounts/index.js.map +1 -1
  24. package/dist/api/routes/admin/discounts/list-discounts.d.ts +2 -6
  25. package/dist/api/routes/admin/discounts/list-discounts.js +15 -28
  26. package/dist/api/routes/admin/discounts/list-discounts.js.map +1 -1
  27. package/dist/api/routes/admin/discounts/update-discount.d.ts +12 -2
  28. package/dist/api/routes/admin/discounts/update-discount.js +70 -15
  29. package/dist/api/routes/admin/discounts/update-discount.js.map +1 -1
  30. package/dist/api/routes/admin/draft-orders/create-draft-order.d.ts +3 -3
  31. package/dist/api/routes/admin/draft-orders/create-draft-order.js.map +1 -1
  32. package/dist/api/routes/admin/draft-orders/create-line-item.d.ts +1 -1
  33. package/dist/api/routes/admin/draft-orders/create-line-item.js.map +1 -1
  34. package/dist/api/routes/admin/draft-orders/index.js +7 -1
  35. package/dist/api/routes/admin/draft-orders/index.js.map +1 -1
  36. package/dist/api/routes/admin/draft-orders/register-payment.js +0 -1
  37. package/dist/api/routes/admin/draft-orders/register-payment.js.map +1 -1
  38. package/dist/api/routes/admin/draft-orders/update-line-item.d.ts +1 -1
  39. package/dist/api/routes/admin/draft-orders/update-line-item.js.map +1 -1
  40. package/dist/api/routes/admin/notifications/index.js.map +1 -1
  41. package/dist/api/routes/admin/notifications/list-notifications.js.map +1 -1
  42. package/dist/api/routes/admin/notifications/resend-notification.js.map +1 -1
  43. package/dist/api/routes/admin/orders/add-shipping-method.d.ts +1 -1
  44. package/dist/api/routes/admin/orders/add-shipping-method.js.map +1 -1
  45. package/dist/api/routes/admin/orders/create-order.js.map +1 -1
  46. package/dist/api/routes/admin/orders/create-swap.js +7 -3
  47. package/dist/api/routes/admin/orders/create-swap.js.map +1 -1
  48. package/dist/api/routes/admin/orders/index.d.ts +1 -1
  49. package/dist/api/routes/admin/orders/index.js +1 -3
  50. package/dist/api/routes/admin/orders/index.js.map +1 -1
  51. package/dist/api/routes/admin/orders/list-orders.js.map +1 -1
  52. package/dist/api/routes/admin/orders/update-order.js.map +1 -1
  53. package/dist/api/routes/admin/price-lists/index.d.ts +7 -0
  54. package/dist/api/routes/admin/price-lists/index.js +3 -0
  55. package/dist/api/routes/admin/price-lists/index.js.map +1 -1
  56. package/dist/api/routes/admin/price-lists/list-price-list-products.d.ts +74 -0
  57. package/dist/api/routes/admin/price-lists/list-price-list-products.js +246 -0
  58. package/dist/api/routes/admin/price-lists/list-price-list-products.js.map +1 -0
  59. package/dist/api/routes/admin/price-lists/update-price-list.d.ts +2 -2
  60. package/dist/api/routes/admin/price-lists/update-price-list.js.map +1 -1
  61. package/dist/api/routes/admin/products/list-products.d.ts +2 -1
  62. package/dist/api/routes/admin/products/list-products.js +31 -65
  63. package/dist/api/routes/admin/products/list-products.js.map +1 -1
  64. package/dist/api/routes/admin/products/update-variant.js +11 -7
  65. package/dist/api/routes/admin/products/update-variant.js.map +1 -1
  66. package/dist/api/routes/admin/regions/index.d.ts +1 -1
  67. package/dist/api/routes/admin/store/get-store.js.map +1 -1
  68. package/dist/api/routes/admin/swaps/index.js +3 -0
  69. package/dist/api/routes/admin/swaps/index.js.map +1 -1
  70. package/dist/api/routes/admin/tax-rates/list-tax-rates.js.map +1 -1
  71. package/dist/api/routes/admin/tax-rates/utils/get-query-config.d.ts +1 -1
  72. package/dist/api/routes/admin/tax-rates/utils/get-query-config.js +4 -2
  73. package/dist/api/routes/admin/tax-rates/utils/get-query-config.js.map +1 -1
  74. package/dist/api/routes/admin/uploads/index.d.ts +1 -1
  75. package/dist/api/routes/store/auth/create-session.js +4 -5
  76. package/dist/api/routes/store/auth/create-session.js.map +1 -1
  77. package/dist/api/routes/store/carts/create-cart.js +8 -5
  78. package/dist/api/routes/store/carts/create-cart.js.map +1 -1
  79. package/dist/api/routes/store/carts/create-line-item.d.ts +1 -1
  80. package/dist/api/routes/store/carts/create-line-item.js +9 -6
  81. package/dist/api/routes/store/carts/create-line-item.js.map +1 -1
  82. package/dist/api/routes/store/carts/index.d.ts +0 -1
  83. package/dist/api/routes/store/carts/index.js +1 -3
  84. package/dist/api/routes/store/carts/index.js.map +1 -1
  85. package/dist/api/routes/store/carts/update-cart.js +7 -7
  86. package/dist/api/routes/store/carts/update-cart.js.map +1 -1
  87. package/dist/api/routes/store/customers/create-customer.js +3 -4
  88. package/dist/api/routes/store/customers/create-customer.js.map +1 -1
  89. package/dist/api/routes/store/orders/index.js +0 -2
  90. package/dist/api/routes/store/orders/index.js.map +1 -1
  91. package/dist/api/routes/store/products/get-product.js +17 -5
  92. package/dist/api/routes/store/products/get-product.js.map +1 -1
  93. package/dist/api/routes/store/products/index.d.ts +2 -2
  94. package/dist/api/routes/store/products/list-products.d.ts +2 -1
  95. package/dist/api/routes/store/products/list-products.js +21 -8
  96. package/dist/api/routes/store/products/list-products.js.map +1 -1
  97. package/dist/api/routes/store/products/search.d.ts +1 -1
  98. package/dist/api/routes/store/products/search.js.map +1 -1
  99. package/dist/api/routes/store/shipping-options/list-options.js.map +1 -1
  100. package/dist/api/routes/store/swaps/create-swap.js +7 -3
  101. package/dist/api/routes/store/swaps/create-swap.js.map +1 -1
  102. package/dist/api/routes/store/variants/get-variant.js +17 -5
  103. package/dist/api/routes/store/variants/get-variant.js.map +1 -1
  104. package/dist/api/routes/store/variants/list-variants.d.ts +2 -1
  105. package/dist/api/routes/store/variants/list-variants.js +38 -9
  106. package/dist/api/routes/store/variants/list-variants.js.map +1 -1
  107. package/dist/commands/develop.js +1 -1
  108. package/dist/commands/develop.js.map +1 -1
  109. package/dist/controllers/customers/index.d.ts +1 -1
  110. package/dist/controllers/customers/list-customers.d.ts +1 -1
  111. package/dist/controllers/customers/list-customers.js +34 -37
  112. package/dist/controllers/customers/list-customers.js.map +1 -1
  113. package/dist/controllers/products/admin-list-products.d.ts +16 -0
  114. package/dist/controllers/products/admin-list-products.js +103 -0
  115. package/dist/controllers/products/admin-list-products.js.map +1 -0
  116. package/dist/helpers/test-request.js +15 -7
  117. package/dist/helpers/test-request.js.map +1 -1
  118. package/dist/index.d.ts +3 -59
  119. package/dist/index.js +3 -62
  120. package/dist/index.js.map +1 -1
  121. package/dist/interfaces/base-service.d.ts +66 -0
  122. package/dist/interfaces/base-service.js +378 -0
  123. package/dist/interfaces/base-service.js.map +1 -0
  124. package/dist/interfaces/index.d.ts +1 -0
  125. package/dist/interfaces/index.js +1 -0
  126. package/dist/interfaces/index.js.map +1 -1
  127. package/dist/interfaces/price-selection-strategy.d.ts +50 -0
  128. package/dist/interfaces/price-selection-strategy.js +34 -0
  129. package/dist/interfaces/price-selection-strategy.js.map +1 -0
  130. package/dist/interfaces/tax-service.d.ts +6 -0
  131. package/dist/interfaces/tax-service.js +31 -0
  132. package/dist/interfaces/tax-service.js.map +1 -1
  133. package/dist/loaders/api.d.ts +9 -5
  134. package/dist/loaders/api.js +2 -6
  135. package/dist/loaders/api.js.map +1 -1
  136. package/dist/loaders/config.d.ts +3 -0
  137. package/dist/loaders/config.js +47 -0
  138. package/dist/loaders/config.js.map +1 -0
  139. package/dist/loaders/database.d.ts +8 -4
  140. package/dist/loaders/database.js.map +1 -1
  141. package/dist/loaders/defaults.d.ts +4 -3
  142. package/dist/loaders/defaults.js +39 -31
  143. package/dist/loaders/defaults.js.map +1 -1
  144. package/dist/loaders/express.d.ts +7 -4
  145. package/dist/loaders/express.js +7 -5
  146. package/dist/loaders/express.js.map +1 -1
  147. package/dist/loaders/index.d.ts +13 -8
  148. package/dist/loaders/index.js +29 -54
  149. package/dist/loaders/index.js.map +1 -1
  150. package/dist/loaders/logger.d.ts +1 -0
  151. package/dist/loaders/logger.js.map +1 -1
  152. package/dist/loaders/models.d.ts +8 -3
  153. package/dist/loaders/models.js +19 -17
  154. package/dist/loaders/models.js.map +1 -1
  155. package/dist/loaders/passport.d.ts +7 -4
  156. package/dist/loaders/passport.js +23 -21
  157. package/dist/loaders/passport.js.map +1 -1
  158. package/dist/loaders/plugins.d.ts +18 -10
  159. package/dist/loaders/plugins.js +27 -24
  160. package/dist/loaders/plugins.js.map +1 -1
  161. package/dist/loaders/redis.d.ts +8 -5
  162. package/dist/loaders/redis.js +3 -3
  163. package/dist/loaders/redis.js.map +1 -1
  164. package/dist/loaders/repositories.d.ts +7 -3
  165. package/dist/loaders/repositories.js +1 -1
  166. package/dist/loaders/repositories.js.map +1 -1
  167. package/dist/loaders/search-index.d.ts +4 -3
  168. package/dist/loaders/search-index.js.map +1 -1
  169. package/dist/loaders/services.d.ts +10 -5
  170. package/dist/loaders/services.js.map +1 -1
  171. package/dist/loaders/strategies.d.ts +1 -1
  172. package/dist/loaders/subscribers.d.ts +7 -3
  173. package/dist/loaders/subscribers.js.map +1 -1
  174. package/dist/migrations/1646324713514-discount_conditions.d.ts +6 -0
  175. package/dist/migrations/1646324713514-discount_conditions.js +335 -0
  176. package/dist/migrations/1646324713514-discount_conditions.js.map +1 -0
  177. package/dist/migrations/1648600574750-add_line_item_adjustments.d.ts +6 -0
  178. package/dist/migrations/1648600574750-add_line_item_adjustments.js +101 -0
  179. package/dist/migrations/1648600574750-add_line_item_adjustments.js.map +1 -0
  180. package/dist/migrations/1648641130007-tax_line_constraints.d.ts +6 -0
  181. package/dist/{api/routes/admin/discounts/remove-valid-product.js → migrations/1648641130007-tax_line_constraints.js} +39 -44
  182. package/dist/migrations/1648641130007-tax_line_constraints.js.map +1 -0
  183. package/dist/migrations/1649775522087-add_batch_job_model.d.ts +6 -0
  184. package/dist/{api/routes/admin/discounts/add-valid-product.js → migrations/1649775522087-add_batch_job_model.js} +39 -44
  185. package/dist/migrations/1649775522087-add_batch_job_model.js.map +1 -0
  186. package/dist/models/batch-job.d.ts +58 -0
  187. package/dist/models/batch-job.js +119 -0
  188. package/dist/models/batch-job.js.map +1 -0
  189. package/dist/models/country.d.ts +1 -1
  190. package/dist/models/country.js +1 -1
  191. package/dist/models/country.js.map +1 -1
  192. package/dist/models/customer-group.js +3 -1
  193. package/dist/models/customer-group.js.map +1 -1
  194. package/dist/models/discount-condition-customer-group.d.ts +39 -0
  195. package/dist/models/discount-condition-customer-group.js +84 -0
  196. package/dist/models/discount-condition-customer-group.js.map +1 -0
  197. package/dist/models/discount-condition-product-collection.d.ts +39 -0
  198. package/dist/models/discount-condition-product-collection.js +84 -0
  199. package/dist/models/discount-condition-product-collection.js.map +1 -0
  200. package/dist/models/discount-condition-product-tag.d.ts +39 -0
  201. package/dist/models/discount-condition-product-tag.js +84 -0
  202. package/dist/models/discount-condition-product-tag.js.map +1 -0
  203. package/dist/models/discount-condition-product-type.d.ts +39 -0
  204. package/dist/models/discount-condition-product-type.js +84 -0
  205. package/dist/models/discount-condition-product-type.js.map +1 -0
  206. package/dist/models/discount-condition-product.d.ts +39 -0
  207. package/dist/models/discount-condition-product.js +84 -0
  208. package/dist/models/discount-condition-product.js.map +1 -0
  209. package/dist/models/discount-condition.d.ts +68 -0
  210. package/dist/models/discount-condition.js +208 -0
  211. package/dist/models/discount-condition.js.map +1 -0
  212. package/dist/models/discount-rule.d.ts +5 -5
  213. package/dist/models/discount-rule.js +6 -17
  214. package/dist/models/discount-rule.js.map +1 -1
  215. package/dist/models/discount.js.map +1 -1
  216. package/dist/models/index.d.ts +66 -0
  217. package/dist/models/index.js +79 -0
  218. package/dist/models/index.js.map +1 -0
  219. package/dist/models/line-item-adjustment.d.ts +13 -0
  220. package/dist/models/line-item-adjustment.js +79 -0
  221. package/dist/models/line-item-adjustment.js.map +1 -0
  222. package/dist/models/line-item-tax-line.js +2 -1
  223. package/dist/models/line-item-tax-line.js.map +1 -1
  224. package/dist/models/line-item.d.ts +2 -0
  225. package/dist/models/line-item.js +5 -0
  226. package/dist/models/line-item.js.map +1 -1
  227. package/dist/models/money-amount.js +4 -1
  228. package/dist/models/money-amount.js.map +1 -1
  229. package/dist/models/price-list.js +6 -2
  230. package/dist/models/price-list.js.map +1 -1
  231. package/dist/models/product-variant.js +9 -3
  232. package/dist/models/product-variant.js.map +1 -1
  233. package/dist/models/shipping-method-tax-line.js +2 -1
  234. package/dist/models/shipping-method-tax-line.js.map +1 -1
  235. package/dist/models/swap.d.ts +4 -4
  236. package/dist/models/swap.js +23 -23
  237. package/dist/models/swap.js.map +1 -1
  238. package/dist/repositories/cart.d.ts +2 -2
  239. package/dist/repositories/cart.js.map +1 -1
  240. package/dist/repositories/customer.d.ts +1 -1
  241. package/dist/repositories/customer.js +7 -9
  242. package/dist/repositories/customer.js.map +1 -1
  243. package/dist/repositories/discount-condition.d.ts +34 -0
  244. package/dist/repositories/discount-condition.js +384 -0
  245. package/dist/repositories/discount-condition.js.map +1 -0
  246. package/dist/repositories/line-item-adjustment.d.ts +4 -0
  247. package/dist/repositories/line-item-adjustment.js +38 -0
  248. package/dist/repositories/line-item-adjustment.js.map +1 -0
  249. package/dist/repositories/line-item-tax-line.d.ts +2 -0
  250. package/dist/repositories/line-item-tax-line.js +80 -0
  251. package/dist/repositories/line-item-tax-line.js.map +1 -1
  252. package/dist/repositories/line-item.js +1 -0
  253. package/dist/repositories/line-item.js.map +1 -1
  254. package/dist/repositories/money-amount.d.ts +1 -0
  255. package/dist/repositories/money-amount.js +49 -2
  256. package/dist/repositories/money-amount.js.map +1 -1
  257. package/dist/repositories/product.d.ts +2 -0
  258. package/dist/repositories/product.js +35 -17
  259. package/dist/repositories/product.js.map +1 -1
  260. package/dist/repositories/shipping-method-tax-line.d.ts +2 -0
  261. package/dist/repositories/shipping-method-tax-line.js +80 -0
  262. package/dist/repositories/shipping-method-tax-line.js.map +1 -1
  263. package/dist/scripts/discount-rule-migration.d.ts +4 -0
  264. package/dist/scripts/discount-rule-migration.js +177 -0
  265. package/dist/scripts/discount-rule-migration.js.map +1 -0
  266. package/dist/scripts/line-item-adjustment-migration.d.ts +4 -0
  267. package/dist/scripts/line-item-adjustment-migration.js +173 -0
  268. package/dist/scripts/line-item-adjustment-migration.js.map +1 -0
  269. package/dist/services/cart.d.ts +58 -47
  270. package/dist/services/cart.js +1306 -1391
  271. package/dist/services/cart.js.map +1 -1
  272. package/dist/services/customer.js +4 -2
  273. package/dist/services/customer.js.map +1 -1
  274. package/dist/services/discount.d.ts +50 -58
  275. package/dist/services/discount.js +363 -152
  276. package/dist/services/discount.js.map +1 -1
  277. package/dist/services/draft-order.js +1 -0
  278. package/dist/services/draft-order.js.map +1 -1
  279. package/dist/services/fulfillment-provider.js +23 -17
  280. package/dist/services/fulfillment-provider.js.map +1 -1
  281. package/dist/services/invite.d.ts +4 -2
  282. package/dist/services/invite.js +11 -9
  283. package/dist/services/invite.js.map +1 -1
  284. package/dist/services/line-item-adjustment.d.ts +87 -0
  285. package/dist/services/line-item-adjustment.js +436 -0
  286. package/dist/services/line-item-adjustment.js.map +1 -0
  287. package/dist/services/line-item.d.ts +48 -40
  288. package/dist/services/line-item.js +226 -193
  289. package/dist/services/line-item.js.map +1 -1
  290. package/dist/services/order.js +4 -4
  291. package/dist/services/order.js.map +1 -1
  292. package/dist/services/payment-provider.js +23 -17
  293. package/dist/services/payment-provider.js.map +1 -1
  294. package/dist/services/product-variant.d.ts +26 -9
  295. package/dist/services/product-variant.js +222 -76
  296. package/dist/services/product-variant.js.map +1 -1
  297. package/dist/services/product.d.ts +22 -4
  298. package/dist/services/product.js +236 -48
  299. package/dist/services/product.js.map +1 -1
  300. package/dist/services/region.js +8 -4
  301. package/dist/services/region.js.map +1 -1
  302. package/dist/services/return.js +0 -1
  303. package/dist/services/return.js.map +1 -1
  304. package/dist/services/shipping-option.d.ts +2 -2
  305. package/dist/services/shipping-option.js +7 -4
  306. package/dist/services/shipping-option.js.map +1 -1
  307. package/dist/services/swap.d.ts +4 -1
  308. package/dist/services/swap.js +29 -16
  309. package/dist/services/swap.js.map +1 -1
  310. package/dist/services/system-tax.d.ts +2 -3
  311. package/dist/services/system-tax.js +2 -2
  312. package/dist/services/system-tax.js.map +1 -1
  313. package/dist/services/tax-provider.d.ts +2 -0
  314. package/dist/services/tax-provider.js +96 -7
  315. package/dist/services/tax-provider.js.map +1 -1
  316. package/dist/services/tax-rate.d.ts +3 -3
  317. package/dist/services/tax-rate.js +1 -1
  318. package/dist/services/tax-rate.js.map +1 -1
  319. package/dist/services/totals.d.ts +9 -8
  320. package/dist/services/totals.js +41 -88
  321. package/dist/services/totals.js.map +1 -1
  322. package/dist/strategies/price-selection.d.ts +13 -0
  323. package/dist/strategies/price-selection.js +185 -0
  324. package/dist/strategies/price-selection.js.map +1 -0
  325. package/dist/subscribers/order.js +1 -7
  326. package/dist/subscribers/order.js.map +1 -1
  327. package/dist/types/batch-job.d.ts +6 -0
  328. package/dist/types/batch-job.js +11 -0
  329. package/dist/types/batch-job.js.map +1 -0
  330. package/dist/types/common.d.ts +1 -3
  331. package/dist/types/discount.d.ts +69 -1
  332. package/dist/types/discount.js +129 -5
  333. package/dist/types/discount.js.map +1 -1
  334. package/dist/types/global.d.ts +34 -0
  335. package/dist/types/global.js +3 -0
  336. package/dist/types/global.js.map +1 -0
  337. package/dist/types/line-item-adjustment.d.ts +9 -0
  338. package/dist/types/line-item-adjustment.js +50 -0
  339. package/dist/types/line-item-adjustment.js.map +1 -0
  340. package/dist/types/price-selection.d.ts +5 -0
  341. package/dist/types/price-selection.js +35 -0
  342. package/dist/types/price-selection.js.map +1 -0
  343. package/dist/types/product-variant.d.ts +6 -0
  344. package/dist/types/product-variant.js.map +1 -1
  345. package/dist/types/product.d.ts +16 -0
  346. package/dist/types/product.js +86 -1
  347. package/dist/types/product.js.map +1 -1
  348. package/dist/types/validators/exactly-one.d.ts +6 -0
  349. package/dist/types/validators/exactly-one.js +37 -0
  350. package/dist/types/validators/exactly-one.js.map +1 -0
  351. package/dist/utils/date-helpers.d.ts +2 -0
  352. package/dist/utils/date-helpers.js +15 -0
  353. package/dist/utils/date-helpers.js.map +1 -0
  354. package/dist/utils/get-query-config.d.ts +11 -0
  355. package/dist/utils/get-query-config.js +88 -0
  356. package/dist/utils/get-query-config.js.map +1 -0
  357. package/package.json +7 -8
  358. package/dist/api/routes/admin/discounts/add-valid-product.d.ts +0 -23
  359. package/dist/api/routes/admin/discounts/add-valid-product.js.map +0 -1
  360. package/dist/api/routes/admin/discounts/remove-valid-product.d.ts +0 -23
  361. package/dist/api/routes/admin/discounts/remove-valid-product.js.map +0 -1
  362. package/dist/api/routes/store/carts/update-payment-method.d.ts +0 -38
  363. package/dist/api/routes/store/carts/update-payment-method.js +0 -122
  364. package/dist/api/routes/store/carts/update-payment-method.js.map +0 -1
  365. package/dist/config/index.d.ts +0 -14
  366. package/dist/config/index.js +0 -40
  367. package/dist/config/index.js.map +0 -1
  368. package/yarn-error.log +0 -6769
@@ -104,17 +104,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
104
104
  var lodash_1 = __importDefault(require("lodash"));
105
105
  var medusa_core_utils_1 = require("medusa-core-utils");
106
106
  var medusa_interfaces_1 = require("medusa-interfaces");
107
+ var typeorm_1 = require("typeorm");
107
108
  /* Provides layer to manipulate carts.
108
109
  * @implements BaseService
109
110
  */
110
111
  var CartService = /** @class */ (function (_super) {
111
112
  __extends(CartService, _super);
112
113
  function CartService(_a) {
113
- var manager = _a.manager, cartRepository = _a.cartRepository, shippingMethodRepository = _a.shippingMethodRepository, 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, addressRepository = _a.addressRepository, paymentSessionRepository = _a.paymentSessionRepository, inventoryService = _a.inventoryService, customShippingOptionService = _a.customShippingOptionService;
114
+ 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, addressRepository = _a.addressRepository, paymentSessionRepository = _a.paymentSessionRepository, inventoryService = _a.inventoryService, customShippingOptionService = _a.customShippingOptionService, lineItemAdjustmentService = _a.lineItemAdjustmentService, priceSelectionStrategy = _a.priceSelectionStrategy;
114
115
  var _this = _super.call(this) || this;
115
116
  _this.manager_ = manager;
116
117
  _this.shippingMethodRepository_ = shippingMethodRepository;
117
118
  _this.cartRepository_ = cartRepository;
119
+ _this.lineItemRepository_ = lineItemRepository;
118
120
  _this.eventBus_ = eventBusService;
119
121
  _this.productVariantService_ = productVariantService;
120
122
  _this.productService_ = productService;
@@ -131,6 +133,8 @@ var CartService = /** @class */ (function (_super) {
131
133
  _this.inventoryService_ = inventoryService;
132
134
  _this.customShippingOptionService_ = customShippingOptionService;
133
135
  _this.taxProviderService_ = taxProviderService;
136
+ _this.lineItemAdjustmentService_ = lineItemAdjustmentService;
137
+ _this.priceSelectionStrategy_ = priceSelectionStrategy;
134
138
  return _this;
135
139
  }
136
140
  CartService.prototype.withTransaction = function (transactionManager) {
@@ -141,6 +145,7 @@ var CartService = /** @class */ (function (_super) {
141
145
  manager: transactionManager,
142
146
  taxProviderService: this.taxProviderService_,
143
147
  cartRepository: this.cartRepository_,
148
+ lineItemRepository: this.lineItemRepository_,
144
149
  eventBusService: this.eventBus_,
145
150
  paymentProviderService: this.paymentProviderService_,
146
151
  paymentSessionRepository: this.paymentSessionRepository_,
@@ -157,6 +162,8 @@ var CartService = /** @class */ (function (_super) {
157
162
  giftCardService: this.giftCardService_,
158
163
  inventoryService: this.inventoryService_,
159
164
  customShippingOptionService: this.customShippingOptionService_,
165
+ lineItemAdjustmentService: this.lineItemAdjustmentService_,
166
+ priceSelectionStrategy: this.priceSelectionStrategy_,
160
167
  });
161
168
  cloned.transactionManager_ = transactionManager;
162
169
  return cloned;
@@ -188,7 +195,6 @@ var CartService = /** @class */ (function (_super) {
188
195
  relationSet.add("gift_cards");
189
196
  relationSet.add("discounts");
190
197
  relationSet.add("discounts.rule");
191
- relationSet.add("discounts.rule.valid_for");
192
198
  // relationSet.add("discounts.parent_discount")
193
199
  // relationSet.add("discounts.parent_discount.rule")
194
200
  // relationSet.add("discounts.parent_discount.regions")
@@ -288,13 +294,21 @@ var CartService = /** @class */ (function (_super) {
288
294
  CartService.prototype.list = function (selector, config) {
289
295
  if (config === void 0) { config = {}; }
290
296
  return __awaiter(this, void 0, void 0, function () {
291
- var cartRepo, query;
297
+ var _this = this;
292
298
  return __generator(this, function (_a) {
293
299
  switch (_a.label) {
294
- case 0:
295
- cartRepo = this.manager_.getCustomRepository(this.cartRepository_);
296
- query = this.buildQuery_(selector, config);
297
- return [4 /*yield*/, cartRepo.find(query)];
300
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
301
+ var cartRepo, query;
302
+ return __generator(this, function (_a) {
303
+ switch (_a.label) {
304
+ case 0:
305
+ cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
306
+ query = this.buildQuery_(selector, config);
307
+ return [4 /*yield*/, cartRepo.find(query)];
308
+ case 1: return [2 /*return*/, _a.sent()];
309
+ }
310
+ });
311
+ }); })];
298
312
  case 1: return [2 /*return*/, _a.sent()];
299
313
  }
300
314
  });
@@ -311,33 +325,41 @@ var CartService = /** @class */ (function (_super) {
311
325
  if (options === void 0) { options = {}; }
312
326
  if (totalsConfig === void 0) { totalsConfig = {}; }
313
327
  return __awaiter(this, void 0, void 0, function () {
314
- var cartRepo, validatedId, _a, select, relations, totalsToSelect, query, rels, raw;
315
- return __generator(this, function (_b) {
316
- switch (_b.label) {
317
- case 0:
318
- cartRepo = this.manager_.getCustomRepository(this.cartRepository_);
319
- validatedId = this.validateId_(cartId);
320
- _a = this.transformQueryForTotals_(options), select = _a.select, relations = _a.relations, totalsToSelect = _a.totalsToSelect;
321
- query = this.buildQuery_({ id: validatedId }, __assign(__assign({}, options), { select: select, relations: relations }));
322
- if (relations && relations.length > 0) {
323
- query.relations = relations;
324
- }
325
- if (select && select.length > 0) {
326
- query.select = select;
327
- }
328
- else {
329
- delete query.select;
330
- }
331
- rels = query.relations;
332
- delete query.relations;
333
- return [4 /*yield*/, cartRepo.findOneWithRelations(rels, query)];
334
- case 1:
335
- raw = _b.sent();
336
- if (!raw) {
337
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Cart with ".concat(cartId, " was not found"));
338
- }
339
- return [4 /*yield*/, this.decorateTotals_(raw, totalsToSelect, totalsConfig)];
340
- case 2: return [2 /*return*/, _b.sent()];
328
+ var _this = this;
329
+ return __generator(this, function (_a) {
330
+ switch (_a.label) {
331
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
332
+ var cartRepo, validatedId, _a, select, relations, totalsToSelect, query, queryRelations, raw;
333
+ return __generator(this, function (_b) {
334
+ switch (_b.label) {
335
+ case 0:
336
+ cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
337
+ validatedId = this.validateId_(cartId);
338
+ _a = this.transformQueryForTotals_(options), select = _a.select, relations = _a.relations, totalsToSelect = _a.totalsToSelect;
339
+ query = this.buildQuery_({ id: validatedId }, __assign(__assign({}, options), { select: select, relations: relations }));
340
+ if (relations && relations.length > 0) {
341
+ query.relations = relations;
342
+ }
343
+ if (select && select.length > 0) {
344
+ query.select = select;
345
+ }
346
+ else {
347
+ query.select = undefined;
348
+ }
349
+ queryRelations = query.relations;
350
+ query.relations = undefined;
351
+ return [4 /*yield*/, cartRepo.findOneWithRelations(queryRelations, query)];
352
+ case 1:
353
+ raw = _b.sent();
354
+ if (!raw) {
355
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Cart with ".concat(cartId, " was not found"));
356
+ }
357
+ return [4 /*yield*/, this.decorateTotals_(raw, totalsToSelect, totalsConfig)];
358
+ case 2: return [2 /*return*/, _b.sent()];
359
+ }
360
+ });
361
+ }); })];
362
+ case 1: return [2 /*return*/, _a.sent()];
341
363
  }
342
364
  });
343
365
  });
@@ -351,101 +373,114 @@ var CartService = /** @class */ (function (_super) {
351
373
  return __awaiter(this, void 0, void 0, function () {
352
374
  var _this = this;
353
375
  return __generator(this, function (_a) {
354
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
355
- var cartRepo, addressRepo, region_id, region, regCountries, toCreate, customer, addr, remainingFields, remainingFields_1, remainingFields_1_1, k, inProgress, result;
356
- var e_2, _a;
357
- return __generator(this, function (_b) {
358
- switch (_b.label) {
359
- case 0:
360
- cartRepo = manager.getCustomRepository(this.cartRepository_);
361
- addressRepo = manager.getCustomRepository(this.addressRepository_);
362
- region_id = data.region_id;
363
- if (!region_id) {
364
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "A region_id must be provided when creating a cart");
365
- }
366
- return [4 /*yield*/, this.regionService_.retrieve(region_id, {
376
+ switch (_a.label) {
377
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
378
+ var cartRepo, addressRepo, region_id, rawCart, customer, region, regCountries, shippingAddress, _a, remainingFields, remainingFields_1, remainingFields_1_1, remainingField, createdCart, cart;
379
+ var e_2, _b;
380
+ return __generator(this, function (_c) {
381
+ switch (_c.label) {
382
+ case 0:
383
+ cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
384
+ addressRepo = transactionManager.getCustomRepository(this.addressRepository_);
385
+ region_id = data.region_id;
386
+ if (!region_id) {
387
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "A region_id must be provided when creating a cart");
388
+ }
389
+ rawCart = {};
390
+ if (!data.email) return [3 /*break*/, 2];
391
+ return [4 /*yield*/, this.createOrFetchUserFromEmail_(data.email)];
392
+ case 1:
393
+ customer = _c.sent();
394
+ rawCart.customer = customer;
395
+ rawCart.customer_id = customer.id;
396
+ rawCart.email = customer.email;
397
+ _c.label = 2;
398
+ case 2: return [4 /*yield*/, this.regionService_
399
+ .withTransaction(transactionManager)
400
+ .retrieve(region_id, {
367
401
  relations: ["countries"],
368
402
  })];
369
- case 1:
370
- region = _b.sent();
371
- regCountries = region.countries.map(function (_a) {
372
- var iso_2 = _a.iso_2;
373
- return iso_2;
374
- });
375
- toCreate = {};
376
- toCreate.region_id = region.id;
377
- if (!(typeof data.email !== "undefined")) return [3 /*break*/, 3];
378
- return [4 /*yield*/, this.createOrFetchUserFromEmail_(data.email)];
379
- case 2:
380
- customer = _b.sent();
381
- toCreate.customer = customer;
382
- toCreate.customer_id = customer.id;
383
- toCreate.email = customer.email;
384
- _b.label = 3;
385
- case 3:
386
- if (!(typeof data.shipping_address_id !== "undefined")) return [3 /*break*/, 5];
387
- return [4 /*yield*/, addressRepo.findOne(data.shipping_address_id)];
388
- case 4:
389
- addr = _b.sent();
390
- if (addr && !regCountries.includes(addr.country_code)) {
391
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Shipping country not in region");
392
- }
393
- toCreate.shipping_address = addr;
394
- _b.label = 5;
395
- case 5:
396
- if (!data.shipping_address) {
397
- if (region.countries.length === 1) {
398
- // Preselect the country if the region only has 1
399
- // and create address entity
400
- toCreate.shipping_address = addressRepo.create({
401
- country_code: regCountries[0],
402
- });
403
- }
404
- }
405
- else {
406
- if (!regCountries.includes(data.shipping_address.country_code)) {
403
+ case 3:
404
+ region = _c.sent();
405
+ regCountries = region.countries.map(function (_a) {
406
+ var iso_2 = _a.iso_2;
407
+ return iso_2;
408
+ });
409
+ rawCart.region_id = region.id;
410
+ if (!(data.shipping_address_id !== undefined)) return [3 /*break*/, 7];
411
+ if (!data.shipping_address_id) return [3 /*break*/, 5];
412
+ return [4 /*yield*/, addressRepo.findOne(data.shipping_address_id)];
413
+ case 4:
414
+ _a = _c.sent();
415
+ return [3 /*break*/, 6];
416
+ case 5:
417
+ _a = null;
418
+ _c.label = 6;
419
+ case 6:
420
+ shippingAddress = _a;
421
+ if (shippingAddress &&
422
+ !regCountries.includes(shippingAddress.country_code)) {
407
423
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Shipping country not in region");
408
424
  }
409
- toCreate.shipping_address = data.shipping_address;
410
- }
411
- remainingFields = [
412
- "billing_address_id",
413
- "context",
414
- "type",
415
- "metadata",
416
- "discounts",
417
- "gift_cards",
418
- ];
419
- try {
420
- for (remainingFields_1 = __values(remainingFields), remainingFields_1_1 = remainingFields_1.next(); !remainingFields_1_1.done; remainingFields_1_1 = remainingFields_1.next()) {
421
- k = remainingFields_1_1.value;
422
- if (typeof data[k] !== "undefined" && k !== "object") {
423
- toCreate[k] = data[k];
425
+ rawCart.shipping_address = shippingAddress;
426
+ _c.label = 7;
427
+ case 7:
428
+ if (!data.shipping_address) {
429
+ if (region.countries.length === 1) {
430
+ // Preselect the country if the region only has 1
431
+ // and create address entity
432
+ rawCart.shipping_address = addressRepo.create({
433
+ country_code: regCountries[0],
434
+ });
424
435
  }
425
436
  }
426
- }
427
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
428
- finally {
437
+ else {
438
+ if (!regCountries.includes(data.shipping_address.country_code)) {
439
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Shipping country not in region");
440
+ }
441
+ rawCart.shipping_address = data.shipping_address;
442
+ }
443
+ remainingFields = [
444
+ "billing_address_id",
445
+ "context",
446
+ "type",
447
+ "metadata",
448
+ "discounts",
449
+ "gift_cards",
450
+ ];
429
451
  try {
430
- if (remainingFields_1_1 && !remainingFields_1_1.done && (_a = remainingFields_1.return)) _a.call(remainingFields_1);
452
+ for (remainingFields_1 = __values(remainingFields), remainingFields_1_1 = remainingFields_1.next(); !remainingFields_1_1.done; remainingFields_1_1 = remainingFields_1.next()) {
453
+ remainingField = remainingFields_1_1.value;
454
+ if (typeof data[remainingField] !== "undefined" &&
455
+ remainingField !== "object") {
456
+ rawCart[remainingField] = data[remainingField];
457
+ }
458
+ }
431
459
  }
432
- finally { if (e_2) throw e_2.error; }
433
- }
434
- inProgress = cartRepo.create(toCreate);
435
- return [4 /*yield*/, cartRepo.save(inProgress)];
436
- case 6:
437
- result = _b.sent();
438
- return [4 /*yield*/, this.eventBus_
439
- .withTransaction(manager)
440
- .emit(CartService.Events.CREATED, {
441
- id: result.id,
442
- })];
443
- case 7:
444
- _b.sent();
445
- return [2 /*return*/, result];
446
- }
447
- });
448
- }); })];
460
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
461
+ finally {
462
+ try {
463
+ if (remainingFields_1_1 && !remainingFields_1_1.done && (_b = remainingFields_1.return)) _b.call(remainingFields_1);
464
+ }
465
+ finally { if (e_2) throw e_2.error; }
466
+ }
467
+ createdCart = cartRepo.create(rawCart);
468
+ return [4 /*yield*/, cartRepo.save(createdCart)];
469
+ case 8:
470
+ cart = _c.sent();
471
+ return [4 /*yield*/, this.eventBus_
472
+ .withTransaction(transactionManager)
473
+ .emit(CartService.Events.CREATED, {
474
+ id: cart.id,
475
+ })];
476
+ case 9:
477
+ _c.sent();
478
+ return [2 /*return*/, cart];
479
+ }
480
+ });
481
+ }); })];
482
+ case 1: return [2 /*return*/, _a.sent()];
483
+ }
449
484
  });
450
485
  });
451
486
  };
@@ -459,102 +494,72 @@ var CartService = /** @class */ (function (_super) {
459
494
  return __awaiter(this, void 0, void 0, function () {
460
495
  var _this = this;
461
496
  return __generator(this, function (_a) {
462
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
463
- var cart, lineItem, _a, _b, method, e_3_1, _c, _d, itm, e_4_1, result;
464
- var e_3, _e, e_4, _f;
465
- return __generator(this, function (_g) {
466
- switch (_g.label) {
467
- case 0: return [4 /*yield*/, this.retrieve(cartId, {
468
- relations: [
469
- "items",
470
- "items.variant",
471
- "items.variant.product",
472
- "payment_sessions",
473
- ],
474
- })];
475
- case 1:
476
- cart = _g.sent();
477
- lineItem = cart.items.find(function (li) { return li.id === lineItemId; });
478
- if (!lineItem) {
479
- return [2 /*return*/, cart];
480
- }
481
- if (!(cart.shipping_methods && cart.shipping_methods.length)) return [3 /*break*/, 9];
482
- _g.label = 2;
483
- case 2:
484
- _g.trys.push([2, 7, 8, 9]);
485
- _a = __values(cart.shipping_methods), _b = _a.next();
486
- _g.label = 3;
487
- case 3:
488
- if (!!_b.done) return [3 /*break*/, 6];
489
- method = _b.value;
490
- return [4 /*yield*/, this.shippingOptionService_
491
- .withTransaction(manager)
492
- .deleteShippingMethod(method)];
493
- case 4:
494
- _g.sent();
495
- _g.label = 5;
496
- case 5:
497
- _b = _a.next();
498
- return [3 /*break*/, 3];
499
- case 6: return [3 /*break*/, 9];
500
- case 7:
501
- e_3_1 = _g.sent();
502
- e_3 = { error: e_3_1 };
503
- return [3 /*break*/, 9];
504
- case 8:
505
- try {
506
- if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
507
- }
508
- finally { if (e_3) throw e_3.error; }
509
- return [7 /*endfinally*/];
510
- case 9:
511
- _g.trys.push([9, 14, 15, 16]);
512
- _c = __values(cart.items), _d = _c.next();
513
- _g.label = 10;
514
- case 10:
515
- if (!!_d.done) return [3 /*break*/, 13];
516
- itm = _d.value;
517
- return [4 /*yield*/, this.lineItemService_.withTransaction(manager).update(itm.id, {
518
- has_shipping: false,
497
+ switch (_a.label) {
498
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
499
+ var cart, lineItem, lineItemRepository, result;
500
+ var _a;
501
+ return __generator(this, function (_b) {
502
+ switch (_b.label) {
503
+ case 0: return [4 /*yield*/, this.retrieve(cartId, {
504
+ relations: [
505
+ "items",
506
+ "items.variant",
507
+ "items.variant.product",
508
+ "payment_sessions",
509
+ ],
519
510
  })];
520
- case 11:
521
- _g.sent();
522
- _g.label = 12;
523
- case 12:
524
- _d = _c.next();
525
- return [3 /*break*/, 10];
526
- case 13: return [3 /*break*/, 16];
527
- case 14:
528
- e_4_1 = _g.sent();
529
- e_4 = { error: e_4_1 };
530
- return [3 /*break*/, 16];
531
- case 15:
532
- try {
533
- if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
534
- }
535
- finally { if (e_4) throw e_4.error; }
536
- return [7 /*endfinally*/];
537
- case 16: return [4 /*yield*/, this.lineItemService_.withTransaction(manager).delete(lineItem.id)];
538
- case 17:
539
- _g.sent();
540
- return [4 /*yield*/, this.retrieve(cartId)
511
+ case 1:
512
+ cart = _b.sent();
513
+ lineItem = cart.items.find(function (item) { return item.id === lineItemId; });
514
+ if (!lineItem) {
515
+ return [2 /*return*/, cart];
516
+ }
517
+ if (!((_a = cart.shipping_methods) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 3];
518
+ return [4 /*yield*/, this.shippingOptionService_
519
+ .withTransaction(transactionManager)
520
+ .deleteShippingMethods(cart.shipping_methods)];
521
+ case 2:
522
+ _b.sent();
523
+ _b.label = 3;
524
+ case 3:
525
+ lineItemRepository = transactionManager.getCustomRepository(this.lineItemRepository_);
526
+ return [4 /*yield*/, lineItemRepository.update({
527
+ id: (0, typeorm_1.In)(cart.items.map(function (item) { return item.id; })),
528
+ }, {
529
+ has_shipping: false,
530
+ })];
531
+ case 4:
532
+ _b.sent();
533
+ return [4 /*yield*/, this.lineItemService_
534
+ .withTransaction(transactionManager)
535
+ .delete(lineItem.id)];
536
+ case 5:
537
+ _b.sent();
538
+ return [4 /*yield*/, this.retrieve(cartId, {
539
+ relations: ["items", "discounts", "discounts.rule"],
540
+ })];
541
+ case 6:
542
+ result = _b.sent();
543
+ return [4 /*yield*/, this.refreshAdjustments_(result)
544
+ // Notify subscribers
545
+ ];
546
+ case 7:
547
+ _b.sent();
541
548
  // Notify subscribers
542
- ];
543
- case 18:
544
- result = _g.sent();
545
- // Notify subscribers
546
- return [4 /*yield*/, this.eventBus_
547
- .withTransaction(manager)
548
- .emit(CartService.Events.UPDATED, {
549
- id: result.id,
550
- })];
551
- case 19:
552
- // Notify subscribers
553
- _g.sent();
554
- return [2 /*return*/, result];
555
- }
556
- });
557
- }); })];
549
+ return [4 /*yield*/, this.eventBus_
550
+ .withTransaction(transactionManager)
551
+ .emit(CartService.Events.UPDATED, {
552
+ id: cart.id,
553
+ })];
554
+ case 8:
555
+ // Notify subscribers
556
+ _b.sent();
557
+ return [2 /*return*/, this.retrieve(cartId)];
558
+ }
559
+ });
560
+ }); })];
561
+ case 1: return [2 /*return*/, _a.sent()];
562
+ }
558
563
  });
559
564
  });
560
565
  };
@@ -593,128 +598,86 @@ var CartService = /** @class */ (function (_super) {
593
598
  return __awaiter(this, void 0, void 0, function () {
594
599
  var _this = this;
595
600
  return __generator(this, function (_a) {
596
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
597
- var cart, currentItem, newQuantity, _a, _b, itm, e_5_1, _c, _d, method, e_6_1, result;
598
- var e_5, _e, e_6, _f;
599
- return __generator(this, function (_g) {
600
- switch (_g.label) {
601
- case 0: return [4 /*yield*/, this.retrieve(cartId, {
602
- relations: [
603
- "shipping_methods",
604
- "items",
605
- "payment_sessions",
606
- "items.variant",
607
- "items.variant.product",
608
- ],
609
- })];
610
- case 1:
611
- cart = _g.sent();
612
- if (lineItem.should_merge) {
613
- currentItem = cart.items.find(function (line) {
614
- if (line.should_merge && line.variant_id === lineItem.variant_id) {
615
- return lodash_1.default.isEqual(line.metadata, lineItem.metadata);
616
- }
617
- return false;
618
- });
619
- }
620
- if (!currentItem) return [3 /*break*/, 4];
621
- newQuantity = currentItem.quantity + lineItem.quantity;
622
- // Confirm inventory or throw error
623
- return [4 /*yield*/, this.inventoryService_
624
- .withTransaction(manager)
625
- .confirmInventory(lineItem.variant_id, newQuantity)];
626
- case 2:
627
- // Confirm inventory or throw error
628
- _g.sent();
629
- return [4 /*yield*/, this.lineItemService_
630
- .withTransaction(manager)
631
- .update(currentItem.id, {
632
- quantity: newQuantity,
633
- })];
634
- case 3:
635
- _g.sent();
636
- return [3 /*break*/, 7];
637
- case 4:
638
- // Confirm inventory or throw error
639
- return [4 /*yield*/, this.inventoryService_
640
- .withTransaction(manager)
641
- .confirmInventory(lineItem.variant_id, lineItem.quantity)];
642
- case 5:
643
- // Confirm inventory or throw error
644
- _g.sent();
645
- return [4 /*yield*/, this.lineItemService_.withTransaction(manager).create(__assign(__assign({}, lineItem), { has_shipping: false, cart_id: cartId }))];
646
- case 6:
647
- _g.sent();
648
- _g.label = 7;
649
- case 7:
650
- _g.trys.push([7, 12, 13, 14]);
651
- _a = __values(cart.items), _b = _a.next();
652
- _g.label = 8;
653
- case 8:
654
- if (!!_b.done) return [3 /*break*/, 11];
655
- itm = _b.value;
656
- return [4 /*yield*/, this.lineItemService_.withTransaction(manager).update(itm.id, {
657
- has_shipping: false,
601
+ switch (_a.label) {
602
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
603
+ var cart, currentItem, quantity, lineItemRepository, result;
604
+ return __generator(this, function (_a) {
605
+ switch (_a.label) {
606
+ case 0: return [4 /*yield*/, this.retrieve(cartId, {
607
+ relations: [
608
+ "shipping_methods",
609
+ "items",
610
+ "items.adjustments",
611
+ "payment_sessions",
612
+ "items.variant",
613
+ "items.variant.product",
614
+ "discounts",
615
+ "discounts.rule",
616
+ ],
658
617
  })];
659
- case 9:
660
- _g.sent();
661
- _g.label = 10;
662
- case 10:
663
- _b = _a.next();
664
- return [3 /*break*/, 8];
665
- case 11: return [3 /*break*/, 14];
666
- case 12:
667
- e_5_1 = _g.sent();
668
- e_5 = { error: e_5_1 };
669
- return [3 /*break*/, 14];
670
- case 13:
671
- try {
672
- if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
673
- }
674
- finally { if (e_5) throw e_5.error; }
675
- return [7 /*endfinally*/];
676
- case 14:
677
- if (!(cart.shipping_methods && cart.shipping_methods.length)) return [3 /*break*/, 22];
678
- _g.label = 15;
679
- case 15:
680
- _g.trys.push([15, 20, 21, 22]);
681
- _c = __values(cart.shipping_methods), _d = _c.next();
682
- _g.label = 16;
683
- case 16:
684
- if (!!_d.done) return [3 /*break*/, 19];
685
- method = _d.value;
686
- return [4 /*yield*/, this.shippingOptionService_
687
- .withTransaction(manager)
688
- .deleteShippingMethod(method)];
689
- case 17:
690
- _g.sent();
691
- _g.label = 18;
692
- case 18:
693
- _d = _c.next();
694
- return [3 /*break*/, 16];
695
- case 19: return [3 /*break*/, 22];
696
- case 20:
697
- e_6_1 = _g.sent();
698
- e_6 = { error: e_6_1 };
699
- return [3 /*break*/, 22];
700
- case 21:
701
- try {
702
- if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
703
- }
704
- finally { if (e_6) throw e_6.error; }
705
- return [7 /*endfinally*/];
706
- case 22: return [4 /*yield*/, this.retrieve(cartId)];
707
- case 23:
708
- result = _g.sent();
709
- return [4 /*yield*/, this.eventBus_
710
- .withTransaction(manager)
711
- .emit(CartService.Events.UPDATED, result)];
712
- case 24:
713
- _g.sent();
714
- return [2 /*return*/, result];
715
- }
716
- });
717
- }); })];
618
+ case 1:
619
+ cart = _a.sent();
620
+ if (lineItem.should_merge) {
621
+ currentItem = cart.items.find(function (item) {
622
+ if (item.should_merge && item.variant_id === lineItem.variant_id) {
623
+ return lodash_1.default.isEqual(item.metadata, lineItem.metadata);
624
+ }
625
+ return false;
626
+ });
627
+ }
628
+ quantity = currentItem
629
+ ? (currentItem.quantity += lineItem.quantity)
630
+ : lineItem.quantity;
631
+ // Confirm inventory or throw error
632
+ return [4 /*yield*/, this.inventoryService_
633
+ .withTransaction(transactionManager)
634
+ .confirmInventory(lineItem.variant_id, quantity)];
635
+ case 2:
636
+ // Confirm inventory or throw error
637
+ _a.sent();
638
+ if (!currentItem) return [3 /*break*/, 4];
639
+ return [4 /*yield*/, this.lineItemService_
640
+ .withTransaction(transactionManager)
641
+ .update(currentItem.id, {
642
+ quantity: currentItem.quantity,
643
+ })];
644
+ case 3:
645
+ _a.sent();
646
+ return [3 /*break*/, 6];
647
+ case 4: return [4 /*yield*/, this.lineItemService_
648
+ .withTransaction(transactionManager)
649
+ .create(__assign(__assign({}, lineItem), { has_shipping: false, cart_id: cartId }))];
650
+ case 5:
651
+ _a.sent();
652
+ _a.label = 6;
653
+ case 6:
654
+ lineItemRepository = transactionManager.getCustomRepository(this.lineItemRepository_);
655
+ return [4 /*yield*/, lineItemRepository.update({
656
+ id: (0, typeorm_1.In)(cart.items.map(function (item) { return item.id; })),
657
+ }, {
658
+ has_shipping: false,
659
+ })];
660
+ case 7:
661
+ _a.sent();
662
+ return [4 /*yield*/, this.retrieve(cartId, {
663
+ relations: ["items", "discounts", "discounts.rule"],
664
+ })];
665
+ case 8:
666
+ result = _a.sent();
667
+ return [4 /*yield*/, this.refreshAdjustments_(result)];
668
+ case 9:
669
+ _a.sent();
670
+ return [4 /*yield*/, this.eventBus_
671
+ .withTransaction(transactionManager)
672
+ .emit(CartService.Events.UPDATED, result)];
673
+ case 10:
674
+ _a.sent();
675
+ return [2 /*return*/, result];
676
+ }
677
+ });
678
+ }); })];
679
+ case 1: return [2 /*return*/, _a.sent()];
680
+ }
718
681
  });
719
682
  });
720
683
  };
@@ -729,50 +692,60 @@ var CartService = /** @class */ (function (_super) {
729
692
  return __awaiter(this, void 0, void 0, function () {
730
693
  var _this = this;
731
694
  return __generator(this, function (_a) {
732
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
733
- var cart, lineItemExists, hasInventory, result;
734
- return __generator(this, function (_a) {
735
- switch (_a.label) {
736
- case 0: return [4 /*yield*/, this.retrieve(cartId, {
737
- relations: ["items", "payment_sessions"],
738
- })
739
- // Ensure that the line item exists in the cart
740
- ];
741
- case 1:
742
- cart = _a.sent();
743
- lineItemExists = cart.items.find(function (i) { return i.id === lineItemId; });
744
- if (!lineItemExists) {
745
- 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");
746
- }
747
- if (!lineItemUpdate.quantity) return [3 /*break*/, 3];
748
- return [4 /*yield*/, this.inventoryService_
749
- .withTransaction(manager)
750
- .confirmInventory(lineItemExists.variant_id, lineItemUpdate.quantity)];
751
- case 2:
752
- hasInventory = _a.sent();
753
- if (!hasInventory) {
754
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Inventory doesn't cover the desired quantity");
755
- }
756
- _a.label = 3;
757
- case 3: return [4 /*yield*/, this.lineItemService_
758
- .withTransaction(manager)
759
- .update(lineItemId, lineItemUpdate)
760
- // Update the line item
761
- ];
762
- case 4:
763
- _a.sent();
764
- return [4 /*yield*/, this.retrieve(cartId)];
765
- case 5:
766
- result = _a.sent();
767
- return [4 /*yield*/, this.eventBus_
768
- .withTransaction(manager)
769
- .emit(CartService.Events.UPDATED, result)];
770
- case 6:
771
- _a.sent();
772
- return [2 /*return*/, result];
773
- }
774
- });
775
- }); })];
695
+ switch (_a.label) {
696
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
697
+ var cart, lineItemExists, hasInventory, updatedCart;
698
+ return __generator(this, function (_a) {
699
+ switch (_a.label) {
700
+ case 0: return [4 /*yield*/, this.retrieve(cartId, {
701
+ relations: ["items", "items.adjustments", "payment_sessions"],
702
+ })
703
+ // Ensure that the line item exists in the cart
704
+ ];
705
+ case 1:
706
+ cart = _a.sent();
707
+ lineItemExists = cart.items.find(function (i) { return i.id === lineItemId; });
708
+ if (!lineItemExists) {
709
+ 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");
710
+ }
711
+ if (!lineItemUpdate.quantity) return [3 /*break*/, 3];
712
+ return [4 /*yield*/, this.inventoryService_
713
+ .withTransaction(transactionManager)
714
+ .confirmInventory(lineItemExists.variant_id, lineItemUpdate.quantity)];
715
+ case 2:
716
+ hasInventory = _a.sent();
717
+ if (!hasInventory) {
718
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Inventory doesn't cover the desired quantity");
719
+ }
720
+ _a.label = 3;
721
+ case 3: return [4 /*yield*/, this.lineItemService_
722
+ .withTransaction(transactionManager)
723
+ .update(lineItemId, lineItemUpdate)];
724
+ case 4:
725
+ _a.sent();
726
+ return [4 /*yield*/, this.retrieve(cartId, {
727
+ relations: ["items", "discounts", "discounts.rule"],
728
+ })];
729
+ case 5:
730
+ updatedCart = _a.sent();
731
+ return [4 /*yield*/, this.refreshAdjustments_(updatedCart)
732
+ // Update the line item
733
+ ];
734
+ case 6:
735
+ _a.sent();
736
+ // Update the line item
737
+ return [4 /*yield*/, this.eventBus_
738
+ .withTransaction(transactionManager)
739
+ .emit(CartService.Events.UPDATED, updatedCart)];
740
+ case 7:
741
+ // Update the line item
742
+ _a.sent();
743
+ return [2 /*return*/, updatedCart];
744
+ }
745
+ });
746
+ }); })];
747
+ case 1: return [2 /*return*/, _a.sent()];
748
+ }
776
749
  });
777
750
  });
778
751
  };
@@ -788,258 +761,190 @@ var CartService = /** @class */ (function (_super) {
788
761
  CartService.prototype.adjustFreeShipping_ = function (cart, shouldAdd) {
789
762
  var _a;
790
763
  return __awaiter(this, void 0, void 0, function () {
764
+ var shippingMethodRepository_1;
791
765
  var _this = this;
792
766
  return __generator(this, function (_b) {
793
767
  switch (_b.label) {
794
768
  case 0:
795
- if (!((_a = cart.shipping_methods) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 4];
796
- if (!shouldAdd) return [3 /*break*/, 2];
797
- return [4 /*yield*/, Promise.all(cart.shipping_methods.map(function (sm) { return __awaiter(_this, void 0, void 0, function () {
798
- var smRepo, method;
799
- return __generator(this, function (_a) {
800
- switch (_a.label) {
801
- case 0:
802
- smRepo = this.manager_.getCustomRepository(this.shippingMethodRepository_);
803
- return [4 /*yield*/, smRepo.findOne({ where: { id: sm.id } })];
804
- case 1:
805
- method = _a.sent();
806
- if (!method) return [3 /*break*/, 3];
807
- method.price = 0;
808
- return [4 /*yield*/, smRepo.save(method)];
809
- case 2:
810
- _a.sent();
811
- _a.label = 3;
812
- case 3: return [2 /*return*/];
813
- }
814
- });
815
- }); }))];
816
- case 1:
817
- _b.sent();
818
- return [3 /*break*/, 4];
819
- case 2: return [4 /*yield*/, Promise.all(cart.shipping_methods.map(function (sm) { return __awaiter(_this, void 0, void 0, function () {
820
- var smRepo;
769
+ if (!((_a = cart.shipping_methods) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 3];
770
+ shippingMethodRepository_1 = this.transactionManager_.getCustomRepository(this.shippingMethodRepository_);
771
+ if (!shouldAdd) return [3 /*break*/, 1];
772
+ return [2 /*return*/, shippingMethodRepository_1.update({
773
+ id: (0, typeorm_1.In)(cart.shipping_methods.map(function (shippingMethod) { return shippingMethod.id; })),
774
+ }, {
775
+ price: 0,
776
+ })];
777
+ case 1: return [4 /*yield*/, Promise.all(cart.shipping_methods.map(function (shippingMethod) { return __awaiter(_this, void 0, void 0, function () {
821
778
  return __generator(this, function (_a) {
822
- switch (_a.label) {
823
- case 0:
824
- smRepo = this.manager_.getCustomRepository(this.shippingMethodRepository_);
825
- // if free shipping discount is removed, we adjust the shipping
826
- // back to its original amount
827
- sm.price = sm.shipping_option.amount;
828
- return [4 /*yield*/, smRepo.save(sm)];
829
- case 1:
830
- _a.sent();
831
- return [2 /*return*/];
832
- }
779
+ // if free shipping discount is removed, we adjust the shipping
780
+ // back to its original amount
781
+ shippingMethod.price = shippingMethod.shipping_option.amount;
782
+ return [2 /*return*/, shippingMethodRepository_1.save(shippingMethod)];
833
783
  });
834
784
  }); }))];
835
- case 3:
785
+ case 2:
836
786
  _b.sent();
837
- _b.label = 4;
838
- case 4: return [2 /*return*/];
787
+ _b.label = 3;
788
+ case 3: return [2 /*return*/];
839
789
  }
840
790
  });
841
791
  });
842
792
  };
843
- CartService.prototype.update = function (cartId, update) {
793
+ CartService.prototype.update = function (cartId, data) {
844
794
  return __awaiter(this, void 0, void 0, function () {
845
795
  var _this = this;
846
796
  return __generator(this, function (_a) {
847
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
848
- var cartRepo, cart, countryCode, customer, addrRepo, address, address, previousDiscounts, _a, _b, code, e_7_1, hasFreeShipping, _c, _d, code, e_8_1, prevContext, result;
849
- var e_7, _e, e_8, _f;
850
- var _g, _h;
851
- return __generator(this, function (_j) {
852
- switch (_j.label) {
853
- case 0:
854
- cartRepo = manager.getCustomRepository(this.cartRepository_);
855
- return [4 /*yield*/, this.retrieve(cartId, {
856
- select: [
857
- "subtotal",
858
- "tax_total",
859
- "shipping_total",
860
- "discount_total",
861
- "total",
862
- ],
863
- relations: [
864
- "items",
865
- "shipping_methods",
866
- "shipping_address",
867
- "billing_address",
868
- "gift_cards",
869
- "customer",
870
- "region",
871
- "payment_sessions",
872
- "region.countries",
873
- "discounts",
874
- "discounts.rule",
875
- "discounts.rule.valid_for",
876
- "discounts.regions",
877
- ],
878
- })];
879
- case 1:
880
- cart = _j.sent();
881
- if (!(typeof update.region_id !== "undefined")) return [3 /*break*/, 3];
882
- countryCode = (_h = (update.country_code || ((_g = update.shipping_address) === null || _g === void 0 ? void 0 : _g.country_code))) !== null && _h !== void 0 ? _h : null;
883
- return [4 /*yield*/, this.setRegion_(cart, update.region_id, countryCode)];
884
- case 2:
885
- _j.sent();
886
- _j.label = 3;
887
- case 3:
888
- if (!(typeof update.customer_id !== "undefined")) return [3 /*break*/, 5];
889
- return [4 /*yield*/, this.updateCustomerId_(cart, update.customer_id)];
890
- case 4:
891
- _j.sent();
892
- return [3 /*break*/, 7];
893
- case 5:
894
- if (!(typeof update.email !== "undefined")) return [3 /*break*/, 7];
895
- return [4 /*yield*/, this.createOrFetchUserFromEmail_(update.email)];
896
- case 6:
897
- customer = _j.sent();
898
- cart.customer = customer;
899
- cart.customer_id = customer.id;
900
- cart.email = customer.email;
901
- _j.label = 7;
902
- case 7:
903
- addrRepo = manager.getCustomRepository(this.addressRepository_);
904
- if (!("shipping_address_id" in update || "shipping_address" in update)) return [3 /*break*/, 9];
905
- address = void 0;
906
- if (typeof update.shipping_address_id !== "undefined") {
907
- address = update.shipping_address_id;
908
- }
909
- else if (typeof update.shipping_address !== "undefined") {
910
- address = update.shipping_address;
911
- }
912
- if (!(typeof address !== "undefined")) return [3 /*break*/, 9];
913
- return [4 /*yield*/, this.updateShippingAddress_(cart, address, addrRepo)];
914
- case 8:
915
- _j.sent();
916
- _j.label = 9;
917
- case 9:
918
- if (!("billing_address_id" in update || "billing_address" in update)) return [3 /*break*/, 11];
919
- address = void 0;
920
- if (typeof update.billing_address_id !== "undefined") {
921
- address = update.billing_address_id;
922
- }
923
- else if (typeof update.billing_address !== "undefined") {
924
- address = update.billing_address;
925
- }
926
- if (!(typeof address !== "undefined")) return [3 /*break*/, 11];
927
- return [4 /*yield*/, this.updateBillingAddress_(cart, address, addrRepo)];
928
- case 10:
929
- _j.sent();
930
- _j.label = 11;
931
- case 11:
932
- if (!(typeof update.discounts !== "undefined")) return [3 /*break*/, 23];
933
- previousDiscounts = cart.discounts;
934
- cart.discounts = [];
935
- _j.label = 12;
936
- case 12:
937
- _j.trys.push([12, 17, 18, 19]);
938
- _a = __values(update.discounts), _b = _a.next();
939
- _j.label = 13;
940
- case 13:
941
- if (!!_b.done) return [3 /*break*/, 16];
942
- code = _b.value.code;
943
- return [4 /*yield*/, this.applyDiscount(cart, code)];
944
- case 14:
945
- _j.sent();
946
- _j.label = 15;
947
- case 15:
948
- _b = _a.next();
949
- return [3 /*break*/, 13];
950
- case 16: return [3 /*break*/, 19];
951
- case 17:
952
- e_7_1 = _j.sent();
953
- e_7 = { error: e_7_1 };
954
- return [3 /*break*/, 19];
955
- case 18:
956
- try {
957
- if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
958
- }
959
- finally { if (e_7) throw e_7.error; }
960
- return [7 /*endfinally*/];
961
- case 19:
962
- hasFreeShipping = cart.discounts.some(function (_a) {
963
- var rule = _a.rule;
964
- return rule.type === "free_shipping";
965
- });
966
- if (!(previousDiscounts.some(function (_a) {
967
- var rule = _a.rule;
968
- return rule.type === "free_shipping";
969
- }) &&
970
- !hasFreeShipping)) return [3 /*break*/, 21];
971
- return [4 /*yield*/, this.adjustFreeShipping_(cart, false)];
972
- case 20:
973
- _j.sent();
974
- _j.label = 21;
975
- case 21:
976
- if (!hasFreeShipping) return [3 /*break*/, 23];
977
- return [4 /*yield*/, this.adjustFreeShipping_(cart, true)];
978
- case 22:
979
- _j.sent();
980
- _j.label = 23;
981
- case 23:
982
- if (!("gift_cards" in update)) return [3 /*break*/, 31];
983
- cart.gift_cards = [];
984
- _j.label = 24;
985
- case 24:
986
- _j.trys.push([24, 29, 30, 31]);
987
- _c = __values(update.gift_cards), _d = _c.next();
988
- _j.label = 25;
989
- case 25:
990
- if (!!_d.done) return [3 /*break*/, 28];
991
- code = _d.value.code;
992
- return [4 /*yield*/, this.applyGiftCard_(cart, code)];
993
- case 26:
994
- _j.sent();
995
- _j.label = 27;
996
- case 27:
997
- _d = _c.next();
998
- return [3 /*break*/, 25];
999
- case 28: return [3 /*break*/, 31];
1000
- case 29:
1001
- e_8_1 = _j.sent();
1002
- e_8 = { error: e_8_1 };
1003
- return [3 /*break*/, 31];
1004
- case 30:
1005
- try {
1006
- if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
1007
- }
1008
- finally { if (e_8) throw e_8.error; }
1009
- return [7 /*endfinally*/];
1010
- case 31:
1011
- if ("metadata" in update) {
1012
- cart.metadata = this.setMetadata_(cart, update.metadata);
1013
- }
1014
- if ("context" in update) {
1015
- prevContext = cart.context || {};
1016
- cart.context = __assign(__assign({}, prevContext), update.context);
1017
- }
1018
- if ("completed_at" in update) {
1019
- cart.completed_at = update.completed_at;
1020
- }
1021
- if ("payment_authorized_at" in update) {
1022
- cart.payment_authorized_at = update.payment_authorized_at;
1023
- }
1024
- return [4 /*yield*/, cartRepo.save(cart)];
1025
- case 32:
1026
- result = _j.sent();
1027
- if (!("email" in update || "customer_id" in update)) return [3 /*break*/, 34];
1028
- return [4 /*yield*/, this.eventBus_
1029
- .withTransaction(manager)
1030
- .emit(CartService.Events.CUSTOMER_UPDATED, result.id)];
1031
- case 33:
1032
- _j.sent();
1033
- _j.label = 34;
1034
- case 34: return [4 /*yield*/, this.eventBus_
1035
- .withTransaction(manager)
1036
- .emit(CartService.Events.UPDATED, result)];
1037
- case 35:
1038
- _j.sent();
1039
- return [2 /*return*/, result];
1040
- }
1041
- });
1042
- }); })];
797
+ switch (_a.label) {
798
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
799
+ var cartRepo, cart, customer, countryCode, addrRepo, billingAddress, shippingAddress, previousDiscounts, hasFreeShipping, prevContext, updatedCart;
800
+ var _this = this;
801
+ var _a, _b, _c, _d, _e, _f;
802
+ return __generator(this, function (_g) {
803
+ switch (_g.label) {
804
+ case 0:
805
+ cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
806
+ return [4 /*yield*/, this.retrieve(cartId, {
807
+ select: [
808
+ "subtotal",
809
+ "tax_total",
810
+ "shipping_total",
811
+ "discount_total",
812
+ "total",
813
+ ],
814
+ relations: [
815
+ "items",
816
+ "shipping_methods",
817
+ "shipping_address",
818
+ "billing_address",
819
+ "gift_cards",
820
+ "customer",
821
+ "region",
822
+ "payment_sessions",
823
+ "region.countries",
824
+ "discounts",
825
+ "discounts.rule",
826
+ "discounts.regions",
827
+ ],
828
+ })];
829
+ case 1:
830
+ cart = _g.sent();
831
+ if (!data.customer_id) return [3 /*break*/, 3];
832
+ return [4 /*yield*/, this.updateCustomerId_(cart, data.customer_id)];
833
+ case 2:
834
+ _g.sent();
835
+ return [3 /*break*/, 5];
836
+ case 3:
837
+ if (!(typeof data.email !== "undefined")) return [3 /*break*/, 5];
838
+ return [4 /*yield*/, this.createOrFetchUserFromEmail_(data.email)];
839
+ case 4:
840
+ customer = _g.sent();
841
+ cart.customer = customer;
842
+ cart.customer_id = customer.id;
843
+ cart.email = customer.email;
844
+ _g.label = 5;
845
+ case 5:
846
+ if (!(typeof data.region_id !== "undefined")) return [3 /*break*/, 7];
847
+ countryCode = (_b = (data.country_code || ((_a = data.shipping_address) === null || _a === void 0 ? void 0 : _a.country_code))) !== null && _b !== void 0 ? _b : null;
848
+ return [4 /*yield*/, this.setRegion_(cart, data.region_id, countryCode)];
849
+ case 6:
850
+ _g.sent();
851
+ _g.label = 7;
852
+ case 7:
853
+ if (!(typeof data.customer_id !== "undefined" ||
854
+ typeof data.region_id !== "undefined")) return [3 /*break*/, 9];
855
+ return [4 /*yield*/, this.updateUnitPrices_(cart, data.region_id, data.customer_id)];
856
+ case 8:
857
+ _g.sent();
858
+ _g.label = 9;
859
+ case 9:
860
+ addrRepo = transactionManager.getCustomRepository(this.addressRepository_);
861
+ billingAddress = (_c = data.billing_address_id) !== null && _c !== void 0 ? _c : data.billing_address;
862
+ if (!(billingAddress !== undefined)) return [3 /*break*/, 11];
863
+ return [4 /*yield*/, this.updateBillingAddress_(cart, billingAddress, addrRepo)];
864
+ case 10:
865
+ _g.sent();
866
+ _g.label = 11;
867
+ case 11:
868
+ shippingAddress = (_d = data.shipping_address_id) !== null && _d !== void 0 ? _d : data.shipping_address;
869
+ if (!(shippingAddress !== undefined)) return [3 /*break*/, 13];
870
+ return [4 /*yield*/, this.updateShippingAddress_(cart, shippingAddress, addrRepo)];
871
+ case 12:
872
+ _g.sent();
873
+ _g.label = 13;
874
+ case 13:
875
+ if (!((_e = data.discounts) === null || _e === void 0 ? void 0 : _e.length)) return [3 /*break*/, 18];
876
+ previousDiscounts = __spreadArray([], __read(cart.discounts), false);
877
+ cart.discounts.length = 0;
878
+ return [4 /*yield*/, Promise.all(data.discounts.map(function (_a) {
879
+ var code = _a.code;
880
+ return _this.applyDiscount(cart, code);
881
+ }))];
882
+ case 14:
883
+ _g.sent();
884
+ hasFreeShipping = cart.discounts.some(function (_a) {
885
+ var rule = _a.rule;
886
+ return (rule === null || rule === void 0 ? void 0 : rule.type) === "free_shipping";
887
+ });
888
+ if (!(previousDiscounts.some(function (_a) {
889
+ var rule = _a.rule;
890
+ return (rule === null || rule === void 0 ? void 0 : rule.type) === "free_shipping";
891
+ }) &&
892
+ !hasFreeShipping)) return [3 /*break*/, 16];
893
+ return [4 /*yield*/, this.adjustFreeShipping_(cart, false)];
894
+ case 15:
895
+ _g.sent();
896
+ _g.label = 16;
897
+ case 16:
898
+ if (!hasFreeShipping) return [3 /*break*/, 18];
899
+ return [4 /*yield*/, this.adjustFreeShipping_(cart, true)];
900
+ case 17:
901
+ _g.sent();
902
+ _g.label = 18;
903
+ case 18:
904
+ if (!("gift_cards" in data)) return [3 /*break*/, 20];
905
+ cart.gift_cards = [];
906
+ return [4 /*yield*/, Promise.all(((_f = data.gift_cards) !== null && _f !== void 0 ? _f : []).map(function (_a) {
907
+ var code = _a.code;
908
+ return _this.applyGiftCard_(cart, code);
909
+ }))];
910
+ case 19:
911
+ _g.sent();
912
+ _g.label = 20;
913
+ case 20:
914
+ if ("metadata" in data) {
915
+ cart.metadata = this.setMetadata_(cart, data.metadata);
916
+ }
917
+ if ("context" in data) {
918
+ prevContext = cart.context || {};
919
+ cart.context = __assign(__assign({}, prevContext), data.context);
920
+ }
921
+ if ("completed_at" in data) {
922
+ cart.completed_at = data.completed_at;
923
+ }
924
+ if ("payment_authorized_at" in data) {
925
+ cart.payment_authorized_at = data.payment_authorized_at;
926
+ }
927
+ return [4 /*yield*/, cartRepo.save(cart)];
928
+ case 21:
929
+ updatedCart = _g.sent();
930
+ if (!("email" in data || "customer_id" in data)) return [3 /*break*/, 23];
931
+ return [4 /*yield*/, this.eventBus_
932
+ .withTransaction(transactionManager)
933
+ .emit(CartService.Events.CUSTOMER_UPDATED, updatedCart.id)];
934
+ case 22:
935
+ _g.sent();
936
+ _g.label = 23;
937
+ case 23: return [4 /*yield*/, this.eventBus_
938
+ .withTransaction(transactionManager)
939
+ .emit(CartService.Events.UPDATED, updatedCart)];
940
+ case 24:
941
+ _g.sent();
942
+ return [2 /*return*/, updatedCart];
943
+ }
944
+ });
945
+ }); })];
946
+ case 1: return [2 /*return*/, _a.sent()];
947
+ }
1043
948
  });
1044
949
  });
1045
950
  };
@@ -1111,27 +1016,27 @@ var CartService = /** @class */ (function (_super) {
1111
1016
  CartService.prototype.updateBillingAddress_ = function (cart, addressOrId, addrRepo) {
1112
1017
  var _a, _b;
1113
1018
  return __awaiter(this, void 0, void 0, function () {
1114
- var address, updated, addr, created;
1115
- return __generator(this, function (_c) {
1116
- switch (_c.label) {
1019
+ var address, _c, addr;
1020
+ return __generator(this, function (_d) {
1021
+ switch (_d.label) {
1117
1022
  case 0:
1118
1023
  if (!(typeof addressOrId === "string")) return [3 /*break*/, 2];
1119
1024
  return [4 /*yield*/, addrRepo.findOne({
1120
1025
  where: { id: addressOrId },
1121
1026
  })];
1122
1027
  case 1:
1123
- address = (_c.sent());
1028
+ address = (_d.sent());
1124
1029
  return [3 /*break*/, 3];
1125
1030
  case 2:
1126
1031
  address = addressOrId;
1127
- _c.label = 3;
1032
+ _d.label = 3;
1128
1033
  case 3:
1129
1034
  address.country_code = (_b = (_a = address.country_code) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : null;
1130
1035
  if (!address.id) return [3 /*break*/, 5];
1036
+ _c = cart;
1131
1037
  return [4 /*yield*/, addrRepo.save(address)];
1132
1038
  case 4:
1133
- updated = _c.sent();
1134
- cart.billing_address = updated;
1039
+ _c.billing_address = _d.sent();
1135
1040
  return [3 /*break*/, 9];
1136
1041
  case 5:
1137
1042
  if (!cart.billing_address_id) return [3 /*break*/, 8];
@@ -1139,15 +1044,14 @@ var CartService = /** @class */ (function (_super) {
1139
1044
  where: { id: cart.billing_address_id },
1140
1045
  })];
1141
1046
  case 6:
1142
- addr = _c.sent();
1047
+ addr = _d.sent();
1143
1048
  return [4 /*yield*/, addrRepo.save(__assign(__assign({}, addr), address))];
1144
1049
  case 7:
1145
- _c.sent();
1050
+ _d.sent();
1146
1051
  return [3 /*break*/, 9];
1147
1052
  case 8:
1148
- created = addrRepo.create(__assign({}, address));
1149
- cart.billing_address = created;
1150
- _c.label = 9;
1053
+ cart.billing_address = addrRepo.create(__assign({}, address));
1054
+ _d.label = 9;
1151
1055
  case 9: return [2 /*return*/];
1152
1056
  }
1153
1057
  });
@@ -1163,9 +1067,9 @@ var CartService = /** @class */ (function (_super) {
1163
1067
  CartService.prototype.updateShippingAddress_ = function (cart, addressOrId, addrRepo) {
1164
1068
  var _a, _b;
1165
1069
  return __awaiter(this, void 0, void 0, function () {
1166
- var address, updated, addr, created;
1167
- return __generator(this, function (_c) {
1168
- switch (_c.label) {
1070
+ var address, _c, addr;
1071
+ return __generator(this, function (_d) {
1072
+ switch (_d.label) {
1169
1073
  case 0:
1170
1074
  if (addressOrId === null) {
1171
1075
  cart.shipping_address = null;
@@ -1176,11 +1080,11 @@ var CartService = /** @class */ (function (_super) {
1176
1080
  where: { id: addressOrId },
1177
1081
  })];
1178
1082
  case 1:
1179
- address = (_c.sent());
1083
+ address = (_d.sent());
1180
1084
  return [3 /*break*/, 3];
1181
1085
  case 2:
1182
1086
  address = addressOrId;
1183
- _c.label = 3;
1087
+ _d.label = 3;
1184
1088
  case 3:
1185
1089
  address.country_code = (_b = (_a = address.country_code) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : null;
1186
1090
  if (address.country_code &&
@@ -1191,10 +1095,10 @@ var CartService = /** @class */ (function (_super) {
1191
1095
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Shipping country must be in the cart region");
1192
1096
  }
1193
1097
  if (!address.id) return [3 /*break*/, 5];
1098
+ _c = cart;
1194
1099
  return [4 /*yield*/, addrRepo.save(address)];
1195
1100
  case 4:
1196
- updated = _c.sent();
1197
- cart.shipping_address = updated;
1101
+ _c.shipping_address = _d.sent();
1198
1102
  return [3 /*break*/, 9];
1199
1103
  case 5:
1200
1104
  if (!cart.shipping_address_id) return [3 /*break*/, 8];
@@ -1202,15 +1106,14 @@ var CartService = /** @class */ (function (_super) {
1202
1106
  where: { id: cart.shipping_address_id },
1203
1107
  })];
1204
1108
  case 6:
1205
- addr = _c.sent();
1109
+ addr = _d.sent();
1206
1110
  return [4 /*yield*/, addrRepo.save(__assign(__assign({}, addr), address))];
1207
1111
  case 7:
1208
- _c.sent();
1112
+ _d.sent();
1209
1113
  return [3 /*break*/, 9];
1210
1114
  case 8:
1211
- created = addrRepo.create(__assign({}, address));
1212
- cart.shipping_address = created;
1213
- _c.label = 9;
1115
+ cart.shipping_address = addrRepo.create(__assign({}, address));
1116
+ _d.label = 9;
1214
1117
  case 9: return [2 /*return*/];
1215
1118
  }
1216
1119
  });
@@ -1221,7 +1124,9 @@ var CartService = /** @class */ (function (_super) {
1221
1124
  var giftCard;
1222
1125
  return __generator(this, function (_a) {
1223
1126
  switch (_a.label) {
1224
- case 0: return [4 /*yield*/, this.giftCardService_.retrieveByCode(code)];
1127
+ case 0: return [4 /*yield*/, this.giftCardService_
1128
+ .withTransaction(this.transactionManager_)
1129
+ .retrieveByCode(code)];
1225
1130
  case 1:
1226
1131
  giftCard = _a.sent();
1227
1132
  if (giftCard.is_disabled) {
@@ -1235,7 +1140,7 @@ var CartService = /** @class */ (function (_super) {
1235
1140
  var id = _a.id;
1236
1141
  return id === giftCard.id;
1237
1142
  })) {
1238
- return [2 /*return*/, Promise.resolve()];
1143
+ return [2 /*return*/];
1239
1144
  }
1240
1145
  cart.gift_cards = __spreadArray(__spreadArray([], __read(cart.gift_cards), false), [giftCard], false);
1241
1146
  return [2 /*return*/];
@@ -1254,80 +1159,65 @@ var CartService = /** @class */ (function (_super) {
1254
1159
  */
1255
1160
  CartService.prototype.applyDiscount = function (cart, discountCode) {
1256
1161
  return __awaiter(this, void 0, void 0, function () {
1257
- var discount, rule, today, regions, parent, toParse, sawNotShipping, newDiscounts;
1162
+ var _this = this;
1258
1163
  return __generator(this, function (_a) {
1259
1164
  switch (_a.label) {
1260
- case 0: return [4 /*yield*/, this.discountService_.retrieveByCode(discountCode, [
1261
- "rule",
1262
- "rule.valid_for",
1263
- "regions",
1264
- ])];
1265
- case 1:
1266
- discount = _a.sent();
1267
- rule = discount.rule;
1268
- // if limit is set and reached, we make an early exit
1269
- if (discount.usage_limit) {
1270
- discount.usage_count = discount.usage_count || 0;
1271
- if (discount.usage_count >= discount.usage_limit) {
1272
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Discount has been used maximum allowed times");
1273
- }
1274
- }
1275
- today = new Date();
1276
- if (discount.starts_at > today) {
1277
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Discount is not valid yet");
1278
- }
1279
- if (discount.ends_at && discount.ends_at < today) {
1280
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Discount is expired");
1281
- }
1282
- regions = discount.regions;
1283
- if (!discount.parent_discount_id) return [3 /*break*/, 3];
1284
- return [4 /*yield*/, this.discountService_.retrieve(discount.parent_discount_id, {
1285
- relations: ["rule", "regions"],
1286
- })];
1287
- case 2:
1288
- parent = _a.sent();
1289
- regions = parent.regions;
1290
- _a.label = 3;
1291
- case 3:
1292
- if (discount.is_disabled) {
1293
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "The discount code is disabled");
1294
- }
1295
- if (!regions.find(function (_a) {
1296
- var id = _a.id;
1297
- return id === cart.region_id;
1298
- })) {
1299
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "The discount is not available in current region");
1300
- }
1301
- // if discount is already there, we simply resolve
1302
- if (cart.discounts.find(function (_a) {
1303
- var id = _a.id;
1304
- return id === discount.id;
1305
- })) {
1306
- return [2 /*return*/, Promise.resolve()];
1307
- }
1308
- toParse = __spreadArray(__spreadArray([], __read(cart.discounts), false), [discount], false);
1309
- sawNotShipping = false;
1310
- newDiscounts = toParse.map(function (d) {
1311
- var drule = d.rule;
1312
- switch (drule.type) {
1313
- case "free_shipping":
1314
- if (d.rule.type === rule.type) {
1315
- return discount;
1316
- }
1317
- return d;
1318
- default:
1319
- if (!sawNotShipping) {
1320
- sawNotShipping = true;
1321
- if (rule.type !== "free_shipping") {
1322
- return discount;
1165
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
1166
+ var discount, rule, toParse, sawNotShipping, newDiscounts;
1167
+ return __generator(this, function (_a) {
1168
+ switch (_a.label) {
1169
+ case 0: return [4 /*yield*/, this.discountService_
1170
+ .withTransaction(transactionManager)
1171
+ .retrieveByCode(discountCode, ["rule", "regions"])];
1172
+ case 1:
1173
+ discount = _a.sent();
1174
+ return [4 /*yield*/, this.discountService_
1175
+ .withTransaction(transactionManager)
1176
+ .validateDiscountForCartOrThrow(cart, discount)];
1177
+ case 2:
1178
+ _a.sent();
1179
+ rule = discount.rule;
1180
+ // if discount is already there, we simply resolve
1181
+ if (cart.discounts.find(function (_a) {
1182
+ var id = _a.id;
1183
+ return id === discount.id;
1184
+ })) {
1185
+ return [2 /*return*/];
1323
1186
  }
1324
- return d;
1325
- }
1326
- return null;
1327
- }
1328
- });
1329
- cart.discounts = newDiscounts.filter(Boolean);
1330
- return [2 /*return*/];
1187
+ toParse = __spreadArray(__spreadArray([], __read(cart.discounts), false), [discount], false);
1188
+ sawNotShipping = false;
1189
+ newDiscounts = toParse.map(function (discountToParse) {
1190
+ var _a;
1191
+ switch ((_a = discountToParse.rule) === null || _a === void 0 ? void 0 : _a.type) {
1192
+ case "free_shipping":
1193
+ if (discountToParse.rule.type === rule.type) {
1194
+ return discount;
1195
+ }
1196
+ return discountToParse;
1197
+ default:
1198
+ if (!sawNotShipping) {
1199
+ sawNotShipping = true;
1200
+ if ((rule === null || rule === void 0 ? void 0 : rule.type) !== "free_shipping") {
1201
+ return discount;
1202
+ }
1203
+ return discountToParse;
1204
+ }
1205
+ return null;
1206
+ }
1207
+ });
1208
+ cart.discounts = newDiscounts.filter(function (newDiscount) {
1209
+ return !!newDiscount;
1210
+ });
1211
+ if (!((rule === null || rule === void 0 ? void 0 : rule.type) !== "free_shipping" && (cart === null || cart === void 0 ? void 0 : cart.items))) return [3 /*break*/, 4];
1212
+ return [4 /*yield*/, this.refreshAdjustments_(cart)];
1213
+ case 3:
1214
+ _a.sent();
1215
+ _a.label = 4;
1216
+ case 4: return [2 /*return*/];
1217
+ }
1218
+ });
1219
+ }); })];
1220
+ case 1: return [2 /*return*/, _a.sent()];
1331
1221
  }
1332
1222
  });
1333
1223
  });
@@ -1342,94 +1232,101 @@ var CartService = /** @class */ (function (_super) {
1342
1232
  return __awaiter(this, void 0, void 0, function () {
1343
1233
  var _this = this;
1344
1234
  return __generator(this, function (_a) {
1345
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1346
- var cart, cartRepo, result;
1347
- var _a;
1348
- return __generator(this, function (_b) {
1349
- switch (_b.label) {
1350
- case 0: return [4 /*yield*/, this.retrieve(cartId, {
1351
- relations: [
1352
- "discounts",
1353
- "discounts.rule",
1354
- "discounts.rule.valid_for",
1355
- "payment_sessions",
1356
- "shipping_methods",
1357
- ],
1358
- })];
1359
- case 1:
1360
- cart = _b.sent();
1361
- if (!cart.discounts.some(function (_a) {
1362
- var rule = _a.rule;
1363
- return rule.type === "free_shipping";
1364
- })) return [3 /*break*/, 3];
1365
- return [4 /*yield*/, this.adjustFreeShipping_(cart, false)];
1366
- case 2:
1367
- _b.sent();
1368
- _b.label = 3;
1369
- case 3:
1370
- cart.discounts = cart.discounts.filter(function (d) { return d.code !== discountCode; });
1371
- cartRepo = manager.getCustomRepository(this.cartRepository_);
1372
- return [4 /*yield*/, cartRepo.save(cart)];
1373
- case 4:
1374
- result = _b.sent();
1375
- if (!((_a = cart.payment_sessions) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 6];
1376
- return [4 /*yield*/, this.setPaymentSessions(cartId)];
1377
- case 5:
1378
- _b.sent();
1379
- _b.label = 6;
1380
- case 6: return [4 /*yield*/, this.eventBus_
1381
- .withTransaction(manager)
1382
- .emit(CartService.Events.UPDATED, result)];
1383
- case 7:
1384
- _b.sent();
1385
- return [2 /*return*/, result];
1386
- }
1387
- });
1388
- }); })];
1389
- });
1390
- });
1391
- };
1392
- /**
1393
- * Updates the currently selected payment session.
1394
- * @param cartId - the id of the cart to update the payment session for
1395
- * @param update - the data to update the payment session with
1396
- * @return the resulting cart
1397
- */
1398
- CartService.prototype.updatePaymentSession = function (cartId, update) {
1399
- return __awaiter(this, void 0, void 0, function () {
1400
- var _this = this;
1401
- return __generator(this, function (_a) {
1402
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1403
- var cart, result;
1404
- return __generator(this, function (_a) {
1405
- switch (_a.label) {
1406
- case 0: return [4 /*yield*/, this.retrieve(cartId, {
1407
- relations: ["payment_sessions"],
1408
- })];
1409
- case 1:
1410
- cart = _a.sent();
1411
- if (!cart.payment_session) return [3 /*break*/, 3];
1412
- return [4 /*yield*/, this.paymentProviderService_.updateSessionData(cart.payment_session, update)];
1413
- case 2:
1414
- _a.sent();
1415
- _a.label = 3;
1416
- case 3: return [4 /*yield*/, this.retrieve(cart.id)];
1417
- case 4:
1418
- result = _a.sent();
1419
- return [4 /*yield*/, this.eventBus_
1420
- .withTransaction(manager)
1421
- .emit(CartService.Events.UPDATED, result)];
1422
- case 5:
1423
- _a.sent();
1424
- return [2 /*return*/, result];
1425
- }
1426
- });
1427
- }); })];
1428
- });
1429
- });
1430
- };
1431
- /**
1432
- * Authorizes a payment for a cart.
1235
+ switch (_a.label) {
1236
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
1237
+ var cart, cartRepo, updatedCart;
1238
+ var _a;
1239
+ return __generator(this, function (_b) {
1240
+ switch (_b.label) {
1241
+ case 0: return [4 /*yield*/, this.retrieve(cartId, {
1242
+ relations: [
1243
+ "discounts",
1244
+ "discounts.rule",
1245
+ "payment_sessions",
1246
+ "shipping_methods",
1247
+ ],
1248
+ })];
1249
+ case 1:
1250
+ cart = _b.sent();
1251
+ if (!cart.discounts.some(function (_a) {
1252
+ var rule = _a.rule;
1253
+ return rule.type === "free_shipping";
1254
+ })) return [3 /*break*/, 3];
1255
+ return [4 /*yield*/, this.adjustFreeShipping_(cart, false)];
1256
+ case 2:
1257
+ _b.sent();
1258
+ _b.label = 3;
1259
+ case 3:
1260
+ cart.discounts = cart.discounts.filter(function (discount) { return discount.code !== discountCode; });
1261
+ cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
1262
+ return [4 /*yield*/, cartRepo.save(cart)];
1263
+ case 4:
1264
+ updatedCart = _b.sent();
1265
+ if (!((_a = updatedCart.payment_sessions) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 6];
1266
+ return [4 /*yield*/, this.setPaymentSessions(cartId)];
1267
+ case 5:
1268
+ _b.sent();
1269
+ _b.label = 6;
1270
+ case 6: return [4 /*yield*/, this.eventBus_
1271
+ .withTransaction(transactionManager)
1272
+ .emit(CartService.Events.UPDATED, updatedCart)];
1273
+ case 7:
1274
+ _b.sent();
1275
+ return [2 /*return*/, updatedCart];
1276
+ }
1277
+ });
1278
+ }); })];
1279
+ case 1: return [2 /*return*/, _a.sent()];
1280
+ }
1281
+ });
1282
+ });
1283
+ };
1284
+ /**
1285
+ * Updates the currently selected payment session.
1286
+ * @param cartId - the id of the cart to update the payment session for
1287
+ * @param update - the data to update the payment session with
1288
+ * @return the resulting cart
1289
+ */
1290
+ CartService.prototype.updatePaymentSession = function (cartId, update) {
1291
+ return __awaiter(this, void 0, void 0, function () {
1292
+ var _this = this;
1293
+ return __generator(this, function (_a) {
1294
+ switch (_a.label) {
1295
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
1296
+ var cart, updatedCart;
1297
+ return __generator(this, function (_a) {
1298
+ switch (_a.label) {
1299
+ case 0: return [4 /*yield*/, this.retrieve(cartId, {
1300
+ relations: ["payment_sessions"],
1301
+ })];
1302
+ case 1:
1303
+ cart = _a.sent();
1304
+ if (!cart.payment_session) return [3 /*break*/, 3];
1305
+ return [4 /*yield*/, this.paymentProviderService_
1306
+ .withTransaction(transactionManager)
1307
+ .updateSessionData(cart.payment_session, update)];
1308
+ case 2:
1309
+ _a.sent();
1310
+ _a.label = 3;
1311
+ case 3: return [4 /*yield*/, this.retrieve(cart.id)];
1312
+ case 4:
1313
+ updatedCart = _a.sent();
1314
+ return [4 /*yield*/, this.eventBus_
1315
+ .withTransaction(transactionManager)
1316
+ .emit(CartService.Events.UPDATED, updatedCart)];
1317
+ case 5:
1318
+ _a.sent();
1319
+ return [2 /*return*/, updatedCart];
1320
+ }
1321
+ });
1322
+ }); })];
1323
+ case 1: return [2 /*return*/, _a.sent()];
1324
+ }
1325
+ });
1326
+ });
1327
+ };
1328
+ /**
1329
+ * Authorizes a payment for a cart.
1433
1330
  * Will authorize with chosen payment provider. This will return
1434
1331
  * a payment object, that we will use to update our cart payment with.
1435
1332
  * Additionally, if the payment does not require more or fails, we will
@@ -1445,58 +1342,64 @@ var CartService = /** @class */ (function (_super) {
1445
1342
  return __awaiter(this, void 0, void 0, function () {
1446
1343
  var _this = this;
1447
1344
  return __generator(this, function (_a) {
1448
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1449
- var cartRepository, cart, session, freshCart, payment, updated;
1450
- return __generator(this, function (_a) {
1451
- switch (_a.label) {
1452
- case 0:
1453
- cartRepository = manager.getCustomRepository(this.cartRepository_);
1454
- return [4 /*yield*/, this.retrieve(cartId, {
1455
- select: ["total"],
1456
- relations: ["region", "payment_sessions"],
1457
- })];
1458
- case 1:
1459
- cart = _a.sent();
1460
- if (typeof cart.total === "undefined") {
1461
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "cart.total should be defined");
1462
- }
1463
- // If cart total is 0, we don't perform anything payment related
1464
- if (cart.total <= 0) {
1465
- cart.payment_authorized_at = new Date();
1466
- return [2 /*return*/, cartRepository.save(cart)];
1467
- }
1468
- return [4 /*yield*/, this.paymentProviderService_
1469
- .withTransaction(manager)
1470
- .authorizePayment(cart.payment_session, context)];
1471
- case 2:
1472
- session = _a.sent();
1473
- return [4 /*yield*/, this.retrieve(cart.id, {
1474
- select: ["total"],
1475
- relations: ["payment_sessions"],
1476
- })];
1477
- case 3:
1478
- freshCart = _a.sent();
1479
- if (!(session.status === "authorized")) return [3 /*break*/, 5];
1480
- return [4 /*yield*/, this.paymentProviderService_
1481
- .withTransaction(manager)
1482
- .createPayment(freshCart)];
1483
- case 4:
1484
- payment = _a.sent();
1485
- freshCart.payment = payment;
1486
- freshCart.payment_authorized_at = new Date();
1487
- _a.label = 5;
1488
- case 5: return [4 /*yield*/, cartRepository.save(freshCart)];
1489
- case 6:
1490
- updated = _a.sent();
1491
- return [4 /*yield*/, this.eventBus_
1492
- .withTransaction(manager)
1493
- .emit(CartService.Events.UPDATED, updated)];
1494
- case 7:
1495
- _a.sent();
1496
- return [2 /*return*/, updated];
1497
- }
1498
- });
1499
- }); })];
1345
+ switch (_a.label) {
1346
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
1347
+ var cartRepository, cart, session, freshCart, _a, updatedCart;
1348
+ return __generator(this, function (_b) {
1349
+ switch (_b.label) {
1350
+ case 0:
1351
+ cartRepository = transactionManager.getCustomRepository(this.cartRepository_);
1352
+ return [4 /*yield*/, this.retrieve(cartId, {
1353
+ select: ["total"],
1354
+ relations: ["region", "payment_sessions"],
1355
+ })];
1356
+ case 1:
1357
+ cart = _b.sent();
1358
+ if (typeof cart.total === "undefined") {
1359
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "cart.total should be defined");
1360
+ }
1361
+ // If cart total is 0, we don't perform anything payment related
1362
+ if (cart.total <= 0) {
1363
+ cart.payment_authorized_at = new Date();
1364
+ return [2 /*return*/, cartRepository.save(cart)];
1365
+ }
1366
+ if (!cart.payment_session) {
1367
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "You cannot complete a cart without a payment session.");
1368
+ }
1369
+ return [4 /*yield*/, this.paymentProviderService_
1370
+ .withTransaction(transactionManager)
1371
+ .authorizePayment(cart.payment_session, context)];
1372
+ case 2:
1373
+ session = _b.sent();
1374
+ return [4 /*yield*/, this.retrieve(cart.id, {
1375
+ select: ["total"],
1376
+ relations: ["payment_sessions"],
1377
+ })];
1378
+ case 3:
1379
+ freshCart = _b.sent();
1380
+ if (!(session.status === "authorized")) return [3 /*break*/, 5];
1381
+ _a = freshCart;
1382
+ return [4 /*yield*/, this.paymentProviderService_
1383
+ .withTransaction(transactionManager)
1384
+ .createPayment(freshCart)];
1385
+ case 4:
1386
+ _a.payment = _b.sent();
1387
+ freshCart.payment_authorized_at = new Date();
1388
+ _b.label = 5;
1389
+ case 5: return [4 /*yield*/, cartRepository.save(freshCart)];
1390
+ case 6:
1391
+ updatedCart = _b.sent();
1392
+ return [4 /*yield*/, this.eventBus_
1393
+ .withTransaction(transactionManager)
1394
+ .emit(CartService.Events.UPDATED, updatedCart)];
1395
+ case 7:
1396
+ _b.sent();
1397
+ return [2 /*return*/, updatedCart];
1398
+ }
1399
+ });
1400
+ }); })];
1401
+ case 1: return [2 /*return*/, _a.sent()];
1402
+ }
1500
1403
  });
1501
1404
  });
1502
1405
  };
@@ -1510,66 +1413,66 @@ var CartService = /** @class */ (function (_super) {
1510
1413
  return __awaiter(this, void 0, void 0, function () {
1511
1414
  var _this = this;
1512
1415
  return __generator(this, function (_a) {
1513
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1514
- var psRepo, cart, sess, result;
1515
- var _this = this;
1516
- return __generator(this, function (_a) {
1517
- switch (_a.label) {
1518
- case 0:
1519
- psRepo = manager.getCustomRepository(this.paymentSessionRepository_);
1520
- return [4 /*yield*/, this.retrieve(cartId, {
1521
- select: [
1522
- "total",
1523
- "subtotal",
1524
- "tax_total",
1525
- "discount_total",
1526
- "gift_card_total",
1527
- ],
1528
- relations: ["region", "region.payment_providers", "payment_sessions"],
1529
- })
1416
+ switch (_a.label) {
1417
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
1418
+ var psRepo, cart, paymentSession, updatedCart;
1419
+ var _this = this;
1420
+ return __generator(this, function (_a) {
1421
+ switch (_a.label) {
1422
+ case 0:
1423
+ psRepo = transactionManager.getCustomRepository(this.paymentSessionRepository_);
1424
+ return [4 /*yield*/, this.retrieve(cartId, {
1425
+ select: [
1426
+ "total",
1427
+ "subtotal",
1428
+ "tax_total",
1429
+ "discount_total",
1430
+ "gift_card_total",
1431
+ ],
1432
+ relations: ["region", "region.payment_providers", "payment_sessions"],
1433
+ })
1434
+ // The region must have the provider id in its providers array
1435
+ ];
1436
+ case 1:
1437
+ cart = _a.sent();
1530
1438
  // The region must have the provider id in its providers array
1531
- ];
1532
- case 1:
1533
- cart = _a.sent();
1534
- // The region must have the provider id in its providers array
1535
- if (providerId !== "system" &&
1536
- !(cart.region.payment_providers.length &&
1537
- cart.region.payment_providers.find(function (_a) {
1538
- var id = _a.id;
1539
- return providerId === id;
1540
- }))) {
1541
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "The payment method is not available in this region");
1542
- }
1543
- return [4 /*yield*/, Promise.all(cart.payment_sessions.map(function (ps) { return __awaiter(_this, void 0, void 0, function () {
1544
- return __generator(this, function (_a) {
1545
- switch (_a.label) {
1546
- case 0: return [4 /*yield*/, psRepo.save(__assign(__assign({}, ps), { is_selected: null }))];
1547
- case 1: return [2 /*return*/, _a.sent()];
1548
- }
1549
- });
1550
- }); }))];
1551
- case 2:
1552
- _a.sent();
1553
- sess = cart.payment_sessions.find(function (ps) { return ps.provider_id === providerId; });
1554
- if (!sess) {
1555
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Could not find payment session");
1556
- }
1557
- sess.is_selected = true;
1558
- return [4 /*yield*/, psRepo.save(sess)];
1559
- case 3:
1560
- _a.sent();
1561
- return [4 /*yield*/, this.retrieve(cartId)];
1562
- case 4:
1563
- result = _a.sent();
1564
- return [4 /*yield*/, this.eventBus_
1565
- .withTransaction(manager)
1566
- .emit(CartService.Events.UPDATED, result)];
1567
- case 5:
1568
- _a.sent();
1569
- return [2 /*return*/, result];
1570
- }
1571
- });
1572
- }); }, "SERIALIZABLE")];
1439
+ if (providerId !== "system" &&
1440
+ !(cart.region.payment_providers.length &&
1441
+ cart.region.payment_providers.find(function (_a) {
1442
+ var id = _a.id;
1443
+ return providerId === id;
1444
+ }))) {
1445
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "The payment method is not available in this region");
1446
+ }
1447
+ return [4 /*yield*/, Promise.all(cart.payment_sessions.map(function (paymentSession) { return __awaiter(_this, void 0, void 0, function () {
1448
+ return __generator(this, function (_a) {
1449
+ return [2 /*return*/, psRepo.save(__assign(__assign({}, paymentSession), { is_selected: null }))];
1450
+ });
1451
+ }); }))];
1452
+ case 2:
1453
+ _a.sent();
1454
+ paymentSession = cart.payment_sessions.find(function (ps) { return ps.provider_id === providerId; });
1455
+ if (!paymentSession) {
1456
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Could not find payment session");
1457
+ }
1458
+ paymentSession.is_selected = true;
1459
+ return [4 /*yield*/, psRepo.save(paymentSession)];
1460
+ case 3:
1461
+ _a.sent();
1462
+ return [4 /*yield*/, this.retrieve(cartId)];
1463
+ case 4:
1464
+ updatedCart = _a.sent();
1465
+ return [4 /*yield*/, this.eventBus_
1466
+ .withTransaction(transactionManager)
1467
+ .emit(CartService.Events.UPDATED, updatedCart)];
1468
+ case 5:
1469
+ _a.sent();
1470
+ return [2 /*return*/, updatedCart];
1471
+ }
1472
+ });
1473
+ }); }, "SERIALIZABLE")];
1474
+ case 1: return [2 /*return*/, _a.sent()];
1475
+ }
1573
1476
  });
1574
1477
  });
1575
1478
  };
@@ -1586,147 +1489,104 @@ var CartService = /** @class */ (function (_super) {
1586
1489
  return __awaiter(this, void 0, void 0, function () {
1587
1490
  var _this = this;
1588
1491
  return __generator(this, function (_a) {
1589
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1590
- var psRepo, cartId, cart, total, region, seen, _loop_1, this_1, _a, _b, session, e_9_1, p, sess, _c, _d, provider, e_10_1;
1591
- var e_9, _e, e_10, _f;
1592
- return __generator(this, function (_g) {
1593
- switch (_g.label) {
1594
- case 0:
1595
- psRepo = manager.getCustomRepository(this.paymentSessionRepository_);
1596
- cartId = typeof cartOrCartId === "string" ? cartOrCartId : cartOrCartId.id;
1597
- return [4 /*yield*/, this.retrieve(cartId, {
1598
- select: [
1599
- "total",
1600
- "subtotal",
1601
- "tax_total",
1602
- "discount_total",
1603
- "shipping_total",
1604
- "gift_card_total",
1605
- ],
1606
- relations: [
1607
- "items",
1608
- "discounts",
1609
- "discounts.rule",
1610
- "discounts.rule.valid_for",
1611
- "gift_cards",
1612
- "shipping_methods",
1613
- "billing_address",
1614
- "shipping_address",
1615
- "region",
1616
- "region.tax_rates",
1617
- "region.payment_providers",
1618
- "payment_sessions",
1619
- "customer",
1620
- ],
1621
- }, { force_taxes: true })];
1622
- case 1:
1623
- cart = _g.sent();
1624
- total = cart.total, region = cart.region;
1625
- if (typeof total === "undefined") {
1626
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "cart.total must be defined");
1627
- }
1628
- seen = [];
1629
- if (!(cart.payment_sessions && cart.payment_sessions.length)) return [3 /*break*/, 9];
1630
- _loop_1 = function (session) {
1631
- return __generator(this, function (_h) {
1632
- switch (_h.label) {
1633
- case 0:
1634
- if (!(total <= 0 ||
1492
+ switch (_a.label) {
1493
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
1494
+ var psRepo, cartId, cart, total, region, seen, paymentProvider, paymentSession;
1495
+ var _this = this;
1496
+ var _a;
1497
+ return __generator(this, function (_b) {
1498
+ switch (_b.label) {
1499
+ case 0:
1500
+ psRepo = transactionManager.getCustomRepository(this.paymentSessionRepository_);
1501
+ cartId = typeof cartOrCartId === "string" ? cartOrCartId : cartOrCartId.id;
1502
+ return [4 /*yield*/, this.retrieve(cartId, {
1503
+ select: [
1504
+ "total",
1505
+ "subtotal",
1506
+ "tax_total",
1507
+ "discount_total",
1508
+ "shipping_total",
1509
+ "gift_card_total",
1510
+ ],
1511
+ relations: [
1512
+ "items",
1513
+ "discounts",
1514
+ "discounts.rule",
1515
+ "gift_cards",
1516
+ "shipping_methods",
1517
+ "billing_address",
1518
+ "shipping_address",
1519
+ "region",
1520
+ "region.tax_rates",
1521
+ "region.payment_providers",
1522
+ "payment_sessions",
1523
+ "customer",
1524
+ ],
1525
+ }, { force_taxes: true })];
1526
+ case 1:
1527
+ cart = _b.sent();
1528
+ total = cart.total, region = cart.region;
1529
+ if (typeof total === "undefined") {
1530
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "cart.total must be defined");
1531
+ }
1532
+ seen = [];
1533
+ if (!((_a = cart.payment_sessions) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 3];
1534
+ return [4 /*yield*/, Promise.all(cart.payment_sessions.map(function (paymentSession) { return __awaiter(_this, void 0, void 0, function () {
1535
+ return __generator(this, function (_a) {
1536
+ if (total <= 0 ||
1635
1537
  !region.payment_providers.find(function (_a) {
1636
1538
  var id = _a.id;
1637
- return id === session.provider_id;
1638
- }))) return [3 /*break*/, 2];
1639
- return [4 /*yield*/, this_1.paymentProviderService_
1640
- .withTransaction(manager)
1641
- .deleteSession(session)];
1642
- case 1:
1643
- _h.sent();
1644
- return [3 /*break*/, 4];
1645
- case 2:
1646
- seen.push(session.provider_id);
1647
- return [4 /*yield*/, this_1.paymentProviderService_
1648
- .withTransaction(manager)
1649
- .updateSession(session, cart)];
1650
- case 3:
1651
- _h.sent();
1652
- _h.label = 4;
1653
- case 4: return [2 /*return*/];
1654
- }
1655
- });
1656
- };
1657
- this_1 = this;
1658
- _g.label = 2;
1659
- case 2:
1660
- _g.trys.push([2, 7, 8, 9]);
1661
- _a = __values(cart.payment_sessions), _b = _a.next();
1662
- _g.label = 3;
1663
- case 3:
1664
- if (!!_b.done) return [3 /*break*/, 6];
1665
- session = _b.value;
1666
- return [5 /*yield**/, _loop_1(session)];
1667
- case 4:
1668
- _g.sent();
1669
- _g.label = 5;
1670
- case 5:
1671
- _b = _a.next();
1672
- return [3 /*break*/, 3];
1673
- case 6: return [3 /*break*/, 9];
1674
- case 7:
1675
- e_9_1 = _g.sent();
1676
- e_9 = { error: e_9_1 };
1677
- return [3 /*break*/, 9];
1678
- case 8:
1679
- try {
1680
- if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
1681
- }
1682
- finally { if (e_9) throw e_9.error; }
1683
- return [7 /*endfinally*/];
1684
- case 9:
1685
- if (!(total > 0)) return [3 /*break*/, 19];
1686
- if (!(region.payment_providers.length === 1 && !cart.payment_session)) return [3 /*break*/, 12];
1687
- p = region.payment_providers[0];
1688
- return [4 /*yield*/, this.paymentProviderService_
1689
- .withTransaction(manager)
1690
- .createSession(p.id, cart)];
1691
- case 10:
1692
- sess = _g.sent();
1693
- sess.is_selected = true;
1694
- return [4 /*yield*/, psRepo.save(sess)];
1695
- case 11:
1696
- _g.sent();
1697
- return [3 /*break*/, 19];
1698
- case 12:
1699
- _g.trys.push([12, 17, 18, 19]);
1700
- _c = __values(region.payment_providers), _d = _c.next();
1701
- _g.label = 13;
1702
- case 13:
1703
- if (!!_d.done) return [3 /*break*/, 16];
1704
- provider = _d.value;
1705
- if (!!seen.includes(provider.id)) return [3 /*break*/, 15];
1706
- return [4 /*yield*/, this.paymentProviderService_
1707
- .withTransaction(manager)
1708
- .createSession(provider.id, cart)];
1709
- case 14:
1710
- _g.sent();
1711
- _g.label = 15;
1712
- case 15:
1713
- _d = _c.next();
1714
- return [3 /*break*/, 13];
1715
- case 16: return [3 /*break*/, 19];
1716
- case 17:
1717
- e_10_1 = _g.sent();
1718
- e_10 = { error: e_10_1 };
1719
- return [3 /*break*/, 19];
1720
- case 18:
1721
- try {
1722
- if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
1723
- }
1724
- finally { if (e_10) throw e_10.error; }
1725
- return [7 /*endfinally*/];
1726
- case 19: return [2 /*return*/];
1727
- }
1728
- });
1729
- }); })];
1539
+ return id === paymentSession.provider_id;
1540
+ })) {
1541
+ return [2 /*return*/, this.paymentProviderService_
1542
+ .withTransaction(transactionManager)
1543
+ .deleteSession(paymentSession)];
1544
+ }
1545
+ else {
1546
+ seen.push(paymentSession.provider_id);
1547
+ return [2 /*return*/, this.paymentProviderService_
1548
+ .withTransaction(transactionManager)
1549
+ .updateSession(paymentSession, cart)];
1550
+ }
1551
+ return [2 /*return*/];
1552
+ });
1553
+ }); }))];
1554
+ case 2:
1555
+ _b.sent();
1556
+ _b.label = 3;
1557
+ case 3:
1558
+ if (!(total > 0)) return [3 /*break*/, 8];
1559
+ if (!(region.payment_providers.length === 1 && !cart.payment_session)) return [3 /*break*/, 6];
1560
+ paymentProvider = region.payment_providers[0];
1561
+ return [4 /*yield*/, this.paymentProviderService_
1562
+ .withTransaction(transactionManager)
1563
+ .createSession(paymentProvider.id, cart)];
1564
+ case 4:
1565
+ paymentSession = _b.sent();
1566
+ paymentSession.is_selected = true;
1567
+ return [4 /*yield*/, psRepo.save(paymentSession)];
1568
+ case 5:
1569
+ _b.sent();
1570
+ return [3 /*break*/, 8];
1571
+ case 6: return [4 /*yield*/, Promise.all(region.payment_providers.map(function (paymentProvider) { return __awaiter(_this, void 0, void 0, function () {
1572
+ return __generator(this, function (_a) {
1573
+ if (!seen.includes(paymentProvider.id)) {
1574
+ return [2 /*return*/, this.paymentProviderService_
1575
+ .withTransaction(transactionManager)
1576
+ .createSession(paymentProvider.id, cart)];
1577
+ }
1578
+ return [2 /*return*/];
1579
+ });
1580
+ }); }))];
1581
+ case 7:
1582
+ _b.sent();
1583
+ _b.label = 8;
1584
+ case 8: return [2 /*return*/];
1585
+ }
1586
+ });
1587
+ }); })];
1588
+ case 1: return [2 /*return*/, _a.sent()];
1589
+ }
1730
1590
  });
1731
1591
  });
1732
1592
  };
@@ -1741,46 +1601,49 @@ var CartService = /** @class */ (function (_super) {
1741
1601
  return __awaiter(this, void 0, void 0, function () {
1742
1602
  var _this = this;
1743
1603
  return __generator(this, function (_a) {
1744
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1745
- var cart, cartRepo, session;
1746
- return __generator(this, function (_a) {
1747
- switch (_a.label) {
1748
- case 0: return [4 /*yield*/, this.retrieve(cartId, {
1749
- relations: ["payment_sessions"],
1750
- })];
1751
- case 1:
1752
- cart = _a.sent();
1753
- cartRepo = manager.getCustomRepository(this.cartRepository_);
1754
- if (!cart.payment_sessions) return [3 /*break*/, 3];
1755
- session = cart.payment_sessions.find(function (_a) {
1756
- var provider_id = _a.provider_id;
1757
- return provider_id === providerId;
1758
- });
1759
- cart.payment_sessions = cart.payment_sessions.filter(function (_a) {
1760
- var provider_id = _a.provider_id;
1761
- return provider_id !== providerId;
1762
- });
1763
- if (!session) return [3 /*break*/, 3];
1764
- // Delete the session with the provider
1765
- return [4 /*yield*/, this.paymentProviderService_
1766
- .withTransaction(manager)
1767
- .deleteSession(session)];
1768
- case 2:
1769
- // Delete the session with the provider
1770
- _a.sent();
1771
- _a.label = 3;
1772
- case 3: return [4 /*yield*/, cartRepo.save(cart)];
1773
- case 4:
1774
- _a.sent();
1775
- return [4 /*yield*/, this.eventBus_
1776
- .withTransaction(manager)
1777
- .emit(CartService.Events.UPDATED, cart)];
1778
- case 5:
1779
- _a.sent();
1780
- return [2 /*return*/, cart];
1781
- }
1782
- });
1783
- }); })];
1604
+ switch (_a.label) {
1605
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
1606
+ var cart, cartRepo, paymentSession;
1607
+ return __generator(this, function (_a) {
1608
+ switch (_a.label) {
1609
+ case 0: return [4 /*yield*/, this.retrieve(cartId, {
1610
+ relations: ["payment_sessions"],
1611
+ })];
1612
+ case 1:
1613
+ cart = _a.sent();
1614
+ cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
1615
+ if (!cart.payment_sessions) return [3 /*break*/, 3];
1616
+ paymentSession = cart.payment_sessions.find(function (_a) {
1617
+ var provider_id = _a.provider_id;
1618
+ return provider_id === providerId;
1619
+ });
1620
+ cart.payment_sessions = cart.payment_sessions.filter(function (_a) {
1621
+ var provider_id = _a.provider_id;
1622
+ return provider_id !== providerId;
1623
+ });
1624
+ if (!paymentSession) return [3 /*break*/, 3];
1625
+ // Delete the session with the provider
1626
+ return [4 /*yield*/, this.paymentProviderService_
1627
+ .withTransaction(transactionManager)
1628
+ .deleteSession(paymentSession)];
1629
+ case 2:
1630
+ // Delete the session with the provider
1631
+ _a.sent();
1632
+ _a.label = 3;
1633
+ case 3: return [4 /*yield*/, cartRepo.save(cart)];
1634
+ case 4:
1635
+ _a.sent();
1636
+ return [4 /*yield*/, this.eventBus_
1637
+ .withTransaction(transactionManager)
1638
+ .emit(CartService.Events.UPDATED, cart)];
1639
+ case 5:
1640
+ _a.sent();
1641
+ return [2 /*return*/, cart];
1642
+ }
1643
+ });
1644
+ }); })];
1645
+ case 1: return [2 /*return*/, _a.sent()];
1646
+ }
1784
1647
  });
1785
1648
  });
1786
1649
  };
@@ -1795,41 +1658,44 @@ var CartService = /** @class */ (function (_super) {
1795
1658
  return __awaiter(this, void 0, void 0, function () {
1796
1659
  var _this = this;
1797
1660
  return __generator(this, function (_a) {
1798
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1799
- var cart, session, result;
1800
- return __generator(this, function (_a) {
1801
- switch (_a.label) {
1802
- case 0: return [4 /*yield*/, this.retrieve(cartId, {
1803
- relations: ["payment_sessions"],
1804
- })];
1805
- case 1:
1806
- cart = _a.sent();
1807
- if (!cart.payment_sessions) return [3 /*break*/, 3];
1808
- session = cart.payment_sessions.find(function (_a) {
1809
- var provider_id = _a.provider_id;
1810
- return provider_id === providerId;
1811
- });
1812
- if (!session) return [3 /*break*/, 3];
1813
- // Delete the session with the provider
1814
- return [4 /*yield*/, this.paymentProviderService_
1815
- .withTransaction(manager)
1816
- .refreshSession(session, cart)];
1817
- case 2:
1818
- // Delete the session with the provider
1819
- _a.sent();
1820
- _a.label = 3;
1821
- case 3: return [4 /*yield*/, this.retrieve(cartId)];
1822
- case 4:
1823
- result = _a.sent();
1824
- return [4 /*yield*/, this.eventBus_
1825
- .withTransaction(manager)
1826
- .emit(CartService.Events.UPDATED, result)];
1827
- case 5:
1828
- _a.sent();
1829
- return [2 /*return*/, result];
1830
- }
1831
- });
1832
- }); })];
1661
+ switch (_a.label) {
1662
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
1663
+ var cart, paymentSession, updatedCart;
1664
+ return __generator(this, function (_a) {
1665
+ switch (_a.label) {
1666
+ case 0: return [4 /*yield*/, this.retrieve(cartId, {
1667
+ relations: ["payment_sessions"],
1668
+ })];
1669
+ case 1:
1670
+ cart = _a.sent();
1671
+ if (!cart.payment_sessions) return [3 /*break*/, 3];
1672
+ paymentSession = cart.payment_sessions.find(function (_a) {
1673
+ var provider_id = _a.provider_id;
1674
+ return provider_id === providerId;
1675
+ });
1676
+ if (!paymentSession) return [3 /*break*/, 3];
1677
+ // Delete the session with the provider
1678
+ return [4 /*yield*/, this.paymentProviderService_
1679
+ .withTransaction(transactionManager)
1680
+ .refreshSession(paymentSession, cart)];
1681
+ case 2:
1682
+ // Delete the session with the provider
1683
+ _a.sent();
1684
+ _a.label = 3;
1685
+ case 3: return [4 /*yield*/, this.retrieve(cartId)];
1686
+ case 4:
1687
+ updatedCart = _a.sent();
1688
+ return [4 /*yield*/, this.eventBus_
1689
+ .withTransaction(transactionManager)
1690
+ .emit(CartService.Events.UPDATED, updatedCart)];
1691
+ case 5:
1692
+ _a.sent();
1693
+ return [2 /*return*/, updatedCart];
1694
+ }
1695
+ });
1696
+ }); })];
1697
+ case 1: return [2 /*return*/, _a.sent()];
1698
+ }
1833
1699
  });
1834
1700
  });
1835
1701
  };
@@ -1849,133 +1715,119 @@ var CartService = /** @class */ (function (_super) {
1849
1715
  return __awaiter(this, void 0, void 0, function () {
1850
1716
  var _this = this;
1851
1717
  return __generator(this, function (_a) {
1852
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1853
- var cart, cartCustomShippingOptions, customShippingOption, shipping_methods, shippingMethodConfig, newMethod, methods, shipping_methods_1, shipping_methods_1_1, sm, e_11_1, _a, _b, item, e_12_1, result;
1854
- var e_11, _c, e_12, _d;
1855
- return __generator(this, function (_e) {
1856
- switch (_e.label) {
1857
- case 0: return [4 /*yield*/, this.retrieve(cartId, {
1858
- select: ["subtotal"],
1859
- relations: [
1860
- "shipping_methods",
1861
- "discounts",
1862
- "discounts.rule",
1863
- "discounts.rule.valid_for",
1864
- "shipping_methods.shipping_option",
1865
- "items",
1866
- "items.variant",
1867
- "payment_sessions",
1868
- "items.variant.product",
1869
- ],
1870
- })];
1871
- case 1:
1872
- cart = _e.sent();
1873
- return [4 /*yield*/, this.customShippingOptionService_.list({ cart_id: cart.id })];
1874
- case 2:
1875
- cartCustomShippingOptions = _e.sent();
1876
- customShippingOption = this.findCustomShippingOption(cartCustomShippingOptions, optionId);
1877
- shipping_methods = cart.shipping_methods;
1878
- shippingMethodConfig = customShippingOption
1879
- ? { cart_id: cart.id, price: customShippingOption.price }
1880
- : {
1881
- cart: cart,
1882
- };
1883
- return [4 /*yield*/, this.shippingOptionService_
1884
- .withTransaction(manager)
1885
- .createShippingMethod(optionId, data, shippingMethodConfig)];
1886
- case 3:
1887
- newMethod = _e.sent();
1888
- methods = [newMethod];
1889
- if (!shipping_methods.length) return [3 /*break*/, 12];
1890
- _e.label = 4;
1891
- case 4:
1892
- _e.trys.push([4, 10, 11, 12]);
1893
- shipping_methods_1 = __values(shipping_methods), shipping_methods_1_1 = shipping_methods_1.next();
1894
- _e.label = 5;
1895
- case 5:
1896
- if (!!shipping_methods_1_1.done) return [3 /*break*/, 9];
1897
- sm = shipping_methods_1_1.value;
1898
- if (!(sm.shipping_option.profile_id ===
1899
- newMethod.shipping_option.profile_id)) return [3 /*break*/, 7];
1900
- return [4 /*yield*/, this.shippingOptionService_
1901
- .withTransaction(manager)
1902
- .deleteShippingMethod(sm)];
1903
- case 6:
1904
- _e.sent();
1905
- return [3 /*break*/, 8];
1906
- case 7:
1907
- methods.push(sm);
1908
- _e.label = 8;
1909
- case 8:
1910
- shipping_methods_1_1 = shipping_methods_1.next();
1911
- return [3 /*break*/, 5];
1912
- case 9: return [3 /*break*/, 12];
1913
- case 10:
1914
- e_11_1 = _e.sent();
1915
- e_11 = { error: e_11_1 };
1916
- return [3 /*break*/, 12];
1917
- case 11:
1918
- try {
1919
- if (shipping_methods_1_1 && !shipping_methods_1_1.done && (_c = shipping_methods_1.return)) _c.call(shipping_methods_1);
1920
- }
1921
- finally { if (e_11) throw e_11.error; }
1922
- return [7 /*endfinally*/];
1923
- case 12:
1924
- _e.trys.push([12, 17, 18, 19]);
1925
- _a = __values(cart.items), _b = _a.next();
1926
- _e.label = 13;
1927
- case 13:
1928
- if (!!_b.done) return [3 /*break*/, 16];
1929
- item = _b.value;
1930
- return [4 /*yield*/, this.lineItemService_.withTransaction(manager).update(item.id, {
1931
- has_shipping: this.validateLineItemShipping_(methods, item),
1718
+ switch (_a.label) {
1719
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
1720
+ var cart, cartCustomShippingOptions, customShippingOption, shipping_methods, shippingMethodConfig, newShippingMethod, methods, shipping_methods_1, shipping_methods_1_1, shippingMethod, e_3_1, updatedCart;
1721
+ var e_3, _a;
1722
+ var _this = this;
1723
+ var _b;
1724
+ return __generator(this, function (_c) {
1725
+ switch (_c.label) {
1726
+ case 0: return [4 /*yield*/, this.retrieve(cartId, {
1727
+ select: ["subtotal"],
1728
+ relations: [
1729
+ "shipping_methods",
1730
+ "discounts",
1731
+ "discounts.rule",
1732
+ "shipping_methods.shipping_option",
1733
+ "items",
1734
+ "items.variant",
1735
+ "payment_sessions",
1736
+ "items.variant.product",
1737
+ ],
1932
1738
  })];
1933
- case 14:
1934
- _e.sent();
1935
- _e.label = 15;
1936
- case 15:
1937
- _b = _a.next();
1938
- return [3 /*break*/, 13];
1939
- case 16: return [3 /*break*/, 19];
1940
- case 17:
1941
- e_12_1 = _e.sent();
1942
- e_12 = { error: e_12_1 };
1943
- return [3 /*break*/, 19];
1944
- case 18:
1945
- try {
1946
- if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
1947
- }
1948
- finally { if (e_12) throw e_12.error; }
1949
- return [7 /*endfinally*/];
1950
- case 19: return [4 /*yield*/, this.retrieve(cartId, {
1951
- relations: [
1952
- "discounts",
1953
- "discounts.rule",
1954
- "discounts.rule.valid_for",
1955
- "shipping_methods",
1956
- ],
1957
- })
1958
- // if cart has freeshipping, adjust price
1959
- ];
1960
- case 20:
1961
- result = _e.sent();
1962
- if (!result.discounts.some(function (_a) {
1963
- var rule = _a.rule;
1964
- return rule.type === "free_shipping";
1965
- })) return [3 /*break*/, 22];
1966
- return [4 /*yield*/, this.adjustFreeShipping_(result, true)];
1967
- case 21:
1968
- _e.sent();
1969
- _e.label = 22;
1970
- case 22: return [4 /*yield*/, this.eventBus_
1971
- .withTransaction(manager)
1972
- .emit(CartService.Events.UPDATED, result)];
1973
- case 23:
1974
- _e.sent();
1975
- return [2 /*return*/, result];
1976
- }
1977
- });
1978
- }); }, "SERIALIZABLE")];
1739
+ case 1:
1740
+ cart = _c.sent();
1741
+ return [4 /*yield*/, this.customShippingOptionService_
1742
+ .withTransaction(transactionManager)
1743
+ .list({ cart_id: cart.id })];
1744
+ case 2:
1745
+ cartCustomShippingOptions = _c.sent();
1746
+ customShippingOption = this.findCustomShippingOption(cartCustomShippingOptions, optionId);
1747
+ shipping_methods = cart.shipping_methods;
1748
+ shippingMethodConfig = customShippingOption
1749
+ ? { cart_id: cart.id, price: customShippingOption.price }
1750
+ : { cart: cart };
1751
+ return [4 /*yield*/, this.shippingOptionService_
1752
+ .withTransaction(transactionManager)
1753
+ .createShippingMethod(optionId, data, shippingMethodConfig)];
1754
+ case 3:
1755
+ newShippingMethod = _c.sent();
1756
+ methods = [newShippingMethod];
1757
+ if (!(shipping_methods === null || shipping_methods === void 0 ? void 0 : shipping_methods.length)) return [3 /*break*/, 12];
1758
+ _c.label = 4;
1759
+ case 4:
1760
+ _c.trys.push([4, 10, 11, 12]);
1761
+ shipping_methods_1 = __values(shipping_methods), shipping_methods_1_1 = shipping_methods_1.next();
1762
+ _c.label = 5;
1763
+ case 5:
1764
+ if (!!shipping_methods_1_1.done) return [3 /*break*/, 9];
1765
+ shippingMethod = shipping_methods_1_1.value;
1766
+ if (!(shippingMethod.shipping_option.profile_id ===
1767
+ newShippingMethod.shipping_option.profile_id)) return [3 /*break*/, 7];
1768
+ return [4 /*yield*/, this.shippingOptionService_
1769
+ .withTransaction(transactionManager)
1770
+ .deleteShippingMethods(shippingMethod)];
1771
+ case 6:
1772
+ _c.sent();
1773
+ return [3 /*break*/, 8];
1774
+ case 7:
1775
+ methods.push(shippingMethod);
1776
+ _c.label = 8;
1777
+ case 8:
1778
+ shipping_methods_1_1 = shipping_methods_1.next();
1779
+ return [3 /*break*/, 5];
1780
+ case 9: return [3 /*break*/, 12];
1781
+ case 10:
1782
+ e_3_1 = _c.sent();
1783
+ e_3 = { error: e_3_1 };
1784
+ return [3 /*break*/, 12];
1785
+ case 11:
1786
+ try {
1787
+ if (shipping_methods_1_1 && !shipping_methods_1_1.done && (_a = shipping_methods_1.return)) _a.call(shipping_methods_1);
1788
+ }
1789
+ finally { if (e_3) throw e_3.error; }
1790
+ return [7 /*endfinally*/];
1791
+ case 12:
1792
+ if (!((_b = cart.items) === null || _b === void 0 ? void 0 : _b.length)) return [3 /*break*/, 14];
1793
+ return [4 /*yield*/, Promise.all(cart.items.map(function (item) { return __awaiter(_this, void 0, void 0, function () {
1794
+ return __generator(this, function (_a) {
1795
+ return [2 /*return*/, this.lineItemService_
1796
+ .withTransaction(transactionManager)
1797
+ .update(item.id, {
1798
+ has_shipping: this.validateLineItemShipping_(methods, item),
1799
+ })];
1800
+ });
1801
+ }); }))];
1802
+ case 13:
1803
+ _c.sent();
1804
+ _c.label = 14;
1805
+ case 14: return [4 /*yield*/, this.retrieve(cartId, {
1806
+ relations: ["discounts", "discounts.rule", "shipping_methods"],
1807
+ })
1808
+ // if cart has freeshipping, adjust price
1809
+ ];
1810
+ case 15:
1811
+ updatedCart = _c.sent();
1812
+ if (!updatedCart.discounts.some(function (_a) {
1813
+ var rule = _a.rule;
1814
+ return rule.type === "free_shipping";
1815
+ })) return [3 /*break*/, 17];
1816
+ return [4 /*yield*/, this.adjustFreeShipping_(updatedCart, true)];
1817
+ case 16:
1818
+ _c.sent();
1819
+ _c.label = 17;
1820
+ case 17: return [4 /*yield*/, this.eventBus_
1821
+ .withTransaction(transactionManager)
1822
+ .emit(CartService.Events.UPDATED, updatedCart)];
1823
+ case 18:
1824
+ _c.sent();
1825
+ return [2 /*return*/, updatedCart];
1826
+ }
1827
+ });
1828
+ }); }, "SERIALIZABLE")];
1829
+ case 1: return [2 /*return*/, _a.sent()];
1830
+ }
1979
1831
  });
1980
1832
  });
1981
1833
  };
@@ -1994,74 +1846,100 @@ var CartService = /** @class */ (function (_super) {
1994
1846
  }
1995
1847
  return customOption;
1996
1848
  };
1997
- /**
1998
- * Set's the region of a cart.
1999
- * @param cart - the cart to set region on
2000
- * @param regionId - the id of the region to set the region to
2001
- * @param countryCode - the country code to set the country to
2002
- * @return the result of the update operation
2003
- */
2004
- CartService.prototype.setRegion_ = function (cart, regionId, countryCode) {
1849
+ CartService.prototype.updateUnitPrices_ = function (cart, regionId, customer_id) {
1850
+ var _a;
2005
1851
  return __awaiter(this, void 0, void 0, function () {
2006
- var region, addrRepo, _a, shippingAddress, updated, updated, smRepo, newDiscounts;
1852
+ var region_1, _b;
2007
1853
  var _this = this;
2008
- return __generator(this, function (_b) {
2009
- switch (_b.label) {
1854
+ return __generator(this, function (_c) {
1855
+ switch (_c.label) {
2010
1856
  case 0:
2011
- if (cart.completed_at || cart.payment_authorized_at) {
2012
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Cannot change the region of a completed cart");
2013
- }
2014
- return [4 /*yield*/, this.regionService_.retrieve(regionId, {
1857
+ if (!((_a = cart.items) === null || _a === void 0 ? void 0 : _a.length)) return [3 /*break*/, 3];
1858
+ return [4 /*yield*/, this.regionService_
1859
+ .withTransaction(this.transactionManager_)
1860
+ .retrieve(regionId || cart.region_id, {
2015
1861
  relations: ["countries"],
2016
1862
  })];
2017
1863
  case 1:
2018
- region = _b.sent();
2019
- addrRepo = this.manager_.getCustomRepository(this.addressRepository_);
2020
- cart.region = region;
2021
- cart.region_id = region.id;
2022
- if (!cart.items.length) return [3 /*break*/, 3];
2023
- _a = cart;
2024
- return [4 /*yield*/, Promise.all(cart.items
2025
- .map(function (item) { return __awaiter(_this, void 0, void 0, function () {
1864
+ region_1 = _c.sent();
1865
+ _b = cart;
1866
+ return [4 /*yield*/, Promise.all(cart.items.map(function (item) { return __awaiter(_this, void 0, void 0, function () {
2026
1867
  var availablePrice;
2027
1868
  return __generator(this, function (_a) {
2028
1869
  switch (_a.label) {
2029
- case 0: return [4 /*yield*/, this.productVariantService_
2030
- .getRegionPrice(item.variant_id, regionId)
1870
+ case 0: return [4 /*yield*/, this.priceSelectionStrategy_
1871
+ .withTransaction(this.transactionManager_)
1872
+ .calculateVariantPrice(item.variant_id, {
1873
+ region_id: region_1.id,
1874
+ currency_code: region_1.currency_code,
1875
+ quantity: item.quantity,
1876
+ customer_id: customer_id || cart.customer_id,
1877
+ include_discount_prices: true,
1878
+ })
2031
1879
  .catch(function () { return undefined; })];
2032
1880
  case 1:
2033
1881
  availablePrice = _a.sent();
2034
- if (!(availablePrice !== undefined)) return [3 /*break*/, 2];
1882
+ if (!(availablePrice !== undefined &&
1883
+ availablePrice.calculatedPrice !== null)) return [3 /*break*/, 2];
2035
1884
  return [2 /*return*/, this.lineItemService_
2036
1885
  .withTransaction(this.transactionManager_)
2037
1886
  .update(item.id, {
2038
1887
  has_shipping: false,
2039
- unit_price: availablePrice,
1888
+ unit_price: availablePrice.calculatedPrice,
2040
1889
  })];
2041
1890
  case 2: return [4 /*yield*/, this.lineItemService_
2042
1891
  .withTransaction(this.transactionManager_)
2043
1892
  .delete(item.id)];
2044
1893
  case 3:
2045
1894
  _a.sent();
2046
- return [2 /*return*/, null];
1895
+ return [2 /*return*/];
2047
1896
  }
2048
1897
  });
2049
- }); })
2050
- .filter(Boolean))];
1898
+ }); }))];
2051
1899
  case 2:
2052
- _a.items = _b.sent();
2053
- _b.label = 3;
2054
- case 3:
1900
+ _b.items = (_c.sent()).filter(function (item) { return !!item; });
1901
+ _c.label = 3;
1902
+ case 3: return [2 /*return*/];
1903
+ }
1904
+ });
1905
+ });
1906
+ };
1907
+ /**
1908
+ * Set's the region of a cart.
1909
+ * @param cart - the cart to set region on
1910
+ * @param regionId - the id of the region to set the region to
1911
+ * @param countryCode - the country code to set the country to
1912
+ * @return the result of the update operation
1913
+ */
1914
+ CartService.prototype.setRegion_ = function (cart, regionId, countryCode) {
1915
+ return __awaiter(this, void 0, void 0, function () {
1916
+ var region, addrRepo, shippingAddress, updated, updated, paymentSessionRepo;
1917
+ return __generator(this, function (_a) {
1918
+ switch (_a.label) {
1919
+ case 0:
1920
+ if (cart.completed_at || cart.payment_authorized_at) {
1921
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Cannot change the region of a completed cart");
1922
+ }
1923
+ return [4 /*yield*/, this.regionService_
1924
+ .withTransaction(this.transactionManager_)
1925
+ .retrieve(regionId, {
1926
+ relations: ["countries"],
1927
+ })];
1928
+ case 1:
1929
+ region = _a.sent();
1930
+ addrRepo = this.transactionManager_.getCustomRepository(this.addressRepository_);
1931
+ cart.region = region;
1932
+ cart.region_id = region.id;
2055
1933
  shippingAddress = {};
2056
- if (!cart.shipping_address_id) return [3 /*break*/, 5];
1934
+ if (!cart.shipping_address_id) return [3 /*break*/, 3];
2057
1935
  return [4 /*yield*/, addrRepo.findOne({
2058
1936
  where: { id: cart.shipping_address_id },
2059
1937
  })];
2060
- case 4:
2061
- shippingAddress = (_b.sent());
2062
- _b.label = 5;
2063
- case 5:
2064
- if (!(countryCode !== null)) return [3 /*break*/, 7];
1938
+ case 2:
1939
+ shippingAddress = (_a.sent());
1940
+ _a.label = 3;
1941
+ case 3:
1942
+ if (!(countryCode !== null)) return [3 /*break*/, 5];
2065
1943
  if (!region.countries.find(function (_a) {
2066
1944
  var iso_2 = _a.iso_2;
2067
1945
  return iso_2 === countryCode.toLowerCase();
@@ -2070,10 +1948,10 @@ var CartService = /** @class */ (function (_super) {
2070
1948
  }
2071
1949
  updated = __assign(__assign({}, shippingAddress), { country_code: countryCode.toLowerCase() });
2072
1950
  return [4 /*yield*/, addrRepo.save(updated)];
2073
- case 6:
2074
- _b.sent();
2075
- return [3 /*break*/, 9];
2076
- case 7:
1951
+ case 4:
1952
+ _a.sent();
1953
+ return [3 /*break*/, 7];
1954
+ case 5:
2077
1955
  updated = __assign({}, shippingAddress);
2078
1956
  // If the country code of a shipping address is set we need to clear it
2079
1957
  if (!lodash_1.default.isEmpty(shippingAddress) && shippingAddress.country_code) {
@@ -2084,42 +1962,38 @@ var CartService = /** @class */ (function (_super) {
2084
1962
  updated = __assign(__assign({}, updated), { country_code: region.countries[0].iso_2 });
2085
1963
  }
2086
1964
  return [4 /*yield*/, this.updateShippingAddress_(cart, updated, addrRepo)];
1965
+ case 6:
1966
+ _a.sent();
1967
+ _a.label = 7;
1968
+ case 7:
1969
+ if (!(cart.shipping_methods && cart.shipping_methods.length)) return [3 /*break*/, 9];
1970
+ return [4 /*yield*/, this.shippingOptionService_
1971
+ .withTransaction(this.transactionManager_)
1972
+ .deleteShippingMethods(cart.shipping_methods)];
2087
1973
  case 8:
2088
- _b.sent();
2089
- _b.label = 9;
1974
+ _a.sent();
1975
+ _a.label = 9;
2090
1976
  case 9:
2091
- if (!(cart.shipping_methods && cart.shipping_methods.length)) return [3 /*break*/, 11];
2092
- smRepo = this.manager_.getCustomRepository(this.shippingMethodRepository_);
2093
- return [4 /*yield*/, smRepo.remove(cart.shipping_methods)];
2094
- case 10:
2095
- _b.sent();
2096
- _b.label = 11;
2097
- case 11:
2098
1977
  if (cart.discounts && cart.discounts.length) {
2099
- newDiscounts = cart.discounts.map(function (d) {
2100
- if (d.regions.find(function (_a) {
1978
+ cart.discounts = cart.discounts.filter(function (discount) {
1979
+ return discount.regions.find(function (_a) {
2101
1980
  var id = _a.id;
2102
1981
  return id === regionId;
2103
- })) {
2104
- return d;
2105
- }
2106
- return null;
1982
+ });
2107
1983
  });
2108
- cart.discounts = newDiscounts.filter(Boolean);
2109
1984
  }
2110
1985
  cart.gift_cards = [];
2111
- if (!(cart.payment_sessions && cart.payment_sessions.length)) return [3 /*break*/, 13];
2112
- return [4 /*yield*/, Promise.all(cart.payment_sessions.map(function (ps) {
2113
- return _this.paymentProviderService_
2114
- .withTransaction(_this.manager_)
2115
- .deleteSession(ps);
2116
- }))];
2117
- case 12:
2118
- _b.sent();
2119
- cart.payment_sessions = [];
1986
+ if (!(cart.payment_sessions && cart.payment_sessions.length)) return [3 /*break*/, 11];
1987
+ paymentSessionRepo = this.transactionManager_.getCustomRepository(this.paymentSessionRepository_);
1988
+ return [4 /*yield*/, paymentSessionRepo.delete({
1989
+ id: (0, typeorm_1.In)(cart.payment_sessions.map(function (paymentSession) { return paymentSession.id; })),
1990
+ })];
1991
+ case 10:
1992
+ _a.sent();
1993
+ cart.payment_sessions.length = 0;
2120
1994
  cart.payment_session = null;
2121
- _b.label = 13;
2122
- case 13: return [2 /*return*/];
1995
+ _a.label = 11;
1996
+ case 11: return [2 /*return*/];
2123
1997
  }
2124
1998
  });
2125
1999
  });
@@ -2134,7 +2008,7 @@ var CartService = /** @class */ (function (_super) {
2134
2008
  var _this = this;
2135
2009
  return __generator(this, function (_a) {
2136
2010
  switch (_a.label) {
2137
- case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
2011
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
2138
2012
  var cart, cartRepo;
2139
2013
  return __generator(this, function (_a) {
2140
2014
  switch (_a.label) {
@@ -2143,7 +2017,6 @@ var CartService = /** @class */ (function (_super) {
2143
2017
  "items",
2144
2018
  "discounts",
2145
2019
  "discounts.rule",
2146
- "discounts.rule.valid_for",
2147
2020
  "payment_sessions",
2148
2021
  ],
2149
2022
  })];
@@ -2155,7 +2028,7 @@ var CartService = /** @class */ (function (_super) {
2155
2028
  if (cart.payment_authorized_at) {
2156
2029
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Can't delete a cart with an authorized payment");
2157
2030
  }
2158
- cartRepo = manager.getCustomRepository(this.cartRepository_);
2031
+ cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
2159
2032
  return [2 /*return*/, cartRepo.remove(cart)];
2160
2033
  }
2161
2034
  });
@@ -2179,29 +2052,32 @@ var CartService = /** @class */ (function (_super) {
2179
2052
  var _this = this;
2180
2053
  return __generator(this, function (_a) {
2181
2054
  switch (_a.label) {
2182
- case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
2183
- var cartRepo, validatedId, cart, existing, result;
2055
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
2056
+ var cartRepo, validatedId, cart, existing, updatedCart;
2184
2057
  var _a;
2185
2058
  return __generator(this, function (_b) {
2186
2059
  switch (_b.label) {
2187
2060
  case 0:
2188
- cartRepo = manager.getCustomRepository(this.cartRepository_);
2061
+ cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
2189
2062
  validatedId = this.validateId_(cartId);
2190
2063
  if (typeof key !== "string") {
2191
2064
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_ARGUMENT, "Key type is invalid. Metadata keys must be strings");
2192
2065
  }
2193
2066
  return [4 /*yield*/, cartRepo.findOne(validatedId)];
2194
2067
  case 1:
2195
- cart = (_b.sent());
2068
+ cart = _b.sent();
2069
+ if (!cart) {
2070
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Unable to find the cart with the given id");
2071
+ }
2196
2072
  existing = cart.metadata || {};
2197
2073
  cart.metadata = __assign(__assign({}, existing), (_a = {}, _a[key] = value, _a));
2198
2074
  return [4 /*yield*/, cartRepo.save(cart)];
2199
2075
  case 2:
2200
- result = _b.sent();
2076
+ updatedCart = _b.sent();
2201
2077
  this.eventBus_
2202
- .withTransaction(manager)
2203
- .emit(CartService.Events.UPDATED, result);
2204
- return [2 /*return*/, result];
2078
+ .withTransaction(transactionManager)
2079
+ .emit(CartService.Events.UPDATED, updatedCart);
2080
+ return [2 /*return*/, updatedCart];
2205
2081
  }
2206
2082
  });
2207
2083
  }); })];
@@ -2214,34 +2090,70 @@ var CartService = /** @class */ (function (_super) {
2214
2090
  return __awaiter(this, void 0, void 0, function () {
2215
2091
  var _this = this;
2216
2092
  return __generator(this, function (_a) {
2217
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
2218
- var cart, calculationContext;
2219
- return __generator(this, function (_a) {
2220
- switch (_a.label) {
2221
- case 0: return [4 /*yield*/, this.retrieve(id, {
2222
- relations: [
2223
- "items",
2224
- "gift_cards",
2225
- "discounts",
2226
- "discounts.rule",
2227
- "discounts.rule.valid_for",
2228
- "shipping_methods",
2229
- "region",
2230
- "region.tax_rates",
2231
- ],
2232
- })];
2233
- case 1:
2234
- cart = _a.sent();
2235
- calculationContext = this.totalsService_.getCalculationContext(cart);
2236
- return [4 /*yield*/, this.taxProviderService_
2237
- .withTransaction(manager)
2238
- .createTaxLines(cart, calculationContext)];
2239
- case 2:
2240
- _a.sent();
2241
- return [2 /*return*/, cart];
2242
- }
2243
- });
2244
- }); })];
2093
+ switch (_a.label) {
2094
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
2095
+ var cart, calculationContext;
2096
+ return __generator(this, function (_a) {
2097
+ switch (_a.label) {
2098
+ case 0: return [4 /*yield*/, this.retrieve(id, {
2099
+ relations: [
2100
+ "items",
2101
+ "gift_cards",
2102
+ "discounts",
2103
+ "discounts.rule",
2104
+ "shipping_methods",
2105
+ "region",
2106
+ "region.tax_rates",
2107
+ ],
2108
+ })];
2109
+ case 1:
2110
+ cart = _a.sent();
2111
+ calculationContext = this.totalsService_
2112
+ .withTransaction(transactionManager)
2113
+ .getCalculationContext(cart);
2114
+ return [4 /*yield*/, this.taxProviderService_
2115
+ .withTransaction(transactionManager)
2116
+ .createTaxLines(cart, calculationContext)];
2117
+ case 2:
2118
+ _a.sent();
2119
+ return [2 /*return*/, cart];
2120
+ }
2121
+ });
2122
+ }); })];
2123
+ case 1: return [2 /*return*/, _a.sent()];
2124
+ }
2125
+ });
2126
+ });
2127
+ };
2128
+ CartService.prototype.refreshAdjustments_ = function (cart) {
2129
+ return __awaiter(this, void 0, void 0, function () {
2130
+ var nonReturnLineIDs;
2131
+ return __generator(this, function (_a) {
2132
+ switch (_a.label) {
2133
+ case 0:
2134
+ nonReturnLineIDs = cart.items
2135
+ .filter(function (item) { return !item.is_return; })
2136
+ .map(function (i) { return i.id; });
2137
+ // delete all old non return line item adjustments
2138
+ return [4 /*yield*/, this.lineItemAdjustmentService_
2139
+ .withTransaction(this.transactionManager_)
2140
+ .delete({
2141
+ item_id: nonReturnLineIDs,
2142
+ })
2143
+ // potentially create/update line item adjustments
2144
+ ];
2145
+ case 1:
2146
+ // delete all old non return line item adjustments
2147
+ _a.sent();
2148
+ // potentially create/update line item adjustments
2149
+ return [4 /*yield*/, this.lineItemAdjustmentService_
2150
+ .withTransaction(this.transactionManager_)
2151
+ .createAdjustments(cart)];
2152
+ case 2:
2153
+ // potentially create/update line item adjustments
2154
+ _a.sent();
2155
+ return [2 /*return*/];
2156
+ }
2245
2157
  });
2246
2158
  });
2247
2159
  };
@@ -2255,35 +2167,38 @@ var CartService = /** @class */ (function (_super) {
2255
2167
  return __awaiter(this, void 0, void 0, function () {
2256
2168
  var _this = this;
2257
2169
  return __generator(this, function (_a) {
2258
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
2259
- var cartRepo, validatedId, cart, updated, result;
2260
- return __generator(this, function (_a) {
2261
- switch (_a.label) {
2262
- case 0:
2263
- cartRepo = manager.getCustomRepository(this.cartRepository_);
2264
- validatedId = this.validateId_(cartId);
2265
- if (typeof key !== "string") {
2266
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_ARGUMENT, "Key type is invalid. Metadata keys must be strings");
2267
- }
2268
- return [4 /*yield*/, cartRepo.findOne(validatedId)];
2269
- case 1:
2270
- cart = _a.sent();
2271
- if (!cart) {
2272
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Cart with id: ".concat(validatedId, " was not found"));
2273
- }
2274
- updated = cart.metadata || {};
2275
- delete updated[key];
2276
- cart.metadata = updated;
2277
- return [4 /*yield*/, cartRepo.save(cart)];
2278
- case 2:
2279
- result = _a.sent();
2280
- this.eventBus_
2281
- .withTransaction(manager)
2282
- .emit(CartService.Events.UPDATED, result);
2283
- return [2 /*return*/, result];
2284
- }
2285
- });
2286
- }); })];
2170
+ switch (_a.label) {
2171
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
2172
+ var cartRepo, validatedId, cart, updated, updatedCart;
2173
+ return __generator(this, function (_a) {
2174
+ switch (_a.label) {
2175
+ case 0:
2176
+ cartRepo = transactionManager.getCustomRepository(this.cartRepository_);
2177
+ validatedId = this.validateId_(cartId);
2178
+ if (typeof key !== "string") {
2179
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_ARGUMENT, "Key type is invalid. Metadata keys must be strings");
2180
+ }
2181
+ return [4 /*yield*/, cartRepo.findOne(validatedId)];
2182
+ case 1:
2183
+ cart = _a.sent();
2184
+ if (!cart) {
2185
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Cart with id: ".concat(validatedId, " was not found"));
2186
+ }
2187
+ updated = cart.metadata || {};
2188
+ delete updated[key];
2189
+ cart.metadata = updated;
2190
+ return [4 /*yield*/, cartRepo.save(cart)];
2191
+ case 2:
2192
+ updatedCart = _a.sent();
2193
+ this.eventBus_
2194
+ .withTransaction(transactionManager)
2195
+ .emit(CartService.Events.UPDATED, updatedCart);
2196
+ return [2 /*return*/, updatedCart];
2197
+ }
2198
+ });
2199
+ }); })];
2200
+ case 1: return [2 /*return*/, _a.sent()];
2201
+ }
2287
2202
  });
2288
2203
  });
2289
2204
  };