diff --git a/src/modules/users/users-profile.controller.spec.ts b/src/modules/users/users-profile.controller.spec.ts index ff3d1960..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(), }, }, ], @@ -322,4 +324,33 @@ describe('UsersProfileController', () => { ) }) }) + + describe('getAnalysis', () => { + test('should get user analysis', async () => { + const findOneBySpy = vi + .spyOn(usersRepository, 'findOneBy') + .mockResolvedValue(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 }) + }) + + 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)) + } }