@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
@@ -110,78 +110,46 @@ 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
- var typeorm_1 = require("typeorm");
113
+ var interfaces_1 = require("../interfaces");
114
+ var utils_1 = require("../utils");
115
115
  var exception_formatter_1 = require("../utils/exception-formatter");
116
- var products_1 = require("../api/routes/admin/products");
117
- /**
118
- * Provides layer to manipulate products.
119
- * @extends BaseService
120
- */
121
116
  var ProductService = /** @class */ (function (_super) {
122
117
  __extends(ProductService, _super);
123
118
  function ProductService(_a) {
124
- var manager = _a.manager, productRepository = _a.productRepository, productVariantRepository = _a.productVariantRepository, productOptionRepository = _a.productOptionRepository, eventBusService = _a.eventBusService, productVariantService = _a.productVariantService, productCollectionService = _a.productCollectionService, productTypeRepository = _a.productTypeRepository, productTagRepository = _a.productTagRepository, imageRepository = _a.imageRepository, searchService = _a.searchService, cartRepository = _a.cartRepository, priceSelectionStrategy = _a.priceSelectionStrategy;
125
- var _this = _super.call(this) || this;
126
- /** @private @const {EntityManager} */
119
+ var manager = _a.manager, productRepository = _a.productRepository, productVariantRepository = _a.productVariantRepository, productOptionRepository = _a.productOptionRepository, eventBusService = _a.eventBusService, productVariantService = _a.productVariantService, productTypeRepository = _a.productTypeRepository, productTagRepository = _a.productTagRepository, imageRepository = _a.imageRepository, searchService = _a.searchService;
120
+ var _this = _super.call(this, {
121
+ manager: manager,
122
+ productRepository: productRepository,
123
+ productVariantRepository: productVariantRepository,
124
+ productOptionRepository: productOptionRepository,
125
+ eventBusService: eventBusService,
126
+ productVariantService: productVariantService,
127
+ productTypeRepository: productTypeRepository,
128
+ productTagRepository: productTagRepository,
129
+ imageRepository: imageRepository,
130
+ searchService: searchService,
131
+ }) || this;
127
132
  _this.manager_ = manager;
128
- /** @private @const {ProductOption} */
129
133
  _this.productOptionRepository_ = productOptionRepository;
130
- /** @private @const {Product} */
131
134
  _this.productRepository_ = productRepository;
132
- /** @private @const {ProductVariant} */
133
135
  _this.productVariantRepository_ = productVariantRepository;
134
- /** @private @const {EventBus} */
135
136
  _this.eventBus_ = eventBusService;
136
- /** @private @const {ProductVariantService} */
137
137
  _this.productVariantService_ = productVariantService;
138
- /** @private @const {ProductCollectionService} */
139
- _this.productCollectionService_ = productCollectionService;
140
- /** @private @const {ProductCollectionService} */
141
138
  _this.productTypeRepository_ = productTypeRepository;
142
- /** @private @const {ProductCollectionService} */
143
139
  _this.productTagRepository_ = productTagRepository;
144
- /** @private @const {ImageRepository} */
145
140
  _this.imageRepository_ = imageRepository;
146
- /** @private @const {SearchService} */
147
141
  _this.searchService_ = searchService;
148
- /** @private @const {CartRepository} */
149
- _this.cartRepository_ = cartRepository;
150
- /** @private @const {IPriceSelectionStrategy} */
151
- _this.priceSelectionStrategy_ = priceSelectionStrategy;
152
142
  return _this;
153
143
  }
154
- ProductService.prototype.withTransaction = function (transactionManager) {
155
- if (!transactionManager) {
156
- return this;
157
- }
158
- var cloned = new ProductService({
159
- manager: transactionManager,
160
- productRepository: this.productRepository_,
161
- productVariantRepository: this.productVariantRepository_,
162
- productOptionRepository: this.productOptionRepository_,
163
- eventBusService: this.eventBus_,
164
- productVariantService: this.productVariantService_,
165
- productCollectionService: this.productCollectionService_,
166
- productTagRepository: this.productTagRepository_,
167
- productTypeRepository: this.productTypeRepository_,
168
- imageRepository: this.imageRepository_,
169
- cartRepository: this.cartRepository_,
170
- priceSelectionStrategy: this.priceSelectionStrategy_,
171
- });
172
- cloned.transactionManager_ = transactionManager;
173
- return cloned;
174
- };
175
144
  /**
176
145
  * Lists products based on the provided parameters.
177
- * @param {object} selector - an object that defines rules to filter products
146
+ * @param selector - an object that defines rules to filter products
178
147
  * by
179
- * @param {object} config - object that defines the scope for what should be
148
+ * @param config - object that defines the scope for what should be
180
149
  * returned
181
- * @return {Promise<Product[]>} the result of the find operation
150
+ * @return the result of the find operation
182
151
  */
183
152
  ProductService.prototype.list = function (selector, config) {
184
- var _a, _b, _c;
185
153
  if (selector === void 0) { selector = {}; }
186
154
  if (config === void 0) { config = {
187
155
  relations: [],
@@ -190,34 +158,27 @@ var ProductService = /** @class */ (function (_super) {
190
158
  include_discount_prices: false,
191
159
  }; }
192
160
  return __awaiter(this, void 0, void 0, function () {
193
- var productRepo, priceIndex, _d, q, query, relations, qb, raw, products, _e;
194
- return __generator(this, function (_f) {
195
- switch (_f.label) {
196
- case 0:
197
- productRepo = this.manager_.getCustomRepository(this.productRepository_);
198
- priceIndex = (_b = (_a = config.relations) === null || _a === void 0 ? void 0 : _a.indexOf("variants.prices")) !== null && _b !== void 0 ? _b : -1;
199
- if (priceIndex >= 0 && config.relations) {
200
- config.relations = __spreadArray([], __read(config.relations), false);
201
- config.relations.splice(priceIndex, 1);
202
- }
203
- _d = this.prepareListQuery_(selector, config), q = _d.q, query = _d.query, relations = _d.relations;
204
- if (!q) return [3 /*break*/, 2];
205
- qb = this.getFreeTextQueryBuilder_(productRepo, query, q);
206
- return [4 /*yield*/, qb.getMany()];
207
- case 1:
208
- raw = _f.sent();
209
- return [2 /*return*/, productRepo.findWithRelations(relations, raw.map(function (i) { return i.id; }), (_c = query.withDeleted) !== null && _c !== void 0 ? _c : false)];
210
- case 2:
211
- products = productRepo.findWithRelations(relations, query);
212
- if (!(priceIndex > -1)) return [3 /*break*/, 4];
213
- return [4 /*yield*/, this.setAdditionalPrices(products, config.currency_code, config.region_id, config.cart_id, config.customer_id, config.include_discount_prices)];
214
- case 3:
215
- _e = _f.sent();
216
- return [3 /*break*/, 5];
217
- case 4:
218
- _e = products;
219
- _f.label = 5;
220
- case 5: return [2 /*return*/, _e];
161
+ var _this = this;
162
+ return __generator(this, function (_a) {
163
+ switch (_a.label) {
164
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
165
+ var productRepo, _a, q, query, relations, _b, products;
166
+ return __generator(this, function (_c) {
167
+ switch (_c.label) {
168
+ case 0:
169
+ productRepo = manager.getCustomRepository(this.productRepository_);
170
+ _a = this.prepareListQuery_(selector, config), q = _a.q, query = _a.query, relations = _a.relations;
171
+ if (!q) return [3 /*break*/, 2];
172
+ return [4 /*yield*/, productRepo.getFreeTextSearchResultsAndCount(q, query, relations)];
173
+ case 1:
174
+ _b = __read.apply(void 0, [_c.sent(), 1]), products = _b[0];
175
+ return [2 /*return*/, products];
176
+ case 2: return [4 /*yield*/, productRepo.findWithRelations(relations, query)];
177
+ case 3: return [2 /*return*/, _c.sent()];
178
+ }
179
+ });
180
+ }); })];
181
+ case 1: return [2 /*return*/, _a.sent()];
221
182
  }
222
183
  });
223
184
  });
@@ -225,17 +186,15 @@ var ProductService = /** @class */ (function (_super) {
225
186
  /**
226
187
  * Lists products based on the provided parameters and includes the count of
227
188
  * products that match the query.
228
- * @param {object} selector - an object that defines rules to filter products
189
+ * @param selector - an object that defines rules to filter products
229
190
  * by
230
- * @param {object} config - object that defines the scope for what should be
191
+ * @param config - object that defines the scope for what should be
231
192
  * returned
232
- * @return {Promise<[Product[], number]>} an array containing the products as
193
+ * @return an array containing the products as
233
194
  * the first element and the total count of products that matches the query
234
195
  * as the second element.
235
196
  */
236
197
  ProductService.prototype.listAndCount = function (selector, config) {
237
- var _a, _b, _c;
238
- if (selector === void 0) { selector = {}; }
239
198
  if (config === void 0) { config = {
240
199
  relations: [],
241
200
  skip: 0,
@@ -243,35 +202,25 @@ var ProductService = /** @class */ (function (_super) {
243
202
  include_discount_prices: false,
244
203
  }; }
245
204
  return __awaiter(this, void 0, void 0, function () {
246
- var productRepo, priceIndex, _d, q, query, relations, qb, _e, raw, count_1, products_2, _f, products, count, productsWithAdditionalPrices;
247
- return __generator(this, function (_g) {
248
- switch (_g.label) {
249
- case 0:
250
- productRepo = this.manager_.getCustomRepository(this.productRepository_);
251
- priceIndex = (_b = (_a = config.relations) === null || _a === void 0 ? void 0 : _a.indexOf("variants.prices")) !== null && _b !== void 0 ? _b : -1;
252
- if (priceIndex >= 0 && config.relations) {
253
- config.relations = __spreadArray([], __read(config.relations), false);
254
- config.relations.splice(priceIndex, 1);
255
- }
256
- _d = this.prepareListQuery_(selector, config), q = _d.q, query = _d.query, relations = _d.relations;
257
- if (!q) return [3 /*break*/, 3];
258
- qb = this.getFreeTextQueryBuilder_(productRepo, query, q);
259
- return [4 /*yield*/, qb.getManyAndCount()];
260
- case 1:
261
- _e = __read.apply(void 0, [_g.sent(), 2]), raw = _e[0], count_1 = _e[1];
262
- return [4 /*yield*/, productRepo.findWithRelations(relations, raw.map(function (i) { return i.id; }), (_c = query.withDeleted) !== null && _c !== void 0 ? _c : false)];
263
- case 2:
264
- products_2 = _g.sent();
265
- return [2 /*return*/, [products_2, count_1]];
266
- case 3: return [4 /*yield*/, productRepo.findWithRelationsAndCount(relations, query)];
267
- case 4:
268
- _f = __read.apply(void 0, [_g.sent(), 2]), products = _f[0], count = _f[1];
269
- if (!(priceIndex > -1)) return [3 /*break*/, 6];
270
- return [4 /*yield*/, this.setAdditionalPrices(products, config.currency_code, config.region_id, config.cart_id, config.customer_id, config.include_discount_prices)];
271
- case 5:
272
- productsWithAdditionalPrices = _g.sent();
273
- return [2 /*return*/, [productsWithAdditionalPrices, count]];
274
- case 6: return [2 /*return*/, [products, count]];
205
+ var _this = this;
206
+ return __generator(this, function (_a) {
207
+ switch (_a.label) {
208
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
209
+ var productRepo, _a, q, query, relations;
210
+ return __generator(this, function (_b) {
211
+ switch (_b.label) {
212
+ case 0:
213
+ productRepo = manager.getCustomRepository(this.productRepository_);
214
+ _a = this.prepareListQuery_(selector, config), q = _a.q, query = _a.query, relations = _a.relations;
215
+ if (!q) return [3 /*break*/, 2];
216
+ return [4 /*yield*/, productRepo.getFreeTextSearchResultsAndCount(q, query, relations)];
217
+ case 1: return [2 /*return*/, _b.sent()];
218
+ case 2: return [4 /*yield*/, productRepo.findWithRelationsAndCount(relations, query)];
219
+ case 3: return [2 /*return*/, _b.sent()];
220
+ }
221
+ });
222
+ }); })];
223
+ case 1: return [2 /*return*/, _a.sent()];
275
224
  }
276
225
  });
277
226
  });
@@ -283,57 +232,52 @@ var ProductService = /** @class */ (function (_super) {
283
232
  */
284
233
  ProductService.prototype.count = function (selector) {
285
234
  if (selector === void 0) { selector = {}; }
286
- var productRepo = this.manager_.getCustomRepository(this.productRepository_);
287
- var query = this.buildQuery_(selector);
288
- return productRepo.count(query);
235
+ return __awaiter(this, void 0, void 0, function () {
236
+ var _this = this;
237
+ return __generator(this, function (_a) {
238
+ switch (_a.label) {
239
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
240
+ var productRepo, query;
241
+ return __generator(this, function (_a) {
242
+ switch (_a.label) {
243
+ case 0:
244
+ productRepo = manager.getCustomRepository(this.productRepository_);
245
+ query = (0, utils_1.buildQuery)(selector);
246
+ return [4 /*yield*/, productRepo.count(query)];
247
+ case 1: return [2 /*return*/, _a.sent()];
248
+ }
249
+ });
250
+ }); })];
251
+ case 1: return [2 /*return*/, _a.sent()];
252
+ }
253
+ });
254
+ });
289
255
  };
290
256
  /**
291
257
  * Gets a product by id.
292
258
  * Throws in case of DB Error and if product was not found.
293
- * @param {string} productId - id of the product to get.
294
- * @param {object} config - object that defines what should be included in the
259
+ * @param productId - id of the product to get.
260
+ * @param config - object that defines what should be included in the
295
261
  * query response
296
- * @return {Promise<Product>} the result of the find one operation.
262
+ * @return the result of the find one operation.
297
263
  */
298
264
  ProductService.prototype.retrieve = function (productId, config) {
299
- var _a, _b;
300
- if (config === void 0) { config = { include_discount_prices: false }; }
265
+ if (config === void 0) { config = {
266
+ include_discount_prices: false,
267
+ }; }
301
268
  return __awaiter(this, void 0, void 0, function () {
302
- var productRepo, validatedId, priceIndex, query, rels, product, _c;
303
- return __generator(this, function (_d) {
304
- switch (_d.label) {
305
- case 0:
306
- productRepo = this.manager_.getCustomRepository(this.productRepository_);
307
- validatedId = this.validateId_(productId);
308
- priceIndex = (_b = (_a = config.relations) === null || _a === void 0 ? void 0 : _a.indexOf("variants.prices")) !== null && _b !== void 0 ? _b : -1;
309
- if (priceIndex >= 0 && config.relations) {
310
- config.relations = __spreadArray([], __read(config.relations), false);
311
- config.relations.splice(priceIndex, 1);
312
- }
313
- query = { where: { id: validatedId } };
314
- if (config.relations && config.relations.length > 0) {
315
- query.relations = config.relations;
316
- }
317
- if (config.select && config.select.length > 0) {
318
- query.select = config.select;
319
- }
320
- rels = query.relations;
321
- delete query.relations;
322
- return [4 /*yield*/, productRepo.findOneWithRelations(rels, query)];
323
- case 1:
324
- product = _d.sent();
325
- if (!product) {
326
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Product with id: ".concat(productId, " was not found"));
327
- }
328
- if (!(priceIndex > -1)) return [3 /*break*/, 3];
329
- return [4 /*yield*/, this.setAdditionalPrices(product, config.currency_code, config.region_id, config.cart_id, config.customer_id, config.include_discount_prices)];
330
- case 2:
331
- _c = _d.sent();
332
- return [3 /*break*/, 4];
333
- case 3:
334
- _c = product;
335
- _d.label = 4;
336
- case 4: return [2 /*return*/, _c];
269
+ var _this = this;
270
+ return __generator(this, function (_a) {
271
+ switch (_a.label) {
272
+ case 0: return [4 /*yield*/, this.atomicPhase_(function () { return __awaiter(_this, void 0, void 0, function () {
273
+ return __generator(this, function (_a) {
274
+ switch (_a.label) {
275
+ case 0: return [4 /*yield*/, this.retrieve_({ id: productId }, config)];
276
+ case 1: return [2 /*return*/, _a.sent()];
277
+ }
278
+ });
279
+ }); })];
280
+ case 1: return [2 /*return*/, _a.sent()];
337
281
  }
338
282
  });
339
283
  });
@@ -341,48 +285,25 @@ var ProductService = /** @class */ (function (_super) {
341
285
  /**
342
286
  * Gets a product by handle.
343
287
  * Throws in case of DB Error and if product was not found.
344
- * @param {string} productHandle - handle of the product to get.
345
- * @param {object} config - details about what to get from the product
346
- * @return {Promise<Product>} the result of the find one operation.
288
+ * @param productHandle - handle of the product to get.
289
+ * @param config - details about what to get from the product
290
+ * @return the result of the find one operation.
347
291
  */
348
292
  ProductService.prototype.retrieveByHandle = function (productHandle, config) {
349
- var _a, _b;
350
293
  if (config === void 0) { config = {}; }
351
294
  return __awaiter(this, void 0, void 0, function () {
352
- var productRepo, priceIndex, query, rels, product, _c;
353
- return __generator(this, function (_d) {
354
- switch (_d.label) {
355
- case 0:
356
- productRepo = this.manager_.getCustomRepository(this.productRepository_);
357
- priceIndex = (_b = (_a = config.relations) === null || _a === void 0 ? void 0 : _a.indexOf("variants.prices")) !== null && _b !== void 0 ? _b : -1;
358
- if (priceIndex >= 0 && config.relations) {
359
- config.relations = __spreadArray([], __read(config.relations), false);
360
- config.relations.splice(priceIndex, 1);
361
- }
362
- query = { where: { handle: productHandle } };
363
- if (config.relations && config.relations.length > 0) {
364
- query.relations = config.relations;
365
- }
366
- if (config.select && config.select.length > 0) {
367
- query.select = config.select;
368
- }
369
- rels = query.relations;
370
- delete query.relations;
371
- return [4 /*yield*/, productRepo.findOneWithRelations(rels, query)];
372
- case 1:
373
- product = _d.sent();
374
- if (!product) {
375
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Product with handle: ".concat(productHandle, " was not found"));
376
- }
377
- if (!(priceIndex > -1)) return [3 /*break*/, 3];
378
- return [4 /*yield*/, this.setAdditionalPrices(product, config.currency_code, config.region_id, config.cart_id, config.customer_id, config.include_discount_prices)];
379
- case 2:
380
- _c = _d.sent();
381
- return [3 /*break*/, 4];
382
- case 3:
383
- _c = product;
384
- _d.label = 4;
385
- case 4: return [2 /*return*/, _c];
295
+ var _this = this;
296
+ return __generator(this, function (_a) {
297
+ switch (_a.label) {
298
+ case 0: return [4 /*yield*/, this.atomicPhase_(function () { return __awaiter(_this, void 0, void 0, function () {
299
+ return __generator(this, function (_a) {
300
+ switch (_a.label) {
301
+ case 0: return [4 /*yield*/, this.retrieve_({ handle: productHandle }, config)];
302
+ case 1: return [2 /*return*/, _a.sent()];
303
+ }
304
+ });
305
+ }); })];
306
+ case 1: return [2 /*return*/, _a.sent()];
386
307
  }
387
308
  });
388
309
  });
@@ -390,319 +311,246 @@ var ProductService = /** @class */ (function (_super) {
390
311
  /**
391
312
  * Gets a product by external id.
392
313
  * Throws in case of DB Error and if product was not found.
393
- * @param {string} externalId - handle of the product to get.
394
- * @param {object} config - details about what to get from the product
395
- * @return {Promise<Product>} the result of the find one operation.
314
+ * @param externalId - handle of the product to get.
315
+ * @param config - details about what to get from the product
316
+ * @return the result of the find one operation.
396
317
  */
397
318
  ProductService.prototype.retrieveByExternalId = function (externalId, config) {
398
- var _a, _b;
399
319
  if (config === void 0) { config = {}; }
400
320
  return __awaiter(this, void 0, void 0, function () {
401
- var productRepo, priceIndex, query, rels, product, _c;
402
- return __generator(this, function (_d) {
403
- switch (_d.label) {
404
- case 0:
405
- productRepo = this.manager_.getCustomRepository(this.productRepository_);
406
- priceIndex = (_b = (_a = config.relations) === null || _a === void 0 ? void 0 : _a.indexOf("variants.prices")) !== null && _b !== void 0 ? _b : -1;
407
- if (priceIndex >= 0 && config.relations) {
408
- config.relations = __spreadArray([], __read(config.relations), false);
409
- config.relations.splice(priceIndex, 1);
410
- }
411
- query = { where: { external_id: externalId } };
412
- if (config.relations && config.relations.length > 0) {
413
- query.relations = config.relations;
414
- }
415
- if (config.select && config.select.length > 0) {
416
- query.select = config.select;
417
- }
418
- rels = query.relations;
419
- delete query.relations;
420
- return [4 /*yield*/, productRepo.findOneWithRelations(rels, query)];
421
- case 1:
422
- product = _d.sent();
423
- if (!product) {
424
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Product with exteral_id: ".concat(externalId, " was not found"));
425
- }
426
- if (!(priceIndex > -1)) return [3 /*break*/, 3];
427
- return [4 /*yield*/, this.setAdditionalPrices(product, config.currency_code, config.region_id, config.cart_id, config.customer_id, config.include_discount_prices)];
428
- case 2:
429
- _c = _d.sent();
430
- return [3 /*break*/, 4];
431
- case 3:
432
- _c = product;
433
- _d.label = 4;
434
- case 4: return [2 /*return*/, _c];
321
+ var _this = this;
322
+ return __generator(this, function (_a) {
323
+ switch (_a.label) {
324
+ case 0: return [4 /*yield*/, this.atomicPhase_(function () { return __awaiter(_this, void 0, void 0, function () {
325
+ return __generator(this, function (_a) {
326
+ switch (_a.label) {
327
+ case 0: return [4 /*yield*/, this.retrieve_({ external_id: externalId }, config)];
328
+ case 1: return [2 /*return*/, _a.sent()];
329
+ }
330
+ });
331
+ }); })];
332
+ case 1: return [2 /*return*/, _a.sent()];
435
333
  }
436
334
  });
437
335
  });
438
336
  };
439
337
  /**
440
- * Gets all variants belonging to a product.
441
- * @param {string} productId - the id of the product to get variants from.
442
- * @param {FindConfig<Product>} config - The config to select and configure relations etc...
443
- * @return {Promise} an array of variants
338
+ * Gets a product by selector.
339
+ * Throws in case of DB Error and if product was not found.
340
+ * @param selector - selector object
341
+ * @param config - object that defines what should be included in the
342
+ * query response
343
+ * @return the result of the find one operation.
444
344
  */
445
- ProductService.prototype.retrieveVariants = function (productId, config) {
345
+ ProductService.prototype.retrieve_ = function (selector, config) {
446
346
  if (config === void 0) { config = {
447
- skip: 0,
448
- take: 50,
449
- relations: products_1.defaultAdminProductsVariantsRelations,
347
+ include_discount_prices: false,
450
348
  }; }
451
349
  return __awaiter(this, void 0, void 0, function () {
452
- var product;
350
+ var _this = this;
453
351
  return __generator(this, function (_a) {
454
352
  switch (_a.label) {
455
- case 0: return [4 /*yield*/, this.retrieve(productId, config)];
456
- case 1:
457
- product = _a.sent();
458
- return [2 /*return*/, product.variants];
353
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
354
+ var productRepo, _a, relations, query, product, selectorConstraints;
355
+ return __generator(this, function (_b) {
356
+ switch (_b.label) {
357
+ case 0:
358
+ productRepo = manager.getCustomRepository(this.productRepository_);
359
+ _a = (0, utils_1.buildQuery)(selector, config), relations = _a.relations, query = __rest(_a, ["relations"]);
360
+ return [4 /*yield*/, productRepo.findOneWithRelations(relations, query)];
361
+ case 1:
362
+ product = _b.sent();
363
+ if (!product) {
364
+ selectorConstraints = Object.entries(selector)
365
+ .map(function (_a) {
366
+ var _b = __read(_a, 2), key = _b[0], value = _b[1];
367
+ return "".concat(key, ": ").concat(value);
368
+ })
369
+ .join(", ");
370
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Product with ".concat(selectorConstraints, " was not found"));
371
+ }
372
+ return [2 /*return*/, product];
373
+ }
374
+ });
375
+ }); })];
376
+ case 1: return [2 /*return*/, _a.sent()];
459
377
  }
460
378
  });
461
379
  });
462
380
  };
463
- ProductService.prototype.listTypes = function () {
381
+ /**
382
+ * Gets all variants belonging to a product.
383
+ * @param productId - the id of the product to get variants from.
384
+ * @param config - The config to select and configure relations etc...
385
+ * @return an array of variants
386
+ */
387
+ ProductService.prototype.retrieveVariants = function (productId, config) {
388
+ if (config === void 0) { config = {
389
+ skip: 0,
390
+ take: 50,
391
+ }; }
464
392
  return __awaiter(this, void 0, void 0, function () {
465
- var productTypeRepository;
393
+ var _this = this;
466
394
  return __generator(this, function (_a) {
467
395
  switch (_a.label) {
468
- case 0:
469
- productTypeRepository = this.manager_.getCustomRepository(this.productTypeRepository_);
470
- return [4 /*yield*/, productTypeRepository.find({})];
396
+ case 0: return [4 /*yield*/, this.atomicPhase_(function () { return __awaiter(_this, void 0, void 0, function () {
397
+ var givenRelations, requiredRelations, relationsSet, product;
398
+ var _a;
399
+ return __generator(this, function (_b) {
400
+ switch (_b.label) {
401
+ case 0:
402
+ givenRelations = (_a = config.relations) !== null && _a !== void 0 ? _a : [];
403
+ requiredRelations = ["variants"];
404
+ relationsSet = new Set(__spreadArray(__spreadArray([], __read(givenRelations), false), __read(requiredRelations), false));
405
+ return [4 /*yield*/, this.retrieve(productId, __assign(__assign({}, config), { relations: __spreadArray([], __read(relationsSet), false) }))];
406
+ case 1:
407
+ product = _b.sent();
408
+ return [2 /*return*/, product.variants];
409
+ }
410
+ });
411
+ }); })];
471
412
  case 1: return [2 /*return*/, _a.sent()];
472
413
  }
473
414
  });
474
415
  });
475
416
  };
476
- ProductService.prototype.listTagsByUsage = function (count) {
477
- if (count === void 0) { count = 10; }
417
+ ProductService.prototype.listTypes = function () {
478
418
  return __awaiter(this, void 0, void 0, function () {
479
- var tags;
419
+ var _this = this;
480
420
  return __generator(this, function (_a) {
481
421
  switch (_a.label) {
482
- case 0: return [4 /*yield*/, this.manager_.query("\n SELECT ID, O.USAGE_COUNT, PT.VALUE\n FROM PRODUCT_TAG PT\n LEFT JOIN\n (SELECT COUNT(*) AS USAGE_COUNT,\n PRODUCT_TAG_ID\n FROM PRODUCT_TAGS\n GROUP BY PRODUCT_TAG_ID) O ON O.PRODUCT_TAG_ID = PT.ID\n ORDER BY O.USAGE_COUNT DESC\n LIMIT $1", [count])];
483
- case 1:
484
- tags = _a.sent();
485
- return [2 /*return*/, tags];
422
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
423
+ var productTypeRepository;
424
+ return __generator(this, function (_a) {
425
+ switch (_a.label) {
426
+ case 0:
427
+ productTypeRepository = manager.getCustomRepository(this.productTypeRepository_);
428
+ return [4 /*yield*/, productTypeRepository.find({})];
429
+ case 1: return [2 /*return*/, _a.sent()];
430
+ }
431
+ });
432
+ }); })];
433
+ case 1: return [2 /*return*/, _a.sent()];
486
434
  }
487
435
  });
488
436
  });
489
437
  };
490
- ProductService.prototype.upsertProductType_ = function (type) {
438
+ ProductService.prototype.listTagsByUsage = function (count) {
439
+ if (count === void 0) { count = 10; }
491
440
  return __awaiter(this, void 0, void 0, function () {
492
- var productTypeRepository, existing, created, result;
441
+ var _this = this;
493
442
  return __generator(this, function (_a) {
494
443
  switch (_a.label) {
495
- case 0:
496
- productTypeRepository = this.manager_.getCustomRepository(this.productTypeRepository_);
497
- if (type === null) {
498
- return [2 /*return*/, null];
499
- }
500
- return [4 /*yield*/, productTypeRepository.findOne({
501
- where: { value: type.value },
502
- })];
503
- case 1:
504
- existing = _a.sent();
505
- if (existing) {
506
- return [2 /*return*/, existing.id];
507
- }
508
- created = productTypeRepository.create({
509
- value: type.value,
510
- });
511
- return [4 /*yield*/, productTypeRepository.save(created)];
512
- case 2:
513
- result = _a.sent();
514
- return [2 /*return*/, result.id];
515
- }
516
- });
517
- });
518
- };
519
- ProductService.prototype.upsertProductTags_ = function (tags) {
520
- return __awaiter(this, void 0, void 0, function () {
521
- var productTagRepository, newTags, tags_1, tags_1_1, tag, existing, created, result, e_1_1;
522
- var e_1, _a;
523
- return __generator(this, function (_b) {
524
- switch (_b.label) {
525
- case 0:
526
- productTagRepository = this.manager_.getCustomRepository(this.productTagRepository_);
527
- newTags = [];
528
- _b.label = 1;
529
- case 1:
530
- _b.trys.push([1, 8, 9, 10]);
531
- tags_1 = __values(tags), tags_1_1 = tags_1.next();
532
- _b.label = 2;
533
- case 2:
534
- if (!!tags_1_1.done) return [3 /*break*/, 7];
535
- tag = tags_1_1.value;
536
- return [4 /*yield*/, productTagRepository.findOne({
537
- where: { value: tag.value },
538
- })];
539
- case 3:
540
- existing = _b.sent();
541
- if (!existing) return [3 /*break*/, 4];
542
- newTags.push(existing);
543
- return [3 /*break*/, 6];
544
- case 4:
545
- created = productTagRepository.create(tag);
546
- return [4 /*yield*/, productTagRepository.save(created)];
547
- case 5:
548
- result = _b.sent();
549
- newTags.push(result);
550
- _b.label = 6;
551
- case 6:
552
- tags_1_1 = tags_1.next();
553
- return [3 /*break*/, 2];
554
- case 7: return [3 /*break*/, 10];
555
- case 8:
556
- e_1_1 = _b.sent();
557
- e_1 = { error: e_1_1 };
558
- return [3 /*break*/, 10];
559
- case 9:
560
- try {
561
- if (tags_1_1 && !tags_1_1.done && (_a = tags_1.return)) _a.call(tags_1);
562
- }
563
- finally { if (e_1) throw e_1.error; }
564
- return [7 /*endfinally*/];
565
- case 10: return [2 /*return*/, newTags];
444
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
445
+ var productTagRepo;
446
+ return __generator(this, function (_a) {
447
+ switch (_a.label) {
448
+ case 0:
449
+ productTagRepo = manager.getCustomRepository(this.productTagRepository_);
450
+ return [4 /*yield*/, productTagRepo.listTagsByUsage(count)];
451
+ case 1: return [2 /*return*/, _a.sent()];
452
+ }
453
+ });
454
+ }); })];
455
+ case 1: return [2 /*return*/, _a.sent()];
566
456
  }
567
457
  });
568
458
  });
569
459
  };
570
460
  /**
571
461
  * Creates a product.
572
- * @param {object} productObject - the product to create
573
- * @return {Promise} resolves to the creation result.
462
+ * @param productObject - the product to create
463
+ * @return resolves to the creation result.
574
464
  */
575
465
  ProductService.prototype.create = function (productObject) {
576
466
  return __awaiter(this, void 0, void 0, function () {
577
467
  var _this = this;
578
468
  return __generator(this, function (_a) {
579
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
580
- var productRepo, optionRepo, options, tags, type, images, rest, product_1, _a, _b, _c, _d, result, error_1;
581
- var _this = this;
582
- return __generator(this, function (_e) {
583
- switch (_e.label) {
584
- case 0:
585
- productRepo = manager.getCustomRepository(this.productRepository_);
586
- optionRepo = manager.getCustomRepository(this.productOptionRepository_);
587
- options = productObject.options, tags = productObject.tags, type = productObject.type, images = productObject.images, rest = __rest(productObject, ["options", "tags", "type", "images"]);
588
- if (!rest.thumbnail && images && images.length) {
589
- rest.thumbnail = images[0];
590
- }
591
- // if product is a giftcard, we should disallow discounts
592
- if (rest.is_giftcard) {
593
- rest.discountable = false;
594
- }
595
- _e.label = 1;
596
- case 1:
597
- _e.trys.push([1, 12, , 13]);
598
- product_1 = productRepo.create(rest);
599
- if (!images) return [3 /*break*/, 3];
600
- _a = product_1;
601
- return [4 /*yield*/, this.upsertImages_(images)];
602
- case 2:
603
- _a.images = _e.sent();
604
- _e.label = 3;
605
- case 3:
606
- if (!tags) return [3 /*break*/, 5];
607
- _b = product_1;
608
- return [4 /*yield*/, this.upsertProductTags_(tags)];
609
- case 4:
610
- _b.tags = _e.sent();
611
- _e.label = 5;
612
- case 5:
613
- if (!(typeof type !== "undefined")) return [3 /*break*/, 7];
614
- _c = product_1;
615
- return [4 /*yield*/, this.upsertProductType_(type)];
616
- case 6:
617
- _c.type_id = _e.sent();
618
- _e.label = 7;
619
- case 7: return [4 /*yield*/, productRepo.save(product_1)];
620
- case 8:
621
- product_1 = _e.sent();
622
- _d = product_1;
623
- return [4 /*yield*/, Promise.all(options.map(function (o) { return __awaiter(_this, void 0, void 0, function () {
624
- var res;
625
- return __generator(this, function (_a) {
626
- switch (_a.label) {
627
- case 0:
628
- res = optionRepo.create(__assign(__assign({}, o), { product_id: product_1.id }));
629
- return [4 /*yield*/, optionRepo.save(res)];
630
- case 1:
631
- _a.sent();
632
- return [2 /*return*/, res];
633
- }
634
- });
635
- }); }))];
636
- case 9:
637
- _d.options = _e.sent();
638
- return [4 /*yield*/, this.retrieve(product_1.id, {
639
- relations: ["options"],
640
- })];
641
- case 10:
642
- result = _e.sent();
643
- return [4 /*yield*/, this.eventBus_
644
- .withTransaction(manager)
645
- .emit(ProductService.Events.CREATED, {
646
- id: result.id,
647
- })];
648
- case 11:
649
- _e.sent();
650
- return [2 /*return*/, result];
651
- case 12:
652
- error_1 = _e.sent();
653
- throw (0, exception_formatter_1.formatException)(error_1);
654
- case 13: return [2 /*return*/];
655
- }
656
- });
657
- }); })];
658
- });
659
- });
660
- };
661
- ProductService.prototype.upsertImages_ = function (images) {
662
- return __awaiter(this, void 0, void 0, function () {
663
- var imageRepository, productImages, images_1, images_1_1, img, existing, created, e_2_1;
664
- var e_2, _a;
665
- return __generator(this, function (_b) {
666
- switch (_b.label) {
667
- case 0:
668
- imageRepository = this.manager_.getCustomRepository(this.imageRepository_);
669
- productImages = [];
670
- _b.label = 1;
671
- case 1:
672
- _b.trys.push([1, 6, 7, 8]);
673
- images_1 = __values(images), images_1_1 = images_1.next();
674
- _b.label = 2;
675
- case 2:
676
- if (!!images_1_1.done) return [3 /*break*/, 5];
677
- img = images_1_1.value;
678
- return [4 /*yield*/, imageRepository.findOne({
679
- where: { url: img },
680
- })];
681
- case 3:
682
- existing = _b.sent();
683
- if (existing) {
684
- productImages.push(existing);
685
- }
686
- else {
687
- created = imageRepository.create({ url: img });
688
- productImages.push(created);
689
- }
690
- _b.label = 4;
691
- case 4:
692
- images_1_1 = images_1.next();
693
- return [3 /*break*/, 2];
694
- case 5: return [3 /*break*/, 8];
695
- case 6:
696
- e_2_1 = _b.sent();
697
- e_2 = { error: e_2_1 };
698
- return [3 /*break*/, 8];
699
- case 7:
700
- try {
701
- if (images_1_1 && !images_1_1.done && (_a = images_1.return)) _a.call(images_1);
702
- }
703
- finally { if (e_2) throw e_2.error; }
704
- return [7 /*endfinally*/];
705
- case 8: return [2 /*return*/, productImages];
469
+ switch (_a.label) {
470
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
471
+ var productRepo, productTagRepo, productTypeRepo, imageRepo, optionRepo, options, tags, type, images, rest, product_1, _a, _b, _c, _d, result, error_1;
472
+ var _this = this;
473
+ var _e;
474
+ return __generator(this, function (_f) {
475
+ switch (_f.label) {
476
+ case 0:
477
+ productRepo = manager.getCustomRepository(this.productRepository_);
478
+ productTagRepo = manager.getCustomRepository(this.productTagRepository_);
479
+ productTypeRepo = manager.getCustomRepository(this.productTypeRepository_);
480
+ imageRepo = manager.getCustomRepository(this.imageRepository_);
481
+ optionRepo = manager.getCustomRepository(this.productOptionRepository_);
482
+ options = productObject.options, tags = productObject.tags, type = productObject.type, images = productObject.images, rest = __rest(productObject, ["options", "tags", "type", "images"]);
483
+ if (!rest.thumbnail && (images === null || images === void 0 ? void 0 : images.length)) {
484
+ rest.thumbnail = images[0];
485
+ }
486
+ // if product is a giftcard, we should disallow discounts
487
+ if (rest.is_giftcard) {
488
+ rest.discountable = false;
489
+ }
490
+ _f.label = 1;
491
+ case 1:
492
+ _f.trys.push([1, 12, , 13]);
493
+ product_1 = productRepo.create(rest);
494
+ if (!(images === null || images === void 0 ? void 0 : images.length)) return [3 /*break*/, 3];
495
+ _a = product_1;
496
+ return [4 /*yield*/, imageRepo.upsertImages(images)];
497
+ case 2:
498
+ _a.images = _f.sent();
499
+ _f.label = 3;
500
+ case 3:
501
+ if (!(tags === null || tags === void 0 ? void 0 : tags.length)) return [3 /*break*/, 5];
502
+ _b = product_1;
503
+ return [4 /*yield*/, productTagRepo.upsertTags(tags)];
504
+ case 4:
505
+ _b.tags = _f.sent();
506
+ _f.label = 5;
507
+ case 5:
508
+ if (!(typeof type !== "undefined")) return [3 /*break*/, 7];
509
+ _c = product_1;
510
+ return [4 /*yield*/, productTypeRepo.upsertType(type)];
511
+ case 6:
512
+ _c.type_id = ((_e = (_f.sent())) === null || _e === void 0 ? void 0 : _e.id) || null;
513
+ _f.label = 7;
514
+ case 7: return [4 /*yield*/, productRepo.save(product_1)];
515
+ case 8:
516
+ product_1 = _f.sent();
517
+ _d = product_1;
518
+ return [4 /*yield*/, Promise.all((options !== null && options !== void 0 ? options : []).map(function (option) { return __awaiter(_this, void 0, void 0, function () {
519
+ var res;
520
+ return __generator(this, function (_a) {
521
+ switch (_a.label) {
522
+ case 0:
523
+ res = optionRepo.create(__assign(__assign({}, option), { product_id: product_1.id }));
524
+ return [4 /*yield*/, optionRepo.save(res)];
525
+ case 1:
526
+ _a.sent();
527
+ return [2 /*return*/, res];
528
+ }
529
+ });
530
+ }); }))];
531
+ case 9:
532
+ _d.options = _f.sent();
533
+ return [4 /*yield*/, this.retrieve(product_1.id, {
534
+ relations: ["options"],
535
+ })];
536
+ case 10:
537
+ result = _f.sent();
538
+ return [4 /*yield*/, this.eventBus_
539
+ .withTransaction(manager)
540
+ .emit(ProductService.Events.CREATED, {
541
+ id: result.id,
542
+ })];
543
+ case 11:
544
+ _f.sent();
545
+ return [2 /*return*/, result];
546
+ case 12:
547
+ error_1 = _f.sent();
548
+ throw (0, exception_formatter_1.formatException)(error_1);
549
+ case 13: return [2 /*return*/];
550
+ }
551
+ });
552
+ }); })];
553
+ case 1: return [2 /*return*/, _a.sent()];
706
554
  }
707
555
  });
708
556
  });
