From 12b1c2c501a7721c6fba217312d458ce24e12571 Mon Sep 17 00:00:00 2001 From: Hamzah Ullah Date: Thu, 14 Dec 2023 16:51:04 -0500 Subject: [PATCH] fix: update redirect logic to take into account available coupon codes (#906) * fix: update redirect logic to take into account available coupon codes * chore: refactor * chore: test * chore: Pr fixes --- src/components/course/data/utils.jsx | 1 + src/components/course/routes/CourseAbout.jsx | 2 + .../course/routes/tests/CourseAbout.test.jsx | 3 ++ .../data/tests/utils.test.js | 38 +++++++++++++++++++ .../enterprise-offers/data/utils.js | 15 +++++--- src/components/my-career/CategoryCard.jsx | 10 ++--- 6 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/components/course/data/utils.jsx b/src/components/course/data/utils.jsx index e95797200e..fb0ee0d2a3 100644 --- a/src/components/course/data/utils.jsx +++ b/src/components/course/data/utils.jsx @@ -434,6 +434,7 @@ export const getCouponCodesDisabledEnrollmentReasonType = ({ const applicableCouponsToCatalog = couponsOverview?.data?.results.filter( coupon => catalogsWithCourse.includes(coupon.enterpriseCatalogUuid), ) || []; + const hasCouponsApplicableToCourse = applicableCouponsToCatalog.length > 0; if (!hasCouponsApplicableToCourse) { return undefined; diff --git a/src/components/course/routes/CourseAbout.jsx b/src/components/course/routes/CourseAbout.jsx index 749e8ff8ca..e249016ee0 100644 --- a/src/components/course/routes/CourseAbout.jsx +++ b/src/components/course/routes/CourseAbout.jsx @@ -29,6 +29,7 @@ const CourseAbout = () => { enterpriseOffers, subscriptionPlan, subscriptionLicense, + couponCodes, } = useContext(UserSubsidyContext); const isCourseAssigned = useIsCourseAssigned(redeemableLearnerCreditPolicies?.learnerContentAssignments, course?.key); @@ -37,6 +38,7 @@ const CourseAbout = () => { enterpriseOffers, subscriptionPlan, subscriptionLicense, + couponCodes.couponCodes, ); const featuredHideCourseSearch = features.FEATURE_ENABLE_TOP_DOWN_ASSIGNMENT && hideCourseSearch; diff --git a/src/components/course/routes/tests/CourseAbout.test.jsx b/src/components/course/routes/tests/CourseAbout.test.jsx index 094b0e7875..102897805c 100644 --- a/src/components/course/routes/tests/CourseAbout.test.jsx +++ b/src/components/course/routes/tests/CourseAbout.test.jsx @@ -65,6 +65,9 @@ const initialUserSubsidyState = { enterpriseOffers: [], subscriptionPlan: {}, subscriptionLicense: {}, + couponCodes: { + couponCodes: [], + }, }; const CourseAboutWrapper = ({ diff --git a/src/components/enterprise-user-subsidy/enterprise-offers/data/tests/utils.test.js b/src/components/enterprise-user-subsidy/enterprise-offers/data/tests/utils.test.js index b061497065..8715925ce8 100644 --- a/src/components/enterprise-user-subsidy/enterprise-offers/data/tests/utils.test.js +++ b/src/components/enterprise-user-subsidy/enterprise-offers/data/tests/utils.test.js @@ -364,6 +364,7 @@ describe('isDisableCourseSearch', () => { subscriptionLicenses: { status: LICENSE_STATUS.ACTIVATED, }, + couponCodes: [], }, { isCourseSearchDisabled: false, @@ -400,6 +401,7 @@ describe('isDisableCourseSearch', () => { subscriptionLicenses: { status: LICENSE_STATUS.ACTIVATED, }, + couponCodes: [], }, { isCourseSearchDisabled: false, @@ -430,6 +432,38 @@ describe('isDisableCourseSearch', () => { subscriptionLicenses: { status: LICENSE_STATUS.ACTIVATED, }, + couponCodes: [], + }, + { + isCourseSearchDisabled: false, + redeemableLearnerCreditPolicies: { + redeemablePolicies: [ + { + policyType: POLICY_TYPES.ASSIGNED_CREDIT, + learnerContentAssignments: [ + { state: ASSIGNMENT_TYPES.ALLOCATED }, + ], + }, + ], + learnerContentAssignments: { + assignments: [], + hasAssignments: false, + activeAssignments: [], + hasActiveAssignments: false, + }, + }, + enterpriseOffers: [{ + isCurrent: true, + }], + subscriptionPlan: { + isActive: false, + }, + subscriptionLicenses: { + status: LICENSE_STATUS.ACTIVATED, + }, + couponCodes: [ + { available: true }, + ], }, { isCourseSearchDisabled: true, @@ -458,6 +492,7 @@ describe('isDisableCourseSearch', () => { subscriptionLicenses: { status: LICENSE_STATUS.ACTIVATED, }, + couponCodes: [], }, { isCourseSearchDisabled: true, @@ -492,6 +527,7 @@ describe('isDisableCourseSearch', () => { subscriptionLicenses: { status: LICENSE_STATUS.ACTIVATED, }, + couponCodes: [], }, ])('isCourseSearchDisabled - (%p), (%s)', ({ isCourseSearchDisabled, @@ -499,12 +535,14 @@ describe('isDisableCourseSearch', () => { enterpriseOffers, subscriptionPlan, subscriptionLicenses, + couponCodes, }) => { const isDisableSearch = isDisableCourseSearch( redeemableLearnerCreditPolicies, enterpriseOffers, subscriptionPlan, subscriptionLicenses, + couponCodes, ); expect(isDisableSearch).toEqual(isCourseSearchDisabled); }); diff --git a/src/components/enterprise-user-subsidy/enterprise-offers/data/utils.js b/src/components/enterprise-user-subsidy/enterprise-offers/data/utils.js index d6f1e6e153..e90cc225d8 100644 --- a/src/components/enterprise-user-subsidy/enterprise-offers/data/utils.js +++ b/src/components/enterprise-user-subsidy/enterprise-offers/data/utils.js @@ -115,6 +115,7 @@ export const transformEnterpriseOffer = (offer) => { * @param {Array} enterpriseOffers - Array of enterprise offers. * @param {Object} subscriptionPlan - Subscription plan object. * @param {Object} subscriptionLicense - Subscription license object. + * @param {Array} couponCodes - Array of couponCodes from the UserSubsidyContext, couponCodes.couponCodes * * @returns {boolean} Returns true if course search should be disabled, otherwise false. */ @@ -123,12 +124,12 @@ export const isDisableCourseSearch = ( enterpriseOffers, subscriptionPlan, subscriptionLicense, + couponCodes, ) => { const { redeemablePolicies, learnerContentAssignments, } = redeemableLearnerCreditPolicies || {}; - const nonAssignablePolicyTypes = redeemablePolicies.filter( item => item.policyType !== POLICY_TYPES.ASSIGNED_CREDIT, ); @@ -137,16 +138,18 @@ export const isDisableCourseSearch = ( } const hasActiveSubPlan = subscriptionPlan?.isActive && subscriptionLicense?.status === LICENSE_STATUS.ACTIVATED; - const activeOffers = enterpriseOffers?.filter(item => item?.isCurrent); - + const hasCouponCodes = couponCodes.filter(code => !!code?.available).length > 0; const allocatedOrAcceptedAssignments = learnerContentAssignments.assignments .filter(item => [ASSIGNMENT_TYPES.ALLOCATED, ASSIGNMENT_TYPES.ACCEPTED].includes(item.state)); + const activeOffers = enterpriseOffers?.filter(item => item?.isCurrent); - if (allocatedOrAcceptedAssignments?.length === 0) { + if (hasActiveSubPlan) { return false; } - - if (hasActiveSubPlan) { + if (hasCouponCodes) { + return false; + } + if (allocatedOrAcceptedAssignments?.length === 0) { return false; } diff --git a/src/components/my-career/CategoryCard.jsx b/src/components/my-career/CategoryCard.jsx index a2d8759ebf..4e5bb5a66d 100644 --- a/src/components/my-career/CategoryCard.jsx +++ b/src/components/my-career/CategoryCard.jsx @@ -1,13 +1,9 @@ import React, { - useMemo, useState, useEffect, useContext, + useContext, useEffect, useMemo, useState, } from 'react'; import PropTypes from 'prop-types'; -import { - Card, - Button, - useToggle, -} from '@edx/paragon'; +import { Button, Card, useToggle } from '@edx/paragon'; import { getConfig } from '@edx/frontend-platform/config'; import algoliasearch from 'algoliasearch/lite'; import { AppContext } from '@edx/frontend-platform/react'; @@ -31,12 +27,14 @@ const CategoryCard = ({ topCategory }) => { enterpriseOffers, subscriptionPlan, subscriptionLicense, + couponCodes, } = useContext(UserSubsidyContext); const hideCourseSearch = isDisableCourseSearch( redeemableLearnerCreditPolicies, enterpriseOffers, subscriptionPlan, subscriptionLicense, + couponCodes.couponCodes, ); const featuredHideCourseSearch = features.FEATURE_ENABLE_TOP_DOWN_ASSIGNMENT && hideCourseSearch;