Skip to content

Commit

Permalink
Better error handling added to services, new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eleanorreem committed Sep 14, 2023
1 parent f3abaab commit 92f6ad8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
41 changes: 30 additions & 11 deletions src/partner-access/partner-access.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,24 +248,43 @@ describe('PartnerAccessService', () => {
});
describe('getUserTherapySessions', () => {
it('should return user emails with their total therapy sessions available and an associated access code id', async () => {
const repoSpyCreateQueryBuilder = jest.spyOn(repo, 'createQueryBuilder');
// Mocks the raw results
const mockResults = [
{
useremail: '[email protected]',
partneraccesscode: 'ABCDEF',
therapytotal: '2',
},
{
useremail: '[email protected]',
partneraccesscode: 'GHIJKL',
therapytotal: '5',
},
];
repoSpyCreateQueryBuilder.mockImplementation(
createQueryBuilderMock({ getRawMany: jest.fn().mockResolvedValue(mockResults) }) as never,
);
const userTherapySessions = await service.getUserTherapySessions();
console.log('user therapy sessionms');
console.log(userTherapySessions);
// expect(userTherapySessions.length).toBeGreaterThan(0);
expect(userTherapySessions.length).toBeGreaterThan(0);
});
});

describe('updatePartnerAccessTherapyCount', () => {
it('should update the number of therapy sessions remaining on an access code', async () => {
const partnerAccessCode = '123435';
const therapySessions = 0;
const test = await service.updatePartnerAccessTherapyCount(
partnerAccessCode,
therapySessions,
const repoSpyCreateQueryBuilder = jest.spyOn(repo, 'findOne');

Check failure on line 275 in src/partner-access/partner-access.service.spec.ts

View workflow job for this annotation

GitHub Actions / build

'repoSpyCreateQueryBuilder' is assigned a value but never used

// Mocks finding an access code record
const findOneResults = { accessCode: 'ABCSDE', id: '122334' };

const sessionFindOneRepoSpy = jest.spyOn(repo, 'createQueryBuilder');
sessionFindOneRepoSpy.mockImplementation(
createQueryBuilderMock({ findOne: jest.fn().mockResolvedValue(findOneResults) }) as never,
);
console.log(' update user therapy sessionms');
console.log(test);
// expect(userTherapySessions.length).toBeGreaterThan(0);

const result = await service.updatePartnerAccessTherapyCount('ABCSDE', 3);
//if an access code exists then update it.
expect(result).toBe('Success');
});
});
});
16 changes: 9 additions & 7 deletions src/partner-access/partner-access.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,20 @@ export class PartnerAccessService {
therapySessions: number
): Promise<string> {
if (!partnerAccessCode) {
this.logger.error(`No access code to update`);
return
//invalid access code
throw new HttpException(PartnerAccessCodeStatusEnum.INVALID_CODE, HttpStatus.BAD_REQUEST);
}
try {
const partnerAccessResponse = await this.partnerAccessRepository.findOne({
accessCode: partnerAccessCode,
featureTherapy: true
where: {
accessCode: partnerAccessCode,
featureTherapy: true
}
});

if (!partnerAccessResponse) {
this.logger.error(`This access does not have therapy enabled`);
throw error;
//invalie access code - cannot find in the db
throw new HttpException(PartnerAccessCodeStatusEnum.INVALID_CODE, HttpStatus.BAD_REQUEST);
}

await this.partnerAccessRepository
Expand All @@ -138,7 +140,7 @@ export class PartnerAccessService {
.set({ therapySessionsRemaining: therapySessions })
.where('accessCode = :partnerAccessCode', { partnerAccessCode })
.execute();
return 'Success'
return 'Success';
}
catch (error) {
throw error;
Expand Down
1 change: 0 additions & 1 deletion test/utils/mockUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ export const createQueryBuilderMock = (modifications?: Record<string, jest.Mock<
set: jest.fn().mockReturnThis(),
returning: jest.fn().mockReturnThis(),
getManyAndCount: jest.fn().mockReturnValueOnce({}),
getRawMany: jest.fn().mockResolvedValue([{one:'test'}]),
...modifications,
}));

0 comments on commit 92f6ad8

Please sign in to comment.