Skip to content

Commit

Permalink
fix(modules/items/service): retrieving albums
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnigos committed Sep 24, 2024
1 parent c7331f0 commit 8045ffc
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 19 deletions.
66 changes: 55 additions & 11 deletions src/modules/items/items.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ describe('ItemsService', () => {
let sdkAlbumsMock: SdkAlbum[]
let albumsMock: Album[]

let findBySpy: MockInstance
let findSpy: MockInstance
let getAlbumsSpy: GetItemsMockInstance<SdkAlbum>
let albumsFindOrCreateSpy: MockInstance

Expand All @@ -332,29 +332,43 @@ describe('ItemsService', () => {
tracks: trackEntitiesMock,
}))

findBySpy = vi.spyOn(entityManagerMock, 'findBy')
findSpy = vi.spyOn(entityManagerMock, 'find')
getAlbumsSpy = vi.spyOn(spotifyService.albums, 'get')
albumsFindOrCreateSpy = vi.spyOn(albumsService, 'findOrCreate')
})

test('should find all albums and does not create any', async () => {
findBySpy.mockResolvedValue(albumsMock)
findSpy.mockResolvedValue(albumsMock)

expect(
await itemsService.findOrCreate(
sdkAlbumsMock as unknown as SdkSimplifiedAlbum[]
)
).toEqual(albumsMock)

expect(findBySpy).toHaveBeenCalledWith(Album, {
externalId: In(albumsExternalIds),
expect(findSpy).toHaveBeenCalledWith(Album, {
where: {
externalId: In(albumsExternalIds),
},
select: {
externalId: true,
},
})
expect(findSpy).toHaveBeenCalledWith(Album, {
where: {
externalId: In(albumsExternalIds),
},
relations: {
artists: true,
},
})
expect(findSpy).toHaveBeenCalledTimes(2)
expect(getAlbumsSpy).not.toHaveBeenCalled()
expect(albumsFindOrCreateSpy).not.toHaveBeenCalled()
})

test('should not find any albums and create all', async () => {
findBySpy.mockResolvedValueOnce([])
findSpy.mockResolvedValueOnce([]).mockResolvedValue(albumsMock)
getAlbumsSpy.mockResolvedValue(sdkAlbumsMock)
albumsFindOrCreateSpy.mockResolvedValue(albumsMock)

Expand All @@ -364,9 +378,23 @@ describe('ItemsService', () => {
)
).toEqual(albumsMock)

expect(findBySpy).toHaveBeenCalledWith(Album, {
externalId: In(albumsExternalIds),
expect(findSpy).toHaveBeenCalledWith(Album, {
where: {
externalId: In(albumsExternalIds),
},
select: {
externalId: true,
},
})
expect(findSpy).toHaveBeenCalledWith(Album, {
where: {
externalId: In(albumsExternalIds),
},
relations: {
artists: true,
},
})
expect(findSpy).toHaveBeenCalledTimes(2)
expect(getAlbumsSpy).toHaveBeenCalledWith(albumsExternalIds, false)
expect(albumsFindOrCreateSpy).toHaveBeenCalledWith(
sdkAlbumsMock,
Expand All @@ -378,7 +406,9 @@ describe('ItemsService', () => {
test('should find some albums and create the rest', async () => {
const foundAlbumsMock = [albumsMock[0], albumsMock[1]]

findBySpy.mockResolvedValueOnce(foundAlbumsMock)
findSpy
.mockResolvedValueOnce(foundAlbumsMock)
.mockResolvedValue(albumsMock)
getAlbumsSpy.mockResolvedValue(sdkAlbumsMock.slice(2))
albumsFindOrCreateSpy.mockResolvedValue(albumsMock.slice(2))

Expand All @@ -388,9 +418,23 @@ describe('ItemsService', () => {
)
).toEqual(albumsMock)

expect(findBySpy).toHaveBeenCalledWith(Album, {
externalId: In(albumsExternalIds),
expect(findSpy).toHaveBeenCalledWith(Album, {
where: {
externalId: In(albumsExternalIds),
},
select: {
externalId: true,
},
})
expect(findSpy).toHaveBeenCalledWith(Album, {
where: {
externalId: In(albumsExternalIds),
},
relations: {
artists: true,
},
})
expect(findSpy).toHaveBeenCalledTimes(2)
expect(getAlbumsSpy).toHaveBeenCalledWith(
albumsExternalIds.slice(2),
false
Expand Down
33 changes: 25 additions & 8 deletions src/modules/items/items.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,43 @@ export class ItemsService {
return this.dataSource.transaction(async manager => {
const albumsExternalIds = removeDuplicates(sdkAlbums.map(({ id }) => id))

const foundAlbums = await manager.findBy(Album, {
externalId: In(albumsExternalIds),
const foundAlbums: { externalId: string }[] = await manager.find(Album, {
where: {
externalId: In(albumsExternalIds),
},
select: {
externalId: true,
},
})
const albumsToCreateExternalIds = albumsExternalIds.filter(
id => !foundAlbums.some(({ externalId }) => id === externalId)
)

if (albumsToCreateExternalIds.length === 0) return foundAlbums
if (albumsToCreateExternalIds.length === 0)
return manager.find(Album, {
where: {
externalId: In(albumsExternalIds),
},
relations: {
artists: true,
},
})

const fetchedAlbums = await this.spotifyService.albums.get(
albumsToCreateExternalIds,
false
)

const createdAlbums = await this.albumsService.findOrCreate(
fetchedAlbums,
manager
)
await this.albumsService.findOrCreate(fetchedAlbums, manager)

return [...foundAlbums, ...createdAlbums]
return manager.find(Album, {
where: {
externalId: In(albumsExternalIds),
},
relations: {
artists: true,
},
})
})
}
}

0 comments on commit 8045ffc

Please sign in to comment.