@medusajs/medusa 1.1.63 → 1.2.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 (293) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/dist/api/index.d.ts +2 -0
  3. package/dist/api/index.js +2 -0
  4. package/dist/api/index.js.map +1 -1
  5. package/dist/api/routes/admin/collections/add-products.d.ts +32 -0
  6. package/dist/api/routes/admin/{product-tags/update-collection.js → collections/add-products.js} +25 -45
  7. package/dist/api/routes/admin/collections/add-products.js.map +1 -0
  8. package/dist/api/routes/admin/collections/index.d.ts +2 -0
  9. package/dist/api/routes/admin/collections/index.js +11 -1
  10. package/dist/api/routes/admin/collections/index.js.map +1 -1
  11. package/dist/api/routes/admin/collections/remove-products.d.ts +32 -0
  12. package/dist/api/routes/admin/{product-tags/create-collection.js → collections/remove-products.js} +35 -49
  13. package/dist/api/routes/admin/collections/remove-products.js.map +1 -0
  14. package/dist/api/routes/admin/customer-groups/create-customer-group.d.ts +27 -0
  15. package/dist/api/routes/admin/customer-groups/create-customer-group.js +103 -0
  16. package/dist/api/routes/admin/customer-groups/create-customer-group.js.map +1 -0
  17. package/dist/api/routes/admin/customer-groups/delete-customer-group.d.ts +28 -0
  18. package/dist/api/routes/admin/{product-tags/delete-collection.js → customer-groups/delete-customer-group.js} +25 -25
  19. package/dist/api/routes/admin/customer-groups/delete-customer-group.js.map +1 -0
  20. package/dist/api/routes/admin/customer-groups/delete-customers-batch.d.ts +27 -0
  21. package/dist/api/routes/admin/customer-groups/delete-customers-batch.js +106 -0
  22. package/dist/api/routes/admin/customer-groups/delete-customers-batch.js.map +1 -0
  23. package/dist/api/routes/admin/customer-groups/get-customer-group.d.ts +25 -0
  24. package/dist/api/routes/admin/customer-groups/get-customer-group.js +113 -0
  25. package/dist/api/routes/admin/customer-groups/get-customer-group.js.map +1 -0
  26. package/dist/api/routes/admin/customer-groups/index.d.ts +13 -0
  27. package/dist/api/routes/admin/customer-groups/index.js +31 -0
  28. package/dist/api/routes/admin/customer-groups/index.js.map +1 -0
  29. package/dist/api/routes/admin/customer-groups/update-customer-group.d.ts +28 -0
  30. package/dist/api/routes/admin/customer-groups/update-customer-group.js +124 -0
  31. package/dist/api/routes/admin/customer-groups/update-customer-group.js.map +1 -0
  32. package/dist/api/routes/admin/customers/get-customer.js +18 -5
  33. package/dist/api/routes/admin/customers/get-customer.js.map +1 -1
  34. package/dist/api/routes/admin/customers/index.d.ts +1 -0
  35. package/dist/api/routes/admin/customers/index.js +2 -0
  36. package/dist/api/routes/admin/customers/index.js.map +1 -1
  37. package/dist/api/routes/admin/customers/update-customer.d.ts +14 -0
  38. package/dist/api/routes/admin/customers/update-customer.js +49 -10
  39. package/dist/api/routes/admin/customers/update-customer.js.map +1 -1
  40. package/dist/api/routes/admin/draft-orders/create-draft-order.d.ts +3 -3
  41. package/dist/api/routes/admin/draft-orders/create-draft-order.js.map +1 -1
  42. package/dist/api/routes/admin/draft-orders/create-line-item.d.ts +1 -1
  43. package/dist/api/routes/admin/draft-orders/create-line-item.js.map +1 -1
  44. package/dist/api/routes/admin/draft-orders/update-line-item.d.ts +1 -1
  45. package/dist/api/routes/admin/draft-orders/update-line-item.js.map +1 -1
  46. package/dist/api/routes/admin/index.js +6 -0
  47. package/dist/api/routes/admin/index.js.map +1 -1
  48. package/dist/api/routes/admin/orders/add-shipping-method.d.ts +1 -1
  49. package/dist/api/routes/admin/orders/add-shipping-method.js.map +1 -1
  50. package/dist/api/routes/admin/orders/create-claim.js +9 -1
  51. package/dist/api/routes/admin/orders/create-claim.js.map +1 -1
  52. package/dist/api/routes/admin/orders/create-swap-shipment.js +1 -0
  53. package/dist/api/routes/admin/orders/create-swap-shipment.js.map +1 -1
  54. package/dist/api/routes/admin/orders/create-swap.js +7 -1
  55. package/dist/api/routes/admin/orders/create-swap.js.map +1 -1
  56. package/dist/api/routes/admin/orders/index.d.ts +0 -1
  57. package/dist/api/routes/admin/orders/index.js +2 -5
  58. package/dist/api/routes/admin/orders/index.js.map +1 -1
  59. package/dist/api/routes/admin/product-types/index.d.ts +15 -0
  60. package/dist/api/routes/admin/product-types/index.js +40 -0
  61. package/dist/api/routes/admin/product-types/index.js.map +1 -0
  62. package/dist/api/routes/admin/product-types/list-product-types.d.ts +39 -0
  63. package/dist/api/routes/admin/{product-tags/list-collections.js → product-types/list-product-types.js} +72 -76
  64. package/dist/api/routes/admin/product-types/list-product-types.js.map +1 -0
  65. package/dist/api/routes/admin/regions/index.js +3 -0
  66. package/dist/api/routes/admin/regions/index.js.map +1 -1
  67. package/dist/api/routes/admin/regions/list-regions.d.ts +27 -3
  68. package/dist/api/routes/admin/regions/list-regions.js +91 -11
  69. package/dist/api/routes/admin/regions/list-regions.js.map +1 -1
  70. package/dist/api/routes/admin/regions/update-region.d.ts +12 -0
  71. package/dist/api/routes/admin/regions/update-region.js +24 -0
  72. package/dist/api/routes/admin/regions/update-region.js.map +1 -1
  73. package/dist/api/routes/admin/shipping-options/index.d.ts +2 -2
  74. package/dist/api/routes/admin/shipping-options/list-shipping-options.js +23 -7
  75. package/dist/api/routes/admin/shipping-options/list-shipping-options.js.map +1 -1
  76. package/dist/api/routes/admin/store/index.d.ts +4 -1
  77. package/dist/api/routes/admin/store/index.js +1 -0
  78. package/dist/api/routes/admin/store/index.js.map +1 -1
  79. package/dist/api/routes/admin/store/list-tax-providers.d.ts +22 -0
  80. package/dist/api/routes/admin/{product-tags/get-collection.js → store/list-tax-providers.js} +21 -22
  81. package/dist/api/routes/admin/store/list-tax-providers.js.map +1 -0
  82. package/dist/api/routes/admin/swaps/index.js +0 -6
  83. package/dist/api/routes/admin/swaps/index.js.map +1 -1
  84. package/dist/api/routes/admin/tax-rates/add-to-product-types.d.ts +1 -1
  85. package/dist/api/routes/admin/tax-rates/add-to-product-types.js +4 -59
  86. package/dist/api/routes/admin/tax-rates/add-to-product-types.js.map +1 -1
  87. package/dist/api/routes/admin/tax-rates/add-to-products.d.ts +1 -1
  88. package/dist/api/routes/admin/tax-rates/add-to-products.js +4 -55
  89. package/dist/api/routes/admin/tax-rates/add-to-products.js.map +1 -1
  90. package/dist/api/routes/admin/tax-rates/add-to-shipping-options.d.ts +1 -1
  91. package/dist/api/routes/admin/tax-rates/add-to-shipping-options.js +4 -59
  92. package/dist/api/routes/admin/tax-rates/add-to-shipping-options.js.map +1 -1
  93. package/dist/api/routes/admin/tax-rates/create-tax-rate.js +14 -113
  94. package/dist/api/routes/admin/tax-rates/create-tax-rate.js.map +1 -1
  95. package/dist/api/routes/admin/tax-rates/index.d.ts +3 -0
  96. package/dist/api/routes/admin/tax-rates/index.js +18 -3
  97. package/dist/api/routes/admin/tax-rates/index.js.map +1 -1
  98. package/dist/api/routes/admin/tax-rates/list-tax-rates.d.ts +2 -2
  99. package/dist/api/routes/admin/tax-rates/list-tax-rates.js.map +1 -1
  100. package/dist/api/routes/admin/tax-rates/remove-from-product-types.d.ts +29 -0
  101. package/dist/api/routes/admin/tax-rates/remove-from-product-types.js +122 -0
  102. package/dist/api/routes/admin/tax-rates/remove-from-product-types.js.map +1 -0
  103. package/dist/api/routes/admin/tax-rates/remove-from-products.d.ts +29 -0
  104. package/dist/api/routes/admin/tax-rates/remove-from-products.js +122 -0
  105. package/dist/api/routes/admin/tax-rates/remove-from-products.js.map +1 -0
  106. package/dist/api/routes/admin/tax-rates/remove-from-shipping-options.d.ts +29 -0
  107. package/dist/api/routes/admin/tax-rates/remove-from-shipping-options.js +122 -0
  108. package/dist/api/routes/admin/tax-rates/remove-from-shipping-options.js.map +1 -0
  109. package/dist/api/routes/admin/tax-rates/update-tax-rate.js +15 -116
  110. package/dist/api/routes/admin/tax-rates/update-tax-rate.js.map +1 -1
  111. package/dist/api/routes/store/carts/add-shipping-method.d.ts +1 -1
  112. package/dist/api/routes/store/carts/add-shipping-method.js.map +1 -1
  113. package/dist/api/routes/store/carts/complete-cart.js +11 -247
  114. package/dist/api/routes/store/carts/complete-cart.js.map +1 -1
  115. package/dist/api/routes/store/carts/index.js +2 -0
  116. package/dist/api/routes/store/carts/index.js.map +1 -1
  117. package/dist/api/routes/store/swaps/create-swap.js +7 -1
  118. package/dist/api/routes/store/swaps/create-swap.js.map +1 -1
  119. package/dist/db-subscribers/region.d.ts +6 -0
  120. package/dist/db-subscribers/region.js +84 -0
  121. package/dist/db-subscribers/region.js.map +1 -0
  122. package/dist/entity-subscribers/region.d.ts +6 -0
  123. package/dist/entity-subscribers/region.js +27 -0
  124. package/dist/entity-subscribers/region.js.map +1 -0
  125. package/dist/helpers/test-request.js +2 -0
  126. package/dist/helpers/test-request.js.map +1 -1
  127. package/dist/index.d.ts +59 -50
  128. package/dist/index.js +60 -101
  129. package/dist/index.js.map +1 -1
  130. package/dist/loaders/db-subscribers.d.ts +6 -0
  131. package/dist/loaders/db-subscribers.js +53 -0
  132. package/dist/loaders/db-subscribers.js.map +1 -0
  133. package/dist/loaders/index.d.ts +2 -1
  134. package/dist/loaders/index.js +17 -4
  135. package/dist/loaders/index.js.map +1 -1
  136. package/dist/loaders/models.js.map +1 -1
  137. package/dist/loaders/passport.js.map +1 -1
  138. package/dist/loaders/plugins.js +26 -0
  139. package/dist/loaders/plugins.js.map +1 -1
  140. package/dist/loaders/services.d.ts +2 -1
  141. package/dist/loaders/services.js +9 -7
  142. package/dist/loaders/services.js.map +1 -1
  143. package/dist/migrations/1641636508055-new_tax_system.js +9 -9
  144. package/dist/migrations/1641636508055-new_tax_system.js.map +1 -1
  145. package/dist/migrations/1644943746861-customer_groups.d.ts +6 -0
  146. package/dist/migrations/1644943746861-customer_groups.js +107 -0
  147. package/dist/migrations/1644943746861-customer_groups.js.map +1 -0
  148. package/dist/models/cart.d.ts +2 -1
  149. package/dist/models/cart.js +1 -0
  150. package/dist/models/cart.js.map +1 -1
  151. package/dist/models/customer-group.d.ts +11 -0
  152. package/dist/models/customer-group.js +81 -0
  153. package/dist/models/customer-group.js.map +1 -0
  154. package/dist/models/customer.d.ts +2 -0
  155. package/dist/models/customer.js +18 -1
  156. package/dist/models/customer.js.map +1 -1
  157. package/dist/models/line-item.d.ts +3 -0
  158. package/dist/models/line-item.js +9 -0
  159. package/dist/models/line-item.js.map +1 -1
  160. package/dist/models/order.d.ts +2 -1
  161. package/dist/models/order.js +3 -2
  162. package/dist/models/order.js.map +1 -1
  163. package/dist/models/product-tax-rate.d.ts +0 -1
  164. package/dist/models/product-tax-rate.js +5 -7
  165. package/dist/models/product-tax-rate.js.map +1 -1
  166. package/dist/models/product-type-tax-rate.d.ts +0 -1
  167. package/dist/models/product-type-tax-rate.js +2 -6
  168. package/dist/models/product-type-tax-rate.js.map +1 -1
  169. package/dist/models/product.d.ts +1 -1
  170. package/dist/models/product.js +2 -2
  171. package/dist/models/product.js.map +1 -1
  172. package/dist/models/region.d.ts +7 -0
  173. package/dist/models/region.js +31 -2
  174. package/dist/models/region.js.map +1 -1
  175. package/dist/models/shipping-method.d.ts +2 -0
  176. package/dist/models/shipping-method.js +7 -0
  177. package/dist/models/shipping-method.js.map +1 -1
  178. package/dist/models/shipping-tax-rate.d.ts +0 -1
  179. package/dist/models/shipping-tax-rate.js +2 -6
  180. package/dist/models/shipping-tax-rate.js.map +1 -1
  181. package/dist/models/swap.d.ts +1 -1
  182. package/dist/models/swap.js +1 -1
  183. package/dist/models/swap.js.map +1 -1
  184. package/dist/repositories/customer-group.d.ts +5 -0
  185. package/dist/repositories/customer-group.js +91 -0
  186. package/dist/repositories/customer-group.js.map +1 -0
  187. package/dist/repositories/line-item.d.ts +12 -0
  188. package/dist/repositories/line-item.js +61 -0
  189. package/dist/repositories/line-item.js.map +1 -1
  190. package/dist/repositories/money-amount.d.ts +6 -0
  191. package/dist/repositories/money-amount.js +97 -0
  192. package/dist/repositories/money-amount.js.map +1 -1
  193. package/dist/repositories/product.d.ts +2 -0
  194. package/dist/repositories/product.js +32 -0
  195. package/dist/repositories/product.js.map +1 -1
  196. package/dist/repositories/tax-provider.d.ts +4 -0
  197. package/dist/repositories/tax-provider.js +38 -0
  198. package/dist/repositories/tax-provider.js.map +1 -0
  199. package/dist/repositories/tax-rate.d.ts +7 -4
  200. package/dist/repositories/tax-rate.js +96 -21
  201. package/dist/repositories/tax-rate.js.map +1 -1
  202. package/dist/services/cart.d.ts +92 -114
  203. package/dist/services/cart.js +142 -144
  204. package/dist/services/cart.js.map +1 -1
  205. package/dist/services/claim.d.ts +22 -33
  206. package/dist/services/claim.js +106 -95
  207. package/dist/services/claim.js.map +1 -1
  208. package/dist/services/customer-group.d.ts +54 -0
  209. package/dist/services/customer-group.js +265 -0
  210. package/dist/services/customer-group.js.map +1 -0
  211. package/dist/services/customer.d.ts +3 -1
  212. package/dist/services/customer.js +28 -17
  213. package/dist/services/customer.js.map +1 -1
  214. package/dist/services/fulfillment.js +1 -1
  215. package/dist/services/fulfillment.js.map +1 -1
  216. package/dist/services/gift-card.js +1 -1
  217. package/dist/services/gift-card.js.map +1 -1
  218. package/dist/services/index.d.ts +4 -0
  219. package/dist/services/index.js +9 -1
  220. package/dist/services/index.js.map +1 -1
  221. package/dist/services/invite.d.ts +3 -3
  222. package/dist/services/invite.js +4 -2
  223. package/dist/services/invite.js.map +1 -1
  224. package/dist/services/line-item.d.ts +12 -1
  225. package/dist/services/line-item.js +49 -1
  226. package/dist/services/line-item.js.map +1 -1
  227. package/dist/services/order.d.ts +6 -2
  228. package/dist/services/order.js +186 -80
  229. package/dist/services/order.js.map +1 -1
  230. package/dist/services/product-collection.d.ts +2 -0
  231. package/dist/services/product-collection.js +49 -0
  232. package/dist/services/product-collection.js.map +1 -1
  233. package/dist/services/product-type.d.ts +15 -0
  234. package/dist/services/product-type.js +44 -0
  235. package/dist/services/product-type.js.map +1 -1
  236. package/dist/services/product-variant.d.ts +2 -1
  237. package/dist/services/product-variant.js +96 -85
  238. package/dist/services/product-variant.js.map +1 -1
  239. package/dist/services/product.js +3 -1
  240. package/dist/services/product.js.map +1 -1
  241. package/dist/services/region.d.ts +4 -1
  242. package/dist/services/region.js +25 -10
  243. package/dist/services/region.js.map +1 -1
  244. package/dist/services/return.d.ts +3 -1
  245. package/dist/services/return.js +37 -26
  246. package/dist/services/return.js.map +1 -1
  247. package/dist/services/shipping-option.d.ts +7 -4
  248. package/dist/services/shipping-option.js +22 -5
  249. package/dist/services/shipping-option.js.map +1 -1
  250. package/dist/services/swap.d.ts +5 -22
  251. package/dist/services/swap.js +308 -454
  252. package/dist/services/swap.js.map +1 -1
  253. package/dist/services/tax-provider.d.ts +3 -0
  254. package/dist/services/tax-provider.js +10 -0
  255. package/dist/services/tax-provider.js.map +1 -1
  256. package/dist/services/tax-rate.d.ts +13 -6
  257. package/dist/services/tax-rate.js +235 -41
  258. package/dist/services/tax-rate.js.map +1 -1
  259. package/dist/services/totals.d.ts +194 -59
  260. package/dist/services/totals.js +581 -106
  261. package/dist/services/totals.js.map +1 -1
  262. package/dist/subscribers/region.d.ts +14 -0
  263. package/dist/subscribers/region.js +65 -0
  264. package/dist/subscribers/region.js.map +1 -0
  265. package/dist/types/cart.d.ts +2 -1
  266. package/dist/types/cart.js +6 -1
  267. package/dist/types/cart.js.map +1 -1
  268. package/dist/types/common.d.ts +5 -1
  269. package/dist/types/common.js +22 -7
  270. package/dist/types/common.js.map +1 -1
  271. package/dist/types/customer-groups.d.ts +11 -0
  272. package/dist/types/customer-groups.js +43 -0
  273. package/dist/types/customer-groups.js.map +1 -0
  274. package/dist/types/orders.d.ts +2 -0
  275. package/dist/types/orders.js +6 -1
  276. package/dist/types/orders.js.map +1 -1
  277. package/dist/types/product.d.ts +6 -0
  278. package/dist/types/product.js +25 -1
  279. package/dist/types/product.js.map +1 -1
  280. package/dist/utils/format-registration-name.js +3 -0
  281. package/dist/utils/format-registration-name.js.map +1 -1
  282. package/package.json +5 -4
  283. package/yarn-error.log +5 -79
  284. package/dist/api/routes/admin/product-tags/create-collection.d.ts +0 -41
  285. package/dist/api/routes/admin/product-tags/create-collection.js.map +0 -1
  286. package/dist/api/routes/admin/product-tags/delete-collection.d.ts +0 -28
  287. package/dist/api/routes/admin/product-tags/delete-collection.js.map +0 -1
  288. package/dist/api/routes/admin/product-tags/get-collection.d.ts +0 -22
  289. package/dist/api/routes/admin/product-tags/get-collection.js.map +0 -1
  290. package/dist/api/routes/admin/product-tags/list-collections.d.ts +0 -40
  291. package/dist/api/routes/admin/product-tags/list-collections.js.map +0 -1
  292. package/dist/api/routes/admin/product-tags/update-collection.d.ts +0 -41
  293. package/dist/api/routes/admin/product-tags/update-collection.js.map +0 -1
