@medusajs/medusa 1.3.2 → 1.3.3

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 (507) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/dist/api/index.d.ts +15 -14
  3. package/dist/api/index.js +15 -14
  4. package/dist/api/index.js.map +1 -1
  5. package/dist/api/middlewares/authenticate-customer.d.ts +2 -1
  6. package/dist/api/middlewares/authenticate-customer.js +1 -2
  7. package/dist/api/middlewares/authenticate-customer.js.map +1 -1
  8. package/dist/api/middlewares/authenticate.d.ts +2 -1
  9. package/dist/api/middlewares/authenticate.js.map +1 -1
  10. package/dist/api/middlewares/await-middleware.d.ts +3 -1
  11. package/dist/api/middlewares/await-middleware.js +2 -31
  12. package/dist/api/middlewares/await-middleware.js.map +1 -1
  13. package/dist/api/middlewares/error-handler.d.ts +3 -1
  14. package/dist/api/middlewares/error-handler.js.map +1 -1
  15. package/dist/api/middlewares/feature-flag-enabled.d.ts +2 -0
  16. package/dist/api/middlewares/feature-flag-enabled.js +57 -0
  17. package/dist/api/middlewares/feature-flag-enabled.js.map +1 -0
  18. package/dist/api/middlewares/index.d.ts +10 -10
  19. package/dist/api/middlewares/index.js +5 -1
  20. package/dist/api/middlewares/index.js.map +1 -1
  21. package/dist/api/middlewares/normalized-query.d.ts +6 -1
  22. package/dist/api/middlewares/normalized-query.js +5 -2
  23. package/dist/api/middlewares/normalized-query.js.map +1 -1
  24. package/dist/api/middlewares/transform-body.d.ts +4 -0
  25. package/dist/api/middlewares/transform-body.js +66 -0
  26. package/dist/api/middlewares/transform-body.js.map +1 -0
  27. package/dist/api/middlewares/transform-query.d.ts +6 -0
  28. package/dist/api/middlewares/transform-query.js +86 -0
  29. package/dist/api/middlewares/transform-query.js.map +1 -0
  30. package/dist/api/routes/admin/batch/cancel-batch-job.d.ts +22 -0
  31. package/dist/api/routes/admin/batch/cancel-batch-job.js +74 -0
  32. package/dist/api/routes/admin/batch/cancel-batch-job.js.map +1 -0
  33. package/dist/api/routes/admin/batch/confirm-batch-job.d.ts +22 -0
  34. package/dist/api/routes/admin/batch/confirm-batch-job.js +74 -0
  35. package/dist/api/routes/admin/batch/confirm-batch-job.js.map +1 -0
  36. package/dist/api/routes/admin/batch/create-batch-job.d.ts +30 -0
  37. package/dist/api/routes/admin/batch/create-batch-job.js +125 -0
  38. package/dist/api/routes/admin/batch/create-batch-job.js.map +1 -0
  39. package/dist/api/routes/admin/batch/get-batch-job.d.ts +22 -0
  40. package/dist/api/routes/admin/batch/get-batch-job.js +67 -0
  41. package/dist/api/routes/admin/batch/get-batch-job.js.map +1 -0
  42. package/dist/api/routes/admin/batch/index.d.ts +4 -0
  43. package/dist/api/routes/admin/batch/index.js +36 -7
  44. package/dist/api/routes/admin/batch/index.js.map +1 -1
  45. package/dist/api/routes/admin/batch/list-batch-jobs.d.ts +15 -7
  46. package/dist/api/routes/admin/batch/list-batch-jobs.js +61 -46
  47. package/dist/api/routes/admin/batch/list-batch-jobs.js.map +1 -1
  48. package/dist/api/routes/admin/collections/add-products.d.ts +2 -1
  49. package/dist/api/routes/admin/collections/add-products.js +4 -7
  50. package/dist/api/routes/admin/collections/add-products.js.map +1 -1
  51. package/dist/api/routes/admin/collections/create-collection.d.ts +2 -1
  52. package/dist/api/routes/admin/collections/create-collection.js +6 -8
  53. package/dist/api/routes/admin/collections/create-collection.js.map +1 -1
  54. package/dist/api/routes/admin/collections/delete-collection.d.ts +2 -1
  55. package/dist/api/routes/admin/collections/delete-collection.js.map +1 -1
  56. package/dist/api/routes/admin/collections/get-collection.d.ts +2 -1
  57. package/dist/api/routes/admin/collections/get-collection.js +4 -5
  58. package/dist/api/routes/admin/collections/get-collection.js.map +1 -1
  59. package/dist/api/routes/admin/collections/index.js +32 -12
  60. package/dist/api/routes/admin/collections/index.js.map +1 -1
  61. package/dist/api/routes/admin/collections/list-collections.d.ts +2 -1
  62. package/dist/api/routes/admin/collections/list-collections.js +9 -19
  63. package/dist/api/routes/admin/collections/list-collections.js.map +1 -1
  64. package/dist/api/routes/admin/collections/remove-products.d.ts +2 -1
  65. package/dist/api/routes/admin/collections/remove-products.js +5 -8
  66. package/dist/api/routes/admin/collections/remove-products.js.map +1 -1
  67. package/dist/api/routes/admin/collections/update-collection.d.ts +2 -1
  68. package/dist/api/routes/admin/collections/update-collection.js +5 -8
  69. package/dist/api/routes/admin/collections/update-collection.js.map +1 -1
  70. package/dist/api/routes/admin/customer-groups/add-customers-batch.d.ts +2 -1
  71. package/dist/api/routes/admin/customer-groups/add-customers-batch.js.map +1 -1
  72. package/dist/api/routes/admin/customer-groups/create-customer-group.d.ts +2 -1
  73. package/dist/api/routes/admin/customer-groups/create-customer-group.js.map +1 -1
  74. package/dist/api/routes/admin/customer-groups/delete-customer-group.d.ts +2 -1
  75. package/dist/api/routes/admin/customer-groups/delete-customer-group.js.map +1 -1
  76. package/dist/api/routes/admin/customer-groups/delete-customers-batch.d.ts +2 -1
  77. package/dist/api/routes/admin/customer-groups/delete-customers-batch.js.map +1 -1
  78. package/dist/api/routes/admin/customer-groups/get-customer-group-customers.d.ts +2 -1
  79. package/dist/api/routes/admin/customer-groups/get-customer-group-customers.js.map +1 -1
  80. package/dist/api/routes/admin/customer-groups/get-customer-group.d.ts +2 -1
  81. package/dist/api/routes/admin/customer-groups/get-customer-group.js +3 -17
  82. package/dist/api/routes/admin/customer-groups/get-customer-group.js.map +1 -1
  83. package/dist/api/routes/admin/customer-groups/index.js +29 -11
  84. package/dist/api/routes/admin/customer-groups/index.js.map +1 -1
  85. package/dist/api/routes/admin/customer-groups/list-customer-groups.d.ts +2 -1
  86. package/dist/api/routes/admin/customer-groups/list-customer-groups.js +10 -45
  87. package/dist/api/routes/admin/customer-groups/list-customer-groups.js.map +1 -1
  88. package/dist/api/routes/admin/customer-groups/update-customer-group.d.ts +2 -1
  89. package/dist/api/routes/admin/customer-groups/update-customer-group.js.map +1 -1
  90. package/dist/api/routes/admin/customers/create-customer.d.ts +1 -1
  91. package/dist/api/routes/admin/customers/create-customer.js.map +1 -1
  92. package/dist/api/routes/admin/customers/update-customer.d.ts +1 -1
  93. package/dist/api/routes/admin/customers/update-customer.js.map +1 -1
  94. package/dist/api/routes/admin/discounts/add-valid-product.d.ts +23 -0
  95. package/dist/api/routes/admin/discounts/add-valid-product.js +82 -0
  96. package/dist/api/routes/admin/discounts/add-valid-product.js.map +1 -0
  97. package/dist/api/routes/admin/discounts/remove-valid-product.d.ts +23 -0
  98. package/dist/api/routes/admin/discounts/remove-valid-product.js +82 -0
  99. package/dist/api/routes/admin/discounts/remove-valid-product.js.map +1 -0
  100. package/dist/api/routes/admin/draft-orders/index.d.ts +1 -1
  101. package/dist/api/routes/admin/draft-orders/index.js +1 -0
  102. package/dist/api/routes/admin/draft-orders/index.js.map +1 -1
  103. package/dist/api/routes/admin/draft-orders/list-draft-orders.js +7 -6
  104. package/dist/api/routes/admin/draft-orders/list-draft-orders.js.map +1 -1
  105. package/dist/api/routes/admin/gift-cards/create-gift-card.d.ts +2 -2
  106. package/dist/api/routes/admin/gift-cards/create-gift-card.js +0 -1
  107. package/dist/api/routes/admin/gift-cards/create-gift-card.js.map +1 -1
  108. package/dist/api/routes/admin/gift-cards/index.d.ts +1 -1
  109. package/dist/api/routes/admin/gift-cards/index.js +19 -5
  110. package/dist/api/routes/admin/gift-cards/index.js.map +1 -1
  111. package/dist/api/routes/admin/gift-cards/list-gift-cards.js +24 -18
  112. package/dist/api/routes/admin/gift-cards/list-gift-cards.js.map +1 -1
  113. package/dist/api/routes/admin/gift-cards/update-gift-card.d.ts +1 -1
  114. package/dist/api/routes/admin/gift-cards/update-gift-card.js.map +1 -1
  115. package/dist/api/routes/admin/orders/create-claim.d.ts +4 -14
  116. package/dist/api/routes/admin/orders/create-claim.js +3 -14
  117. package/dist/api/routes/admin/orders/create-claim.js.map +1 -1
  118. package/dist/api/routes/admin/orders/fulfill-claim.d.ts +1 -1
  119. package/dist/api/routes/admin/orders/fulfill-claim.js.map +1 -1
  120. package/dist/api/routes/admin/orders/update-claim.d.ts +1 -1
  121. package/dist/api/routes/admin/orders/update-claim.js.map +1 -1
  122. package/dist/api/routes/admin/price-lists/create-price-list.d.ts +2 -1
  123. package/dist/api/routes/admin/price-lists/create-price-list.js +4 -7
  124. package/dist/api/routes/admin/price-lists/create-price-list.js.map +1 -1
  125. package/dist/api/routes/admin/price-lists/index.js +26 -7
  126. package/dist/api/routes/admin/price-lists/index.js.map +1 -1
  127. package/dist/api/routes/admin/price-lists/list-price-list-products.d.ts +3 -7
  128. package/dist/api/routes/admin/price-lists/list-price-list-products.js +21 -59
  129. package/dist/api/routes/admin/price-lists/list-price-list-products.js.map +1 -1
  130. package/dist/api/routes/admin/price-lists/list-price-lists.d.ts +2 -1
  131. package/dist/api/routes/admin/price-lists/list-price-lists.js +8 -40
  132. package/dist/api/routes/admin/price-lists/list-price-lists.js.map +1 -1
  133. package/dist/api/routes/admin/products/add-option.js +26 -6
  134. package/dist/api/routes/admin/products/add-option.js.map +1 -1
  135. package/dist/api/routes/admin/products/create-product.d.ts +3 -3
  136. package/dist/api/routes/admin/products/create-product.js +13 -9
  137. package/dist/api/routes/admin/products/create-product.js.map +1 -1
  138. package/dist/api/routes/admin/products/delete-variant.js +26 -6
  139. package/dist/api/routes/admin/products/delete-variant.js.map +1 -1
  140. package/dist/api/routes/admin/products/get-product.js +24 -4
  141. package/dist/api/routes/admin/products/get-product.js.map +1 -1
  142. package/dist/api/routes/admin/products/get-variants.d.ts +24 -0
  143. package/dist/api/routes/admin/products/get-variants.js +76 -0
  144. package/dist/api/routes/admin/products/get-variants.js.map +1 -0
  145. package/dist/api/routes/admin/products/index.d.ts +3 -2
  146. package/dist/api/routes/admin/products/index.js +1 -4
  147. package/dist/api/routes/admin/products/index.js.map +1 -1
  148. package/dist/api/routes/admin/products/list-products.d.ts +1 -6
  149. package/dist/api/routes/admin/products/list-products.js +3 -9
  150. package/dist/api/routes/admin/products/list-products.js.map +1 -1
  151. package/dist/api/routes/admin/products/update-product.d.ts +3 -3
  152. package/dist/api/routes/admin/products/update-product.js +28 -7
  153. package/dist/api/routes/admin/products/update-product.js.map +1 -1
  154. package/dist/api/routes/admin/products/update-variant.js +27 -10
  155. package/dist/api/routes/admin/products/update-variant.js.map +1 -1
  156. package/dist/api/routes/admin/shipping-options/list-shipping-options.js +6 -2
  157. package/dist/api/routes/admin/shipping-options/list-shipping-options.js.map +1 -1
  158. package/dist/api/routes/admin/store/get-store.js +4 -2
  159. package/dist/api/routes/admin/store/get-store.js.map +1 -1
  160. package/dist/api/routes/admin/store/update-store.d.ts +1 -0
  161. package/dist/api/routes/admin/store/update-store.js +5 -0
  162. package/dist/api/routes/admin/store/update-store.js.map +1 -1
  163. package/dist/api/routes/admin/uploads/create-upload.d.ts +1 -1
  164. package/dist/api/routes/admin/uploads/create-upload.js +5 -5
  165. package/dist/api/routes/admin/uploads/create-upload.js.map +1 -1
  166. package/dist/api/routes/admin/uploads/delete-upload.d.ts +4 -1
  167. package/dist/api/routes/admin/uploads/delete-upload.js +29 -11
  168. package/dist/api/routes/admin/uploads/delete-upload.js.map +1 -1
  169. package/dist/api/routes/admin/uploads/get-download-url.d.ts +28 -0
  170. package/dist/api/routes/admin/uploads/get-download-url.js +99 -0
  171. package/dist/api/routes/admin/uploads/get-download-url.js.map +1 -0
  172. package/dist/api/routes/admin/uploads/index.d.ts +10 -3
  173. package/dist/api/routes/admin/uploads/index.js +19 -4
  174. package/dist/api/routes/admin/uploads/index.js.map +1 -1
  175. package/dist/api/routes/store/carts/add-shipping-method.js +6 -2
  176. package/dist/api/routes/store/carts/add-shipping-method.js.map +1 -1
  177. package/dist/api/routes/store/carts/calculate-taxes.js +9 -2
  178. package/dist/api/routes/store/carts/calculate-taxes.js.map +1 -1
  179. package/dist/api/routes/store/carts/create-cart.js +6 -2
  180. package/dist/api/routes/store/carts/create-cart.js.map +1 -1
  181. package/dist/api/routes/store/carts/create-line-item.js +6 -2
  182. package/dist/api/routes/store/carts/create-line-item.js.map +1 -1
  183. package/dist/api/routes/store/carts/create-payment-sessions.js +6 -2
  184. package/dist/api/routes/store/carts/create-payment-sessions.js.map +1 -1
  185. package/dist/api/routes/store/carts/decorate-line-items-with-totals.d.ts +5 -0
  186. package/dist/api/routes/store/carts/decorate-line-items-with-totals.js +71 -0
  187. package/dist/api/routes/store/carts/decorate-line-items-with-totals.js.map +1 -0
  188. package/dist/api/routes/store/carts/delete-discount.js +6 -2
  189. package/dist/api/routes/store/carts/delete-discount.js.map +1 -1
  190. package/dist/api/routes/store/carts/delete-line-item.js +6 -2
  191. package/dist/api/routes/store/carts/delete-line-item.js.map +1 -1
  192. package/dist/api/routes/store/carts/delete-payment-session.js +6 -2
  193. package/dist/api/routes/store/carts/delete-payment-session.js.map +1 -1
  194. package/dist/api/routes/store/carts/get-cart.js +6 -2
  195. package/dist/api/routes/store/carts/get-cart.js.map +1 -1
  196. package/dist/api/routes/store/carts/refresh-payment-session.js +6 -2
  197. package/dist/api/routes/store/carts/refresh-payment-session.js.map +1 -1
  198. package/dist/api/routes/store/carts/set-payment-session.js +6 -2
  199. package/dist/api/routes/store/carts/set-payment-session.js.map +1 -1
  200. package/dist/api/routes/store/carts/update-cart.js +6 -2
  201. package/dist/api/routes/store/carts/update-cart.js.map +1 -1
  202. package/dist/api/routes/store/carts/update-line-item.js +6 -2
  203. package/dist/api/routes/store/carts/update-line-item.js.map +1 -1
  204. package/dist/api/routes/store/carts/update-payment-method.d.ts +38 -0
  205. package/dist/api/routes/store/carts/update-payment-method.js +122 -0
  206. package/dist/api/routes/store/carts/update-payment-method.js.map +1 -0
  207. package/dist/api/routes/store/carts/update-payment-session.js +6 -2
  208. package/dist/api/routes/store/carts/update-payment-session.js.map +1 -1
  209. package/dist/api/routes/store/customers/get-payment-methods.js +6 -7
  210. package/dist/api/routes/store/customers/get-payment-methods.js.map +1 -1
  211. package/dist/api/routes/store/customers/index.d.ts +1 -1
  212. package/dist/api/routes/store/customers/index.js.map +1 -1
  213. package/dist/api/routes/store/customers/update-address.js +1 -1
  214. package/dist/api/routes/store/customers/update-address.js.map +1 -1
  215. package/dist/api/routes/store/customers/update-customer.d.ts +1 -1
  216. package/dist/api/routes/store/customers/update-customer.js.map +1 -1
  217. package/dist/api/routes/store/gift-cards/index.d.ts +1 -1
  218. package/dist/api/routes/store/gift-cards/index.js +6 -1
  219. package/dist/api/routes/store/gift-cards/index.js.map +1 -1
  220. package/dist/api/routes/store/products/get-product.js +51 -12
  221. package/dist/api/routes/store/products/get-product.js.map +1 -1
  222. package/dist/api/routes/store/products/list-products.js +30 -7
  223. package/dist/api/routes/store/products/list-products.js.map +1 -1
  224. package/dist/api/routes/store/shipping-options/list-options.js +6 -2
  225. package/dist/api/routes/store/shipping-options/list-options.js.map +1 -1
  226. package/dist/api/routes/store/shipping-options/list-shipping-options.js +8 -2
  227. package/dist/api/routes/store/shipping-options/list-shipping-options.js.map +1 -1
  228. package/dist/api/routes/store/variants/get-variant.js +51 -12
  229. package/dist/api/routes/store/variants/get-variant.js.map +1 -1
  230. package/dist/api/routes/store/variants/list-variants.js +29 -6
  231. package/dist/api/routes/store/variants/list-variants.js.map +1 -1
  232. package/dist/commands/migrate.js +29 -14
  233. package/dist/commands/migrate.js.map +1 -1
  234. package/dist/commands/seed.js +14 -10
  235. package/dist/commands/seed.js.map +1 -1
  236. package/dist/commands/start.js +1 -1
  237. package/dist/commands/utils/get-migrations.d.ts +2 -1
  238. package/dist/commands/utils/get-migrations.js +95 -36
  239. package/dist/commands/utils/get-migrations.js.map +1 -1
  240. package/dist/config/index.d.ts +14 -0
  241. package/dist/config/index.js +40 -0
  242. package/dist/config/index.js.map +1 -0
  243. package/dist/controllers/products/admin-list-products.js +16 -8
  244. package/dist/controllers/products/admin-list-products.js.map +1 -1
  245. package/dist/helpers/test-request.js +21 -15
  246. package/dist/helpers/test-request.js.map +1 -1
  247. package/dist/interfaces/abstract-parser.d.ts +25 -0
  248. package/dist/interfaces/abstract-parser.js +14 -0
  249. package/dist/interfaces/abstract-parser.js.map +1 -0
  250. package/dist/interfaces/base-service.d.ts +66 -0
  251. package/dist/interfaces/base-service.js +377 -0
  252. package/dist/interfaces/base-service.js.map +1 -0
  253. package/dist/interfaces/batch-job-strategy.d.ts +34 -0
  254. package/dist/interfaces/batch-job-strategy.js +182 -0
  255. package/dist/interfaces/batch-job-strategy.js.map +1 -0
  256. package/dist/interfaces/csv-parser.d.ts +46 -0
  257. package/dist/interfaces/csv-parser.js +15 -0
  258. package/dist/interfaces/csv-parser.js.map +1 -0
  259. package/dist/interfaces/file-service.d.ts +66 -0
  260. package/dist/interfaces/file-service.js +32 -0
  261. package/dist/interfaces/file-service.js.map +1 -0
  262. package/dist/interfaces/index.d.ts +2 -0
  263. package/dist/interfaces/index.js +2 -0
  264. package/dist/interfaces/index.js.map +1 -1
  265. package/dist/loaders/config.js +3 -2
  266. package/dist/loaders/config.js.map +1 -1
  267. package/dist/loaders/feature-flags/index.d.ts +6 -0
  268. package/dist/loaders/feature-flags/index.js +71 -0
  269. package/dist/loaders/feature-flags/index.js.map +1 -0
  270. package/dist/loaders/index.d.ts +1 -1
  271. package/dist/loaders/index.js +8 -5
  272. package/dist/loaders/index.js.map +1 -1
  273. package/dist/loaders/plugins.d.ts +4 -3
  274. package/dist/loaders/plugins.js +71 -34
  275. package/dist/loaders/plugins.js.map +1 -1
  276. package/dist/loaders/redis.d.ts +1 -1
  277. package/dist/loaders/redis.js.map +1 -1
  278. package/dist/loaders/search-index.js +1 -0
  279. package/dist/loaders/search-index.js.map +1 -1
  280. package/dist/loaders/strategies.d.ts +2 -2
  281. package/dist/loaders/strategies.js +21 -7
  282. package/dist/loaders/strategies.js.map +1 -1
  283. package/dist/migrations/1649775522087-add_batch_job_model.js +4 -4
  284. package/dist/migrations/1649775522087-add_batch_job_model.js.map +1 -1
  285. package/dist/models/batch-job.d.ts +45 -4
  286. package/dist/models/batch-job.js +94 -7
  287. package/dist/models/batch-job.js.map +1 -1
  288. package/dist/models/customer.d.ts +1 -1
  289. package/dist/models/customer.js +1 -1
  290. package/dist/models/customer.js.map +1 -1
  291. package/dist/models/draft-order.d.ts +1 -2
  292. package/dist/models/draft-order.js +2 -2
  293. package/dist/models/draft-order.js.map +1 -1
  294. package/dist/models/line-item.d.ts +9 -2
  295. package/dist/models/line-item.js +2 -2
  296. package/dist/models/line-item.js.map +1 -1
  297. package/dist/models/product.d.ts +17 -17
  298. package/dist/models/product.js +34 -34
  299. package/dist/models/product.js.map +1 -1
  300. package/dist/models/shipping-method.d.ts +1 -1
  301. package/dist/models/shipping-method.js +1 -1
  302. package/dist/models/shipping-method.js.map +1 -1
  303. package/dist/models/shipping-option.d.ts +1 -1
  304. package/dist/models/shipping-option.js +1 -1
  305. package/dist/models/shipping-option.js.map +1 -1
  306. package/dist/repositories/customer.d.ts +2 -1
  307. package/dist/repositories/customer.js +18 -7
  308. package/dist/repositories/customer.js.map +1 -1
  309. package/dist/repositories/gift-card.d.ts +5 -1
  310. package/dist/repositories/gift-card.js +124 -29
  311. package/dist/repositories/gift-card.js.map +1 -1
  312. package/dist/repositories/image.d.ts +1 -0
  313. package/dist/repositories/image.js +100 -0
  314. package/dist/repositories/image.js.map +1 -1
  315. package/dist/repositories/price-list.js.map +1 -1
  316. package/dist/repositories/product-tag.d.ts +6 -0
  317. package/dist/repositories/product-tag.js +113 -0
  318. package/dist/repositories/product-tag.js.map +1 -1
  319. package/dist/repositories/product-type.d.ts +5 -0
  320. package/dist/repositories/product-type.js +64 -0
  321. package/dist/repositories/product-type.js.map +1 -1
  322. package/dist/repositories/product-variant.js +4 -5
  323. package/dist/repositories/product-variant.js.map +1 -1
  324. package/dist/repositories/product.d.ts +14 -17
  325. package/dist/repositories/product.js +64 -2
  326. package/dist/repositories/product.js.map +1 -1
  327. package/dist/repositories/tax-rate.d.ts +1 -1
  328. package/dist/repositories/tax-rate.js +2 -9
  329. package/dist/repositories/tax-rate.js.map +1 -1
  330. package/dist/services/auth.d.ts +16 -7
  331. package/dist/services/auth.js +130 -93
  332. package/dist/services/auth.js.map +1 -1
  333. package/dist/services/batch-job.d.ts +30 -5
  334. package/dist/services/batch-job.js +437 -2
  335. package/dist/services/batch-job.js.map +1 -1
  336. package/dist/services/cart.js +34 -13
  337. package/dist/services/cart.js.map +1 -1
  338. package/dist/services/claim-item.d.ts +23 -27
  339. package/dist/services/claim-item.js +217 -247
  340. package/dist/services/claim-item.js.map +1 -1
  341. package/dist/services/claim.d.ts +89 -71
  342. package/dist/services/claim.js +911 -879
  343. package/dist/services/claim.js.map +1 -1
  344. package/dist/services/csv-parser.d.ts +17 -0
  345. package/dist/services/csv-parser.js +319 -0
  346. package/dist/services/csv-parser.js.map +1 -0
  347. package/dist/services/custom-shipping-option.d.ts +30 -32
  348. package/dist/services/custom-shipping-option.js +78 -74
  349. package/dist/services/custom-shipping-option.js.map +1 -1
  350. package/dist/services/customer.d.ts +48 -50
  351. package/dist/services/customer.js +465 -428
  352. package/dist/services/customer.js.map +1 -1
  353. package/dist/services/discount-condition.d.ts +13 -13
  354. package/dist/services/discount-condition.js +5 -16
  355. package/dist/services/discount-condition.js.map +1 -1
  356. package/dist/services/discount.d.ts +22 -16
  357. package/dist/services/discount.js +530 -484
  358. package/dist/services/discount.js.map +1 -1
  359. package/dist/services/draft-order.d.ts +69 -66
  360. package/dist/services/draft-order.js +375 -353
  361. package/dist/services/draft-order.js.map +1 -1
  362. package/dist/services/event-bus.d.ts +72 -56
  363. package/dist/services/event-bus.js +122 -99
  364. package/dist/services/event-bus.js.map +1 -1
  365. package/dist/services/file.d.ts +14 -0
  366. package/dist/services/file.js +43 -0
  367. package/dist/services/file.js.map +1 -0
  368. package/dist/services/fulfillment.d.ts +63 -64
  369. package/dist/services/fulfillment.js +200 -194
  370. package/dist/services/fulfillment.js.map +1 -1
  371. package/dist/services/gift-card.d.ts +54 -45
  372. package/dist/services/gift-card.js +239 -221
  373. package/dist/services/gift-card.js.map +1 -1
  374. package/dist/services/idempotency-key.d.ts +1 -5
  375. package/dist/services/idempotency-key.js +2 -5
  376. package/dist/services/idempotency-key.js.map +1 -1
  377. package/dist/services/index.d.ts +11 -9
  378. package/dist/services/index.js +23 -19
  379. package/dist/services/index.js.map +1 -1
  380. package/dist/services/line-item.d.ts +3 -2
  381. package/dist/services/line-item.js +9 -7
  382. package/dist/services/line-item.js.map +1 -1
  383. package/dist/services/price-list.d.ts +4 -1
  384. package/dist/services/price-list.js +7 -4
  385. package/dist/services/price-list.js.map +1 -1
  386. package/dist/services/pricing.d.ts +101 -0
  387. package/dist/services/pricing.js +588 -0
  388. package/dist/services/pricing.js.map +1 -0
  389. package/dist/services/product-variant.d.ts +0 -11
  390. package/dist/services/product-variant.js +28 -138
  391. package/dist/services/product-variant.js.map +1 -1
  392. package/dist/services/product.d.ts +107 -145
  393. package/dist/services/product.js +708 -1036
  394. package/dist/services/product.js.map +1 -1
  395. package/dist/services/region.js +4 -4
  396. package/dist/services/region.js.map +1 -1
  397. package/dist/services/shipping-option.d.ts +43 -108
  398. package/dist/services/shipping-option.js +305 -352
  399. package/dist/services/shipping-option.js.map +1 -1
  400. package/dist/services/shipping-profile.d.ts +2 -2
  401. package/dist/services/shipping-profile.js +1 -1
  402. package/dist/services/store.d.ts +36 -45
  403. package/dist/services/store.js +207 -217
  404. package/dist/services/store.js.map +1 -1
  405. package/dist/services/strategy-resolver.d.ts +13 -0
  406. package/dist/services/strategy-resolver.js +40 -0
  407. package/dist/services/strategy-resolver.js.map +1 -0
  408. package/dist/services/tax-provider.d.ts +7 -3
  409. package/dist/services/tax-provider.js +6 -6
  410. package/dist/services/tax-provider.js.map +1 -1
  411. package/dist/services/tax-rate.d.ts +1 -1
  412. package/dist/services/tax-rate.js +2 -2
  413. package/dist/services/tax-rate.js.map +1 -1
  414. package/dist/services/totals.js.map +1 -1
  415. package/dist/services/user.d.ts +8 -9
  416. package/dist/services/user.js +216 -168
  417. package/dist/services/user.js.map +1 -1
  418. package/dist/strategies/__fixtures__/order-export-data.d.ts +3 -0
  419. package/dist/strategies/__fixtures__/order-export-data.js +70 -0
  420. package/dist/strategies/__fixtures__/order-export-data.js.map +1 -0
  421. package/dist/strategies/__fixtures__/product-export-data.d.ts +319 -0
  422. package/dist/strategies/__fixtures__/product-export-data.js +401 -0
  423. package/dist/strategies/__fixtures__/product-export-data.js.map +1 -0
  424. package/dist/strategies/batch-jobs/order/export.d.ts +36 -0
  425. package/dist/strategies/batch-jobs/order/export.js +351 -0
  426. package/dist/strategies/batch-jobs/order/export.js.map +1 -0
  427. package/dist/strategies/batch-jobs/order/index.d.ts +29 -0
  428. package/dist/strategies/batch-jobs/order/index.js +115 -0
  429. package/dist/strategies/batch-jobs/order/index.js.map +1 -0
  430. package/dist/strategies/batch-jobs/product/export.d.ts +36 -0
  431. package/dist/strategies/batch-jobs/product/export.js +546 -0
  432. package/dist/strategies/batch-jobs/product/export.js.map +1 -0
  433. package/dist/strategies/batch-jobs/product/index.d.ts +45 -0
  434. package/dist/strategies/batch-jobs/product/index.js +272 -0
  435. package/dist/strategies/batch-jobs/product/index.js.map +1 -0
  436. package/dist/subscribers/batch-job.d.ts +17 -0
  437. package/dist/subscribers/batch-job.js +96 -0
  438. package/dist/subscribers/batch-job.js.map +1 -0
  439. package/dist/types/batch-job.d.ts +23 -2
  440. package/dist/types/batch-job.js +4 -1
  441. package/dist/types/batch-job.js.map +1 -1
  442. package/dist/types/claim.d.ts +67 -0
  443. package/dist/types/claim.js +3 -0
  444. package/dist/types/claim.js.map +1 -0
  445. package/dist/types/common.d.ts +27 -3
  446. package/dist/types/common.js.map +1 -1
  447. package/dist/types/customers.d.ts +24 -0
  448. package/dist/types/customers.js.map +1 -1
  449. package/dist/types/discount.d.ts +2 -1
  450. package/dist/types/discount.js.map +1 -1
  451. package/dist/types/feature-flags.d.ts +9 -0
  452. package/dist/types/feature-flags.js +3 -0
  453. package/dist/types/feature-flags.js.map +1 -0
  454. package/dist/types/fulfillment.d.ts +27 -0
  455. package/dist/types/fulfillment.js +3 -0
  456. package/dist/types/fulfillment.js.map +1 -0
  457. package/dist/types/gift-card.d.ts +21 -0
  458. package/dist/types/gift-card.js +3 -0
  459. package/dist/types/gift-card.js.map +1 -0
  460. package/dist/types/global.d.ts +12 -1
  461. package/dist/types/price-list.d.ts +9 -0
  462. package/dist/types/pricing.d.ts +30 -0
  463. package/dist/types/pricing.js +3 -0
  464. package/dist/types/pricing.js.map +1 -0
  465. package/dist/types/product-variant.d.ts +4 -3
  466. package/dist/types/product-variant.js.map +1 -1
  467. package/dist/types/product.d.ts +110 -8
  468. package/dist/types/product.js +8 -11
  469. package/dist/types/product.js.map +1 -1
  470. package/dist/types/shipping-options.d.ts +62 -0
  471. package/dist/types/shipping-options.js +3 -0
  472. package/dist/types/shipping-options.js.map +1 -0
  473. package/dist/types/store.d.ts +9 -0
  474. package/dist/types/store.js +3 -0
  475. package/dist/types/store.js.map +1 -0
  476. package/dist/utils/build-query.d.ts +7 -7
  477. package/dist/utils/build-query.js +9 -5
  478. package/dist/utils/build-query.js.map +1 -1
  479. package/dist/utils/countries.d.ts +3 -2
  480. package/dist/utils/countries.js.map +1 -1
  481. package/dist/utils/currencies.d.ts +10 -1899
  482. package/dist/utils/currencies.js.map +1 -1
  483. package/dist/utils/db-aware-column.js +2 -2
  484. package/dist/utils/db-aware-column.js.map +1 -1
  485. package/dist/utils/feature-flag-decorators.d.ts +4 -0
  486. package/dist/utils/feature-flag-decorators.js +52 -0
  487. package/dist/utils/feature-flag-decorators.js.map +1 -0
  488. package/dist/utils/flag-router.d.ts +7 -0
  489. package/dist/utils/flag-router.js +19 -0
  490. package/dist/utils/flag-router.js.map +1 -0
  491. package/dist/utils/format-registration-name.d.ts +4 -4
  492. package/dist/utils/format-registration-name.js +7 -10
  493. package/dist/utils/format-registration-name.js.map +1 -1
  494. package/dist/utils/get-query-config.d.ts +4 -6
  495. package/dist/utils/get-query-config.js +46 -1
  496. package/dist/utils/get-query-config.js.map +1 -1
  497. package/dist/utils/omit-relation-if-exists.d.ts +7 -0
  498. package/dist/utils/omit-relation-if-exists.js +16 -0
  499. package/dist/utils/omit-relation-if-exists.js.map +1 -0
  500. package/dist/utils/set-metadata.d.ts +6 -6
  501. package/dist/utils/set-metadata.js +5 -5
  502. package/dist/utils/set-metadata.js.map +1 -1
  503. package/dist/utils/sleep.d.ts +1 -0
  504. package/dist/utils/sleep.js +50 -0
  505. package/dist/utils/sleep.js.map +1 -0
  506. package/package.json +6 -4
  507. package/yarn-error.log +29038 -0
