Skip to content

Commit

Permalink
fix: resource models fixes (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
annarhughes authored Dec 9, 2024
1 parent f075c87 commit 7872fcf
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 17 deletions.
66 changes: 66 additions & 0 deletions src/resource-feedback/resource-feedback.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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 { ResourceService } from 'src/resource/resource.service';
import { FEEDBACK_TAGS_ENUM } from 'src/utils/constants';
import { mockResource } from 'test/utils/mockData';
import { Repository } from 'typeorm';
import { ResourceFeedbackEntity } from '../entities/resource-feedback.entity';
import { ResourceFeedbackService } from './resource-feedback.service';

describe('ResourceFeedbackService', () => {
let service: ResourceFeedbackService;
let mockResourceFeedbackRepository: DeepMocked<Repository<ResourceFeedbackEntity>>;
let mockResourceService: DeepMocked<ResourceService>;

const resourceFeedbackDto = {
resourceId: mockResource.id,
feedbackTags: FEEDBACK_TAGS_ENUM.RELATABLE,
feedbackDescription: 'feedback comments',
} as ResourceFeedbackEntity;

beforeEach(async () => {
mockResourceFeedbackRepository = createMock<Repository<ResourceFeedbackEntity>>();
mockResourceService = createMock<ResourceService>();

const module: TestingModule = await Test.createTestingModule({
providers: [
ResourceFeedbackService,
{
provide: getRepositoryToken(ResourceFeedbackEntity),
useValue: mockResourceFeedbackRepository,
},
{
provide: ResourceService,
useValue: mockResourceService,
},
],
}).compile();

service = module.get<ResourceFeedbackService>(ResourceFeedbackService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('create', () => {
it('should create a resource feedback when resource exists', async () => {
jest.spyOn(mockResourceService, 'findOne').mockResolvedValueOnce(mockResource);
jest.spyOn(mockResourceFeedbackRepository, 'save').mockResolvedValueOnce(resourceFeedbackDto);

const result = await service.create(resourceFeedbackDto);
expect(result).toEqual(resourceFeedbackDto);
expect(mockResourceFeedbackRepository.save).toHaveBeenCalledWith(resourceFeedbackDto);
});

it('should throw an HttpException when resource does not exist', async () => {
jest.spyOn(mockResourceService, 'findOne').mockResolvedValueOnce(null);

await expect(service.create(resourceFeedbackDto)).rejects.toThrow(
new HttpException('RESOURCE NOT FOUND', HttpStatus.NOT_FOUND),
);
});
});
});
17 changes: 11 additions & 6 deletions src/webhooks/webhooks.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
mockCourseStoryblokResult,
mockPartnerAccessEntity,
mockResource,
mockResource2,
mockResourceStoryblokResult,
mockSession,
mockSessionStoryblokResult,
Expand All @@ -43,6 +44,7 @@ import {
mockPartnerAccessRepositoryMethods,
mockPartnerAdminRepositoryMethods,
mockPartnerRepositoryMethods,
mockResourceRepositoryMethods,
mockSessionRepositoryMethods,
mockSlackMessageClientMethods,
mockTherapySessionRepositoryMethods,
Expand Down Expand Up @@ -85,7 +87,9 @@ describe('WebhooksService', () => {
const mockedCoursePartnerService = createMock<CoursePartnerService>(
mockCoursePartnerServiceMethods,
);
const mockedResourceRepository = createMock<Repository<ResourceEntity>>();
const mockedResourceRepository = createMock<Repository<ResourceEntity>>(
mockResourceRepositoryMethods,
);
const mockedUserRepository = createMock<Repository<UserEntity>>(mockUserRepositoryMethods);
const mockedTherapySessionRepository = createMock<Repository<TherapySessionEntity>>(
mockTherapySessionRepositoryMethods,
Expand Down Expand Up @@ -480,7 +484,7 @@ describe('WebhooksService', () => {
expect(deletedResource.status).toBe(STORYBLOK_STORY_STATUS_ENUM.DELETED);
});

it.skip('should handle a new resource', async () => {
it('should handle a new resource', async () => {
const resourceSaveRepoSpy = jest.spyOn(mockedResourceRepository, 'save');
const resourceFindOneRepoSpy = jest
.spyOn(mockedResourceRepository, 'findOneBy')
Expand Down Expand Up @@ -516,18 +520,18 @@ describe('WebhooksService', () => {
expect(resource).toEqual(expectedResponse);
expect(resourceSaveRepoSpy).toHaveBeenCalledWith(expectedResponse);
expect(resourceFindOneRepoSpy).toHaveBeenCalledWith({
storyblokUuid: mockResourceStoryblokResult.data.story.id,
storyblokUuid: mockResourceStoryblokResult.data.story.uuid,
});

resourceSaveRepoSpy.mockClear();
resourceFindOneRepoSpy.mockClear();
});

it.skip('should handle updating an existing resource', async () => {
it('should handle updating an existing resource', async () => {
const resourceSaveRepoSpy = jest.spyOn(mockedResourceRepository, 'save');
const resourceFindOneRepoSpy = jest
.spyOn(mockedResourceRepository, 'findOneBy')
.mockImplementationOnce(async () => mockResource);
.mockImplementationOnce(async () => mockResource2);

const updatedMockResourceStoryblokResult = { ...mockResourceStoryblokResult };
const newName = 'New resource name';
Expand All @@ -552,6 +556,7 @@ describe('WebhooksService', () => {
};

const expectedResponse = {
...mockResource2,
storyblokId: mockResourceStoryblokResult.data.story.id,
storyblokUuid: mockResourceStoryblokResult.data.story.uuid,
status: STORYBLOK_STORY_STATUS_ENUM.PUBLISHED,
Expand All @@ -565,7 +570,7 @@ describe('WebhooksService', () => {
expect(updatedResource).toEqual(expectedResponse);
expect(resourceSaveRepoSpy).toHaveBeenCalled();
expect(resourceFindOneRepoSpy).toHaveBeenCalledWith({
storyblokUuid: mockResourceStoryblokResult.data.story.id,
storyblokUuid: mockResourceStoryblokResult.data.story.uuid,
});

resourceSaveRepoSpy.mockClear();
Expand Down
23 changes: 14 additions & 9 deletions src/webhooks/webhooks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ export class WebhooksService {
}
}

private async updateOrCreateStory(storyData: ISbStoryData, status: STORYBLOK_STORY_STATUS_ENUM) {
private async updateOrCreateStoryData(
storyData: ISbStoryData,
status: STORYBLOK_STORY_STATUS_ENUM,
) {
const storyPageComponent = storyData.content.component as STORYBLOK_PAGE_COMPONENTS;

const updatedStoryData = {
Expand Down Expand Up @@ -349,9 +352,7 @@ export class WebhooksService {
}
}

async updateDeletedStory(story_id: number) {
const status = STORYBLOK_STORY_STATUS_ENUM.DELETED;

async updateInactiveStoryStatus(story_id: number, status: STORYBLOK_STORY_STATUS_ENUM) {
// Story is deleted so cant be fetched from storyblok to get story type
// Try to find course with matching story_id first
let course = await this.courseRepository.findOneBy({
Expand Down Expand Up @@ -394,12 +395,16 @@ export class WebhooksService {

this.logger.log(`Storyblok story ${status} request - ${story_id}`);

if (status === STORYBLOK_STORY_STATUS_ENUM.DELETED) {
return this.updateDeletedStory(story_id);
if (
status === STORYBLOK_STORY_STATUS_ENUM.UNPUBLISHED ||
status === STORYBLOK_STORY_STATUS_ENUM.DELETED
) {
// Story can't be retrieved from storyblok so we just update the status of existing records
return this.updateInactiveStoryStatus(story_id, status);
}

// Story was either published, unpublished, or moved
// First get story data from storyblok
// Story was either published or moved
// Retrieve the story data from storyblok before handling the update/create
let story: ISbStoryData;

const Storyblok = new StoryblokClient({
Expand All @@ -422,6 +427,6 @@ export class WebhooksService {
}

// Create or update the resource/course/session record in our database
return this.updateOrCreateStory(story, status);
return this.updateOrCreateStoryData(story, status);
}
}
18 changes: 16 additions & 2 deletions test/utils/mockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const mockResourceStoryblokResult = {
created_at: '2022-05-05T11:29:10.888Z',
published_at: '2022-05-19T16:32:44.502Z',
id: 98765,
uuid: 'resourceUuid',
uuid: 'resourceUuid2',
content: {
_uid: '23456',
name: 'Resource name 2',
Expand Down Expand Up @@ -138,7 +138,7 @@ export const mockResource: ResourceEntity = {
resourceUser: [],
id: 'resourceId1',
storyblokId: 123456,
storyblokUuid: 'resourceStoryblokUuid1',
storyblokUuid: 'resourceUuid1',
slug: 'resources/shorts/resource-name',
status: STORYBLOK_STORY_STATUS_ENUM.PUBLISHED,
name: 'Resource name',
Expand All @@ -148,6 +148,20 @@ export const mockResource: ResourceEntity = {
resourceFeedback: [],
};

export const mockResource2: ResourceEntity = {
id: 'resourceId2',
storyblokId: 98765,
storyblokUuid: 'resourceUuid2',
slug: 'resources/shorts/resource-name',
status: STORYBLOK_STORY_STATUS_ENUM.PUBLISHED,
name: 'Resource name 2',
category: RESOURCE_CATEGORIES.SHORT_VIDEO,
createdAt: new Date(100),
updatedAt: new Date(100),
resourceUser: [],
resourceFeedback: [],
};

export const mockIFirebaseUser: IFirebaseUser = {
iss: '',
aud: '',
Expand Down

0 comments on commit 7872fcf

Please sign in to comment.