@medusajs/medusa 1.3.4 → 2.0.0-next.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 (502) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +27 -0
  3. package/dist/api/middlewares/await-middleware.js +7 -0
  4. package/dist/api/middlewares/await-middleware.js.map +1 -1
  5. package/dist/api/middlewares/error-handler.d.ts +1 -1
  6. package/dist/api/middlewares/error-handler.js.map +1 -1
  7. package/dist/api/middlewares/transform-query.d.ts +1 -1
  8. package/dist/api/middlewares/transform-query.js +2 -0
  9. package/dist/api/middlewares/transform-query.js.map +1 -1
  10. package/dist/api/middlewares/validators/product-existence.d.ts +3 -0
  11. package/dist/api/middlewares/validators/product-existence.js +86 -0
  12. package/dist/api/middlewares/validators/product-existence.js.map +1 -0
  13. package/dist/api/middlewares/validators/sales-channel-existence.d.ts +3 -0
  14. package/dist/api/middlewares/validators/sales-channel-existence.js +86 -0
  15. package/dist/api/middlewares/validators/sales-channel-existence.js.map +1 -0
  16. package/dist/api/routes/admin/auth/create-session.js +10 -2
  17. package/dist/api/routes/admin/auth/create-session.js.map +1 -1
  18. package/dist/api/routes/admin/batch/cancel-batch-job.js +12 -2
  19. package/dist/api/routes/admin/batch/cancel-batch-job.js.map +1 -1
  20. package/dist/api/routes/admin/batch/confirm-batch-job.js +12 -2
  21. package/dist/api/routes/admin/batch/confirm-batch-job.js.map +1 -1
  22. package/dist/api/routes/admin/batch/create-batch-job.js +10 -2
  23. package/dist/api/routes/admin/batch/create-batch-job.js.map +1 -1
  24. package/dist/api/routes/admin/collections/add-products.js +10 -2
  25. package/dist/api/routes/admin/collections/add-products.js.map +1 -1
  26. package/dist/api/routes/admin/collections/create-collection.js +10 -2
  27. package/dist/api/routes/admin/collections/create-collection.js.map +1 -1
  28. package/dist/api/routes/admin/collections/delete-collection.js +10 -2
  29. package/dist/api/routes/admin/collections/delete-collection.js.map +1 -1
  30. package/dist/api/routes/admin/collections/remove-products.js +10 -2
  31. package/dist/api/routes/admin/collections/remove-products.js.map +1 -1
  32. package/dist/api/routes/admin/collections/update-collection.js +10 -2
  33. package/dist/api/routes/admin/collections/update-collection.js.map +1 -1
  34. package/dist/api/routes/admin/customer-groups/add-customers-batch.js +15 -5
  35. package/dist/api/routes/admin/customer-groups/add-customers-batch.js.map +1 -1
  36. package/dist/api/routes/admin/customer-groups/create-customer-group.js +12 -2
  37. package/dist/api/routes/admin/customer-groups/create-customer-group.js.map +1 -1
  38. package/dist/api/routes/admin/customer-groups/delete-customer-group.js +12 -2
  39. package/dist/api/routes/admin/customer-groups/delete-customer-group.js.map +1 -1
  40. package/dist/api/routes/admin/customer-groups/delete-customers-batch.js +15 -5
  41. package/dist/api/routes/admin/customer-groups/delete-customers-batch.js.map +1 -1
  42. package/dist/api/routes/admin/customer-groups/update-customer-group.js +12 -2
  43. package/dist/api/routes/admin/customer-groups/update-customer-group.js.map +1 -1
  44. package/dist/api/routes/admin/customers/create-customer.js +12 -2
  45. package/dist/api/routes/admin/customers/create-customer.js.map +1 -1
  46. package/dist/api/routes/admin/customers/update-customer.js +12 -2
  47. package/dist/api/routes/admin/customers/update-customer.js.map +1 -1
  48. package/dist/api/routes/admin/discounts/add-region.js +12 -2
  49. package/dist/api/routes/admin/discounts/add-region.js.map +1 -1
  50. package/dist/api/routes/admin/discounts/create-condition.d.ts +1 -1
  51. package/dist/api/routes/admin/discounts/create-condition.js +13 -3
  52. package/dist/api/routes/admin/discounts/create-condition.js.map +1 -1
  53. package/dist/api/routes/admin/discounts/create-discount.d.ts +1 -2
  54. package/dist/api/routes/admin/discounts/create-discount.js +12 -3
  55. package/dist/api/routes/admin/discounts/create-discount.js.map +1 -1
  56. package/dist/api/routes/admin/discounts/create-dynamic-code.js +12 -2
  57. package/dist/api/routes/admin/discounts/create-dynamic-code.js.map +1 -1
  58. package/dist/api/routes/admin/discounts/delete-condition.js +12 -2
  59. package/dist/api/routes/admin/discounts/delete-condition.js.map +1 -1
  60. package/dist/api/routes/admin/discounts/delete-discount.js +12 -2
  61. package/dist/api/routes/admin/discounts/delete-discount.js.map +1 -1
  62. package/dist/api/routes/admin/discounts/delete-dynamic-code.js +12 -2
  63. package/dist/api/routes/admin/discounts/delete-dynamic-code.js.map +1 -1
  64. package/dist/api/routes/admin/discounts/remove-region.js +12 -2
  65. package/dist/api/routes/admin/discounts/remove-region.js.map +1 -1
  66. package/dist/api/routes/admin/discounts/update-condition.js +12 -2
  67. package/dist/api/routes/admin/discounts/update-condition.js.map +1 -1
  68. package/dist/api/routes/admin/discounts/update-discount.d.ts +1 -1
  69. package/dist/api/routes/admin/discounts/update-discount.js +13 -3
  70. package/dist/api/routes/admin/discounts/update-discount.js.map +1 -1
  71. package/dist/api/routes/admin/draft-orders/create-draft-order.js +12 -2
  72. package/dist/api/routes/admin/draft-orders/create-draft-order.js.map +1 -1
  73. package/dist/api/routes/admin/draft-orders/create-line-item.js +4 -2
  74. package/dist/api/routes/admin/draft-orders/create-line-item.js.map +1 -1
  75. package/dist/api/routes/admin/draft-orders/delete-draft-order.js +12 -2
  76. package/dist/api/routes/admin/draft-orders/delete-draft-order.js.map +1 -1
  77. package/dist/api/routes/admin/draft-orders/register-payment.js +6 -3
  78. package/dist/api/routes/admin/draft-orders/register-payment.js.map +1 -1
  79. package/dist/api/routes/admin/draft-orders/update-draft-order.js +28 -12
  80. package/dist/api/routes/admin/draft-orders/update-draft-order.js.map +1 -1
  81. package/dist/api/routes/admin/gift-cards/create-gift-card.js +10 -2
  82. package/dist/api/routes/admin/gift-cards/create-gift-card.js.map +1 -1
  83. package/dist/api/routes/admin/gift-cards/delete-gift-card.js +10 -2
  84. package/dist/api/routes/admin/gift-cards/delete-gift-card.js.map +1 -1
  85. package/dist/api/routes/admin/gift-cards/update-gift-card.js +12 -2
  86. package/dist/api/routes/admin/gift-cards/update-gift-card.js.map +1 -1
  87. package/dist/api/routes/admin/invites/accept-invite.js +12 -2
  88. package/dist/api/routes/admin/invites/accept-invite.js.map +1 -1
  89. package/dist/api/routes/admin/invites/create-invite.js +12 -2
  90. package/dist/api/routes/admin/invites/create-invite.js.map +1 -1
  91. package/dist/api/routes/admin/invites/delete-invite.js +12 -2
  92. package/dist/api/routes/admin/invites/delete-invite.js.map +1 -1
  93. package/dist/api/routes/admin/invites/resend-invite.js +12 -2
  94. package/dist/api/routes/admin/invites/resend-invite.js.map +1 -1
  95. package/dist/api/routes/admin/notes/create-note.js +15 -7
  96. package/dist/api/routes/admin/notes/create-note.js.map +1 -1
  97. package/dist/api/routes/admin/notes/delete-note.js +10 -2
  98. package/dist/api/routes/admin/notes/delete-note.js.map +1 -1
  99. package/dist/api/routes/admin/notes/update-note.js +12 -2
  100. package/dist/api/routes/admin/notes/update-note.js.map +1 -1
  101. package/dist/api/routes/admin/notifications/list-notifications.js +8 -7
  102. package/dist/api/routes/admin/notifications/list-notifications.js.map +1 -1
  103. package/dist/api/routes/admin/notifications/resend-notification.js +12 -2
  104. package/dist/api/routes/admin/notifications/resend-notification.js.map +1 -1
  105. package/dist/api/routes/admin/orders/add-shipping-method.js +14 -4
  106. package/dist/api/routes/admin/orders/add-shipping-method.js.map +1 -1
  107. package/dist/api/routes/admin/orders/archive-order.js +10 -2
  108. package/dist/api/routes/admin/orders/archive-order.js.map +1 -1
  109. package/dist/api/routes/admin/orders/cancel-claim.js +12 -2
  110. package/dist/api/routes/admin/orders/cancel-claim.js.map +1 -1
  111. package/dist/api/routes/admin/orders/cancel-fulfillment-claim.js +12 -2
  112. package/dist/api/routes/admin/orders/cancel-fulfillment-claim.js.map +1 -1
  113. package/dist/api/routes/admin/orders/cancel-fulfillment-swap.js +12 -2
  114. package/dist/api/routes/admin/orders/cancel-fulfillment-swap.js.map +1 -1
  115. package/dist/api/routes/admin/orders/cancel-fulfillment.js +12 -2
  116. package/dist/api/routes/admin/orders/cancel-fulfillment.js.map +1 -1
  117. package/dist/api/routes/admin/orders/cancel-order.js +10 -2
  118. package/dist/api/routes/admin/orders/cancel-order.js.map +1 -1
  119. package/dist/api/routes/admin/orders/cancel-swap.js +10 -2
  120. package/dist/api/routes/admin/orders/cancel-swap.js.map +1 -1
  121. package/dist/api/routes/admin/orders/capture-payment.js +12 -2
  122. package/dist/api/routes/admin/orders/capture-payment.js.map +1 -1
  123. package/dist/api/routes/admin/orders/complete-order.js +12 -2
  124. package/dist/api/routes/admin/orders/complete-order.js.map +1 -1
  125. package/dist/api/routes/admin/orders/create-claim-shipment.js +18 -8
  126. package/dist/api/routes/admin/orders/create-claim-shipment.js.map +1 -1
  127. package/dist/api/routes/admin/orders/create-claim.js +199 -140
  128. package/dist/api/routes/admin/orders/create-claim.js.map +1 -1
  129. package/dist/api/routes/admin/orders/create-fulfillment.d.ts +1 -1
  130. package/dist/api/routes/admin/orders/create-fulfillment.js +15 -5
  131. package/dist/api/routes/admin/orders/create-fulfillment.js.map +1 -1
  132. package/dist/api/routes/admin/orders/create-shipment.js +23 -8
  133. package/dist/api/routes/admin/orders/create-shipment.js.map +1 -1
  134. package/dist/api/routes/admin/orders/create-swap-shipment.js +16 -8
  135. package/dist/api/routes/admin/orders/create-swap-shipment.js.map +1 -1
  136. package/dist/api/routes/admin/orders/create-swap.js +150 -101
  137. package/dist/api/routes/admin/orders/create-swap.js.map +1 -1
  138. package/dist/api/routes/admin/orders/fulfill-swap.js +8 -9
  139. package/dist/api/routes/admin/orders/fulfill-swap.js.map +1 -1
  140. package/dist/api/routes/admin/orders/index.d.ts +1 -3
  141. package/dist/api/routes/admin/orders/index.js +0 -10
  142. package/dist/api/routes/admin/orders/index.js.map +1 -1
  143. package/dist/api/routes/admin/orders/list-orders.d.ts +1 -0
  144. package/dist/api/routes/admin/orders/list-orders.js +1 -0
  145. package/dist/api/routes/admin/orders/list-orders.js.map +1 -1
  146. package/dist/api/routes/admin/orders/refund-payment.js +14 -4
  147. package/dist/api/routes/admin/orders/refund-payment.js.map +1 -1
  148. package/dist/api/routes/admin/orders/request-return.js +180 -131
  149. package/dist/api/routes/admin/orders/request-return.js.map +1 -1
  150. package/dist/api/routes/admin/orders/update-claim.js +12 -2
  151. package/dist/api/routes/admin/orders/update-claim.js.map +1 -1
  152. package/dist/api/routes/admin/orders/update-order.d.ts +9 -9
  153. package/dist/api/routes/admin/orders/update-order.js +16 -6
  154. package/dist/api/routes/admin/orders/update-order.js.map +1 -1
  155. package/dist/api/routes/admin/price-lists/add-prices-batch.js +12 -2
  156. package/dist/api/routes/admin/price-lists/add-prices-batch.js.map +1 -1
  157. package/dist/api/routes/admin/price-lists/create-price-list.js +12 -2
  158. package/dist/api/routes/admin/price-lists/create-price-list.js.map +1 -1
  159. package/dist/api/routes/admin/price-lists/delete-price-list.js +10 -2
  160. package/dist/api/routes/admin/price-lists/delete-price-list.js.map +1 -1
  161. package/dist/api/routes/admin/price-lists/delete-prices-batch.js +12 -2
  162. package/dist/api/routes/admin/price-lists/delete-prices-batch.js.map +1 -1
  163. package/dist/api/routes/admin/price-lists/delete-product-prices.js +12 -4
  164. package/dist/api/routes/admin/price-lists/delete-product-prices.js.map +1 -1
  165. package/dist/api/routes/admin/price-lists/delete-variant-prices.js +12 -4
  166. package/dist/api/routes/admin/price-lists/delete-variant-prices.js.map +1 -1
  167. package/dist/api/routes/admin/price-lists/list-price-list-products.d.ts +2 -2
  168. package/dist/api/routes/admin/price-lists/list-price-list-products.js +1 -2
  169. package/dist/api/routes/admin/price-lists/list-price-list-products.js.map +1 -1
  170. package/dist/api/routes/admin/price-lists/list-price-lists.js.map +1 -1
  171. package/dist/api/routes/admin/price-lists/update-price-list.js +12 -2
  172. package/dist/api/routes/admin/price-lists/update-price-list.js.map +1 -1
  173. package/dist/api/routes/admin/products/add-option.js +12 -2
  174. package/dist/api/routes/admin/products/add-option.js.map +1 -1
  175. package/dist/api/routes/admin/products/create-product.d.ts +10 -8
  176. package/dist/api/routes/admin/products/create-product.js +36 -37
  177. package/dist/api/routes/admin/products/create-product.js.map +1 -1
  178. package/dist/api/routes/admin/products/create-variant.js +12 -2
  179. package/dist/api/routes/admin/products/create-variant.js.map +1 -1
  180. package/dist/api/routes/admin/products/delete-option.js +12 -2
  181. package/dist/api/routes/admin/products/delete-option.js.map +1 -1
  182. package/dist/api/routes/admin/products/delete-product.js +10 -2
  183. package/dist/api/routes/admin/products/delete-product.js.map +1 -1
  184. package/dist/api/routes/admin/products/delete-variant.js +14 -2
  185. package/dist/api/routes/admin/products/delete-variant.js.map +1 -1
  186. package/dist/api/routes/admin/products/index.d.ts +2 -2
  187. package/dist/api/routes/admin/products/index.js +6 -31
  188. package/dist/api/routes/admin/products/index.js.map +1 -1
  189. package/dist/api/routes/admin/products/list-products.d.ts +3 -20
  190. package/dist/api/routes/admin/products/list-products.js +15 -103
  191. package/dist/api/routes/admin/products/list-products.js.map +1 -1
  192. package/dist/api/routes/admin/products/set-metadata.js +18 -10
  193. package/dist/api/routes/admin/products/set-metadata.js.map +1 -1
  194. package/dist/api/routes/admin/products/update-option.js +12 -2
  195. package/dist/api/routes/admin/products/update-option.js.map +1 -1
  196. package/dist/api/routes/admin/products/update-product.d.ts +10 -8
  197. package/dist/api/routes/admin/products/update-product.js +40 -33
  198. package/dist/api/routes/admin/products/update-product.js.map +1 -1
  199. package/dist/api/routes/admin/products/update-variant.js +14 -2
  200. package/dist/api/routes/admin/products/update-variant.js.map +1 -1
  201. package/dist/api/routes/admin/regions/add-country.js +12 -2
  202. package/dist/api/routes/admin/regions/add-country.js.map +1 -1
  203. package/dist/api/routes/admin/regions/add-fulfillment-provider.js +12 -2
  204. package/dist/api/routes/admin/regions/add-fulfillment-provider.js.map +1 -1
  205. package/dist/api/routes/admin/regions/add-payment-provider.js +12 -2
  206. package/dist/api/routes/admin/regions/add-payment-provider.js.map +1 -1
  207. package/dist/api/routes/admin/regions/create-region.js +12 -2
  208. package/dist/api/routes/admin/regions/create-region.js.map +1 -1
  209. package/dist/api/routes/admin/regions/delete-region.js +12 -2
  210. package/dist/api/routes/admin/regions/delete-region.js.map +1 -1
  211. package/dist/api/routes/admin/regions/index.d.ts +0 -1
  212. package/dist/api/routes/admin/regions/index.js +0 -9
  213. package/dist/api/routes/admin/regions/index.js.map +1 -1
  214. package/dist/api/routes/admin/regions/remove-country.js +12 -2
  215. package/dist/api/routes/admin/regions/remove-country.js.map +1 -1
  216. package/dist/api/routes/admin/regions/remove-fulfillment-provider.js +12 -2
  217. package/dist/api/routes/admin/regions/remove-fulfillment-provider.js.map +1 -1
  218. package/dist/api/routes/admin/regions/remove-payment-provider.js +12 -2
  219. package/dist/api/routes/admin/regions/remove-payment-provider.js.map +1 -1
  220. package/dist/api/routes/admin/regions/update-region.js +12 -2
  221. package/dist/api/routes/admin/regions/update-region.js.map +1 -1
  222. package/dist/api/routes/admin/return-reasons/create-reason.js +12 -2
  223. package/dist/api/routes/admin/return-reasons/create-reason.js.map +1 -1
  224. package/dist/api/routes/admin/return-reasons/delete-reason.js +12 -2
  225. package/dist/api/routes/admin/return-reasons/delete-reason.js.map +1 -1
  226. package/dist/api/routes/admin/return-reasons/update-reason.js +12 -2
  227. package/dist/api/routes/admin/return-reasons/update-reason.js.map +1 -1
  228. package/dist/api/routes/admin/returns/cancel-return.js +10 -2
  229. package/dist/api/routes/admin/returns/cancel-return.js.map +1 -1
  230. package/dist/api/routes/admin/sales-channels/add-product-batch.d.ts +28 -0
  231. package/dist/api/routes/admin/sales-channels/add-product-batch.js +111 -0
  232. package/dist/api/routes/admin/sales-channels/add-product-batch.js.map +1 -0
  233. package/dist/api/routes/admin/sales-channels/create-sales-channel.d.ts +2 -1
  234. package/dist/api/routes/admin/sales-channels/create-sales-channel.js +19 -3
  235. package/dist/api/routes/admin/sales-channels/create-sales-channel.js.map +1 -1
  236. package/dist/api/routes/admin/sales-channels/delete-products-batch.d.ts +28 -0
  237. package/dist/api/routes/admin/sales-channels/delete-products-batch.js +111 -0
  238. package/dist/api/routes/admin/sales-channels/delete-products-batch.js.map +1 -0
  239. package/dist/api/routes/admin/sales-channels/delete-sales-channel.js +12 -2
  240. package/dist/api/routes/admin/sales-channels/delete-sales-channel.js.map +1 -1
  241. package/dist/api/routes/admin/sales-channels/index.d.ts +7 -3
  242. package/dist/api/routes/admin/sales-channels/index.js +18 -8
  243. package/dist/api/routes/admin/sales-channels/index.js.map +1 -1
  244. package/dist/api/routes/admin/sales-channels/list-sales-channels.d.ts +57 -0
  245. package/dist/api/routes/admin/sales-channels/list-sales-channels.js +197 -0
  246. package/dist/api/routes/admin/sales-channels/list-sales-channels.js.map +1 -0
  247. package/dist/api/routes/admin/sales-channels/update-sales-channel.js +12 -2
  248. package/dist/api/routes/admin/sales-channels/update-sales-channel.js.map +1 -1
  249. package/dist/api/routes/admin/shipping-options/create-shipping-option.js +13 -2
  250. package/dist/api/routes/admin/shipping-options/create-shipping-option.js.map +1 -1
  251. package/dist/api/routes/admin/shipping-options/delete-shipping-option.js +12 -2
  252. package/dist/api/routes/admin/shipping-options/delete-shipping-option.js.map +1 -1
  253. package/dist/api/routes/admin/shipping-options/update-shipping-option.js +12 -2
  254. package/dist/api/routes/admin/shipping-options/update-shipping-option.js.map +1 -1
  255. package/dist/api/routes/admin/shipping-profiles/create-shipping-profile.js +12 -2
  256. package/dist/api/routes/admin/shipping-profiles/create-shipping-profile.js.map +1 -1
  257. package/dist/api/routes/admin/shipping-profiles/delete-shipping-profile.js +12 -2
  258. package/dist/api/routes/admin/shipping-profiles/delete-shipping-profile.js.map +1 -1
  259. package/dist/api/routes/admin/shipping-profiles/update-shipping-profile.js +12 -2
  260. package/dist/api/routes/admin/shipping-profiles/update-shipping-profile.js.map +1 -1
  261. package/dist/api/routes/admin/store/add-currency.js +12 -2
  262. package/dist/api/routes/admin/store/add-currency.js.map +1 -1
  263. package/dist/api/routes/admin/store/get-store.js +6 -2
  264. package/dist/api/routes/admin/store/get-store.js.map +1 -1
  265. package/dist/api/routes/admin/store/remove-currency.js +12 -2
  266. package/dist/api/routes/admin/store/remove-currency.js.map +1 -1
  267. package/dist/api/routes/admin/store/update-store.js +12 -2
  268. package/dist/api/routes/admin/store/update-store.js.map +1 -1
  269. package/dist/api/routes/admin/tax-rates/add-to-product-types.js +12 -2
  270. package/dist/api/routes/admin/tax-rates/add-to-product-types.js.map +1 -1
  271. package/dist/api/routes/admin/tax-rates/add-to-products.js +12 -2
  272. package/dist/api/routes/admin/tax-rates/add-to-products.js.map +1 -1
  273. package/dist/api/routes/admin/tax-rates/add-to-shipping-options.js +12 -2
  274. package/dist/api/routes/admin/tax-rates/add-to-shipping-options.js.map +1 -1
  275. package/dist/api/routes/admin/tax-rates/delete-tax-rate.js +10 -2
  276. package/dist/api/routes/admin/tax-rates/delete-tax-rate.js.map +1 -1
  277. package/dist/api/routes/admin/tax-rates/remove-from-product-types.js +12 -2
  278. package/dist/api/routes/admin/tax-rates/remove-from-product-types.js.map +1 -1
  279. package/dist/api/routes/admin/tax-rates/remove-from-products.js +12 -2
  280. package/dist/api/routes/admin/tax-rates/remove-from-products.js.map +1 -1
  281. package/dist/api/routes/admin/tax-rates/remove-from-shipping-options.js +12 -2
  282. package/dist/api/routes/admin/tax-rates/remove-from-shipping-options.js.map +1 -1
  283. package/dist/api/routes/admin/users/create-user.js +12 -2
  284. package/dist/api/routes/admin/users/create-user.js.map +1 -1
  285. package/dist/api/routes/admin/users/delete-user.js +10 -2
  286. package/dist/api/routes/admin/users/delete-user.js.map +1 -1
  287. package/dist/api/routes/admin/users/reset-password-token.js +12 -4
  288. package/dist/api/routes/admin/users/reset-password-token.js.map +1 -1
  289. package/dist/api/routes/admin/users/reset-password.js +17 -8
  290. package/dist/api/routes/admin/users/reset-password.js.map +1 -1
  291. package/dist/api/routes/admin/users/update-user.js +12 -2
  292. package/dist/api/routes/admin/users/update-user.js.map +1 -1
  293. package/dist/api/routes/store/auth/create-session.js +12 -2
  294. package/dist/api/routes/store/auth/create-session.js.map +1 -1
  295. package/dist/api/routes/store/carts/calculate-taxes.js +84 -49
  296. package/dist/api/routes/store/carts/calculate-taxes.js.map +1 -1
  297. package/dist/api/routes/store/carts/complete-cart.js +10 -2
  298. package/dist/api/routes/store/carts/complete-cart.js.map +1 -1
  299. package/dist/api/routes/store/carts/create-cart.d.ts +4 -0
  300. package/dist/api/routes/store/carts/create-cart.js +72 -85
  301. package/dist/api/routes/store/carts/create-cart.js.map +1 -1
  302. package/dist/api/routes/store/carts/create-line-item.js +6 -3
  303. package/dist/api/routes/store/carts/create-line-item.js.map +1 -1
  304. package/dist/api/routes/store/carts/create-payment-sessions.js +10 -2
  305. package/dist/api/routes/store/carts/create-payment-sessions.js.map +1 -1
  306. package/dist/api/routes/store/carts/decorate-line-items-with-totals.js +20 -10
  307. package/dist/api/routes/store/carts/decorate-line-items-with-totals.js.map +1 -1
  308. package/dist/api/routes/store/carts/delete-payment-session.js +10 -2
  309. package/dist/api/routes/store/carts/delete-payment-session.js.map +1 -1
  310. package/dist/api/routes/store/carts/index.js +6 -30
  311. package/dist/api/routes/store/carts/index.js.map +1 -1
  312. package/dist/api/routes/store/carts/refresh-payment-session.js +10 -2
  313. package/dist/api/routes/store/carts/refresh-payment-session.js.map +1 -1
  314. package/dist/api/routes/store/carts/set-payment-session.js +11 -3
  315. package/dist/api/routes/store/carts/set-payment-session.js.map +1 -1
  316. package/dist/api/routes/store/carts/update-cart.d.ts +4 -0
  317. package/dist/api/routes/store/carts/update-cart.js +49 -64
  318. package/dist/api/routes/store/carts/update-cart.js.map +1 -1
  319. package/dist/api/routes/store/carts/update-line-item.js +1 -1
  320. package/dist/api/routes/store/carts/update-line-item.js.map +1 -1
  321. package/dist/api/routes/store/carts/update-payment-session.js +17 -7
  322. package/dist/api/routes/store/carts/update-payment-session.js.map +1 -1
  323. package/dist/api/routes/store/customers/create-address.js +13 -3
  324. package/dist/api/routes/store/customers/create-address.js.map +1 -1
  325. package/dist/api/routes/store/customers/create-customer.js +12 -2
  326. package/dist/api/routes/store/customers/create-customer.js.map +1 -1
  327. package/dist/api/routes/store/customers/delete-address.js +12 -2
  328. package/dist/api/routes/store/customers/delete-address.js.map +1 -1
  329. package/dist/api/routes/store/customers/list-orders.js.map +1 -1
  330. package/dist/api/routes/store/customers/reset-password-token.js +12 -4
  331. package/dist/api/routes/store/customers/reset-password-token.js.map +1 -1
  332. package/dist/api/routes/store/customers/reset-password.js +14 -4
  333. package/dist/api/routes/store/customers/reset-password.js.map +1 -1
  334. package/dist/api/routes/store/customers/update-address.js +12 -2
  335. package/dist/api/routes/store/customers/update-address.js.map +1 -1
  336. package/dist/api/routes/store/customers/update-customer.js +12 -2
  337. package/dist/api/routes/store/customers/update-customer.js.map +1 -1
  338. package/dist/api/routes/store/orders/index.d.ts +1 -1
  339. package/dist/api/routes/store/orders/index.js.map +1 -1
  340. package/dist/api/routes/store/returns/create-return.js +145 -100
  341. package/dist/api/routes/store/returns/create-return.js.map +1 -1
  342. package/dist/api/routes/store/shipping-options/list-shipping-options.js +7 -1
  343. package/dist/api/routes/store/shipping-options/list-shipping-options.js.map +1 -1
  344. package/dist/api/routes/store/swaps/create-swap.js +148 -99
  345. package/dist/api/routes/store/swaps/create-swap.js.map +1 -1
  346. package/dist/interfaces/base-repository.d.ts +3 -0
  347. package/dist/interfaces/base-repository.js +28 -0
  348. package/dist/interfaces/base-repository.js.map +1 -0
  349. package/dist/interfaces/index.d.ts +3 -0
  350. package/dist/interfaces/index.js +3 -0
  351. package/dist/interfaces/index.js.map +1 -1
  352. package/dist/interfaces/notification-service.d.ts +18 -0
  353. package/dist/interfaces/notification-service.js +40 -0
  354. package/dist/interfaces/notification-service.js.map +1 -0
  355. package/dist/interfaces/search-service.d.ts +78 -0
  356. package/dist/interfaces/search-service.js +42 -0
  357. package/dist/interfaces/search-service.js.map +1 -0
  358. package/dist/loaders/index.js +1 -1
  359. package/dist/loaders/index.js.map +1 -1
  360. package/dist/loaders/plugins.js +10 -12
  361. package/dist/loaders/plugins.js.map +1 -1
  362. package/dist/loaders/search-index.d.ts +1 -0
  363. package/dist/loaders/search-index.js +7 -64
  364. package/dist/loaders/search-index.js.map +1 -1
  365. package/dist/models/index.d.ts +1 -0
  366. package/dist/models/index.js +1 -0
  367. package/dist/models/index.js.map +1 -1
  368. package/dist/models/notification.d.ts +1 -1
  369. package/dist/models/notification.js +1 -1
  370. package/dist/models/notification.js.map +1 -1
  371. package/dist/models/order.d.ts +1 -1
  372. package/dist/models/payment-session.d.ts +1 -1
  373. package/dist/models/payment-session.js +4 -4
  374. package/dist/models/payment-session.js.map +1 -1
  375. package/dist/models/product.js +1 -1
  376. package/dist/models/product.js.map +1 -1
  377. package/dist/repositories/claim.js.map +1 -1
  378. package/dist/repositories/order.d.ts +3 -3
  379. package/dist/repositories/order.js +2 -2
  380. package/dist/repositories/order.js.map +1 -1
  381. package/dist/repositories/price-list.js.map +1 -1
  382. package/dist/repositories/product.d.ts +2 -2
  383. package/dist/repositories/product.js +19 -11
  384. package/dist/repositories/product.js.map +1 -1
  385. package/dist/repositories/sales-channel.d.ts +5 -1
  386. package/dist/repositories/sales-channel.js +116 -0
  387. package/dist/repositories/sales-channel.js.map +1 -1
  388. package/dist/services/batch-job.js +18 -32
  389. package/dist/services/batch-job.js.map +1 -1
  390. package/dist/services/cart.d.ts +37 -10
  391. package/dist/services/cart.js +293 -161
  392. package/dist/services/cart.js.map +1 -1
  393. package/dist/services/claim.js +18 -32
  394. package/dist/services/claim.js.map +1 -1
  395. package/dist/services/custom-shipping-option.js +28 -53
  396. package/dist/services/custom-shipping-option.js.map +1 -1
  397. package/dist/services/customer.js +33 -78
  398. package/dist/services/customer.js.map +1 -1
  399. package/dist/services/discount-condition.js +12 -19
  400. package/dist/services/discount-condition.js.map +1 -1
  401. package/dist/services/discount.js +67 -91
  402. package/dist/services/discount.js.map +1 -1
  403. package/dist/services/draft-order.js +57 -85
  404. package/dist/services/draft-order.js.map +1 -1
  405. package/dist/services/fulfillment.d.ts +1 -1
  406. package/dist/services/fulfillment.js +12 -19
  407. package/dist/services/fulfillment.js.map +1 -1
  408. package/dist/services/gift-card.js +67 -116
  409. package/dist/services/gift-card.js.map +1 -1
  410. package/dist/services/idempotency-key.d.ts +2 -3
  411. package/dist/services/idempotency-key.js +3 -3
  412. package/dist/services/idempotency-key.js.map +1 -1
  413. package/dist/services/inventory.d.ts +22 -21
  414. package/dist/services/inventory.js +57 -55
  415. package/dist/services/inventory.js.map +1 -1
  416. package/dist/services/line-item.js +19 -33
  417. package/dist/services/line-item.js.map +1 -1
  418. package/dist/services/note.d.ts +43 -48
  419. package/dist/services/note.js +106 -118
  420. package/dist/services/note.js.map +1 -1
  421. package/dist/services/notification.d.ts +54 -51
  422. package/dist/services/notification.js +118 -113
  423. package/dist/services/notification.js.map +1 -1
  424. package/dist/services/order.d.ts +163 -184
  425. package/dist/services/order.js +1248 -1319
  426. package/dist/services/order.js.map +1 -1
  427. package/dist/services/price-list.js +35 -45
  428. package/dist/services/price-list.js.map +1 -1
  429. package/dist/services/pricing.js +32 -35
  430. package/dist/services/pricing.js.map +1 -1
  431. package/dist/services/product.d.ts +7 -4
  432. package/dist/services/product.js +151 -170
  433. package/dist/services/product.js.map +1 -1
  434. package/dist/services/sales-channel.d.ts +22 -1
  435. package/dist/services/sales-channel.js +130 -28
  436. package/dist/services/sales-channel.js.map +1 -1
  437. package/dist/services/search.d.ts +23 -19
  438. package/dist/services/search.js +10 -9
  439. package/dist/services/search.js.map +1 -1
  440. package/dist/services/shipping-option.d.ts +1 -1
  441. package/dist/services/shipping-option.js +77 -84
  442. package/dist/services/shipping-option.js.map +1 -1
  443. package/dist/services/shipping-profile.js +37 -30
  444. package/dist/services/shipping-profile.js.map +1 -1
  445. package/dist/services/store.d.ts +5 -5
  446. package/dist/services/store.js +13 -20
  447. package/dist/services/store.js.map +1 -1
  448. package/dist/services/swap.js +3 -1
  449. package/dist/services/swap.js.map +1 -1
  450. package/dist/services/tax-provider.d.ts +18 -19
  451. package/dist/services/tax-provider.js +81 -61
  452. package/dist/services/tax-provider.js.map +1 -1
  453. package/dist/services/tax-rate.js +23 -46
  454. package/dist/services/tax-rate.js.map +1 -1
  455. package/dist/services/totals.d.ts +7 -4
  456. package/dist/services/totals.js +19 -7
  457. package/dist/services/totals.js.map +1 -1
  458. package/dist/services/user.d.ts +3 -0
  459. package/dist/services/user.js +70 -76
  460. package/dist/services/user.js.map +1 -1
  461. package/dist/strategies/__fixtures__/order-export-data.js +11 -0
  462. package/dist/strategies/__fixtures__/order-export-data.js.map +1 -1
  463. package/dist/strategies/__fixtures__/product-export-data.d.ts +10 -0
  464. package/dist/strategies/__fixtures__/product-export-data.js +7 -0
  465. package/dist/strategies/__fixtures__/product-export-data.js.map +1 -1
  466. package/dist/strategies/batch-jobs/order/export.d.ts +10 -4
  467. package/dist/strategies/batch-jobs/order/export.js +29 -11
  468. package/dist/strategies/batch-jobs/order/export.js.map +1 -1
  469. package/dist/strategies/batch-jobs/order/index.js +1 -1
  470. package/dist/strategies/batch-jobs/order/index.js.map +1 -1
  471. package/dist/strategies/batch-jobs/product/export.d.ts +13 -1
  472. package/dist/strategies/batch-jobs/product/export.js +137 -80
  473. package/dist/strategies/batch-jobs/product/export.js.map +1 -1
  474. package/dist/strategies/batch-jobs/product/index.d.ts +1 -0
  475. package/dist/strategies/batch-jobs/product/index.js.map +1 -1
  476. package/dist/subscribers/search-indexing.d.ts +18 -0
  477. package/dist/subscribers/search-indexing.js +126 -0
  478. package/dist/subscribers/search-indexing.js.map +1 -0
  479. package/dist/types/cart.d.ts +7 -4
  480. package/dist/types/common.d.ts +10 -1
  481. package/dist/types/common.js +63 -1
  482. package/dist/types/common.js.map +1 -1
  483. package/dist/types/gift-card.d.ts +3 -1
  484. package/dist/types/global.d.ts +3 -2
  485. package/dist/types/orders.d.ts +80 -3
  486. package/dist/types/orders.js +8 -2
  487. package/dist/types/orders.js.map +1 -1
  488. package/dist/types/product.d.ts +16 -0
  489. package/dist/types/product.js +46 -1
  490. package/dist/types/product.js.map +1 -1
  491. package/dist/types/sales-channels.d.ts +3 -0
  492. package/dist/types/sales-channels.js +21 -0
  493. package/dist/types/sales-channels.js.map +1 -1
  494. package/dist/utils/index.d.ts +5 -4
  495. package/dist/utils/index.js +1 -0
  496. package/dist/utils/index.js.map +1 -1
  497. package/dist/utils/remove-undefined-properties.d.ts +1 -0
  498. package/dist/utils/remove-undefined-properties.js +46 -0
  499. package/dist/utils/remove-undefined-properties.js.map +1 -0
  500. package/jest.config.js +6 -6
  501. package/package.json +4 -5
  502. package/yarn-error.log +29038 -0