@@ -112,11 +112,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
112
112
  var iso8601_duration_1 = require("iso8601-duration");
113
113
  var lodash_1 = require("lodash");
114
114
  var medusa_core_utils_1 = require("medusa-core-utils");
115
- var medusa_interfaces_1 = require("medusa-interfaces");
116
115
  var typeorm_1 = require("typeorm");
117
116
  var discount_rule_1 = require("../models/discount-rule");
118
117
  var date_helpers_1 = require("../utils/date-helpers");
119
118
  var exception_formatter_1 = require("../utils/exception-formatter");
119
+ var interfaces_1 = require("../interfaces");
120
+ var utils_1 = require("../utils");
120
121
  /**
121
122
  * Provides layer to manipulate discounts.
122
123
  * @implements {BaseService}
@@ -125,52 +126,22 @@ var DiscountService = /** @class */ (function (_super) {
125
126
  __extends(DiscountService, _super);
126
127
  function DiscountService(_a) {
127
128
  var manager = _a.manager, discountRepository = _a.discountRepository, discountRuleRepository = _a.discountRuleRepository, giftCardRepository = _a.giftCardRepository, discountConditionRepository = _a.discountConditionRepository, discountConditionService = _a.discountConditionService, totalsService = _a.totalsService, productService = _a.productService, regionService = _a.regionService, customerService = _a.customerService, eventBusService = _a.eventBusService;
128
- var _this = _super.call(this) || this;
129
- /** @private @const {EntityManager} */
129
+ var _this =
130
+ // eslint-disable-next-line prefer-rest-params
131
+ _super.call(this, arguments[0]) || this;
130
132
  _this.manager_ = manager;
131
- /** @private @const {DiscountRepository} */
132
133
  _this.discountRepository_ = discountRepository;
133
- /** @private @const {DiscountRuleRepository} */
134
134
  _this.discountRuleRepository_ = discountRuleRepository;
135
- /** @private @const {GiftCardRepository} */
136
135
  _this.giftCardRepository_ = giftCardRepository;
137
- /** @private @const {DiscountConditionRepository} */
138
136
  _this.discountConditionRepository_ = discountConditionRepository;
139
- /** @private @const {DiscountConditionRepository} */
140
137
  _this.discountConditionService_ = discountConditionService;
141
- /** @private @const {TotalsService} */
142
138
  _this.totalsService_ = totalsService;
143
- /** @private @const {ProductService} */
144
139
  _this.productService_ = productService;
145
- /** @private @const {RegionService} */
146
140
  _this.regionService_ = regionService;
147
- /** @private @const {CustomerService} */
148
141
  _this.customerService_ = customerService;
149
- /** @private @const {EventBus} */
150
142
  _this.eventBus_ = eventBusService;
151
143
  return _this;
152
144
  }
153
- DiscountService.prototype.withTransaction = function (transactionManager) {
154
- if (!transactionManager) {
155
- return this;
156
- }
157
- var cloned = new DiscountService({
158
- manager: transactionManager,
159
- discountRepository: this.discountRepository_,
160
- discountRuleRepository: this.discountRuleRepository_,
161
- giftCardRepository: this.giftCardRepository_,
162
- discountConditionRepository: this.discountConditionRepository_,
163
- discountConditionService: this.discountConditionService_,
164
- totalsService: this.totalsService_,
165
- productService: this.productService_,
166
- regionService: this.regionService_,
167
- customerService: this.customerService_,
168
- eventBusService: this.eventBus_,
169
- });
170
- cloned.transactionManager_ = transactionManager;
171
- cloned.manager_ = transactionManager;
172
- return cloned;
173
- };
174
145
  /**
175
146
  * Creates a discount rule with provided data given that the data is validated.
176
147
  * @param {DiscountRule} discountRule - the discount rule to create
@@ -191,11 +162,23 @@ var DiscountService = /** @class */ (function (_super) {
191
162
  if (selector === void 0) { selector = {}; }
192
163
  if (config === void 0) { config = { relations: [], skip: 0, take: 10 }; }
193
164
  return __awaiter(this, void 0, void 0, function () {
194
- var discountRepo, query;
165
+ var _this = this;
195
166
  return __generator(this, function (_a) {
196
- discountRepo = this.manager_.getCustomRepository(this.discountRepository_);
197
- query = this.buildQuery_(selector, config);
198
- return [2 /*return*/, discountRepo.find(query)];
167
+ switch (_a.label) {
168
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
169
+ var discountRepo, query;
170
+ return __generator(this, function (_a) {
171
+ switch (_a.label) {
172
+ case 0:
173
+ discountRepo = transactionManager.getCustomRepository(this.discountRepository_);
174
+ query = (0, utils_1.buildQuery)(selector, config);
175
+ return [4 /*yield*/, discountRepo.find(query)];
176
+ case 1: return [2 /*return*/, _a.sent()];
177
+ }
178
+ });
179
+ }); })];
180
+ case 1: return [2 /*return*/, _a.sent()];
181
+ }
199
182
  });
200
183
  });