@@ -720,221 +568,231 @@ var ProductService = /** @class */ (function (_super) {
720
568
  return __awaiter(this, void 0, void 0, function () {
721
569
  var _this = this;
722
570
  return __generator(this, function (_a) {
723
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
724
- var productRepo, productVariantRepo, product, variants, metadata, images, tags, type, rest, _a, _b, _c, _loop_1, _d, _e, variant, e_3_1, newVariants, _loop_2, this_1, _f, _g, _h, i, newVariant, e_4_1, _j, _k, _l, key, value, result;
725
- var e_3, _m, e_4, _o, e_5, _p;
726
- return __generator(this, function (_q) {
727
- switch (_q.label) {
728
- case 0:
729
- productRepo = manager.getCustomRepository(this.productRepository_);
730
- productVariantRepo = manager.getCustomRepository(this.productVariantRepository_);
731
- return [4 /*yield*/, this.retrieve(productId, {
732
- relations: ["variants", "tags", "images"],
733
- })];
734
- case 1:
735
- product = _q.sent();
736
- variants = update.variants, metadata = update.metadata, images = update.images, tags = update.tags, type = update.type, rest = __rest(update, ["variants", "metadata", "images", "tags", "type"]);
737
- if (!product.thumbnail && !update.thumbnail && (images === null || images === void 0 ? void 0 : images.length)) {
738
- product.thumbnail = images[0];
739
- }
740
- if (!images) return [3 /*break*/, 3];
741
- _a = product;
742
- return [4 /*yield*/, this.upsertImages_(images)];
743
- case 2:
744
- _a.images = _q.sent();
745
- _q.label = 3;
746
- case 3:
747
- if (metadata) {
748
- product.metadata = this.setMetadata_(product, metadata);
749
- }
750
- if (!(typeof type !== "undefined")) return [3 /*break*/, 5];
751
- _b = product;
752
- return [4 /*yield*/, this.upsertProductType_(type)];
753
- case 4:
754
- _b.type_id = _q.sent();
755
- _q.label = 5;
756
- case 5:
757
- if (!tags) return [3 /*break*/, 7];
758
- _c = product;
759
- return [4 /*yield*/, this.upsertProductTags_(tags)];
760
- case 6:
761
- _c.tags = _q.sent();
762
- _q.label = 7;
763
- case 7:
764
- if (!variants) return [3 /*break*/, 24];
765
- _loop_1 = function (variant) {
766
- var exists;
767
- return __generator(this, function (_r) {
768
- switch (_r.label) {
769
- case 0:
770
- exists = variants.find(function (v) { return v.id && variant.id === v.id; });
771
- if (!!exists) return [3 /*break*/, 2];
772
- return [4 /*yield*/, productVariantRepo.remove(variant)];
773
- case 1:
774
- _r.sent();
775
- _r.label = 2;
776
- case 2: return [2 /*return*/];
777
- }
778
- });
779
- };
780
- _q.label = 8;
781
- case 8:
782
- _q.trys.push([8, 13, 14, 15]);
783
- _d = __values(product.variants), _e = _d.next();
784
- _q.label = 9;
785
- case 9:
786
- if (!!_e.done) return [3 /*break*/, 12];
787
- variant = _e.value;
788
- return [5 /*yield**/, _loop_1(variant)];
789
- case 10:
790
- _q.sent();
791
- _q.label = 11;
792
- case 11:
793
- _e = _d.next();
794
- return [3 /*break*/, 9];
795
- case 12: return [3 /*break*/, 15];
796
- case 13:
797
- e_3_1 = _q.sent();
798
- e_3 = { error: e_3_1 };
799
- return [3 /*break*/, 15];
800
- case 14:
801
- try {
802
- if (_e && !_e.done && (_m = _d.return)) _m.call(_d);
803
- }
804
- finally { if (e_3) throw e_3.error; }
805
- return [7 /*endfinally*/];
806
- case 15:
807
- newVariants = [];
808
- _loop_2 = function (i, newVariant) {
809
- var variant, saved, created;
810
- return __generator(this, function (_s) {
811
- switch (_s.label) {
812
- case 0:
813
- newVariant.variant_rank = i;
814
- if (!newVariant.id) return [3 /*break*/, 2];
815
- variant = product.variants.find(function (v) { return v.id === newVariant.id; });
816
- if (!variant) {
817
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Variant with id: ".concat(newVariant.id, " is not associated with this product"));
818
- }
819
- return [4 /*yield*/, this_1.productVariantService_
571
+ switch (_a.label) {
572
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
573
+ var productRepo, productVariantRepo, productTagRepo, productTypeRepo, imageRepo, product, variants, metadata, images, tags, type, rest, _a, _b, _c, _loop_1, _d, _e, variant, e_1_1, newVariants, _loop_2, this_1, _f, _g, _h, i, newVariant, e_2_1, _j, _k, _l, key, value, result;
574
+ var e_1, _m, e_2, _o, e_3, _p;
575
+ var _q;
576
+ return __generator(this, function (_r) {
577
+ switch (_r.label) {
578
+ case 0:
579
+ productRepo = manager.getCustomRepository(this.productRepository_);
580
+ productVariantRepo = manager.getCustomRepository(this.productVariantRepository_);
581
+ productTagRepo = manager.getCustomRepository(this.productTagRepository_);
582
+ productTypeRepo = manager.getCustomRepository(this.productTypeRepository_);
583
+ imageRepo = manager.getCustomRepository(this.imageRepository_);
584
+ return [4 /*yield*/, this.retrieve(productId, {
585
+ relations: ["variants", "tags", "images"],
586
+ })];
587
+ case 1:
588
+ product = _r.sent();
589
+ variants = update.variants, metadata = update.metadata, images = update.images, tags = update.tags, type = update.type, rest = __rest(update, ["variants", "metadata", "images", "tags", "type"]);
590
+ if (!product.thumbnail && !update.thumbnail && (images === null || images === void 0 ? void 0 : images.length)) {
591
+ product.thumbnail = images[0];
592
+ }
593
+ if (!images) return [3 /*break*/, 3];
594
+ _a = product;
595
+ return [4 /*yield*/, imageRepo.upsertImages(images)];
596
+ case 2:
597
+ _a.images = _r.sent();
598
+ _r.label = 3;
599
+ case 3:
600
+ if (metadata) {
601
+ product.metadata = (0, utils_1.setMetadata)(product, metadata);
602
+ }
603
+ if (!(typeof type !== "undefined")) return [3 /*break*/, 5];
604
+ _b = product;
605
+ return [4 /*yield*/, productTypeRepo.upsertType(type)];
606
+ case 4:
607
+ _b.type_id = ((_q = (_r.sent())) === null || _q === void 0 ? void 0 : _q.id) || null;
608
+ _r.label = 5;
609
+ case 5:
610
+ if (!tags) return [3 /*break*/, 7];
611
+ _c = product;
612
+ return [4 /*yield*/, productTagRepo.upsertTags(tags)];
613
+ case 6:
614
+ _c.tags = _r.sent();
615
+ _r.label = 7;
616
+ case 7:
617
+ if (!variants) return [3 /*break*/, 24];
618
+ _loop_1 = function (variant) {
619
+ var exists;
620
+ return __generator(this, function (_s) {
621
+ switch (_s.label) {
622
+ case 0:
623
+ exists = variants.find(function (v) { return v.id && variant.id === v.id; });
624
+ if (!!exists) return [3 /*break*/, 2];
625
+ return [4 /*yield*/, productVariantRepo.remove(variant)];
626
+ case 1:
627
+ _s.sent();
628
+ _s.label = 2;
629
+ case 2: return [2 /*return*/];
630
+ }
631
+ });
632
+ };
633
+ _r.label = 8;
634
+ case 8:
635
+ _r.trys.push([8, 13, 14, 15]);
636
+ _d = __values(product.variants), _e = _d.next();
637
+ _r.label = 9;
638
+ case 9:
639
+ if (!!_e.done) return [3 /*break*/, 12];
640
+ variant = _e.value;
641
+ return [5 /*yield**/, _loop_1(variant)];
642
+ case 10:
643
+ _r.sent();
644
+ _r.label = 11;
645
+ case 11:
646
+ _e = _d.next();
647
+ return [3 /*break*/, 9];
648
+ case 12: return [3 /*break*/, 15];
649
+ case 13:
650
+ e_1_1 = _r.sent();
651
+ e_1 = { error: e_1_1 };
652
+ return [3 /*break*/, 15];
653
+ case 14:
654
+ try {
655
+ if (_e && !_e.done && (_m = _d.return)) _m.call(_d);
656
+ }
657
+ finally { if (e_1) throw e_1.error; }
658
+ return [7 /*endfinally*/];
659
+ case 15:
660
+ newVariants = [];
661
+ _loop_2 = function (i, newVariant) {
662
+ var variant_rank, variant, saved, created;
663
+ return __generator(this, function (_t) {
664
+ switch (_t.label) {
665
+ case 0:
666
+ variant_rank = i;
667
+ if (!newVariant.id) return [3 /*break*/, 2];
668
+ variant = product.variants.find(function (v) { return v.id === newVariant.id; });
669
+ if (!variant) {
670
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Variant with id: ".concat(newVariant.id, " is not associated with this product"));
671
+ }
672
+ return [4 /*yield*/, this_1.productVariantService_
673
+ .withTransaction(manager)
674
+ .update(variant, __assign(__assign({}, newVariant), { variant_rank: variant_rank, product_id: variant.product_id }))];
675
+ case 1:
676
+ saved = _t.sent();
677
+ newVariants.push(saved);
678
+ return [3 /*break*/, 4];
679
+ case 2: return [4 /*yield*/, this_1.productVariantService_
820
680
  .withTransaction(manager)
821
- .update(variant, newVariant)];
822
- case 1:
823
- saved = _s.sent();
824
- newVariants.push(saved);
825
- return [3 /*break*/, 4];
826
- case 2: return [4 /*yield*/, this_1.productVariantService_
827
- .withTransaction(manager)
828
- .create(product.id, newVariant)];
829
- case 3:
830
- created = _s.sent();
831
- newVariants.push(created);
832
- _s.label = 4;
833
- case 4: return [2 /*return*/];
834
- }
835
- });
836
- };
837
- this_1 = this;
838
- _q.label = 16;
839
- case 16:
840
- _q.trys.push([16, 21, 22, 23]);
841
- _f = __values(variants.entries()), _g = _f.next();
842
- _q.label = 17;
843
- case 17:
844
- if (!!_g.done) return [3 /*break*/, 20];
845
- _h = __read(_g.value, 2), i = _h[0], newVariant = _h[1];
846
- return [5 /*yield**/, _loop_2(i, newVariant)];
847
- case 18:
848
- _q.sent();
849
- _q.label = 19;
850
- case 19:
851
- _g = _f.next();
852
- return [3 /*break*/, 17];
853
- case 20: return [3 /*break*/, 23];
854
- case 21:
855
- e_4_1 = _q.sent();
856
- e_4 = { error: e_4_1 };
857
- return [3 /*break*/, 23];
858
- case 22:
859
- try {
860
- if (_g && !_g.done && (_o = _f.return)) _o.call(_f);
861
- }
862
- finally { if (e_4) throw e_4.error; }
863
- return [7 /*endfinally*/];
864
- case 23:
865
- product.variants = newVariants;
866
- _q.label = 24;
867
- case 24:
868
- try {
869
- for (_j = __values(Object.entries(rest)), _k = _j.next(); !_k.done; _k = _j.next()) {
870
- _l = __read(_k.value, 2), key = _l[0], value = _l[1];
871
- if (typeof value !== "undefined") {
872
- product[key] = value;
873
- }
681
+ .create(product.id, __assign(__assign({}, newVariant), { variant_rank: variant_rank, options: newVariant.options || [], prices: newVariant.prices || [] }))];
682
+ case 3:
683
+ created = _t.sent();
684
+ newVariants.push(created);
685
+ _t.label = 4;
686
+ case 4: return [2 /*return*/];
687
+ }
688
+ });
689
+ };
690
+ this_1 = this;
691
+ _r.label = 16;
692
+ case 16:
693
+ _r.trys.push([16, 21, 22, 23]);
694
+ _f = __values(variants.entries()), _g = _f.next();
695
+ _r.label = 17;
696
+ case 17:
697
+ if (!!_g.done) return [3 /*break*/, 20];
698
+ _h = __read(_g.value, 2), i = _h[0], newVariant = _h[1];
699
+ return [5 /*yield**/, _loop_2(i, newVariant)];
700
+ case 18:
701
+ _r.sent();
702
+ _r.label = 19;
703
+ case 19:
704
+ _g = _f.next();
705
+ return [3 /*break*/, 17];
706
+ case 20: return [3 /*break*/, 23];
707
+ case 21:
708
+ e_2_1 = _r.sent();
709
+ e_2 = { error: e_2_1 };
710
+ return [3 /*break*/, 23];
711
+ case 22:
712
+ try {
713
+ if (_g && !_g.done && (_o = _f.return)) _o.call(_f);
874
714
  }
875
- }
876
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
877
- finally {
715
+ finally { if (e_2) throw e_2.error; }
716
+ return [7 /*endfinally*/];
717
+ case 23:
718
+ product.variants = newVariants;
719
+ _r.label = 24;
720
+ case 24:
878
721
  try {
879
- if (_k && !_k.done && (_p = _j.return)) _p.call(_j);
722
+ for (_j = __values(Object.entries(rest)), _k = _j.next(); !_k.done; _k = _j.next()) {
723
+ _l = __read(_k.value, 2), key = _l[0], value = _l[1];
724
+ if (typeof value !== "undefined") {
725
+ product[key] = value;
726
+ }
727
+ }
880
728
  }
881
- finally { if (e_5) throw e_5.error; }
882
- }
883
- return [4 /*yield*/, productRepo.save(product)];
884
- case 25:
885
- result = _q.sent();
886
- return [4 /*yield*/, this.eventBus_
887
- .withTransaction(manager)
888
- .emit(ProductService.Events.UPDATED, {
889
- id: result.id,
890
- fields: Object.keys(update),
891
- })];
892
- case 26:
893
- _q.sent();
894
- return [2 /*return*/, result];
895
- }
896
- });
897
- }); })];
729
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
730
+ finally {
731
+ try {
732
+ if (_k && !_k.done && (_p = _j.return)) _p.call(_j);
733
+ }
734
+ finally { if (e_3) throw e_3.error; }
735
+ }
736
+ return [4 /*yield*/, productRepo.save(product)];
737
+ case 25:
738
+ result = _r.sent();
739
+ return [4 /*yield*/, this.eventBus_
740
+ .withTransaction(manager)
741
+ .emit(ProductService.Events.UPDATED, {
742
+ id: result.id,
743
+ fields: Object.keys(update),
744
+ })];
745
+ case 26:
746
+ _r.sent();
747
+ return [2 /*return*/, result];
748
+ }
749
+ });
750
+ }); })];
751
+ case 1: return [2 /*return*/, _a.sent()];
752
+ }
898
753
  });