@@ -110,94 +110,48 @@ var __values = (this && this.__values) || function(o) {
110
110
  };
111
111
  Object.defineProperty(exports, "__esModule", { value: true });
112
112
  var medusa_core_utils_1 = require("medusa-core-utils");
113
- var medusa_interfaces_1 = require("medusa-interfaces");
114
113
  var typeorm_1 = require("typeorm");
114
+ var interfaces_1 = require("../interfaces");
115
+ var utils_1 = require("../utils");
116
+ var models_1 = require("../models");
115
117
  var OrderService = /** @class */ (function (_super) {
116
118
  __extends(OrderService, _super);
117
119
  function OrderService(_a) {
118
120
  var manager = _a.manager, orderRepository = _a.orderRepository, customerService = _a.customerService, paymentProviderService = _a.paymentProviderService, shippingOptionService = _a.shippingOptionService, shippingProfileService = _a.shippingProfileService, discountService = _a.discountService, fulfillmentProviderService = _a.fulfillmentProviderService, fulfillmentService = _a.fulfillmentService, lineItemService = _a.lineItemService, totalsService = _a.totalsService, regionService = _a.regionService, cartService = _a.cartService, addressRepository = _a.addressRepository, giftCardService = _a.giftCardService, draftOrderService = _a.draftOrderService, inventoryService = _a.inventoryService, eventBusService = _a.eventBusService;
119
- var _this = _super.call(this) || this;
120
- /** @private @constant {EntityManager} */
121
+ var _this =
122
+ // eslint-disable-next-line prefer-rest-params
123
+ _super.call(this, arguments[0]) || this;
121
124
  _this.manager_ = manager;
122
- /** @private @constant {OrderRepository} */
123
125
  _this.orderRepository_ = orderRepository;
124
- /** @private @constant {CustomerService} */
125
126
  _this.customerService_ = customerService;
126
- /** @private @constant {PaymentProviderService} */
127
127
  _this.paymentProviderService_ = paymentProviderService;
128
- /** @private @constant {ShippingProvileService} */
129
128
  _this.shippingProfileService_ = shippingProfileService;
130
- /** @private @constant {FulfillmentProviderService} */
131
129
  _this.fulfillmentProviderService_ = fulfillmentProviderService;
132
- /** @private @constant {LineItemService} */
133
130
  _this.lineItemService_ = lineItemService;
134
- /** @private @constant {TotalsService} */
135
131
  _this.totalsService_ = totalsService;
136
- /** @private @constant {RegionService} */
137
132
  _this.regionService_ = regionService;
138
- /** @private @constant {FulfillmentService} */
139
133
  _this.fulfillmentService_ = fulfillmentService;
140
- /** @private @constant {DiscountService} */
141
134
  _this.discountService_ = discountService;
142
- /** @private @constant {DiscountService} */
143
135
  _this.giftCardService_ = giftCardService;
144
- /** @private @constant {EventBus} */
145
136
  _this.eventBus_ = eventBusService;
146
- /** @private @constant {ShippingOptionService} */
147
137
  _this.shippingOptionService_ = shippingOptionService;
148
- /** @private @constant {CartService} */
149
138
  _this.cartService_ = cartService;
150
- /** @private @constant {AddressRepository} */
151
139
  _this.addressRepository_ = addressRepository;
152
- /** @private @constant {DraftOrderService} */
153
140
  _this.draftOrderService_ = draftOrderService;
154
- /** @private @constant {InventoryService} */
155
141
  _this.inventoryService_ = inventoryService;
156
142
  return _this;
157
143
  }
158
- OrderService.prototype.withTransaction = function (manager) {
159
- if (!manager) {
160
- return this;
161
- }
162
- var cloned = new OrderService({
163
- manager: manager,
164
- orderRepository: this.orderRepository_,
165
- eventBusService: this.eventBus_,
166
- paymentProviderService: this.paymentProviderService_,
167
- regionService: this.regionService_,
168
- lineItemService: this.lineItemService_,
169
- shippingOptionService: this.shippingOptionService_,
170
- shippingProfileService: this.shippingProfileService_,
171
- fulfillmentProviderService: this.fulfillmentProviderService_,
172
- fulfillmentService: this.fulfillmentService_,
173
- customerService: this.customerService_,
174
- discountService: this.discountService_,
175
- totalsService: this.totalsService_,
176
- cartService: this.cartService_,
177
- giftCardService: this.giftCardService_,
178
- addressRepository: this.addressRepository_,
179
- draftOrderService: this.draftOrderService_,
180
- inventoryService: this.inventoryService_,
181
- });
182
- cloned.transactionManager_ = manager;
183
- cloned.manager_ = manager;
184
- return cloned;
185
- };
186
144
  /**
187
- * Used to validate order ids. Throws an error if the cast fails
188
- * @param {string} rawId - the raw order id to validate.
189
- * @return {string} the validated id
190
- */
191
- OrderService.prototype.validateId_ = function (rawId) {
192
- return rawId;
193
- };
194
- /**
195
- * @param {Object} selector - the query object for find
196
- * @param {Object} config - the config to be used for find
197
- * @return {Promise} the result of the find operation
145
+ * @param selector the query object for find
146
+ * @param config the config to be used for find
147
+ * @return the result of the find operation
198
148
  */
199
149
  OrderService.prototype.list = function (selector, config) {
200
- if (config === void 0) { config = { skip: 0, take: 50, order: { created_at: "DESC" } }; }
150
+ if (config === void 0) { config = {
151
+ skip: 0,
152
+ take: 50,
153
+ order: { created_at: "DESC" },
154
+ }; }
201
155
  return __awaiter(this, void 0, void 0, function () {
202
156
  var orderRepo, query, _a, select, relations, totalsToSelect, raw;
203
157
  var _this = this;
@@ -205,8 +159,8 @@ var OrderService = /** @class */ (function (_super) {
205
159
  switch (_b.label) {
206
160
  case 0:
207
161
  orderRepo = this.manager_.getCustomRepository(this.orderRepository_);
208
- query = this.buildQuery_(selector, config);
209
- _a = this.transformQueryForTotals_(config), select = _a.select, relations = _a.relations, totalsToSelect = _a.totalsToSelect;
162
+ query = (0, utils_1.buildQuery)(selector, config);
163
+ _a = this.transformQueryForTotals(config), select = _a.select, relations = _a.relations, totalsToSelect = _a.totalsToSelect;
210
164
  if (select && select.length) {
211
165
  query.select = select;
212
166
  }
@@ -218,7 +172,7 @@ var OrderService = /** @class */ (function (_super) {
218
172
  raw = _b.sent();
219
173
  return [4 /*yield*/, Promise.all(raw.map(function (r) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
220
174
  switch (_a.label) {
221
- case 0: return [4 /*yield*/, this.decorateTotals_(r, totalsToSelect)];
175
+ case 0: return [4 /*yield*/, this.decorateTotals(r, totalsToSelect)];
222
176
  case 1: return [2 /*return*/, _a.sent()];
223
177
  }
224
178
  }); }); }))];
@@ -228,7 +182,11 @@ var OrderService = /** @class */ (function (_super) {
228
182
  });
229
183
  };
230
184
  OrderService.prototype.listAndCount = function (selector, config) {
231
- if (config === void 0) { config = { skip: 0, take: 50, order: { created_at: "DESC" } }; }
185
+ if (config === void 0) { config = {
186
+ skip: 0,
187
+ take: 50,
188
+ order: { created_at: "DESC" },
189
+ }; }
232
190
  return __awaiter(this, void 0, void 0, function () {
233
191
  var orderRepo, q, query, where_1, _a, select, relations, totalsToSelect, rels, raw, count, orders;
234
192
  var _this = this;
@@ -240,7 +198,7 @@ var OrderService = /** @class */ (function (_super) {
240
198
  q = selector.q;
241
199
  delete selector.q;
242
200
  }
243
- query = this.buildQuery_(selector, config);
201
+ query = (0, utils_1.buildQuery)(selector, config);
244
202
  if (q) {
245
203
  where_1 = query.where;
246
204
  delete where_1.display_id;
@@ -262,7 +220,7 @@ var OrderService = /** @class */ (function (_super) {
262
220
  }));
263
221
  };
264
222
  }
265
- _a = this.transformQueryForTotals_(config), select = _a.select, relations = _a.relations, totalsToSelect = _a.totalsToSelect;
223
+ _a = this.transformQueryForTotals(config), select = _a.select, relations = _a.relations, totalsToSelect = _a.totalsToSelect;
266
224
  if (select && select.length) {
267
225
  query.select = select;
268
226
  }
@@ -276,7 +234,7 @@ var OrderService = /** @class */ (function (_super) {
276
234
  count = _b.sent();
277
235
  return [4 /*yield*/, Promise.all(raw.map(function (r) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
278
236
  switch (_a.label) {
279
- case 0: return [4 /*yield*/, this.decorateTotals_(r, totalsToSelect)];
237
+ case 0: return [4 /*yield*/, this.decorateTotals(r, totalsToSelect)];
280
238
  case 1: return [2 /*return*/, _a.sent()];
281
239
  }
282
240
  }); }); }))];
@@ -287,7 +245,7 @@ var OrderService = /** @class */ (function (_super) {
287
245
  });
288
246
  });
289
247
  };