201
184
  };
@@ -212,30 +195,38 @@ var DiscountService = /** @class */ (function (_super) {
212
195
  order: { created_at: "DESC" },
213
196
  }; }
214
197
  return __awaiter(this, void 0, void 0, function () {
215
- var discountRepo, q, query, where_1, _a, discounts, count;
216
- return __generator(this, function (_b) {
217
- switch (_b.label) {
218
- case 0:
219
- discountRepo = this.manager_.getCustomRepository(this.discountRepository_);
220
- if ("q" in selector) {
221
- q = selector.q;
222
- delete selector.q;
223
- }
224
- query = this.buildQuery_(selector, config);
225
- if (q) {
226
- where_1 = query.where;
227
- delete where_1.code;
228
- query.where = function (qb) {
229
- qb.where(where_1);
230
- qb.andWhere(new typeorm_1.Brackets(function (qb) {
231
- qb.where({ code: (0, typeorm_1.ILike)("%".concat(q, "%")) });
232
- }));
233
- };
234
- }
235
- return [4 /*yield*/, discountRepo.findAndCount(query)];
236
- case 1:
237
- _a = __read.apply(void 0, [_b.sent(), 2]), discounts = _a[0], count = _a[1];
238
- return [2 /*return*/, [discounts, count]];
198
+ var _this = this;
199
+ return __generator(this, function (_a) {
200
+ switch (_a.label) {
201
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
202
+ var discountRepo, q, query, where_1, _a, discounts, count;
203
+ return __generator(this, function (_b) {
204
+ switch (_b.label) {
205
+ case 0:
206
+ discountRepo = transactionManager.getCustomRepository(this.discountRepository_);
207
+ if ("q" in selector) {
208
+ q = selector.q;
209
+ delete selector.q;
210
+ }
211
+ query = (0, utils_1.buildQuery)(selector, config);
212
+ if (q) {
213
+ where_1 = query.where;
214
+ delete where_1.code;
215
+ query.where = function (qb) {
216
+ qb.where(where_1);
217
+ qb.andWhere(new typeorm_1.Brackets(function (qb) {
218
+ qb.where({ code: (0, typeorm_1.ILike)("%".concat(q, "%")) });
219
+ }));
220
+ };
221
+ }
222
+ return [4 /*yield*/, discountRepo.findAndCount(query)];
223
+ case 1:
224
+ _a = __read.apply(void 0, [_b.sent(), 2]), discounts = _a[0], count = _a[1];
225
+ return [2 /*return*/, [discounts, count]];
226
+ }
227
+ });
228
+ }); })];
229
+ case 1: return [2 /*return*/, _a.sent()];
239
230
  }
240
231
  });