899
754
  });
900
755
  };
901
756
  /**
902
757
  * Deletes a product from a given product id. The product's associated
903
758
  * variants will also be deleted.
904
- * @param {string} productId - the id of the product to delete. Must be
759
+ * @param productId - the id of the product to delete. Must be
905
760
  * castable as an ObjectId
906
- * @return {Promise} empty promise
761
+ * @return empty promise
907
762
  */
908
763
  ProductService.prototype.delete = function (productId) {
909
764
  return __awaiter(this, void 0, void 0, function () {
910
765
  var _this = this;
911
766
  return __generator(this, function (_a) {
912
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
913
- var productRepo, product;
914
- return __generator(this, function (_a) {
915
- switch (_a.label) {
916
- case 0:
917
- productRepo = manager.getCustomRepository(this.productRepository_);
918
- return [4 /*yield*/, productRepo.findOne({ id: productId }, { relations: ["variants", "variants.prices", "variants.options"] })];
919
- case 1:
920
- product = _a.sent();
921
- if (!product) {
922
- return [2 /*return*/];
923
- }
924
- return [4 /*yield*/, productRepo.softRemove(product)];
925
- case 2:
926
- _a.sent();
927
- return [4 /*yield*/, this.eventBus_
928
- .withTransaction(manager)
929
- .emit(ProductService.Events.DELETED, {
930
- id: productId,
931
- })];
932
- case 3:
933
- _a.sent();
934
- return [2 /*return*/, Promise.resolve()];
935
- }
936
- });
937
- }); })];
767
+ switch (_a.label) {
768
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
769
+ var productRepo, product;
770
+ return __generator(this, function (_a) {
771
+ switch (_a.label) {
772
+ case 0:
773
+ productRepo = manager.getCustomRepository(this.productRepository_);
774
+ return [4 /*yield*/, productRepo.findOne({ id: productId }, { relations: ["variants", "variants.prices", "variants.options"] })];
775
+ case 1:
776
+ product = _a.sent();
777
+ if (!product) {
778
+ return [2 /*return*/];
779
+ }
780
+ return [4 /*yield*/, productRepo.softRemove(product)];
781
+ case 2:
782
+ _a.sent();
783
+ return [4 /*yield*/, this.eventBus_
784
+ .withTransaction(manager)
785
+ .emit(ProductService.Events.DELETED, {
786
+ id: productId,
787
+ })];
788
+ case 3:
789
+ _a.sent();
790
+ return [2 /*return*/, Promise.resolve()];
791
+ }
792
+ });
793
+ }); })];
794
+ case 1: return [2 /*return*/, _a.sent()];
795
+ }
938
796
  });
