@medusajs/medusa 1.7.2 → 1.7.3-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (302) hide show
  1. package/.turbo/turbo-test.log +0 -75
  2. package/CHANGELOG.md +50 -0
  3. package/dist/api/index.d.ts +1 -0
  4. package/dist/api/index.js +1 -0
  5. package/dist/api/index.js.map +1 -1
  6. package/dist/api/middlewares/check-registered-modules.d.ts +4 -0
  7. package/dist/api/middlewares/check-registered-modules.js +78 -0
  8. package/dist/api/middlewares/check-registered-modules.js.map +1 -0
  9. package/dist/api/routes/admin/discounts/add-resources-to-condition-batch.d.ts +1 -1
  10. package/dist/api/routes/admin/discounts/add-resources-to-condition-batch.js +1 -1
  11. package/dist/api/routes/admin/discounts/delete-resources-from-condition-batch.d.ts +1 -1
  12. package/dist/api/routes/admin/discounts/delete-resources-from-condition-batch.js +1 -1
  13. package/dist/api/routes/admin/discounts/list-discounts.d.ts +1 -1
  14. package/dist/api/routes/admin/index.js +4 -0
  15. package/dist/api/routes/admin/index.js.map +1 -1
  16. package/dist/api/routes/admin/order-edits/add-line-item.d.ts +1 -1
  17. package/dist/api/routes/admin/order-edits/add-line-item.js +1 -1
  18. package/dist/api/routes/admin/order-edits/delete-line-item.d.ts +1 -1
  19. package/dist/api/routes/admin/order-edits/delete-line-item.js +1 -1
  20. package/dist/api/routes/admin/order-edits/delete-order-edit-item-change.d.ts +1 -1
  21. package/dist/api/routes/admin/order-edits/delete-order-edit-item-change.js +1 -1
  22. package/dist/api/routes/admin/order-edits/delete-order-edit.d.ts +1 -1
  23. package/dist/api/routes/admin/order-edits/delete-order-edit.js +1 -1
  24. package/dist/api/routes/admin/order-edits/get-order-edit.d.ts +1 -1
  25. package/dist/api/routes/admin/order-edits/get-order-edit.js +1 -1
  26. package/dist/api/routes/admin/order-edits/list-order-edit.d.ts +1 -1
  27. package/dist/api/routes/admin/order-edits/request-confirmation.d.ts +1 -1
  28. package/dist/api/routes/admin/order-edits/request-confirmation.js +1 -1
  29. package/dist/api/routes/admin/order-edits/update-order-edit-line-item.d.ts +1 -1
  30. package/dist/api/routes/admin/order-edits/update-order-edit-line-item.js +1 -1
  31. package/dist/api/routes/admin/order-edits/update-order-edit.d.ts +1 -1
  32. package/dist/api/routes/admin/order-edits/update-order-edit.js +1 -1
  33. package/dist/api/routes/admin/orders/cancel-fulfillment.d.ts +5 -0
  34. package/dist/api/routes/admin/orders/cancel-fulfillment.js +46 -2
  35. package/dist/api/routes/admin/orders/cancel-fulfillment.js.map +1 -1
  36. package/dist/api/routes/admin/orders/create-fulfillment.d.ts +1 -0
  37. package/dist/api/routes/admin/orders/create-fulfillment.js +87 -5
  38. package/dist/api/routes/admin/orders/create-fulfillment.js.map +1 -1
  39. package/dist/api/routes/admin/orders/index.js +3 -7
  40. package/dist/api/routes/admin/orders/index.js.map +1 -1
  41. package/dist/api/routes/admin/payment-collections/delete-payment-collection.d.ts +1 -1
  42. package/dist/api/routes/admin/payment-collections/delete-payment-collection.js +1 -1
  43. package/dist/api/routes/admin/payment-collections/get-payment-collection.d.ts +1 -1
  44. package/dist/api/routes/admin/payment-collections/get-payment-collection.js +1 -1
  45. package/dist/api/routes/admin/payment-collections/mark-authorized-payment-collection.d.ts +1 -1
  46. package/dist/api/routes/admin/payment-collections/mark-authorized-payment-collection.js +1 -1
  47. package/dist/api/routes/admin/payment-collections/update-payment-collection.d.ts +1 -1
  48. package/dist/api/routes/admin/payment-collections/update-payment-collection.js +1 -1
  49. package/dist/api/routes/admin/price-lists/list-price-list-products.d.ts +2 -2
  50. package/dist/api/routes/admin/price-lists/list-price-list-products.js +1 -1
  51. package/dist/api/routes/admin/product-categories/delete-product-category.d.ts +64 -0
  52. package/dist/api/routes/admin/product-categories/delete-product-category.js +129 -0
  53. package/dist/api/routes/admin/product-categories/delete-product-category.js.map +1 -0
  54. package/dist/api/routes/admin/product-categories/get-product-category.d.ts +58 -0
  55. package/dist/api/routes/admin/product-categories/get-product-category.js +134 -0
  56. package/dist/api/routes/admin/product-categories/get-product-category.js.map +1 -0
  57. package/dist/api/routes/admin/product-categories/index.d.ts +7 -0
  58. package/dist/api/routes/admin/product-categories/index.js +68 -0
  59. package/dist/api/routes/admin/product-categories/index.js.map +1 -0
  60. package/dist/api/routes/admin/product-categories/list-product-categories.d.ts +78 -0
  61. package/dist/api/routes/admin/product-categories/list-product-categories.js +206 -0
  62. package/dist/api/routes/admin/product-categories/list-product-categories.js.map +1 -0
  63. package/dist/api/routes/admin/publishable-api-keys/add-channels-batch.d.ts +1 -1
  64. package/dist/api/routes/admin/publishable-api-keys/add-channels-batch.js +1 -1
  65. package/dist/api/routes/admin/publishable-api-keys/create-publishable-api-key.d.ts +1 -1
  66. package/dist/api/routes/admin/publishable-api-keys/create-publishable-api-key.js +1 -1
  67. package/dist/api/routes/admin/publishable-api-keys/delete-channels-batch.d.ts +1 -1
  68. package/dist/api/routes/admin/publishable-api-keys/delete-channels-batch.js +1 -1
  69. package/dist/api/routes/admin/publishable-api-keys/delete-publishable-api-key.d.ts +1 -1
  70. package/dist/api/routes/admin/publishable-api-keys/delete-publishable-api-key.js +1 -1
  71. package/dist/api/routes/admin/publishable-api-keys/get-publishable-api-key.d.ts +1 -1
  72. package/dist/api/routes/admin/publishable-api-keys/get-publishable-api-key.js +1 -1
  73. package/dist/api/routes/admin/publishable-api-keys/list-publishable-api-key-sales-channels.d.ts +2 -2
  74. package/dist/api/routes/admin/publishable-api-keys/list-publishable-api-key-sales-channels.js +1 -1
  75. package/dist/api/routes/admin/publishable-api-keys/list-publishable-api-keys.d.ts +1 -1
  76. package/dist/api/routes/admin/publishable-api-keys/revoke-publishable-api-key.d.ts +1 -1
  77. package/dist/api/routes/admin/publishable-api-keys/revoke-publishable-api-key.js +1 -1
  78. package/dist/api/routes/admin/publishable-api-keys/update-publishable-api-key.d.ts +1 -1
  79. package/dist/api/routes/admin/publishable-api-keys/update-publishable-api-key.js +1 -1
  80. package/dist/api/routes/admin/returns/cancel-return.js.map +1 -1
  81. package/dist/api/routes/admin/returns/receive-return.d.ts +1 -0
  82. package/dist/api/routes/admin/returns/receive-return.js +8 -1
  83. package/dist/api/routes/admin/returns/receive-return.js.map +1 -1
  84. package/dist/api/routes/admin/sales-channels/associate-stock-location.d.ts +79 -0
  85. package/dist/api/routes/admin/sales-channels/associate-stock-location.js +163 -0
  86. package/dist/api/routes/admin/sales-channels/associate-stock-location.js.map +1 -0
  87. package/dist/api/routes/admin/sales-channels/index.d.ts +2 -0
  88. package/dist/api/routes/admin/sales-channels/index.js +6 -0
  89. package/dist/api/routes/admin/sales-channels/index.js.map +1 -1
  90. package/dist/api/routes/admin/sales-channels/list-sales-channels.d.ts +1 -1
  91. package/dist/api/routes/admin/sales-channels/remove-stock-location.d.ts +88 -0
  92. package/dist/api/routes/admin/sales-channels/remove-stock-location.js +174 -0
  93. package/dist/api/routes/admin/sales-channels/remove-stock-location.js.map +1 -0
  94. package/dist/api/routes/admin/stock-locations/create-stock-location.d.ts +105 -0
  95. package/dist/api/routes/admin/stock-locations/create-stock-location.js +240 -0
  96. package/dist/api/routes/admin/stock-locations/create-stock-location.js.map +1 -0
  97. package/dist/api/routes/admin/stock-locations/get-stock-location.d.ts +48 -0
  98. package/dist/api/routes/admin/stock-locations/get-stock-location.js +121 -0
  99. package/dist/api/routes/admin/stock-locations/get-stock-location.js.map +1 -0
  100. package/dist/api/routes/admin/stock-locations/index.d.ts +18 -0
  101. package/dist/api/routes/admin/stock-locations/index.js +78 -0
  102. package/dist/api/routes/admin/stock-locations/index.js.map +1 -0
  103. package/dist/api/routes/admin/stock-locations/list-stock-locations.d.ts +144 -0
  104. package/dist/api/routes/admin/stock-locations/list-stock-locations.js +265 -0
  105. package/dist/api/routes/admin/stock-locations/list-stock-locations.js.map +1 -0
  106. package/dist/api/routes/admin/stock-locations/update-stock-location.d.ts +103 -0
  107. package/dist/api/routes/admin/stock-locations/update-stock-location.js +240 -0
  108. package/dist/api/routes/admin/stock-locations/update-stock-location.js.map +1 -0
  109. package/dist/api/routes/admin/uploads/create-protected-upload.d.ts +2 -2
  110. package/dist/api/routes/admin/uploads/create-protected-upload.js +2 -2
  111. package/dist/api/routes/admin/uploads/delete-upload.d.ts +1 -1
  112. package/dist/api/routes/admin/uploads/delete-upload.js +1 -1
  113. package/dist/api/routes/admin/uploads/get-download-url.d.ts +1 -1
  114. package/dist/api/routes/admin/uploads/get-download-url.js +1 -1
  115. package/dist/api/routes/store/order-edits/complete-order-edit.js +35 -34
  116. package/dist/api/routes/store/order-edits/complete-order-edit.js.map +1 -1
  117. package/dist/api/routes/store/orders/confirm-order-request.d.ts +2 -2
  118. package/dist/api/routes/store/orders/confirm-order-request.js +2 -2
  119. package/dist/api/routes/store/orders/request-order.d.ts +1 -1
  120. package/dist/api/routes/store/orders/request-order.js +1 -1
  121. package/dist/api/routes/store/payment-collections/authorize-batch-payment-sessions.d.ts +2 -2
  122. package/dist/api/routes/store/payment-collections/authorize-batch-payment-sessions.js +2 -2
  123. package/dist/api/routes/store/payment-collections/authorize-payment-session.d.ts +2 -2
  124. package/dist/api/routes/store/payment-collections/authorize-payment-session.js +2 -2
  125. package/dist/api/routes/store/payment-collections/get-payment-collection.d.ts +2 -2
  126. package/dist/api/routes/store/payment-collections/get-payment-collection.js +2 -2
  127. package/dist/api/routes/store/payment-collections/manage-batch-payment-sessions.d.ts +2 -2
  128. package/dist/api/routes/store/payment-collections/manage-batch-payment-sessions.js +2 -2
  129. package/dist/api/routes/store/payment-collections/manage-payment-session.d.ts +2 -2
  130. package/dist/api/routes/store/payment-collections/manage-payment-session.js +2 -2
  131. package/dist/api/routes/store/payment-collections/refresh-payment-session.d.ts +1 -1
  132. package/dist/api/routes/store/payment-collections/refresh-payment-session.js +1 -1
  133. package/dist/commands/develop.js +24 -0
  134. package/dist/commands/develop.js.map +1 -1
  135. package/dist/index.d.ts +6 -2
  136. package/dist/index.js +6 -2
  137. package/dist/index.js.map +1 -1
  138. package/dist/interfaces/payment-processor.d.ts +102 -0
  139. package/dist/interfaces/payment-processor.js +31 -0
  140. package/dist/interfaces/payment-processor.js.map +1 -0
  141. package/dist/interfaces/payment-service.d.ts +92 -1
  142. package/dist/interfaces/payment-service.js +10 -0
  143. package/dist/interfaces/payment-service.js.map +1 -1
  144. package/dist/interfaces/services/event-bus.d.ts +3 -0
  145. package/dist/interfaces/services/event-bus.js +3 -0
  146. package/dist/interfaces/services/event-bus.js.map +1 -0
  147. package/dist/interfaces/services/index.d.ts +3 -0
  148. package/dist/interfaces/services/index.js +3 -0
  149. package/dist/interfaces/services/index.js.map +1 -1
  150. package/dist/interfaces/services/inventory.d.ts +24 -0
  151. package/dist/interfaces/services/inventory.js +3 -0
  152. package/dist/interfaces/services/inventory.js.map +1 -0
  153. package/dist/interfaces/services/stock-location.d.ts +9 -0
  154. package/dist/interfaces/services/stock-location.js +3 -0
  155. package/dist/interfaces/services/stock-location.js.map +1 -0
  156. package/dist/loaders/feature-flags/product-categories.d.ts +3 -0
  157. package/dist/loaders/feature-flags/product-categories.js +10 -0
  158. package/dist/loaders/feature-flags/product-categories.js.map +1 -0
  159. package/dist/loaders/feature-flags/publishable-api-keys.js +1 -1
  160. package/dist/loaders/feature-flags/publishable-api-keys.js.map +1 -1
  161. package/dist/loaders/feature-flags/sales-channels.js +1 -1
  162. package/dist/loaders/feature-flags/sales-channels.js.map +1 -1
  163. package/dist/loaders/plugins.d.ts +1 -0
  164. package/dist/loaders/plugins.js +3 -1
  165. package/dist/loaders/plugins.js.map +1 -1
  166. package/dist/migrations/1671711415179-multi_location.d.ts +6 -0
  167. package/dist/migrations/1671711415179-multi_location.js +119 -0
  168. package/dist/migrations/1671711415179-multi_location.js.map +1 -0
  169. package/dist/migrations/1672906846559-product-category.d.ts +6 -0
  170. package/dist/migrations/1672906846559-product-category.js +83 -0
  171. package/dist/migrations/1672906846559-product-category.js.map +1 -0
  172. package/dist/migrations/1672906846560-payment-session-is-initiated.d.ts +6 -0
  173. package/dist/migrations/1672906846560-payment-session-is-initiated.js +81 -0
  174. package/dist/migrations/1672906846560-payment-session-is-initiated.js.map +1 -0
  175. package/dist/migrations/1673003729870-staged_job_options.d.ts +6 -0
  176. package/dist/migrations/1673003729870-staged_job_options.js +71 -0
  177. package/dist/migrations/1673003729870-staged_job_options.js.map +1 -0
  178. package/dist/models/fulfillment.d.ts +5 -0
  179. package/dist/models/fulfillment.js +8 -0
  180. package/dist/models/fulfillment.js.map +1 -1
  181. package/dist/models/index.d.ts +4 -0
  182. package/dist/models/index.js +4 -0
  183. package/dist/models/index.js.map +1 -1
  184. package/dist/models/line-item.d.ts +6 -6
  185. package/dist/models/line-item.js +9 -9
  186. package/dist/models/line-item.js.map +1 -1
  187. package/dist/models/payment-session.d.ts +6 -0
  188. package/dist/models/payment-session.js +9 -0
  189. package/dist/models/payment-session.js.map +1 -1
  190. package/dist/models/product-category.d.ts +67 -0
  191. package/dist/models/product-category.js +144 -0
  192. package/dist/models/product-category.js.map +1 -0
  193. package/dist/models/product-variant-inventory-item.d.ts +7 -0
  194. package/dist/models/product-variant-inventory-item.js +66 -0
  195. package/dist/models/product-variant-inventory-item.js.map +1 -0
  196. package/dist/models/return.d.ts +7 -6
  197. package/dist/models/return.js +14 -9
  198. package/dist/models/return.js.map +1 -1
  199. package/dist/models/sales-channel-location.d.ts +6 -0
  200. package/dist/models/sales-channel-location.js +62 -0
  201. package/dist/models/sales-channel-location.js.map +1 -0
  202. package/dist/models/staged-job.d.ts +1 -0
  203. package/dist/models/staged-job.js +4 -0
  204. package/dist/models/staged-job.js.map +1 -1
  205. package/dist/models/store.d.ts +6 -5
  206. package/dist/models/store.js +11 -7
  207. package/dist/models/store.js.map +1 -1
  208. package/dist/repositories/customer-group.js +18 -2
  209. package/dist/repositories/customer-group.js.map +1 -1
  210. package/dist/repositories/product-category.d.ts +6 -0
  211. package/dist/repositories/product-category.js +125 -0
  212. package/dist/repositories/product-category.js.map +1 -0
  213. package/dist/repositories/product-variant.js +30 -5
  214. package/dist/repositories/product-variant.js.map +1 -1
  215. package/dist/repositories/product.js +7 -2
  216. package/dist/repositories/product.js.map +1 -1
  217. package/dist/scripts/migrate-inventory-items.d.ts +1 -0
  218. package/dist/scripts/migrate-inventory-items.js +197 -0
  219. package/dist/scripts/migrate-inventory-items.js.map +1 -0
  220. package/dist/services/cache.js +2 -5
  221. package/dist/services/cache.js.map +1 -1
  222. package/dist/services/cart.d.ts +9 -26
  223. package/dist/services/cart.js +276 -182
  224. package/dist/services/cart.js.map +1 -1
  225. package/dist/services/claim-item.js +3 -0
  226. package/dist/services/claim-item.js.map +1 -1
  227. package/dist/services/claim.d.ts +4 -4
  228. package/dist/services/claim.js +132 -166
  229. package/dist/services/claim.js.map +1 -1
  230. package/dist/services/event-bus.d.ts +23 -10
  231. package/dist/services/event-bus.js +106 -42
  232. package/dist/services/event-bus.js.map +1 -1
  233. package/dist/services/fulfillment.d.ts +7 -3
  234. package/dist/services/fulfillment.js +79 -90
  235. package/dist/services/fulfillment.js.map +1 -1
  236. package/dist/services/index.d.ts +4 -1
  237. package/dist/services/index.js +10 -4
  238. package/dist/services/index.js.map +1 -1
  239. package/dist/services/line-item.d.ts +5 -5
  240. package/dist/services/line-item.js +2 -1
  241. package/dist/services/line-item.js.map +1 -1
  242. package/dist/services/order.d.ts +5 -19
  243. package/dist/services/order.js +146 -181
  244. package/dist/services/order.js.map +1 -1
  245. package/dist/services/payment-collection.d.ts +2 -2
  246. package/dist/services/payment-collection.js +88 -128
  247. package/dist/services/payment-collection.js.map +1 -1
  248. package/dist/services/payment-provider.d.ts +2 -1
  249. package/dist/services/payment-provider.js +32 -27
  250. package/dist/services/payment-provider.js.map +1 -1
  251. package/dist/services/product-category.d.ts +41 -0
  252. package/dist/services/product-category.js +190 -0
  253. package/dist/services/product-category.js.map +1 -0
  254. package/dist/services/product-variant-inventory.d.ts +115 -0
  255. package/dist/services/product-variant-inventory.js +747 -0
  256. package/dist/services/product-variant-inventory.js.map +1 -0
  257. package/dist/services/return.d.ts +7 -12
  258. package/dist/services/return.js +12 -9
  259. package/dist/services/return.js.map +1 -1
  260. package/dist/services/sales-channel-inventory.d.ts +24 -0
  261. package/dist/services/sales-channel-inventory.js +70 -0
  262. package/dist/services/sales-channel-inventory.js.map +1 -0
  263. package/dist/services/sales-channel-location.d.ts +41 -0
  264. package/dist/services/sales-channel-location.js +159 -0
  265. package/dist/services/sales-channel-location.js.map +1 -0
  266. package/dist/services/swap.d.ts +4 -4
  267. package/dist/services/swap.js +78 -127
  268. package/dist/services/swap.js.map +1 -1
  269. package/dist/services/totals.d.ts +2 -2
  270. package/dist/services/totals.js +2 -2
  271. package/dist/services/totals.js.map +1 -1
  272. package/dist/strategies/batch-jobs/product/import.d.ts +1 -1
  273. package/dist/strategies/batch-jobs/product/import.js +4 -3
  274. package/dist/strategies/batch-jobs/product/import.js.map +1 -1
  275. package/dist/strategies/cart-completion.d.ts +14 -9
  276. package/dist/strategies/cart-completion.js +111 -48
  277. package/dist/strategies/cart-completion.js.map +1 -1
  278. package/dist/subscribers/cart.d.ts +2 -4
  279. package/dist/subscribers/cart.js +8 -21
  280. package/dist/subscribers/cart.js.map +1 -1
  281. package/dist/subscribers/product.d.ts +1 -5
  282. package/dist/subscribers/product.js +15 -5
  283. package/dist/subscribers/product.js.map +1 -1
  284. package/dist/types/inventory.d.ts +106 -0
  285. package/dist/types/inventory.js +3 -0
  286. package/dist/types/inventory.js.map +1 -0
  287. package/dist/types/payment.d.ts +1 -0
  288. package/dist/types/product.d.ts +1 -0
  289. package/dist/types/product.js.map +1 -1
  290. package/dist/types/stock-location.d.ts +245 -0
  291. package/dist/types/stock-location.js +3 -0
  292. package/dist/types/stock-location.js.map +1 -0
  293. package/dist/utils/index.d.ts +1 -0
  294. package/dist/utils/index.js +1 -0
  295. package/dist/utils/index.js.map +1 -1
  296. package/dist/utils/repository.d.ts +4 -3
  297. package/dist/utils/repository.js +43 -23
  298. package/dist/utils/repository.js.map +1 -1
  299. package/package.json +2 -2
  300. package/dist/services/inventory.d.ts +0 -31
  301. package/dist/services/inventory.js +0 -151
  302. package/dist/services/inventory.js.map +0 -1
