From 3c0868e4ac0b5f84299820b3cea40f0ce0abae9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Sant=C3=A1ngelo?= Date: Tue, 15 Mar 2022 16:59:02 -0300 Subject: [PATCH] feat: delete by ids --- src/Collection/Collection.router.spec.ts | 22 ++++----- src/Collection/Collection.service.ts | 48 ++++++++++--------- .../ItemCuration/ItemCuration.model.ts | 8 +++- src/ThirdParty/ThirdParty.router.spec.ts | 2 +- src/ThirdParty/ThirdParty.service.ts | 2 +- 5 files changed, 43 insertions(+), 39 deletions(-) diff --git a/src/Collection/Collection.router.spec.ts b/src/Collection/Collection.router.spec.ts index 91ad22a8..763e8461 100644 --- a/src/Collection/Collection.router.spec.ts +++ b/src/Collection/Collection.router.spec.ts @@ -1375,9 +1375,6 @@ describe('Collection router', () => { }) describe('and interating with the database fails', () => { - let slotUsageChequeDeleteSpy: jest.SpyInstance< - ReturnType - > let itemCurationDeleteSpy: jest.SpyInstance< ReturnType > @@ -1390,18 +1387,15 @@ describe('Collection router', () => { jest.spyOn(ethers.utils, 'verifyMessage').mockReturnValue('0x') jest.spyOn(SlotUsageCheque, 'create').mockResolvedValueOnce({}) + jest.spyOn(SlotUsageCheque, 'delete').mockResolvedValueOnce('') jest .spyOn(ItemCuration, 'create') .mockRejectedValueOnce(new Error('Database error')) - slotUsageChequeDeleteSpy = jest - .spyOn(SlotUsageCheque, 'delete') - .mockResolvedValue('') - itemCurationDeleteSpy = jest - .spyOn(ItemCuration, 'delete') - .mockResolvedValue('') + .spyOn(ItemCuration, 'deleteByIds') + .mockResolvedValue([]) }) it('should respond with a 400 and a message signaling that the database errored out', () => { @@ -1442,12 +1436,12 @@ describe('Collection router', () => { }) .expect(400) .then(() => { - expect(slotUsageChequeDeleteSpy).toHaveBeenCalledWith({ - created_at: expect.any(Date), - }) - expect(itemCurationDeleteSpy).toHaveBeenCalledWith({ - created_at: expect.any(Date), + expect(SlotUsageCheque.delete).toHaveBeenCalledWith({ + id: expect.any(String), }) + expect(itemCurationDeleteSpy).toHaveBeenCalledWith([ + expect.any(String), + ]) }) }) }) diff --git a/src/Collection/Collection.service.ts b/src/Collection/Collection.service.ts index f143b542..3adcd1d4 100644 --- a/src/Collection/Collection.service.ts +++ b/src/Collection/Collection.service.ts @@ -201,33 +201,37 @@ export class CollectionService { } const now = new Date() + let itemCurationIds: string[] = [] let itemCurations: ItemCurationAttributes[] = [] let lastItemCuration: ItemCurationAttributes + let newSlotUsageCheque: SlotUsageChequeAttributes = { + id: uuid(), + signature, + qty, + salt, + collection_id: dbCollection.id, + third_party_id: dbCollection.third_party_id, + created_at: now, + updated_at: now, + } try { - await SlotUsageCheque.create({ - id: uuid(), - signature, - qty, - salt, - collection_id: dbCollection.id, - third_party_id: dbCollection.third_party_id, - created_at: now, - updated_at: now, - }) + await SlotUsageCheque.create( + newSlotUsageCheque + ) const promises = [] for (const item of dbItems) { - promises.push( - ItemCuration.create({ - id: uuid(), - item_id: item.id, - status: CurationStatus.PENDING, - content_hash: item.local_content_hash, - created_at: now, - updated_at: now, - }) - ) + const itemCuration: ItemCurationAttributes = { + id: uuid(), + item_id: item.id, + status: CurationStatus.PENDING, + content_hash: item.local_content_hash, + created_at: now, + updated_at: now, + } + itemCurationIds.push(itemCuration.id) + promises.push(ItemCuration.create(itemCuration)) } itemCurations = await Promise.all(promises) @@ -251,8 +255,8 @@ export class CollectionService { } catch (error) { // Rollback the cheque and all item curations just created in case any database interaction fails await Promise.all([ - SlotUsageCheque.delete({ created_at: now }), - ItemCuration.delete({ created_at: now }), + SlotUsageCheque.delete({ id: newSlotUsageCheque.id }), + ItemCuration.deleteByIds(itemCurationIds), ]) throw new InvalidRequestError( diff --git a/src/Curation/ItemCuration/ItemCuration.model.ts b/src/Curation/ItemCuration/ItemCuration.model.ts index 2c6693b6..4880c271 100644 --- a/src/Curation/ItemCuration/ItemCuration.model.ts +++ b/src/Curation/ItemCuration/ItemCuration.model.ts @@ -60,7 +60,13 @@ export class ItemCuration extends Model { return itemCurations[0] } - static async getCountByThirdPartyId(thirdPartyId: string) { + static async deleteByIds(ids: string[]) { + return this.query(SQL` + DELETE FROM ${raw(this.tableName)} + WHERE id = ANY(${ids})`) + } + + static async countByThirdPartyId(thirdPartyId: string) { const counts = await this.query<{ count: number }>( SQL`SELECT COUNT(DISTINCT item_curations.id) AS Count FROM ${raw(ItemCuration.tableName)} AS item_curations diff --git a/src/ThirdParty/ThirdParty.router.spec.ts b/src/ThirdParty/ThirdParty.router.spec.ts index 0730e93b..9a6fdbed 100644 --- a/src/ThirdParty/ThirdParty.router.spec.ts +++ b/src/ThirdParty/ThirdParty.router.spec.ts @@ -108,7 +108,7 @@ describe('ThirdParty router', () => { maxSlots ) ;(thirdPartyAPI.isManager as jest.Mock).mockResolvedValueOnce(true) - ;(ItemCuration.getCountByThirdPartyId as jest.Mock).mockResolvedValueOnce( + ;(ItemCuration.countByThirdPartyId as jest.Mock).mockResolvedValueOnce( itemsInCuration ) url = '/thirdParties/aThirdPartyId/slots' diff --git a/src/ThirdParty/ThirdParty.service.ts b/src/ThirdParty/ThirdParty.service.ts index 50835530..84d5a7b9 100644 --- a/src/ThirdParty/ThirdParty.service.ts +++ b/src/ThirdParty/ThirdParty.service.ts @@ -8,7 +8,7 @@ export class ThirdPartyService { ): Promise { const [maxItems, itemCurationsCount] = await Promise.all([ thirdPartyAPI.fetchMaxItemsByThirdParty(thirdPartyId), - ItemCuration.getCountByThirdPartyId(thirdPartyId), + ItemCuration.countByThirdPartyId(thirdPartyId), ]) return maxItems - itemCurationsCount }