Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

203 fix fetching user history if play history is empty #204

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading