Skip to content

Commit

Permalink
feat: delete by ids
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosantangelo committed Mar 15, 2022
1 parent f47b8e6 commit 3c0868e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 39 deletions.
22 changes: 8 additions & 14 deletions src/Collection/Collection.router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1375,9 +1375,6 @@ describe('Collection router', () => {
})

describe('and interating with the database fails', () => {
let slotUsageChequeDeleteSpy: jest.SpyInstance<
ReturnType<typeof SlotUsageCheque.delete>
>
let itemCurationDeleteSpy: jest.SpyInstance<
ReturnType<typeof ItemCuration.delete>
>
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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),
])
})
})
})
Expand Down
48 changes: 26 additions & 22 deletions src/Collection/Collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SlotUsageChequeAttributes>({
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<SlotUsageChequeAttributes>(
newSlotUsageCheque
)

const promises = []
for (const item of dbItems) {
promises.push(
ItemCuration.create<ItemCurationAttributes>({
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<ItemCurationAttributes>(itemCuration))
}

itemCurations = await Promise.all(promises)
Expand All @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion src/Curation/ItemCuration/ItemCuration.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ export class ItemCuration extends Model<ItemCurationAttributes> {
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
Expand Down
2 changes: 1 addition & 1 deletion src/ThirdParty/ThirdParty.router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/ThirdParty/ThirdParty.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class ThirdPartyService {
): Promise<number> {
const [maxItems, itemCurationsCount] = await Promise.all([
thirdPartyAPI.fetchMaxItemsByThirdParty(thirdPartyId),
ItemCuration.getCountByThirdPartyId(thirdPartyId),
ItemCuration.countByThirdPartyId(thirdPartyId),
])
return maxItems - itemCurationsCount
}
Expand Down

0 comments on commit 3c0868e

Please sign in to comment.