@@ -14,6 +14,42 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
16
  })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (_) try {
33
+ 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;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
17
53
  var __values = (this && this.__values) || function(o) {
18
54
  var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
19
55
  if (m) return m.call(o);
@@ -57,28 +93,52 @@ Object.defineProperty(exports, "__esModule", { value: true });
57
93
  var lodash_1 = __importDefault(require("lodash"));
58
94
  var medusa_interfaces_1 = require("medusa-interfaces");
59
95
  var medusa_core_utils_1 = require("medusa-core-utils");
96
+ var discount_rule_1 = require("../models/discount-rule");
97
+ var cart_1 = require("../types/cart");
98
+ var orders_1 = require("../types/orders");
60
99
  /**
61
100
  * A service that calculates total and subtotals for orders, carts etc..
62
101
  * @implements {BaseService}
63
102
  */
64
103
  var TotalsService = /** @class */ (function (_super) {
65
104
  __extends(TotalsService, _super);
66
- function TotalsService() {
67
- return _super.call(this) || this;
105
+ function TotalsService(_a) {
106
+ var taxProviderService = _a.taxProviderService, taxCalculationStrategy = _a.taxCalculationStrategy;
107
+ var _this = _super.call(this) || this;
108
+ _this.taxProviderService_ = taxProviderService;
109
+ _this.taxCalculationStrategy_ = taxCalculationStrategy;
110
+ return _this;
68
111
  }
69
112
  /**
70
113
  * Calculates subtotal of a given cart or order.
71
- * @param {object} object - object to calculate total for
72
- * @return {int} the calculated subtotal
114
+ * @param cartOrOrder - object to calculate total for
115
+ * @param options - options to calculate by
116
+ * @return the calculated subtotal
73
117
  */
74
- TotalsService.prototype.getTotal = function (object) {
75
- var subtotal = this.getSubtotal(object);
76
- var taxTotal = this.getTaxTotal(object);
77
- var discountTotal = this.getDiscountTotal(object);
78
- var giftCardTotal = this.getGiftCardTotal(object);
79
- var shippingTotal = this.getShippingTotal(object);
80
- return subtotal + taxTotal + shippingTotal - discountTotal - giftCardTotal;
118
+ TotalsService.prototype.getTotal = function (cartOrOrder, options) {
119
+ if (options === void 0) { options = {}; }
120
+ return __awaiter(this, void 0, void 0, function () {
121
+ var subtotal, taxTotal, discountTotal, giftCardTotal, shippingTotal;
122
+ return __generator(this, function (_a) {
123
+ switch (_a.label) {
124
+ case 0:
125
+ subtotal = this.getSubtotal(cartOrOrder);
126
+ return [4 /*yield*/, this.getTaxTotal(cartOrOrder, options.force_taxes)];
127
+ case 1:
128
+ taxTotal = (_a.sent()) || 0;
129
+ discountTotal = this.getDiscountTotal(cartOrOrder);
130
+ giftCardTotal = this.getGiftCardTotal(cartOrOrder);
131
+ shippingTotal = this.getShippingTotal(cartOrOrder);
132
+ return [2 /*return*/, subtotal + taxTotal + shippingTotal - discountTotal - giftCardTotal];
133
+ }
134
+ });
135
+ });
81
136
  };
137
+ /**
138
+ * Gets the total payments made on an order
139
+ * @param order - the order to calculate paid amount for
140
+ * @return the total paid amount
141
+ */
82
142
  TotalsService.prototype.getPaidTotal = function (order) {
83
143
  var _a;
84
144
  var total = (_a = order.payments) === null || _a === void 0 ? void 0 : _a.reduce(function (acc, next) {
@@ -87,6 +147,12 @@ var TotalsService = /** @class */ (function (_super) {
87
147
  }, 0);
88
148
  return total;
89
149
  };
150
+ /**
151
+ * The total paid for swaps. May be negative in case of negative swap
152
+ * difference.
153
+ * @param order - the order to calculate swap total for
154
+ * @return the swap total
155
+ */
90
156
  TotalsService.prototype.getSwapTotal = function (order) {
91
157
  var e_1, _a;
92
158
  var swapTotal = 0;
@@ -107,19 +173,96 @@ var TotalsService = /** @class */ (function (_super) {
107
173
  }
108
174
  return swapTotal;
109
175
  };
176
+ /**
177
+ * Gets the totals breakdown for a shipping method. Fetches tax lines if not
178
+ * already provided.
179
+ * @param shippingMethod - the shipping method to get totals breakdown for.
180
+ * @param cartOrOrder - the cart or order to use as context for the breakdown
181
+ * @param opts - options for what should be included
182
+ * @returns An object that breaks down the totals for the shipping method
183
+ */
184
+ TotalsService.prototype.getShippingMethodTotals = function (shippingMethod, cartOrOrder, opts) {
185
+ if (opts === void 0) { opts = {}; }
186
+ return __awaiter(this, void 0, void 0, function () {
187
+ var calculationContext, totals, taxLines, orderLines, _a;
188
+ return __generator(this, function (_b) {
189
+ switch (_b.label) {
190
+ case 0:
191
+ calculationContext = this.getCalculationContext(cartOrOrder, {
192
+ exclude_shipping: true,
193
+ });
194
+ calculationContext.shipping_methods = [shippingMethod];
195
+ totals = {
196
+ price: shippingMethod.price,
197
+ original_total: shippingMethod.price,
198
+ total: shippingMethod.price,
199
+ original_tax_total: 0,
200
+ tax_total: 0,
201
+ tax_lines: shippingMethod.tax_lines || [],
202
+ };
203
+ if (!opts.include_tax) return [3 /*break*/, 7];
204
+ if (!((0, orders_1.isOrder)(cartOrOrder) && cartOrOrder.tax_rate !== null)) return [3 /*break*/, 1];
205
+ totals.original_tax_total = Math.round(totals.original_tax_total * (cartOrOrder.tax_rate / 100));
206
+ totals.tax_total = Math.round(totals.original_tax_total * (cartOrOrder.tax_rate / 100));
207
+ return [3 /*break*/, 5];
208
+ case 1:
209
+ taxLines = void 0;
210
+ if (!(opts.use_tax_lines || (0, orders_1.isOrder)(cartOrOrder))) return [3 /*break*/, 2];
211
+ if (typeof shippingMethod.tax_lines === "undefined") {
212
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Tax Lines must be joined on shipping method to calculate taxes");
213
+ }
214
+ taxLines = shippingMethod.tax_lines;
215
+ return [3 /*break*/, 4];
216
+ case 2: return [4 /*yield*/, this.taxProviderService_.getTaxLines(cartOrOrder.items, calculationContext)];
217
+ case 3:
218
+ orderLines = _b.sent();
219
+ console.log(orderLines);
220
+ taxLines = orderLines.filter(function (ol) {
221
+ if ("shipping_method_id" in ol) {
222
+ return ol.shipping_method_id === shippingMethod.id;
223
+ }
224
+ return false;
225
+ });
226
+ _b.label = 4;
227
+ case 4:
228
+ totals.tax_lines = taxLines;
229
+ _b.label = 5;
230
+ case 5:
231
+ if (!(totals.tax_lines.length > 0)) return [3 /*break*/, 7];
232
+ _a = totals;
233
+ return [4 /*yield*/, this.taxCalculationStrategy_.calculate([], totals.tax_lines, calculationContext)];
234
+ case 6:
235
+ _a.original_tax_total =
236
+ _b.sent();
237
+ totals.tax_total = totals.original_tax_total;
238
+ totals.original_total += totals.original_tax_total;
239
+ totals.total += totals.tax_total;
240
+ _b.label = 7;
241
+ case 7:
242
+ if (cartOrOrder.discounts) {
243
+ if (cartOrOrder.discounts.some(function (d) { return d.rule.type === "free_shipping"; })) {
244
+ totals.total = 0;
245
+ totals.tax_total = 0;
246
+ }
247
+ }
248
+ return [2 /*return*/, totals];
249
+ }
250
+ });
251
+ });
252
+ };
110
253
  /**
111
254
  * Calculates subtotal of a given cart or order.
112
- * @param {(Cart|Order)} object - cart or order to calculate subtotal for
113
- * @param {Object} opts - options
114
- * @return {int} the calculated subtotal
255
+ * @param cartOrOrder - cart or order to calculate subtotal for
256
+ * @param opts - options
257
+ * @return the calculated subtotal
115
258
  */
116
- TotalsService.prototype.getSubtotal = function (object, opts) {
259
+ TotalsService.prototype.getSubtotal = function (cartOrOrder, opts) {
117
260
  if (opts === void 0) { opts = {}; }
118
261
  var subtotal = 0;
119
- if (!object.items) {
262
+ if (!cartOrOrder.items) {
120
263
  return subtotal;
121
264
  }
122
- object.items.map(function (item) {
265
+ cartOrOrder.items.map(function (item) {
123
266
  if (opts.excludeNonDiscounts) {
124
267
  if (item.allow_discounts) {
125
268
  subtotal += item.unit_price * item.quantity;
@@ -133,11 +276,11 @@ var TotalsService = /** @class */ (function (_super) {
133
276
  };
134
277
  /**
135
278
  * Calculates shipping total
136
- * @param {Cart | Object} object - cart or order to calculate subtotal for
137
- * @return {int} shipping total
279
+ * @param cartOrOrder - cart or order to calculate subtotal for
280
+ * @return shipping total
138
281
  */
139
- TotalsService.prototype.getShippingTotal = function (object) {
140
- var shipping_methods = object.shipping_methods;
282
+ TotalsService.prototype.getShippingTotal = function (cartOrOrder) {
283
+ var shipping_methods = cartOrOrder.shipping_methods;
141
284
  return shipping_methods.reduce(function (acc, next) {
142
285
  return acc + next.price;
143
286
  }, 0);
@@ -145,55 +288,214 @@ var TotalsService = /** @class */ (function (_super) {
145
288
  /**
146
289
  * Calculates tax total
147
290
  * Currently based on the Danish tax system
148
- * @param {Cart | Object} object - cart or order to calculate subtotal for
149
- * @return {int} tax total
291
+ * @param cartOrOrder - cart or order to calculate tax total for
292
+ * @param forceTaxes - whether taxes should be calculated regardless
293
+ * of region settings
294
+ * @return tax total
150
295
  */
151
- TotalsService.prototype.getTaxTotal = function (object) {
152
- var subtotal = this.getSubtotal(object);
153
- var shippingTotal = this.getShippingTotal(object);
154
- var discountTotal = this.getDiscountTotal(object);
155
- var giftCardTotal = this.getGiftCardTotal(object);
156
- var tax_rate = typeof object.tax_rate !== "undefined"
157
- ? object.tax_rate
158
- : object.region.tax_rate;
159
- return this.rounded((subtotal - discountTotal - giftCardTotal + shippingTotal) *
160
- (tax_rate / 100));
296
+ TotalsService.prototype.getTaxTotal = function (cartOrOrder, forceTaxes) {
297
+ if (forceTaxes === void 0) { forceTaxes = false; }
298
+ return __awaiter(this, void 0, void 0, function () {
299
+ var calculationContext, taxLines, taxLinesJoined, shippingTaxLines, subtotal, shippingTotal, discountTotal, giftCardTotal, returnTaxLines, toReturn;
300
+ return __generator(this, function (_a) {
301
+ switch (_a.label) {
302
+ case 0:
303
+ if ((0, cart_1.isCart)(cartOrOrder) &&
304
+ !forceTaxes &&
305
+ !cartOrOrder.region.automatic_taxes) {
306
+ return [2 /*return*/, null];
307
+ }
308
+ calculationContext = this.getCalculationContext(cartOrOrder);
309
+ if (!(0, orders_1.isOrder)(cartOrOrder)) return [3 /*break*/, 1];
310
+ taxLinesJoined = cartOrOrder.items.every(function (i) { return typeof i.tax_lines !== "undefined"; });
311
+ if (!taxLinesJoined) {
312
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Order tax calculations must have tax lines joined on line items");
313
+ }
314
+ if (cartOrOrder.tax_rate === null) {
315
+ taxLines = cartOrOrder.items.flatMap(function (li) { return li.tax_lines; });
316
+ shippingTaxLines = cartOrOrder.shipping_methods.flatMap(function (sm) { return sm.tax_lines; });
317
+ taxLines = taxLines.concat(shippingTaxLines);
318
+ }
319
+ else {
320
+ subtotal = this.getSubtotal(cartOrOrder);
321
+ shippingTotal = this.getShippingTotal(cartOrOrder);
322
+ discountTotal = this.getDiscountTotal(cartOrOrder);
323
+ giftCardTotal = this.getGiftCardTotal(cartOrOrder);
324
+ return [2 /*return*/, this.rounded((subtotal - discountTotal - giftCardTotal + shippingTotal) *
325
+ (cartOrOrder.tax_rate / 100))];
326
+ }
327
+ return [3 /*break*/, 3];
328
+ case 1: return [4 /*yield*/, this.taxProviderService_.getTaxLines(cartOrOrder.items, calculationContext)];
329
+ case 2:
330
+ taxLines = _a.sent();
331
+ if (cartOrOrder.type === "swap") {
332
+ returnTaxLines = cartOrOrder.items.flatMap(function (i) {
333
+ if (i.is_return) {
334
+ if (typeof i.tax_lines === "undefined") {
335
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Return Line Items must join tax lines");
336
+ }
337
+ return i.tax_lines;
338
+ }
339
+ return [];
340
+ });
341
+ taxLines = taxLines.concat(returnTaxLines);
342
+ }
343
+ _a.label = 3;
344
+ case 3: return [4 /*yield*/, this.taxCalculationStrategy_.calculate(cartOrOrder.items, taxLines, calculationContext)];
345
+ case 4:
346
+ toReturn = _a.sent();
347
+ return [2 /*return*/, this.rounded(toReturn)];
348
+ }
349
+ });
350
+ });
161
351
  };
162
- TotalsService.prototype.getRefundedTotal = function (object) {
163
- if (!object.refunds) {
352
+ /**
353
+ * Gets a map of discounts and gift cards that apply to line items in an
354
+ * order. The function calculates the amount of a discount or gift card that
355
+ * applies to a specific line item.
356
+ * @param orderOrCart - the order or cart to get an allocation map for
357
+ * @param options - controls what should be included in allocation map
358
+ * @return the allocation map for the line items in the cart or order.
359
+ */
360
+ TotalsService.prototype.getAllocationMap = function (orderOrCart, options) {
361
+ var e_2, _a, e_3, _b;
362
+ if (options === void 0) { options = {}; }
363
+ var allocationMap = {};
364
+ if (!options.exclude_discounts) {
365
+ var lineDiscounts = [];
366
+ var discount = orderOrCart.discounts.find(function (_a) {
367
+ var rule = _a.rule;
368
+ return rule.type !== "free_shipping";
369
+ });
370
+ if (discount) {
371
+ lineDiscounts = this.getLineDiscounts(orderOrCart, discount);
372
+ }
373
+ try {
374
+ for (var lineDiscounts_1 = __values(lineDiscounts), lineDiscounts_1_1 = lineDiscounts_1.next(); !lineDiscounts_1_1.done; lineDiscounts_1_1 = lineDiscounts_1.next()) {
375
+ var ld = lineDiscounts_1_1.value;
376
+ if (allocationMap[ld.item.id]) {
377
+ allocationMap[ld.item.id].discount = {
378
+ amount: ld.amount,
379
+ unit_amount: ld.amount / ld.item.quantity,
380
+ };
381
+ }
382
+ else {
383
+ allocationMap[ld.item.id] = {
384
+ discount: {
385
+ amount: ld.amount,
386
+ unit_amount: ld.amount / ld.item.quantity,
387
+ },
388
+ };
389
+ }
390
+ }
391
+ }
392
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
393
+ finally {
394
+ try {
395
+ if (lineDiscounts_1_1 && !lineDiscounts_1_1.done && (_a = lineDiscounts_1.return)) _a.call(lineDiscounts_1);
396
+ }
397
+ finally { if (e_2) throw e_2.error; }
398
+ }
399
+ }
400
+ if (!options.exclude_gift_cards) {
401
+ var lineGiftCards = [];
402
+ if (orderOrCart.gift_cards && orderOrCart.gift_cards.length) {
403
+ var subtotal = this.getSubtotal(orderOrCart);
404
+ var giftCardTotal = this.getGiftCardTotal(orderOrCart);
405
+ // If the fixed discount exceeds the subtotal we should
406
+ // calculate a 100% discount
407
+ var nominator = Math.min(giftCardTotal, subtotal);
408
+ var percentage_1 = nominator / subtotal;
409
+ lineGiftCards = orderOrCart.items.map(function (l) {
410
+ return {
411
+ item: l,
412
+ amount: l.unit_price * l.quantity * percentage_1,
413
+ };
414
+ });
415
+ }
416
+ try {
417
+ for (var lineGiftCards_1 = __values(lineGiftCards), lineGiftCards_1_1 = lineGiftCards_1.next(); !lineGiftCards_1_1.done; lineGiftCards_1_1 = lineGiftCards_1.next()) {
418
+ var lgc = lineGiftCards_1_1.value;
419
+ if (allocationMap[lgc.item.id]) {
420
+ allocationMap[lgc.item.id].gift_card = {
421
+ amount: lgc.amount,
422
+ unit_amount: lgc.amount / lgc.item.quantity,
423
+ };
424
+ }
425
+ else {
426
+ allocationMap[lgc.item.id] = {
427
+ discount: {
428
+ amount: lgc.amount,
429
+ unit_amount: lgc.amount / lgc.item.quantity,
430
+ },
431
+ };
432
+ }
433
+ }
434
+ }
435
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
436
+ finally {
437
+ try {
438
+ if (lineGiftCards_1_1 && !lineGiftCards_1_1.done && (_b = lineGiftCards_1.return)) _b.call(lineGiftCards_1);
439
+ }
440
+ finally { if (e_3) throw e_3.error; }
441
+ }
442
+ }
443
+ return allocationMap;
444
+ };
445
+ /**
446
+ * Gets the total refund amount for an order.
447
+ * @param order - the order to get total refund amount for.
448
+ * @return the total refunded amount for an order.
449
+ */
450
+ TotalsService.prototype.getRefundedTotal = function (order) {
451
+ if (!order.refunds) {
164
452
  return 0;
165
453
  }
166
- var total = object.refunds.reduce(function (acc, next) { return acc + next.amount; }, 0);
454
+ var total = order.refunds.reduce(function (acc, next) { return acc + next.amount; }, 0);
167
455
  return this.rounded(total);
168
456
  };
169
- TotalsService.prototype.getLineItemRefund = function (object, lineItem) {
170
- var discounts = object.discounts;
171
- var tax_rate = typeof object.tax_rate !== "undefined"
172
- ? object.tax_rate
173
- : object.region.tax_rate;
174
- var taxRate = (tax_rate || 0) / 100;
175
- var discount = discounts.find(function (_a) {
176
- var rule = _a.rule;
177
- return rule.type !== "free_shipping";
178
- });
179
- if (!discount || !lineItem.allow_discounts) {
180
- return lineItem.unit_price * lineItem.quantity * (1 + taxRate);
457
+ /**
458
+ * The amount that can be refunded for a given line item.
459
+ * @param order - order to use as context for the calculation
460
+ * @param lineItem - the line item to calculate the refund amount for.
461
+ * @return the line item refund amount.
462
+ */
463
+ TotalsService.prototype.getLineItemRefund = function (order, lineItem) {
464
+ var _this = this;
465
+ var _a, _b;
466
+ var allocationMap = this.getAllocationMap(order);
467
+ var discountAmount = (((_b = (_a = allocationMap[lineItem.id]) === null || _a === void 0 ? void 0 : _a.discount) === null || _b === void 0 ? void 0 : _b.unit_amount) || 0) *
468
+ lineItem.quantity;
469
+ var lineSubtotal = lineItem.unit_price * lineItem.quantity - discountAmount;
470
+ /*
471
+ * Used for backcompat with old tax system
472
+ */
473
+ if (order.tax_rate !== null) {
474
+ var taxRate = order.tax_rate / 100;
475
+ return this.rounded(lineSubtotal * (1 + taxRate));
476
+ }
477
+ /*
478
+ * New tax system uses the tax lines registerd on the line items
479
+ */
480
+ if (typeof lineItem.tax_lines === "undefined") {
481
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Tax calculation did not receive tax_lines");
181
482
  }
182
- var lineDiscounts = this.getLineDiscounts(object, discount);
183
- var discountedLine = lineDiscounts.find(function (line) { return line.item.id === lineItem.id; });
184
- var discountAmount = (discountedLine.amount / discountedLine.item.quantity) * lineItem.quantity;
185
- return this.rounded((lineItem.unit_price * lineItem.quantity - discountAmount) * (1 + taxRate));
483
+ var taxTotal = lineItem.tax_lines.reduce(function (acc, next) {
484
+ var taxRate = next.rate / 100;
485
+ return acc + _this.rounded(lineSubtotal * taxRate);
486
+ }, 0);
487
+ return lineSubtotal + taxTotal;
186
488
  };
187
489
  /**
188
490
  * Calculates refund total of line items.
189
491
  * If any of the items to return have been discounted, we need to
190
492
  * apply the discount again before refunding them.
191
- * @param {Order} order - cart or order to calculate subtotal for
192
- * @param {[LineItem]} lineItems -
193
- * @return {int} the calculated subtotal
493
+ * @param order - cart or order to calculate subtotal for
494
+ * @param lineItems - the line items to calculate refund total for
495
+ * @return the calculated subtotal
194
496
  */
195
497
  TotalsService.prototype.getRefundTotal = function (order, lineItems) {
196
- var e_2, _a, e_3, _b;
498
+ var e_4, _a, e_5, _b;
197
499
  var _this = this;
198
500
  var itemIds = order.items.map(function (i) { return i.id; });
199
501
  // in case we swap a swap, we need to include swap items
@@ -205,12 +507,12 @@ var TotalsService = /** @class */ (function (_super) {
205
507
  itemIds = __spreadArray(__spreadArray([], __read(itemIds), false), __read(swapItemIds), false);
206
508
  }
207
509
  }
208
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
510
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
209
511
  finally {
210
512
  try {
211
513
  if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
212
514
  }
213
- finally { if (e_2) throw e_2.error; }
515
+ finally { if (e_4) throw e_4.error; }
214
516
  }
215
517
  }
216
518
  if (order.claims && order.claims.length) {
@@ -221,12 +523,12 @@ var TotalsService = /** @class */ (function (_super) {
221
523
  itemIds = __spreadArray(__spreadArray([], __read(itemIds), false), __read(claimItemIds), false);
222
524
  }
223
525
  }
224
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
526
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
225
527
  finally {
226
528
  try {
227
529
  if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
228
530
  }
229
- finally { if (e_3) throw e_3.error; }
531
+ finally { if (e_5) throw e_5.error; }
230
532
  }
231
533
  }
232
534
  var refunds = lineItems.map(function (i) {
@@ -239,13 +541,12 @@ var TotalsService = /** @class */ (function (_super) {
239
541
  };
240
542
  /**
241
543
  * Calculates either fixed or percentage discount of a variant
242
- * @param {string} lineItem - id of line item
243
- * @param {string} variant - id of variant in line item
244
- * @param {int} variantPrice - price of the variant based on region
245
- * @param {int} value - discount value
246
- * @param {string} discountType - the type of discount (fixed or percentage)
247
- * @return {{ string, string, int }} triples of lineitem, variant and
248
- * applied discount
544
+ * @param lineItem - id of line item
545
+ * @param variant - id of variant in line item
546
+ * @param variantPrice - price of the variant based on region
547
+ * @param value - discount value
548
+ * @param discountType - the type of discount (fixed or percentage)
549
+ * @return triples of lineitem, variant and applied discount
249
550
  */
250
551
  TotalsService.prototype.calculateDiscount_ = function (lineItem, variant, variantPrice, value, discountType) {
251
552
  if (!lineItem.allow_discounts) {
@@ -255,7 +556,7 @@ var TotalsService = /** @class */ (function (_super) {
255
556
  amount: 0,
256
557
  };
257
558
  }
258
- if (discountType === "percentage") {
559
+ if (discountType === discount_rule_1.DiscountRuleType.PERCENTAGE) {
259
560
  return {
260
561
  lineItem: lineItem,
261
562
  variant: variant,
@@ -278,13 +579,12 @@ var TotalsService = /** @class */ (function (_super) {
278
579
  * make sure to only apply the discount on valid variants. And finally we
279
580
  * return ether an array of percentages discounts or fixed discounts
280
581
  * alongside the variant on which the discount was applied.
281
- * @param {Discount} discount - the discount to which we do the calculation
282
- * @param {Cart} cart - the cart to calculate discounts for
283
- * @return {[{ string, string, int }]} array of triples of lineitem, variant
284
- * and applied discount
582
+ * @param discount - the discount to which we do the calculation
583
+ * @param cart - the cart to calculate discounts for
584
+ * @return array of triples of lineitem, variant and applied discount
285
585
  */
286
586
  TotalsService.prototype.getAllocationItemDiscounts = function (discount, cart) {
287
- var e_4, _a;
587
+ var e_6, _a;
288
588
  var _this = this;
289
589
  var _b;
290
590
  var discounts = [];
@@ -304,72 +604,81 @@ var TotalsService = /** @class */ (function (_super) {
304
604
  _loop_1(item);
305
605
  }
306
606
  }
307
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
607
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
308
608
  finally {
309
609
  try {
310
610
  if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
311
611
  }
312
- finally { if (e_4) throw e_4.error; }
612
+ finally { if (e_6) throw e_6.error; }
313
613
  }
314
614
  return discounts;
315
615
  };
316
- TotalsService.prototype.getLineDiscounts = function (cart, discount) {
317
- var e_5, _a, e_6, _b;
318
- var subtotal = this.getSubtotal(cart, { excludeNonDiscounts: true });
319
- var merged = __spreadArray([], __read(cart.items), false);
616
+ /**
617
+ * Returns the discount amount allocated to the line items of an order.
618
+ * @param cartOrOrder - the cart or order to get line discount allocations for
619
+ * @param discount - the discount to use as context for the calculation
620
+ * @return the allocations that the discount has on the items in the cart or
621
+ * order
622
+ */
623
+ TotalsService.prototype.getLineDiscounts = function (cartOrOrder, discount) {
624
+ var e_7, _a, e_8, _b;
625
+ var subtotal = this.getSubtotal(cartOrOrder, {
626
+ excludeNonDiscounts: true,
627
+ });
628
+ var merged = __spreadArray([], __read(cartOrOrder.items), false);
320
629
  // merge items from order with items from order swaps
321
- if (cart.swaps && cart.swaps.length) {
630
+ if ("swaps" in cartOrOrder && cartOrOrder.swaps.length) {
322
631
  try {
323
- for (var _c = __values(cart.swaps), _d = _c.next(); !_d.done; _d = _c.next()) {
632
+ for (var _c = __values(cartOrOrder.swaps), _d = _c.next(); !_d.done; _d = _c.next()) {
324
633
  var s = _d.value;
325
634
  merged = __spreadArray(__spreadArray([], __read(merged), false), __read(s.additional_items), false);
326
635
  }
327
636
  }
328
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
637
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
329
638
  finally {
330
639
  try {
331
640
  if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
332
641
  }
333
- finally { if (e_5) throw e_5.error; }
642
+ finally { if (e_7) throw e_7.error; }
334
643
  }
335
644
  }
336
- if (cart.claims && cart.claims.length) {
645
+ if ("claims" in cartOrOrder && cartOrOrder.claims.length) {
337
646
  try {
338
- for (var _e = __values(cart.claims), _f = _e.next(); !_f.done; _f = _e.next()) {
647
+ for (var _e = __values(cartOrOrder.claims), _f = _e.next(); !_f.done; _f = _e.next()) {
339
648
  var c = _f.value;
340
649
  merged = __spreadArray(__spreadArray([], __read(merged), false), __read(c.additional_items), false);
341
650
  }
342
651
  }
343
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
652
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
344
653
  finally {
345
654
  try {
346
655
  if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
347
656
  }
348
- finally { if (e_6) throw e_6.error; }
657
+ finally { if (e_8) throw e_8.error; }
349
658
  }
350
659
  }
351
660
  var _g = discount.rule, type = _g.type, allocation = _g.allocation, value = _g.value;
352
661
  if (allocation === "total") {
353
- var percentage_1 = 0;
662
+ var percentage_2 = 0;
354
663
  if (type === "percentage") {
355
- percentage_1 = value / 100;
664
+ percentage_2 = value / 100;
356
665
  }
357
666
  else if (type === "fixed") {
358
667
  // If the fixed discount exceeds the subtotal we should
359
668
  // calculate a 100% discount
360
669
  var nominator = Math.min(value, subtotal);
361
- percentage_1 = nominator / subtotal;
670
+ percentage_2 = nominator / subtotal;
362
671
  }
363
672
  return merged.map(function (item) {
364
673
  var lineTotal = item.unit_price * item.quantity;
365
674
  return {
366
675
  item: item,
367
- amount: lineTotal * percentage_1,
676
+ amount: item.allow_discounts ? lineTotal * percentage_2 : 0,
368
677
  };
369
678
  });
370
679
  }
371
680
  else if (allocation === "item") {
372
- var allocationDiscounts_1 = this.getAllocationItemDiscounts(discount, cart, type);
681
+ var allocationDiscounts_1 = this.getAllocationItemDiscounts(discount, cartOrOrder);
373
682
  return merged.map(function (item) {
374
683
  var discounted = allocationDiscounts_1.find(function (a) { return a.lineItem.id === item.id; });
375
684
  return {
@@ -380,31 +689,165 @@ var TotalsService = /** @class */ (function (_super) {
380
689
  }
381
690
  return merged.map(function (i) { return ({ item: i, amount: 0 }); });
382
691
  };
383
- TotalsService.prototype.getGiftCardTotal = function (cart) {
384
- var giftCardable = this.getSubtotal(cart) - this.getDiscountTotal(cart);
385
- if (cart.gift_card_transactions) {
386
- return cart.gift_card_transactions.reduce(function (acc, next) { return acc + next.amount; }, 0);
692
+ /**
693
+ * Breaks down the totals related to a line item; these are the subtotal, the
694
+ * amount of discount applied to the line item, the amount of a gift card
695
+ * applied to a line item and the amount of tax applied to a line item.
696
+ * @param lineItem - the line item to calculate totals for
697
+ * @param cartOrOrder - the cart or order to use as context for the calculation
698
+ * @param options - the options to evaluate the line item totals for
699
+ * @returns the breakdown of the line item totals
700
+ */
701
+ TotalsService.prototype.getLineItemTotals = function (lineItem, cartOrOrder, options) {
702
+ var _a, _b;
703
+ if (options === void 0) { options = {}; }
704
+ return __awaiter(this, void 0, void 0, function () {
705
+ var calculationContext, lineItemAllocation, subtotal, gift_card_total, discount_total, lineItemTotals, taxLines, orderLines, _c, _d;
706
+ return __generator(this, function (_e) {
707
+ switch (_e.label) {
708
+ case 0:
709
+ calculationContext = this.getCalculationContext(cartOrOrder, {
710
+ exclude_shipping: true,
711
+ });
712
+ lineItemAllocation = calculationContext.allocation_map[lineItem.id] || {};
713
+ subtotal = lineItem.unit_price * lineItem.quantity;
714
+ gift_card_total = ((_a = lineItemAllocation.gift_card) === null || _a === void 0 ? void 0 : _a.amount) || 0;
715
+ discount_total = (((_b = lineItemAllocation.discount) === null || _b === void 0 ? void 0 : _b.unit_amount) || 0) * lineItem.quantity;
716
+ lineItemTotals = {
717
+ unit_price: lineItem.unit_price,
718
+ quantity: lineItem.quantity,
719
+ subtotal: subtotal,
720
+ gift_card_total: gift_card_total,
721
+ discount_total: discount_total,
722
+ total: subtotal - discount_total,
723
+ original_total: subtotal,
724
+ original_tax_total: 0,
725
+ tax_total: 0,
726
+ tax_lines: lineItem.tax_lines || [],
727
+ };
728
+ if (!options.include_tax) return [3 /*break*/, 6];
729
+ if (!((0, orders_1.isOrder)(cartOrOrder) && cartOrOrder.tax_rate !== null)) return [3 /*break*/, 1];
730
+ lineItemTotals.original_tax_total =
731
+ subtotal * (cartOrOrder.tax_rate / 100);
732
+ lineItemTotals.tax_total =
733
+ (subtotal - discount_total) * (cartOrOrder.tax_rate / 100);
734
+ lineItemTotals.total += lineItemTotals.tax_total;
735
+ lineItemTotals.original_total += lineItemTotals.original_tax_total;
736
+ return [3 /*break*/, 6];
737
+ case 1:
738
+ taxLines = void 0;
739
+ if (!(options.use_tax_lines || (0, orders_1.isOrder)(cartOrOrder))) return [3 /*break*/, 2];
740
+ if (typeof lineItem.tax_lines === "undefined") {
741
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Tax Lines must be joined on items to calculate taxes");
742
+ }
743
+ taxLines = lineItem.tax_lines;
744
+ return [3 /*break*/, 5];
745
+ case 2:
746
+ if (!lineItem.is_return) return [3 /*break*/, 3];
747
+ if (typeof lineItem.tax_lines === "undefined") {
748
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.UNEXPECTED_STATE, "Return Line Items must join tax lines");
749
+ }
750
+ taxLines = lineItem.tax_lines;
751
+ return [3 /*break*/, 5];
752
+ case 3: return [4 /*yield*/, this.taxProviderService_.getTaxLines(cartOrOrder.items, calculationContext)];
753
+ case 4:
754
+ orderLines = _e.sent();
755
+ taxLines = orderLines.filter(function (ol) {
756
+ if ("item_id" in ol) {
757
+ return ol.item_id === lineItem.id;
758
+ }
759
+ return false;
760
+ });
761
+ _e.label = 5;
762
+ case 5:
763
+ lineItemTotals.tax_lines = taxLines;
764
+ _e.label = 6;
765
+ case 6:
766
+ if (!(lineItemTotals.tax_lines.length > 0)) return [3 /*break*/, 9];
767
+ _c = lineItemTotals;
768
+ return [4 /*yield*/, this.taxCalculationStrategy_.calculate([lineItem], lineItemTotals.tax_lines, calculationContext)];
769
+ case 7:
770
+ _c.tax_total = _e.sent();
771
+ lineItemTotals.total += lineItemTotals.tax_total;
772
+ calculationContext.allocation_map = {}; // Don't account for discounts
773
+ _d = lineItemTotals;
774
+ return [4 /*yield*/, this.taxCalculationStrategy_.calculate([lineItem], lineItemTotals.tax_lines, calculationContext)];
775
+ case 8:
776
+ _d.original_tax_total =
777
+ _e.sent();
778
+ lineItemTotals.original_total += lineItemTotals.original_tax_total;
779
+ _e.label = 9;
780
+ case 9: return [2 /*return*/, lineItemTotals];
781
+ }
782
+ });
783
+ });
784
+ };
785
+ /**
786
+ * Gets a total for a line item. The total can take gift cards, discounts and
787
+ * taxes into account. This can be controlled through the options.
788
+ * @param lineItem - the line item to calculate a total for
789
+ * @param cartOrOrder - the cart or order to use as context for the calculation
790
+ * @param options - the options to use for the calculation
791
+ * @returns the line item total
792
+ */
793
+ TotalsService.prototype.getLineItemTotal = function (lineItem, cartOrOrder, options) {
794
+ if (options === void 0) { options = {}; }
795
+ return __awaiter(this, void 0, void 0, function () {
796
+ var lineItemTotals, toReturn;
797
+ return __generator(this, function (_a) {
798
+ switch (_a.label) {
799
+ case 0: return [4 /*yield*/, this.getLineItemTotals(lineItem, cartOrOrder, {
800
+ include_tax: options.include_tax,
801
+ })];
802
+ case 1:
803
+ lineItemTotals = _a.sent();
804
+ toReturn = lineItemTotals.subtotal;
805
+ if (!options.exclude_discounts) {
806
+ toReturn += lineItemTotals.discount_total;
807
+ }
808
+ if (!options.exclude_gift_cards) {
809
+ toReturn += lineItemTotals.gift_card_total;
810
+ }
811
+ if (options.include_tax) {
812
+ toReturn += lineItemTotals.tax_total;
813
+ }
814
+ return [2 /*return*/, toReturn];
815
+ }
816
+ });
817
+ });
818
+ };
819
+ /**
820
+ * Gets the gift card amount on a cart or order.
821
+ * @param cartOrOrder - the cart or order to get gift card amount for
822
+ * @return the gift card amount applied to the cart or order
823
+ */
824
+ TotalsService.prototype.getGiftCardTotal = function (cartOrOrder) {
825
+ var giftCardable = this.getSubtotal(cartOrOrder) - this.getDiscountTotal(cartOrOrder);
826
+ if ("gift_card_transactions" in cartOrOrder) {
827
+ return cartOrOrder.gift_card_transactions.reduce(function (acc, next) { return acc + next.amount; }, 0);
387
828
  }
388
- if (!cart.gift_cards || !cart.gift_cards.length) {
829
+ if (!cartOrOrder.gift_cards || !cartOrOrder.gift_cards.length) {
389
830
  return 0;
390
831
  }
391
- var toReturn = cart.gift_cards.reduce(function (acc, next) { return acc + next.balance; }, 0);
832
+ var toReturn = cartOrOrder.gift_cards.reduce(function (acc, next) { return acc + next.balance; }, 0);
392
833
  return Math.min(giftCardable, toReturn);
393
834
  };
394
835
  /**
395
836
  * Calculates the total discount amount for each of the different supported
396
837
  * discount types. If discounts aren't present or invalid returns 0.
397
- * @param {Cart} cart - the cart to calculate discounts for
398
- * @return {int} the total discounts amount
838
+ * @param cartOrOrder - the cart or order to calculate discounts for
839
+ * @return the total discounts amount
399
840
  */
400
- TotalsService.prototype.getDiscountTotal = function (cart) {
401
- var subtotal = this.getSubtotal(cart, { excludeNonDiscounts: true });
402
- if (!cart.discounts || !cart.discounts.length) {
841
+ TotalsService.prototype.getDiscountTotal = function (cartOrOrder) {
842
+ var subtotal = this.getSubtotal(cartOrOrder, {
843
+ excludeNonDiscounts: true,
844
+ });
845
+ if (!cartOrOrder.discounts || !cartOrOrder.discounts.length) {
403
846
  return 0;
404
847
  }
405
848
  // we only support having free shipping and one other discount, so first
406
849
  // find the discount, which is not free shipping.
407
- var discount = cart.discounts.find(function (_a) {
850
+ var discount = cartOrOrder.discounts.find(function (_a) {
408
851
  var rule = _a.rule;
409
852
  return rule.type !== "free_shipping";
410
853
  });
@@ -417,14 +860,14 @@ var TotalsService = /** @class */ (function (_super) {
417
860
  toReturn = (subtotal / 100) * value;
418
861
  }
419
862
  else if (type === "percentage" && allocation === "item") {
420
- var itemPercentageDiscounts = this.getAllocationItemDiscounts(discount, cart, "percentage");
863
+ var itemPercentageDiscounts = this.getAllocationItemDiscounts(discount, cartOrOrder);
421
864
  toReturn = lodash_1.default.sumBy(itemPercentageDiscounts, function (d) { return d.amount; });
422
865
  }
423
866
  else if (type === "fixed" && allocation === "total") {
424
867
  toReturn = value;
425
868
  }
426
869
  else if (type === "fixed" && allocation === "item") {
427
- var itemFixedDiscounts = this.getAllocationItemDiscounts(discount, cart, "fixed");
870
+ var itemFixedDiscounts = this.getAllocationItemDiscounts(discount, cartOrOrder);
428
871
  toReturn = lodash_1.default.sumBy(itemFixedDiscounts, function (d) { return d.amount; });
429
872
  }
430
873
  if (subtotal < 0) {
@@ -432,6 +875,38 @@ var TotalsService = /** @class */ (function (_super) {
432
875
  }
433
876
  return this.rounded(Math.min(subtotal, toReturn));
434
877
  };
878
+ /**
879
+ * Prepares the calculation context for a tax total calculation.
880
+ * @param cartOrOrder - the cart or order to get the calculation context for
881
+ * @param options - options to gather context by
882
+ * @return the tax calculation context
883
+ */
884
+ TotalsService.prototype.getCalculationContext = function (cartOrOrder, options) {
885
+ var _a;
886
+ if (options === void 0) { options = {}; }
887
+ var allocationMap = this.getAllocationMap(cartOrOrder, {
888
+ exclude_gift_cards: options.exclude_gift_cards,
889
+ exclude_discounts: options.exclude_discounts,
890
+ });
891
+ var shippingMethods = [];
892
+ // Default to include shipping methods
893
+ if (!options.exclude_shipping) {
894
+ shippingMethods = cartOrOrder.shipping_methods || [];
895
+ }
896
+ return {
897
+ shipping_address: cartOrOrder.shipping_address,
898
+ shipping_methods: shippingMethods,
899
+ customer: cartOrOrder.customer,
900
+ region: cartOrOrder.region,
901
+ is_return: (_a = options.is_return) !== null && _a !== void 0 ? _a : false,
902
+ allocation_map: allocationMap,
903
+ };
904
+ };
905
+ /**
906
+ * Rounds a number using Math.round.
907
+ * @param value - the value to round
908
+ * @return the rounded value
909
+ */
435
910
  TotalsService.prototype.rounded = function (value) {
436
911
  return Math.round(value);
437
912
  };