diff --git a/src/modules/items/items.service.spec.ts b/src/modules/items/items.service.spec.ts index 28169f7b..1024e8fb 100644 --- a/src/modules/items/items.service.spec.ts +++ b/src/modules/items/items.service.spec.ts @@ -310,7 +310,7 @@ describe('ItemsService', () => { let sdkAlbumsMock: SdkAlbum[] let albumsMock: Album[] - let findBySpy: MockInstance + let findSpy: MockInstance let getAlbumsSpy: GetItemsMockInstance let albumsFindOrCreateSpy: MockInstance @@ -332,13 +332,13 @@ 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( @@ -346,15 +346,29 @@ 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).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) @@ -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, @@ -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)) @@ -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 diff --git a/src/modules/items/items.service.ts b/src/modules/items/items.service.ts index 4a96233c..816503d2 100644 --- a/src/modules/items/items.service.ts +++ b/src/modules/items/items.service.ts @@ -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, + }, + }) }) } }