939
797
  });
940
798
  };
@@ -942,314 +800,240 @@ var ProductService = /** @class */ (function (_super) {
942
800
  * Adds an option to a product. Options can, for example, be "Size", "Color",
943
801
  * etc. Will update all the products variants with a dummy value for the newly
944
802
  * created option. The same option cannot be added more than once.
945
- * @param {string} productId - the product to apply the new option to
946
- * @param {string} optionTitle - the display title of the option, e.g. "Size"
947
- * @return {Promise} the result of the model update operation
803
+ * @param productId - the product to apply the new option to
804
+ * @param optionTitle - the display title of the option, e.g. "Size"
805
+ * @return the result of the model update operation
948
806
  */
949
807
  ProductService.prototype.addOption = function (productId, optionTitle) {
950
808
  return __awaiter(this, void 0, void 0, function () {
951
809
  var _this = this;
952
810
  return __generator(this, function (_a) {
953
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
954
- var productOptionRepo, product, option, _a, _b, variant, result;
955
- var e_6, _c;
956
- return __generator(this, function (_d) {
957
- switch (_d.label) {
958
- case 0:
959
- productOptionRepo = manager.getCustomRepository(this.productOptionRepository_);
960
- return [4 /*yield*/, this.retrieve(productId, {
961
- relations: ["options", "variants"],
962
- })];
963
- case 1:
964
- product = _d.sent();
965
- if (product.options.find(function (o) { return o.title === optionTitle; })) {
966
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.DUPLICATE_ERROR, "An option with the title: ".concat(optionTitle, " already exists"));
967
- }
968
- return [4 /*yield*/, productOptionRepo.create({
969
- title: optionTitle,
970
- product_id: productId,
971
- })];
972
- case 2:
973
- option = _d.sent();
974
- return [4 /*yield*/, productOptionRepo.save(option)];
975
- case 3:
976
- _d.sent();
977
- try {
978
- for (_a = __values(product.variants), _b = _a.next(); !_b.done; _b = _a.next()) {
979
- variant = _b.value;
980
- this.productVariantService_
981
- .withTransaction(manager)
982
- .addOptionValue(variant.id, option.id, "Default Value");
811
+ switch (_a.label) {
812
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
813
+ var productOptionRepo, product, option, _a, _b, variant, result;
814
+ var e_4, _c;
815
+ return __generator(this, function (_d) {
816
+ switch (_d.label) {
817
+ case 0:
818
+ productOptionRepo = manager.getCustomRepository(this.productOptionRepository_);
819
+ return [4 /*yield*/, this.retrieve(productId, {
820
+ relations: ["options", "variants"],
821
+ })];
822
+ case 1:
823
+ product = _d.sent();
824
+ if (product.options.find(function (o) { return o.title === optionTitle; })) {
825
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.DUPLICATE_ERROR, "An option with the title: ".concat(optionTitle, " already exists"));
983
826
  }
984
- }
985
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
986
- finally {
827
+ return [4 /*yield*/, productOptionRepo.create({
828
+ title: optionTitle,
829
+ product_id: productId,
830
+ })];
831
+ case 2:
832
+ option = _d.sent();
833
+ return [4 /*yield*/, productOptionRepo.save(option)];
834
+ case 3:
835
+ _d.sent();
987
836
  try {
988
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
837
+ for (_a = __values(product.variants), _b = _a.next(); !_b.done; _b = _a.next()) {
838
+ variant = _b.value;
839
+ this.productVariantService_
840
+ .withTransaction(manager)
841
+ .addOptionValue(variant.id, option.id, "Default Value");
842
+ }
989
843
  }
990
- finally { if (e_6) throw e_6.error; }
991
- }
992
- return [4 /*yield*/, this.retrieve(productId)];
993
- case 4:
994
- result = _d.sent();
995
- return [4 /*yield*/, this.eventBus_
996
- .withTransaction(manager)
997
- .emit(ProductService.Events.UPDATED, result)];
998
- case 5:
999
- _d.sent();
1000
- return [2 /*return*/, result];
1001
- }
1002
- });
1003
- }); })];
1004
- });
1005
- });
1006
- };
1007
- ProductService.prototype.reorderVariants = function (productId, variantOrder) {
1008
- return __awaiter(this, void 0, void 0, function () {
1009
- var _this = this;
1010
- return __generator(this, function (_a) {
1011
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1012
- var productRepo, product, result;
1013
- return __generator(this, function (_a) {
1014
- switch (_a.label) {
1015
- case 0:
1016
- productRepo = manager.getCustomRepository(this.productRepository_);
1017
- return [4 /*yield*/, this.retrieve(productId, {
1018
- relations: ["variants"],
1019
- })];
1020
- case 1:
1021
- product = _a.sent();
1022
- if (product.variants.length !== variantOrder.length) {
1023
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Product variants and new variant order differ in length.");
1024
- }
1025
- product.variants = variantOrder.map(function (vId) {
1026
- var variant = product.variants.find(function (v) { return v.id === vId; });
1027
- if (!variant) {
1028
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Product has no variant with id: ".concat(vId));
844
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
845
+ finally {
846
+ try {
847
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
848
+ }
849
+ finally { if (e_4) throw e_4.error; }
1029
850
  }
1030
- return variant;
1031
- });
1032
- result = productRepo.save(product);
1033
- return [4 /*yield*/, this.eventBus_
1034
- .withTransaction(manager)
1035
- .emit(ProductService.Events.UPDATED, result)];
1036
- case 2:
1037
- _a.sent();
1038
- return [2 /*return*/, result];
1039
- }
1040
- });
1041
- }); })];
851
+ return [4 /*yield*/, this.retrieve(productId)];
852
+ case 4:
853
+ result = _d.sent();
854
+ return [4 /*yield*/, this.eventBus_
855
+ .withTransaction(manager)
856
+ .emit(ProductService.Events.UPDATED, result)];
857
+ case 5:
858
+ _d.sent();
859
+ return [2 /*return*/, result];
860
+ }
861
+ });
862
+ }); })];
863
+ case 1: return [2 /*return*/, _a.sent()];
864
+ }
1042
865
  });
