Skip to content

Commit

Permalink
feat: use new license manager endpoint to fetch license data (#176)
Browse files Browse the repository at this point in the history
* feat: use new license manager endpoint to fetch license data (learner-licenses)
  • Loading branch information
chavesj authored Jan 11, 2021
1 parent d584d94 commit 3b1992b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/components/enterprise-user-subsidy/UserSubsidy.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
67 changes: 25 additions & 42 deletions src/components/enterprise-user-subsidy/data/hooks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
10 changes: 3 additions & 7 deletions src/components/enterprise-user-subsidy/data/service.js
Original file line number Diff line number Diff line change
@@ -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,
});
Expand Down
26 changes: 16 additions & 10 deletions src/components/enterprise-user-subsidy/tests/UserSubsidy.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => (
<AppContext.Provider
value={{
enterpriseConfig: { slug: TEST_ENTERPRISE_SLUG },
enterpriseConfig: {
slug: TEST_ENTERPRISE_SLUG,
uuid: TEST_ENTERPRISE_UUID,
},
...contextValue,
}}
>
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 3b1992b

Please sign in to comment.