resource-url-manager.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /**
  6. * @api
  7. */
  8. define([
  9. 'Magento_Customer/js/model/customer',
  10. 'Magento_Checkout/js/model/url-builder',
  11. 'mageUtils'
  12. ], function (customer, urlBuilder, utils) {
  13. 'use strict';
  14. return {
  15. /**
  16. * @param {Object} quote
  17. * @return {*}
  18. */
  19. getUrlForTotalsEstimationForNewAddress: function (quote) {
  20. var params = this.getCheckoutMethod() == 'guest' ? //eslint-disable-line eqeqeq
  21. {
  22. cartId: quote.getQuoteId()
  23. } : {},
  24. urls = {
  25. 'guest': '/guest-carts/:cartId/totals-information',
  26. 'customer': '/carts/mine/totals-information'
  27. };
  28. return this.getUrl(urls, params);
  29. },
  30. /**
  31. * @param {Object} quote
  32. * @return {*}
  33. */
  34. getUrlForEstimationShippingMethodsForNewAddress: function (quote) {
  35. var params = this.getCheckoutMethod() == 'guest' ? //eslint-disable-line eqeqeq
  36. {
  37. quoteId: quote.getQuoteId()
  38. } : {},
  39. urls = {
  40. 'guest': '/guest-carts/:quoteId/estimate-shipping-methods',
  41. 'customer': '/carts/mine/estimate-shipping-methods'
  42. };
  43. return this.getUrl(urls, params);
  44. },
  45. /**
  46. * @param {Object} quote
  47. * @return {*}
  48. */
  49. getUrlForEstimationShippingMethodsByAddressId: function (quote) {
  50. var params = this.getCheckoutMethod() == 'guest' ? //eslint-disable-line eqeqeq
  51. {
  52. quoteId: quote.getQuoteId()
  53. } : {},
  54. urls = {
  55. 'default': '/carts/mine/estimate-shipping-methods-by-address-id'
  56. };
  57. return this.getUrl(urls, params);
  58. },
  59. /**
  60. * @param {String} couponCode
  61. * @param {String} quoteId
  62. * @return {*}
  63. */
  64. getApplyCouponUrl: function (couponCode, quoteId) {
  65. var params = this.getCheckoutMethod() == 'guest' ? //eslint-disable-line eqeqeq
  66. {
  67. quoteId: quoteId
  68. } : {},
  69. urls = {
  70. 'guest': '/guest-carts/' + quoteId + '/coupons/' + encodeURIComponent(couponCode),
  71. 'customer': '/carts/mine/coupons/' + encodeURIComponent(couponCode)
  72. };
  73. return this.getUrl(urls, params);
  74. },
  75. /**
  76. * @param {String} quoteId
  77. * @return {*}
  78. */
  79. getCancelCouponUrl: function (quoteId) {
  80. var params = this.getCheckoutMethod() == 'guest' ? //eslint-disable-line eqeqeq
  81. {
  82. quoteId: quoteId
  83. } : {},
  84. urls = {
  85. 'guest': '/guest-carts/' + quoteId + '/coupons/',
  86. 'customer': '/carts/mine/coupons/'
  87. };
  88. return this.getUrl(urls, params);
  89. },
  90. /**
  91. * @param {Object} quote
  92. * @return {*}
  93. */
  94. getUrlForCartTotals: function (quote) {
  95. var params = this.getCheckoutMethod() == 'guest' ? //eslint-disable-line eqeqeq
  96. {
  97. quoteId: quote.getQuoteId()
  98. } : {},
  99. urls = {
  100. 'guest': '/guest-carts/:quoteId/totals',
  101. 'customer': '/carts/mine/totals'
  102. };
  103. return this.getUrl(urls, params);
  104. },
  105. /**
  106. * @param {Object} quote
  107. * @return {*}
  108. */
  109. getUrlForSetShippingInformation: function (quote) {
  110. var params = this.getCheckoutMethod() == 'guest' ? //eslint-disable-line eqeqeq
  111. {
  112. cartId: quote.getQuoteId()
  113. } : {},
  114. urls = {
  115. 'guest': '/guest-carts/:cartId/shipping-information',
  116. 'customer': '/carts/mine/shipping-information'
  117. };
  118. return this.getUrl(urls, params);
  119. },
  120. /**
  121. * Get url for service.
  122. *
  123. * @param {*} urls
  124. * @param {*} urlParams
  125. * @return {String|*}
  126. */
  127. getUrl: function (urls, urlParams) {
  128. var url;
  129. if (utils.isEmpty(urls)) {
  130. return 'Provided service call does not exist.';
  131. }
  132. if (!utils.isEmpty(urls['default'])) {
  133. url = urls['default'];
  134. } else {
  135. url = urls[this.getCheckoutMethod()];
  136. }
  137. return urlBuilder.createUrl(url, urlParams);
  138. },
  139. /**
  140. * @return {String}
  141. */
  142. getCheckoutMethod: function () {
  143. return customer.isLoggedIn() ? 'customer' : 'guest';
  144. }
  145. };
  146. }
  147. );