Skip to content

Commit

Permalink
feat(modules/users/users-profile.controller): add /state route
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnigos committed Dec 23, 2023
1 parent dd7b2db commit 690a721
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/common/constants/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const NO_TOKEN_PROVIDED = 'No token provided' as const
1 change: 1 addition & 0 deletions src/modules/player/player.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ import { Environment } from '@config/environment'
],
controllers: [PlayerController],
providers: [PlayerService],
exports: [PlayerService],
})
export class PlayerModule {}
41 changes: 41 additions & 0 deletions src/modules/users/users-profile.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import {
spotifyResponseWithOffsetMockFactory,
artistsMock,
analysisMock,
playbackStateMock,
} from '@common/mocks'
import { SecretData } from '@modules/auth/dtos'
import { PlayerService } from '@modules/player'

describe('UsersProfileController', () => {
const accessToken = 'accessToken'
Expand All @@ -34,6 +36,7 @@ describe('UsersProfileController', () => {
let usersRepository: UsersRepository
let authService: AuthService
let statisticsService: StatisticsService
let playerService: PlayerService

beforeEach(async () => {
const module = await Test.createTestingModule({
Expand Down Expand Up @@ -63,13 +66,20 @@ describe('UsersProfileController', () => {
analysis: vi.fn(),
},
},
{
provide: PlayerService,
useValue: {
currentPlaybackState: vi.fn(),
},
},
],
}).compile()

usersProfileController = module.get(UsersProfileController)
usersRepository = module.get(UsersRepository)
authService = module.get(AuthService)
statisticsService = module.get(StatisticsService)
playerService = module.get(PlayerService)
})

test('should be defined', () => {
Expand Down Expand Up @@ -358,4 +368,35 @@ describe('UsersProfileController', () => {
expect(findOneBySpy).toHaveBeenCalledWith({ id })
})
})

describe('getState', () => {
test('should get user playback state', async () => {
const findOneBySpy = vi
.spyOn(usersRepository, 'findOneBy')
.mockResolvedValue(userMock)
const tokenSpy = vi
.spyOn(authService, 'token')
.mockResolvedValue(secretDataMock)
const currentPlaybackStateSpy = vi
.spyOn(playerService, 'currentPlaybackState')
.mockResolvedValue(playbackStateMock)

expect(await usersProfileController.getState(id)).toEqual(
playbackStateMock
)
expect(currentPlaybackStateSpy).toHaveBeenCalledWith(accessToken)
expect(findOneBySpy).toHaveBeenCalledWith({ id })
expect(tokenSpy).toHaveBeenCalledWith({
refreshToken: userMock.refreshToken,
})
})

test('should throw an error if no user is found', async () => {
const findOneBySpy = vi.spyOn(usersRepository, 'findOneBy')

await expect(usersProfileController.getState(id)).rejects.toThrowError()

expect(findOneBySpy).toHaveBeenCalledWith({ id })
})
})
})
33 changes: 32 additions & 1 deletion src/modules/users/users-profile.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { UsersRepository } from './users.repository'
import { USER } from './users.controller'

import { PlayerService } from '@modules/player'
import { ApiItemQuery } from '@modules/statistics/decorators'
import {
StatisticsService,
Expand All @@ -43,7 +44,8 @@ export class UsersProfileController {
private readonly usersRepository: UsersRepository,
@Inject(forwardRef(() => AuthService))
private readonly authService: AuthService,
private readonly statisticsService: StatisticsService
private readonly statisticsService: StatisticsService,
private readonly playerService: PlayerService
) {}

@Get('last-tracks')
Expand Down Expand Up @@ -213,4 +215,33 @@ export class UsersProfileController {

return this.statisticsService.analysis(accessToken)
}

@Get('state')
@ApiOperation({
summary: "Getting user's playback state.",
})
@ApiParam({ name: 'id' })
@ApiOkResponse({
description: ONE_SUCCESFULLY_FOUND(USER),
})
@ApiNotFoundResponse({
description: NOT_BEEN_FOUND(USER),
})
@ApiBadRequestResponse({
description: ONE_IS_INVALID('uuid'),
})
async getState(
@Param('id', ParseUUIDPipe) id: string,
@Token() _token?: string
) {
const foundUser = await this.usersRepository.findOneBy({ id })

if (!foundUser) throw new NotFoundException(NOT_BEEN_FOUND(USER))

const { accessToken } = await this.authService.token({
refreshToken: foundUser.refreshToken,
})

return this.playerService.currentPlaybackState(accessToken)
}
}
2 changes: 2 additions & 0 deletions src/modules/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { UsersProfileController } from './users-profile.controller'

import { AuthModule } from '@modules/auth'
import { StatisticsModule } from '@modules/statistics'
import { PlayerModule } from '@modules/player'

@Module({
imports: [
TypeOrmModule.forFeature([User]),
forwardRef(() => AuthModule),
StatisticsModule,
PlayerModule,
],
providers: [UsersRepository],
controllers: [UsersController, UsersProfileController],
Expand Down

0 comments on commit 690a721

Please sign in to comment.