241
232
  });
@@ -250,69 +241,72 @@ var DiscountService = /** @class */ (function (_super) {
250
241
  return __awaiter(this, void 0, void 0, function () {
251
242
  var _this = this;
252
243
  return __generator(this, function (_a) {
253
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
254
- var discountRepo, ruleRepo, conditions, ruleToCreate, validatedRule, _a, discountRule, createdDiscountRule, created, result_1, error_1;
255
- var _this = this;
256
- var _b, _c;
257
- return __generator(this, function (_d) {
258
- switch (_d.label) {
259
- case 0:
260
- discountRepo = manager.getCustomRepository(this.discountRepository_);
261
- ruleRepo = manager.getCustomRepository(this.discountRuleRepository_);
262
- conditions = (_b = discount.rule) === null || _b === void 0 ? void 0 : _b.conditions;
263
- ruleToCreate = (0, lodash_1.omit)(discount.rule, ["conditions"]);
264
- validatedRule = this.validateDiscountRule_(ruleToCreate);
265
- if ((discount === null || discount === void 0 ? void 0 : discount.regions) &&
266
- (discount === null || discount === void 0 ? void 0 : discount.regions.length) > 1 &&
267
- ((_c = discount === null || discount === void 0 ? void 0 : discount.rule) === null || _c === void 0 ? void 0 : _c.type) === "fixed") {
268
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Fixed discounts can have one region");
269
- }
270
- _d.label = 1;
271
- case 1:
272
- _d.trys.push([1, 8, , 9]);
273
- if (!discount.regions) return [3 /*break*/, 3];
274
- _a = discount;
275
- return [4 /*yield*/, Promise.all(discount.regions.map(function (regionId) {
276
- return _this.regionService_.withTransaction(manager).retrieve(regionId);
277
- }))];
278
- case 2:
279
- _a.regions = _d.sent();
280
- _d.label = 3;
281
- case 3:
282
- discountRule = ruleRepo.create(validatedRule);
283
- return [4 /*yield*/, ruleRepo.save(discountRule)];
284
- case 4:
285
- createdDiscountRule = _d.sent();
286
- discount.code = discount.code.toUpperCase();
287
- created = discountRepo.create(discount);
288
- created.rule = createdDiscountRule;
289
- return [4 /*yield*/, discountRepo.save(created)];
290
- case 5:
291
- result_1 = _d.sent();
292
- if (!(conditions === null || conditions === void 0 ? void 0 : conditions.length)) return [3 /*break*/, 7];
293
- return [4 /*yield*/, Promise.all(conditions.map(function (cond) { return __awaiter(_this, void 0, void 0, function () {
294
- return __generator(this, function (_a) {
295
- switch (_a.label) {
296
- case 0: return [4 /*yield*/, this.discountConditionService_
297
- .withTransaction(manager)
298
- .upsertCondition(__assign({ rule_id: result_1.rule_id }, cond))];
299
- case 1:
300
- _a.sent();
301
- return [2 /*return*/];
302
- }
303
- });
304
- }); }))];
305
- case 6:
306
- _d.sent();
307
- _d.label = 7;
308
- case 7: return [2 /*return*/, result_1];
309
- case 8:
310
- error_1 = _d.sent();
311
- throw (0, exception_formatter_1.formatException)(error_1);
312
- case 9: return [2 /*return*/];
313
- }
314
- });
315
- }); })];
244
+ switch (_a.label) {
245
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
246
+ var discountRepo, ruleRepo, conditions, ruleToCreate, validatedRule, _a, discountRule, createdDiscountRule, created, result_1, error_1;
247
+ var _this = this;
248
+ var _b, _c;
249
+ return __generator(this, function (_d) {
250
+ switch (_d.label) {
251
+ case 0:
252
+ discountRepo = manager.getCustomRepository(this.discountRepository_);
253
+ ruleRepo = manager.getCustomRepository(this.discountRuleRepository_);
254
+ conditions = (_b = discount.rule) === null || _b === void 0 ? void 0 : _b.conditions;
255
+ ruleToCreate = (0, lodash_1.omit)(discount.rule, ["conditions"]);
256
+ validatedRule = this.validateDiscountRule_(ruleToCreate);
257
+ if ((discount === null || discount === void 0 ? void 0 : discount.regions) &&
258
+ (discount === null || discount === void 0 ? void 0 : discount.regions.length) > 1 &&
259
+ ((_c = discount === null || discount === void 0 ? void 0 : discount.rule) === null || _c === void 0 ? void 0 : _c.type) === "fixed") {
260
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Fixed discounts can have one region");
261
+ }
262
+ _d.label = 1;
263
+ case 1:
264
+ _d.trys.push([1, 8, , 9]);
265
+ if (!discount.regions) return [3 /*break*/, 3];
266
+ _a = discount;
267
+ return [4 /*yield*/, Promise.all(discount.regions.map(function (regionId) {
268
+ return _this.regionService_.withTransaction(manager).retrieve(regionId);
269
+ }))];
270
+ case 2:
271
+ _a.regions = (_d.sent());
272
+ _d.label = 3;
273
+ case 3:
274
+ discountRule = ruleRepo.create(validatedRule);
275
+ return [4 /*yield*/, ruleRepo.save(discountRule)];
276
+ case 4:
277
+ createdDiscountRule = _d.sent();
278
+ discount.code = discount.code.toUpperCase();
279
+ created = discountRepo.create(discount);
280
+ created.rule = createdDiscountRule;
281
+ return [4 /*yield*/, discountRepo.save(created)];
282
+ case 5:
283
+ result_1 = _d.sent();
284
+ if (!(conditions === null || conditions === void 0 ? void 0 : conditions.length)) return [3 /*break*/, 7];
285
+ return [4 /*yield*/, Promise.all(conditions.map(function (cond) { return __awaiter(_this, void 0, void 0, function () {
286
+ return __generator(this, function (_a) {
287
+ switch (_a.label) {
288
+ case 0: return [4 /*yield*/, this.discountConditionService_
289
+ .withTransaction(manager)
290
+ .upsertCondition(__assign({ rule_id: result_1.rule_id }, cond))];
291
+ case 1:
292
+ _a.sent();
293
+ return [2 /*return*/];
294
+ }
295
+ });
296
+ }); }))];
297
+ case 6:
298
+ _d.sent();
299
+ _d.label = 7;
300
+ case 7: return [2 /*return*/, result_1];
301
+ case 8:
302
+ error_1 = _d.sent();
303
+ throw (0, exception_formatter_1.formatException)(error_1);
304
+ case 9: return [2 /*return*/];
305
+ }
306
+ });
307
+ }); })];
308
+ case 1: return [2 /*return*/, _a.sent()];
309
+ }
316
310
  });
