Skip to content

Commit

Permalink
Merge pull request #386 from chaynHQ/develop
Browse files Browse the repository at this point in the history
Merge Develop onto Main
  • Loading branch information
annarhughes authored Jan 17, 2024
2 parents 8e54699 + b238f4c commit e52085d
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 309 deletions.
3 changes: 3 additions & 0 deletions src/api/slack/slack-api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable, Logger } from '@nestjs/common';
import { AxiosResponse } from 'axios';
import { isProduction } from 'src/utils/constants';
import apiCall from '../apiCalls';

@Injectable()
Expand All @@ -9,6 +10,8 @@ export class SlackMessageClient {
public async sendMessageToTherapySlackChannel(
text: string,
): Promise<AxiosResponse<any, any> | string> {
if (!isProduction) return; // only send messages in production environment

try {
const response = await apiCall({
url: process.env.SLACK_WEBHOOK_URL,
Expand Down
2 changes: 1 addition & 1 deletion src/partner-access/dtos/zapier-body.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class ZapierSimplybookBodyDto {
@IsString()
@IsOptional()
@ApiProperty({ type: String })
client_id?: string; // This is userId - not to be confused with the simplybook.client_id
user_id?: string;

@IsString()
@IsOptional()
Expand Down
2 changes: 1 addition & 1 deletion src/webhooks/therapy-session.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface ITherapySession {
id?: string;
action?: SIMPLYBOOK_ACTION_ENUM;
client_email?: string;
client_id?: string;
user_id?: string;
client_timezone?: string;
service_name?: string;
service_provider_name?: string;
Expand Down
9 changes: 1 addition & 8 deletions src/webhooks/webhooks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@ export class WebhooksController {
async updatePartnerAccessTherapy(
@Body() simplybookBodyDto: ZapierSimplybookBodyDto,
): Promise<TherapySessionEntity> {
const updatedPartnerAccessTherapy = await this.webhooksService.updatePartnerAccessTherapy(
simplybookBodyDto,
);
this.logger.log(
`Updated partner access therapy: ${updatedPartnerAccessTherapy.clientEmail} - ${updatedPartnerAccessTherapy.bookingCode}`,
);

return updatedPartnerAccessTherapy;
return this.webhooksService.updatePartnerAccessTherapy(simplybookBodyDto);
}

@UseGuards(ZapierAuthGuard)
Expand Down
60 changes: 46 additions & 14 deletions src/webhooks/webhooks.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ describe('WebhooksService', () => {
courseSaveRepoSpy.mockClear();
});
});

describe('updatePartnerAccessTherapy', () => {
it('should update the booking time when action is update and time is different TODO ', async () => {
const newStartTime = '2022-09-12T09:30:00+0000';
Expand All @@ -447,17 +448,17 @@ describe('WebhooksService', () => {
expect(therapyRepoFindOneSpy).toBeCalled();
});

it('should throw when action is on a user that doesnt exist', async () => {
it('should throw an error when action is on a user that doesnt exist', async () => {
const userFindOneRepoSpy = jest
.spyOn(mockedUserRepository, 'findOne')
.mockImplementationOnce(() => undefined);
await expect(service.updatePartnerAccessTherapy(mockSimplybookBodyBase)).rejects.toThrowError(
'Unable to find user',
'UpdatePartnerAccessTherapy - error finding user with userID 115e272a-5fc3-4991-8ea9-12dacad25bae and origin client_email [email protected]',
);
expect(userFindOneRepoSpy).toBeCalled();
});

it('when creating a new therapy session and the client_id/ userId is not provided, it should get userId from previous entry', async () => {
it('when creating a new therapy session and the userId is not provided, it should get userId from previous entry', async () => {
const findTherapySessionSpy = jest
.spyOn(mockedTherapySessionRepository, 'findOne')
.mockImplementationOnce(async () => {
Expand All @@ -470,7 +471,7 @@ describe('WebhooksService', () => {
});
const newTherapySession = await service.updatePartnerAccessTherapy({
...mockSimplybookBodyBase,
client_id: undefined,
user_id: undefined,
action: SIMPLYBOOK_ACTION_ENUM.NEW_BOOKING,
});

Expand All @@ -484,16 +485,17 @@ describe('WebhooksService', () => {
});

expect(findTherapySessionSpy).toBeCalledWith({
clientEmail: mockSimplybookBodyBase.client_email,
where: `"clientEmail" ILIKE '[email protected]' AND "bookingCode" LIKE 'abc'`,
});

expect(findPartnerAccessSpy).toBeCalledWith({
userId: 'userId1',
featureTherapy: true,
active: true,
});
});

it('when creating a new therapy session and the client_id/ userId is not provided and no previousTherapySession exists, it should get userId from the userDatabase', async () => {
it('when creating a new therapy session and the user_id/ userId is not provided and no previousTherapySession exists, it should get userId from the userDatabase', async () => {
const findTherapySessionSpy = jest
.spyOn(mockedTherapySessionRepository, 'findOne')
.mockImplementationOnce(async () => {
Expand All @@ -508,7 +510,7 @@ describe('WebhooksService', () => {
});
const newTherapySession = await service.updatePartnerAccessTherapy({
...mockSimplybookBodyBase,
client_id: undefined,
user_id: undefined,
action: SIMPLYBOOK_ACTION_ENUM.NEW_BOOKING,
});

Expand All @@ -522,18 +524,28 @@ describe('WebhooksService', () => {
});

expect(findTherapySessionSpy).toBeCalledWith({
clientEmail: mockSimplybookBodyBase.client_email,
where: `"clientEmail" ILIKE '[email protected]' AND "bookingCode" LIKE 'abc'`,
});
expect(findUserSpy).toBeCalledWith({
email: mockSimplybookBodyBase.client_email,
id: 'userId1',
});

expect(findPartnerAccessSpy).toBeCalledWith({
userId: 'userId1',
featureTherapy: true,
active: true,
});
});

it('should not error when client email is different case to user record email', async () => {
await expect(
service.updatePartnerAccessTherapy({
...mockSimplybookBodyBase,
client_email: '[email protected]',
}),
).resolves.toHaveProperty('action', SIMPLYBOOK_ACTION_ENUM.UPDATED_BOOKING);
});

it('should set a booking as cancelled when action is cancel', async () => {
await expect(
service.updatePartnerAccessTherapy({
Expand All @@ -542,12 +554,28 @@ describe('WebhooksService', () => {
}),
).resolves.toHaveProperty('action', SIMPLYBOOK_ACTION_ENUM.CANCELLED_BOOKING);
});

it('should add therapyRemaining to original partner access when action is cancel', async () => {
const partnerAccessSaveSpy = jest.spyOn(mockedPartnerAccessRepository, 'save');
await expect(
service.updatePartnerAccessTherapy({
...mockSimplybookBodyBase,
...{ action: SIMPLYBOOK_ACTION_ENUM.CANCELLED_BOOKING },
}),
).resolves.toHaveProperty('action', SIMPLYBOOK_ACTION_ENUM.CANCELLED_BOOKING);
expect(partnerAccessSaveSpy).toBeCalledWith({
...mockPartnerAccessEntity,
therapySessionsRemaining: mockPartnerAccessEntity.therapySessionsRemaining + 1,
therapySessionsRedeemed: mockPartnerAccessEntity.therapySessionsRedeemed - 1,
});
});

it('should set a booking as cancelled when action is cancel and there are no therapy sessions remaining TODO', async () => {
// mock that there is no therapy sessions remaining on partner access
const partnerAccessFindSpy = jest
.spyOn(mockedPartnerAccessRepository, 'find')
.spyOn(mockedPartnerAccessRepository, 'findOne')
.mockImplementationOnce(async () => {
return [{ ...mockPartnerAccessEntity, therapySessionsRemaining: 0 }];
return { ...mockPartnerAccessEntity, therapySessionsRemaining: 0 };
});
await expect(
service.updatePartnerAccessTherapy({
Expand All @@ -567,7 +595,9 @@ describe('WebhooksService', () => {
...mockSimplybookBodyBase,
...{ action: SIMPLYBOOK_ACTION_ENUM.NEW_BOOKING },
}),
).rejects.toThrow('Unable to find partner access');
).rejects.toThrow(
'newPartnerAccessTherapy - no partner therapy access - email [email protected] userId userId1',
);
});
it('should deduct therapyRemaining when user creates a new booking', async () => {
jest.spyOn(mockedPartnerAccessRepository, 'find').mockImplementationOnce(async () => {
Expand Down Expand Up @@ -611,7 +641,9 @@ describe('WebhooksService', () => {
...mockSimplybookBodyBase,
...{ action: SIMPLYBOOK_ACTION_ENUM.NEW_BOOKING },
}),
).rejects.toThrowError('No therapy sessions remaining');
).rejects.toThrowError(
'newPartnerAccessTherapy - user has partner therapy access but has 0 therapy sessions remaining - email [email protected] userId userId1',
);
});
it('if user has 2 partner access codes and booking is tied to second code, user should be able to update booking', async () => {
jest.spyOn(mockedPartnerAccessRepository, 'find').mockImplementationOnce(async () => {
Expand Down Expand Up @@ -844,7 +876,7 @@ describe('WebhooksService', () => {
});

await expect(service.createEventLog(eventDto)).rejects.toThrowError(
`webhooksService.createEventLog error, no user attached to email ${eventDto.email}`,
`createEventLog webhook failed - no user attached to email [email protected]`,
);
});
it('should throw 500 if failed to create user', async () => {
Expand Down
Loading

0 comments on commit e52085d

Please sign in to comment.