Skip to content

Commit

Permalink
refactor: move bffs service file to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstankiewicz committed Dec 10, 2024
1 parent fb57c12 commit 71d9685
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jest.mock('@edx/frontend-platform/logging', () => ({
logInfo: jest.fn(),
}));

const mockEnterpriseCustomer = enterpriseCustomerFactory();
const mockEnterpriseCustomer = enterpriseCustomerFactory() as Types.EnterpriseCustomer;
const mockCustomerAgreementUuid = uuidv4();
const mockSubscriptionCatalogUuid = uuidv4();
const mockSubscriptionLicenseUuid = uuidv4();
Expand Down Expand Up @@ -145,10 +145,10 @@ describe('fetchEnterpriseLearnerDashboard', () => {
it.each([
{
enterpriseId: mockEnterpriseCustomer.uuid,
enterpriseSlug: null,
enterpriseSlug: undefined,
},
{
enterpriseId: null,
enterpriseId: undefined,
enterpriseSlug: mockEnterpriseCustomer.slug,
},
{
Expand All @@ -167,10 +167,10 @@ describe('fetchEnterpriseLearnerDashboard', () => {
it.each([
{
enterpriseId: mockEnterpriseCustomer.uuid,
enterpriseSlug: null,
enterpriseSlug: undefined,
},
{
enterpriseId: null,
enterpriseId: undefined,
enterpriseSlug: mockEnterpriseCustomer.slug,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export function logErrorsAndWarningsFromBFFResponse({ url, response }) {
* @param {String} [args.options.enterpriseSlug] - The slug of the enterprise customer.
* @returns {Promise<Object>} - The response from the BFF.
*/
export async function makeBFFRequest({ url, defaultResponse, options = {} }) {
export async function makeBFFRequest({
url,
defaultResponse,
options = {} as Types.BFFRequestOptions,
}) {
const { enterpriseId, enterpriseSlug, ...optionsRest } = options;
const snakeCaseOptionsRest = optionsRest ? snakeCaseObject(optionsRest) : {};

Expand Down Expand Up @@ -76,17 +80,32 @@ export async function makeBFFRequest({ url, defaultResponse, options = {} }) {
}
}

export interface EnterpriseLearnerDashboardOptions {
enterpriseId?: string;
enterpriseSlug?: string;
}

/**
* Fetch the learner dashboard BFF API for the specified enterprise customer.
* @param {Object} args
* @param {String} [args.enterpriseId] - The UUID of the enterprise customer.
* @param {String} [args.enterpriseSlug] - The slug of the enterprise customer.
* @returns {Promise<Object>} - The learner dashboard metadata.
*/
export async function fetchEnterpriseLearnerDashboard({ enterpriseId, enterpriseSlug }) {
export async function fetchEnterpriseLearnerDashboard({
enterpriseId,
enterpriseSlug,
}: EnterpriseLearnerDashboardOptions) {
const options = {} as Types.BFFRequestOptions;
if (enterpriseId) {
options.enterpriseId = enterpriseId;
}
if (enterpriseSlug) {
options.enterpriseSlug = enterpriseSlug;
}
return makeBFFRequest({
url: `${getConfig().ENTERPRISE_ACCESS_BASE_URL}/api/v1/bffs/learner/dashboard/`,
defaultResponse: learnerDashboardBFFResponse,
options: { enterpriseId, enterpriseSlug },
options,
});
}
12 changes: 10 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ export interface AuthenticatedUser {
administrator: boolean;
}

export interface BFFRequestAdditionalOptions {
[key: string]: any; // Allow any additional properties
}

export type BFFRequestOptions =
| ({ enterpriseId: string; enterpriseSlug?: string; } & BFFRequestAdditionalOptions)
| ({ enterpriseId?: string; enterpriseSlug: string; } & BFFRequestAdditionalOptions);

export interface EnterpriseCustomer {
uuid: string;
slug: string;
Expand All @@ -43,7 +51,7 @@ export interface EnterpriseLearnerData {
staffEnterpriseCustomer: Types.EnterpriseCustomer;
}

interface DueDate {
interface EnrollmentDueDate {
name: string;
date: string;
url: string;
Expand All @@ -69,7 +77,7 @@ export interface EnterpriseCourseEnrollment {
course_run_url: string;
resume_course_run_url?: string;
is_revoked: boolean;
due_dates: DueDate[];
due_dates: EnrollmentDueDate[];
}

// Application Data (subsidy)
Expand Down

0 comments on commit 71d9685

Please sign in to comment.