Skip to content

Commit

Permalink
refactor: extractEnterpriseId to extractEnterpriseCustomer (#1056)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstankiewicz authored Apr 15, 2024
1 parent 750a30d commit 6f4b6d1
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { queryEnterpriseLearner } from './queries';
* @param {string} params.enterpriseSlug - The enterprise slug.
* @returns {Promise<string>} - The enterprise ID to use for subsquent queries in route loaders.
*/
async function extractEnterpriseId({
async function extractEnterpriseCustomer({
queryClient,
authenticatedUser,
enterpriseSlug,
Expand All @@ -26,24 +26,24 @@ async function extractEnterpriseId({
// If there is no slug provided (i.e., on the root page route `/`), use
// the currently active enterprise customer user.
if (!enterpriseSlug) {
return activeEnterpriseCustomer.uuid;
return activeEnterpriseCustomer;
}

const foundEnterpriseIdForSlug = allLinkedEnterpriseCustomerUsers.find(
const foundEnterpriseCustomerForSlug = allLinkedEnterpriseCustomerUsers.find(
(enterpriseCustomerUser) => enterpriseCustomerUser.enterpriseCustomer?.slug === enterpriseSlug,
)?.enterpriseCustomer.uuid;
)?.enterpriseCustomer;

// Otherwise, there is a slug provided for a specific enterprise customer. If the
// user is linked to the enterprise customer for the given slug, return the enterprise
// enterprise ID for that enterprise customer. If there is no linked enterprise customer
// for the given slug, but the user is staff, return the enterprise ID from the staff-only
// enterprise customer metadata.
if (foundEnterpriseIdForSlug || staffEnterpriseCustomer) {
return foundEnterpriseIdForSlug || staffEnterpriseCustomer.uuid;
if (foundEnterpriseCustomerForSlug || staffEnterpriseCustomer) {
return foundEnterpriseCustomerForSlug || staffEnterpriseCustomer;
}

// If no enterprise customer is found for the given user/slug, throw an error.
throw new Error(`Could not find enterprise customer for user ${authenticatedUser.userId} and slug ${enterpriseSlug}`);
throw new Error(`Could not find enterprise customer for slug ${enterpriseSlug}`);
}

export default extractEnterpriseId;
export default extractEnterpriseCustomer;
2 changes: 1 addition & 1 deletion src/components/app/data/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { default as extractEnterpriseId } from './extractEnterpriseId';
export { default as extractEnterpriseCustomer } from './extractEnterpriseCustomer';
export { default as queries } from './queryKeyFactory';

export * from './queries';
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { screen, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import { IntlProvider } from '@edx/frontend-platform/i18n';
import { getConfig } from '@edx/frontend-platform';

import { renderWithRouterProvider } from '../../../../../utils/tests';
import makeEnterpriseInviteLoader from '../enterpriseInviteLoader';
import {
extractEnterpriseId, postLinkEnterpriseLearner,
extractEnterpriseCustomer, postLinkEnterpriseLearner,
} from '../../../data';
import EnterpriseInviteRoute from '../../EnterpriseInviteRoute';
import { ensureAuthenticatedUser } from '../../data';
Expand All @@ -16,7 +17,7 @@ jest.mock('../../data', () => ({
}));
jest.mock('../../../data', () => ({
...jest.requireActual('../../../data'),
extractEnterpriseId: jest.fn(),
extractEnterpriseCustomer: jest.fn(),
postLinkEnterpriseLearner: jest.fn(),
}));
jest.mock('@edx/frontend-platform/auth', () => ({
Expand All @@ -29,8 +30,8 @@ jest.mock('@edx/frontend-platform/logging', () => ({
getLoggingService: jest.fn(),
logError: jest.fn(),
}));
jest.mock('@edx/frontend-platform/config', () => ({
...jest.requireActual('@edx/frontend-platform/config'),
jest.mock('@edx/frontend-platform', () => ({
...jest.requireActual('@edx/frontend-platform'),
getConfig: jest.fn().mockReturnValue({
LEARNER_SUPPORT_URL: 'https://test-learner-support-url',
MARKETING_SITE_BASE_URL: 'https://test-marketing-site-base-url',
Expand All @@ -40,10 +41,14 @@ jest.mock('@edx/frontend-platform/config', () => ({
const mockEnterpriseId = 'test-enterprise-uuid';
const mockEnterpriseSlug = 'test-enterprise-slug';
const mockEnterpriseCustomerInviteKey = 'test-enterprise-customer-invite-key';
extractEnterpriseId.mockResolvedValue(mockEnterpriseId);
extractEnterpriseCustomer.mockResolvedValue({ uuid: mockEnterpriseId });
postLinkEnterpriseLearner.mockResolvedValue({
enterpriseCustomerSlug: mockEnterpriseSlug,
});
getConfig.mockReturnValue({
LEARNER_SUPPORT_URL: 'https://test-learner-support-url',
MARKETING_SITE_BASE_URL: 'https://test-marketing-site-base-url',
});

const EnterpriseInviteRouteWrapper = () => (
<IntlProvider locale="en">
Expand Down
6 changes: 3 additions & 3 deletions src/components/app/routes/loaders/tests/rootLoader.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { renderWithRouterProvider } from '../../../../../utils/tests';
import makeRootLoader from '../rootLoader';
import { ensureAuthenticatedUser } from '../../data';
import {
extractEnterpriseId,
extractEnterpriseCustomer,
queryBrowseAndRequestConfiguration,
queryContentHighlightsConfiguration,
queryCouponCodeRequests,
Expand All @@ -28,7 +28,7 @@ jest.mock('../../data', () => ({
}));
jest.mock('../../../data', () => ({
...jest.requireActual('../../../data'),
extractEnterpriseId: jest.fn(),
extractEnterpriseCustomer: jest.fn(),
updateUserActiveEnterprise: jest.fn(),
}));

Expand Down Expand Up @@ -64,7 +64,7 @@ describe('rootLoader', () => {
jest.clearAllMocks();
localStorage.clear();
ensureAuthenticatedUser.mockResolvedValue(mockAuthenticatedUser);
extractEnterpriseId.mockResolvedValue(mockEnterpriseCustomer.uuid);
extractEnterpriseCustomer.mockResolvedValue(mockEnterpriseCustomer);
});

it('does nothing if the user is not authenticated', async () => {
Expand Down
31 changes: 16 additions & 15 deletions src/components/course/data/courseLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
queryCanRedeem,
queryCourseMetadata,
queryEnterpriseCourseEnrollments,
extractEnterpriseId,
extractEnterpriseCustomer,
queryRedeemablePolicies,
getLateRedemptionBufferDays,
querySubscriptions,
Expand Down Expand Up @@ -47,23 +47,22 @@ export default function makeCourseLoader(queryClient) {
// need to replace it with `+` again to be a valid course run key.
const courseRunKey = requestUrl.searchParams.get('course_run_key')?.replaceAll(' ', '+');

const enterpriseId = await extractEnterpriseId({
const enterpriseCustomer = await extractEnterpriseCustomer({
queryClient,
authenticatedUser,
enterpriseSlug,
});

const subsidyQueries = Promise.all([
queryClient.ensureQueryData(queryRedeemablePolicies({
enterpriseUuid: enterpriseId,
enterpriseUuid: enterpriseCustomer.uuid,
lmsUserId: authenticatedUser.userId,
})),
queryClient.ensureQueryData(querySubscriptions(enterpriseId)),
queryClient.ensureQueryData(queryEnterpriseLearnerOffers(enterpriseId)),
queryClient.ensureQueryData(queryCouponCodes(enterpriseId)),
queryClient.ensureQueryData(queryLicenseRequests(enterpriseId, authenticatedUser.email)),
queryClient.ensureQueryData(queryCouponCodeRequests(enterpriseId, authenticatedUser.email)),
queryClient.ensureQueryData(queryBrowseAndRequestConfiguration(enterpriseId)),
queryClient.ensureQueryData(querySubscriptions(enterpriseCustomer.uuid)),
queryClient.ensureQueryData(queryEnterpriseLearnerOffers(enterpriseCustomer.uuid)),
queryClient.ensureQueryData(queryCouponCodes(enterpriseCustomer.uuid)),
queryClient.ensureQueryData(queryLicenseRequests(enterpriseCustomer.uuid, authenticatedUser.email)),
queryClient.ensureQueryData(queryCouponCodeRequests(enterpriseCustomer.uuid, authenticatedUser.email)),
queryClient.ensureQueryData(queryBrowseAndRequestConfiguration(enterpriseCustomer.uuid)),
]);

await Promise.all([
Expand All @@ -75,15 +74,17 @@ export default function makeCourseLoader(queryClient) {
return null;
}
const redeemableLearnerCreditPolicies = await queryClient.ensureQueryData(queryRedeemablePolicies({
enterpriseUuid: enterpriseId,
enterpriseUuid: enterpriseCustomer.uuid,
lmsUserId: authenticatedUser.userId,
}));
const isEnrollableBufferDays = getLateRedemptionBufferDays(redeemableLearnerCreditPolicies.redeemablePolicies);
return queryClient.ensureQueryData(queryCanRedeem(enterpriseId, courseMetadata, isEnrollableBufferDays));
return queryClient.ensureQueryData(
queryCanRedeem(enterpriseCustomer.uuid, courseMetadata, isEnrollableBufferDays),
);
}),
queryClient.ensureQueryData(queryEnterpriseCourseEnrollments(enterpriseId)),
queryClient.ensureQueryData(queryEnterpriseCourseEnrollments(enterpriseCustomer.uuid)),
queryClient.ensureQueryData(queryUserEntitlements()),
queryClient.ensureQueryData(queryEnterpriseCustomerContainsContent(enterpriseId, [courseKey])),
queryClient.ensureQueryData(queryEnterpriseCustomerContainsContent(enterpriseCustomer.uuid, [courseKey])),
queryClient.ensureQueryData(queryCourseReviews(courseKey)),
subsidyQueries.then(async (subsidyResponses) => {
const redeemableLearnerCreditPolicies = subsidyResponses[0];
Expand Down Expand Up @@ -124,7 +125,7 @@ export default function makeCourseLoader(queryClient) {
subscriptionLicense,
});
return queryClient.ensureQueryData(queryCourseRecommendations(
enterpriseId,
enterpriseCustomer.uuid,
courseKey,
searchCatalogs,
));
Expand Down
6 changes: 3 additions & 3 deletions src/components/course/data/courseLoader.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import '@testing-library/jest-dom/extend-expect';
import { renderWithRouterProvider } from '../../../utils/tests';
import makeCourseLoader from './courseLoader';
import {
extractEnterpriseId,
extractEnterpriseCustomer,
queryBrowseAndRequestConfiguration,
queryCanRedeem,
queryCouponCodeRequests,
Expand All @@ -31,7 +31,7 @@ jest.mock('../../app/routes/data', () => ({
}));
jest.mock('../../app/data', () => ({
...jest.requireActual('../../app/data'),
extractEnterpriseId: jest.fn(),
extractEnterpriseCustomer: jest.fn(),
}));
jest.mock('@edx/frontend-platform/auth', () => ({
...jest.requireActual('@edx/frontend-platform/auth'),
Expand All @@ -46,7 +46,7 @@ jest.mock('@edx/frontend-platform/logging', () => ({
const mockCourseKey = 'edX+DemoX';
const mockSubscriptionCatalog = 'test-subscription-catalog-uuid';
const mockEnterpriseCustomer = enterpriseCustomerFactory();
extractEnterpriseId.mockResolvedValue(mockEnterpriseCustomer.uuid);
extractEnterpriseCustomer.mockResolvedValue(mockEnterpriseCustomer);

const mockAuthenticatedUser = authenticatedUserFactory();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { generatePath, redirect } from 'react-router-dom';

import {
extractEnterpriseId,
extractEnterpriseCustomer,
getLateRedemptionBufferDays,
queryCanRedeem,
queryCourseMetadata,
Expand All @@ -24,7 +24,7 @@ export default function makeExternalCourseEnrollmentLoader(queryClient) {
courseKey,
courseRunKey,
} = params;
const enterpriseId = await extractEnterpriseId({
const enterpriseCustomer = await extractEnterpriseCustomer({
queryClient,
authenticatedUser,
enterpriseSlug,
Expand All @@ -38,12 +38,12 @@ export default function makeExternalCourseEnrollmentLoader(queryClient) {
return;
}
const redeemableLearnerCreditPolicies = await queryClient.ensureQueryData(queryRedeemablePolicies({
enterpriseUuid: enterpriseId,
enterpriseUuid: enterpriseCustomer.uuid,
lmsUserId: authenticatedUser.userId,
}));
const isEnrollableBufferDays = getLateRedemptionBufferDays(redeemableLearnerCreditPolicies.redeemablePolicies);
const canRedeem = await queryClient.ensureQueryData(
queryCanRedeem(enterpriseId, courseMetadata, isEnrollableBufferDays),
queryCanRedeem(enterpriseCustomer.uuid, courseMetadata, isEnrollableBufferDays),
);
const hasSuccessfulRedemption = !!canRedeem.find(r => r.contentKey === courseRunKey)?.hasSuccessfulRedemption;
if (hasSuccessfulRedemption) {
Expand Down
10 changes: 5 additions & 5 deletions src/components/dashboard/data/dashboardLoader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ensureAuthenticatedUser } from '../../app/routes/data';
import {
extractEnterpriseId,
extractEnterpriseCustomer,
queryEnterpriseCourseEnrollments,
queryEnterprisePathwaysList,
queryEnterpriseProgramsList,
Expand All @@ -21,15 +21,15 @@ export default function makeDashboardLoader(queryClient) {
}

const { enterpriseSlug } = params;
const enterpriseId = await extractEnterpriseId({
const enterpriseCustomer = await extractEnterpriseCustomer({
queryClient,
authenticatedUser,
enterpriseSlug,
});
await Promise.all([
queryClient.ensureQueryData(queryEnterpriseCourseEnrollments(enterpriseId)),
queryClient.ensureQueryData(queryEnterpriseProgramsList(enterpriseId)),
queryClient.ensureQueryData(queryEnterprisePathwaysList(enterpriseId)),
queryClient.ensureQueryData(queryEnterpriseCourseEnrollments(enterpriseCustomer.uuid)),
queryClient.ensureQueryData(queryEnterpriseProgramsList(enterpriseCustomer.uuid)),
queryClient.ensureQueryData(queryEnterprisePathwaysList(enterpriseCustomer.uuid)),
]);

return null;
Expand Down
6 changes: 3 additions & 3 deletions src/components/dashboard/data/dashboardLoader.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import '@testing-library/jest-dom/extend-expect';
import { renderWithRouterProvider } from '../../../utils/tests';
import makeDashboardLoader from './dashboardLoader';
import {
extractEnterpriseId,
extractEnterpriseCustomer,
queryEnterpriseCourseEnrollments,
queryEnterprisePathwaysList,
queryEnterpriseProgramsList,
Expand All @@ -17,7 +17,7 @@ jest.mock('../../app/routes/data', () => ({
}));
jest.mock('../../app/data', () => ({
...jest.requireActual('../../app/data'),
extractEnterpriseId: jest.fn(),
extractEnterpriseCustomer: jest.fn(),
}));
jest.mock('@edx/frontend-platform/auth', () => ({
...jest.requireActual('@edx/frontend-platform/auth'),
Expand All @@ -30,7 +30,7 @@ jest.mock('@edx/frontend-platform/logging', () => ({
}));

const mockEnterpriseId = 'test-enterprise-uuid';
extractEnterpriseId.mockResolvedValue(mockEnterpriseId);
extractEnterpriseCustomer.mockResolvedValue({ uuid: mockEnterpriseId });

const mockQueryClient = {
ensureQueryData: jest.fn().mockResolvedValue({}),
Expand Down
6 changes: 3 additions & 3 deletions src/components/program/data/programLoader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ensureAuthenticatedUser } from '../../app/routes/data';
import { extractEnterpriseId, queryEnterpriseProgram } from '../../app/data';
import { extractEnterpriseCustomer, queryEnterpriseProgram } from '../../app/data';

export default function makeProgramLoader(queryClient) {
return async function programLoader({ params = {}, request }) {
Expand All @@ -12,13 +12,13 @@ export default function makeProgramLoader(queryClient) {

const { enterpriseSlug, programUUID } = params;

const enterpriseId = await extractEnterpriseId({
const enterpriseCustomer = await extractEnterpriseCustomer({
queryClient,
authenticatedUser,
enterpriseSlug,
});

await queryClient.ensureQueryData(queryEnterpriseProgram(enterpriseId, programUUID));
await queryClient.ensureQueryData(queryEnterpriseProgram(enterpriseCustomer.uuid, programUUID));

return null;
};
Expand Down
6 changes: 3 additions & 3 deletions src/components/program/data/programLoader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import '@testing-library/jest-dom/extend-expect';

import { renderWithRouterProvider } from '../../../utils/tests';
import { ensureAuthenticatedUser } from '../../app/routes/data';
import { extractEnterpriseId, queryEnterpriseProgram } from '../../app/data';
import { extractEnterpriseCustomer, queryEnterpriseProgram } from '../../app/data';
import makeProgramLoader from './programLoader';

jest.mock('../../app/routes/data', () => ({
Expand All @@ -14,14 +14,14 @@ jest.mock('../../app/routes/data', () => ({

jest.mock('../../app/data', () => ({
...jest.requireActual('../../app/data'),
extractEnterpriseId: jest.fn(),
extractEnterpriseCustomer: jest.fn(),
}));

const mockEnterpriseId = 'test-enterprise-uuid';
const mockProgramUUID = 'test-program-uuid';
const mockProgramsURL = `/${mockEnterpriseId}/program/${mockProgramUUID}`;

extractEnterpriseId.mockResolvedValue(mockEnterpriseId);
extractEnterpriseCustomer.mockResolvedValue({ uuid: mockEnterpriseId });

const mockQueryClient = {
ensureQueryData: jest.fn().mockResolvedValue({}),
Expand Down
8 changes: 4 additions & 4 deletions src/components/search/data/searchLoader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getConfig } from '@edx/frontend-platform/config';
import { ensureAuthenticatedUser } from '../../app/routes/data';
import { extractEnterpriseId, queryAcademiesList, queryContentHighlightSets } from '../../app/data';
import { extractEnterpriseCustomer, queryAcademiesList, queryContentHighlightSets } from '../../app/data';

export default function makeSearchLoader(queryClient) {
return async function searchLoader({ params = {}, request }) {
Expand All @@ -13,20 +13,20 @@ export default function makeSearchLoader(queryClient) {

const { enterpriseSlug } = params;

const enterpriseId = await extractEnterpriseId({
const enterpriseCustomer = await extractEnterpriseCustomer({
queryClient,
authenticatedUser,
enterpriseSlug,
});
const searchData = [
queryClient.ensureQueryData(
queryAcademiesList(enterpriseId),
queryAcademiesList(enterpriseCustomer.uuid),
),
];
if (getConfig().FEATURE_CONTENT_HIGHLIGHTS) {
searchData.push(
queryClient.ensureQueryData(
queryContentHighlightSets(enterpriseId),
queryContentHighlightSets(enterpriseCustomer.uuid),
),
);
}
Expand Down

0 comments on commit 6f4b6d1

Please sign in to comment.