Skip to content

Commit

Permalink
Merge pull request #262 from edx/trhodes/ent-4486
Browse files Browse the repository at this point in the history
fix: remove subscription plan API call; use new license subscriptionPlan attribute
  • Loading branch information
taliaedX authored May 4, 2021
2 parents a9b3f30 + bc2863d commit 945df36
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 75 deletions.
23 changes: 14 additions & 9 deletions src/components/enterprise-user-subsidy/UserSubsidy.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React, { createContext, useContext, useMemo } from 'react';
import React, {
createContext, useContext, useEffect, useMemo, useState,
} from 'react';
import PropTypes from 'prop-types';
import { AppContext } from '@edx/frontend-platform/react';
import { Container } from '@edx/paragon';

import { LoadingSpinner } from '../loading-spinner';

import {
useEnterpriseCustomerSubscriptionPlan,
useSubscriptionLicenseForUser,
useOffers,
} from './data/hooks';
Expand All @@ -16,19 +17,23 @@ export const UserSubsidyContext = createContext();

const UserSubsidy = ({ children }) => {
const { enterpriseConfig } = useContext(AppContext);
const [
subscriptionPlan,
isLoadingSubscriptionPlan,
] = useEnterpriseCustomerSubscriptionPlan(enterpriseConfig.uuid);
const [subscriptionLicense, isLoadingLicense] = useSubscriptionLicenseForUser(enterpriseConfig.uuid);
const [offers, isLoadingOffers] = useOffers(enterpriseConfig.uuid);
const [subscriptionPlan, setSubscriptionPlan] = useState();

useEffect(
() => {
setSubscriptionPlan(subscriptionLicense?.subscriptionPlan);
},
[subscriptionLicense],
);

const isLoadingSubsidies = useMemo(
() => {
const loadingStates = [isLoadingSubscriptionPlan, isLoadingLicense, isLoadingOffers];
const loadingStates = [isLoadingLicense, isLoadingOffers];
return loadingStates.includes(true);
},
[isLoadingSubscriptionPlan, isLoadingLicense, isLoadingOffers],
[isLoadingLicense, isLoadingOffers],
);

const contextValue = useMemo(
Expand All @@ -37,7 +42,7 @@ const UserSubsidy = ({ children }) => {
return {};
}
let hasAccessToPortal = false;
if (subscriptionPlan) {
if (subscriptionLicense) {
hasAccessToPortal = subscriptionLicense?.status === LICENSE_STATUS.ACTIVATED;
}
if (offers.offersCount > 0) {
Expand Down
29 changes: 0 additions & 29 deletions src/components/enterprise-user-subsidy/data/hooks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,10 @@ import offersReducer, { initialOfferState } from '../offers/data/reducer';

import { LICENSE_STATUS } from './constants';
import {
fetchEnterpriseCustomerSubscriptionPlan,
fetchSubscriptionLicensesForUser,
} from './service';
import { features } from '../../../config';

export function useEnterpriseCustomerSubscriptionPlan(enterpriseUUID) {
const [subscriptionPlan, setSubscriptionPlan] = useState();
const [isLoading, setIsLoading] = useState(true);

useEffect(() => {
if (!enterpriseUUID) {
setSubscriptionPlan(null);
return;
}
fetchEnterpriseCustomerSubscriptionPlan(enterpriseUUID)
.then((response) => {
const { results } = camelCaseObject(response.data);
const activePlans = results.filter(plan => plan.isActive);
const activeSubscriptionPlan = activePlans.pop() || null;
setSubscriptionPlan(activeSubscriptionPlan);
})
.catch((error) => {
logError(new Error(error));
setSubscriptionPlan(null);
})
.finally(() => {
setIsLoading(false);
});
}, [enterpriseUUID]);

return [subscriptionPlan, isLoading];
}

export function useSubscriptionLicenseForUser(enterpriseId) {
const [license, setLicense] = useState();
const [isLoading, setIsLoading] = useState(true);
Expand Down
12 changes: 0 additions & 12 deletions src/components/enterprise-user-subsidy/data/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@ import qs from 'query-string';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { getConfig } from '@edx/frontend-platform/config';

export function fetchEnterpriseCustomerSubscriptionPlan(enterpriseUUID) {
const queryParams = {
enterprise_customer_uuid: enterpriseUUID,
};
const config = getConfig();
const url = `${config.LICENSE_MANAGER_URL}/api/v1/learner-subscriptions/?${qs.stringify(queryParams)}`;
const httpClient = getAuthenticatedHttpClient({
useCache: config.USE_API_CACHE,
});
return httpClient.get(url);
}

export function fetchSubscriptionLicensesForUser(enterpriseUUID) {
const queryParams = {
enterprise_customer_uuid: enterpriseUUID,
Expand Down
34 changes: 9 additions & 25 deletions src/components/enterprise-user-subsidy/tests/UserSubsidy.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { renderWithRouter } from '../../../utils/tests';
import { LICENSE_STATUS, LOADING_SCREEN_READER_TEXT } from '../data/constants';
import {
fetchSubscriptionLicensesForUser,
fetchEnterpriseCustomerSubscriptionPlan,
} from '../data/service';
import { fetchOffers } from '../offers/data/service';

Expand Down Expand Up @@ -75,7 +74,6 @@ describe('UserSubsidy', () => {
};
fetchOffers.mockResolvedValueOnce(response);
fetchSubscriptionLicensesForUser.mockResolvedValueOnce(response);
fetchEnterpriseCustomerSubscriptionPlan.mockResolvedValueOnce(response);
});

afterEach(() => {
Expand Down Expand Up @@ -124,7 +122,6 @@ describe('UserSubsidy', () => {
results: [],
},
};
fetchEnterpriseCustomerSubscriptionPlan.mockResolvedValueOnce(response);
fetchSubscriptionLicensesForUser.mockResolvedValueOnce(response);
const Component = (
<UserSubsidyWithAppContext>
Expand All @@ -134,7 +131,6 @@ describe('UserSubsidy', () => {
renderWithRouter(Component, {
route: `/${TEST_ENTERPRISE_SLUG}`,
});
expect(fetchEnterpriseCustomerSubscriptionPlan).toHaveBeenCalledWith(TEST_ENTERPRISE_UUID);
expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledWith(TEST_ENTERPRISE_UUID);
expect(fetchOffers).toHaveBeenCalledWith({
enterprise_uuid: TEST_ENTERPRISE_UUID,
Expand All @@ -147,11 +143,6 @@ describe('UserSubsidy', () => {
});

test('with license, shows correct portal access', async () => {
fetchEnterpriseCustomerSubscriptionPlan.mockResolvedValueOnce({
data: {
results: [mockSubscriptionPlan],
},
});
fetchSubscriptionLicensesForUser.mockResolvedValueOnce({
data: {
results: [{
Expand All @@ -168,7 +159,6 @@ describe('UserSubsidy', () => {
renderWithRouter(Component, {
route: `/${TEST_ENTERPRISE_SLUG}`,
});
expect(fetchEnterpriseCustomerSubscriptionPlan).toHaveBeenCalledWith(TEST_ENTERPRISE_UUID);
expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledWith(TEST_ENTERPRISE_UUID);
expect(fetchOffers).toHaveBeenCalledWith({
enterprise_uuid: TEST_ENTERPRISE_UUID,
Expand All @@ -182,16 +172,12 @@ describe('UserSubsidy', () => {
});

test('provides license data', async () => {
fetchEnterpriseCustomerSubscriptionPlan.mockResolvedValueOnce({
data: {
results: [mockSubscriptionPlan],
},
});
fetchSubscriptionLicensesForUser.mockResolvedValueOnce({
data: {
results: [{
uuid: TEST_LICENSE_UUID,
status: LICENSE_STATUS.ACTIVATED,
subscriptionPlan: mockSubscriptionPlan,
}],
},
});
Expand All @@ -203,7 +189,6 @@ describe('UserSubsidy', () => {
renderWithRouter(Component, {
route: `/${TEST_ENTERPRISE_SLUG}`,
});
expect(fetchEnterpriseCustomerSubscriptionPlan).toHaveBeenCalledWith(TEST_ENTERPRISE_UUID);
expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledWith(TEST_ENTERPRISE_UUID);
expect(fetchOffers).toHaveBeenCalledWith({
enterprise_uuid: TEST_ENTERPRISE_UUID,
Expand All @@ -217,16 +202,12 @@ describe('UserSubsidy', () => {
});

test('provides offers data', async () => {
fetchEnterpriseCustomerSubscriptionPlan.mockResolvedValueOnce({
data: {
results: [mockSubscriptionPlan],
},
});
fetchSubscriptionLicensesForUser.mockResolvedValueOnce({
data: {
results: [{
uuid: TEST_LICENSE_UUID,
status: LICENSE_STATUS.ACTIVATED,
subscriptionPlan: mockSubscriptionPlan,
}],
},
});
Expand All @@ -238,7 +219,6 @@ describe('UserSubsidy', () => {
renderWithRouter(Component, {
route: `/${TEST_ENTERPRISE_SLUG}`,
});
expect(fetchEnterpriseCustomerSubscriptionPlan).toHaveBeenCalledWith(TEST_ENTERPRISE_UUID);
expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledWith(TEST_ENTERPRISE_UUID);
expect(fetchOffers).toHaveBeenCalledTimes(1);
expect(fetchOffers).toHaveBeenCalledWith({ enterprise_uuid: TEST_ENTERPRISE_UUID, full_discount_only: 'True', is_active: 'True' });
Expand All @@ -251,9 +231,13 @@ describe('UserSubsidy', () => {

describe('with subscription plan that has started, but not yet ended, no offers', () => {
beforeEach(() => {
fetchEnterpriseCustomerSubscriptionPlan.mockResolvedValueOnce({
fetchSubscriptionLicensesForUser.mockResolvedValueOnce({
data: {
results: [mockSubscriptionPlan],
results: [{
uuid: TEST_LICENSE_UUID,
status: LICENSE_STATUS.ACTIVATED,
subscriptionPlan: mockSubscriptionPlan,
}],
},
});
fetchOffers.mockResolvedValueOnce({
Expand All @@ -274,6 +258,7 @@ describe('UserSubsidy', () => {
results: [{
uuid: TEST_LICENSE_UUID,
status: LICENSE_STATUS.ACTIVATED,
subscriptionPlan: mockSubscriptionPlan,
}],
},
});
Expand All @@ -290,7 +275,6 @@ describe('UserSubsidy', () => {
// assert component is initially loading
expect(screen.getByText(LOADING_SCREEN_READER_TEXT)).toBeInTheDocument();

expect(fetchEnterpriseCustomerSubscriptionPlan).toHaveBeenCalledWith(TEST_ENTERPRISE_UUID);
expect(fetchSubscriptionLicensesForUser).toHaveBeenCalledWith(TEST_ENTERPRISE_UUID);

await waitFor(() => {
Expand Down

0 comments on commit 945df36

Please sign in to comment.