1043
866
  });
1044
867
  };
1045
- /**
1046
- * Changes the order of a product's options. Will throw if the length of
1047
- * optionOrder and the length of the product's options are different. Will
1048
- * throw optionOrder contains an id not associated with the product.
1049
- * @param {string} productId - the product whose options we are reordering
1050
- * @param {string[]} optionOrder - the ids of the product's options in the
1051
- * new order
1052
- * @return {Promise} the result of the update operation
1053
- */
1054
- ProductService.prototype.reorderOptions = function (productId, optionOrder) {
868
+ ProductService.prototype.reorderVariants = function (productId, variantOrder) {
1055
869
  return __awaiter(this, void 0, void 0, function () {
1056
870
  var _this = this;
1057
871
  return __generator(this, function (_a) {
1058
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1059
- var productRepo, product, result;
1060
- return __generator(this, function (_a) {
1061
- switch (_a.label) {
1062
- case 0:
1063
- productRepo = manager.getCustomRepository(this.productRepository_);
1064
- return [4 /*yield*/, this.retrieve(productId, { relations: ["options"] })];
1065
- case 1:
1066
- product = _a.sent();
1067
- if (product.options.length !== optionOrder.length) {
1068
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Product options and new options order differ in length.");
1069
- }
1070
- product.options = optionOrder.map(function (oId) {
1071
- var option = product.options.find(function (o) { return o.id === oId; });
1072
- if (!option) {
1073
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Product has no option with id: ".concat(oId));
872
+ switch (_a.label) {
873
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
874
+ var productRepo, product, result;
875
+ return __generator(this, function (_a) {
876
+ switch (_a.label) {
877
+ case 0:
878
+ productRepo = manager.getCustomRepository(this.productRepository_);
879
+ return [4 /*yield*/, this.retrieve(productId, {
880
+ relations: ["variants"],
881
+ })];
882
+ case 1:
883
+ product = _a.sent();
884
+ if (product.variants.length !== variantOrder.length) {
885
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Product variants and new variant order differ in length.");
1074
886
  }
1075
- return option;
1076
- });
1077
- result = productRepo.save(product);
1078
- return [4 /*yield*/, this.eventBus_
1079
- .withTransaction(manager)
1080
- .emit(ProductService.Events.UPDATED, result)];
1081
- case 2:
1082
- _a.sent();
1083
- return [2 /*return*/, result];
1084
- }
1085
- });
1086
- }); })];
887
+ product.variants = variantOrder.map(function (vId) {
888
+ var variant = product.variants.find(function (v) { return v.id === vId; });
889
+ if (!variant) {
890
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Product has no variant with id: ".concat(vId));
891
+ }
892
+ return variant;
893
+ });
894
+ result = productRepo.save(product);
895
+ return [4 /*yield*/, this.eventBus_
896
+ .withTransaction(manager)
897
+ .emit(ProductService.Events.UPDATED, result)];
898
+ case 2:
899
+ _a.sent();
900
+ return [2 /*return*/, result];
901
+ }
902
+ });
903
+ }); })];
904
+ case 1: return [2 /*return*/, _a.sent()];
905
+ }
1087
906
  });
