Skip to content

Commit

Permalink
fix(modules): fetching user history if play history is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnigos committed Apr 12, 2024
1 parent 3180d7e commit 443a391
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 28 deletions.
72 changes: 46 additions & 26 deletions src/modules/history/tracks/history-tracks.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,33 +97,53 @@ describe('HistoryTracksService', () => {
})
})

test('should create history tracks from play history', async () => {
const playHistoryMock: PlayHistory[] = [
{
played_at: new Date().toISOString(),
track: sdkTrackMock,
context: mock<Context>(),
},
]

const findOrCreateTracks = vi
.spyOn(tracksService, 'findOrCreate')
.mockResolvedValue(trackEntitiesMock)

const createHistoryTrackSpy = vi
.spyOn(historyTracksRepository, 'createHistoryTrack')
.mockResolvedValue(historyTrackMock)

expect(
await historyTracksService.createHistoryTracksFromPlayHistory(
playHistoryMock,
userMock
describe('createHistoryTracksFromPlayHistory', () => {
test('should return empty array if no tracks are found or created', async () => {
const playHistoryMock = []

const findOrCreateTracks = vi
.spyOn(tracksService, 'findOrCreate')
.mockResolvedValue([])

expect(
await historyTracksService.createHistoryTracksFromPlayHistory(
playHistoryMock,
userMock
)
).toEqual([])
expect(findOrCreateTracks).toHaveBeenCalledWith(
playHistoryMock.map(({ track }) => track)
)
).toEqual(historyTracksMock)
})

test('should create history tracks from play history', async () => {
const playHistoryMock: PlayHistory[] = [
{
played_at: new Date().toISOString(),
track: sdkTrackMock,
context: mock<Context>(),
},
]

const findOrCreateTracks = vi
.spyOn(tracksService, 'findOrCreate')
.mockResolvedValue(trackEntitiesMock)

const createHistoryTrackSpy = vi
.spyOn(historyTracksRepository, 'createHistoryTrack')
.mockResolvedValue(historyTrackMock)

expect(findOrCreateTracks).toHaveBeenCalledWith(
playHistoryMock.map(({ track }) => track)
)
expect(createHistoryTrackSpy).toHaveBeenCalled()
expect(
await historyTracksService.createHistoryTracksFromPlayHistory(
playHistoryMock,
userMock
)
).toEqual(historyTracksMock)

expect(findOrCreateTracks).toHaveBeenCalledWith(
playHistoryMock.map(({ track }) => track)
)
expect(createHistoryTrackSpy).toHaveBeenCalled()
})
})
})
2 changes: 2 additions & 0 deletions src/modules/history/tracks/history-tracks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export class HistoryTracksService {
playHistory.map(({ track }) => track)
)

if (tracks.length === 0) return []

const historyTracks: HistoryTrack[] = []

for (const track of tracks) {
Expand Down
22 changes: 22 additions & 0 deletions src/modules/tracks/tracks.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ describe('TracksService', () => {
expect(findOrCreateTracksFromDtosSpy).not.toHaveBeenCalled()
})

test('should return empty array if no data', async () => {
expect(await tracksService.findOrCreate([])).toEqual([])
expect(findOrCreateTrackFromExternalIdSpy).not.toHaveBeenCalled()
expect(findOrCreateTrackFromDtoSpy).not.toHaveBeenCalled()
expect(findOrCreateTracksFromExternalIdsSpy).not.toHaveBeenCalled()
expect(findOrCreateTracksFromDtosSpy).not.toHaveBeenCalled()
})

test('should find or create tracks from external ids', async () => {
findOrCreateTracksFromExternalIdsSpy.mockResolvedValue(trackEntitiesMock)

Expand Down Expand Up @@ -326,6 +334,14 @@ describe('TracksService', () => {
findOrCreateAlbum = vi.spyOn(albumsService, 'findOrCreate')
})

test('should return empty array if no external ids', async () => {
expect(
await tracksService.findOrCreateTracksFromExternalIds([], [])
).toEqual([])
expect(findTrackByExternalIdSpy).not.toHaveBeenCalled()
expect(findOrCreateAlbum).not.toHaveBeenCalled()
})

test('should find tracks by external ids', async () => {
findTrackByExternalIdSpy.mockResolvedValue(foundTracks)

Expand Down Expand Up @@ -404,6 +420,12 @@ describe('TracksService', () => {
findOrCreateAlbum = vi.spyOn(albumsService, 'findOrCreate')
})

test('should return empty array if no tracks', async () => {
expect(await tracksService.findOrCreateTracksFromDtos([])).toEqual([])
expect(findTrackByExternalIdSpy).not.toHaveBeenCalled()
expect(findOrCreateAlbum).not.toHaveBeenCalled()
})

test('should find tracks by external ids', async () => {
findTrackByExternalIdSpy.mockResolvedValue(foundTracks)

Expand Down
11 changes: 9 additions & 2 deletions src/modules/tracks/tracks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,16 @@ export class TracksService implements ItemService<SdkCreateTrack, Track> {

if ('id' in data) return this.findOrCreateTrackFromDto(data)

if (Array.isArray(data) && data.length > 0)
if (Array.isArray(data)) {
if (data.length === 0) return []

return typeof data[0] === 'string'
? this.findOrCreateTracksFromExternalIds(
data as string[],
idOrIds as string[]
)
: this.findOrCreateTracksFromDtos(data as SdkCreateTrack[])
}
}

async findOrCreateTrackFromExternalId(
Expand All @@ -158,6 +161,8 @@ export class TracksService implements ItemService<SdkCreateTrack, Track> {
externalIds: string[],
albumsExternalIds: string[]
): Promise<Track[]> {
if (externalIds.length === 0 || albumsExternalIds.length === 0) return []

const filteredExternalIds = removeDuplicates(externalIds)

const foundTracks =
Expand Down Expand Up @@ -189,7 +194,9 @@ export class TracksService implements ItemService<SdkCreateTrack, Track> {
return this.findOrCreateTrackFromDto(track)
}

async findOrCreateTracksFromDtos(tracks: SdkCreateTrack[]) {
async findOrCreateTracksFromDtos(tracks: SdkCreateTrack[]): Promise<Track[]> {
if (tracks.length === 0) return []

const externalIds = tracks.map(track => track.id)

const foundTracks =
Expand Down

0 comments on commit 443a391

Please sign in to comment.