diff --git a/src/common/adapters/playback-state.adapter.ts b/src/common/adapters/playback-state.adapter.ts index 29efa9a7..f1a94269 100644 --- a/src/common/adapters/playback-state.adapter.ts +++ b/src/common/adapters/playback-state.adapter.ts @@ -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, @@ -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, } } diff --git a/src/common/types/spotify/playback-state.ts b/src/common/types/spotify/playback-state.ts index 86bf297d..96d03c7c 100644 --- a/src/common/types/spotify/playback-state.ts +++ b/src/common/types/spotify/playback-state.ts @@ -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 } diff --git a/src/common/utils/catch-spotify-error.ts b/src/common/utils/catch-spotify-error.ts index e6533fc1..0dc233a4 100644 --- a/src/common/utils/catch-spotify-error.ts +++ b/src/common/utils/catch-spotify-error.ts @@ -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) { diff --git a/src/modules/player/player.service.spec.ts b/src/modules/player/player.service.spec.ts index 5ade831e..d1dec79d 100644 --- a/src/modules/player/player.service.spec.ts +++ b/src/modules/player/player.service.spec.ts @@ -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', () => { @@ -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', () => { diff --git a/src/modules/player/player.service.ts b/src/modules/player/player.service.ts index 80e3b852..31b72a32 100644 --- a/src/modules/player/player.service.ts +++ b/src/modules/player/player.service.ts @@ -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' @@ -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) ) ) @@ -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) ) diff --git a/src/modules/statistics/statistics.service.spec.ts b/src/modules/statistics/statistics.service.spec.ts index 1f12cd5d..0227e591 100644 --- a/src/modules/statistics/statistics.service.spec.ts +++ b/src/modules/statistics/statistics.service.spec.ts @@ -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) diff --git a/src/modules/statistics/statistics.service.ts b/src/modules/statistics/statistics.service.ts index a1bf536e..75967ae8 100644 --- a/src/modules/statistics/statistics.service.ts +++ b/src/modules/statistics/statistics.service.ts @@ -164,7 +164,7 @@ export class StatisticsService { return firstValueFrom( this.httpService .get>( - `/me/top/artists?limit=50`, + '/me/top/tracks?limit=50', applyAuthorizationHeader(accessToken) ) .pipe(