1088
907
  });
1089
908
  };
1090
909
  /**
1091
910
  * Updates a product's option. Throws if the call tries to update an option
1092
911
  * not associated with the product. Throws if the updated title already exists.
1093
- * @param {string} productId - the product whose option we are updating
1094
- * @param {string} optionId - the id of the option we are updating
1095
- * @param {object} data - the data to update the option with
1096
- * @return {Promise} the updated product
912
+ * @param productId - the product whose option we are updating
913
+ * @param optionId - the id of the option we are updating
914
+ * @param data - the data to update the option with
915
+ * @return the updated product
1097
916
  */
1098
917
  ProductService.prototype.updateOption = function (productId, optionId, data) {
1099
918
  return __awaiter(this, void 0, void 0, function () {
1100
919
  var _this = this;
1101
920
  return __generator(this, function (_a) {
1102
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1103
- var productOptionRepo, product, title, values, optionExists, productOption;
1104
- return __generator(this, function (_a) {
1105
- switch (_a.label) {
1106
- case 0:
1107
- productOptionRepo = manager.getCustomRepository(this.productOptionRepository_);
1108
- return [4 /*yield*/, this.retrieve(productId, { relations: ["options"] })];
1109
- case 1:
1110
- product = _a.sent();
1111
- title = data.title, values = data.values;
1112
- optionExists = product.options.some(function (o) {
1113
- return o.title.toUpperCase() === title.toUpperCase() && o.id !== optionId;
1114
- });
1115
- if (optionExists) {
1116
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "An option with title ".concat(title, " already exists"));
1117
- }
1118
- return [4 /*yield*/, productOptionRepo.findOne({
1119
- where: { id: optionId },
1120
- })];
1121
- case 2:
1122
- productOption = _a.sent();
1123
- if (!productOption) {
1124
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Option with id: ".concat(optionId, " does not exist"));
1125
- }
1126
- productOption.title = title;
1127
- productOption.values = values;
1128
- return [4 /*yield*/, productOptionRepo.save(productOption)];
1129
- case 3:
1130
- _a.sent();
1131
- return [4 /*yield*/, this.eventBus_
1132
- .withTransaction(manager)
1133
- .emit(ProductService.Events.UPDATED, product)];
1134
- case 4:
1135
- _a.sent();
1136
- return [2 /*return*/, product];
1137
- }
1138
- });
1139
- }); })];
921
+ switch (_a.label) {
922
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
923
+ var productOptionRepo, product, title, values, optionExists, productOption;
924
+ return __generator(this, function (_a) {
925
+ switch (_a.label) {
926
+ case 0:
927
+ productOptionRepo = manager.getCustomRepository(this.productOptionRepository_);
928
+ return [4 /*yield*/, this.retrieve(productId, { relations: ["options"] })];
929
+ case 1:
930
+ product = _a.sent();
931
+ title = data.title, values = data.values;
932
+ optionExists = product.options.some(function (o) {
933
+ return o.title.toUpperCase() === title.toUpperCase() && o.id !== optionId;
934
+ });
935
+ if (optionExists) {
936
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "An option with title ".concat(title, " already exists"));
937
+ }
938
+ return [4 /*yield*/, productOptionRepo.findOne({
939
+ where: { id: optionId },
940
+ })];
941
+ case 2:
942
+ productOption = _a.sent();
943
+ if (!productOption) {
944
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Option with id: ".concat(optionId, " does not exist"));
945
+ }
946
+ productOption.title = title;
947
+ if (values) {
948
+ productOption.values = values;
949
+ }
950
+ return [4 /*yield*/, productOptionRepo.save(productOption)];
951
+ case 3:
952
+ _a.sent();
953
+ return [4 /*yield*/, this.eventBus_
954
+ .withTransaction(manager)
955
+ .emit(ProductService.Events.UPDATED, product)];
956
+ case 4:
957
+ _a.sent();
958
+ return [2 /*return*/, product];
959
+ }
960
+ });
961
+ }); })];
962
+ case 1: return [2 /*return*/, _a.sent()];
963
+ }
1140
964
  });