290
- OrderService.prototype.transformQueryForTotals_ = function (config) {
248
+ OrderService.prototype.transformQueryForTotals = function (config) {
291
249
  var select = config.select, relations = config.relations;
292
250
  if (!select) {
293
251
  return {
@@ -347,22 +305,21 @@ var OrderService = /** @class */ (function (_super) {
347
305
  };
348
306
  /**
349
307
  * Gets an order by id.
350
- * @param {string} orderId - id of order to retrieve
351
- * @param {Object} config - config of order to retrieve
352
- * @return {Promise<Order>} the order document
308
+ * @param orderId - id of order to retrieve
309
+ * @param config - config of order to retrieve
310
+ * @return the order document
353
311
  */
354
312
  OrderService.prototype.retrieve = function (orderId, config) {
355
313
  if (config === void 0) { config = {}; }
356
314
  return __awaiter(this, void 0, void 0, function () {
357
- var orderRepo, validatedId, _a, select, relations, totalsToSelect, query, rels, raw;
315
+ var orderRepo, _a, select, relations, totalsToSelect, query, rels, raw;
358
316
  return __generator(this, function (_b) {
359
317
  switch (_b.label) {
360
318
  case 0:
361
319
  orderRepo = this.manager_.getCustomRepository(this.orderRepository_);
362
- validatedId = this.validateId_(orderId);
363
- _a = this.transformQueryForTotals_(config), select = _a.select, relations = _a.relations, totalsToSelect = _a.totalsToSelect;
320
+ _a = this.transformQueryForTotals(config), select = _a.select, relations = _a.relations, totalsToSelect = _a.totalsToSelect;
364
321
  query = {
365
- where: { id: validatedId },
322
+ where: { id: orderId },
366
323
  };
367
324
  if (relations && relations.length > 0) {
368
325
  query.relations = relations;
@@ -378,7 +335,7 @@ var OrderService = /** @class */ (function (_super) {
378
335
  if (!raw) {
379
336
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Order with ".concat(orderId, " was not found"));
380
337
  }
381
- return [4 /*yield*/, this.decorateTotals_(raw, totalsToSelect)];
338
+ return [4 /*yield*/, this.decorateTotals(raw, totalsToSelect)];
382
339
  case 2: return [2 /*return*/, _b.sent()];
383
340
  }
384
341
  });
@@ -386,19 +343,19 @@ var OrderService = /** @class */ (function (_super) {
386
343
  };
387
344
  /**
388
345
  * Gets an order by cart id.
389
- * @param {string} cartId - cart id to find order
390
- * @param {Object} config - the config to be used to find order
391
- * @return {Promise<Order>} the order document
346
+ * @param cartId - cart id to find order
347
+ * @param config - the config to be used to find order
348
+ * @return the order document
392
349
  */
393
350
  OrderService.prototype.retrieveByCartId = function (cartId, config) {
394
351
  if (config === void 0) { config = {}; }
395
352
  return __awaiter(this, void 0, void 0, function () {
396
- var orderRepo, _a, select, relations, totalsToSelect, query, raw, order;
353
+ var orderRepo, _a, select, relations, totalsToSelect, query, raw;
397
354
  return __generator(this, function (_b) {
398
355
  switch (_b.label) {
399
356
  case 0:
400
357
  orderRepo = this.manager_.getCustomRepository(this.orderRepository_);
401
- _a = this.transformQueryForTotals_(config), select = _a.select, relations = _a.relations, totalsToSelect = _a.totalsToSelect;
358
+ _a = this.transformQueryForTotals(config), select = _a.select, relations = _a.relations, totalsToSelect = _a.totalsToSelect;
402
359
  query = {
403
360
  where: { cart_id: cartId },
404
361
  };
@@ -414,29 +371,27 @@ var OrderService = /** @class */ (function (_super) {
414
371
  if (!raw) {
415
372
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Order with cart id: ".concat(cartId, " was not found"));
416
373
  }
417
- return [4 /*yield*/, this.decorateTotals_(raw, totalsToSelect)];
418
- case 2:
419
- order = _b.sent();
420
- return [2 /*return*/, order];
374
+ return [4 /*yield*/, this.decorateTotals(raw, totalsToSelect)];
375
+ case 2: return [2 /*return*/, _b.sent()];
421
376
  }
422
377
  });
423
378
  });
424
379
  };
425
380
  /**
426
381
  * Gets an order by id.
427
- * @param {string} externalId - id of order to retrieve
428
- * @param {object} config - query config to get order by
429
- * @return {Promise<Order>} the order document
382
+ * @param externalId - id of order to retrieve
383
+ * @param config - query config to get order by
384
+ * @return the order document
430
385
  */
431
386
  OrderService.prototype.retrieveByExternalId = function (externalId, config) {
432
387
  if (config === void 0) { config = {}; }
433
388
  return __awaiter(this, void 0, void 0, function () {
434
- var orderRepo, _a, select, relations, totalsToSelect, query, rels, raw, order;
389
+ var orderRepo, _a, select, relations, totalsToSelect, query, rels, raw;
435
390
  return __generator(this, function (_b) {
436
391
  switch (_b.label) {
437
392
  case 0:
438
393
  orderRepo = this.manager_.getCustomRepository(this.orderRepository_);
439
- _a = this.transformQueryForTotals_(config), select = _a.select, relations = _a.relations, totalsToSelect = _a.totalsToSelect;
394
+ _a = this.transformQueryForTotals(config), select = _a.select, relations = _a.relations, totalsToSelect = _a.totalsToSelect;
440
395
  query = {
441
396
  where: { external_id: externalId },
442
397
  };
@@ -454,354 +409,350 @@ var OrderService = /** @class */ (function (_super) {
454
409
  if (!raw) {
455
410
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Order with external id ".concat(externalId, " was not found"));
456
411
  }
457
- return [4 /*yield*/, this.decorateTotals_(raw, totalsToSelect)];
458
- case 2:
459
- order = _b.sent();
460
- return [2 /*return*/, order];
412
+ return [4 /*yield*/, this.decorateTotals(raw, totalsToSelect)];
413
+ case 2: return [2 /*return*/, _b.sent()];
461
414
  }
462
415
  });
463
416
  });
464
417
  };
465
418
  /**
466
419
  * Checks the existence of an order by cart id.
467
- * @param {string} cartId - cart id to find order
468
- * @return {Promise<Order>} the order document
420
+ * @param cartId - cart id to find order
421
+ * @return the order document
469
422
  */
470
423
  OrderService.prototype.existsByCartId = function (cartId) {
471
424
  return __awaiter(this, void 0, void 0, function () {
472
425
  var order;
473
426
  return __generator(this, function (_a) {
474
427
  switch (_a.label) {
475
- case 0: return [4 /*yield*/, this.retrieveByCartId(cartId).catch(function (_) { return undefined; })];
428
+ case 0: return [4 /*yield*/, this.retrieveByCartId(cartId).catch(function () { return undefined; })];
476
429
  case 1:
477
430
  order = _a.sent();
478
- if (!order) {
479
- return [2 /*return*/, false];
480
- }
481
- return [2 /*return*/, true];
431
+ return [2 /*return*/, !!order];
482
432
  }
483
433
  });
484
434
  });
485
435
  };
486
436
  /**
487
- * @param {string} orderId - id of the order to complete
488
- * @return {Promise} the result of the find operation
437
+ * @param orderId - id of the order to complete
438
+ * @return the result of the find operation
489
439
  */
490
440
  OrderService.prototype.completeOrder = function (orderId) {
491
441
  return __awaiter(this, void 0, void 0, function () {
492
442
  var _this = this;
493
443
  return __generator(this, function (_a) {
494
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
495
- var order, completeOrderJob, orderRepo;
496
- return __generator(this, function (_a) {
497
- switch (_a.label) {
498
- case 0: return [4 /*yield*/, this.retrieve(orderId)];
499
- case 1:
500
- order = _a.sent();
501
- if (order.status === "canceled") {
502
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A canceled order cannot be completed");
503
- }
504
- return [4 /*yield*/, this.eventBus_.emit(OrderService.Events.COMPLETED, {
505
- id: orderId,
506
- no_notification: order.no_notification,
507
- })];
508
- case 2:
509
- completeOrderJob = _a.sent();
510
- return [4 /*yield*/, completeOrderJob.finished().catch(function (error) {
511
- throw error;
512
- })];
513
- case 3:
514
- _a.sent();
515
- order.status = "completed";
516
- orderRepo = manager.getCustomRepository(this.orderRepository_);
517
- return [2 /*return*/, orderRepo.save(order)];
518
- }
519
- });
520
- }); })];
444
+ switch (_a.label) {
445
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
446
+ var order, orderRepo;
447
+ return __generator(this, function (_a) {
448
+ switch (_a.label) {
449
+ case 0: return [4 /*yield*/, this.retrieve(orderId)];
450
+ case 1:
451
+ order = _a.sent();
452
+ if (order.status === "canceled") {
453
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A canceled order cannot be completed");
454
+ }
455
+ return [4 /*yield*/, this.eventBus_.emit(OrderService.Events.COMPLETED, {
456
+ id: orderId,
457
+ no_notification: order.no_notification,
458
+ })];
459
+ case 2:
460
+ _a.sent();
461
+ order.status = models_1.OrderStatus.COMPLETED;
462
+ orderRepo = manager.getCustomRepository(this.orderRepository_);
463
+ return [2 /*return*/, orderRepo.save(order)];
464
+ }
465
+ });
466
+ }); })];
467
+ case 1: return [2 /*return*/, _a.sent()];
468
+ }
521
469
  });
522
470
  });
523
471
  };
524
472
  /**
525
473
  * Creates an order from a cart
526
- * @param {string} cartId - id of the cart to create an order from
527
- * @return {Promise} resolves to the creation result.
474
+ * @param cartId - id of the cart to create an order from
475
+ * @return resolves to the creation result.
528
476
  */
529
477
  OrderService.prototype.createFromCart = function (cartId) {
530
478
  return __awaiter(this, void 0, void 0, function () {
531
479
  var _this = this;
532
480
  return __generator(this, function (_a) {
533
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
534
- var cartService, inventoryService, cart, payment, region, total, _a, _b, item, err_1, e_1_1, exists, paymentStatus, orderRepo, toCreate, draft, o, result, gcBalance, gcService, _c, _d, g, newBalance, usage, e_2_1, _e, _f, method, e_3_1, lineItemService, _g, _h, item, e_4_1, _j, _k, item, e_5_1;
535
- var e_1, _l, e_2, _m, e_3, _o, e_4, _p, e_5, _q;
536
- return __generator(this, function (_r) {
537
- switch (_r.label) {
538
- case 0:
539
- cartService = this.cartService_.withTransaction(manager);
540
- inventoryService = this.inventoryService_.withTransaction(manager);
541
- return [4 /*yield*/, cartService.retrieve(cartId, {
542
- select: ["subtotal", "total"],
543
- relations: [
544
- "region",
545
- "payment",
546
- "items",
547
- "discounts",
548
- "discounts.rule",
549
- "gift_cards",
550
- "shipping_methods",
551
- ],
552
- })];
553
- case 1:
554
- cart = _r.sent();
555
- if (cart.items.length === 0) {
556
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Cannot create order from empty cart");
557
- }
558
- payment = cart.payment, region = cart.region, total = cart.total;
559
- _r.label = 2;
560
- case 2:
561
- _r.trys.push([2, 12, 13, 14]);
562
- _a = __values(cart.items), _b = _a.next();
563
- _r.label = 3;
564
- case 3:
565
- if (!!_b.done) return [3 /*break*/, 11];
566
- item = _b.value;
567
- _r.label = 4;
568
- case 4:
569
- _r.trys.push([4, 6, , 10]);
570
- return [4 /*yield*/, inventoryService.confirmInventory(item.variant_id, item.quantity)];
571
- case 5:
572
- _r.sent();
573
- return [3 /*break*/, 10];
574
- case 6:
575
- err_1 = _r.sent();
576
- if (!payment) return [3 /*break*/, 8];
577
- return [4 /*yield*/, this.paymentProviderService_
578
- .withTransaction(manager)
579
- .cancelPayment(payment)];
580
- case 7:
581
- _r.sent();
582
- _r.label = 8;
583
- case 8: return [4 /*yield*/, cartService.update(cart.id, { payment_authorized_at: null })];
584
- case 9:
585
- _r.sent();
586
- throw err_1;
587
- case 10:
588
- _b = _a.next();
589
- return [3 /*break*/, 3];
590
- case 11: return [3 /*break*/, 14];
591
- case 12:
592
- e_1_1 = _r.sent();
593
- e_1 = { error: e_1_1 };
594
- return [3 /*break*/, 14];
595
- case 13:
596
- try {
597
- if (_b && !_b.done && (_l = _a.return)) _l.call(_a);
598
- }
599
- finally { if (e_1) throw e_1.error; }
600
- return [7 /*endfinally*/];
601
- case 14: return [4 /*yield*/, this.existsByCartId(cart.id)];
602
- case 15:
603
- exists = _r.sent();
604
- if (exists) {
605
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.DUPLICATE_ERROR, "Order from cart already exists");
606
- }
607
- if (!(total !== 0)) return [3 /*break*/, 17];
608
- // Throw if payment method does not exist
609
- if (!payment) {
610
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_ARGUMENT, "Cart does not contain a payment method");
611
- }
612
- return [4 /*yield*/, this.paymentProviderService_
613
- .withTransaction(manager)
614
- .getStatus(payment)
481
+ switch (_a.label) {
482
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
483
+ var cartService, inventoryService, cart, payment, region, total, _a, _b, item, err_1, e_1_1, exists, paymentStatus, orderRepo, toCreate, draft, o, result, gcBalance, gcService, _c, _d, g, newBalance, usage, e_2_1, _e, _f, method, e_3_1, lineItemService, _g, _h, item, e_4_1, _j, _k, item, e_5_1;
484
+ var e_1, _l, e_2, _m, e_3, _o, e_4, _p, e_5, _q;
485
+ return __generator(this, function (_r) {
486
+ switch (_r.label) {
487
+ case 0:
488
+ cartService = this.cartService_.withTransaction(manager);
489
+ inventoryService = this.inventoryService_.withTransaction(manager);
490
+ return [4 /*yield*/, cartService.retrieve(cartId, {
491
+ select: ["subtotal", "total"],
492
+ relations: [
493
+ "region",
494
+ "payment",
495
+ "items",
496
+ "discounts",
497
+ "discounts.rule",
498
+ "gift_cards",
499
+ "shipping_methods",
500
+ ],
501
+ })];
502
+ case 1:
503
+ cart = _r.sent();
504
+ if (cart.items.length === 0) {
505
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Cannot create order from empty cart");
506
+ }
507
+ payment = cart.payment, region = cart.region, total = cart.total;
508
+ _r.label = 2;
509
+ case 2:
510
+ _r.trys.push([2, 12, 13, 14]);
511
+ _a = __values(cart.items), _b = _a.next();
512
+ _r.label = 3;
513
+ case 3:
514
+ if (!!_b.done) return [3 /*break*/, 11];
515
+ item = _b.value;
516
+ _r.label = 4;
517
+ case 4:
518
+ _r.trys.push([4, 6, , 10]);
519
+ return [4 /*yield*/, inventoryService.confirmInventory(item.variant_id, item.quantity)];
520
+ case 5:
521
+ _r.sent();
522
+ return [3 /*break*/, 10];
523
+ case 6:
524
+ err_1 = _r.sent();
525
+ if (!payment) return [3 /*break*/, 8];
526
+ return [4 /*yield*/, this.paymentProviderService_
527
+ .withTransaction(manager)
528
+ .cancelPayment(payment)];
529
+ case 7:
530
+ _r.sent();
531
+ _r.label = 8;
532
+ case 8: return [4 /*yield*/, cartService.update(cart.id, { payment_authorized_at: null })];
533
+ case 9:
534
+ _r.sent();
535
+ throw err_1;
536
+ case 10:
537
+ _b = _a.next();
538
+ return [3 /*break*/, 3];
539
+ case 11: return [3 /*break*/, 14];
540
+ case 12:
541
+ e_1_1 = _r.sent();
542
+ e_1 = { error: e_1_1 };
543
+ return [3 /*break*/, 14];
544
+ case 13:
545
+ try {
546
+ if (_b && !_b.done && (_l = _a.return)) _l.call(_a);
547
+ }
548
+ finally { if (e_1) throw e_1.error; }
549
+ return [7 /*endfinally*/];
550
+ case 14: return [4 /*yield*/, this.existsByCartId(cart.id)];
551
+ case 15:
552
+ exists = _r.sent();
553
+ if (exists) {
554
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.DUPLICATE_ERROR, "Order from cart already exists");
555
+ }
556
+ if (!(total !== 0)) return [3 /*break*/, 17];
557
+ // Throw if payment method does not exist
558
+ if (!payment) {
559
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_ARGUMENT, "Cart does not contain a payment method");
560
+ }
561
+ return [4 /*yield*/, this.paymentProviderService_
562
+ .withTransaction(manager)
563
+ .getStatus(payment)
564
+ // If payment status is not authorized, we throw
565
+ ];
566
+ case 16:
567
+ paymentStatus = _r.sent();
615
568
  // If payment status is not authorized, we throw
616
- ];
617
- case 16:
618
- paymentStatus = _r.sent();
619
- // If payment status is not authorized, we throw
620
- if (paymentStatus !== "authorized" && paymentStatus !== "succeeded") {
621
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_ARGUMENT, "Payment method is not authorized");
622
- }
623
- _r.label = 17;
624
- case 17:
625
- orderRepo = manager.getCustomRepository(this.orderRepository_);
626
- toCreate = {
627
- payment_status: "awaiting",
628
- discounts: cart.discounts,
629
- gift_cards: cart.gift_cards,
630
- shipping_methods: cart.shipping_methods,
631
- shipping_address_id: cart.shipping_address_id,
632
- billing_address_id: cart.billing_address_id,
633
- region_id: cart.region_id,
634
- email: cart.email,
635
- customer_id: cart.customer_id,
636
- cart_id: cart.id,
637
- currency_code: region.currency_code,
638
- metadata: cart.metadata || {},
639
- };
640
- if (!(cart.type === "draft_order")) return [3 /*break*/, 19];
641
- return [4 /*yield*/, this.draftOrderService_
642
- .withTransaction(manager)
643
- .retrieveByCartId(cart.id)];
644
- case 18:
645
- draft = _r.sent();
646
- toCreate.draft_order_id = draft.id;
647
- toCreate.no_notification = draft.no_notification_order;
648
- _r.label = 19;
649
- case 19:
650
- o = orderRepo.create(toCreate);
651
- return [4 /*yield*/, orderRepo.save(o)];
652
- case 20:
653
- result = _r.sent();
654
- if (!(total !== 0)) return [3 /*break*/, 22];
655
- return [4 /*yield*/, this.paymentProviderService_
569
+ if (paymentStatus !== "authorized") {
570
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_ARGUMENT, "Payment method is not authorized");
571
+ }
572
+ _r.label = 17;
573
+ case 17:
574
+ orderRepo = manager.getCustomRepository(this.orderRepository_);
575
+ toCreate = {
576
+ payment_status: "awaiting",
577
+ discounts: cart.discounts,
578
+ gift_cards: cart.gift_cards,
579
+ shipping_methods: cart.shipping_methods,
580
+ shipping_address_id: cart.shipping_address_id,
581
+ billing_address_id: cart.billing_address_id,
582
+ region_id: cart.region_id,
583
+ email: cart.email,
584
+ customer_id: cart.customer_id,
585
+ cart_id: cart.id,
586
+ currency_code: region.currency_code,
587
+ metadata: cart.metadata || {},
588
+ };
589
+ if (!(cart.type === "draft_order")) return [3 /*break*/, 19];
590
+ return [4 /*yield*/, this.draftOrderService_
591
+ .withTransaction(manager)
592
+ .retrieveByCartId(cart.id)];
593
+ case 18:
594
+ draft = _r.sent();
595
+ toCreate.draft_order_id = draft.id;
596
+ toCreate.no_notification = draft.no_notification_order;
597
+ _r.label = 19;
598
+ case 19:
599
+ o = orderRepo.create(toCreate);
600
+ return [4 /*yield*/, orderRepo.save(o)];
601
+ case 20:
602
+ result = _r.sent();
603
+ if (!(total !== 0 && payment)) return [3 /*break*/, 22];
604
+ return [4 /*yield*/, this.paymentProviderService_
605
+ .withTransaction(manager)
606
+ .updatePayment(payment.id, {
607
+ order_id: result.id,
608
+ })];
609
+ case 21:
610
+ _r.sent();
611
+ _r.label = 22;
612
+ case 22: return [4 /*yield*/, this.totalsService_.getGiftCardableAmount(cart)];
613
+ case 23:
614
+ gcBalance = _r.sent();
615
+ gcService = this.giftCardService_.withTransaction(manager);
616
+ _r.label = 24;
617
+ case 24:
618
+ _r.trys.push([24, 30, 31, 32]);
619
+ _c = __values(cart.gift_cards), _d = _c.next();
620
+ _r.label = 25;
621
+ case 25:
622
+ if (!!_d.done) return [3 /*break*/, 29];
623
+ g = _d.value;
624
+ newBalance = Math.max(0, g.balance - gcBalance);
625
+ usage = g.balance - newBalance;
626
+ return [4 /*yield*/, gcService.update(g.id, {
627
+ balance: newBalance,
628
+ is_disabled: newBalance === 0,
629
+ })];
630
+ case 26:
631
+ _r.sent();
632
+ return [4 /*yield*/, gcService.createTransaction({
633
+ gift_card_id: g.id,
634
+ order_id: result.id,
635
+ amount: usage,
636
+ is_taxable: cart.region.gift_cards_taxable,
637
+ tax_rate: cart.region.gift_cards_taxable
638
+ ? cart.region.tax_rate
639
+ : null,
640
+ })];
641
+ case 27:
642
+ _r.sent();
643
+ gcBalance = gcBalance - usage;
644
+ _r.label = 28;
645
+ case 28:
646
+ _d = _c.next();
647
+ return [3 /*break*/, 25];
648
+ case 29: return [3 /*break*/, 32];
649
+ case 30:
650
+ e_2_1 = _r.sent();
651
+ e_2 = { error: e_2_1 };
652
+ return [3 /*break*/, 32];
653
+ case 31:
654
+ try {
655
+ if (_d && !_d.done && (_m = _c.return)) _m.call(_c);
656
+ }
657
+ finally { if (e_2) throw e_2.error; }
658
+ return [7 /*endfinally*/];
659
+ case 32:
660
+ _r.trys.push([32, 37, 38, 39]);
661
+ _e = __values(cart.shipping_methods), _f = _e.next();
662
+ _r.label = 33;
663
+ case 33:
664
+ if (!!_f.done) return [3 /*break*/, 36];
665
+ method = _f.value;
666
+ return [4 /*yield*/, this.shippingOptionService_
667
+ .withTransaction(manager)
668
+ .updateShippingMethod(method.id, { order_id: result.id })];
669
+ case 34:
670
+ _r.sent();
671
+ _r.label = 35;
672
+ case 35:
673
+ _f = _e.next();
674
+ return [3 /*break*/, 33];
675
+ case 36: return [3 /*break*/, 39];
676
+ case 37:
677
+ e_3_1 = _r.sent();
678
+ e_3 = { error: e_3_1 };
679
+ return [3 /*break*/, 39];
680
+ case 38:
681
+ try {
682
+ if (_f && !_f.done && (_o = _e.return)) _o.call(_e);
683
+ }
684
+ finally { if (e_3) throw e_3.error; }
685
+ return [7 /*endfinally*/];
686
+ case 39:
687
+ lineItemService = this.lineItemService_.withTransaction(manager);
688
+ _r.label = 40;
689
+ case 40:
690
+ _r.trys.push([40, 45, 46, 47]);
691
+ _g = __values(cart.items), _h = _g.next();
692
+ _r.label = 41;
693
+ case 41:
694
+ if (!!_h.done) return [3 /*break*/, 44];
695
+ item = _h.value;
696
+ return [4 /*yield*/, lineItemService.update(item.id, { order_id: result.id })];
697
+ case 42:
698
+ _r.sent();
699
+ _r.label = 43;
700
+ case 43:
701
+ _h = _g.next();
702
+ return [3 /*break*/, 41];
703
+ case 44: return [3 /*break*/, 47];
704
+ case 45:
705
+ e_4_1 = _r.sent();
706
+ e_4 = { error: e_4_1 };
707
+ return [3 /*break*/, 47];
708
+ case 46:
709
+ try {
710
+ if (_h && !_h.done && (_p = _g.return)) _p.call(_g);
711
+ }
712
+ finally { if (e_4) throw e_4.error; }
713
+ return [7 /*endfinally*/];
714
+ case 47:
715
+ _r.trys.push([47, 52, 53, 54]);
716
+ _j = __values(cart.items), _k = _j.next();
717
+ _r.label = 48;
718
+ case 48:
719
+ if (!!_k.done) return [3 /*break*/, 51];
720
+ item = _k.value;
721
+ return [4 /*yield*/, inventoryService.adjustInventory(item.variant_id, -item.quantity)];
722
+ case 49:
723
+ _r.sent();
724
+ _r.label = 50;
725
+ case 50:
726
+ _k = _j.next();
727
+ return [3 /*break*/, 48];
728
+ case 51: return [3 /*break*/, 54];
729
+ case 52:
730
+ e_5_1 = _r.sent();
731
+ e_5 = { error: e_5_1 };
732
+ return [3 /*break*/, 54];
733
+ case 53:
734
+ try {
735
+ if (_k && !_k.done && (_q = _j.return)) _q.call(_j);
736
+ }
737
+ finally { if (e_5) throw e_5.error; }
738
+ return [7 /*endfinally*/];
739
+ case 54: return [4 /*yield*/, this.eventBus_
656
740
  .withTransaction(manager)
657
- .updatePayment(payment.id, {
658
- order_id: result.id,
659
- })];
660
- case 21:
661
- _r.sent();
662
- _r.label = 22;
663
- case 22: return [4 /*yield*/, this.totalsService_.getGiftCardableAmount(cart)];
664
- case 23:
665
- gcBalance = _r.sent();
666
- gcService = this.giftCardService_.withTransaction(manager);
667
- _r.label = 24;
668
- case 24:
669
- _r.trys.push([24, 30, 31, 32]);
670
- _c = __values(cart.gift_cards), _d = _c.next();
671
- _r.label = 25;
672
- case 25:
673
- if (!!_d.done) return [3 /*break*/, 29];
674
- g = _d.value;
675
- newBalance = Math.max(0, g.balance - gcBalance);
676
- usage = g.balance - newBalance;
677
- return [4 /*yield*/, gcService.update(g.id, {
678
- balance: newBalance,
679
- disabled: newBalance === 0,
680
- })];
681
- case 26:
682
- _r.sent();
683
- return [4 /*yield*/, gcService.createTransaction({
684
- gift_card_id: g.id,
685
- order_id: result.id,
686
- amount: usage,
687
- is_taxable: cart.region.gift_cards_taxable,
688
- tax_rate: cart.region.gift_cards_taxable
689
- ? cart.region.tax_rate
690
- : null,
741
+ .emit(OrderService.Events.PLACED, {
742
+ id: result.id,
743
+ no_notification: result.no_notification,
691
744
  })];
692
- case 27:
693
- _r.sent();
694
- gcBalance = gcBalance - usage;
695
- _r.label = 28;
696
- case 28:
697
- _d = _c.next();
698
- return [3 /*break*/, 25];
699
- case 29: return [3 /*break*/, 32];
700
- case 30:
701
- e_2_1 = _r.sent();
702
- e_2 = { error: e_2_1 };
703
- return [3 /*break*/, 32];
704
- case 31:
705
- try {
706
- if (_d && !_d.done && (_m = _c.return)) _m.call(_c);
707
- }
708
- finally { if (e_2) throw e_2.error; }
709
- return [7 /*endfinally*/];
710
- case 32:
711
- _r.trys.push([32, 37, 38, 39]);
712
- _e = __values(cart.shipping_methods), _f = _e.next();
713
- _r.label = 33;
714
- case 33:
715
- if (!!_f.done) return [3 /*break*/, 36];
716
- method = _f.value;
717
- return [4 /*yield*/, this.shippingOptionService_
718
- .withTransaction(manager)
719
- .updateShippingMethod(method.id, { order_id: result.id })];
720
- case 34:
721
- _r.sent();
722
- _r.label = 35;
723
- case 35:
724
- _f = _e.next();
725
- return [3 /*break*/, 33];
726
- case 36: return [3 /*break*/, 39];
727
- case 37:
728
- e_3_1 = _r.sent();
729
- e_3 = { error: e_3_1 };
730
- return [3 /*break*/, 39];
731
- case 38:
732
- try {
733
- if (_f && !_f.done && (_o = _e.return)) _o.call(_e);
734
- }
735
- finally { if (e_3) throw e_3.error; }
736
- return [7 /*endfinally*/];
737
- case 39:
738
- lineItemService = this.lineItemService_.withTransaction(manager);
739
- _r.label = 40;
740
- case 40:
741
- _r.trys.push([40, 45, 46, 47]);
742
- _g = __values(cart.items), _h = _g.next();
743
- _r.label = 41;
744
- case 41:
745
- if (!!_h.done) return [3 /*break*/, 44];
746
- item = _h.value;
747
- return [4 /*yield*/, lineItemService.update(item.id, { order_id: result.id })];
748
- case 42:
749
- _r.sent();
750
- _r.label = 43;
751
- case 43:
752
- _h = _g.next();
753
- return [3 /*break*/, 41];
754
- case 44: return [3 /*break*/, 47];
755
- case 45:
756
- e_4_1 = _r.sent();
757
- e_4 = { error: e_4_1 };
758
- return [3 /*break*/, 47];
759
- case 46:
760
- try {
761
- if (_h && !_h.done && (_p = _g.return)) _p.call(_g);
762
- }
763
- finally { if (e_4) throw e_4.error; }
764
- return [7 /*endfinally*/];
765
- case 47:
766
- _r.trys.push([47, 52, 53, 54]);
767
- _j = __values(cart.items), _k = _j.next();
768
- _r.label = 48;
769
- case 48:
770
- if (!!_k.done) return [3 /*break*/, 51];
771
- item = _k.value;
772
- return [4 /*yield*/, inventoryService.adjustInventory(item.variant_id, -item.quantity)];
773
- case 49:
774
- _r.sent();
775
- _r.label = 50;
776
- case 50:
777
- _k = _j.next();
778
- return [3 /*break*/, 48];
779
- case 51: return [3 /*break*/, 54];
780
- case 52:
781
- e_5_1 = _r.sent();
782
- e_5 = { error: e_5_1 };
783
- return [3 /*break*/, 54];
784
- case 53:
785
- try {
786
- if (_k && !_k.done && (_q = _j.return)) _q.call(_j);
787
- }
788
- finally { if (e_5) throw e_5.error; }
789
- return [7 /*endfinally*/];
790
- case 54: return [4 /*yield*/, this.eventBus_
791
- .withTransaction(manager)
792
- .emit(OrderService.Events.PLACED, {
793
- id: result.id,
794
- no_notification: result.no_notification,
795
- })];
796
- case 55:
797
- _r.sent();
798
- return [4 /*yield*/, cartService.update(cart.id, { completed_at: new Date() })];
799
- case 56:
800
- _r.sent();
801
- return [2 /*return*/, result];
802
- }
803
- });
804
- }); })];
745
+ case 55:
746
+ _r.sent();
747
+ return [4 /*yield*/, cartService.update(cart.id, { completed_at: new Date() })];
748
+ case 56:
749
+ _r.sent();
750
+ return [2 /*return*/, result];
751
+ }
752
+ });
753
+ }); })];
754
+ case 1: return [2 /*return*/, _a.sent()];
755
+ }
805
756
  });