317
311
  });
318
312
  };
@@ -325,20 +319,27 @@ var DiscountService = /** @class */ (function (_super) {
325
319
  DiscountService.prototype.retrieve = function (discountId, config) {
326
320
  if (config === void 0) { config = {}; }
327
321
  return __awaiter(this, void 0, void 0, function () {
328
- var discountRepo, validatedId, query, discount;
322
+ var _this = this;
329
323
  return __generator(this, function (_a) {
330
324
  switch (_a.label) {
331
- case 0:
332
- discountRepo = this.manager_.getCustomRepository(this.discountRepository_);
333
- validatedId = this.validateId_(discountId);
334
- query = this.buildQuery_({ id: validatedId }, config);
335
- return [4 /*yield*/, discountRepo.findOne(query)];
336
- case 1:
337
- discount = _a.sent();
338
- if (!discount) {
339
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Discount with ".concat(discountId, " was not found"));
340
- }
341
- return [2 /*return*/, discount];
325
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
326
+ var discountRepo, query, discount;
327
+ return __generator(this, function (_a) {
328
+ switch (_a.label) {
329
+ case 0:
330
+ discountRepo = transactionManager.getCustomRepository(this.discountRepository_);
331
+ query = (0, utils_1.buildQuery)({ id: discountId }, config);
332
+ return [4 /*yield*/, discountRepo.findOne(query)];
333
+ case 1:
334
+ discount = _a.sent();
335
+ if (!discount) {
336
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Discount with ".concat(discountId, " was not found"));
337
+ }
338
+ return [2 /*return*/, discount];
339
+ }
340
+ });
341
+ }); })];
342
+ case 1: return [2 /*return*/, _a.sent()];
342
343
  }
343
344
  });
344
345
  });
