Skip to content

Commit

Permalink
Merge pull request #552 from chaynHQ/develop
Browse files Browse the repository at this point in the history
Merge Develop onto Main
  • Loading branch information
eleanorreem authored Jul 23, 2024
2 parents b82ebff + 85c120e commit f7c3d34
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/api/slack/slack-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,23 @@ export class SlackMessageClient {
return err;
}
}

public async sendMessageToBloomUserChannel(text: string): Promise<AxiosResponse | string> {
if (!isProduction) return; // only send messages in production environment

try {
const response = await apiCall({
url: process.env.SLACK_BLOOM_USERS_WEBHOOK_URL,
type: 'post',
data: {
text: text,
},
});
this.logger.log({ event: 'SESSION_FEEDBACK_SLACK_MESSAGE_SENT' });
return response;
} catch (err) {
this.logger.error('Unable to sendMessageToBloomUserSlackChannel', err);
return err;
}
}
}
1 change: 0 additions & 1 deletion src/session-feedback/dtos/session-feedback.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export class SessionFeedbackDto {
})
feedbackTags: FEEDBACK_TAGS_ENUM;

@IsNotEmpty()
@IsString()
@ApiProperty({ type: String })
feedbackDescription: string;
Expand Down
10 changes: 8 additions & 2 deletions src/session-feedback/session-feedback.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { HttpException, HttpStatus } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { SlackMessageClient } from 'src/api/slack/slack-api';
import { PartnerAccessEntity } from 'src/entities/partner-access.entity';
import { PartnerEntity } from 'src/entities/partner.entity';
import { SessionFeedbackEntity } from 'src/entities/session-feedback.entity';
Expand Down Expand Up @@ -34,6 +35,7 @@ describe('SessionFeedbackService', () => {
let mockTherapySessionRepository: DeepMocked<Repository<TherapySessionEntity>>;
let mockSessionFeedbackRepository: DeepMocked<Repository<SessionFeedbackEntity>>;
let mockSessionService: DeepMocked<SessionService>;
let mockSlackMessageClient: DeepMocked<SlackMessageClient>;

beforeEach(async () => {
mockPartnerAccessRepository = createMock<Repository<PartnerAccessEntity>>();
Expand All @@ -45,6 +47,7 @@ describe('SessionFeedbackService', () => {
mockTherapySessionRepository = createMock<Repository<TherapySessionEntity>>();
mockSessionFeedbackRepository = createMock<Repository<SessionFeedbackEntity>>();
mockSessionService = createMock<SessionService>();
mockSlackMessageClient = createMock<SlackMessageClient>();

const module: TestingModule = await Test.createTestingModule({
providers: [
Expand Down Expand Up @@ -85,6 +88,7 @@ describe('SessionFeedbackService', () => {
provide: SessionService,
useValue: mockSessionService,
},
{ provide: SlackMessageClient, useValue: mockSlackMessageClient },
],
}).compile();

Expand All @@ -96,13 +100,15 @@ describe('SessionFeedbackService', () => {
});
describe('createSessionFeedback', () => {
it('when session id exists should return dto', async () => {
jest.spyOn(mockSessionService, 'getSession').mockResolvedValueOnce(mockSessionEntity);
jest
.spyOn(mockSessionService, 'getSessionAndCourse')
.mockResolvedValueOnce(mockSessionEntity);
const response = await service.createSessionFeedback(sessionFeedbackDto);

expect(response).toMatchObject(sessionFeedbackDto);
});
it('when session id does not exist should throw exception', async () => {
jest.spyOn(mockSessionService, 'getSession').mockResolvedValueOnce(null);
jest.spyOn(mockSessionService, 'getSessionAndCourse').mockResolvedValueOnce(null);

await expect(service.createSessionFeedback(sessionFeedbackDto)).rejects.toThrow(
new HttpException('SESSION NOT FOUND', HttpStatus.NOT_FOUND),
Expand Down
11 changes: 10 additions & 1 deletion src/session-feedback/session-feedback.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { SlackMessageClient } from 'src/api/slack/slack-api';
import { PartnerAccessEntity } from 'src/entities/partner-access.entity';
import { PartnerEntity } from 'src/entities/partner.entity';
import { SessionFeedbackEntity } from 'src/entities/session-feedback.entity';
Expand Down Expand Up @@ -32,20 +33,28 @@ export class SessionFeedbackService {
@InjectRepository(SessionFeedbackEntity)
private sessionFeedbackRepository: Repository<SessionFeedbackEntity>,
private readonly sessionService: SessionService,
private slackMessageClient: SlackMessageClient,
) {}

public async createSessionFeedback(
sessionFeedbackDto: SessionFeedbackDto,
): Promise<SessionFeedbackDto> {
const session = await this.sessionService.getSession(sessionFeedbackDto.sessionId);
const session = await this.sessionService.getSessionAndCourse(sessionFeedbackDto.sessionId);

if (!session) {
throw new HttpException('SESSION NOT FOUND', HttpStatus.NOT_FOUND);
}

const sessionFeedbackObject = this.sessionFeedbackRepository.create(sessionFeedbackDto);
await this.sessionFeedbackRepository.save(sessionFeedbackObject);
this.sendSlackSessionFeedback(sessionFeedbackDto, session);

return sessionFeedbackDto;
}
// We don't need to wait for this to finish so async is not needed
sendSlackSessionFeedback(sessionFeedbackDto: SessionFeedbackDto, session: SessionEntity) {
this.slackMessageClient.sendMessageToBloomUserChannel(
`*${session.name}* in *${session.course?.name}* was rated *_${sessionFeedbackDto.feedbackTags}_* ${sessionFeedbackDto.feedbackDescription.length > 0 ? `with the comment: \n> _${sessionFeedbackDto.feedbackDescription}_` : ''}`,
);
}
}
8 changes: 8 additions & 0 deletions src/session/session.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export class SessionService {
return await this.sessionRepository.findOneBy({ id });
}

async getSessionAndCourse(id: string): Promise<SessionEntity> {
return await this.sessionRepository
.createQueryBuilder('session')
.leftJoinAndSelect('session.course', 'course')
.where('session.id = :id', { id })
.getOne();
}

async getSessionByStoryblokId(storyblokId: number): Promise<SessionEntity> {
return await this.sessionRepository
.createQueryBuilder('session')
Expand Down
2 changes: 1 addition & 1 deletion src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class UserController {
// Use only if users have not been added to mailchimp due to e.g. an ongoing bug
@ApiBearerAuth()
@Post('/bulk-mailchimp-upload')
@UseGuards(FirebaseAuthGuard)
@UseGuards(SuperAdminAuthGuard)
async bulkUploadMailchimpProfiles() {
return await this.userService.bulkUploadMailchimpProfiles();
}
Expand Down

0 comments on commit f7c3d34

Please sign in to comment.