806
757
  });
807
758
  };
@@ -809,14 +760,12 @@ var OrderService = /** @class */ (function (_super) {
809
760
  * Adds a shipment to the order to indicate that an order has left the
810
761
  * warehouse. Will ask the fulfillment provider for any documents that may
811
762
  * have been created in regards to the shipment.
812
- * @param {string} orderId - the id of the order that has been shipped
813
- * @param {string} fulfillmentId - the fulfillment that has now been shipped
814
- * @param {TrackingLink[] | undefined} trackingLinks - array of tracking numebers
763
+ * @param orderId - the id of the order that has been shipped
764
+ * @param fulfillmentId - the fulfillment that has now been shipped
765
+ * @param trackingLinks - array of tracking numebers
815
766
  * associated with the shipment
816
- * @param {Object} config - the config of the order that has been shipped
817
- * @param {Dictionary<String, String>} metadata - optional metadata to add to
818
- * the fulfillment
819
- * @return {order} the resulting order following the update.
767
+ * @param config - the config of the order that has been shipped
768
+ * @return the resulting order following the update.
820
769
  */
821
770
  OrderService.prototype.createShipment = function (orderId, fulfillmentId, trackingLinks, config) {
822
771
  if (config === void 0) { config = {
@@ -827,187 +776,162 @@ var OrderService = /** @class */ (function (_super) {
827
776
  var metadata, no_notification;
828
777
  var _this = this;
829
778
  return __generator(this, function (_a) {
830
- metadata = config.metadata, no_notification = config.no_notification;
831
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
832
- var order, shipment, evaluatedNoNotification, shipmentRes, _loop_1, this_1, _a, _b, item, e_6_1, orderRepo, result;
833
- var e_6, _c;
834
- return __generator(this, function (_d) {
835
- switch (_d.label) {
836
- case 0: return [4 /*yield*/, this.retrieve(orderId, { relations: ["items"] })];
837
- case 1:
838
- order = _d.sent();
839
- return [4 /*yield*/, this.fulfillmentService_.retrieve(fulfillmentId)];
840
- case 2:
841
- shipment = _d.sent();
842
- if (order.status === "canceled") {
843
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A canceled order cannot be fulfilled as shipped");
844
- }
845
- if (!shipment || shipment.order_id !== orderId) {
846
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Could not find fulfillment");
847
- }
848
- evaluatedNoNotification = no_notification !== undefined
849
- ? no_notification
850
- : shipment.no_notification;
851
- return [4 /*yield*/, this.fulfillmentService_
852
- .withTransaction(manager)
853
- .createShipment(fulfillmentId, trackingLinks, {
854
- metadata: metadata,
855
- no_notification: evaluatedNoNotification,
856
- })];
857
- case 3:
858
- shipmentRes = _d.sent();
859
- order.fulfillment_status = "shipped";
860
- _loop_1 = function (item) {
861
- var shipped, shippedQty;
862
- return __generator(this, function (_e) {
863
- switch (_e.label) {
864
- case 0:
865
- shipped = shipmentRes.items.find(function (si) { return si.item_id === item.id; });
866
- if (!shipped) return [3 /*break*/, 2];
867
- shippedQty = (item.shipped_quantity || 0) + shipped.quantity;
868
- if (shippedQty !== item.quantity) {
869
- order.fulfillment_status = "partially_shipped";
870
- }
871
- return [4 /*yield*/, this_1.lineItemService_.withTransaction(manager).update(item.id, {
872
- shipped_quantity: shippedQty,
873
- })];
874
- case 1:
875
- _e.sent();
876
- return [3 /*break*/, 3];
877
- case 2:
878
- if (item.shipped_quantity !== item.quantity) {
879
- order.fulfillment_status = "partially_shipped";
779
+ switch (_a.label) {
780
+ case 0:
781
+ metadata = config.metadata, no_notification = config.no_notification;
782
+ return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
783
+ var order, shipment, evaluatedNoNotification, shipmentRes, _loop_1, this_1, _a, _b, item, e_6_1, orderRepo, result;
784
+ var e_6, _c;
785
+ return __generator(this, function (_d) {
786
+ switch (_d.label) {
787
+ case 0: return [4 /*yield*/, this.retrieve(orderId, { relations: ["items"] })];
788
+ case 1:
789
+ order = _d.sent();
790
+ return [4 /*yield*/, this.fulfillmentService_
791
+ .withTransaction(manager)
792
+ .retrieve(fulfillmentId)];
793
+ case 2:
794
+ shipment = _d.sent();
795
+ if (order.status === "canceled") {
796
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A canceled order cannot be fulfilled as shipped");
797
+ }
798
+ if (!shipment || shipment.order_id !== orderId) {
799
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Could not find fulfillment");
800
+ }
801
+ evaluatedNoNotification = no_notification !== undefined
802
+ ? no_notification
803
+ : shipment.no_notification;
804
+ return [4 /*yield*/, this.fulfillmentService_
805
+ .withTransaction(manager)
806
+ .createShipment(fulfillmentId, trackingLinks, {
807
+ metadata: metadata,
808
+ no_notification: evaluatedNoNotification,
809
+ })];
810
+ case 3:
811
+ shipmentRes = _d.sent();
812
+ order.fulfillment_status = models_1.FulfillmentStatus.SHIPPED;
813
+ _loop_1 = function (item) {
814
+ var shipped, shippedQty;
815
+ return __generator(this, function (_e) {
816
+ switch (_e.label) {
817
+ case 0:
818
+ shipped = shipmentRes.items.find(function (si) { return si.item_id === item.id; });
819
+ if (!shipped) return [3 /*break*/, 2];
820
+ shippedQty = (item.shipped_quantity || 0) + shipped.quantity;
821
+ if (shippedQty !== item.quantity) {
822
+ order.fulfillment_status = models_1.FulfillmentStatus.PARTIALLY_SHIPPED;
823
+ }
824
+ return [4 /*yield*/, this_1.lineItemService_.withTransaction(manager).update(item.id, {
825
+ shipped_quantity: shippedQty,
826
+ })];
827
+ case 1:
828
+ _e.sent();
829
+ return [3 /*break*/, 3];
830
+ case 2:
831
+ if (item.shipped_quantity !== item.quantity) {
832
+ order.fulfillment_status = models_1.FulfillmentStatus.PARTIALLY_SHIPPED;
833
+ }
834
+ _e.label = 3;
835
+ case 3: return [2 /*return*/];
880
836
  }
881
- _e.label = 3;
882
- case 3: return [2 /*return*/];
837
+ });
838
+ };
839
+ this_1 = this;
840
+ _d.label = 4;
841
+ case 4:
842
+ _d.trys.push([4, 9, 10, 11]);
843
+ _a = __values(order.items), _b = _a.next();
844
+ _d.label = 5;
845
+ case 5:
846
+ if (!!_b.done) return [3 /*break*/, 8];
847
+ item = _b.value;
848
+ return [5 /*yield**/, _loop_1(item)];
849
+ case 6:
850
+ _d.sent();
851
+ _d.label = 7;
852
+ case 7:
853
+ _b = _a.next();
854
+ return [3 /*break*/, 5];
855
+ case 8: return [3 /*break*/, 11];
856
+ case 9:
857
+ e_6_1 = _d.sent();
858
+ e_6 = { error: e_6_1 };
859
+ return [3 /*break*/, 11];
860
+ case 10:
861
+ try {
862
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
883
863
  }
884
- });
885
- };
886
- this_1 = this;
887
- _d.label = 4;
888
- case 4:
889
- _d.trys.push([4, 9, 10, 11]);
890
- _a = __values(order.items), _b = _a.next();
891
- _d.label = 5;
892
- case 5:
893
- if (!!_b.done) return [3 /*break*/, 8];
894
- item = _b.value;
895
- return [5 /*yield**/, _loop_1(item)];
896
- case 6:
897
- _d.sent();
898
- _d.label = 7;
899
- case 7:
900
- _b = _a.next();
901
- return [3 /*break*/, 5];
902
- case 8: return [3 /*break*/, 11];
903
- case 9:
904
- e_6_1 = _d.sent();
905
- e_6 = { error: e_6_1 };
906
- return [3 /*break*/, 11];
907
- case 10:
908
- try {
909
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
864
+ finally { if (e_6) throw e_6.error; }
865
+ return [7 /*endfinally*/];
866
+ case 11:
867
+ orderRepo = manager.getCustomRepository(this.orderRepository_);
868
+ return [4 /*yield*/, orderRepo.save(order)];
869
+ case 12:
870
+ result = _d.sent();
871
+ return [4 /*yield*/, this.eventBus_
872
+ .withTransaction(manager)
873
+ .emit(OrderService.Events.SHIPMENT_CREATED, {
874
+ id: orderId,
875
+ fulfillment_id: shipmentRes.id,
876
+ no_notification: evaluatedNoNotification,
877
+ })];
878
+ case 13:
879
+ _d.sent();
880
+ return [2 /*return*/, result];
910
881
  }
911
- finally { if (e_6) throw e_6.error; }
912
- return [7 /*endfinally*/];
913
- case 11:
914
- orderRepo = manager.getCustomRepository(this.orderRepository_);
915
- return [4 /*yield*/, orderRepo.save(order)];
916
- case 12:
917
- result = _d.sent();
918
- return [4 /*yield*/, this.eventBus_
919
- .withTransaction(manager)
920
- .emit(OrderService.Events.SHIPMENT_CREATED, {
921
- id: orderId,
922
- fulfillment_id: shipmentRes.id,
923
- no_notification: evaluatedNoNotification,
924
- })];
925
- case 13:
926
- _d.sent();
927
- return [2 /*return*/, result];
928
- }
929
- });
930
- }); })];
931
- });
932
- });
933
- };
934
- /**
935
- * Creates an order
936
- * @param {object} data - the data to create an order
937
- * @return {Promise} resolves to the creation result.
938
- */
939
- OrderService.prototype.create = function (data) {
940
- return __awaiter(this, void 0, void 0, function () {
941
- var _this = this;
942
- return __generator(this, function (_a) {
943
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
944
- var orderRepo, order, result;
945
- return __generator(this, function (_a) {
946
- switch (_a.label) {
947
- case 0:
948
- orderRepo = manager.getCustomRepository(this.orderRepository_);
949
- order = orderRepo.create(data);
950
- return [4 /*yield*/, orderRepo.save(order)];
951
- case 1:
952
- result = _a.sent();
953
- return [4 /*yield*/, this.eventBus_
954
- .withTransaction(manager)
955
- .emit(OrderService.Events.PLACED, {
956
- id: result.id,
957
- no_notification: order.no_notification,
958
- })];
959
- case 2:
960
- _a.sent();
961
- return [2 /*return*/, result];
962
- }
963
- });
964
- }); })];
882
+ });
883
+ }); })];
884
+ case 1: return [2 /*return*/, _a.sent()];
885
+ }
965
886
  });
