Fix group plan localized pricing and force 2 decimal number for non-integer currency value (#8576)

GitOrigin-RevId: d6dedf7af04c79cef6b9cd39f5496610ffc52938
This commit is contained in:
M Fahru 2022-07-07 09:36:25 -04:00 committed by Copybot
parent 6c7fd1dad0
commit e97c56b105
4 changed files with 181 additions and 65 deletions

View file

@ -1,6 +1,7 @@
import getMeta from '../../../utils/meta' import getMeta from '../../../utils/meta'
import { swapModal } from '../../utils/swapModal' import { swapModal } from '../../utils/swapModal'
import * as eventTracking from '../../../infrastructure/event-tracking' import * as eventTracking from '../../../infrastructure/event-tracking'
import { createLocalizedGroupPlanPrice } from '../utils/group-plan-pricing'
function getFormValues() { function getFormValues() {
const modalEl = document.querySelector('[data-ol-group-plan-modal]') const modalEl = document.querySelector('[data-ol-group-plan-modal]')
@ -16,18 +17,16 @@ function getFormValues() {
} }
export function updateGroupModalPlanPricing() { export function updateGroupModalPlanPricing() {
const groupPlans = getMeta('ol-groupPlans')
const currencySymbols = getMeta('ol-currencySymbols')
const modalEl = document.querySelector('[data-ol-group-plan-modal]') const modalEl = document.querySelector('[data-ol-group-plan-modal]')
const { planCode, size, currency, usage } = getFormValues() const { planCode, size, currency, usage } = getFormValues()
const priceInCents = const { localizedPrice, localizedPerUserPrice } =
groupPlans[usage][planCode][currency][size].price_in_cents createLocalizedGroupPlanPrice({
const priceInUnit = (priceInCents / 100).toFixed() plan: planCode,
const currencySymbol = currencySymbols[currency] licenseSize: size,
const displayPrice = `${currencySymbol}${priceInUnit}` currency,
const perUserPrice = parseFloat((priceInCents / 100 / size).toFixed(2)) usage,
})
modalEl.querySelectorAll('[data-ol-group-plan-plan-code]').forEach(el => { modalEl.querySelectorAll('[data-ol-group-plan-plan-code]').forEach(el => {
el.hidden = el.getAttribute('data-ol-group-plan-plan-code') !== planCode el.hidden = el.getAttribute('data-ol-group-plan-plan-code') !== planCode
@ -36,10 +35,10 @@ export function updateGroupModalPlanPricing() {
el.hidden = el.getAttribute('data-ol-group-plan-usage') !== usage el.hidden = el.getAttribute('data-ol-group-plan-usage') !== usage
}) })
modalEl.querySelector('[data-ol-group-plan-display-price]').innerText = modalEl.querySelector('[data-ol-group-plan-display-price]').innerText =
displayPrice localizedPrice
modalEl.querySelector( modalEl.querySelector(
'[data-ol-group-plan-price-per-user]' '[data-ol-group-plan-price-per-user]'
).innerText = `${currencySymbol}${perUserPrice} per user` ).innerText = `${localizedPerUserPrice} per user`
modalEl.querySelector('[data-ol-group-plan-educational-discount]').hidden = modalEl.querySelector('[data-ol-group-plan-educational-discount]').hidden =
usage !== 'educational' usage !== 'educational'

View file

@ -0,0 +1,48 @@
import getMeta from '../../../utils/meta'
// plan: 'collaborator' or 'professional'
// the rest of available arguments can be seen in the groupPlans value
export function createLocalizedGroupPlanPrice({
plan,
licenseSize,
currency,
usage,
}) {
const groupPlans = getMeta('ol-groupPlans')
const currencySymbols = getMeta('ol-currencySymbols')
const priceInCents =
groupPlans[usage][plan][currency][licenseSize].price_in_cents
const price = priceInCents / 100
const perUserPrice = price / parseInt(licenseSize)
const strPrice = price.toFixed()
let strPerUserPrice = ''
if (Number.isInteger(perUserPrice)) {
strPerUserPrice = String(perUserPrice)
} else {
strPerUserPrice = perUserPrice.toFixed(2)
}
const currencySymbol = currencySymbols[currency]
switch (currencySymbol) {
case 'Fr':
return {
localizedPrice: `${currencySymbol} ${strPrice}`,
localizedPerUserPrice: `${currencySymbol} ${strPerUserPrice}`,
}
case 'kr':
return {
localizedPrice: `${strPrice} ${currencySymbol}`,
localizedPerUserPrice: `${strPerUserPrice} ${currencySymbol}`,
}
default: {
return {
localizedPrice: `${currencySymbol}${strPrice}`,
localizedPerUserPrice: `${currencySymbol}${strPerUserPrice}`,
}
}
}
}

View file

@ -1,93 +1,76 @@
import { updateGroupModalPlanPricing } from '../../../../features/plans/group-plan-modal' import { updateGroupModalPlanPricing } from '../../../../features/plans/group-plan-modal'
import '../../../../features/plans/plans-v2-group-plan-modal' import '../../../../features/plans/plans-v2-group-plan-modal'
import { createLocalizedGroupPlanPrice } from '../../../../features/plans/utils/group-plan-pricing'
import getMeta from '../../../../utils/meta' import getMeta from '../../../../utils/meta'
const MINIMUM_NUMBER_OF_LICENSES_EDUCATIONAL_DISCOUNT = 10 const MINIMUM_LICENSE_SIZE_EDUCATIONAL_DISCOUNT = 10
export function updateMainGroupPlanPricing() { export function updateMainGroupPlanPricing() {
const groupPlans = getMeta('ol-groupPlans') const currency = getMeta('ol-recommendedCurrency')
const currencySymbols = getMeta('ol-currencySymbols')
const currentCurrencyCode = getMeta('ol-recommendedCurrency')
const formEl = document.querySelector( const formEl = document.querySelector(
'[data-ol-plans-v2-license-picker-form]' '[data-ol-plans-v2-license-picker-form]'
) )
const numberOfLicenses = formEl.querySelector( const licenseSize = formEl.querySelector(
'[data-ol-plans-v2-license-picker-select]' '[data-ol-plans-v2-license-picker-select]'
).value ).value
const currency = currentCurrencyCode
const currencySymbol = currencySymbols[currency]
const usage = formEl.querySelector( const usage = formEl.querySelector(
'[data-ol-plans-v2-license-picker-educational-discount-input]' '[data-ol-plans-v2-license-picker-educational-discount-input]'
).checked ).checked
? 'educational' ? 'educational'
: 'enterprise' : 'enterprise'
function calculatePrice(plan) { const {
const priceInCents = localizedPrice: localizedPriceProfessional,
groupPlans[usage][plan][currency][numberOfLicenses].price_in_cents localizedPerUserPrice: localizedPerUserPriceProfessional,
const priceInUnit = (priceInCents / 100).toFixed() } = createLocalizedGroupPlanPrice({
const perUserPrice = (priceInCents / 100 / numberOfLicenses).toFixed(2) plan: 'professional',
licenseSize,
return { priceInUnit, perUserPrice } currency,
} usage,
})
const { const {
priceInUnit: priceInUnitProfessional, localizedPrice: localizedPriceCollaborator,
perUserPrice: perUserPriceProfessional, localizedPerUserPrice: localizedPerUserPriceCollaborator,
} = calculatePrice('professional') } = createLocalizedGroupPlanPrice({
let displayPriceProfessional = `${currencySymbol}${priceInUnitProfessional}` plan: 'collaborator',
let displayPerUserPriceProfessional = `${currencySymbol}${perUserPriceProfessional}` licenseSize,
currency,
const { usage,
priceInUnit: priceInUnitCollaborator, })
perUserPrice: perUserPriceCollaborator,
} = calculatePrice('collaborator')
let displayPriceCollaborator = `${currencySymbol}${priceInUnitCollaborator}`
let displayPerUserPriceCollaborator = `${currencySymbol}${perUserPriceCollaborator}`
if (currencySymbol === 'kr') {
displayPriceProfessional = `${priceInUnitProfessional} ${currencySymbol}`
displayPerUserPriceProfessional = `${perUserPriceProfessional} ${currencySymbol}`
displayPriceCollaborator = `${priceInUnitCollaborator} ${currencySymbol}`
displayPerUserPriceCollaborator = `${perUserPriceCollaborator} ${currencySymbol}`
} else if (currencySymbol === 'Fr') {
displayPriceProfessional = `${currencySymbol} ${priceInUnitProfessional}`
displayPerUserPriceProfessional = `${currencySymbol} ${perUserPriceProfessional}`
displayPriceCollaborator = `${currencySymbol} ${priceInUnitCollaborator}`
displayPerUserPriceCollaborator = `${currencySymbol} ${perUserPriceCollaborator}`
}
document.querySelector( document.querySelector(
'[data-ol-plans-v2-group-total-price="professional"]' '[data-ol-plans-v2-group-total-price="professional"]'
).innerText = displayPriceProfessional ).innerText = localizedPriceProfessional
document.querySelector(
'[data-ol-plans-v2-group-total-price="collaborator"]'
).innerText = displayPriceCollaborator
document.querySelector(
'[data-ol-plans-v2-group-price-per-user="collaborator"]'
).innerText = displayPerUserPriceCollaborator
document.querySelector( document.querySelector(
'[data-ol-plans-v2-group-price-per-user="professional"]' '[data-ol-plans-v2-group-price-per-user="professional"]'
).innerText = displayPerUserPriceProfessional ).innerText = localizedPerUserPriceProfessional
// educational discount can only be activated if numberOfLicenses is >= 10 document.querySelector(
const notEligibleForDiscount = '[data-ol-plans-v2-group-total-price="collaborator"]'
numberOfLicenses < MINIMUM_NUMBER_OF_LICENSES_EDUCATIONAL_DISCOUNT ).innerText = localizedPriceCollaborator
document.querySelector(
'[data-ol-plans-v2-group-price-per-user="collaborator"]'
).innerText = localizedPerUserPriceCollaborator
const notEligibleForEducationalDiscount =
licenseSize < MINIMUM_LICENSE_SIZE_EDUCATIONAL_DISCOUNT
formEl formEl
.querySelector( .querySelector(
'[data-ol-plans-v2-license-picker-educational-discount-label]' '[data-ol-plans-v2-license-picker-educational-discount-label]'
) )
.classList.toggle('disabled', notEligibleForDiscount) .classList.toggle('disabled', notEligibleForEducationalDiscount)
formEl.querySelector( formEl.querySelector(
'[data-ol-plans-v2-license-picker-educational-discount-input]' '[data-ol-plans-v2-license-picker-educational-discount-input]'
).disabled = notEligibleForDiscount ).disabled = notEligibleForEducationalDiscount
if (notEligibleForDiscount) { if (notEligibleForEducationalDiscount) {
// force disable educational discount checkbox // force disable educational discount checkbox
formEl.querySelector( formEl.querySelector(
'[data-ol-plans-v2-license-picker-educational-discount-input]' '[data-ol-plans-v2-license-picker-educational-discount-input]'

View file

@ -0,0 +1,86 @@
import { expect } from 'chai'
import { createLocalizedGroupPlanPrice } from '../../../../frontend/js/features/plans/utils/group-plan-pricing'
describe('group-plan-pricing', function () {
beforeEach(function () {
window.metaAttributesCache = window.metaAttributesCache || new Map()
window.metaAttributesCache.set('ol-groupPlans', {
enterprise: {
professional: {
CHF: {
2: {
price_in_cents: 10000,
},
},
DKK: {
2: {
price_in_cents: 20000,
},
},
USD: {
2: {
price_in_cents: 30000,
},
},
},
},
})
window.metaAttributesCache.set('ol-currencySymbols', {
CHF: 'Fr',
DKK: 'kr',
USD: '$',
})
})
afterEach(function () {
window.metaAttributesCache = new Map()
})
describe('createLocalizedGroupPlanPrice', function () {
describe('CHF currency', function () {
it('should return the correct localized price', function () {
const localizedGroupPlanPrice = createLocalizedGroupPlanPrice({
plan: 'professional',
currency: 'CHF',
licenseSize: '2',
usage: 'enterprise',
})
expect(localizedGroupPlanPrice).to.deep.equal({
localizedPrice: 'Fr 100',
localizedPerUserPrice: 'Fr 50',
})
})
})
describe('DKK currency', function () {
it('should return the correct localized price', function () {
const localizedGroupPlanPrice = createLocalizedGroupPlanPrice({
plan: 'professional',
currency: 'DKK',
licenseSize: '2',
usage: 'enterprise',
})
expect(localizedGroupPlanPrice).to.deep.equal({
localizedPrice: '200 kr',
localizedPerUserPrice: '100 kr',
})
})
})
describe('other supported currencies', function () {
it('should return the correct localized price', function () {
const localizedGroupPlanPrice = createLocalizedGroupPlanPrice({
plan: 'professional',
currency: 'USD',
licenseSize: '2',
usage: 'enterprise',
})
expect(localizedGroupPlanPrice).to.deep.equal({
localizedPrice: '$300',
localizedPerUserPrice: '$150',
})
})
})
})
})