Skip to content

feat: use discount info endpoint for streak discount information #1763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CREDIT_HELP_LINK_URL=''
CSRF_TOKEN_API_PATH=''
DISCOVERY_API_BASE_URL=''
DISCUSSIONS_MFE_BASE_URL=''
DISCOUNT_CODE_INFO_URL=''
ECOMMERCE_BASE_URL=''
ENABLE_JUMPNAV='true'
ENABLE_NOTICES=''
Expand Down
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CREDIT_HELP_LINK_URL='https://help.edx.org/edxlearner/s/article/Can-I-receive-co
CSRF_TOKEN_API_PATH='/csrf/api/v1/token'
DISCOVERY_API_BASE_URL='http://localhost:18381'
DISCUSSIONS_MFE_BASE_URL='http://localhost:2002'
DISCOUNT_CODE_INFO_URL=''
ECOMMERCE_BASE_URL='http://localhost:18130'
ENABLE_JUMPNAV='true'
ENABLE_NOTICES=''
Expand Down
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CREDIT_HELP_LINK_URL='https://help.edx.org/edxlearner/s/article/Can-I-receive-co
CSRF_TOKEN_API_PATH='/csrf/api/v1/token'
DISCOVERY_API_BASE_URL='http://localhost:18381'
DISCUSSIONS_MFE_BASE_URL='http://localhost:2002'
DISCOUNT_CODE_INFO_URL='http://localhost:8140/lms/discount-code-info/'
ECOMMERCE_BASE_URL='http://localhost:18130'
ENABLE_JUMPNAV='true'
ENABLE_NOTICES=''
Expand Down
1 change: 1 addition & 0 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ initialize({
CREDENTIALS_BASE_URL: process.env.CREDENTIALS_BASE_URL || null,
CREDIT_HELP_LINK_URL: process.env.CREDIT_HELP_LINK_URL || null,
DISCUSSIONS_MFE_BASE_URL: process.env.DISCUSSIONS_MFE_BASE_URL || null,
DISCOUNT_CODE_INFO_URL: process.env.DISCOUNT_CODE_INFO_URL || null,
ENTERPRISE_LEARNER_PORTAL_HOSTNAME: process.env.ENTERPRISE_LEARNER_PORTAL_HOSTNAME || null,
ENTERPRISE_LEARNER_PORTAL_URL: process.env.ENTERPRISE_LEARNER_PORTAL_URL || null,
ENABLE_JUMPNAV: process.env.ENABLE_JUMPNAV || null,
Expand Down
58 changes: 41 additions & 17 deletions src/shared/streak-celebration/StreakCelebrationModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ async function calculateVoucherDiscount(voucher, sku, username) {
.then(res => camelCaseObject(res));
}

async function getDiscountCodeInfo(code, courseId) {
const params = new URLSearchParams();
params.append('code', code);
params.append('course_run_key', courseId);
const url = `${getConfig().DISCOUNT_CODE_INFO_URL}?${params.toString()}`;

return getAuthenticatedHttpClient().get(url)
.then(res => camelCaseObject(res));
}

const CloseText = ({ intl }) => (
<span>
{intl.formatMessage(messages.streakButton)}
Expand Down Expand Up @@ -83,34 +93,48 @@ const StreakModal = ({

// Ask ecommerce to calculate discount savings
useEffect(() => {
if (streakDiscountCouponEnabled && verifiedMode && getConfig().ECOMMERCE_BASE_URL) {
calculateVoucherDiscount(discountCode, verifiedMode.sku, username)
.then(
(result) => {
(async () => {
let newDiscountPercentage = 0;
try {
if (streakDiscountCouponEnabled && verifiedMode) {
if (getConfig().DISCOUNT_CODE_INFO_URL) {
const result = await getDiscountCodeInfo(discountCode, courseId);
const { isApplicable, discountPercentage } = result.data;
if (isApplicable) {
// Just store the percent, because ecommerce doesn't give us the
// currency symbol to use, so we want to use the symbol that LMS
// gives us. And we don't want to assume ecommerce's currency is
// the same as the LMS. So we'll keep using the values in
// verifiedMode, just multiplied by the received percentage.
newDiscountPercentage = discountPercentage;
sendTrackEvent('edx.bi.course.streak_discount_enabled', {
course_id: courseId,
sku: verifiedMode.sku,
});
}
} else if (getConfig().ECOMMERCE_BASE_URL) {
const result = await calculateVoucherDiscount(discountCode, verifiedMode.sku, username);
const { totalInclTax, totalInclTaxExclDiscounts } = result.data;
if (totalInclTaxExclDiscounts && totalInclTax !== totalInclTaxExclDiscounts) {
// Just store the percent (rather than using these values directly), because ecommerce doesn't give us
// the currency symbol to use, so we want to use the symbol that LMS gives us. And I don't want to assume
// ecommerce's currency is the same as the LMS. So we'll keep using the values in verifiedMode, just
// multiplied by the calculated percentage.
setDiscountPercent(1 - totalInclTax / totalInclTaxExclDiscounts);
newDiscountPercentage = 1 - totalInclTax / totalInclTaxExclDiscounts;
sendTrackEvent('edx.bi.course.streak_discount_enabled', {
course_id: courseId,
sku: verifiedMode.sku,
});
} else {
setDiscountPercent(0);
}
},
() => {
// ignore any errors - we just won't show the discount to the user then
setDiscountPercent(0);
},
);
} else {
setDiscountPercent(0);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}
}
} catch {
// ignore any errors - we just won't show the discount to the user then
} finally {
setDiscountPercent(newDiscountPercentage);
}
})();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [streakDiscountCouponEnabled, username, verifiedMode]);

if (!isStreakCelebrationOpen) {
Expand Down
9 changes: 6 additions & 3 deletions src/shared/streak-celebration/StreakCelebrationModal.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ describe('Loaded Tab Page', () => {
let mockData;
let testStore;
let axiosMock;
const calculateUrl = `${getConfig().ECOMMERCE_BASE_URL}/api/v2/baskets/calculate/?code=ZGY11119949&sku=8CF08E5&username=MockUser`;
const courseMetadata = Factory.build('courseMetadata');
const courseHomeMetadata = Factory.build('courseHomeMetadata', { celebrations: { streak_length_to_celebrate: 3 } });
const params = new URLSearchParams();
params.append('code', 'ZGY11119949');
params.append('course_run_key', courseMetadata.id);
const calculateUrl = `${getConfig().DISCOUNT_CODE_INFO_URL}?${params.toString()}`;

function setDiscount(percent) {
mockData.streakDiscountCouponEnabled = true;
axiosMock.onGet(calculateUrl).reply(200, {
total_incl_tax: 100 - percent,
total_incl_tax_excl_discounts: 100,
isApplicable: true,
discountPercentage: percent / 100,
});
}

Expand Down