966
887
  });
967
888
  };
968
889
  /**
969
890
  * Updates the order's billing address.
970
- * @param {object} order - the order to update
971
- * @param {object} address - the value to set the billing address to
972
- * @return {Promise} the result of the update operation
891
+ * @param order - the order to update
892
+ * @param address - the value to set the billing address to
893
+ * @return the result of the update operation
973
894
  */
974
- OrderService.prototype.updateBillingAddress_ = function (order, address) {
895
+ OrderService.prototype.updateBillingAddress = function (order, address) {
896
+ var _a, _b, _c, _d;
975
897
  return __awaiter(this, void 0, void 0, function () {
976
898
  var addrRepo, region, addr, created;
977
- return __generator(this, function (_a) {
978
- switch (_a.label) {
899
+ return __generator(this, function (_e) {
900
+ switch (_e.label) {
979
901
  case 0:
980
902
  addrRepo = this.manager_.getCustomRepository(this.addressRepository_);
981
- address.country_code = address.country_code.toLowerCase();
982
- return [4 /*yield*/, this.regionService_.retrieve(order.region_id, {
903
+ address.country_code = (_b = (_a = address.country_code) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : null;
904
+ return [4 /*yield*/, this.regionService_
905
+ .withTransaction(this.manager_)
906
+ .retrieve(order.region_id, {
983
907
  relations: ["countries"],
984
908
  })];
985
909
  case 1:
986
- region = _a.sent();
910
+ region = _e.sent();
987
911
  if (!region.countries.find(function (_a) {
988
912
  var iso_2 = _a.iso_2;
989
913
  return address.country_code === iso_2;
990
914
  })) {
991
915
  throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Shipping country must be in the order region");
992
916
  }
993
- address.country_code = address.country_code.toLowerCase();
917
+ address.country_code = (_d = (_c = address.country_code) === null || _c === void 0 ? void 0 : _c.toLowerCase()) !== null && _d !== void 0 ? _d : null;
994
918
  if (!order.billing_address_id) return [3 /*break*/, 4];
995
919
  return [4 /*yield*/, addrRepo.findOne({
996
920
  where: { id: order.billing_address_id },
997
921
  })];
998
922
  case 2:
999
- addr = _a.sent();
923
+ addr = _e.sent();
1000
924
  return [4 /*yield*/, addrRepo.save(__assign(__assign({}, addr), address))];
1001
925
  case 3:
1002
- _a.sent();
926
+ _e.sent();
1003
927
  return [3 /*break*/, 7];
1004
928
  case 4: return [4 /*yield*/, addrRepo.create(__assign({}, address))];
1005
929
  case 5:
1006
- created = _a.sent();
930
+ created = _e.sent();
1007
931
  return [4 /*yield*/, addrRepo.save(created)];
1008
932
  case 6:
1009
- _a.sent();
1010
- _a.label = 7;
933
+ _e.sent();
934
+ _e.label = 7;
1011
935
  case 7: return [2 /*return*/];
1012
936
  }
1013
937
  });
@@ -1015,23 +939,26 @@ var OrderService = /** @class */ (function (_super) {
1015
939
  };
1016
940
  /**
1017
941
  * Updates the order's shipping address.
1018
- * @param {object} order - the order to update
1019
- * @param {object} address - the value to set the shipping address to
1020
- * @return {Promise} the result of the update operation
942
+ * @param order - the order to update
943
+ * @param address - the value to set the shipping address to
944
+ * @return the result of the update operation
1021
945
  */
1022
- OrderService.prototype.updateShippingAddress_ = function (order, address) {
946
+ OrderService.prototype.updateShippingAddress = function (order, address) {
947
+ var _a, _b;
1023
948
  return __awaiter(this, void 0, void 0, function () {
1024
949
  var addrRepo, region, addr, created;
1025
- return __generator(this, function (_a) {
1026
- switch (_a.label) {
950
+ return __generator(this, function (_c) {
951
+ switch (_c.label) {
1027
952
  case 0:
1028
953
  addrRepo = this.manager_.getCustomRepository(this.addressRepository_);
1029
- address.country_code = address.country_code.toLowerCase();
1030
- return [4 /*yield*/, this.regionService_.retrieve(order.region_id, {
954
+ address.country_code = (_b = (_a = address.country_code) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : null;
955
+ return [4 /*yield*/, this.regionService_
956
+ .withTransaction(this.manager_)
957
+ .retrieve(order.region_id, {
1031
958
  relations: ["countries"],
1032
959
  })];
1033
960
  case 1:
1034
- region = _a.sent();
961
+ region = _c.sent();
1035
962
  if (!region.countries.find(function (_a) {
1036
963
  var iso_2 = _a.iso_2;
1037
964
  return address.country_code === iso_2;
@@ -1043,17 +970,17 @@ var OrderService = /** @class */ (function (_super) {
1043
970
  where: { id: order.shipping_address_id },
1044
971
  })];
1045
972
  case 2:
1046
- addr = _a.sent();
973
+ addr = _c.sent();
1047
974
  return [4 /*yield*/, addrRepo.save(__assign(__assign({}, addr), address))];
1048
975
  case 3:
1049
- _a.sent();
976
+ _c.sent();
1050
977
  return [3 /*break*/, 6];
1051
978
  case 4:
1052
979
  created = addrRepo.create(__assign({}, address));
1053
980
  return [4 /*yield*/, addrRepo.save(created)];
1054
981
  case 5:
1055
- _a.sent();
1056
- _a.label = 6;
982
+ _c.sent();
983
+ _c.label = 6;
1057
984
  case 6: return [2 /*return*/];
1058
985
  }
1059
986
  });
@@ -1064,79 +991,82 @@ var OrderService = /** @class */ (function (_super) {
1064
991
  return __awaiter(this, void 0, void 0, function () {
1065
992
  var _this = this;
1066
993
  return __generator(this, function (_a) {
1067
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1068
- var order, shipping_methods, newMethod, methods, shipping_methods_1, shipping_methods_1_1, sm, e_7_1, result;
1069
- var e_7, _a;
1070
- return __generator(this, function (_b) {
1071
- switch (_b.label) {
1072
- case 0: return [4 /*yield*/, this.retrieve(orderId, {
1073
- select: ["subtotal"],
1074
- relations: [
1075
- "shipping_methods",
1076
- "shipping_methods.shipping_option",
1077
- "items",
1078
- "items.variant",
1079
- "items.variant.product",
1080
- ],
1081
- })];
1082
- case 1:
1083
- order = _b.sent();
1084
- shipping_methods = order.shipping_methods;
1085
- if (order.status === "canceled") {
1086
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A shipping method cannot be added to a canceled order");
1087
- }
1088
- return [4 /*yield*/, this.shippingOptionService_
1089
- .withTransaction(manager)
1090
- .createShippingMethod(optionId, data, __assign({ order: order }, config))];
1091
- case 2:
1092
- newMethod = _b.sent();
1093
- methods = [newMethod];
1094
- if (!shipping_methods.length) return [3 /*break*/, 11];
1095
- _b.label = 3;
1096
- case 3:
1097
- _b.trys.push([3, 9, 10, 11]);
1098
- shipping_methods_1 = __values(shipping_methods), shipping_methods_1_1 = shipping_methods_1.next();
1099
- _b.label = 4;
1100
- case 4:
1101
- if (!!shipping_methods_1_1.done) return [3 /*break*/, 8];
1102
- sm = shipping_methods_1_1.value;
1103
- if (!(sm.shipping_option.profile_id ===
1104
- newMethod.shipping_option.profile_id)) return [3 /*break*/, 6];
1105
- return [4 /*yield*/, this.shippingOptionService_
1106
- .withTransaction(manager)
1107
- .deleteShippingMethods(sm)];
1108
- case 5:
1109
- _b.sent();
1110
- return [3 /*break*/, 7];
1111
- case 6:
1112
- methods.push(sm);
1113
- _b.label = 7;
1114
- case 7:
1115
- shipping_methods_1_1 = shipping_methods_1.next();
1116
- return [3 /*break*/, 4];
1117
- case 8: return [3 /*break*/, 11];
1118
- case 9:
1119
- e_7_1 = _b.sent();
1120
- e_7 = { error: e_7_1 };
1121
- return [3 /*break*/, 11];
1122
- case 10:
1123
- try {
1124
- if (shipping_methods_1_1 && !shipping_methods_1_1.done && (_a = shipping_methods_1.return)) _a.call(shipping_methods_1);
1125
- }
1126
- finally { if (e_7) throw e_7.error; }
1127
- return [7 /*endfinally*/];
1128
- case 11: return [4 /*yield*/, this.retrieve(orderId)];
1129
- case 12:
1130
- result = _b.sent();
1131
- return [4 /*yield*/, this.eventBus_
1132
- .withTransaction(manager)
1133
- .emit(OrderService.Events.UPDATED, { id: result.id })];
1134
- case 13:
1135
- _b.sent();
1136
- return [2 /*return*/, result];
1137
- }
1138
- });
1139
- }); })];
994
+ switch (_a.label) {
995
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
996
+ var order, shipping_methods, newMethod, methods, shipping_methods_1, shipping_methods_1_1, sm, e_7_1, result;
997
+ var e_7, _a;
998
+ return __generator(this, function (_b) {
999
+ switch (_b.label) {
1000
+ case 0: return [4 /*yield*/, this.retrieve(orderId, {
1001
+ select: ["subtotal"],
1002
+ relations: [
1003
+ "shipping_methods",
1004
+ "shipping_methods.shipping_option",
1005
+ "items",
1006
+ "items.variant",
1007
+ "items.variant.product",
1008
+ ],
1009
+ })];
1010
+ case 1:
1011
+ order = _b.sent();
1012
+ shipping_methods = order.shipping_methods;
1013
+ if (order.status === "canceled") {
1014
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A shipping method cannot be added to a canceled order");
1015
+ }
1016
+ return [4 /*yield*/, this.shippingOptionService_
1017
+ .withTransaction(manager)
1018
+ .createShippingMethod(optionId, data !== null && data !== void 0 ? data : {}, __assign({ order: order }, config))];
1019
+ case 2:
1020
+ newMethod = _b.sent();
1021
+ methods = [newMethod];
1022
+ if (!shipping_methods.length) return [3 /*break*/, 11];
1023
+ _b.label = 3;
1024
+ case 3:
1025
+ _b.trys.push([3, 9, 10, 11]);
1026
+ shipping_methods_1 = __values(shipping_methods), shipping_methods_1_1 = shipping_methods_1.next();
1027
+ _b.label = 4;
1028
+ case 4:
1029
+ if (!!shipping_methods_1_1.done) return [3 /*break*/, 8];
1030
+ sm = shipping_methods_1_1.value;
1031
+ if (!(sm.shipping_option.profile_id ===
1032
+ newMethod.shipping_option.profile_id)) return [3 /*break*/, 6];
1033
+ return [4 /*yield*/, this.shippingOptionService_
1034
+ .withTransaction(manager)
1035
+ .deleteShippingMethods(sm)];
1036
+ case 5:
1037
+ _b.sent();
1038
+ return [3 /*break*/, 7];
1039
+ case 6:
1040
+ methods.push(sm);
1041
+ _b.label = 7;
1042
+ case 7:
1043
+ shipping_methods_1_1 = shipping_methods_1.next();
1044
+ return [3 /*break*/, 4];
1045
+ case 8: return [3 /*break*/, 11];
1046
+ case 9:
1047
+ e_7_1 = _b.sent();
1048
+ e_7 = { error: e_7_1 };
1049
+ return [3 /*break*/, 11];
1050
+ case 10:
1051
+ try {
1052
+ if (shipping_methods_1_1 && !shipping_methods_1_1.done && (_a = shipping_methods_1.return)) _a.call(shipping_methods_1);
1053
+ }
1054
+ finally { if (e_7) throw e_7.error; }
1055
+ return [7 /*endfinally*/];
1056
+ case 11: return [4 /*yield*/, this.retrieve(orderId)];
1057
+ case 12:
1058
+ result = _b.sent();
1059
+ return [4 /*yield*/, this.eventBus_
1060
+ .withTransaction(manager)
1061
+ .emit(OrderService.Events.UPDATED, { id: result.id })];
1062
+ case 13:
1063
+ _b.sent();
1064
+ return [2 /*return*/, result];
1065
+ }
1066
+ });
1067
+ }); })];
1068
+ case 1: return [2 /*return*/, _a.sent()];
1069
+ }
1140
1070
  });
1141
1071
  });
1142
1072
  };
@@ -1144,111 +1074,114 @@ var OrderService = /** @class */ (function (_super) {
1144
1074
  * Updates an order. Metadata updates should
1145
1075
  * use dedicated method, e.g. `setMetadata` etc. The function
1146
1076
  * will throw errors if metadata updates are attempted.
1147
- * @param {string} orderId - the id of the order. Must be a string that
1077
+ * @param orderId - the id of the order. Must be a string that
1148
1078
  * can be casted to an ObjectId
1149
- * @param {object} update - an object with the update values.
1150
- * @return {Promise} resolves to the update result.
1079
+ * @param update - an object with the update values.
1080
+ * @return resolves to the update result.
1151
1081
  */
1152
1082
  OrderService.prototype.update = function (orderId, update) {
1153
1083
  return __awaiter(this, void 0, void 0, function () {
1154
1084
  var _this = this;
1155
1085
  return __generator(this, function (_a) {
1156
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1157
- var order, metadata, shipping_address, billing_address, no_notification, items, rest, items_1, items_1_1, item, e_8_1, _a, _b, _c, key, value, orderRepo, result;
1158
- var e_8, _d, e_9, _e;
1159
- return __generator(this, function (_f) {
1160
- switch (_f.label) {
1161
- case 0: return [4 /*yield*/, this.retrieve(orderId)];
1162
- case 1:
1163
- order = _f.sent();
1164
- if (order.status === "canceled") {
1165
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A canceled order cannot be updated");
1166
- }
1167
- if ((update.payment || update.items) &&
1168
- (order.fulfillment_status !== "not_fulfilled" ||
1169
- order.payment_status !== "awaiting" ||
1170
- order.status !== "pending")) {
1171
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Can't update shipping, billing, items and payment method when order is processed");
1172
- }
1173
- if (update.status || update.fulfillment_status || update.payment_status) {
1174
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Can't update order statuses. This will happen automatically. Use metadata in order for additional statuses");
1175
- }
1176
- metadata = update.metadata, shipping_address = update.shipping_address, billing_address = update.billing_address, no_notification = update.no_notification, items = update.items, rest = __rest(update, ["metadata", "shipping_address", "billing_address", "no_notification", "items"]);
1177
- if (update.metadata) {
1178
- order.metadata = this.setMetadata_(order, metadata);
1179
- }
1180
- if (!update.shipping_address) return [3 /*break*/, 3];
1181
- return [4 /*yield*/, this.updateShippingAddress_(order, shipping_address)];
1182
- case 2:
1183
- _f.sent();
1184
- _f.label = 3;
1185
- case 3:
1186
- if (!update.billing_address) return [3 /*break*/, 5];
1187
- return [4 /*yield*/, this.updateBillingAddress_(order, billing_address)];
1188
- case 4:
1189
- _f.sent();
1190
- _f.label = 5;
1191
- case 5:
1192
- if (update.no_notification) {
1193
- order.no_notification = no_notification;
1194
- }
1195
- if (!update.items) return [3 /*break*/, 13];
1196
- _f.label = 6;
1197
- case 6:
1198
- _f.trys.push([6, 11, 12, 13]);
1199
- items_1 = __values(items), items_1_1 = items_1.next();
1200
- _f.label = 7;
1201
- case 7:
1202
- if (!!items_1_1.done) return [3 /*break*/, 10];
1203
- item = items_1_1.value;
1204
- return [4 /*yield*/, this.lineItemService_.withTransaction(manager).create(__assign(__assign({}, item), { order_id: orderId }))];
1205
- case 8:
1206
- _f.sent();
1207
- _f.label = 9;
1208
- case 9:
1209
- items_1_1 = items_1.next();
1210
- return [3 /*break*/, 7];
1211
- case 10: return [3 /*break*/, 13];
1212
- case 11:
1213
- e_8_1 = _f.sent();
1214
- e_8 = { error: e_8_1 };
1215
- return [3 /*break*/, 13];
1216
- case 12:
1217
- try {
1218
- if (items_1_1 && !items_1_1.done && (_d = items_1.return)) _d.call(items_1);
1219
- }
1220
- finally { if (e_8) throw e_8.error; }
1221
- return [7 /*endfinally*/];
1222
- case 13:
1223
- try {
1224
- for (_a = __values(Object.entries(rest)), _b = _a.next(); !_b.done; _b = _a.next()) {
1225
- _c = __read(_b.value, 2), key = _c[0], value = _c[1];
1226
- order[key] = value;
1086
+ switch (_a.label) {
1087
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1088
+ var order, metadata, shipping_address, billing_address, no_notification, items, rest, _a, _b, item, e_8_1, _c, _d, _e, key, value, orderRepo, result;
1089
+ var e_8, _f, e_9, _g;
1090
+ return __generator(this, function (_h) {
1091
+ switch (_h.label) {
1092
+ case 0: return [4 /*yield*/, this.retrieve(orderId)];
1093
+ case 1:
1094
+ order = _h.sent();
1095
+ if (order.status === "canceled") {
1096
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A canceled order cannot be updated");
1227
1097
  }
1228
- }
1229
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
1230
- finally {
1098
+ if ((update.payment || update.items) &&
1099
+ (order.fulfillment_status !== "not_fulfilled" ||
1100
+ order.payment_status !== "awaiting" ||
1101
+ order.status !== "pending")) {
1102
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Can't update shipping, billing, items and payment method when order is processed");
1103
+ }
1104
+ if (update.status || update.fulfillment_status || update.payment_status) {
1105
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Can't update order statuses. This will happen automatically. Use metadata in order for additional statuses");
1106
+ }
1107
+ metadata = update.metadata, shipping_address = update.shipping_address, billing_address = update.billing_address, no_notification = update.no_notification, items = update.items, rest = __rest(update, ["metadata", "shipping_address", "billing_address", "no_notification", "items"]);
1108
+ if (update.metadata) {
1109
+ order.metadata = (0, utils_1.setMetadata)(order, metadata !== null && metadata !== void 0 ? metadata : {});
1110
+ }
1111
+ if (!update.shipping_address) return [3 /*break*/, 3];
1112
+ return [4 /*yield*/, this.updateShippingAddress(order, shipping_address)];
1113
+ case 2:
1114
+ _h.sent();
1115
+ _h.label = 3;
1116
+ case 3:
1117
+ if (!update.billing_address) return [3 /*break*/, 5];
1118
+ return [4 /*yield*/, this.updateBillingAddress(order, billing_address)];
1119
+ case 4:
1120
+ _h.sent();
1121
+ _h.label = 5;
1122
+ case 5:
1123
+ if (update.no_notification) {
1124
+ order.no_notification = no_notification !== null && no_notification !== void 0 ? no_notification : false;
1125
+ }
1126
+ if (!update.items) return [3 /*break*/, 13];
1127
+ _h.label = 6;
1128
+ case 6:
1129
+ _h.trys.push([6, 11, 12, 13]);
1130
+ _a = __values(items), _b = _a.next();
1131
+ _h.label = 7;
1132
+ case 7:
1133
+ if (!!_b.done) return [3 /*break*/, 10];
1134
+ item = _b.value;
1135
+ return [4 /*yield*/, this.lineItemService_.withTransaction(manager).create(__assign(__assign({}, item), { order_id: orderId }))];
1136
+ case 8:
1137
+ _h.sent();
1138
+ _h.label = 9;
1139
+ case 9:
1140
+ _b = _a.next();
1141
+ return [3 /*break*/, 7];
1142
+ case 10: return [3 /*break*/, 13];
1143
+ case 11:
1144
+ e_8_1 = _h.sent();
1145
+ e_8 = { error: e_8_1 };
1146
+ return [3 /*break*/, 13];
1147
+ case 12:
1231
1148
  try {
1232
- if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
1149
+ if (_b && !_b.done && (_f = _a.return)) _f.call(_a);
1233
1150
  }
1234
- finally { if (e_9) throw e_9.error; }
1235
- }
1236
- orderRepo = manager.getCustomRepository(this.orderRepository_);
1237
- return [4 /*yield*/, orderRepo.save(order)];
1238
- case 14:
1239
- result = _f.sent();
1240
- return [4 /*yield*/, this.eventBus_
1241
- .withTransaction(manager)
1242
- .emit(OrderService.Events.UPDATED, {
1243
- id: orderId,
1244
- no_notification: order.no_notification,
1245
- })];
1246
- case 15:
1247
- _f.sent();
1248
- return [2 /*return*/, result];
1249
- }
1250
- });
1251
- }); })];
1151
+ finally { if (e_8) throw e_8.error; }
1152
+ return [7 /*endfinally*/];
1153
+ case 13:
1154
+ try {
1155
+ for (_c = __values(Object.entries(rest)), _d = _c.next(); !_d.done; _d = _c.next()) {
1156
+ _e = __read(_d.value, 2), key = _e[0], value = _e[1];
1157
+ order[key] = value;
1158
+ }
1159
+ }
1160
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
1161
+ finally {
1162
+ try {
1163
+ if (_d && !_d.done && (_g = _c.return)) _g.call(_c);
1164
+ }
1165
+ finally { if (e_9) throw e_9.error; }
1166
+ }
1167
+ orderRepo = manager.getCustomRepository(this.orderRepository_);
1168
+ return [4 /*yield*/, orderRepo.save(order)];
1169
+ case 14:
1170
+ result = _h.sent();
1171
+ return [4 /*yield*/, this.eventBus_
1172
+ .withTransaction(manager)
1173
+ .emit(OrderService.Events.UPDATED, {
1174
+ id: orderId,
1175
+ no_notification: order.no_notification,
1176
+ })];
1177
+ case 15:
1178
+ _h.sent();
1179
+ return [2 /*return*/, result];
1180
+ }
1181
+ });
1182
+ }); })];
1183
+ case 1: return [2 /*return*/, _a.sent()];
1184
+ }
1252
1185
  });
1253
1186
  });
1254
1187
  };
@@ -1256,229 +1189,235 @@ var OrderService = /** @class */ (function (_super) {
1256
1189
  * Cancels an order.
1257
1190
  * Throws if fulfillment process has been initiated.
1258
1191
  * Throws if payment process has been initiated.
1259
- * @param {string} orderId - id of order to cancel.
1260
- * @return {Promise} result of the update operation.
1192
+ * @param orderId - id of order to cancel.
1193
+ * @return result of the update operation.
1261
1194
  */
1262
1195
  OrderService.prototype.cancel = function (orderId) {
1263
1196
  return __awaiter(this, void 0, void 0, function () {
1264
1197
  var _this = this;
1265
1198
  return __generator(this, function (_a) {
1266
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1267
- var order, throwErrorIf, notCanceled, _a, _b, item, e_10_1, _c, _d, p, e_11_1, orderRepo, result;
1268
- var e_10, _e, e_11, _f;
1269
- var _g;
1270
- return __generator(this, function (_h) {
1271
- switch (_h.label) {
1272
- case 0: return [4 /*yield*/, this.retrieve(orderId, {
1273
- relations: [
1274
- "fulfillments",
1275
- "payments",
1276
- "returns",
1277
- "claims",
1278
- "swaps",
1279
- "items",
1280
- ],
1281
- })];
1282
- case 1:
1283
- order = _h.sent();
1284
- if (((_g = order.refunds) === null || _g === void 0 ? void 0 : _g.length) > 0) {
1285
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Order with refund(s) cannot be canceled");
1286
- }
1287
- throwErrorIf = function (arr, pred, type) {
1288
- return arr === null || arr === void 0 ? void 0 : arr.filter(pred).find(function (_) {
1289
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "All ".concat(type, " must be canceled before canceling an order"));
1290
- });
1291
- };
1292
- notCanceled = function (o) { return !o.canceled_at; };
1293
- throwErrorIf(order.fulfillments, notCanceled, "fulfillments");
1294
- throwErrorIf(order.returns, function (r) { return r.status !== "canceled"; }, "returns");
1295
- throwErrorIf(order.swaps, notCanceled, "swaps");
1296
- throwErrorIf(order.claims, notCanceled, "claims");
1297
- _h.label = 2;
1298
- case 2:
1299
- _h.trys.push([2, 7, 8, 9]);
1300
- _a = __values(order.items), _b = _a.next();
1301
- _h.label = 3;
1302
- case 3:
1303
- if (!!_b.done) return [3 /*break*/, 6];
1304
- item = _b.value;
1305
- return [4 /*yield*/, this.inventoryService_
1306
- .withTransaction(manager)
1307
- .adjustInventory(item.variant_id, item.quantity)];
1308
- case 4:
1309
- _h.sent();
1310
- _h.label = 5;
1311
- case 5:
1312
- _b = _a.next();
1313
- return [3 /*break*/, 3];
1314
- case 6: return [3 /*break*/, 9];
1315
- case 7:
1316
- e_10_1 = _h.sent();
1317
- e_10 = { error: e_10_1 };
1318
- return [3 /*break*/, 9];
1319
- case 8:
1320
- try {
1321
- if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
1322
- }
1323
- finally { if (e_10) throw e_10.error; }
1324
- return [7 /*endfinally*/];
1325
- case 9:
1326
- _h.trys.push([9, 14, 15, 16]);
1327
- _c = __values(order.payments), _d = _c.next();
1328
- _h.label = 10;
1329
- case 10:
1330
- if (!!_d.done) return [3 /*break*/, 13];
1331
- p = _d.value;
1332
- return [4 /*yield*/, this.paymentProviderService_
1333
- .withTransaction(manager)
1334
- .cancelPayment(p)];
1335
- case 11:
1336
- _h.sent();
1337
- _h.label = 12;
1338
- case 12:
1339
- _d = _c.next();
1340
- return [3 /*break*/, 10];
1341
- case 13: return [3 /*break*/, 16];
1342
- case 14:
1343
- e_11_1 = _h.sent();
1344
- e_11 = { error: e_11_1 };
1345
- return [3 /*break*/, 16];
1346
- case 15:
1347
- try {
1348
- if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
1349
- }
1350
- finally { if (e_11) throw e_11.error; }
1351
- return [7 /*endfinally*/];
1352
- case 16:
1353
- order.status = "canceled";
1354
- order.fulfillment_status = "canceled";
1355
- order.payment_status = "canceled";
1356
- order.canceled_at = new Date();
1357
- orderRepo = manager.getCustomRepository(this.orderRepository_);
1358
- return [4 /*yield*/, orderRepo.save(order)];
1359
- case 17:
1360
- result = _h.sent();
1361
- return [4 /*yield*/, this.eventBus_
1362
- .withTransaction(manager)
1363
- .emit(OrderService.Events.CANCELED, {
1364
- id: order.id,
1365
- no_notification: order.no_notification,
1199
+ switch (_a.label) {
1200
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1201
+ var order, throwErrorIf, notCanceled, _a, _b, item, e_10_1, _c, _d, p, e_11_1, orderRepo, result;
1202
+ var e_10, _e, e_11, _f;
1203
+ var _g;
1204
+ return __generator(this, function (_h) {
1205
+ switch (_h.label) {
1206
+ case 0: return [4 /*yield*/, this.retrieve(orderId, {
1207
+ relations: [
1208
+ "fulfillments",
1209
+ "payments",
1210
+ "returns",
1211
+ "claims",
1212
+ "swaps",
1213
+ "items",
1214
+ ],
1366
1215
  })];
1367
- case 18:
1368
- _h.sent();
1369
- return [2 /*return*/, result];
1370
- }
1371
- });
1372
- }); })];
1216
+ case 1:
1217
+ order = _h.sent();
1218
+ if (((_g = order.refunds) === null || _g === void 0 ? void 0 : _g.length) > 0) {
1219
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Order with refund(s) cannot be canceled");
1220
+ }
1221
+ throwErrorIf = function (arr, pred, type) {
1222
+ if (arr === null || arr === void 0 ? void 0 : arr.filter(pred).length) {
1223
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "All ".concat(type, " must be canceled before canceling an order"));
1224
+ }
1225
+ };
1226
+ notCanceled = function (o) { return !o.canceled_at; };
1227
+ throwErrorIf(order.fulfillments, notCanceled, "fulfillments");
1228
+ throwErrorIf(order.returns, function (r) { return r.status !== "canceled"; }, "returns");
1229
+ throwErrorIf(order.swaps, notCanceled, "swaps");
1230
+ throwErrorIf(order.claims, notCanceled, "claims");
1231
+ _h.label = 2;
1232
+ case 2:
1233
+ _h.trys.push([2, 7, 8, 9]);
1234
+ _a = __values(order.items), _b = _a.next();
1235
+ _h.label = 3;
1236
+ case 3:
1237
+ if (!!_b.done) return [3 /*break*/, 6];
1238
+ item = _b.value;
1239
+ return [4 /*yield*/, this.inventoryService_
1240
+ .withTransaction(manager)
1241
+ .adjustInventory(item.variant_id, item.quantity)];
1242
+ case 4:
1243
+ _h.sent();
1244
+ _h.label = 5;
1245
+ case 5:
1246
+ _b = _a.next();
1247
+ return [3 /*break*/, 3];
1248
+ case 6: return [3 /*break*/, 9];
1249
+ case 7:
1250
+ e_10_1 = _h.sent();
1251
+ e_10 = { error: e_10_1 };
1252
+ return [3 /*break*/, 9];
1253
+ case 8:
1254
+ try {
1255
+ if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
1256
+ }
1257
+ finally { if (e_10) throw e_10.error; }
1258
+ return [7 /*endfinally*/];
1259
+ case 9:
1260
+ _h.trys.push([9, 14, 15, 16]);
1261
+ _c = __values(order.payments), _d = _c.next();
1262
+ _h.label = 10;
1263
+ case 10:
1264
+ if (!!_d.done) return [3 /*break*/, 13];
1265
+ p = _d.value;
1266
+ return [4 /*yield*/, this.paymentProviderService_
1267
+ .withTransaction(manager)
1268
+ .cancelPayment(p)];
1269
+ case 11:
1270
+ _h.sent();
1271
+ _h.label = 12;
1272
+ case 12:
1273
+ _d = _c.next();
1274
+ return [3 /*break*/, 10];
1275
+ case 13: return [3 /*break*/, 16];
1276
+ case 14:
1277
+ e_11_1 = _h.sent();
1278
+ e_11 = { error: e_11_1 };
1279
+ return [3 /*break*/, 16];
1280
+ case 15:
1281
+ try {
1282
+ if (_d && !_d.done && (_f = _c.return)) _f.call(_c);
1283
+ }
1284
+ finally { if (e_11) throw e_11.error; }
1285
+ return [7 /*endfinally*/];
1286
+ case 16:
1287
+ order.status = models_1.OrderStatus.CANCELED;
1288
+ order.fulfillment_status = models_1.FulfillmentStatus.CANCELED;
1289
+ order.payment_status = models_1.PaymentStatus.CANCELED;
1290
+ order.canceled_at = new Date();
1291
+ orderRepo = manager.getCustomRepository(this.orderRepository_);
1292
+ return [4 /*yield*/, orderRepo.save(order)];
1293
+ case 17:
1294
+ result = _h.sent();
1295
+ return [4 /*yield*/, this.eventBus_
1296
+ .withTransaction(manager)
1297
+ .emit(OrderService.Events.CANCELED, {
1298
+ id: order.id,
1299
+ no_notification: order.no_notification,
1300
+ })];
1301
+ case 18:
1302
+ _h.sent();
1303
+ return [2 /*return*/, result];
1304
+ }
1305
+ });
1306
+ }); })];
1307
+ case 1: return [2 /*return*/, _a.sent()];
1308
+ }
1373
1309
  });
1374
1310
  });
1375
1311
  };
1376
1312
  /**
1377
1313
  * Captures payment for an order.
1378
- * @param {string} orderId - id of order to capture payment for.
1379
- * @return {Promise} result of the update operation.
1314
+ * @param orderId - id of order to capture payment for.
1315
+ * @return result of the update operation.
1380
1316
  */
1381
1317
  OrderService.prototype.capturePayment = function (orderId) {
1382
1318
  return __awaiter(this, void 0, void 0, function () {
1383
1319
  var _this = this;
1384
1320
  return __generator(this, function (_a) {
1385
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1386
- var orderRepo, order, payments, _loop_2, this_2, _a, _b, p, e_12_1, result;
1387
- var e_12, _c;
1388
- var _this = this;
1389
- return __generator(this, function (_d) {
1390
- switch (_d.label) {
1391
- case 0:
1392
- orderRepo = manager.getCustomRepository(this.orderRepository_);
1393
- return [4 /*yield*/, this.retrieve(orderId, { relations: ["payments"] })];
1394
- case 1:
1395
- order = _d.sent();
1396
- if (order.status === "canceled") {
1397
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A canceled order cannot capture payment");
1398
- }
1399
- payments = [];
1400
- _loop_2 = function (p) {
1401
- var result_1;
1402
- return __generator(this, function (_e) {
1403
- switch (_e.label) {
1404
- case 0:
1405
- if (!(p.captured_at === null)) return [3 /*break*/, 2];
1406
- return [4 /*yield*/, this_2.paymentProviderService_
1407
- .withTransaction(manager)
1408
- .capturePayment(p)
1409
- .catch(function (err) {
1410
- _this.eventBus_
1321
+ switch (_a.label) {
1322
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1323
+ var orderRepo, order, payments, _loop_2, this_2, _a, _b, p, e_12_1, result;
1324
+ var e_12, _c;
1325
+ var _this = this;
1326
+ return __generator(this, function (_d) {
1327
+ switch (_d.label) {
1328
+ case 0:
1329
+ orderRepo = manager.getCustomRepository(this.orderRepository_);
1330
+ return [4 /*yield*/, this.retrieve(orderId, { relations: ["payments"] })];
1331
+ case 1:
1332
+ order = _d.sent();
1333
+ if (order.status === "canceled") {
1334
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A canceled order cannot capture payment");
1335
+ }
1336
+ payments = [];
1337
+ _loop_2 = function (p) {
1338
+ var result_1;
1339
+ return __generator(this, function (_e) {
1340
+ switch (_e.label) {
1341
+ case 0:
1342
+ if (!(p.captured_at === null)) return [3 /*break*/, 2];
1343
+ return [4 /*yield*/, this_2.paymentProviderService_
1411
1344
  .withTransaction(manager)
1412
- .emit(OrderService.Events.PAYMENT_CAPTURE_FAILED, {
1413
- id: orderId,
1414
- payment_id: p.id,
1415
- error: err,
1416
- no_notification: order.no_notification,
1417
- });
1418
- })];
1419
- case 1:
1420
- result_1 = _e.sent();
1421
- if (result_1) {
1422
- payments.push(result_1);
1423
- }
1424
- else {
1345
+ .capturePayment(p)
1346
+ .catch(function (err) {
1347
+ _this.eventBus_
1348
+ .withTransaction(manager)
1349
+ .emit(OrderService.Events.PAYMENT_CAPTURE_FAILED, {
1350
+ id: orderId,
1351
+ payment_id: p.id,
1352
+ error: err,
1353
+ no_notification: order.no_notification,
1354
+ });
1355
+ })];
1356
+ case 1:
1357
+ result_1 = _e.sent();
1358
+ if (result_1) {
1359
+ payments.push(result_1);
1360
+ }
1361
+ else {
1362
+ payments.push(p);
1363
+ }
1364
+ return [3 /*break*/, 3];
1365
+ case 2:
1425
1366
  payments.push(p);
1426
- }
1427
- return [3 /*break*/, 3];
1428
- case 2:
1429
- payments.push(p);
1430
- _e.label = 3;
1431
- case 3: return [2 /*return*/];
1432
- }
1433
- });
1434
- };
1435
- this_2 = this;
1436
- _d.label = 2;
1437
- case 2:
1438
- _d.trys.push([2, 7, 8, 9]);
1439
- _a = __values(order.payments), _b = _a.next();
1440
- _d.label = 3;
1441
- case 3:
1442
- if (!!_b.done) return [3 /*break*/, 6];
1443
- p = _b.value;
1444
- return [5 /*yield**/, _loop_2(p)];
1445
- case 4:
1446
- _d.sent();
1447
- _d.label = 5;
1448
- case 5:
1449
- _b = _a.next();
1450
- return [3 /*break*/, 3];
1451
- case 6: return [3 /*break*/, 9];
1452
- case 7:
1453
- e_12_1 = _d.sent();
1454
- e_12 = { error: e_12_1 };
1455
- return [3 /*break*/, 9];
1456
- case 8:
1457
- try {
1458
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
1459
- }
1460
- finally { if (e_12) throw e_12.error; }
1461
- return [7 /*endfinally*/];
1462
- case 9:
1463
- order.payments = payments;
1464
- order.payment_status = payments.every(function (p) { return p.captured_at !== null; })
1465
- ? "captured"
1466
- : "requires_action";
1467
- return [4 /*yield*/, orderRepo.save(order)];
1468
- case 10:
1469
- result = _d.sent();
1470
- if (order.payment_status === "captured") {
1471
- this.eventBus_
1472
- .withTransaction(manager)
1473
- .emit(OrderService.Events.PAYMENT_CAPTURED, {
1474
- id: result.id,
1475
- no_notification: order.no_notification,
1476
- });
1477
- }
1478
- return [2 /*return*/, result];
1479
- }
1480
- });
1481
- }); })];
1367
+ _e.label = 3;
1368
+ case 3: return [2 /*return*/];
1369
+ }
1370
+ });
1371
+ };
1372
+ this_2 = this;
1373
+ _d.label = 2;
1374
+ case 2:
1375
+ _d.trys.push([2, 7, 8, 9]);
1376
+ _a = __values(order.payments), _b = _a.next();
1377
+ _d.label = 3;
1378
+ case 3:
1379
+ if (!!_b.done) return [3 /*break*/, 6];
1380
+ p = _b.value;
1381
+ return [5 /*yield**/, _loop_2(p)];
1382
+ case 4:
1383
+ _d.sent();
1384
+ _d.label = 5;
1385
+ case 5:
1386
+ _b = _a.next();
1387
+ return [3 /*break*/, 3];
1388
+ case 6: return [3 /*break*/, 9];
1389
+ case 7:
1390
+ e_12_1 = _d.sent();
1391
+ e_12 = { error: e_12_1 };
1392
+ return [3 /*break*/, 9];
1393
+ case 8:
1394
+ try {
1395
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
1396
+ }
1397
+ finally { if (e_12) throw e_12.error; }
1398
+ return [7 /*endfinally*/];
1399
+ case 9:
1400
+ order.payments = payments;
1401
+ order.payment_status = payments.every(function (p) { return p.captured_at !== null; })
1402
+ ? models_1.PaymentStatus.CAPTURED
1403
+ : models_1.PaymentStatus.REQUIRES_ACTION;
1404
+ return [4 /*yield*/, orderRepo.save(order)];
1405
+ case 10:
1406
+ result = _d.sent();
1407
+ if (order.payment_status === models_1.PaymentStatus.CAPTURED) {
1408
+ this.eventBus_
1409
+ .withTransaction(manager)
1410
+ .emit(OrderService.Events.PAYMENT_CAPTURED, {
1411
+ id: result.id,
1412
+ no_notification: order.no_notification,
1413
+ });
1414
+ }
1415
+ return [2 /*return*/, result];
1416
+ }
1417
+ });
1418
+ }); })];
1419
+ case 1: return [2 /*return*/, _a.sent()];
1420
+ }
1482
1421
  });
