diff --git a/src/components/EnterpriseSubsidiesContext/data/hooks.js b/src/components/EnterpriseSubsidiesContext/data/hooks.js index 9b2d2e0579..123d022df1 100644 --- a/src/components/EnterpriseSubsidiesContext/data/hooks.js +++ b/src/components/EnterpriseSubsidiesContext/data/hooks.js @@ -38,25 +38,29 @@ export const useEnterpriseOffers = ({ enablePortalLearnerCreditManagementScreen, results = camelCaseObject(ecommerceApiResponse.data.results); source = 'ecommerceApi'; } - + let activeSubsidyFound = false; if (results.length !== 0) { - const subsidy = results[0]; - const isCurrent = source === 'ecommerceApi' - ? subsidy.isCurrent - : dayjs().isSameOrBefore(subsidy.expirationDatetime) - && dayjs().isSameOrAfter(subsidy.activeDatetime); - const offerData = { - id: subsidy.uuid || subsidy.id, - name: subsidy.title || subsidy.displayName, - start: subsidy.activeDatetime || subsidy.startDatetime, - end: subsidy.expirationDatetime || subsidy.endDatetime, - isCurrent, - }; - setOffers([offerData]); - } - // We only released learner credit management to customers with 1 offer for the MVP. - if (results.length === 1) { - setCanManageLearnerCredit(true); + let subsidy = results[0]; + for (let i = 0; i < results.length; i++) { + subsidy = results[i]; + activeSubsidyFound = source === 'ecommerceApi' + ? subsidy.isCurrent + : subsidy.isActive; + if (activeSubsidyFound === true) { + break; + } + } + if (activeSubsidyFound === true) { + const offerData = { + id: subsidy.uuid || subsidy.id, + name: subsidy.title || subsidy.displayName, + start: subsidy.activeDatetime || subsidy.startDatetime, + end: subsidy.expirationDatetime || subsidy.endDatetime, + isCurrent: activeSubsidyFound, + }; + setOffers([offerData]); + setCanManageLearnerCredit(true); + } } } catch (error) { logError(error); diff --git a/src/components/EnterpriseSubsidiesContext/data/tests/hooks.test.js b/src/components/EnterpriseSubsidiesContext/data/tests/hooks.test.js index 668fa9d386..adf8580b52 100644 --- a/src/components/EnterpriseSubsidiesContext/data/tests/hooks.test.js +++ b/src/components/EnterpriseSubsidiesContext/data/tests/hooks.test.js @@ -94,6 +94,7 @@ describe('useEnterpriseOffers', () => { title: 'offer-name', active_datetime: '2021-05-15T19:56:09Z', expiration_datetime: '2100-05-15T19:56:09Z', + is_active: true, }]; SubsidyApiService.getSubsidyByCustomerUUID.mockResolvedValueOnce({ data: { @@ -119,25 +120,79 @@ describe('useEnterpriseOffers', () => { }); }); - it('should set canManageLearnerCredit to false if enterprise offer or subsidy does not have exactly 1 offer', async () => { + it('should set canManageLearnerCredit to false if active enterprise offer or subsidy not found', async () => { const mockOffers = [{ subsidyUuid: 'offer-1' }, { subsidyUuid: 'offer-2' }]; const mockSubsidyServiceResponse = [ { uuid: 'offer-1', title: 'offer-name', - active_datetime: '2021-05-15T19:56:09Z', - expiration_datetime: '2100-05-15T19:56:09Z', + active_datetime: '2005-05-15T19:56:09Z', + expiration_datetime: '2006-05-15T19:56:09Z', + is_active: false, }, { uuid: 'offer-2', + title: 'offer-name-2', + active_datetime: '2006-05-15T19:56:09Z', + expiration_datetime: '2007-05-15T19:56:09Z', + is_active: false, + }, + ]; + const mockOfferData = []; + + EcommerceApiService.fetchEnterpriseOffers.mockResolvedValueOnce({ + data: { + results: mockOffers, + }, + }); + SubsidyApiService.getSubsidyByCustomerUUID.mockResolvedValueOnce({ + data: { + results: mockSubsidyServiceResponse, + }, + }); + + const { result, waitForNextUpdate } = renderHook(() => useEnterpriseOffers({ + enablePortalLearnerCreditManagementScreen: true, + enterpriseId: TEST_ENTERPRISE_UUID, + })); + + await waitForNextUpdate(); + + expect(SubsidyApiService.getSubsidyByCustomerUUID).toHaveBeenCalledWith( + TEST_ENTERPRISE_UUID, + { subsidyType: 'learner_credit' }, + ); + expect(result.current).toEqual({ + offers: mockOfferData, + isLoading: false, + canManageLearnerCredit: false, + }); + }); + + it('should return the active enterprise offer or subsidy when multiple available', async () => { + const mockOffers = [{ subsidyUuid: 'offer-1' }, { subsidyUuid: 'offer-2' }]; + const mockSubsidyServiceResponse = [ + { + uuid: 'offer-1', + title: 'offer-name', + active_datetime: '2005-05-15T19:56:09Z', + expiration_datetime: '2006-05-15T19:56:09Z', + is_active: false, + }, + { + uuid: 'offer-2', + title: 'offer-name-2', + active_datetime: '2006-05-15T19:56:09Z', + expiration_datetime: '2099-05-15T19:56:09Z', + is_active: true, }, ]; const mockOfferData = [ { - id: 'offer-1', - name: 'offer-name', - start: '2021-05-15T19:56:09Z', - end: '2100-05-15T19:56:09Z', + id: 'offer-2', + name: 'offer-name-2', + start: '2006-05-15T19:56:09Z', + end: '2099-05-15T19:56:09Z', isCurrent: true, }, ]; @@ -167,7 +222,7 @@ describe('useEnterpriseOffers', () => { expect(result.current).toEqual({ offers: mockOfferData, isLoading: false, - canManageLearnerCredit: false, + canManageLearnerCredit: true, }); }); });