Skip to content

Commit

Permalink
feat(modules/auth.controller): fix profile method
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnigos committed Dec 19, 2023
1 parent adcc16c commit a24a128
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
15 changes: 12 additions & 3 deletions src/modules/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,17 @@ describe('AuthController', () => {
})

test('should return profile', async () => {
vi.spyOn(authService, 'profile').mockResolvedValue(profileMock)

expect(await authController.profile('123')).toEqual(profileMock)
const accessToken = '123'

const profileSpy = vi
.spyOn(authService, 'profile')
.mockResolvedValue(profileMock)
const findOneByProfileIdSpy = vi
.spyOn(usersRepository, 'findOneByProfileId')
.mockResolvedValue(userMock)

expect(await authController.profile(accessToken)).toEqual(userMock)
expect(profileSpy).toHaveBeenCalledWith(accessToken)
expect(findOneByProfileIdSpy).toHaveBeenCalledWith(profileMock.id)
})
})
22 changes: 12 additions & 10 deletions src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ export class AuthController {
spotifyProfile.id
)

if (!foundUser && refreshToken) {
const profile = await this.profilesService.create(spotifyProfile)
if (refreshToken) {
if (!foundUser) {
const profile = await this.profilesService.create(spotifyProfile)

await this.usersRepository.createUser({
profile,
refreshToken,
})
}
await this.usersRepository.createUser({
profile,
refreshToken,
})
}

if (refreshToken && accessToken) {
return {
url: `${this.configService.get(
CLIENT_CALLBACK_URL
Expand Down Expand Up @@ -119,7 +119,9 @@ export class AuthController {
@ApiOkResponse({
description: "User's profile has been succesfully found",
})
profile(@Token() accessToken: string) {
return this.authService.profile(accessToken)
async profile(@Token() accessToken: string) {
const { id } = await this.authService.profile(accessToken)

return this.usersRepository.findOneByProfileId(id)
}
}

0 comments on commit a24a128

Please sign in to comment.