From 71d968500061dd0ac065b9daf2d2c72ebda6a13a Mon Sep 17 00:00:00 2001 From: Adam Stankiewicz Date: Tue, 10 Dec 2024 11:58:55 -0500 Subject: [PATCH] refactor: move bffs service file to TS --- .../services/{bffs.test.js => bffs.test.ts} | 10 ++++---- .../app/data/services/{bffs.js => bffs.ts} | 25 ++++++++++++++++--- src/types.d.ts | 12 +++++++-- 3 files changed, 37 insertions(+), 10 deletions(-) rename src/components/app/data/services/{bffs.test.js => bffs.test.ts} (97%) rename src/components/app/data/services/{bffs.js => bffs.ts} (84%) diff --git a/src/components/app/data/services/bffs.test.js b/src/components/app/data/services/bffs.test.ts similarity index 97% rename from src/components/app/data/services/bffs.test.js rename to src/components/app/data/services/bffs.test.ts index 450138c85..6ca3fb1c0 100644 --- a/src/components/app/data/services/bffs.test.js +++ b/src/components/app/data/services/bffs.test.ts @@ -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(); @@ -145,10 +145,10 @@ describe('fetchEnterpriseLearnerDashboard', () => { it.each([ { enterpriseId: mockEnterpriseCustomer.uuid, - enterpriseSlug: null, + enterpriseSlug: undefined, }, { - enterpriseId: null, + enterpriseId: undefined, enterpriseSlug: mockEnterpriseCustomer.slug, }, { @@ -167,10 +167,10 @@ describe('fetchEnterpriseLearnerDashboard', () => { it.each([ { enterpriseId: mockEnterpriseCustomer.uuid, - enterpriseSlug: null, + enterpriseSlug: undefined, }, { - enterpriseId: null, + enterpriseId: undefined, enterpriseSlug: mockEnterpriseCustomer.slug, }, { diff --git a/src/components/app/data/services/bffs.js b/src/components/app/data/services/bffs.ts similarity index 84% rename from src/components/app/data/services/bffs.js rename to src/components/app/data/services/bffs.ts index 14a4fcbae..cc1bac8d3 100644 --- a/src/components/app/data/services/bffs.js +++ b/src/components/app/data/services/bffs.ts @@ -45,7 +45,11 @@ export function logErrorsAndWarningsFromBFFResponse({ url, response }) { * @param {String} [args.options.enterpriseSlug] - The slug of the enterprise customer. * @returns {Promise} - 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) : {}; @@ -76,6 +80,11 @@ 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 @@ -83,10 +92,20 @@ export async function makeBFFRequest({ url, defaultResponse, options = {} }) { * @param {String} [args.enterpriseSlug] - The slug of the enterprise customer. * @returns {Promise} - 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, }); } diff --git a/src/types.d.ts b/src/types.d.ts index 094568314..4a4a0d56f 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -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; @@ -43,7 +51,7 @@ export interface EnterpriseLearnerData { staffEnterpriseCustomer: Types.EnterpriseCustomer; } -interface DueDate { +interface EnrollmentDueDate { name: string; date: string; url: string; @@ -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)