@@ -352,25 +353,33 @@ var DiscountService = /** @class */ (function (_super) {
352
353
  DiscountService.prototype.retrieveByCode = function (discountCode, config) {
353
354
  if (config === void 0) { config = {}; }
354
355
  return __awaiter(this, void 0, void 0, function () {
355
- var discountRepo, query, discount;
356
+ var _this = this;
356
357
  return __generator(this, function (_a) {
357
358
  switch (_a.label) {
358
- case 0:
359
- discountRepo = this.manager_.getCustomRepository(this.discountRepository_);
360
- query = this.buildQuery_({ code: discountCode, is_dynamic: false }, config);
361
- return [4 /*yield*/, discountRepo.findOne(query)];
362
- case 1:
363
- discount = _a.sent();
364
- if (!!discount) return [3 /*break*/, 3];
365
- query = this.buildQuery_({ code: discountCode, is_dynamic: true }, config);
366
- return [4 /*yield*/, discountRepo.findOne(query)];
367
- case 2:
368
- discount = _a.sent();
369
- if (!discount) {
370
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Discount with code ".concat(discountCode, " was not found"));
371
- }
372
- _a.label = 3;
373
- case 3: return [2 /*return*/, discount];
359
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (transactionManager) { return __awaiter(_this, void 0, void 0, function () {
360
+ var discountRepo, query, discount;
361
+ return __generator(this, function (_a) {
362
+ switch (_a.label) {
363
+ case 0:
364
+ discountRepo = transactionManager.getCustomRepository(this.discountRepository_);
365
+ query = (0, utils_1.buildQuery)({ code: discountCode, is_dynamic: false }, config);
366
+ return [4 /*yield*/, discountRepo.findOne(query)];
367
+ case 1:
368
+ discount = _a.sent();
369
+ if (!!discount) return [3 /*break*/, 3];
370
+ query = (0, utils_1.buildQuery)({ code: discountCode, is_dynamic: true }, config);
371
+ return [4 /*yield*/, discountRepo.findOne(query)];
372
+ case 2:
373
+ discount = _a.sent();
374
+ if (!discount) {
375
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Discount with code ".concat(discountCode, " was not found"));
376
+ }
377
+ _a.label = 3;
378
+ case 3: return [2 /*return*/, discount];
379
+ }
380
+ });
381
+ }); })];
382
+ case 1: return [2 /*return*/, _a.sent()];
374
383
  }
375
384
  });
376
385
  });
@@ -385,98 +394,98 @@ var DiscountService = /** @class */ (function (_super) {
385
394
  return __awaiter(this, void 0, void 0, function () {
386
395
  var _this = this;
387
396
  return __generator(this, function (_a) {
388
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
389
- var discountRepo, ruleRepo, discount, conditions, ruleToUpdate, rule, metadata, regions, rest, _a, _b, ruleUpdate, updatedRule, _c, _d, key, updated;
390
- var e_1, _e;
391
- var _this = this;
392
- var _f;
393
- return __generator(this, function (_g) {
394
- switch (_g.label) {
395
- case 0:
396
- discountRepo = manager.getCustomRepository(this.discountRepository_);
397
- ruleRepo = manager.getCustomRepository(this.discountRuleRepository_);
398
- return [4 /*yield*/, this.retrieve(discountId, {
399
- relations: ["rule"],
400
- })];
401
- case 1:
402
- discount = _g.sent();
403
- conditions = (_f = update === null || update === void 0 ? void 0 : update.rule) === null || _f === void 0 ? void 0 : _f.conditions;
404
- ruleToUpdate = (0, lodash_1.omit)(update.rule, "conditions");
405
- if (!(0, lodash_1.isEmpty)(ruleToUpdate)) {
406
- update.rule = ruleToUpdate;
407
- }
408
- rule = update.rule, metadata = update.metadata, regions = update.regions, rest = __rest(update, ["rule", "metadata", "regions"]);
409
- if (rest.ends_at) {
410
- if (discount.starts_at >= new Date(rest.ends_at)) {
411
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "\"ends_at\" must be greater than \"starts_at\"");
397
+ switch (_a.label) {
398
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
399
+ var discountRepo, ruleRepo, discount, conditions, ruleToUpdate, rule, metadata, regions, rest, _a, _b, ruleUpdate, _c, _d, key;
400
+ var e_1, _e;
401
+ var _this = this;
402
+ var _f;
403
+ return __generator(this, function (_g) {
404
+ switch (_g.label) {
405
+ case 0:
406
+ discountRepo = manager.getCustomRepository(this.discountRepository_);
407
+ ruleRepo = manager.getCustomRepository(this.discountRuleRepository_);
408
+ return [4 /*yield*/, this.retrieve(discountId, {
409
+ relations: ["rule"],
410
+ })];
411
+ case 1:
412
+ discount = _g.sent();
413
+ conditions = (_f = update === null || update === void 0 ? void 0 : update.rule) === null || _f === void 0 ? void 0 : _f.conditions;
414
+ ruleToUpdate = (0, lodash_1.omit)(update.rule, "conditions");
415
+ if (!(0, lodash_1.isEmpty)(ruleToUpdate)) {
416
+ update.rule = ruleToUpdate;
412
417
  }
413
- }
414
- if (regions && (regions === null || regions === void 0 ? void 0 : regions.length) > 1 && discount.rule.type === "fixed") {
415
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Fixed discounts can have one region");
416
- }
417
- if (!(conditions === null || conditions === void 0 ? void 0 : conditions.length)) return [3 /*break*/, 3];
418
- return [4 /*yield*/, Promise.all(conditions.map(function (cond) { return __awaiter(_this, void 0, void 0, function () {
419
- return __generator(this, function (_a) {
420
- switch (_a.label) {
421
- case 0: return [4 /*yield*/, this.discountConditionService_
422
- .withTransaction(manager)
423
- .upsertCondition(__assign({ rule_id: discount.rule_id }, cond))];
424
- case 1:
425
- _a.sent();
426
- return [2 /*return*/];
427
- }
428
- });
429
- }); }))];
430
- case 2:
431
- _g.sent();
432
- _g.label = 3;
433
- case 3:
434
- if (!regions) return [3 /*break*/, 5];
435
- _a = discount;
436
- return [4 /*yield*/, Promise.all(regions.map(function (regionId) { return _this.regionService_.retrieve(regionId); }))];
437
- case 4:
438
- _a.regions = _g.sent();
439
- _g.label = 5;
440
- case 5:
441
- if (!metadata) return [3 /*break*/, 7];
442
- _b = discount;
443
- return [4 /*yield*/, this.setMetadata_(discount.id, metadata)];
444
- case 6:
445
- _b.metadata = _g.sent();
446
- _g.label = 7;
447
- case 7:
448
- if (rule) {
449
- ruleUpdate = rule;
450
- if (rule.value) {
451
- this.validateDiscountRule_({
452
- value: rule.value,
453
- type: discount.rule.type,
454
- });
418
+ rule = update.rule, metadata = update.metadata, regions = update.regions, rest = __rest(update, ["rule", "metadata", "regions"]);
419
+ if (rest.ends_at) {
420
+ if (discount.starts_at >= new Date(rest.ends_at)) {
421
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "\"ends_at\" must be greater than \"starts_at\"");
422
+ }
423
+ }
424
+ if (regions && (regions === null || regions === void 0 ? void 0 : regions.length) > 1 && discount.rule.type === "fixed") {
425
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Fixed discounts can have one region");
455
426
  }
456
- updatedRule = ruleRepo.create(__assign(__assign({}, discount.rule), ruleUpdate));
457
- discount.rule = updatedRule;
458
- }
459
- try {
460
- for (_c = __values(Object.keys(rest).filter(function (k) { return typeof rest[k] !== "undefined"; })), _d = _c.next(); !_d.done; _d = _c.next()) {
461
- key = _d.value;
462
- discount[key] = rest[key];
427
+ if (!(conditions === null || conditions === void 0 ? void 0 : conditions.length)) return [3 /*break*/, 3];
428
+ return [4 /*yield*/, Promise.all(conditions.map(function (cond) { return __awaiter(_this, void 0, void 0, function () {
429
+ return __generator(this, function (_a) {
430
+ switch (_a.label) {
431
+ case 0: return [4 /*yield*/, this.discountConditionService_
432
+ .withTransaction(manager)
433
+ .upsertCondition(__assign({ rule_id: discount.rule_id }, cond))];
434
+ case 1:
435
+ _a.sent();
436
+ return [2 /*return*/];
437
+ }
438
+ });
439
+ }); }))];
440
+ case 2:
441
+ _g.sent();
442
+ _g.label = 3;
443
+ case 3:
444
+ if (!regions) return [3 /*break*/, 5];
445
+ _a = discount;
446
+ return [4 /*yield*/, Promise.all(regions.map(function (regionId) { return _this.regionService_.retrieve(regionId); }))];
447
+ case 4:
448
+ _a.regions = _g.sent();
449
+ _g.label = 5;
450
+ case 5:
451
+ if (!metadata) return [3 /*break*/, 7];
452
+ _b = discount;
453
+ return [4 /*yield*/, (0, utils_1.setMetadata)(discount, metadata)];
454
+ case 6:
455
+ _b.metadata = _g.sent();
456
+ _g.label = 7;
457
+ case 7:
458
+ if (rule) {
459
+ ruleUpdate = rule;
460
+ if (rule.value) {
461
+ this.validateDiscountRule_({
462
+ value: rule.value,
463
+ type: discount.rule.type,
464
+ });
465
+ }
466
+ discount.rule = ruleRepo.create(__assign(__assign({}, discount.rule), ruleUpdate));
463
467
  }
464
- }
465
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
466
- finally {
467
468
  try {
468
- if (_d && !_d.done && (_e = _c.return)) _e.call(_c);
469
+ for (_c = __values(Object.keys(rest).filter(function (k) { return typeof rest[k] !== "undefined"; })), _d = _c.next(); !_d.done; _d = _c.next()) {
470
+ key = _d.value;
471
+ discount[key] = rest[key];
472
+ }
473
+ }
474
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
475
+ finally {
476
+ try {
477
+ if (_d && !_d.done && (_e = _c.return)) _e.call(_c);
478
+ }
479
+ finally { if (e_1) throw e_1.error; }
469
480
  }
470
- finally { if (e_1) throw e_1.error; }
471
- }
472
- discount.code = discount.code.toUpperCase();
473
- return [4 /*yield*/, discountRepo.save(discount)];
474
- case 8:
475
- updated = _g.sent();
476
- return [2 /*return*/, updated];
477
- }
478
- });
479
- }); })];
481
+ discount.code = discount.code.toUpperCase();
482
+ return [4 /*yield*/, discountRepo.save(discount)];
483
+ case 8: return [2 /*return*/, _g.sent()];
484
+ }
485
+ });
486
+ }); })];
487
+ case 1: return [2 /*return*/, _a.sent()];
488
+ }
480
489
  });
481
490
  });
482
491
  };
@@ -490,37 +499,36 @@ var DiscountService = /** @class */ (function (_super) {
490
499
  return __awaiter(this, void 0, void 0, function () {
491
500
  var _this = this;
492
501
  return __generator(this, function (_a) {
493
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
494
- var discountRepo, discount, toCreate, lastValidDate, created, result;
495
- return __generator(this, function (_a) {
496
- switch (_a.label) {
497
- case 0:
498
- discountRepo = manager.getCustomRepository(this.discountRepository_);
499
- return [4 /*yield*/, this.retrieve(discountId)];
500
- case 1:
501
- discount = _a.sent();
502
- if (!discount.is_dynamic) {
503
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Discount must be set to dynamic");
504
- }
505
- if (!data.code) {
506
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Discount must have a code");
507
- }
508
- toCreate = __assign(__assign({}, data), { rule_id: discount.rule_id, is_dynamic: true, is_disabled: false, code: data.code.toUpperCase(), parent_discount_id: discount.id, usage_limit: discount.usage_limit });
509
- if (discount.valid_duration) {
510
- lastValidDate = new Date();
511
- lastValidDate.setSeconds(lastValidDate.getSeconds() + (0, iso8601_duration_1.toSeconds)((0, iso8601_duration_1.parse)(discount.valid_duration)));
512
- toCreate.ends_at = lastValidDate;
513
- }
514
- return [4 /*yield*/, discountRepo.create(toCreate)];
515
- case 2:
516
- created = _a.sent();
517
- return [4 /*yield*/, discountRepo.save(created)];
518
- case 3:
519
- result = _a.sent();
520
- return [2 /*return*/, result];
521
- }
522
- });
523
- }); })];
502
+ switch (_a.label) {
503
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
504
+ var discountRepo, discount, toCreate, lastValidDate, created;
505
+ return __generator(this, function (_a) {
506
+ switch (_a.label) {
507
+ case 0:
508
+ discountRepo = manager.getCustomRepository(this.discountRepository_);
509
+ return [4 /*yield*/, this.retrieve(discountId)];
510
+ case 1:
511
+ discount = _a.sent();
512
+ if (!discount.is_dynamic) {
513
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Discount must be set to dynamic");
514
+ }
515
+ if (!data.code) {
516
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Discount must have a code");
517
+ }
518
+ toCreate = __assign(__assign({}, data), { rule_id: discount.rule_id, is_dynamic: true, is_disabled: false, code: data.code.toUpperCase(), parent_discount_id: discount.id, usage_limit: discount.usage_limit });
519
+ if (discount.valid_duration) {
520
+ lastValidDate = new Date();
521
+ lastValidDate.setSeconds(lastValidDate.getSeconds() + (0, iso8601_duration_1.toSeconds)((0, iso8601_duration_1.parse)(discount.valid_duration)));
522
+ toCreate.ends_at = lastValidDate;
523
+ }
524
+ created = discountRepo.create(toCreate);
525
+ return [4 /*yield*/, discountRepo.save(created)];
526
+ case 2: return [2 /*return*/, _a.sent()];
527
+ }
528
+ });
529
+ }); })];
530
+ case 1: return [2 /*return*/, _a.sent()];
531
+ }
524
532
  });
525
533
  });
526
534
  };