1483
1422
  });
1484
1423
  };
@@ -1487,13 +1426,13 @@ var OrderService = /** @class */ (function (_super) {
1487
1426
  * fulfillable quantity is lower than the requested fulfillment quantity.
1488
1427
  * Fulfillable quantity is calculated by subtracting the already fulfilled
1489
1428
  * quantity from the quantity that was originally purchased.
1490
- * @param {LineItem} item - the line item to check has sufficient fulfillable
1429
+ * @param item - the line item to check has sufficient fulfillable
1491
1430
  * quantity.
1492
- * @param {number} quantity - the quantity that is requested to be fulfilled.
1493
- * @return {LineItem} a line item that has the requested fulfillment quantity
1431
+ * @param quantity - the quantity that is requested to be fulfilled.
1432
+ * @return a line item that has the requested fulfillment quantity
1494
1433
  * set.
1495
1434
  */
1496
- OrderService.prototype.validateFulfillmentLineItem_ = function (item, quantity) {
1435
+ OrderService.prototype.validateFulfillmentLineItem = function (item, quantity) {
1497
1436
  if (!item) {
1498
1437
  // This will in most cases be called by a webhook so to ensure that
1499
1438
  // things go through smoothly in instances where extra items outside
@@ -1510,10 +1449,10 @@ var OrderService = /** @class */ (function (_super) {
1510
1449
  * In a situation where the order has more than one shipping method,
1511
1450
  * we need to partition the order items, such that they can be sent
1512
1451
  * to their respective fulfillment provider.
1513
- * @param {string} orderId - id of order to cancel.
1514
- * @param {Object} itemsToFulfill - items to fulfil.
1515
- * @param {Object} config - the config to cancel.
1516
- * @return {Promise} result of the update operation.
1452
+ * @param orderId - id of order to cancel.
1453
+ * @param itemsToFulfill - items to fulfil.
1454
+ * @param config - the config to cancel.
1455
+ * @return result of the update operation.
1517
1456
  */
1518
1457
  OrderService.prototype.createFulfillment = function (orderId, itemsToFulfill, config) {
1519
1458
  if (config === void 0) { config = {
@@ -1524,229 +1463,235 @@ var OrderService = /** @class */ (function (_super) {
1524
1463
  var metadata, no_notification;
1525
1464
  var _this = this;
1526
1465
  return __generator(this, function (_a) {
1527
- metadata = config.metadata, no_notification = config.no_notification;
1528
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1529
- var order, fulfillments, successfullyFulfilled, fulfillments_1, fulfillments_1_1, f, _loop_3, this_3, _a, _b, item, e_13_1, orderRepo, result, evaluatedNoNotification, fulfillments_2, fulfillments_2_1, fulfillment, e_14_1;
1530
- var e_15, _c, e_13, _d, e_14, _e;
1531
- var _f;
1532
- return __generator(this, function (_g) {
1533
- switch (_g.label) {
1534
- case 0: return [4 /*yield*/, this.retrieve(orderId, {
1535
- select: [
1536
- "subtotal",
1537
- "shipping_total",
1538
- "discount_total",
1539
- "tax_total",
1540
- "gift_card_total",
1541
- "total",
1542
- ],
1543
- relations: [
1544
- "discounts",
1545
- "discounts.rule",
1546
- "region",
1547
- "fulfillments",
1548
- "shipping_address",
1549
- "billing_address",
1550
- "shipping_methods",
1551
- "shipping_methods.shipping_option",
1552
- "items",
1553
- "items.adjustments",
1554
- "items.variant",
1555
- "items.variant.product",
1556
- "payments",
1557
- ],
1558
- })];
1559
- case 1:
1560
- order = _g.sent();
1561
- if (order.status === "canceled") {
1562
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A canceled order cannot be fulfilled");
1563
- }
1564
- if (!((_f = order.shipping_methods) === null || _f === void 0 ? void 0 : _f.length)) {
1565
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Cannot fulfill an order that lacks shipping methods");
1566
- }
1567
- return [4 /*yield*/, this.fulfillmentService_
1568
- .withTransaction(manager)
1569
- .createFulfillment(order, itemsToFulfill, {
1570
- metadata: metadata,
1571
- no_notification: no_notification,
1572
- order_id: orderId,
1573
- })];
1574
- case 2:
1575
- fulfillments = _g.sent();
1576
- successfullyFulfilled = [];
1577
- try {
1578
- for (fulfillments_1 = __values(fulfillments), fulfillments_1_1 = fulfillments_1.next(); !fulfillments_1_1.done; fulfillments_1_1 = fulfillments_1.next()) {
1579
- f = fulfillments_1_1.value;
1580
- successfullyFulfilled = __spreadArray(__spreadArray([], __read(successfullyFulfilled), false), __read(f.items), false);
1581
- }
1582
- }
1583
- catch (e_15_1) { e_15 = { error: e_15_1 }; }
1584
- finally {
1585
- try {
1586
- if (fulfillments_1_1 && !fulfillments_1_1.done && (_c = fulfillments_1.return)) _c.call(fulfillments_1);
1587
- }
1588
- finally { if (e_15) throw e_15.error; }
1589
- }
1590
- order.fulfillment_status = "fulfilled";
1591
- _loop_3 = function (item) {
1592
- var fulfillmentItem, fulfilledQuantity;
1593
- return __generator(this, function (_h) {
1594
- switch (_h.label) {
1595
- case 0:
1596
- fulfillmentItem = successfullyFulfilled.find(function (f) { return item.id === f.item_id; });
1597
- if (!fulfillmentItem) return [3 /*break*/, 2];
1598
- fulfilledQuantity = (item.fulfilled_quantity || 0) + fulfillmentItem.quantity;
1599
- // Update the fulfilled quantity
1600
- return [4 /*yield*/, this_3.lineItemService_.withTransaction(manager).update(item.id, {
1601
- fulfilled_quantity: fulfilledQuantity,
1602
- })];
1603
- case 1:
1604
- // Update the fulfilled quantity
1605
- _h.sent();
1606
- if (item.quantity !== fulfilledQuantity) {
1607
- order.fulfillment_status = "partially_fulfilled";
1608
- }
1609
- return [3 /*break*/, 3];
1610
- case 2:
1611
- if (item.quantity !== item.fulfilled_quantity) {
1612
- order.fulfillment_status = "partially_fulfilled";
1466
+ switch (_a.label) {
1467
+ case 0:
1468
+ metadata = config.metadata, no_notification = config.no_notification;
1469
+ return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1470
+ var order, fulfillments, successfullyFulfilled, fulfillments_1, fulfillments_1_1, f, _loop_3, this_3, _a, _b, item, e_13_1, orderRepo, result, evaluatedNoNotification, fulfillments_2, fulfillments_2_1, fulfillment, e_14_1;
1471
+ var e_15, _c, e_13, _d, e_14, _e;
1472
+ var _f;
1473
+ return __generator(this, function (_g) {
1474
+ switch (_g.label) {
1475
+ case 0: return [4 /*yield*/, this.retrieve(orderId, {
1476
+ select: [
1477
+ "subtotal",
1478
+ "shipping_total",
1479
+ "discount_total",
1480
+ "tax_total",
1481
+ "gift_card_total",
1482
+ "total",
1483
+ ],
1484
+ relations: [
1485
+ "discounts",
1486
+ "discounts.rule",
1487
+ "region",
1488
+ "fulfillments",
1489
+ "shipping_address",
1490
+ "billing_address",
1491
+ "shipping_methods",
1492
+ "shipping_methods.shipping_option",
1493
+ "items",
1494
+ "items.adjustments",
1495
+ "items.variant",
1496
+ "items.variant.product",
1497
+ "payments",
1498
+ ],
1499
+ })];
1500
+ case 1:
1501
+ order = _g.sent();
1502
+ if (order.status === models_1.OrderStatus.CANCELED) {
1503
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A canceled order cannot be fulfilled");
1504
+ }
1505
+ if (!((_f = order.shipping_methods) === null || _f === void 0 ? void 0 : _f.length)) {
1506
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Cannot fulfill an order that lacks shipping methods");
1507
+ }
1508
+ return [4 /*yield*/, this.fulfillmentService_
1509
+ .withTransaction(manager)
1510
+ .createFulfillment(order, itemsToFulfill, {
1511
+ metadata: metadata,
1512
+ no_notification: no_notification,
1513
+ order_id: orderId,
1514
+ })];
1515
+ case 2:
1516
+ fulfillments = _g.sent();
1517
+ successfullyFulfilled = [];
1518
+ try {
1519
+ for (fulfillments_1 = __values(fulfillments), fulfillments_1_1 = fulfillments_1.next(); !fulfillments_1_1.done; fulfillments_1_1 = fulfillments_1.next()) {
1520
+ f = fulfillments_1_1.value;
1521
+ successfullyFulfilled = __spreadArray(__spreadArray([], __read(successfullyFulfilled), false), __read(f.items), false);
1522
+ }
1523
+ }
1524
+ catch (e_15_1) { e_15 = { error: e_15_1 }; }
1525
+ finally {
1526
+ try {
1527
+ if (fulfillments_1_1 && !fulfillments_1_1.done && (_c = fulfillments_1.return)) _c.call(fulfillments_1);
1528
+ }
1529
+ finally { if (e_15) throw e_15.error; }
1530
+ }
1531
+ order.fulfillment_status = models_1.FulfillmentStatus.FULFILLED;
1532
+ _loop_3 = function (item) {
1533
+ var fulfillmentItem, fulfilledQuantity;
1534
+ return __generator(this, function (_h) {
1535
+ switch (_h.label) {
1536
+ case 0:
1537
+ fulfillmentItem = successfullyFulfilled.find(function (f) { return item.id === f.item_id; });
1538
+ if (!fulfillmentItem) return [3 /*break*/, 2];
1539
+ fulfilledQuantity = (item.fulfilled_quantity || 0) + fulfillmentItem.quantity;
1540
+ // Update the fulfilled quantity
1541
+ return [4 /*yield*/, this_3.lineItemService_.withTransaction(manager).update(item.id, {
1542
+ fulfilled_quantity: fulfilledQuantity,
1543
+ })];
1544
+ case 1:
1545
+ // Update the fulfilled quantity
1546
+ _h.sent();
1547
+ if (item.quantity !== fulfilledQuantity) {
1548
+ order.fulfillment_status = models_1.FulfillmentStatus.PARTIALLY_FULFILLED;
1549
+ }
1550
+ return [3 /*break*/, 3];
1551
+ case 2:
1552
+ if (item.quantity !== item.fulfilled_quantity) {
1553
+ order.fulfillment_status = models_1.FulfillmentStatus.PARTIALLY_FULFILLED;
1554
+ }
1555
+ _h.label = 3;
1556
+ case 3: return [2 /*return*/];
1613
1557
  }
1614
- _h.label = 3;
1615
- case 3: return [2 /*return*/];
1558
+ });
1559
+ };
1560
+ this_3 = this;
1561
+ _g.label = 3;
1562
+ case 3:
1563
+ _g.trys.push([3, 8, 9, 10]);
1564
+ _a = __values(order.items), _b = _a.next();
1565
+ _g.label = 4;
1566
+ case 4:
1567
+ if (!!_b.done) return [3 /*break*/, 7];
1568
+ item = _b.value;
1569
+ return [5 /*yield**/, _loop_3(item)];
1570
+ case 5:
1571
+ _g.sent();
1572
+ _g.label = 6;
1573
+ case 6:
1574
+ _b = _a.next();
1575
+ return [3 /*break*/, 4];
1576
+ case 7: return [3 /*break*/, 10];
1577
+ case 8:
1578
+ e_13_1 = _g.sent();
1579
+ e_13 = { error: e_13_1 };
1580
+ return [3 /*break*/, 10];
1581
+ case 9:
1582
+ try {
1583
+ if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
1616
1584
  }
1617
- });
1618
- };
1619
- this_3 = this;
1620
- _g.label = 3;
1621
- case 3:
1622
- _g.trys.push([3, 8, 9, 10]);
1623
- _a = __values(order.items), _b = _a.next();
1624
- _g.label = 4;
1625
- case 4:
1626
- if (!!_b.done) return [3 /*break*/, 7];
1627
- item = _b.value;
1628
- return [5 /*yield**/, _loop_3(item)];
1629
- case 5:
1630
- _g.sent();
1631
- _g.label = 6;
1632
- case 6:
1633
- _b = _a.next();
1634
- return [3 /*break*/, 4];
1635
- case 7: return [3 /*break*/, 10];
1636
- case 8:
1637
- e_13_1 = _g.sent();
1638
- e_13 = { error: e_13_1 };
1639
- return [3 /*break*/, 10];
1640
- case 9:
1641
- try {
1642
- if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
1643
- }
1644
- finally { if (e_13) throw e_13.error; }
1645
- return [7 /*endfinally*/];
1646
- case 10:
1647
- orderRepo = manager.getCustomRepository(this.orderRepository_);
1648
- order.fulfillments = __spreadArray(__spreadArray([], __read(order.fulfillments), false), __read(fulfillments), false);
1649
- return [4 /*yield*/, orderRepo.save(order)];
1650
- case 11:
1651
- result = _g.sent();
1652
- evaluatedNoNotification = no_notification !== undefined ? no_notification : order.no_notification;
1653
- _g.label = 12;
1654
- case 12:
1655
- _g.trys.push([12, 17, 18, 19]);
1656
- fulfillments_2 = __values(fulfillments), fulfillments_2_1 = fulfillments_2.next();
1657
- _g.label = 13;
1658
- case 13:
1659
- if (!!fulfillments_2_1.done) return [3 /*break*/, 16];
1660
- fulfillment = fulfillments_2_1.value;
1661
- return [4 /*yield*/, this.eventBus_
1662
- .withTransaction(manager)
1663
- .emit(OrderService.Events.FULFILLMENT_CREATED, {
1664
- id: orderId,
1665
- fulfillment_id: fulfillment.id,
1666
- no_notification: evaluatedNoNotification,
1667
- })];
1668
- case 14:
1669
- _g.sent();
1670
- _g.label = 15;
1671
- case 15:
1672
- fulfillments_2_1 = fulfillments_2.next();
1673
- return [3 /*break*/, 13];
1674
- case 16: return [3 /*break*/, 19];
1675
- case 17:
1676
- e_14_1 = _g.sent();
1677
- e_14 = { error: e_14_1 };
1678
- return [3 /*break*/, 19];
1679
- case 18:
1680
- try {
1681
- if (fulfillments_2_1 && !fulfillments_2_1.done && (_e = fulfillments_2.return)) _e.call(fulfillments_2);
1585
+ finally { if (e_13) throw e_13.error; }
1586
+ return [7 /*endfinally*/];
1587
+ case 10:
1588
+ orderRepo = manager.getCustomRepository(this.orderRepository_);
1589
+ order.fulfillments = __spreadArray(__spreadArray([], __read(order.fulfillments), false), __read(fulfillments), false);
1590
+ return [4 /*yield*/, orderRepo.save(order)];
1591
+ case 11:
1592
+ result = _g.sent();
1593
+ evaluatedNoNotification = no_notification !== undefined ? no_notification : order.no_notification;
1594
+ _g.label = 12;
1595
+ case 12:
1596
+ _g.trys.push([12, 17, 18, 19]);
1597
+ fulfillments_2 = __values(fulfillments), fulfillments_2_1 = fulfillments_2.next();
1598
+ _g.label = 13;
1599
+ case 13:
1600
+ if (!!fulfillments_2_1.done) return [3 /*break*/, 16];
1601
+ fulfillment = fulfillments_2_1.value;
1602
+ return [4 /*yield*/, this.eventBus_
1603
+ .withTransaction(manager)
1604
+ .emit(OrderService.Events.FULFILLMENT_CREATED, {
1605
+ id: orderId,
1606
+ fulfillment_id: fulfillment.id,
1607
+ no_notification: evaluatedNoNotification,
1608
+ })];
1609
+ case 14:
1610
+ _g.sent();
1611
+ _g.label = 15;
1612
+ case 15:
1613
+ fulfillments_2_1 = fulfillments_2.next();
1614
+ return [3 /*break*/, 13];
1615
+ case 16: return [3 /*break*/, 19];
1616
+ case 17:
1617
+ e_14_1 = _g.sent();
1618
+ e_14 = { error: e_14_1 };
1619
+ return [3 /*break*/, 19];
1620
+ case 18:
1621
+ try {
1622
+ if (fulfillments_2_1 && !fulfillments_2_1.done && (_e = fulfillments_2.return)) _e.call(fulfillments_2);
1623
+ }
1624
+ finally { if (e_14) throw e_14.error; }
1625
+ return [7 /*endfinally*/];
1626
+ case 19: return [2 /*return*/, result];
1682
1627
  }
1683
- finally { if (e_14) throw e_14.error; }
1684
- return [7 /*endfinally*/];
1685
- case 19: return [2 /*return*/, result];
1686
- }
1687
- });
1688
- }); })];
1628
+ });
1629
+ }); })];
1630
+ case 1: return [2 /*return*/, _a.sent()];
1631
+ }
1689
1632
  });
1690
1633
  });
1691
1634
  };
1692
1635
  /**
1693
1636
  * Cancels a fulfillment (if related to an order)
1694
- * @param {string} fulfillmentId - the ID of the fulfillment to cancel
1695
- * @return {Promise} updated order
1637
+ * @param fulfillmentId - the ID of the fulfillment to cancel
1638
+ * @return updated order
1696
1639
  */
1697
1640
  OrderService.prototype.cancelFulfillment = function (fulfillmentId) {
1698
1641
  return __awaiter(this, void 0, void 0, function () {
1699
1642
  var _this = this;
1700
1643
  return __generator(this, function (_a) {
1701
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1702
- var canceled, order, orderRepo, updated;
1703
- return __generator(this, function (_a) {
1704
- switch (_a.label) {
1705
- case 0: return [4 /*yield*/, this.fulfillmentService_
1706
- .withTransaction(manager)
1707
- .cancelFulfillment(fulfillmentId)];
1708
- case 1:
1709
- canceled = _a.sent();
1710
- if (!canceled.order_id) {
1711
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Fufillment not related to an order");
1712
- }
1713
- return [4 /*yield*/, this.retrieve(canceled.order_id)];
1714
- case 2:
1715
- order = _a.sent();
1716
- order.fulfillment_status = "canceled";
1717
- orderRepo = manager.getCustomRepository(this.orderRepository_);
1718
- return [4 /*yield*/, orderRepo.save(order)];
1719
- case 3:
1720
- updated = _a.sent();
1721
- return [4 /*yield*/, this.eventBus_
1644
+ switch (_a.label) {
1645
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1646
+ var canceled, order, orderRepo, updated;
1647
+ return __generator(this, function (_a) {
1648
+ switch (_a.label) {
1649
+ case 0: return [4 /*yield*/, this.fulfillmentService_
1722
1650
  .withTransaction(manager)
1723
- .emit(OrderService.Events.FULFILLMENT_CANCELED, {
1724
- id: order.id,
1725
- fulfillment_id: canceled.id,
1726
- no_notification: canceled.no_notification,
1727
- })];
1728
- case 4:
1729
- _a.sent();
1730
- return [2 /*return*/, updated];
1731
- }
1732
- });
1733
- }); })];
1651
+ .cancelFulfillment(fulfillmentId)];
1652
+ case 1:
1653
+ canceled = _a.sent();
1654
+ if (!canceled.order_id) {
1655
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Fufillment not related to an order");
1656
+ }
1657
+ return [4 /*yield*/, this.retrieve(canceled.order_id)];
1658
+ case 2:
1659
+ order = _a.sent();
1660
+ order.fulfillment_status = models_1.FulfillmentStatus.CANCELED;
1661
+ orderRepo = manager.getCustomRepository(this.orderRepository_);
1662
+ return [4 /*yield*/, orderRepo.save(order)];
1663
+ case 3:
1664
+ updated = _a.sent();
1665
+ return [4 /*yield*/, this.eventBus_
1666
+ .withTransaction(manager)
1667
+ .emit(OrderService.Events.FULFILLMENT_CANCELED, {
1668
+ id: order.id,
1669
+ fulfillment_id: canceled.id,
1670
+ no_notification: canceled.no_notification,
1671
+ })];
1672
+ case 4:
1673
+ _a.sent();
1674
+ return [2 /*return*/, updated];
1675
+ }
1676
+ });
1677
+ }); })];
1678
+ case 1: return [2 /*return*/, _a.sent()];
1679
+ }
1734
1680
  });