1141
965
  });
1142
966
  };
1143
967
  /**
1144
968
  * Delete an option from a product.
1145
- * @param {string} productId - the product to delete an option from
1146
- * @param {string} optionId - the option to delete
1147
- * @return {Promise} the updated product
969
+ * @param productId - the product to delete an option from
970
+ * @param optionId - the option to delete
971
+ * @return the updated product
1148
972
  */
1149
973
  ProductService.prototype.deleteOption = function (productId, optionId) {
1150
974
  return __awaiter(this, void 0, void 0, function () {
1151
975
  var _this = this;
1152
976
  return __generator(this, function (_a) {
1153
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1154
- var productOptionRepo, product, productOption, firstVariant, valueToMatch, equalsFirst;
1155
- var _this = this;
1156
- return __generator(this, function (_a) {
1157
- switch (_a.label) {
1158
- case 0:
1159
- productOptionRepo = manager.getCustomRepository(this.productOptionRepository_);
1160
- return [4 /*yield*/, this.retrieve(productId, {
1161
- relations: ["variants", "variants.options"],
1162
- })];
1163
- case 1:
1164
- product = _a.sent();
1165
- return [4 /*yield*/, productOptionRepo.findOne({
1166
- where: { id: optionId, product_id: productId },
1167
- })];
1168
- case 2:
1169
- productOption = _a.sent();
1170
- if (!productOption) {
1171
- return [2 /*return*/, Promise.resolve()];
1172
- }
1173
- firstVariant = product.variants[0];
1174
- valueToMatch = firstVariant.options.find(function (o) { return o.option_id === optionId; }).value;
1175
- return [4 /*yield*/, Promise.all(product.variants.map(function (v) { return __awaiter(_this, void 0, void 0, function () {
1176
- var option;
1177
- return __generator(this, function (_a) {
1178
- option = v.options.find(function (o) { return o.option_id === optionId; });
1179
- return [2 /*return*/, option.value === valueToMatch];
1180
- });
1181
- }); }))];
1182
- case 3:
1183
- equalsFirst = _a.sent();
1184
- if (!equalsFirst.every(function (v) { return v; })) {
1185
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "To delete an option, first delete all variants, such that when an option is deleted, no duplicate variants will exist.");
1186
- }
1187
- // If we reach this point, we can safely delete the product option
1188
- return [4 /*yield*/, productOptionRepo.softRemove(productOption)];
1189
- case 4:
1190
- // If we reach this point, we can safely delete the product option
1191
- _a.sent();
1192
- return [4 /*yield*/, this.eventBus_
1193
- .withTransaction(manager)
1194
- .emit(ProductService.Events.UPDATED, product)];
1195
- case 5:
1196
- _a.sent();
1197
- return [2 /*return*/, product];
1198
- }
1199
- });
1200
- }); })];
1201
- });
1202
- });
1203
- };
1204
- /**
1205
- * Decorates a product with product variants.
1206
- * @param {string} productId - the productId to decorate.
1207
- * @param {string[]} fields - the fields to include.
1208
- * @param {string[]} expandFields - fields to expand.
1209
- * @param {object} config - retrieve config for price calculation.
1210
- * @return {Product} return the decorated product.
1211
- */
1212
- ProductService.prototype.decorate = function (productId, fields, expandFields, config) {
1213
- var _a;
1214
- if (fields === void 0) { fields = []; }
1215
- if (expandFields === void 0) { expandFields = []; }
1216
- if (config === void 0) { config = {}; }
1217
- return __awaiter(this, void 0, void 0, function () {
1218
- var requiredFields, priceIndex, product, _b;
1219
- return __generator(this, function (_c) {
1220
- switch (_c.label) {
1221
- case 0:
1222
- requiredFields = ["id", "metadata"];
1223
- priceIndex = (_a = expandFields.indexOf("variants.prices")) !== null && _a !== void 0 ? _a : -1;
1224
- if (priceIndex >= 0 && expandFields.length) {
1225
- expandFields = __spreadArray([], __read(expandFields), false);
1226
- expandFields.splice(priceIndex, 1);
1227
- }
1228
- fields = fields.concat(requiredFields);
1229
- return [4 /*yield*/, this.retrieve(productId, {
1230
- select: fields,
1231
- relations: expandFields,
1232
- })];
1233
- case 1:
1234
- product = _c.sent();
1235
- if (!(priceIndex > -1)) return [3 /*break*/, 3];
1236
- return [4 /*yield*/, this.setAdditionalPrices(product, config.currency_code, config.region_id, config.cart_id, config.customer_id, config.include_discount_prices)];
1237
- case 2:
1238
- _b = _c.sent();
1239
- return [3 /*break*/, 4];
1240
- case 3:
1241
- _b = product;
1242
- _c.label = 4;
1243
- case 4: return [2 /*return*/, _b];
977
+ switch (_a.label) {
978
+ case 0: return [4 /*yield*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
979
+ var productOptionRepo, product, productOption, firstVariant, valueToMatch, equalsFirst;
980
+ var _this = this;
981
+ var _a;
982
+ return __generator(this, function (_b) {
983
+ switch (_b.label) {
984
+ case 0:
985
+ productOptionRepo = manager.getCustomRepository(this.productOptionRepository_);
986
+ return [4 /*yield*/, this.retrieve(productId, {
987
+ relations: ["variants", "variants.options"],
988
+ })];
989
+ case 1:
990
+ product = _b.sent();
991
+ return [4 /*yield*/, productOptionRepo.findOne({
992
+ where: { id: optionId, product_id: productId },
993
+ })];
994
+ case 2:
995
+ productOption = _b.sent();
996
+ if (!productOption) {
997
+ return [2 /*return*/, Promise.resolve()];
998
+ }
999
+ firstVariant = product.variants[0];
1000
+ valueToMatch = (_a = firstVariant.options.find(function (o) { return o.option_id === optionId; })) === null || _a === void 0 ? void 0 : _a.value;
1001
+ return [4 /*yield*/, Promise.all(product.variants.map(function (v) { return __awaiter(_this, void 0, void 0, function () {
1002
+ var option;
1003
+ return __generator(this, function (_a) {
1004
+ option = v.options.find(function (o) { return o.option_id === optionId; });
1005
+ return [2 /*return*/, (option === null || option === void 0 ? void 0 : option.value) === valueToMatch];
1006
+ });
1007
+ }); }))];
1008
+ case 3:
1009
+ equalsFirst = _b.sent();
1010
+ if (!equalsFirst.every(function (v) { return v; })) {
1011
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "To delete an option, first delete all variants, such that when an option is deleted, no duplicate variants will exist.");
1012
+ }
1013
+ // If we reach this point, we can safely delete the product option
1014
+ return [4 /*yield*/, productOptionRepo.softRemove(productOption)];
1015
+ case 4:
1016
+ // If we reach this point, we can safely delete the product option
1017
+ _b.sent();
1018
+ return [4 /*yield*/, this.eventBus_
1019
+ .withTransaction(manager)
1020
+ .emit(ProductService.Events.UPDATED, product)];
1021
+ case 5:
1022
+ _b.sent();
1023
+ return [2 /*return*/, product];
1024
+ }
1025
+ });
1026
+ }); })];
1027
+ case 1: return [2 /*return*/, _a.sent()];
1244
1028
  }
