Skip to content

Commit

Permalink
feat: migrate enterprise customer business logic to the BFF layer
Browse files Browse the repository at this point in the history
  • Loading branch information
brobro10000 committed Jan 29, 2025
1 parent 03088e0 commit 77341d7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
7 changes: 3 additions & 4 deletions src/components/app/data/queries/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { matchPath } from 'react-router-dom';
import { queryEnterpriseLearnerDashboardBFF } from './queries';
import { isBFFEnabled } from '../utils';

/**
* Resolves the appropriate BFF query function to use for the current route.
Expand All @@ -12,9 +11,9 @@ export function resolveBFFQuery(pathname, options = {}) {
const { enterpriseCustomerUuid, enterpriseFeatures } = options;

Check failure on line 11 in src/components/app/data/queries/utils.js

View workflow job for this annotation

GitHub Actions / tests

'enterpriseCustomerUuid' is assigned a value but never used

Check failure on line 11 in src/components/app/data/queries/utils.js

View workflow job for this annotation

GitHub Actions / tests

'enterpriseFeatures' is assigned a value but never used

// Exit early if BFF is not enabled for the enterprise customer and/or request user
if (!isBFFEnabled(enterpriseCustomerUuid, enterpriseFeatures)) {
return null;
}
// if (!isBFFEnabled(enterpriseCustomerUuid, enterpriseFeatures)) {
// return null;
// }

// Define route patterns and their corresponding query functions
const routeToBFFQueryMap = [
Expand Down
6 changes: 5 additions & 1 deletion src/components/app/data/services/enterpriseCustomerUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export async function fetchEnterpriseLearnerData(username, enterpriseSlug, optio
response: enterpriseCustomerUsersResponse,
} = await fetchPaginatedData(url);
const { enterpriseFeatures } = enterpriseCustomerUsersResponse;

// Transform enterprise customer user results
const transformedEnterpriseCustomersUsers = enterpriseCustomersUsers.map(
enterpriseCustomerUser => ({
Expand Down Expand Up @@ -113,6 +112,9 @@ export async function fetchEnterpriseLearnerData(username, enterpriseSlug, optio
foundEnterpriseCustomerUserForCurrentSlug,
staffEnterpriseCustomer,
});

// shouldUpdateActiveEnterpriseCustomer should always be null since its generated primarily from the BFF
// layer to act as a flag on whether to update the active enterprise customer
return {
enterpriseCustomer,
enterpriseCustomerUserRoleAssignments: roleAssignments,
Expand All @@ -121,6 +123,7 @@ export async function fetchEnterpriseLearnerData(username, enterpriseSlug, optio
allLinkedEnterpriseCustomerUsers: transformedEnterpriseCustomersUsers,
enterpriseFeatures,
staffEnterpriseCustomer,
shouldUpdateActiveEnterpriseCustomer: null,
};
} catch (error) {
logError(error);
Expand All @@ -132,6 +135,7 @@ export async function fetchEnterpriseLearnerData(username, enterpriseSlug, optio
allLinkedEnterpriseCustomerUsers: [],
enterpriseFeatures: {},
staffEnterpriseCustomer: null,
shouldUpdateActiveEnterpriseCustomer: null,
};
}
}
Expand Down
1 change: 1 addition & 0 deletions src/components/app/data/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,7 @@ export function isBFFEnabled(enterpriseCustomerUuid, enterpriseFeatures) {
// Check the following conditions:
// 1. BFF is enabled for the enterprise customer.
// 2. BFF is enabled for the request user via Waffle flag (supporting percentage-based rollout)
return true;
const isBFFEnabledForCustomer = isBFFEnabledForEnterpriseCustomer(enterpriseCustomerUuid);
if (isBFFEnabledForCustomer || enterpriseFeatures?.enterpriseLearnerBffEnabled) {
return true;
Expand Down
46 changes: 34 additions & 12 deletions src/components/app/routes/loaders/rootLoader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { queryEnterpriseLearner } from '../../data';
import { queryEnterpriseLearner, resolveBFFQuery, updateUserActiveEnterprise } from '../../data';
import {
ensureActiveEnterpriseCustomerUser,
ensureAuthenticatedUser,
Expand All @@ -23,33 +23,55 @@ const makeRootLoader: Types.MakeRouteLoaderFunctionWithQueryClient = function ma

// Retrieve linked enterprise customers for the current user from query cache
// or fetch from the server if not available.
const enterpriseLearnerData = await queryClient.ensureQueryData<Types.EnterpriseLearnerData>(
queryEnterpriseLearner(username, enterpriseSlug),
const matchedBFFQuery = resolveBFFQuery(
requestUrl.pathname,
);
let enterpriseLearnerData;
if (matchedBFFQuery) {
enterpriseLearnerData = await queryClient.ensureQueryData(
matchedBFFQuery({ enterpriseSlug }),
);
} else {
enterpriseLearnerData = await queryClient.ensureQueryData<Types.EnterpriseLearnerData>(
// @ts-ignore
queryEnterpriseLearner(username, enterpriseSlug),
);
}

let {
enterpriseCustomer,
activeEnterpriseCustomer,
allLinkedEnterpriseCustomerUsers,
} = enterpriseLearnerData;
const { staffEnterpriseCustomer, enterpriseFeatures } = enterpriseLearnerData;
const { staffEnterpriseCustomer, enterpriseFeatures, shouldUpdateActiveEnterpriseCustomer } = enterpriseLearnerData;

// User has no active, linked enterprise customer and no staff-only customer metadata exists; return early.
if (!enterpriseCustomer) {
return null;
}

let updateActiveEnterpriseCustomerUserResult: {
enterpriseCustomer: any; updatedLinkedEnterpriseCustomerUsers: any;
} | null = null;
// Ensure the active enterprise customer user is updated, when applicable (e.g., the
// current enterprise slug in the URL does not match the active enterprise customer's slug).
const updateActiveEnterpriseCustomerUserResult = await ensureActiveEnterpriseCustomerUser({
enterpriseSlug,
activeEnterpriseCustomer,
staffEnterpriseCustomer,
allLinkedEnterpriseCustomerUsers,
requestUrl,
});
// The logic to ensure active enterprise customer user is done in the BFF, but updating the
// active enterprise customer is still required
if (matchedBFFQuery && shouldUpdateActiveEnterpriseCustomer) {
await updateUserActiveEnterprise({ enterpriseCustomer });
} else {
updateActiveEnterpriseCustomerUserResult = await ensureActiveEnterpriseCustomerUser({
enterpriseSlug,
activeEnterpriseCustomer,
staffEnterpriseCustomer,
allLinkedEnterpriseCustomerUsers,
requestUrl,
});
}

// If the active enterprise customer user was updated, override the previous active
// enterprise customer user data with the new active enterprise customer user data
// for subsequent queries.
// for subsequent queries. This action is already completed in the BFF.
if (updateActiveEnterpriseCustomerUserResult) {
const {
enterpriseCustomer: nextActiveEnterpriseCustomer,
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface EnterpriseLearnerData {
allLinkedEnterpriseCustomerUsers: any[];
staffEnterpriseCustomer: Types.EnterpriseCustomer;
enterpriseFeatures: Types.EnterpriseFeatures;
shouldUpdateActiveEnterpriseCustomer: boolean;
}

interface EnrollmentDueDate {
Expand Down

0 comments on commit 77341d7

Please sign in to comment.