1735
1681
  });
1736
1682
  };
1737
1683
  /**
1738
1684
  * Retrieves the order line items, given an array of items.
1739
- * @param {Order} order - the order to get line items from
1740
- * @param {{ item_id: string, quantity: number }} items - the items to get
1741
- * @param {function} transformer - a function to apply to each of the items
1685
+ * @param order - the order to get line items from
1686
+ * @param items - the items to get
1687
+ * @param transformer - a function to apply to each of the items
1742
1688
  * retrieved from the order, should return a line item. If the transformer
1743
1689
  * returns an undefined value the line item will be filtered from the
1744
1690
  * returned array.
1745
- * @return {Promise<Array<LineItem>>} the line items generated by the transformer.
1691
+ * @return the line items generated by the transformer.
1746
1692
  */
1747
- OrderService.prototype.getFulfillmentItems_ = function (order, items, transformer) {
1693
+ OrderService.prototype.getFulfillmentItems = function (order, items, transformer) {
1748
1694
  return __awaiter(this, void 0, void 0, function () {
1749
- var toReturn;
1750
1695
  var _this = this;
1751
1696
  return __generator(this, function (_a) {
1752
1697
  switch (_a.label) {
@@ -1755,14 +1700,12 @@ var OrderService = /** @class */ (function (_super) {
1755
1700
  return __awaiter(_this, void 0, void 0, function () {
1756
1701
  var item;
1757
1702
  return __generator(this, function (_b) {
1758
- item = order.items.find(function (i) { return i.id.equals(item_id); });
1703
+ item = order.items.find(function (i) { return i.id === item_id; });
1759
1704
  return [2 /*return*/, transformer(item, quantity)];
1760
1705
  });
1761
1706
  });
1762
1707
  }))];
1763
- case 1:
1764
- toReturn = _a.sent();
1765
- return [2 /*return*/, toReturn.filter(function (i) { return !!i; })];
1708
+ case 1: return [2 /*return*/, (_a.sent()).filter(function (i) { return !!i; })];
1766
1709
  }
1767
1710
  });
1768
1711
  });
