From 3b1992b0b366177922afb91ba9cd87b958d6758d Mon Sep 17 00:00:00 2001 From: Jeff Chaves <58956521+chavesj@users.noreply.github.com> Date: Mon, 11 Jan 2021 16:44:48 -0500 Subject: [PATCH] feat: use new license manager endpoint to fetch license data (#176) * feat: use new license manager endpoint to fetch license data (learner-licenses) --- .../enterprise-user-subsidy/UserSubsidy.jsx | 2 +- .../enterprise-user-subsidy/data/hooks.jsx | 67 +++++++------------ .../enterprise-user-subsidy/data/service.js | 10 +-- .../tests/UserSubsidy.test.jsx | 26 ++++--- 4 files changed, 45 insertions(+), 60 deletions(-) diff --git a/src/components/enterprise-user-subsidy/UserSubsidy.jsx b/src/components/enterprise-user-subsidy/UserSubsidy.jsx index 48c310d9a0..62913ee958 100644 --- a/src/components/enterprise-user-subsidy/UserSubsidy.jsx +++ b/src/components/enterprise-user-subsidy/UserSubsidy.jsx @@ -11,7 +11,7 @@ export const UserSubsidyContext = createContext(); const UserSubsidy = ({ children }) => { const { subscriptionPlan, enterpriseConfig } = useContext(AppContext); - const [subscriptionLicense, isLoadingLicense] = useSubscriptionLicenseForUser(subscriptionPlan); + const [subscriptionLicense, isLoadingLicense] = useSubscriptionLicenseForUser(enterpriseConfig.uuid); const [offers, isLoadingOffers] = useOffers(enterpriseConfig.uuid); const isLoadingSubsidies = useMemo( diff --git a/src/components/enterprise-user-subsidy/data/hooks.jsx b/src/components/enterprise-user-subsidy/data/hooks.jsx index 27530451f9..a9f3dd146a 100644 --- a/src/components/enterprise-user-subsidy/data/hooks.jsx +++ b/src/components/enterprise-user-subsidy/data/hooks.jsx @@ -9,55 +9,38 @@ import offersReducer, { initialOfferState } from '../offers/data/reducer'; import { LICENSE_STATUS } from './constants'; import { fetchSubscriptionLicensesForUser } from './service'; -import { - hasValidStartExpirationDates, - isDefinedAndNotNull, - isDefinedAndNull, -} from '../../../utils/common'; import { features } from '../../../config'; -export function useSubscriptionLicenseForUser(subscriptionPlan) { +export function useSubscriptionLicenseForUser(enterpriseId) { const [license, setLicense] = useState(); const [isLoading, setIsLoading] = useState(true); useEffect(() => { - if (isDefinedAndNull(subscriptionPlan)) { - setIsLoading(false); - return; - } - - if (isDefinedAndNotNull(subscriptionPlan)) { - if (!hasValidStartExpirationDates(subscriptionPlan)) { - setIsLoading(false); - return; - } - - fetchSubscriptionLicensesForUser(subscriptionPlan.uuid) - .then((response) => { - const { results } = camelCaseObject(response.data); - const activated = results.filter(result => result.status === LICENSE_STATUS.ACTIVATED); - const assigned = results.filter(result => result.status === LICENSE_STATUS.ASSIGNED); - const revoked = results.filter(result => result.status === LICENSE_STATUS.REVOKED); - - if (activated.length) { - setLicense(activated.pop()); - } else if (assigned.length) { - setLicense(assigned.pop()); - } else if (revoked.length) { - setLicense(revoked.pop()); - } else { - setLicense(null); - } - }) - .catch((error) => { - logError(new Error(error)); + fetchSubscriptionLicensesForUser(enterpriseId) + .then((response) => { + const { results } = camelCaseObject(response.data); + const activated = results.filter(result => result.status === LICENSE_STATUS.ACTIVATED); + const assigned = results.filter(result => result.status === LICENSE_STATUS.ASSIGNED); + const revoked = results.filter(result => result.status === LICENSE_STATUS.REVOKED); + + if (activated.length) { + setLicense(activated.shift()); + } else if (assigned.length) { + setLicense(assigned.shift()); + } else if (revoked.length) { + setLicense(revoked.shift()); + } else { setLicense(null); - }) - .finally(() => { - setIsLoading(false); - }); - } - }, [subscriptionPlan]); + } + }) + .catch((error) => { + logError(new Error(error)); + setLicense(null); + }) + .finally(() => { + setIsLoading(false); + }); + }, [enterpriseId]); return [license, isLoading]; } diff --git a/src/components/enterprise-user-subsidy/data/service.js b/src/components/enterprise-user-subsidy/data/service.js index d989047a6a..0363c7edc3 100644 --- a/src/components/enterprise-user-subsidy/data/service.js +++ b/src/components/enterprise-user-subsidy/data/service.js @@ -1,13 +1,9 @@ -import qs from 'query-string'; -import { getAuthenticatedUser, getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; +import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; import { getConfig } from '@edx/frontend-platform/config'; -export function fetchSubscriptionLicensesForUser(subscriptionUuid) { +export function fetchSubscriptionLicensesForUser(enterpriseUuid) { const config = getConfig(); - const user = getAuthenticatedUser(); - const { email } = user; - const queryParams = { search: email }; - const url = `${config.LICENSE_MANAGER_URL}/api/v1/subscriptions/${subscriptionUuid}/license/?${qs.stringify(queryParams)}`; + const url = `${config.LICENSE_MANAGER_URL}/api/v1/learner-licenses/?enterprise_customer_uuid=${enterpriseUuid}`; const httpClient = getAuthenticatedHttpClient({ useCache: config.USE_API_CACHE, }); diff --git a/src/components/enterprise-user-subsidy/tests/UserSubsidy.test.jsx b/src/components/enterprise-user-subsidy/tests/UserSubsidy.test.jsx index d5e09a9da7..873e59726e 100644 --- a/src/components/enterprise-user-subsidy/tests/UserSubsidy.test.jsx +++ b/src/components/enterprise-user-subsidy/tests/UserSubsidy.test.jsx @@ -20,12 +20,16 @@ jest.mock('../../../config', () => ({ const TEST_SUBSCRIPTION_UUID = 'test-subscription-uuid'; const TEST_LICENSE_UUID = 'test-license-uuid'; const TEST_ENTERPRISE_SLUG = 'test-enterprise-slug'; +const TEST_ENTERPRISE_UUID = 'test-enterprise-uuid'; // eslint-disable-next-line react/prop-types const UserSubsidyWithAppContext = ({ contextValue = {}, children }) => ( @@ -59,13 +63,12 @@ describe('UserSubsidy', () => { beforeEach(() => { const promise = Promise.resolve({ data: { - data: { - count: 0, - results: [], - }, + count: 0, + results: [], }, }); fetchOffers.mockResolvedValueOnce(promise); + fetchSubscriptionLicensesForUser.mockResolvedValueOnce(promise); }); afterEach(() => { @@ -133,11 +136,12 @@ describe('UserSubsidy', () => { route: `/${TEST_ENTERPRISE_SLUG}`, }); expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledTimes(1); - expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledWith(TEST_SUBSCRIPTION_UUID); + expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledWith(TEST_ENTERPRISE_UUID); await waitFor(() => { expect(screen.queryByText('Has access: false')).toBeInTheDocument(); }); }); + test('with license, shows correct portal access', async () => { const promise = Promise.resolve({ data: { @@ -157,11 +161,12 @@ describe('UserSubsidy', () => { route: `/${TEST_ENTERPRISE_SLUG}`, }); expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledTimes(1); - expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledWith(TEST_SUBSCRIPTION_UUID); + expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledWith(TEST_ENTERPRISE_UUID); await waitFor(() => { expect(screen.queryByText('Has access: true')).toBeInTheDocument(); }); }); + test('provides license data', async () => { const promise = Promise.resolve({ data: { @@ -181,11 +186,12 @@ describe('UserSubsidy', () => { route: `/${TEST_ENTERPRISE_SLUG}`, }); expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledTimes(1); - expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledWith(TEST_SUBSCRIPTION_UUID); + expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledWith(TEST_ENTERPRISE_UUID); await waitFor(() => { expect(screen.queryByText(`License status: ${LICENSE_STATUS.ACTIVATED}`)).toBeInTheDocument(); }); }); + test('provides offers data', async () => { const promise = Promise.resolve({ data: { @@ -205,7 +211,7 @@ describe('UserSubsidy', () => { route: `/${TEST_ENTERPRISE_SLUG}`, }); expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledTimes(1); - expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledWith(TEST_SUBSCRIPTION_UUID); + expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledWith(TEST_ENTERPRISE_UUID); expect(fetchOffers).toHaveBeenCalledTimes(1); await waitFor(() => { expect(screen.queryByText('Offers count: 0')).toBeInTheDocument(); @@ -255,7 +261,7 @@ describe('UserSubsidy', () => { expect(screen.getByText(LOADING_SCREEN_READER_TEXT)).toBeInTheDocument(); expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledTimes(1); - expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledWith(TEST_SUBSCRIPTION_UUID); + expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledWith(TEST_ENTERPRISE_UUID); await waitFor(() => { expect(screen.getByTestId('did-i-render')).toBeInTheDocument();