Skip to content

Commit

Permalink
fix: create partner access save (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
annarhughes authored Apr 15, 2024
1 parent 2457feb commit 3b4f8a7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
25 changes: 19 additions & 6 deletions src/partner-access/partner-access.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,31 @@ describe('PartnerAccessService', () => {
it('should be defined', () => {
expect(service).toBeDefined();
});

describe('createPartnerAccess', () => {
it('when supplied with correct data should return access code', async () => {
const partnerAccessDto = { ...mockPartnerAccessEntityBase, ...createPartnerAccessDto };
const expectedPartnerAccess = {
...partnerAccessDto,
partnerAdminId,
partnerId,
accessCode: '123456',
};

const partnerAccessRepositorySpy = jest
.spyOn(mockPartnerAccessRepository, 'save')
.mockImplementationOnce(jest.fn().mockResolvedValue(expectedPartnerAccess)) as never;

const { ...newPartnerAccess } = await service.createPartnerAccess(
partnerAccessDto,
partnerId,
partnerAdminId,
);
expect(newPartnerAccess).toStrictEqual({
...partnerAccessDto,
partnerAdminId,
partnerId,
accessCode: '123456',
});
expect(newPartnerAccess).toStrictEqual(expectedPartnerAccess);
expect(newPartnerAccess.accessCode).toHaveLength(6);
expect(partnerAccessRepositorySpy).toHaveBeenCalled();
});

it('tries again when it creates a code that already exists', async () => {
const repoSpyCreateQueryBuilder = jest.spyOn(repo, 'createQueryBuilder');
// Mocks that the accesscode already exists
Expand All @@ -116,6 +124,7 @@ describe('PartnerAccessService', () => {
repoSpyCreateQueryBuilder.mockRestore();
});
});

describe('assignPartnerAccess', () => {
it('should update crisp profile and assign partner access', async () => {
const repoSpyCreateQueryBuilder = jest.spyOn(repo, 'createQueryBuilder');
Expand Down Expand Up @@ -157,6 +166,7 @@ describe('PartnerAccessService', () => {
});
});
});

describe('assignPartnerAccessOnSignUp', () => {
it('when partnerAccess is supplied, it should create a partner access and assign to user', async () => {
jest.spyOn(repo, 'createQueryBuilder').mockImplementationOnce(
Expand All @@ -177,6 +187,7 @@ describe('PartnerAccessService', () => {
expect(partnerAccess.featureTherapy).toBeTruthy();
});
});

describe('createAndAssignPartnerAccess', () => {
it('when partnerId is supplied, it should create a partner access and assign to user', async () => {
const partnerAccess = await service.createAndAssignPartnerAccess(
Expand All @@ -192,6 +203,7 @@ describe('PartnerAccessService', () => {
expect(partnerAccess.featureTherapy).toBeFalsy();
});
});

describe('getPartnerAccessCodes', () => {
it('when no filter dto is supplied, it should return all partner accesses', async () => {
const partnerAccesses = await service.getPartnerAccessCodes(undefined);
Expand Down Expand Up @@ -244,6 +256,7 @@ 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');
Expand Down
6 changes: 3 additions & 3 deletions src/partner-access/partner-access.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ export class PartnerAccessService {
partnerAdminId: string | null,
): Promise<PartnerAccessEntity> {
const accessCode = await this.generateAccessCode(6);
const partnerAccess = {
const partnerAccess = this.partnerAccessRepository.create({
...createPartnerAccessDto,
partnerAdminId,
partnerId,
accessCode,
};
return await this.partnerAccessRepository.create(partnerAccess);
});
return await this.partnerAccessRepository.save(partnerAccess);
}

private async generateAccessCode(length: number): Promise<string> {
Expand Down

0 comments on commit 3b4f8a7

Please sign in to comment.