@@ -1770,43 +1713,44 @@ var OrderService = /** @class */ (function (_super) {
1770
1713
  /**
1771
1714
  * Archives an order. It only alloved, if the order has been fulfilled
1772
1715
  * and payment has been captured.
1773
- * @param {string} orderId - the order to archive
1774
- * @return {Promise} the result of the update operation
1716
+ * @param orderId - the order to archive
1717
+ * @return the result of the update operation
1775
1718
  */
1776
1719
  OrderService.prototype.archive = function (orderId) {
1777
1720
  return __awaiter(this, void 0, void 0, function () {
1778
1721
  var _this = this;
1779
1722
  return __generator(this, function (_a) {
1780
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1781
- var order, orderRepo, result;
1782
- return __generator(this, function (_a) {
1783
- switch (_a.label) {
1784
- case 0: return [4 /*yield*/, this.retrieve(orderId)];
1785
- case 1:
1786
- order = _a.sent();
1787
- if (order.status !== ("completed" || "refunded")) {
1788
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Can't archive an unprocessed order");
1789
- }
1790
- order.status = "archived";
1791
- orderRepo = manager.getCustomRepository(this.orderRepository_);
1792
- return [4 /*yield*/, orderRepo.save(order)];
1793
- case 2:
1794
- result = _a.sent();
1795
- return [2 /*return*/, result];
1796
- }
1797
- });
1798
- }); })];
1723
+ switch (_a.label) {
1724
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1725
+ var order, orderRepo;
1726
+ return __generator(this, function (_a) {
1727
+ switch (_a.label) {
1728
+ case 0: return [4 /*yield*/, this.retrieve(orderId)];
1729
+ case 1:
1730
+ order = _a.sent();
1731
+ if (order.status !== ("completed" || "refunded")) {
1732
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Can't archive an unprocessed order");
1733
+ }
1734
+ order.status = models_1.OrderStatus.ARCHIVED;
1735
+ orderRepo = manager.getCustomRepository(this.orderRepository_);
1736
+ return [4 /*yield*/, orderRepo.save(order)];
1737
+ case 2: return [2 /*return*/, _a.sent()];
1738
+ }
1739
+ });
1740
+ }); })];
1741
+ case 1: return [2 /*return*/, _a.sent()];
1742
+ }
1799
1743
  });
1800
1744
  });
1801
1745
  };
1802
1746
  /**
1803
1747
  * Refunds a given amount back to the customer.
1804
- * @param {string} orderId - id of the order to refund.
1805
- * @param {float} refundAmount - the amount to refund.
1806
- * @param {string} reason - the reason to refund.
1807
- * @param {string | undefined} note - note for refund.
1808
- * @param {Object} config - the config for refund.
1809
- * @return {Promise} the result of the refund operation.
1748
+ * @param orderId - id of the order to refund.
1749
+ * @param refundAmount - the amount to refund.
1750
+ * @param reason - the reason to refund.
1751
+ * @param note - note for refund.
1752
+ * @param config - the config for refund.
1753
+ * @return the result of the refund operation.
1810
1754
  */
1811
1755
  OrderService.prototype.createRefund = function (orderId, refundAmount, reason, note, config) {
1812
1756
  if (config === void 0) { config = {
@@ -1816,45 +1760,49 @@ var OrderService = /** @class */ (function (_super) {
1816
1760
  var no_notification;
1817
1761
  var _this = this;
1818
1762
  return __generator(this, function (_a) {
1819
- no_notification = config.no_notification;
1820
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1821
- var order, refund, result, evaluatedNoNotification;
1822
- return __generator(this, function (_a) {
1823
- switch (_a.label) {
1824
- case 0: return [4 /*yield*/, this.retrieve(orderId, {
1825
- select: ["refundable_amount", "total", "refunded_total"],
1826
- relations: ["payments"],
1827
- })];
1828
- case 1:
1829
- order = _a.sent();
1830
- if (order.status === "canceled") {
1831
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A canceled order cannot be refunded");
1832
- }
1833
- if (refundAmount > order.refundable_amount) {
1834
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Cannot refund more than the original order amount");
1763
+ switch (_a.label) {
1764
+ case 0:
1765
+ no_notification = config.no_notification;
1766
+ return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1767
+ var order, refund, result, evaluatedNoNotification;
1768
+ return __generator(this, function (_a) {
1769
+ switch (_a.label) {
1770
+ case 0: return [4 /*yield*/, this.retrieve(orderId, {
1771
+ select: ["refundable_amount", "total", "refunded_total"],
1772
+ relations: ["payments"],
1773
+ })];
1774
+ case 1:
1775
+ order = _a.sent();
1776
+ if (order.status === "canceled") {
1777
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A canceled order cannot be refunded");
1778
+ }
1779
+ if (refundAmount > order.refundable_amount) {
1780
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Cannot refund more than the original order amount");
1781
+ }
1782
+ return [4 /*yield*/, this.paymentProviderService_
1783
+ .withTransaction(manager)
1784
+ .refundPayment(order.payments, refundAmount, reason, note)];
1785
+ case 2:
1786
+ refund = _a.sent();
1787
+ return [4 /*yield*/, this.retrieve(orderId)];
1788
+ case 3:
1789
+ result = _a.sent();
1790
+ evaluatedNoNotification = no_notification !== undefined ? no_notification : order.no_notification;
1791
+ this.eventBus_.emit(OrderService.Events.REFUND_CREATED, {
1792
+ id: result.id,
1793
+ refund_id: refund.id,
1794
+ no_notification: evaluatedNoNotification,
1795
+ });
1796
+ return [2 /*return*/, result];
1835
1797
  }
1836
- return [4 /*yield*/, this.paymentProviderService_
1837
- .withTransaction(manager)
1838
- .refundPayment(order.payments, refundAmount, reason, note)];
1839
- case 2:
1840
- refund = _a.sent();
1841
- return [4 /*yield*/, this.retrieve(orderId)];
1842
- case 3:
1843
- result = _a.sent();
1844
- evaluatedNoNotification = no_notification !== undefined ? no_notification : order.no_notification;
1845
- this.eventBus_.emit(OrderService.Events.REFUND_CREATED, {
1846
- id: result.id,
1847
- refund_id: refund.id,
1848
- no_notification: evaluatedNoNotification,
1849
- });
1850
- return [2 /*return*/, result];
1851
- }
1852
- });
1853
- }); })];
1798
+ });
1799
+ }); })];
1800
+ case 1: return [2 /*return*/, _a.sent()];
1801
+ }
1854
1802
  });
1855
1803
  });
1856
1804
  };
1857
- OrderService.prototype.decorateTotals_ = function (order, totalsFields) {
1805
+ OrderService.prototype.decorateTotals = function (order, totalsFields) {
1858
1806
  if (totalsFields === void 0) { totalsFields = []; }
1859
1807
  return __awaiter(this, void 0, void 0, function () {
1860
1808
  var totalsFields_1, totalsFields_1_1, totalField, _a, giftCardBreakdown, _b, _c, paid_total, refunded_total, _d, _e, s, _f, _g, c, e_16_1;
@@ -1919,7 +1867,9 @@ var OrderService = /** @class */ (function (_super) {
1919
1867
  _l.label = 8;
1920
1868
  case 8:
1921
1869
  _c = order;
1922
- return [4 /*yield*/, this.totalsService_.getTotal(order)];
1870
+ return [4 /*yield*/, this.totalsService_
1871
+ .withTransaction(this.manager_)
1872
+ .getTotal(order)];
1923
1873
  case 9:
1924
1874
  _c.total = _l.sent();
1925
1875
  return [3 /*break*/, 17];
@@ -2017,119 +1967,98 @@ var OrderService = /** @class */ (function (_super) {
2017
1967
  * retuned items are not matching the requested items. Setting the
2018
1968
  * allowMismatch argument to true, will process the return, ignoring any
2019
1969
  * mismatches.
2020
- * @param {string} orderId - the order to return.
2021
- * @param {object} receivedReturn - the received return
2022
- * @param {float} customRefundAmount - the custom refund amount return
2023
- * @return {Promise} the result of the update operation
1970
+ * @param orderId - the order to return.
1971
+ * @param receivedReturn - the received return
1972
+ * @param customRefundAmount - the custom refund amount return
1973
+ * @return the result of the update operation
2024
1974
  */
2025
1975
  OrderService.prototype.registerReturnReceived = function (orderId, receivedReturn, customRefundAmount) {
2026
1976
  return __awaiter(this, void 0, void 0, function () {
2027
1977
  var _this = this;
2028
1978
  return __generator(this, function (_a) {
2029
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
2030
- var order, refundAmount, orderRepo, result_2, isFullReturn, _a, _b, i, refund, result;
2031
- var e_19, _c;
2032
- return __generator(this, function (_d) {
2033
- switch (_d.label) {
2034
- case 0: return [4 /*yield*/, this.retrieve(orderId, {
2035
- select: ["total", "refunded_total", "refundable_amount"],
2036
- relations: ["items", "returns", "payments"],
2037
- })];
2038
- case 1:
2039
- order = _d.sent();
2040
- if (order.status === "canceled") {
2041
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A canceled order cannot be registered as received");
2042
- }
2043
- if (!receivedReturn || receivedReturn.order_id !== orderId) {
2044
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Received return does not exist");
2045
- }
2046
- refundAmount = customRefundAmount || receivedReturn.refund_amount;
2047
- orderRepo = manager.getCustomRepository(this.orderRepository_);
2048
- if (!(refundAmount > order.refundable_amount)) return [3 /*break*/, 3];
2049
- order.fulfillment_status = "requires_action";
2050
- return [4 /*yield*/, orderRepo.save(order)];
2051
- case 2:
2052
- result_2 = _d.sent();
2053
- this.eventBus_
2054
- .withTransaction(manager)
2055
- .emit(OrderService.Events.RETURN_ACTION_REQUIRED, {
2056
- id: result_2.id,
2057
- return_id: receivedReturn.id,
2058
- no_notification: receivedReturn.no_notification,
2059
- });
2060
- return [2 /*return*/, result_2];
2061
- case 3:
2062
- isFullReturn = true;
2063
- try {
2064
- for (_a = __values(order.items), _b = _a.next(); !_b.done; _b = _a.next()) {
2065
- i = _b.value;
2066
- if (i.returned_quantity !== i.quantity) {
2067
- isFullReturn = false;
2068
- }
1979
+ switch (_a.label) {
1980
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1981
+ var order, refundAmount, orderRepo, result_2, isFullReturn, _a, _b, i, refund, result;
1982
+ var e_19, _c;
1983
+ return __generator(this, function (_d) {
1984
+ switch (_d.label) {
1985
+ case 0: return [4 /*yield*/, this.retrieve(orderId, {
1986
+ select: ["total", "refunded_total", "refundable_amount"],
1987
+ relations: ["items", "returns", "payments"],
1988
+ })];
1989
+ case 1:
1990
+ order = _d.sent();
1991
+ if (order.status === "canceled") {
1992
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "A canceled order cannot be registered as received");
2069
1993
  }
2070
- }
2071
- catch (e_19_1) { e_19 = { error: e_19_1 }; }
2072
- finally {
2073
- try {
2074
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
1994
+ if (!receivedReturn || receivedReturn.order_id !== orderId) {
1995
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Received return does not exist");
2075
1996
  }
2076
- finally { if (e_19) throw e_19.error; }
2077
- }
2078
- if (!(receivedReturn.refund_amount > 0)) return [3 /*break*/, 5];
2079
- return [4 /*yield*/, this.paymentProviderService_
2080
- .withTransaction(manager)
2081
- .refundPayment(order.payments, receivedReturn.refund_amount, "return")];
2082
- case 4:
2083
- refund = _d.sent();
2084
- order.refunds = __spreadArray(__spreadArray([], __read((order.refunds || [])), false), [refund], false);
2085
- _d.label = 5;
2086
- case 5:
2087
- if (isFullReturn) {
2088
- order.fulfillment_status = "returned";
2089
- }
2090
- else {
2091
- order.fulfillment_status = "partially_returned";
2092
- }
2093
- return [4 /*yield*/, orderRepo.save(order)];
2094
- case 6:
2095
- result = _d.sent();
2096
- return [4 /*yield*/, this.eventBus_
1997
+ refundAmount = customRefundAmount || receivedReturn.refund_amount;
1998
+ orderRepo = manager.getCustomRepository(this.orderRepository_);
1999
+ if (!(refundAmount > order.refundable_amount)) return [3 /*break*/, 3];
2000
+ order.fulfillment_status = models_1.FulfillmentStatus.REQUIRES_ACTION;
2001
+ return [4 /*yield*/, orderRepo.save(order)];
2002
+ case 2:
2003
+ result_2 = _d.sent();
2004
+ this.eventBus_
2097
2005
  .withTransaction(manager)
2098
- .emit(OrderService.Events.ITEMS_RETURNED, {
2099
- id: order.id,
2006
+ .emit(OrderService.Events.RETURN_ACTION_REQUIRED, {
2007
+ id: result_2.id,
2100
2008
  return_id: receivedReturn.id,
2101
2009
  no_notification: receivedReturn.no_notification,
2102
- })];
2103
- case 7:
2104
- _d.sent();
2105
- return [2 /*return*/, result];
2106
- }
2107
- });
2108
- }); })];
2109
- });
2110
- });
2111
- };
2112
- /**
2113
- * Dedicated method to delete metadata for an order.
2114
- * @param {string} orderId - the order to delete metadata from.
2115
- * @param {string} key - key for metadata field
2116
- * @return {Promise} resolves to the updated result.
2117
- */
2118
- OrderService.prototype.deleteMetadata = function (orderId, key) {
2119
- return __awaiter(this, void 0, void 0, function () {
2120
- var validatedId, keyPath;
2121
- var _a;
2122
- return __generator(this, function (_b) {
2123
- validatedId = this.validateId_(orderId);
2124
- if (typeof key !== "string") {
2125
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_ARGUMENT, "Key type is invalid. Metadata keys must be strings");
2010
+ });
2011
+ return [2 /*return*/, result_2];
2012
+ case 3:
2013
+ isFullReturn = true;
2014
+ try {
2015
+ for (_a = __values(order.items), _b = _a.next(); !_b.done; _b = _a.next()) {
2016
+ i = _b.value;
2017
+ if (i.returned_quantity !== i.quantity) {
2018
+ isFullReturn = false;
2019
+ }
2020
+ }
2021
+ }
2022
+ catch (e_19_1) { e_19 = { error: e_19_1 }; }
2023
+ finally {
2024
+ try {
2025
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2026
+ }
2027
+ finally { if (e_19) throw e_19.error; }
2028
+ }
2029
+ if (!(receivedReturn.refund_amount > 0)) return [3 /*break*/, 5];
2030
+ return [4 /*yield*/, this.paymentProviderService_
2031
+ .withTransaction(manager)
2032
+ .refundPayment(order.payments, receivedReturn.refund_amount, "return")];
2033
+ case 4:
2034
+ refund = _d.sent();
2035
+ order.refunds = __spreadArray(__spreadArray([], __read((order.refunds || [])), false), [refund], false);
2036
+ _d.label = 5;
2037
+ case 5:
2038
+ if (isFullReturn) {
2039
+ order.fulfillment_status = models_1.FulfillmentStatus.RETURNED;
2040
+ }
2041
+ else {
2042
+ order.fulfillment_status = models_1.FulfillmentStatus.PARTIALLY_RETURNED;
2043
+ }
2044
+ return [4 /*yield*/, orderRepo.save(order)];
2045
+ case 6:
2046
+ result = _d.sent();
2047
+ return [4 /*yield*/, this.eventBus_
2048
+ .withTransaction(manager)
2049
+ .emit(OrderService.Events.ITEMS_RETURNED, {
2050
+ id: order.id,
2051
+ return_id: receivedReturn.id,
2052
+ no_notification: receivedReturn.no_notification,
2053
+ })];
2054
+ case 7:
2055
+ _d.sent();
2056
+ return [2 /*return*/, result];
2057
+ }
2058
+ });
2059
+ }); })];
2060
+ case 1: return [2 /*return*/, _a.sent()];
2126
2061
  }
2127
- keyPath = "metadata.".concat(key);
2128
- return [2 /*return*/, this.orderModel_
2129
- .updateOne({ _id: validatedId }, { $unset: (_a = {}, _a[keyPath] = "", _a) })
2130
- .catch(function (err) {
2131
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.DB_ERROR, err.message);
2132
- })];
2133
2062
  });
2134
2063
  });
2135
2064
  };
@@ -2152,6 +2081,6 @@ var OrderService = /** @class */ (function (_super) {
2152
2081
  COMPLETED: "order.completed",
2153
2082
  };
2154
2083
  return OrderService;
2155
- }(medusa_interfaces_1.BaseService));
2084
+ }(interfaces_1.TransactionBaseService));
2156
2085
  exports.default = OrderService;
2157
2086
  //# sourceMappingURL=order.js.map