From 3561f0276695397f2238841424aeb49f118c1fc6 Mon Sep 17 00:00:00 2001 From: Mnigos Date: Sat, 9 Dec 2023 00:55:40 +0100 Subject: [PATCH 1/2] feat(modules/users-profile.controller): add `getAnalysis` method` --- .../users/users-profile.controller.spec.ts | 19 +++++++++++++ src/modules/users/users-profile.controller.ts | 28 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/modules/users/users-profile.controller.spec.ts b/src/modules/users/users-profile.controller.spec.ts index ff3d1960..cf4e800f 100644 --- a/src/modules/users/users-profile.controller.spec.ts +++ b/src/modules/users/users-profile.controller.spec.ts @@ -322,4 +322,23 @@ describe('UsersProfileController', () => { ) }) }) + + describe('getAnalysis', () => { + test('should get user analysis', async () => { + const findOneBySpy = vi + .spyOn(usersRepository, 'findOneBy') + .mockResolvedValue(userMock) + + expect(await usersProfileController.getAnalysis(id)).toEqual(userMock) + expect(findOneBySpy).toHaveBeenCalledWith({ id }) + }) + + test('should throw an error if no user is found', () => { + const findOneBySpy = vi.spyOn(usersRepository, 'findOneBy') + + expect(usersProfileController.getAnalysis(id)).rejects.toThrowError() + + expect(findOneBySpy).toHaveBeenCalledWith({ id }) + }) + }) }) diff --git a/src/modules/users/users-profile.controller.ts b/src/modules/users/users-profile.controller.ts index af1bece7..7e404128 100644 --- a/src/modules/users/users-profile.controller.ts +++ b/src/modules/users/users-profile.controller.ts @@ -177,4 +177,32 @@ export class UsersProfileController { this.statisticsService.topGenres(accessToken, limit, timeRange, offset) ) } + + @Get('analysis') + @ApiOperation({ + summary: "Getting user's analysis.", + }) + @ApiParam({ name: 'id' }) + @ApiOkResponse({ + description: ONE_SUCCESFULLY_FOUND(USER), + }) + @ApiNotFoundResponse({ + description: NOT_BEEN_FOUND(USER), + }) + @ApiBadRequestResponse({ + description: ONE_IS_INVALID('uuid'), + }) + async getAnalysis(@Param('id', ParseUUIDPipe) id: string) { + const foundUser = await this.usersRepository.findOneBy({ id }) + + if (!foundUser) throw new NotFoundException(NOT_BEEN_FOUND(USER)) + + const { accessToken } = await firstValueFrom( + this.authService.token({ + refreshToken: foundUser.refreshToken, + }) + ) + + return firstValueFrom(this.statisticsService.analysis(accessToken)) + } } From 29afeba098b53441b81c84b3fe6b2ff0e0f6654e Mon Sep 17 00:00:00 2001 From: Mnigos Date: Sat, 9 Dec 2023 01:27:02 +0100 Subject: [PATCH 2/2] test(modules/users-profile.controller): `should get user analysis` test --- .../users/users-profile.controller.spec.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/modules/users/users-profile.controller.spec.ts b/src/modules/users/users-profile.controller.spec.ts index cf4e800f..73865301 100644 --- a/src/modules/users/users-profile.controller.spec.ts +++ b/src/modules/users/users-profile.controller.spec.ts @@ -14,6 +14,7 @@ import { topGenresMock, spotifyResponseWithOffsetMockFactory, artistsMock, + analysisMock, } from '@common/mocks' import { SecretData } from '@modules/auth/dtos' @@ -60,6 +61,7 @@ describe('UsersProfileController', () => { topTracks: vi.fn(), topGenres: vi.fn(), topArtists: vi.fn(), + analysis: vi.fn(), }, }, ], @@ -328,8 +330,18 @@ describe('UsersProfileController', () => { const findOneBySpy = vi .spyOn(usersRepository, 'findOneBy') .mockResolvedValue(userMock) - - expect(await usersProfileController.getAnalysis(id)).toEqual(userMock) + const tokenSpy = vi + .spyOn(authService, 'token') + .mockReturnValue(of(secretDataMock)) + const analysisSpy = vi + .spyOn(statisticsService, 'analysis') + .mockReturnValue(of(analysisMock)) + + expect(await usersProfileController.getAnalysis(id)).toEqual(analysisMock) + expect(tokenSpy).toHaveBeenCalledWith({ + refreshToken: userMock.refreshToken, + }) + expect(analysisSpy).toHaveBeenCalledWith(accessToken) expect(findOneBySpy).toHaveBeenCalledWith({ id }) })