@@ -534,27 +542,30 @@ var DiscountService = /** @class */ (function (_super) {
534
542
  return __awaiter(this, void 0, void 0, function () {
535
543
  var _this = this;
536
544
  return __generator(this, function (_a) {
537
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
538
- var discountRepo, discount;
539
- return __generator(this, function (_a) {
540
- switch (_a.label) {
541
- case 0:
542
- discountRepo = manager.getCustomRepository(this.discountRepository_);
543
- return [4 /*yield*/, discountRepo.findOne({
544
- where: { parent_discount_id: discountId, code: code },
545
- })];
546
- case 1:
547
- discount = _a.sent();
548
- if (!discount) {
549
- return [2 /*return*/, Promise.resolve()];
550
- }
551
- return [4 /*yield*/, discountRepo.softRemove(discount)];
552
- case 2:
553
- _a.sent();
554
- return [2 /*return*/, Promise.resolve()];
555
- }
556
- });
557
- }); })];
545
+ switch (_a.label) {
546
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
547
+ var discountRepo, discount;
548
+ return __generator(this, function (_a) {
549
+ switch (_a.label) {
550
+ case 0:
551
+ discountRepo = manager.getCustomRepository(this.discountRepository_);
552
+ return [4 /*yield*/, discountRepo.findOne({
553
+ where: { parent_discount_id: discountId, code: code },
554
+ })];
555
+ case 1:
556
+ discount = _a.sent();
557
+ if (!discount) {
558
+ return [2 /*return*/];
559
+ }
560
+ return [4 /*yield*/, discountRepo.softRemove(discount)];
561
+ case 2:
562
+ _a.sent();
563
+ return [2 /*return*/];
564
+ }
565
+ });
566
+ }); })];
567
+ case 1: return [2 /*return*/, _a.sent()];
568
+ }
558
569
  });
559
570
  });
560
571
  };
@@ -568,37 +579,38 @@ var DiscountService = /** @class */ (function (_super) {
568
579
  return __awaiter(this, void 0, void 0, function () {
569
580
  var _this = this;
570
581
  return __generator(this, function (_a) {
571
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
572
- var discountRepo, discount, exists, region, updated;
573
- var _a;
574
- return __generator(this, function (_b) {
575
- switch (_b.label) {
576
- case 0:
577
- discountRepo = manager.getCustomRepository(this.discountRepository_);
578
- return [4 /*yield*/, this.retrieve(discountId, {
579
- relations: ["regions", "rule"],
580
- })];
581
- case 1:
582
- discount = _b.sent();
583
- exists = discount.regions.find(function (r) { return r.id === regionId; });
584
- // If region is already present, we return early
585
- if (exists) {
586
- return [2 /*return*/, discount];
587
- }
588
- if (((_a = discount.regions) === null || _a === void 0 ? void 0 : _a.length) === 1 && discount.rule.type === "fixed") {
589
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Fixed discounts can have one region");
590
- }
591
- return [4 /*yield*/, this.regionService_.retrieve(regionId)];
592
- case 2:
593
- region = _b.sent();
594
- discount.regions = __spreadArray(__spreadArray([], __read(discount.regions), false), [region], false);
595
- return [4 /*yield*/, discountRepo.save(discount)];
596
- case 3:
597
- updated = _b.sent();
598
- return [2 /*return*/, updated];
599
- }
600
- });
601
- }); })];
582
+ switch (_a.label) {
583
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
584
+ var discountRepo, discount, exists, region;
585
+ var _a;
586
+ return __generator(this, function (_b) {
587
+ switch (_b.label) {
588
+ case 0:
589
+ discountRepo = manager.getCustomRepository(this.discountRepository_);
590
+ return [4 /*yield*/, this.retrieve(discountId, {
591
+ relations: ["regions", "rule"],
592
+ })];
593
+ case 1:
594
+ discount = _b.sent();
595
+ exists = discount.regions.find(function (r) { return r.id === regionId; });
596
+ // If region is already present, we return early
597
+ if (exists) {
598
+ return [2 /*return*/, discount];
599
+ }
600
+ if (((_a = discount.regions) === null || _a === void 0 ? void 0 : _a.length) === 1 && discount.rule.type === "fixed") {
601
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Fixed discounts can have one region");
602
+ }
603
+ return [4 /*yield*/, this.regionService_.retrieve(regionId)];
604
+ case 2:
605
+ region = _b.sent();
606
+ discount.regions = __spreadArray(__spreadArray([], __read(discount.regions), false), [region], false);
607
+ return [4 /*yield*/, discountRepo.save(discount)];
608
+ case 3: return [2 /*return*/, _b.sent()];
609
+ }
610
+ });
611
+ }); })];
612
+ case 1: return [2 /*return*/, _a.sent()];
613
+ }
602
614
  });
603
615
  });
604
616
  };
@@ -612,30 +624,31 @@ var DiscountService = /** @class */ (function (_super) {
612
624
  return __awaiter(this, void 0, void 0, function () {
613
625
  var _this = this;
614
626
  return __generator(this, function (_a) {
615
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
616
- var discountRepo, discount, exists, updated;
617
- return __generator(this, function (_a) {
618
- switch (_a.label) {
619
- case 0:
620
- discountRepo = manager.getCustomRepository(this.discountRepository_);
621
- return [4 /*yield*/, this.retrieve(discountId, {
622
- relations: ["regions"],
623
- })];
624
- case 1:
625
- discount = _a.sent();
626
- exists = discount.regions.find(function (r) { return r.id === regionId; });
627
- // If region is not present, we return early
628
- if (!exists) {
629
- return [2 /*return*/, discount];
630
- }
631
- discount.regions = discount.regions.filter(function (r) { return r.id !== regionId; });
632
- return [4 /*yield*/, discountRepo.save(discount)];
633
- case 2:
634
- updated = _a.sent();
635
- return [2 /*return*/, updated];
636
- }
637
- });
638
- }); })];
627
+ switch (_a.label) {
628
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
629
+ var discountRepo, discount, exists;
630
+ return __generator(this, function (_a) {
631
+ switch (_a.label) {
632
+ case 0:
633
+ discountRepo = manager.getCustomRepository(this.discountRepository_);
634
+ return [4 /*yield*/, this.retrieve(discountId, {
635
+ relations: ["regions"],
636
+ })];
637
+ case 1:
638
+ discount = _a.sent();
639
+ exists = discount.regions.find(function (r) { return r.id === regionId; });
640
+ // If region is not present, we return early
641
+ if (!exists) {
642
+ return [2 /*return*/, discount];
643
+ }
644
+ discount.regions = discount.regions.filter(function (r) { return r.id !== regionId; });
645
+ return [4 /*yield*/, discountRepo.save(discount)];
646
+ case 2: return [2 /*return*/, _a.sent()];
647
+ }
648
+ });
649
+ }); })];
650
+ case 1: return [2 /*return*/, _a.sent()];
651
+ }
639
652
  });
640
653
  });
641
654
  };
@@ -648,25 +661,28 @@ var DiscountService = /** @class */ (function (_super) {
648
661
  return __awaiter(this, void 0, void 0, function () {
649
662
  var _this = this;
650
663
  return __generator(this, function (_a) {
651
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
652
- var discountRepo, discount;
653
- return __generator(this, function (_a) {
654
- switch (_a.label) {
655
- case 0:
656
- discountRepo = manager.getCustomRepository(this.discountRepository_);
657
- return [4 /*yield*/, discountRepo.findOne({ where: { id: discountId } })];
658
- case 1:
659
- discount = _a.sent();
660
- if (!discount) {
661
- return [2 /*return*/, Promise.resolve()];
662
- }
663
- return [4 /*yield*/, discountRepo.softRemove(discount)];
664
- case 2:
665
- _a.sent();
666
- return [2 /*return*/, Promise.resolve()];
667
- }
668
- });
669
- }); })];
664
+ switch (_a.label) {
665
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
666
+ var discountRepo, discount;
667
+ return __generator(this, function (_a) {
668
+ switch (_a.label) {
669
+ case 0:
670
+ discountRepo = manager.getCustomRepository(this.discountRepository_);
671
+ return [4 /*yield*/, discountRepo.findOne({ where: { id: discountId } })];
672
+ case 1:
673
+ discount = _a.sent();
674
+ if (!discount) {
675
+ return [2 /*return*/];
676
+ }
677
+ return [4 /*yield*/, discountRepo.softRemove(discount)];
678
+ case 2:
679
+ _a.sent();
680
+ return [2 /*return*/];
681
+ }
682
+ });
683
+ }); })];
684
+ case 1: return [2 /*return*/, _a.sent()];
685
+ }
670
686
  });
671
687
  });
672
688
  };
@@ -674,101 +690,120 @@ var DiscountService = /** @class */ (function (_super) {
674
690
  return __awaiter(this, void 0, void 0, function () {
675
691
  var _this = this;
676
692
  return __generator(this, function (_a) {
677
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
678
- var discountConditionRepo, product;
679
- return __generator(this, function (_a) {
680
- switch (_a.label) {
681
- case 0:
682
- discountConditionRepo = manager.getCustomRepository(this.discountConditionRepository_);
683
- // In case of custom line items, we don't have a product id.
684
- // Instead of throwing, we simply invalidate the discount.
685
- if (!productId) {
686
- return [2 /*return*/, false];
687
- }
688
- return [4 /*yield*/, this.productService_.retrieve(productId, {
689
- relations: ["tags"],
690
- })];
691
- case 1:
692
- product = _a.sent();
693
- return [4 /*yield*/, discountConditionRepo.isValidForProduct(discountRuleId, product.id)];
694
- case 2: return [2 /*return*/, _a.sent()];
695
- }
696
- });
697
- }); })];
693
+ switch (_a.label) {
694
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
695
+ var discountConditionRepo, product;
696
+ return __generator(this, function (_a) {
697
+ switch (_a.label) {
698
+ case 0:
699
+ discountConditionRepo = manager.getCustomRepository(this.discountConditionRepository_);
700
+ // In case of custom line items, we don't have a product id.
701
+ // Instead of throwing, we simply invalidate the discount.
702
+ if (!productId) {
703
+ return [2 /*return*/, false];
704
+ }
705
+ return [4 /*yield*/, this.productService_.retrieve(productId, {
706
+ relations: ["tags"],
707
+ })];
708
+ case 1:
709
+ product = _a.sent();
710
+ return [4 /*yield*/, discountConditionRepo.isValidForProduct(discountRuleId, product.id)];
711
+ case 2: return [2 /*return*/, _a.sent()];
712
+ }
713
+ });
714
+ }); })];
715
+ case 1: return [2 /*return*/, _a.sent()];
716
+ }
698
717
  });
699
718
  });
700
719
  };