1245
1029
  });
1246
1030
  });
1247
1031
  };
1248
1032
  /**
1249
1033
  * Creates a query object to be used for list queries.
1250
- * @param {object} selector - the selector to create the query from
1251
- * @param {object} config - the config to use for the query
1252
- * @return {object} an object containing the query, relations and free-text
1034
+ * @param selector - the selector to create the query from
1035
+ * @param config - the config to use for the query
1036
+ * @return an object containing the query, relations and free-text
1253
1037
  * search param.
1254
1038
  */
1255
1039
  ProductService.prototype.prepareListQuery_ = function (selector, config) {
@@ -1258,7 +1042,7 @@ var ProductService = /** @class */ (function (_super) {
1258
1042
  q = selector.q;
1259
1043
  delete selector.q;
1260
1044
  }
1261
- var query = this.buildQuery_(selector, config);
1045
+ var query = (0, utils_1.buildQuery)(selector, config);
1262
1046
  if (config.relations && config.relations.length > 0) {
1263
1047
  query.relations = config.relations;
1264
1048
  }
@@ -1273,118 +1057,6 @@ var ProductService = /** @class */ (function (_super) {
1273
1057
  q: q,
1274
1058
  };
1275
1059
  };
1276
- /**
1277
- * Creates a QueryBuilder that can fetch products based on free text.
1278
- * @param {ProductRepository} productRepo - an instance of a ProductRepositry
1279
- * @param {FindOptions<Product>} query - the query to get products by
1280
- * @param {string} q - the text to perform free text search from
1281
- * @return {QueryBuilder<Product>} a query builder that can fetch products
1282
- */
1283
- ProductService.prototype.getFreeTextQueryBuilder_ = function (productRepo, query, q) {
1284
- var where = query.where;
1285
- delete where.description;
1286
- delete where.title;
1287
- var qb = productRepo
1288
- .createQueryBuilder("product")
1289
- .leftJoinAndSelect("product.variants", "variant")
1290
- .leftJoinAndSelect("product.collection", "collection")
1291
- .select(["product.id"])
1292
- .where(where)
1293
- .andWhere(new typeorm_1.Brackets(function (qb) {
1294
- qb.where("product.description ILIKE :q", { q: "%".concat(q, "%") })
1295
- .orWhere("product.title ILIKE :q", { q: "%".concat(q, "%") })
1296
- .orWhere("variant.title ILIKE :q", { q: "%".concat(q, "%") })
1297
- .orWhere("variant.sku ILIKE :q", { q: "%".concat(q, "%") })
1298
- .orWhere("collection.title ILIKE :q", { q: "%".concat(q, "%") });
1299
- }))
1300
- .skip(query.skip)
1301
- .take(query.take);
1302
- if (query.withDeleted) {
1303
- qb = qb.withDeleted();
1304
- }
1305
- return qb;
1306
- };
1307
- /**
1308
- * Set additional prices on a list of products.
1309
- * @param {Product[] | Product} products list of products on which to set additional prices
1310
- * @param {string} currency_code currency code to fetch prices for
1311
- * @param {string} region_id region to fetch prices for
1312
- * @param {string} cart_id string of cart to use as a basis for getting currency and region
1313
- * @param {string} customer_id id of potentially logged in customer, used to get prices valid for their customer groups
1314
- * @param {boolean} include_discount_prices indication wether or not to include sales prices in result
1315
- * @return {Promise<Product[]>} A list of products with variants decorated with "additional_prices"
1316
- */
1317
- ProductService.prototype.setAdditionalPrices = function (products, currency_code, region_id, cart_id, customer_id, include_discount_prices) {
1318
- if (include_discount_prices === void 0) { include_discount_prices = false; }
1319
- return __awaiter(this, void 0, void 0, function () {
1320
- var _this = this;
1321
- return __generator(this, function (_a) {
1322
- return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
1323
- var cartRepo, regionId, currencyCode, cart, productArray, priceSelectionStrategy, productsWithPrices;
1324
- var _this = this;
1325
- return __generator(this, function (_a) {
1326
- switch (_a.label) {
1327
- case 0:
1328
- cartRepo = this.manager_.getCustomRepository(this.cartRepository_);
1329
- regionId = region_id;
1330
- currencyCode = currency_code;
1331
- if (!cart_id) return [3 /*break*/, 2];
1332
- return [4 /*yield*/, cartRepo.findOne({
1333
- where: { id: cart_id },
1334
- relations: ["region"],
1335
- })];
1336
- case 1:
1337
- cart = _a.sent();
1338
- regionId = cart.region.id;
1339
- currencyCode = cart.region.currency_code;
1340
- _a.label = 2;
1341
- case 2:
1342
- productArray = Array.isArray(products) ? products : [products];
1343
- priceSelectionStrategy = this.priceSelectionStrategy_.withTransaction(manager);
1344
- return [4 /*yield*/, Promise.all(productArray.map(function (p) { return __awaiter(_this, void 0, void 0, function () {
1345
- var _a;
1346
- var _this = this;
1347
- var _b;
1348
- return __generator(this, function (_c) {
1349
- switch (_c.label) {
1350
- case 0:
1351
- if (!((_b = p.variants) === null || _b === void 0 ? void 0 : _b.length)) return [3 /*break*/, 2];
1352
- _a = p;
1353
- return [4 /*yield*/, Promise.all(p.variants.map(function (v) { return __awaiter(_this, void 0, void 0, function () {
1354
- var prices;
1355
- return __generator(this, function (_a) {
1356
- switch (_a.label) {
1357
- case 0: return [4 /*yield*/, priceSelectionStrategy.calculateVariantPrice(v.id, {
1358
- region_id: regionId,
1359
- currency_code: currencyCode,
1360
- cart_id: cart_id,
1361
- customer_id: customer_id,
1362
- include_discount_prices: include_discount_prices,
1363
- })];
1364
- case 1:
1365
- prices = _a.sent();
1366
- return [2 /*return*/, __assign(__assign({}, v), { prices: prices.prices, original_price: prices.originalPrice, calculated_price: prices.calculatedPrice, calculated_price_type: prices.calculatedPriceType })];
1367
- }
1368
- });
1369
- }); }))];
1370
- case 1:
1371
- _a.variants = _c.sent();
1372
- _c.label = 2;
1373
- case 2: return [2 /*return*/, p];
1374
- }
1375
- });
1376
- }); }))];
1377
- case 3:
1378
- productsWithPrices = _a.sent();
1379
- return [2 /*return*/, Array.isArray(products)
1380
- ? productsWithPrices
1381
- : productsWithPrices[0]];
1382
- }
1383
- });
1384
- }); })];
1385
- });
1386
- });
1387
- };
1388
1060
  ProductService.IndexName = "products";
1389
1061
  ProductService.Events = {
1390
1062
  UPDATED: "product.updated",
@@ -1392,6 +1064,6 @@ var ProductService = /** @class */ (function (_super) {
1392
1064
  DELETED: "product.deleted",
1393
1065
  };
1394
1066
  return ProductService;
1395
- }(medusa_interfaces_1.BaseService));
1067
+ }(interfaces_1.TransactionBaseService));
1396
1068
  exports.default = ProductService;
1397
1069
  //# sourceMappingURL=product.js.map