Skip to content

Commit

Permalink
Merge branch 'master' into iahmad/ENT-7309
Browse files Browse the repository at this point in the history
  • Loading branch information
zamanafzal authored Aug 2, 2023
2 parents 5665f30 + 21489d0 commit 2b3b824
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 26 deletions.
40 changes: 22 additions & 18 deletions src/components/EnterpriseSubsidiesContext/data/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
71 changes: 63 additions & 8 deletions src/components/EnterpriseSubsidiesContext/data/tests/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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,
},
];
Expand Down Expand Up @@ -167,7 +222,7 @@ describe('useEnterpriseOffers', () => {
expect(result.current).toEqual({
offers: mockOfferData,
isLoading: false,
canManageLearnerCredit: false,
canManageLearnerCredit: true,
});
});
});
Expand Down

0 comments on commit 2b3b824

Please sign in to comment.