701
720
  DiscountService.prototype.calculateDiscountForLineItem = function (discountId, lineItem, cart) {
702
721
  return __awaiter(this, void 0, void 0, function () {
703
- var adjustment, discount, _a, type, value, allocation, fullItemPrice, subtotal, nominator, itemRelativeToSubtotal, totalItemPercentage;
704
- return __generator(this, function (_b) {
705
- switch (_b.label) {
706
- case 0:
707
- adjustment = 0;
708
- if (!lineItem.allow_discounts) {
709
- return [2 /*return*/, adjustment];
710
- }
711
- return [4 /*yield*/, this.retrieve(discountId, { relations: ["rule"] })];
712
- case 1:
713
- discount = _b.sent();
714
- _a = discount.rule, type = _a.type, value = _a.value, allocation = _a.allocation;
715
- fullItemPrice = lineItem.unit_price * lineItem.quantity;
716
- if (type === discount_rule_1.DiscountRuleType.PERCENTAGE) {
717
- adjustment = Math.round((fullItemPrice / 100) * value);
718
- }
719
- else if (type === discount_rule_1.DiscountRuleType.FIXED &&
720
- allocation === discount_rule_1.AllocationType.TOTAL) {
721
- subtotal = this.totalsService_.getSubtotal(cart, {
722
- excludeNonDiscounts: true,
722
+ var _this = this;
723
+ return __generator(this, function (_a) {
724
+ switch (_a.label) {
725
+ case 0: return [4 /*yield*/, this.atomicPhase_(function () { return __awaiter(_this, void 0, void 0, function () {
726
+ var adjustment, discount, _a, type, value, allocation, fullItemPrice, subtotal, nominator, itemRelativeToSubtotal, totalItemPercentage;
727
+ return __generator(this, function (_b) {
728
+ switch (_b.label) {
729
+ case 0:
730
+ adjustment = 0;
731
+ if (!lineItem.allow_discounts) {
732
+ return [2 /*return*/, adjustment];
733
+ }
734
+ return [4 /*yield*/, this.retrieve(discountId, { relations: ["rule"] })];
735
+ case 1:
736
+ discount = _b.sent();
737
+ _a = discount.rule, type = _a.type, value = _a.value, allocation = _a.allocation;
738
+ fullItemPrice = lineItem.unit_price * lineItem.quantity;
739
+ if (type === discount_rule_1.DiscountRuleType.PERCENTAGE) {
740
+ adjustment = Math.round((fullItemPrice / 100) * value);
741
+ }
742
+ else if (type === discount_rule_1.DiscountRuleType.FIXED &&
743
+ allocation === discount_rule_1.AllocationType.TOTAL) {
744
+ subtotal = this.totalsService_.getSubtotal(cart, {
745
+ excludeNonDiscounts: true,
746
+ });
747
+ nominator = Math.min(value, subtotal);
748
+ itemRelativeToSubtotal = lineItem.unit_price / subtotal;
749
+ totalItemPercentage = itemRelativeToSubtotal * lineItem.quantity;
750
+ adjustment = Math.round(nominator * totalItemPercentage);
751
+ }
752
+ else {
753
+ adjustment = value * lineItem.quantity;
754
+ }
755
+ // if the amount of the discount exceeds the total price of the item,
756
+ // we return the total item price, else the fixed amount
757
+ return [2 /*return*/, adjustment >= fullItemPrice ? fullItemPrice : adjustment];
758
+ }
723
759
  });
724
- nominator = Math.min(value, subtotal);
725
- itemRelativeToSubtotal = lineItem.unit_price / subtotal;
726
- totalItemPercentage = itemRelativeToSubtotal * lineItem.quantity;
727
- adjustment = Math.round(nominator * totalItemPercentage);
728
- }
729
- else {
730
- adjustment = value * lineItem.quantity;
731
- }
732
- // if the amount of the discount exceeds the total price of the item,
733
- // we return the total item price, else the fixed amount
734
- return [2 /*return*/, adjustment >= fullItemPrice ? fullItemPrice : adjustment];
760
+ }); })];
761
+ case 1: return [2 /*return*/, _a.sent()];
735
762
  }
736
763
  });
737
764
  });
738
765
  };
739
766
  DiscountService.prototype.validateDiscountForCartOrThrow = function (cart, discount) {
740
767
  return __awaiter(this, void 0, void 0, function () {
741
- var isValidForRegion, canApplyForCustomer;
768
+ var _this = this;
742
769
  return __generator(this, function (_a) {
743
770
  switch (_a.label) {
744
- case 0:
745
- if (this.hasReachedLimit(discount)) {
746
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Discount has been used maximum allowed times");
747
- }
748
- if (this.hasNotStarted(discount)) {
749
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Discount is not valid yet");
750
- }
751
- if (this.hasExpired(discount)) {
752
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Discount is expired");
753
- }
754
- if (this.isDisabled(discount)) {
755
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "The discount code is disabled");
756
- }
757
- return [4 /*yield*/, this.isValidForRegion(discount, cart.region_id)];
758
- case 1:
759
- isValidForRegion = _a.sent();
760
- if (!isValidForRegion) {
761
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "The discount is not available in current region");
762
- }
763
- if (!cart.customer_id) return [3 /*break*/, 3];
764
- return [4 /*yield*/, this.canApplyForCustomer(discount.rule.id, cart.customer_id)];
765
- case 2:
766
- canApplyForCustomer = _a.sent();
767
- if (!canApplyForCustomer) {
768
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Discount is not valid for customer");
769
- }
770
- _a.label = 3;
771
- case 3: return [2 /*return*/];
771
+ case 0: return [4 /*yield*/, this.atomicPhase_(function () { return __awaiter(_this, void 0, void 0, function () {
772
+ var isValidForRegion, canApplyForCustomer;
773
+ return __generator(this, function (_a) {
774
+ switch (_a.label) {
775
+ case 0:
776
+ if (this.hasReachedLimit(discount)) {
777
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Discount has been used maximum allowed times");
778
+ }
779
+ if (this.hasNotStarted(discount)) {
780
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Discount is not valid yet");
781
+ }
782
+ if (this.hasExpired(discount)) {
783
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Discount is expired");
784
+ }
785
+ if (this.isDisabled(discount)) {
786
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "The discount code is disabled");
787
+ }
788
+ return [4 /*yield*/, this.isValidForRegion(discount, cart.region_id)];
789
+ case 1:
790
+ isValidForRegion = _a.sent();
791
+ if (!isValidForRegion) {
792
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "The discount is not available in current region");
793
+ }
794
+ if (!cart.customer_id) return [3 /*break*/, 3];
795
+ return [4 /*yield*/, this.canApplyForCustomer(discount.rule.id, cart.customer_id)];
796
+ case 2:
797
+ canApplyForCustomer = _a.sent();
798
+ if (!canApplyForCustomer) {
799
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Discount is not valid for customer");
800
+ }
801
+ _a.label = 3;
802
+ case 3: return [2 /*return*/];
803
+ }
804
+ });
805
+ }); })];
806
+ case 1: return [2 /*return*/, _a.sent()];
772
807
  }
773
808
  });
774
809
  });
@@ -792,23 +827,31 @@ var DiscountService = /** @class */ (function (_super) {
792
827
  };
793
828
  DiscountService.prototype.isValidForRegion = function (discount, region_id) {
794
829
  return __awaiter(this, void 0, void 0, function () {
795
- var regions, parent;
830
+ var _this = this;
796
831
  return __generator(this, function (_a) {
797
832
  switch (_a.label) {
798
- case 0:
799
- regions = discount.regions;
800
- if (!discount.parent_discount_id) return [3 /*break*/, 2];
801
- return [4 /*yield*/, this.retrieve(discount.parent_discount_id, {
802
- relations: ["rule", "regions"],
803
- })];
804
- case 1:
805
- parent = _a.sent();
806
- regions = parent.regions;
807
- _a.label = 2;
808
- case 2: return [2 /*return*/, regions.find(function (_a) {
809
- var id = _a.id;
810
- return id === region_id;
811
- }) !== undefined];
833
+ case 0: return [4 /*yield*/, this.atomicPhase_(function () { return __awaiter(_this, void 0, void 0, function () {
834
+ var regions, parent;
835
+ return __generator(this, function (_a) {
836
+ switch (_a.label) {
837
+ case 0:
838
+ regions = discount.regions;
839
+ if (!discount.parent_discount_id) return [3 /*break*/, 2];
840
+ return [4 /*yield*/, this.retrieve(discount.parent_discount_id, {
841
+ relations: ["rule", "regions"],
842
+ })];
843
+ case 1:
844
+ parent = _a.sent();
845
+ regions = parent.regions;
846
+ _a.label = 2;
847
+ case 2: return [2 /*return*/, regions.find(function (_a) {
848
+ var id = _a.id;
849
+ return id === region_id;
850
+ }) !== undefined];
851
+ }
852
+ });
853
+ }); })];
854
+ case 1: return [2 /*return*/, _a.sent()];
812
855
  }
813
856
  });
814
857
  });
@@ -817,30 +860,33 @@ var DiscountService = /** @class */ (function (_super) {
817
860
  return __awaiter(this, void 0, void 0, function () {
818
861
  var _this = this;
819
862
  return __generator(this, function (_a) {
820
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
821
- var discountConditionRepo, customer;
822
- return __generator(this, function (_a) {
823
- switch (_a.label) {
824
- case 0:
825
- discountConditionRepo = manager.getCustomRepository(this.discountConditionRepository_);
826
- // Instead of throwing on missing customer id, we simply invalidate the discount
827
- if (!customerId) {
828
- return [2 /*return*/, false];
829
- }
830
- return [4 /*yield*/, this.customerService_.retrieve(customerId, {
831
- relations: ["groups"],
832
- })];
833
- case 1:
834
- customer = _a.sent();
835
- return [4 /*yield*/, discountConditionRepo.canApplyForCustomer(discountRuleId, customer.id)];
836
- case 2: return [2 /*return*/, _a.sent()];
837
- }
838
- });
839
- }); })];
863
+ switch (_a.label) {
864
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
865
+ var discountConditionRepo, customer;
866
+ return __generator(this, function (_a) {
867
+ switch (_a.label) {
868
+ case 0:
869
+ discountConditionRepo = manager.getCustomRepository(this.discountConditionRepository_);
870
+ // Instead of throwing on missing customer id, we simply invalidate the discount
871
+ if (!customerId) {
872
+ return [2 /*return*/, false];
873
+ }
874
+ return [4 /*yield*/, this.customerService_.retrieve(customerId, {
875
+ relations: ["groups"],
876
+ })];
877
+ case 1:
878
+ customer = _a.sent();
879
+ return [4 /*yield*/, discountConditionRepo.canApplyForCustomer(discountRuleId, customer.id)];
880
+ case 2: return [2 /*return*/, _a.sent()];
881
+ }
882
+ });
883
+ }); })];
884
+ case 1: return [2 /*return*/, _a.sent()];
885
+ }
840
886
  });
841
887
  });
842
888
  };
843
889
  return DiscountService;
844
- }(medusa_interfaces_1.BaseService));
890
+ }(interfaces_1.TransactionBaseService));
845
891
  exports.default = DiscountService;
846
892
  //# sourceMappingURL=discount.js.map