Skip to content

Commit

Permalink
fix: errors with spotify fetcher methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnigos committed Dec 19, 2023
1 parent a24a128 commit 9bc2492
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 51 deletions.
9 changes: 3 additions & 6 deletions src/common/adapters/playback-state.adapter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PlaybackState, SpotifyPlaybackState } from '../types/spotify'

import { adaptDevices } from './devices.adapter'
import { adaptTracks } from './tracks.adapter'
import { adaptTrack } from './tracks.adapter'

export const adaptPlaybackState = ({
device,
Expand All @@ -10,14 +10,11 @@ export const adaptPlaybackState = ({
is_playing,
item,
}: SpotifyPlaybackState): PlaybackState => {
const [formattedDevice] = adaptDevices([device])
const [formattedTrack] = adaptTracks([item])

return {
device: formattedDevice,
device: device ? adaptDevices([device])[0] : undefined,
repeatState: repeat_state,
shuffleState: shuffle_state,
isPlaying: is_playing,
track: formattedTrack,
track: item ? adaptTrack(item) : undefined,
}
}
20 changes: 10 additions & 10 deletions src/common/types/spotify/playback-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ export enum ShuffleState {
}

export interface SpotifyPlaybackState {
device: SpotifyDevice
repeat_state: RepeatedState
shuffle_state: ShuffleState
is_playing: boolean
item: SpotifyTrack
device?: SpotifyDevice
repeat_state?: RepeatedState
shuffle_state?: ShuffleState
is_playing?: boolean
item?: SpotifyTrack
}

export interface PlaybackState {
device: Device
repeatState: RepeatedState
shuffleState: ShuffleState
isPlaying: boolean
track: Track
device?: Device
repeatState?: RepeatedState
shuffleState?: ShuffleState
isPlaying?: boolean
track?: Track
}
2 changes: 2 additions & 0 deletions src/common/utils/catch-spotify-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const SPOTIFY_DEFAULT_ERROR_MESSAGE =
'Something went wrong with fetching data from spotify API:'

export const catchSpotifyError = (response: SpotifyResponseError) => {
console.log('res', response)
console.log(response.data)
const { data, status } = response

if ('error_description' in data) {
Expand Down
24 changes: 0 additions & 24 deletions src/modules/player/player.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,6 @@ describe('PlayerService', () => {

expect(await playerService.availableDevices('awd')).toEqual(devicesMock)
})

test('should throw Forbidden expception because no device is currently playing', async () => {
vi.spyOn(httpService, 'get').mockReturnValue(
of(
axiosResponseMockFactory({
devices: [],
})
)
)

await expect(playerService.availableDevices('awd')).rejects.toThrowError(
ForbiddenException
)
})
})

describe('currentPlaybackState', () => {
Expand All @@ -86,16 +72,6 @@ describe('PlayerService', () => {
playbackStateMock
)
})

test.skip('should throw Forbidden expception because No device is currently playing', async () => {
vi.spyOn(httpService, 'get').mockReturnValue(
of(axiosResponseMockFactory(forbiddenExceptionObserver))
)

await expect(
playerService.currentPlaybackState('awd')
).rejects.toThrowError(ForbiddenException)
})
})

describe('pausePlayer', () => {
Expand Down
10 changes: 1 addition & 9 deletions src/modules/player/player.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpService } from '@nestjs/axios'
import { Injectable, ForbiddenException } from '@nestjs/common'
import { map, catchError, tap, timer, exhaustMap, firstValueFrom } from 'rxjs'
import { map, catchError, timer, exhaustMap, firstValueFrom } from 'rxjs'

import { PlayerMessage } from './messages'

Expand Down Expand Up @@ -32,10 +32,6 @@ export class PlayerService {
.pipe(
map(response => response.data.devices),
catchError(catchSpotifyError),
tap(devices => {
if (devices.length <= 0)
throw new ForbiddenException(PlayerMessage.NO_AVAIBLE_DEVICES)
}),
map(adaptDevices)
)
)
Expand All @@ -50,10 +46,6 @@ export class PlayerService {
)
.pipe(
map(response => response.data),
tap(playbackState => {
if (!playbackState.device.is_active)
throw new ForbiddenException(PlayerMessage.NO_PLAYING_DEVICE)
}),
map(adaptPlaybackState),
catchError(catchSpotifyError)
)
Expand Down
2 changes: 1 addition & 1 deletion src/modules/statistics/statistics.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('StatisticsService', () => {

test('should generate analysis', async () => {
vi.spyOn(httpService, 'get').mockImplementation((path: string) => {
return path === '/me/top/artists?limit=50'
return path === '/me/top/tracks?limit=50'
? of(
axiosResponseMockFactory(
spotifyResponseMockFactory(spotifyArtistsMock)
Expand Down
2 changes: 1 addition & 1 deletion src/modules/statistics/statistics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class StatisticsService {
return firstValueFrom(
this.httpService
.get<SpotifyResponse<SpotifyArtist>>(
`/me/top/artists?limit=50`,
'/me/top/tracks?limit=50',
applyAuthorizationHeader(accessToken)
)
.pipe(
Expand Down

0 comments on commit 9bc2492

Please sign in to comment.