subscription-dashboard.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-return-assign,
  5. no-undef,
  6. no-unused-vars,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS104: Avoid inline assignments
  14. * DS204: Change includes calls to have a more natural evaluation order
  15. * DS207: Consider shorter variations of null checks
  16. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  17. */
  18. define(['base'], function(App) {
  19. const SUBSCRIPTION_URL = '/user/subscription/update'
  20. const ensureRecurlyIsSetup = _.once(() => {
  21. if (typeof recurly === 'undefined' || !recurly) {
  22. return false
  23. }
  24. recurly.configure(window.recurlyApiKey)
  25. return true
  26. })
  27. App.controller('MetricsEmailController', function($scope, $http) {
  28. $scope.institutionEmailSubscription = function(institutionId) {
  29. var inst = _.find(window.managedInstitutions, function(institution) {
  30. return institution.v1Id === parseInt(institutionId)
  31. })
  32. if (inst.metricsEmail.optedOutUserIds.includes(window.user_id)) {
  33. return 'Subscribe'
  34. } else {
  35. return 'Unsubscribe'
  36. }
  37. }
  38. $scope.changeInstitutionalEmailSubscription = function(institutionId) {
  39. $scope.subscriptionChanging = true
  40. return $http({
  41. method: 'POST',
  42. url: `/institutions/${institutionId}/emailSubscription`,
  43. headers: {
  44. 'X-CSRF-Token': window.csrfToken
  45. }
  46. }).then(function successCallback(response) {
  47. window.managedInstitutions = _.map(window.managedInstitutions, function(
  48. institution
  49. ) {
  50. if (institution.v1Id === parseInt(institutionId)) {
  51. institution.metricsEmail.optedOutUserIds = response.data
  52. }
  53. return institution
  54. })
  55. $scope.subscriptionChanging = false
  56. })
  57. }
  58. })
  59. App.factory('RecurlyPricing', function($q, MultiCurrencyPricing) {
  60. return {
  61. loadDisplayPriceWithTax: function(planCode, currency, taxRate) {
  62. if (!ensureRecurlyIsSetup()) return
  63. const currencySymbol = MultiCurrencyPricing.plans[currency].symbol
  64. const pricing = recurly.Pricing()
  65. return $q(function(resolve, reject) {
  66. pricing
  67. .plan(planCode, { quantity: 1 })
  68. .currency(currency)
  69. .done(function(price) {
  70. const totalPriceExTax = parseFloat(price.next.total)
  71. let taxAmmount = totalPriceExTax * taxRate
  72. if (isNaN(taxAmmount)) {
  73. taxAmmount = 0
  74. }
  75. let total = totalPriceExTax + taxAmmount
  76. if (total % 1 !== 0) {
  77. total = total.toFixed(2)
  78. }
  79. resolve(`${currencySymbol}${total}`)
  80. })
  81. })
  82. }
  83. }
  84. })
  85. App.controller('ChangePlanFormController', function(
  86. $scope,
  87. $modal,
  88. RecurlyPricing
  89. ) {
  90. if (!ensureRecurlyIsSetup()) return
  91. $scope.changePlan = () =>
  92. $modal.open({
  93. templateUrl: 'confirmChangePlanModalTemplate',
  94. controller: 'ConfirmChangePlanController',
  95. scope: $scope
  96. })
  97. $scope.$watch('plan', function(plan) {
  98. if (!plan) return
  99. const planCode = plan.planCode
  100. const { currency, taxRate } = window.subscription.recurly
  101. $scope.price = '...' // Placeholder while we talk to recurly
  102. RecurlyPricing.loadDisplayPriceWithTax(planCode, currency, taxRate).then(
  103. price => {
  104. $scope.price = price
  105. }
  106. )
  107. })
  108. })
  109. App.controller('ConfirmChangePlanController', function(
  110. $scope,
  111. $modalInstance,
  112. $http
  113. ) {
  114. $scope.confirmChangePlan = function() {
  115. const body = {
  116. plan_code: $scope.plan.planCode,
  117. _csrf: window.csrfToken
  118. }
  119. $scope.inflight = true
  120. return $http
  121. .post(`${SUBSCRIPTION_URL}?origin=confirmChangePlan`, body)
  122. .then(() => location.reload())
  123. .catch(() => console.log('something went wrong changing plan'))
  124. }
  125. return ($scope.cancel = () => $modalInstance.dismiss('cancel'))
  126. })
  127. App.controller('LeaveGroupModalController', function(
  128. $scope,
  129. $modalInstance,
  130. $http
  131. ) {
  132. $scope.confirmLeaveGroup = function() {
  133. $scope.inflight = true
  134. return $http({
  135. url: '/subscription/group/user',
  136. method: 'DELETE',
  137. params: { admin_user_id: $scope.admin_id, _csrf: window.csrfToken }
  138. })
  139. .then(() => location.reload())
  140. .catch(() => console.log('something went wrong changing plan'))
  141. }
  142. return ($scope.cancel = () => $modalInstance.dismiss('cancel'))
  143. })
  144. App.controller('GroupMembershipController', function($scope, $modal) {
  145. $scope.removeSelfFromGroup = function(admin_id) {
  146. $scope.admin_id = admin_id
  147. return $modal.open({
  148. templateUrl: 'LeaveGroupModalTemplate',
  149. controller: 'LeaveGroupModalController',
  150. scope: $scope
  151. })
  152. }
  153. })
  154. App.controller('RecurlySubscriptionController', function($scope) {
  155. const recurlyIsSetup = ensureRecurlyIsSetup()
  156. $scope.showChangePlanButton = recurlyIsSetup && !subscription.groupPlan
  157. $scope.recurlyLoadError = !recurlyIsSetup
  158. $scope.switchToDefaultView = () => {
  159. $scope.showCancellation = false
  160. $scope.showChangePlan = false
  161. }
  162. $scope.switchToDefaultView()
  163. $scope.switchToCancellationView = () => {
  164. $scope.showCancellation = true
  165. $scope.showChangePlan = false
  166. }
  167. $scope.switchToChangePlanView = () => {
  168. $scope.showCancellation = false
  169. $scope.showChangePlan = true
  170. }
  171. })
  172. App.controller('RecurlyCancellationController', function(
  173. $scope,
  174. RecurlyPricing,
  175. $http
  176. ) {
  177. if (!ensureRecurlyIsSetup()) return
  178. const subscription = window.subscription
  179. const sevenDaysTime = new Date()
  180. sevenDaysTime.setDate(sevenDaysTime.getDate() + 7)
  181. const freeTrialEndDate = new Date(subscription.recurly.trial_ends_at)
  182. const freeTrialInFuture = freeTrialEndDate > new Date()
  183. const freeTrialExpiresUnderSevenDays = freeTrialEndDate < sevenDaysTime
  184. const isMonthlyCollab =
  185. subscription.plan.planCode.indexOf('collaborator') !== -1 &&
  186. subscription.plan.planCode.indexOf('ann') === -1 &&
  187. !subscription.groupPlan
  188. const stillInFreeTrial = freeTrialInFuture && freeTrialExpiresUnderSevenDays
  189. if (isMonthlyCollab && stillInFreeTrial) {
  190. $scope.showExtendFreeTrial = true
  191. } else if (isMonthlyCollab && !stillInFreeTrial) {
  192. $scope.showDowngradeToStudent = true
  193. } else {
  194. $scope.showBasicCancel = true
  195. }
  196. const { currency, taxRate } = window.subscription.recurly
  197. $scope.studentPrice = '...' // Placeholder while we talk to recurly
  198. RecurlyPricing.loadDisplayPriceWithTax('student', currency, taxRate).then(
  199. price => {
  200. $scope.studentPrice = price
  201. }
  202. )
  203. $scope.downgradeToStudent = function() {
  204. const body = {
  205. plan_code: 'student',
  206. _csrf: window.csrfToken
  207. }
  208. $scope.inflight = true
  209. return $http
  210. .post(`${SUBSCRIPTION_URL}?origin=downgradeToStudent`, body)
  211. .then(() => location.reload())
  212. .catch(() => console.log('something went wrong changing plan'))
  213. }
  214. $scope.cancelSubscription = function() {
  215. const body = { _csrf: window.csrfToken }
  216. $scope.inflight = true
  217. return $http
  218. .post('/user/subscription/cancel', body)
  219. .then(() => (location.href = '/user/subscription/canceled'))
  220. .catch(() => console.log('something went wrong changing plan'))
  221. }
  222. $scope.extendTrial = function() {
  223. const body = { _csrf: window.csrfToken }
  224. $scope.inflight = true
  225. return $http
  226. .put('/user/subscription/extend', body)
  227. .then(() => location.reload())
  228. .catch(() => console.log('something went wrong changing plan'))
  229. }
  230. })
  231. })