From a6b28d2137c7404e28268ec5a7b418a6dc326bab Mon Sep 17 00:00:00 2001 From: Mnigos Date: Tue, 17 Oct 2023 12:53:03 +0200 Subject: [PATCH] feat(modules/profiles): implement `ProfileService` with `create` method --- src/modules/profiles/index.ts | 1 + src/modules/profiles/profiles.module.ts | 9 ++- src/modules/profiles/profiles.service.spec.ts | 64 +++++++++++++++++++ src/modules/profiles/profiles.service.ts | 29 +++++++++ 4 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 src/modules/profiles/profiles.service.spec.ts create mode 100644 src/modules/profiles/profiles.service.ts diff --git a/src/modules/profiles/index.ts b/src/modules/profiles/index.ts index 5e03c159..11500497 100644 --- a/src/modules/profiles/index.ts +++ b/src/modules/profiles/index.ts @@ -1,3 +1,4 @@ export * from './profile.entity' export * from './profiles.repository' export * from './profiles.module' +export * from './profiles.service' diff --git a/src/modules/profiles/profiles.module.ts b/src/modules/profiles/profiles.module.ts index a8234810..c9af49ee 100644 --- a/src/modules/profiles/profiles.module.ts +++ b/src/modules/profiles/profiles.module.ts @@ -3,10 +3,13 @@ import { TypeOrmModule } from '@nestjs/typeorm' import { Profile } from './profile.entity' import { ProfilesRepository } from './profiles.repository' +import { ProfilesService } from './profiles.service' + +import { ImagesModule } from '@modules/images' @Module({ - imports: [TypeOrmModule.forFeature([Profile])], - providers: [ProfilesRepository], - exports: [ProfilesRepository], + imports: [TypeOrmModule.forFeature([Profile]), ImagesModule], + providers: [ProfilesRepository, ProfilesService], + exports: [ProfilesRepository, ProfilesService], }) export class ProfilesModule {} diff --git a/src/modules/profiles/profiles.service.spec.ts b/src/modules/profiles/profiles.service.spec.ts new file mode 100644 index 00000000..efc300c4 --- /dev/null +++ b/src/modules/profiles/profiles.service.spec.ts @@ -0,0 +1,64 @@ +import { beforeEach, describe, expect, test, vi } from 'vitest' +import { Test, TestingModule } from '@nestjs/testing' +import { mock } from 'vitest-mock-extended' + +import { ProfilesService } from './profiles.service' +import { ProfilesRepository } from './profiles.repository' +import { CreateProfile } from './dtos' +import { Profile } from './profile.entity' + +import { Image, ImagesRepository } from '@modules/images' + +describe('ProfilesService', () => { + let profileService: ProfilesService + let profileRepository: ProfilesRepository + let imagesRepository: ImagesRepository + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + ProfilesService, + { + provide: ProfilesRepository, + useValue: { + createProfile: vi.fn(), + }, + }, + { + provide: ImagesRepository, + useValue: { + createImage: vi.fn(), + }, + }, + ], + }).compile() + + profileService = module.get(ProfilesService) + profileRepository = module.get(ProfilesRepository) + imagesRepository = module.get(ImagesRepository) + }) + + test('should be defined', () => { + expect(profileService).toBeDefined() + }) + + test('should create profile', async () => { + const imageMock = mock() + const createProfileMock = mock({ + images: [imageMock], + }) + const profileMock = mock() + + const createProfileSpy = vi + .spyOn(profileRepository, 'createProfile') + .mockResolvedValue(profileMock) + const createImageSpy = vi + .spyOn(imagesRepository, 'createImage') + .mockResolvedValue(imageMock) + + expect(await profileService.create(createProfileMock)).toEqual(profileMock) + + expect(createProfileSpy).toHaveBeenCalled() + expect(createImageSpy).toHaveBeenCalled() + }) +}) diff --git a/src/modules/profiles/profiles.service.ts b/src/modules/profiles/profiles.service.ts new file mode 100644 index 00000000..8d076033 --- /dev/null +++ b/src/modules/profiles/profiles.service.ts @@ -0,0 +1,29 @@ +import { Injectable } from '@nestjs/common' + +import { ProfilesRepository } from './profiles.repository' +import { CreateProfile } from './dtos' + +import { ImagesRepository } from '@modules/images' + +@Injectable() +export class ProfilesService { + constructor( + private readonly profilesRepository: ProfilesRepository, + private readonly imagesRepository: ImagesRepository + ) {} + + create({ images, ...newProfile }: CreateProfile) { + let imageEntities = [] + + images.map(async image => { + const newImage = await this.imagesRepository.createImage(image) + + imageEntities = [newImage] + }) + + return this.profilesRepository.createProfile({ + ...newProfile, + images: imageEntities, + }) + } +}