@@ -0,0 +1,747 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
29
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
30
+ return new (P || (P = Promise))(function (resolve, reject) {
31
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
32
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
33
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
34
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
35
+ });
36
+ };
37
+ var __generator = (this && this.__generator) || function (thisArg, body) {
38
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
39
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
40
+ function verb(n) { return function (v) { return step([n, v]); }; }
41
+ function step(op) {
42
+ if (f) throw new TypeError("Generator is already executing.");
43
+ while (_) try {
44
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
45
+ if (y = 0, t) op = [op[0] & 2, t.value];
46
+ switch (op[0]) {
47
+ case 0: case 1: t = op; break;
48
+ case 4: _.label++; return { value: op[1], done: false };
49
+ case 5: _.label++; y = op[1]; op = [0]; continue;
50
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
51
+ default:
52
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
53
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
54
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
55
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
56
+ if (t[2]) _.ops.pop();
57
+ _.trys.pop(); continue;
58
+ }
59
+ op = body.call(thisArg, _);
60
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
61
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
62
+ }
63
+ };
64
+ var __read = (this && this.__read) || function (o, n) {
65
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
66
+ if (!m) return o;
67
+ var i = m.call(o), r, ar = [], e;
68
+ try {
69
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
70
+ }
71
+ catch (error) { e = { error: error }; }
72
+ finally {
73
+ try {
74
+ if (r && !r.done && (m = i["return"])) m.call(i);
75
+ }
76
+ finally { if (e) throw e.error; }
77
+ }
78
+ return ar;
79
+ };
80
+ var __values = (this && this.__values) || function(o) {
81
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
82
+ if (m) return m.call(o);
83
+ if (o && typeof o.length === "number") return {
84
+ next: function () {
85
+ if (o && i >= o.length) o = void 0;
86
+ return { value: o && o[i++], done: !o };
87
+ }
88
+ };
89
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
90
+ };
91
+ Object.defineProperty(exports, "__esModule", { value: true });
92
+ var medusa_core_utils_1 = require("medusa-core-utils");
93
+ var typeorm_1 = require("typeorm");
94
+ var interfaces_1 = require("../interfaces");
95
+ var product_variant_inventory_item_1 = require("../models/product-variant-inventory-item");
96
+ var ProductVariantInventoryService = /** @class */ (function (_super) {
97
+ __extends(ProductVariantInventoryService, _super);
98
+ function ProductVariantInventoryService(_a) {
99
+ var manager = _a.manager, stockLocationService = _a.stockLocationService, salesChannelLocationService = _a.salesChannelLocationService, productVariantService = _a.productVariantService, inventoryService = _a.inventoryService;
100
+ var _this =
101
+ // eslint-disable-next-line prefer-rest-params
102
+ _super.call(this, arguments[0]) || this;
103
+ _this.manager_ = manager;
104
+ _this.salesChannelLocationService_ = salesChannelLocationService;
105
+ _this.stockLocationService_ = stockLocationService;
106
+ _this.productVariantService_ = productVariantService;
107
+ _this.inventoryService_ = inventoryService;
108
+ return _this;
109
+ }
110
+ /**
111
+ * confirms if requested inventory is available
112
+ * @param variantId id of the variant to confirm inventory for
113
+ * @param quantity quantity of inventory to confirm is available
114
+ * @param context optionally include a sales channel if applicable
115
+ * @returns boolean indicating if inventory is available
116
+ */
117
+ ProductVariantInventoryService.prototype.confirmInventory = function (variantId, quantity, context) {
118
+ if (context === void 0) { context = {}; }
119
+ return __awaiter(this, void 0, void 0, function () {
120
+ var manager, productVariant, variantInventory, locations, stockLocations, hasInventory;
121
+ var _this = this;
122
+ return __generator(this, function (_a) {
123
+ switch (_a.label) {
124
+ case 0:
125
+ if (!variantId) {
126
+ return [2 /*return*/, true];
127
+ }
128
+ manager = this.transactionManager_ || this.manager_;
129
+ return [4 /*yield*/, this.productVariantService_
130
+ .withTransaction(manager)
131
+ .retrieve(variantId, {
132
+ select: [
133
+ "id",
134
+ "allow_backorder",
135
+ "manage_inventory",
136
+ "inventory_quantity",
137
+ ],
138
+ })
139
+ // If the variant allows backorders or if inventory isn't managed we
140
+ // don't need to check inventory
141
+ ];
142
+ case 1:
143
+ productVariant = _a.sent();
144
+ // If the variant allows backorders or if inventory isn't managed we
145
+ // don't need to check inventory
146
+ if (productVariant.allow_backorder || !productVariant.manage_inventory) {
147
+ return [2 /*return*/, true];
148
+ }
149
+ if (!this.inventoryService_) {
150
+ return [2 /*return*/, productVariant.inventory_quantity >= quantity];
151
+ }
152
+ return [4 /*yield*/, this.listByVariant(variantId)
153
+ // If there are no inventory items attached to the variant we default
154
+ // to true
155
+ ];
156
+ case 2:
157
+ variantInventory = _a.sent();
158
+ // If there are no inventory items attached to the variant we default
159
+ // to true
160
+ if (variantInventory.length === 0) {
161
+ return [2 /*return*/, true];
162
+ }
163
+ locations = [];
164
+ if (!context.salesChannelId) return [3 /*break*/, 4];
165
+ return [4 /*yield*/, this.salesChannelLocationService_.listLocations(context.salesChannelId)];
166
+ case 3:
167
+ locations = _a.sent();
168
+ return [3 /*break*/, 6];
169
+ case 4: return [4 /*yield*/, this.stockLocationService_.list({}, { select: ["id"] })];
170
+ case 5:
171
+ stockLocations = _a.sent();
172
+ locations = stockLocations.map(function (l) { return l.id; });
173
+ _a.label = 6;
174
+ case 6: return [4 /*yield*/, Promise.all(variantInventory.map(function (inventoryPart) { return __awaiter(_this, void 0, void 0, function () {
175
+ var itemQuantity;
176
+ return __generator(this, function (_a) {
177
+ switch (_a.label) {
178
+ case 0:
179
+ itemQuantity = inventoryPart.quantity * quantity;
180
+ return [4 /*yield*/, this.inventoryService_.confirmInventory(inventoryPart.inventory_item_id, locations, itemQuantity)];
181
+ case 1: return [2 /*return*/, _a.sent()];
182
+ }
183
+ });
184
+ }); }))];
185
+ case 7:
186
+ hasInventory = _a.sent();
187
+ return [2 /*return*/, hasInventory.every(Boolean)];
188
+ }
189
+ });
190
+ });
191
+ };
192
+ /**
193
+ * Retrieves a product variant inventory item by its inventory item ID and variant ID.
194
+ *
195
+ * @param inventoryItemId - The ID of the inventory item to retrieve.
196
+ * @param variantId - The ID of the variant to retrieve.
197
+ * @returns A promise that resolves with the product variant inventory item.
198
+ */
199
+ ProductVariantInventoryService.prototype.retrieve = function (inventoryItemId, variantId) {
200
+ return __awaiter(this, void 0, void 0, function () {
201
+ var manager, variantInventoryRepo, variantInventory;
202
+ return __generator(this, function (_a) {
203
+ switch (_a.label) {
204
+ case 0:
205
+ manager = this.transactionManager_ || this.manager_;
206
+ variantInventoryRepo = manager.getRepository(product_variant_inventory_item_1.ProductVariantInventoryItem);
207
+ return [4 /*yield*/, variantInventoryRepo.findOne({
208
+ where: { inventory_item_id: inventoryItemId, variant_id: variantId },
209
+ })];
210
+ case 1:
211
+ variantInventory = _a.sent();
212
+ if (!variantInventory) {
213
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_FOUND, "Inventory item with id ".concat(inventoryItemId, " not found"));
214
+ }
215
+ return [2 /*return*/, variantInventory];
216
+ }
217
+ });
218
+ });
219
+ };
220
+ /**
221
+ * list registered inventory items
222
+ * @param itemIds list inventory item ids
223
+ * @returns list of inventory items
224
+ */
225
+ ProductVariantInventoryService.prototype.listByItem = function (itemIds) {
226
+ return __awaiter(this, void 0, void 0, function () {
227
+ var manager, variantInventoryRepo, variantInventory;
228
+ return __generator(this, function (_a) {
229
+ switch (_a.label) {
230
+ case 0:
231
+ manager = this.transactionManager_ || this.manager_;
232
+ variantInventoryRepo = manager.getRepository(product_variant_inventory_item_1.ProductVariantInventoryItem);
233
+ return [4 /*yield*/, variantInventoryRepo.find({
234
+ where: { inventory_item_id: (0, typeorm_1.In)(itemIds) },
235
+ })];
236
+ case 1:
237
+ variantInventory = _a.sent();
238
+ return [2 /*return*/, variantInventory];
239
+ }
240
+ });
241
+ });
242
+ };
243
+ /**
244
+ * List inventory items for a specific variant
245
+ * @param variantId variant id
246
+ * @returns variant inventory items for the variant id
247
+ */
248
+ ProductVariantInventoryService.prototype.listByVariant = function (variantId) {
249
+ return __awaiter(this, void 0, void 0, function () {
250
+ var manager, variantInventoryRepo, ids, variantInventory;
251
+ return __generator(this, function (_a) {
252
+ switch (_a.label) {
253
+ case 0:
254
+ manager = this.transactionManager_ || this.manager_;
255
+ variantInventoryRepo = manager.getRepository(product_variant_inventory_item_1.ProductVariantInventoryItem);
256
+ ids = Array.isArray(variantId) ? variantId : [variantId];
257
+ return [4 /*yield*/, variantInventoryRepo.find({
258
+ where: { variant_id: (0, typeorm_1.In)(ids) },
259
+ })];
260
+ case 1:
261
+ variantInventory = _a.sent();
262
+ return [2 /*return*/, variantInventory];
263
+ }
264
+ });
265
+ });
266
+ };
267
+ /**
268
+ * lists variant by inventory item id
269
+ * @param itemId item id
270
+ * @returns a list of product variants that are associated with the item id
271
+ */
272
+ ProductVariantInventoryService.prototype.listVariantsByItem = function (itemId) {
273
+ return __awaiter(this, void 0, void 0, function () {
274
+ var variantInventory, items;
275
+ return __generator(this, function (_a) {
276
+ switch (_a.label) {
277
+ case 0:
278
+ if (!this.inventoryService_) {
279
+ return [2 /*return*/, []];
280
+ }
281
+ return [4 /*yield*/, this.listByItem([itemId])];
282
+ case 1:
283
+ variantInventory = _a.sent();
284
+ return [4 /*yield*/, this.productVariantService_.list({
285
+ id: variantInventory.map(function (i) { return i.variant_id; }),
286
+ })];
287
+ case 2:
288
+ items = _a.sent();
289
+ return [2 /*return*/, items];
290
+ }
291
+ });
292
+ });
293
+ };
294
+ /**
295
+ * lists inventory items for a given variant
296
+ * @param variantId variant id
297
+ * @returns lidt of inventory items for the variant
298
+ */
299
+ ProductVariantInventoryService.prototype.listInventoryItemsByVariant = function (variantId) {
300
+ return __awaiter(this, void 0, void 0, function () {
301
+ var variantInventory, _a, items;
302
+ return __generator(this, function (_b) {
303
+ switch (_b.label) {
304
+ case 0:
305
+ if (!this.inventoryService_) {
306
+ return [2 /*return*/, []];
307
+ }
308
+ return [4 /*yield*/, this.listByVariant(variantId)];
309
+ case 1:
310
+ variantInventory = _b.sent();
311
+ return [4 /*yield*/, this.inventoryService_.listInventoryItems({
312
+ id: variantInventory.map(function (i) { return i.inventory_item_id; }),
313
+ })];
314
+ case 2:
315
+ _a = __read.apply(void 0, [_b.sent(), 1]), items = _a[0];
316
+ return [2 /*return*/, items];
317
+ }
318
+ });
319
+ });
320
+ };
321
+ /**
322
+ * Attach a variant to an inventory item
323
+ * @param variantId variant id
324
+ * @param inventoryItemId inventory item id
325
+ * @param quantity quantity of variant to attach
326
+ * @returns the variant inventory item
327
+ */
328
+ ProductVariantInventoryService.prototype.attachInventoryItem = function (variantId, inventoryItemId, quantity) {
329
+ return __awaiter(this, void 0, void 0, function () {
330
+ var manager, variantInventoryRepo, existing, quantityToStore, variantInventory;
331
+ return __generator(this, function (_a) {
332
+ switch (_a.label) {
333
+ case 0:
334
+ manager = this.transactionManager_ || this.manager_;
335
+ // Verify that variant exists
336
+ return [4 /*yield*/, this.productVariantService_
337
+ .withTransaction(manager)
338
+ .retrieve(variantId, {
339
+ select: ["id"],
340
+ })
341
+ // Verify that item exists
342
+ ];
343
+ case 1:
344
+ // Verify that variant exists
345
+ _a.sent();
346
+ // Verify that item exists
347
+ return [4 /*yield*/, this.inventoryService_.retrieveInventoryItem(inventoryItemId, {
348
+ select: ["id"],
349
+ })];
350
+ case 2:
351
+ // Verify that item exists
352
+ _a.sent();
353
+ variantInventoryRepo = manager.getRepository(product_variant_inventory_item_1.ProductVariantInventoryItem);
354
+ return [4 /*yield*/, variantInventoryRepo.findOne({
355
+ where: {
356
+ variant_id: variantId,
357
+ inventory_item_id: inventoryItemId,
358
+ },
359
+ })];
360
+ case 3:
361
+ existing = _a.sent();
362
+ if (existing) {
363
+ return [2 /*return*/, existing];
364
+ }
365
+ quantityToStore = 1;
366
+ if (typeof quantity !== "undefined") {
367
+ if (quantity < 1) {
368
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Quantity must be greater than 0");
369
+ }
370
+ else {
371
+ quantityToStore = quantity;
372
+ }
373
+ }
374
+ variantInventory = variantInventoryRepo.create({
375
+ variant_id: variantId,
376
+ inventory_item_id: inventoryItemId,
377
+ quantity: quantityToStore,
378
+ });
379
+ return [4 /*yield*/, variantInventoryRepo.save(variantInventory)];
380
+ case 4: return [2 /*return*/, _a.sent()];
381
+ }
382
+ });
383
+ });
384
+ };
385
+ /**
386
+ * Remove a variant from an inventory item
387
+ * @param variantId variant id
388
+ * @param inventoryItemId inventory item id
389
+ */
390
+ ProductVariantInventoryService.prototype.detachInventoryItem = function (variantId, inventoryItemId) {
391
+ return __awaiter(this, void 0, void 0, function () {
392
+ var manager, variantInventoryRepo, existing;
393
+ return __generator(this, function (_a) {
394
+ switch (_a.label) {
395
+ case 0:
396
+ manager = this.transactionManager_ || this.manager_;
397
+ variantInventoryRepo = manager.getRepository(product_variant_inventory_item_1.ProductVariantInventoryItem);
398
+ return [4 /*yield*/, variantInventoryRepo.findOne({
399
+ where: {
400
+ variant_id: variantId,
401
+ inventory_item_id: inventoryItemId,
402
+ },
403
+ })];
404
+ case 1:
405
+ existing = _a.sent();
406
+ if (!existing) return [3 /*break*/, 3];
407
+ return [4 /*yield*/, variantInventoryRepo.remove(existing)];
408
+ case 2:
409
+ _a.sent();
410
+ _a.label = 3;
411
+ case 3: return [2 /*return*/];
412
+ }
413
+ });
414
+ });
415
+ };
416
+ /**
417
+ * Reserves a quantity of a variant
418
+ * @param variantId variant id
419
+ * @param quantity quantity to reserve
420
+ * @param context optional parameters
421
+ */
422
+ ProductVariantInventoryService.prototype.reserveQuantity = function (variantId, quantity, context) {
423
+ if (context === void 0) { context = {}; }
424
+ return __awaiter(this, void 0, void 0, function () {
425
+ var manager, toReserve, variantInventory, locationId, locations;
426
+ var _this = this;
427
+ return __generator(this, function (_a) {
428
+ switch (_a.label) {
429
+ case 0:
430
+ manager = this.transactionManager_ || this.manager_;
431
+ if (!this.inventoryService_) {
432
+ return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
433
+ var variantServiceTx, variant;
434
+ return __generator(this, function (_a) {
435
+ switch (_a.label) {
436
+ case 0:
437
+ variantServiceTx = this.productVariantService_.withTransaction(manager);
438
+ return [4 /*yield*/, variantServiceTx.retrieve(variantId, {
439
+ select: ["id", "inventory_quantity"],
440
+ })];
441
+ case 1:
442
+ variant = _a.sent();
443
+ return [4 /*yield*/, variantServiceTx.update(variant.id, {
444
+ inventory_quantity: variant.inventory_quantity - quantity,
445
+ })];
446
+ case 2:
447
+ _a.sent();
448
+ return [2 /*return*/];
449
+ }
450
+ });
451
+ }); })];
452
+ }
453
+ toReserve = {
454
+ type: "order",
455
+ line_item_id: context.lineItemId,
456
+ };
457
+ return [4 /*yield*/, this.listByVariant(variantId)];
458
+ case 1:
459
+ variantInventory = _a.sent();
460
+ if (variantInventory.length === 0) {
461
+ return [2 /*return*/];
462
+ }
463
+ locationId = context.locationId;
464
+ if (!(!(0, medusa_core_utils_1.isDefined)(locationId) && context.salesChannelId)) return [3 /*break*/, 3];
465
+ return [4 /*yield*/, this.salesChannelLocationService_
466
+ .withTransaction(manager)
467
+ .listLocations(context.salesChannelId)];
468
+ case 2:
469
+ locations = _a.sent();
470
+ if (!locations.length) {
471
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, "Must provide location_id or sales_channel_id to a Sales Channel that has associated Stock Locations");
472
+ }
473
+ locationId = locations[0];
474
+ _a.label = 3;
475
+ case 3: return [4 /*yield*/, Promise.all(variantInventory.map(function (inventoryPart) { return __awaiter(_this, void 0, void 0, function () {
476
+ var itemQuantity;
477
+ return __generator(this, function (_a) {
478
+ switch (_a.label) {
479
+ case 0:
480
+ itemQuantity = inventoryPart.quantity * quantity;
481
+ return [4 /*yield*/, this.inventoryService_.createReservationItem(__assign(__assign({}, toReserve), { location_id: locationId, inventory_item_id: inventoryPart.inventory_item_id, quantity: itemQuantity }))];
482
+ case 1: return [2 /*return*/, _a.sent()];
483
+ }
484
+ });
485
+ }); }))];
486
+ case 4:
487
+ _a.sent();
488
+ return [2 /*return*/];
489
+ }
490
+ });
491
+ });
492
+ };
493
+ /**
494
+ * Adjusts the quantity of reservations for a line item by a given amount.
495
+ * @param {string} lineItemId - The ID of the line item
496
+ * @param {string} variantId - The ID of the variant
497
+ * @param {string} locationId - The ID of the location to prefer adjusting quantities at
498
+ * @param {number} quantity - The amount to adjust the quantity by
499
+ */
500
+ ProductVariantInventoryService.prototype.adjustReservationsQuantityByLineItem = function (lineItemId, variantId, locationId, quantity) {
501
+ var _a;
502
+ return __awaiter(this, void 0, void 0, function () {
503
+ var _b, reservations, reservationCount, reservation, productVariantInventory, reservationQtyUpdate;
504
+ var _this = this;
505
+ return __generator(this, function (_c) {
506
+ switch (_c.label) {
507
+ case 0:
508
+ if (!this.inventoryService_) {
509
+ return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
510
+ var variantServiceTx, variant;
511
+ return __generator(this, function (_a) {
512
+ switch (_a.label) {
513
+ case 0:
514
+ variantServiceTx = this.productVariantService_.withTransaction(manager);
515
+ return [4 /*yield*/, variantServiceTx.retrieve(variantId, {
516
+ select: ["id", "inventory_quantity", "manage_inventory"],
517
+ })];
518
+ case 1:
519
+ variant = _a.sent();
520
+ if (!variant.manage_inventory) {
521
+ return [2 /*return*/];
522
+ }
523
+ return [4 /*yield*/, variantServiceTx.update(variantId, {
524
+ inventory_quantity: variant.inventory_quantity - quantity,
525
+ })];
526
+ case 2:
527
+ _a.sent();
528
+ return [2 /*return*/];
529
+ }
530
+ });
531
+ }); })];
532
+ }
533
+ return [4 /*yield*/, this.inventoryService_.listReservationItems({
534
+ line_item_id: lineItemId,
535
+ }, {
536
+ order: { created_at: "DESC" },
537
+ })];
538
+ case 1:
539
+ _b = __read.apply(void 0, [_c.sent(), 2]), reservations = _b[0], reservationCount = _b[1];
540
+ if (!reservationCount) return [3 /*break*/, 6];
541
+ reservation = reservations[0];
542
+ reservation =
543
+ (_a = reservations.find(function (r) { return r.location_id === locationId && r.quantity >= quantity; })) !== null && _a !== void 0 ? _a : reservation;
544
+ return [4 /*yield*/, this.retrieve(reservation.inventory_item_id, variantId)];
545
+ case 2:
546
+ productVariantInventory = _c.sent();
547
+ reservationQtyUpdate = reservation.quantity - quantity * productVariantInventory.quantity;
548
+ if (!(reservationQtyUpdate === 0)) return [3 /*break*/, 4];
549
+ return [4 /*yield*/, this.inventoryService_.deleteReservationItem(reservation.id)];
550
+ case 3:
551
+ _c.sent();
552
+ return [3 /*break*/, 6];
553
+ case 4: return [4 /*yield*/, this.inventoryService_.updateReservationItem(reservation.id, {
554
+ quantity: reservationQtyUpdate,
555
+ })];
556
+ case 5:
557
+ _c.sent();
558
+ _c.label = 6;
559
+ case 6: return [2 /*return*/];
560
+ }
561
+ });
562
+ });
563
+ };
564
+ /**
565
+ * Validate stock at a location for fulfillment items
566
+ * @param items Fulfillment Line items to validate quantities for
567
+ * @param locationId Location to validate stock at
568
+ * @returns nothing if successful, throws error if not
569
+ */
570
+ ProductVariantInventoryService.prototype.validateInventoryAtLocation = function (items, locationId) {
571
+ return __awaiter(this, void 0, void 0, function () {
572
+ var itemsToValidate, itemsToValidate_1, itemsToValidate_1_1, item, pvInventoryItems, _a, inventoryLevels, pviMap, inventoryLevels_1, inventoryLevels_1_1, inventoryLevel, pvInventoryItem, e_1_1;
573
+ var e_1, _b, e_2, _c;
574
+ return __generator(this, function (_d) {
575
+ switch (_d.label) {
576
+ case 0:
577
+ if (!this.inventoryService_) {
578
+ return [2 /*return*/];
579
+ }
580
+ itemsToValidate = items.filter(function (item) { return item.variant_id; });
581
+ _d.label = 1;
582
+ case 1:
583
+ _d.trys.push([1, 7, 8, 9]);
584
+ itemsToValidate_1 = __values(itemsToValidate), itemsToValidate_1_1 = itemsToValidate_1.next();
585
+ _d.label = 2;
586
+ case 2:
587
+ if (!!itemsToValidate_1_1.done) return [3 /*break*/, 6];
588
+ item = itemsToValidate_1_1.value;
589
+ return [4 /*yield*/, this.listByVariant(item.variant_id)];
590
+ case 3:
591
+ pvInventoryItems = _d.sent();
592
+ return [4 /*yield*/, this.inventoryService_.listInventoryLevels({
593
+ inventory_item_id: pvInventoryItems.map(function (i) { return i.inventory_item_id; }),
594
+ location_id: locationId,
595
+ })];
596
+ case 4:
597
+ _a = __read.apply(void 0, [_d.sent(), 1]), inventoryLevels = _a[0];
598
+ pviMap = new Map(pvInventoryItems.map(function (pvi) { return [pvi.inventory_item_id, pvi]; }));
599
+ try {
600
+ for (inventoryLevels_1 = (e_2 = void 0, __values(inventoryLevels)), inventoryLevels_1_1 = inventoryLevels_1.next(); !inventoryLevels_1_1.done; inventoryLevels_1_1 = inventoryLevels_1.next()) {
601
+ inventoryLevel = inventoryLevels_1_1.value;
602
+ pvInventoryItem = pviMap[inventoryLevel.inventory_item_id];
603
+ if (!pvInventoryItem ||
604
+ pvInventoryItem.quantity * item.quantity >
605
+ inventoryLevel.stocked_quantity) {
606
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.NOT_ALLOWED, "Insufficient stock for item: ".concat(item.title));
607
+ }
608
+ }
609
+ }
610
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
611
+ finally {
612
+ try {
613
+ if (inventoryLevels_1_1 && !inventoryLevels_1_1.done && (_c = inventoryLevels_1.return)) _c.call(inventoryLevels_1);
614
+ }
615
+ finally { if (e_2) throw e_2.error; }
616
+ }
617
+ _d.label = 5;
618
+ case 5:
619
+ itemsToValidate_1_1 = itemsToValidate_1.next();
620
+ return [3 /*break*/, 2];
621
+ case 6: return [3 /*break*/, 9];
622
+ case 7:
623
+ e_1_1 = _d.sent();
624
+ e_1 = { error: e_1_1 };
625
+ return [3 /*break*/, 9];
626
+ case 8:
627
+ try {
628
+ if (itemsToValidate_1_1 && !itemsToValidate_1_1.done && (_b = itemsToValidate_1.return)) _b.call(itemsToValidate_1);
629
+ }
630
+ finally { if (e_1) throw e_1.error; }
631
+ return [7 /*endfinally*/];
632
+ case 9: return [2 /*return*/];
633
+ }
634
+ });
635
+ });
636
+ };
637
+ /**
638
+ * delete a reservation of variant quantity
639
+ * @param lineItemId line item id
640
+ * @param variantId variant id
641
+ * @param quantity quantity to release
642
+ */
643
+ ProductVariantInventoryService.prototype.deleteReservationsByLineItem = function (lineItemId, variantId, quantity) {
644
+ return __awaiter(this, void 0, void 0, function () {
645
+ var _this = this;
646
+ return __generator(this, function (_a) {
647
+ switch (_a.label) {
648
+ case 0:
649
+ if (!this.inventoryService_) {
650
+ return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
651
+ var productVariantServiceTx, variant;
652
+ return __generator(this, function (_a) {
653
+ switch (_a.label) {
654
+ case 0:
655
+ productVariantServiceTx = this.productVariantService_.withTransaction(manager);
656
+ return [4 /*yield*/, productVariantServiceTx.retrieve(variantId, {
657
+ select: ["id", "inventory_quantity", "manage_inventory"],
658
+ })];
659
+ case 1:
660
+ variant = _a.sent();
661
+ if (!variant.manage_inventory) {
662
+ return [2 /*return*/];
663
+ }
664
+ return [4 /*yield*/, productVariantServiceTx.update(variantId, {
665
+ inventory_quantity: variant.inventory_quantity + quantity,
666
+ })];
667
+ case 2:
668
+ _a.sent();
669
+ return [2 /*return*/];
670
+ }
671
+ });
672
+ }); })];
673
+ }
674
+ return [4 /*yield*/, this.inventoryService_.deleteReservationItemsByLineItem(lineItemId)];
675
+ case 1:
676
+ _a.sent();
677
+ return [2 /*return*/];
678
+ }
679
+ });
680
+ });
681
+ };
682
+ /**
683
+ * Adjusts inventory of a variant on a location
684
+ * @param variantId variant id
685
+ * @param locationId location id
686
+ * @param quantity quantity to adjust
687
+ */
688
+ ProductVariantInventoryService.prototype.adjustInventory = function (variantId, locationId, quantity) {
689
+ return __awaiter(this, void 0, void 0, function () {
690
+ var variantInventory;
691
+ var _this = this;
692
+ return __generator(this, function (_a) {
693
+ switch (_a.label) {
694
+ case 0:
695
+ if (!!this.inventoryService_) return [3 /*break*/, 1];
696
+ return [2 /*return*/, this.atomicPhase_(function (manager) { return __awaiter(_this, void 0, void 0, function () {
697
+ var productVariantServiceTx, variant;
698
+ return __generator(this, function (_a) {
699
+ switch (_a.label) {
700
+ case 0:
701
+ productVariantServiceTx = this.productVariantService_.withTransaction(manager);
702
+ return [4 /*yield*/, productVariantServiceTx.retrieve(variantId, {
703
+ select: ["id", "inventory_quantity", "manage_inventory"],
704
+ })];
705
+ case 1:
706
+ variant = _a.sent();
707
+ if (!variant.manage_inventory) {
708
+ return [2 /*return*/];
709
+ }
710
+ return [4 /*yield*/, productVariantServiceTx.update(variantId, {
711
+ inventory_quantity: variant.inventory_quantity + quantity,
712
+ })];
713
+ case 2:
714
+ _a.sent();
715
+ return [2 /*return*/];
716
+ }
717
+ });
718
+ }); })];
719
+ case 1: return [4 /*yield*/, this.listByVariant(variantId)];
720
+ case 2:
721
+ variantInventory = _a.sent();
722
+ if (variantInventory.length === 0) {
723
+ return [2 /*return*/];
724
+ }
725
+ return [4 /*yield*/, Promise.all(variantInventory.map(function (inventoryPart) { return __awaiter(_this, void 0, void 0, function () {
726
+ var itemQuantity;
727
+ return __generator(this, function (_a) {
728
+ switch (_a.label) {
729
+ case 0:
730
+ itemQuantity = inventoryPart.quantity * quantity;
731
+ return [4 /*yield*/, this.inventoryService_.adjustInventory(inventoryPart.inventory_item_id, locationId, itemQuantity)];
732
+ case 1: return [2 /*return*/, _a.sent()];
733
+ }
734
+ });
735
+ }); }))];
736
+ case 3:
737
+ _a.sent();
738
+ _a.label = 4;
739
+ case 4: return [2 /*return*/];
740
+ }
741
+ });
742
+ });
743
+ };
744
+ return ProductVariantInventoryService;
745
+ }(interfaces_1.TransactionBaseService));
746
+ exports.default = ProductVariantInventoryService;
747
+ //# sourceMappingURL=product-variant-inventory.js.map