Skip to content

Commit

Permalink
Merge 2788402 into f8821dc
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmahidalgo committed Apr 6, 2022
2 parents f8821dc + 2788402 commit 80b6f99
Show file tree
Hide file tree
Showing 15 changed files with 693 additions and 243 deletions.
37 changes: 33 additions & 4 deletions src/Collection/Collection.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,55 @@ import { Model, raw, SQL } from 'decentraland-server'
import { Item } from '../Item/Item.model'
import { CollectionAttributes } from './Collection.types'

type CollectionWithItemCount = CollectionAttributes & {
item_count: number
}

export class Collection extends Model<CollectionAttributes> {
static tableName = 'collections'

static findAll() {
return this.query<CollectionWithItemCount>(SQL`
SELECT *, (SELECT COUNT(*) FROM ${raw(
Item.tableName
)} WHERE items.collection_id = collections.id) as item_count
FROM ${raw(this.tableName)}
`)
}

static findByAllByAddress(address: string) {
return this.query<CollectionWithItemCount>(SQL`
SELECT *, (SELECT COUNT(*) FROM ${raw(
Item.tableName
)} WHERE items.collection_id = collections.id) as item_count
FROM ${raw(this.tableName)}
WHERE eth_address = ${address}
`)
}

static findByIds(ids: string[]) {
return this.query<CollectionAttributes>(SQL`
SELECT *
return this.query<CollectionWithItemCount>(SQL`
SELECT *, (SELECT COUNT(*) FROM ${raw(
Item.tableName
)} WHERE items.collection_id = collections.id) as item_count
FROM ${raw(this.tableName)}
WHERE id = ANY(${ids})`)
}

static findByThirdPartyIds(thirdPartyIds: string[]) {
return this.query<CollectionAttributes>(SQL`
SELECT *
SELECT *, (SELECT COUNT(*) FROM ${raw(
Item.tableName
)} WHERE items.collection_id = collections.id) as item_count
FROM ${raw(this.tableName)}
WHERE third_party_id = ANY(${thirdPartyIds})`)
}

static findByContractAddresses(contractAddresses: string[]) {
return this.query<CollectionAttributes>(SQL`
SELECT *
SELECT *, (SELECT COUNT(*) FROM ${raw(
Item.tableName
)} WHERE items.collection_id = collections.id) as item_count
FROM ${raw(this.tableName)}
WHERE contract_address = ANY(${contractAddresses})`)
}
Expand Down
137 changes: 70 additions & 67 deletions src/Collection/Collection.router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ describe('Collection router', () => {
describe('when retrieving all the collections', () => {
beforeEach(() => {
;(isCommitteeMember as jest.Mock).mockResolvedValueOnce(true)
;(Collection.find as jest.Mock)
;(Collection.findAll as jest.Mock)
.mockResolvedValueOnce([dbCollection])
.mockResolvedValueOnce([])
;(Collection.findByContractAddresses as jest.Mock).mockResolvedValueOnce(
Expand Down Expand Up @@ -860,7 +860,9 @@ describe('Collection router', () => {

describe('when retrieving the collections of an address', () => {
beforeEach(() => {
;(Collection.find as jest.Mock).mockReturnValueOnce([dbCollection])
;(Collection.findByAllByAddress as jest.Mock).mockReturnValueOnce([
dbCollection,
])
;(Collection.findByContractAddresses as jest.Mock).mockReturnValueOnce([])
;(Collection.findByThirdPartyIds as jest.Mock).mockReturnValueOnce([
dbTPCollection,
Expand Down Expand Up @@ -909,7 +911,7 @@ describe('Collection router', () => {
beforeEach(() => {
mockExistsMiddleware(Collection, dbCollection.id)
;(hasPublicAccess as jest.Mock).mockResolvedValueOnce(true)
;(Collection.findOne as jest.Mock).mockReturnValueOnce(dbCollection)
;(Collection.findByIds as jest.Mock).mockReturnValueOnce([dbCollection])
;(collectionAPI.fetchCollection as jest.Mock).mockReturnValueOnce(null)
url = `/collections/${dbCollection.id}`
})
Expand All @@ -927,7 +929,7 @@ describe('Collection router', () => {
},
ok: true,
})
expect(Collection.findOne).toHaveBeenCalledWith(dbCollection.id)
expect(Collection.findByIds).toHaveBeenCalledWith([dbCollection.id])
})
})
})
Expand All @@ -943,9 +945,9 @@ describe('Collection router', () => {
jest.spyOn(Date, 'now').mockReturnValueOnce(now)
mockExistsMiddleware(Collection, dbCollection.id)
mockCollectionAuthorizationMiddleware(dbCollection.id, wallet.address)
;(Collection.findOne as jest.MockedFunction<
;(Collection.findByIds as jest.MockedFunction<
typeof Collection.findOne
>).mockResolvedValueOnce(dbCollection)
>).mockResolvedValueOnce([dbCollection])
url = `/collections/${dbCollection.id}/lock`
})

Expand Down Expand Up @@ -982,9 +984,9 @@ describe('Collection router', () => {
;(Collection.count as jest.MockedFunction<
typeof Collection.count
>).mockResolvedValueOnce(1)
;(Collection.findOne as jest.MockedFunction<
;(Collection.findByIds as jest.MockedFunction<
typeof Collection.findOne
>).mockResolvedValueOnce(dbCollection)
>).mockResolvedValueOnce([dbCollection])
})

it('should fail with an error if the update throws', () => {
Expand Down Expand Up @@ -1052,9 +1054,9 @@ describe('Collection router', () => {
wallet.address,
true
)
;(Collection.findOne as jest.MockedFunction<
;(Collection.findByIds as jest.MockedFunction<
typeof Collection.findOne
>).mockResolvedValueOnce(dbTPCollection)
>).mockResolvedValueOnce([dbTPCollection])
})

describe('and it has third party items already published', () => {
Expand Down Expand Up @@ -1152,9 +1154,9 @@ describe('Collection router', () => {
beforeEach(() => {
mockExistsMiddleware(Collection, dbCollection.id)
mockCollectionAuthorizationMiddleware(dbCollection.id, wallet.address)
;(Collection.findOne as jest.MockedFunction<
;(Collection.findByIds as jest.MockedFunction<
typeof Collection.findOne
>).mockResolvedValueOnce(dbCollection)
>).mockResolvedValueOnce([dbCollection])
})

describe('and it is already published', () => {
Expand Down Expand Up @@ -1259,9 +1261,9 @@ describe('Collection router', () => {
describe('when sending an empty item ids array', () => {
beforeEach(() => {
;(Item.findByIds as jest.Mock).mockResolvedValueOnce([])
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
dbTPCollection
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollection,
])
})

it('should respond with a 400 and a message signaling the item ids should not be empty', () => {
Expand Down Expand Up @@ -1293,9 +1295,9 @@ describe('Collection router', () => {
let tpCurationsAmonut: number

beforeEach(() => {
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
dbTPCollection
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollection,
])
items = [
{ ...dbTPItemMock, id: 'c241ef7c-4466-41b0-bf94-be1b8c331fdb' },
{ ...dbTPItemMock, id: 'anotherId' },
Expand Down Expand Up @@ -1340,9 +1342,9 @@ describe('Collection router', () => {
let items: ItemAttributes[]

beforeEach(() => {
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
dbTPCollection
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollection,
])
items = [
{ ...dbTPItemMock, id: 'c241ef7c-4466-41b0-bf94-be1b8c331fdb' },
{ ...dbTPItemMock, id: 'anotherId' },
Expand Down Expand Up @@ -1376,9 +1378,9 @@ describe('Collection router', () => {

describe('when sending an invalid signature', () => {
beforeEach(() => {
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
dbTPCollection
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollection,
])
;(Item.findByIds as jest.Mock).mockResolvedValueOnce([dbTPItemMock])
})

Expand Down Expand Up @@ -1413,9 +1415,9 @@ describe('Collection router', () => {
let itemIds: string[]

beforeEach(() => {
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
dbTPCollection
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollection,
])
items = [
{ ...dbTPItemMock, id: uuid(), collection_id: '1' },
{ ...dbTPItemMock, id: uuid(), collection_id: '1' },
Expand Down Expand Up @@ -1459,9 +1461,9 @@ describe('Collection router', () => {

describe('when the collection already has a pending ItemCuration', () => {
beforeEach(() => {
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
dbTPCollection
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollection,
])
;(Item.findByIds as jest.Mock).mockResolvedValueOnce([dbItemMock])
;(ItemCuration.findLastCreatedByCollectionIdAndStatus as jest.Mock).mockResolvedValueOnce(
itemCurationMock
Expand Down Expand Up @@ -1505,9 +1507,9 @@ describe('Collection router', () => {
let slotUsageCheque: SlotUsageChequeAttributes

beforeEach(() => {
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
dbTPCollection
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollection,
])
dbItems = [
{ ...dbItemMock, id: uuid() },
{ ...dbItemMock, id: uuid() },
Expand Down Expand Up @@ -1627,9 +1629,6 @@ describe('Collection router', () => {
;(ItemCuration.create as jest.Mock).mockResolvedValue(
itemCurationMock
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollectionMock,
])
;(createPost as jest.Mock).mockResolvedValueOnce({
id: forumId,
link: forumLink,
Expand All @@ -1650,9 +1649,9 @@ describe('Collection router', () => {

describe('and the item collection does not have a virtual curation', () => {
beforeEach(() => {
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
dbTPCollection
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollection,
])
})
it('should create a SlotUsageCheque record with the request data', () => {
return server
Expand Down Expand Up @@ -1728,9 +1727,9 @@ describe('Collection router', () => {
let collectionCuration: CollectionCurationAttributes

beforeEach(() => {
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
dbTPCollection
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollection,
])
collectionCuration = { id: uuid() } as CollectionCurationAttributes
;(CollectionCuration.findOne as jest.Mock).mockResolvedValueOnce(
collectionCuration
Expand Down Expand Up @@ -1774,9 +1773,9 @@ describe('Collection router', () => {

describe('and the collection is being published for the first time', () => {
beforeEach(() => {
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
dbTPCollection
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollection,
])
})
it('should create a forum post with the response data', () => {
return server
Expand Down Expand Up @@ -1852,10 +1851,12 @@ describe('Collection router', () => {
let forumId: number
beforeEach(() => {
forumId = 1
;(Collection.findOne as jest.Mock).mockResolvedValueOnce({
...dbTPCollection,
forum_id: forumId,
})
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
{
...dbTPCollection,
forum_id: forumId,
},
])
})
it('should update the forum post with the response data', () => {
return server
Expand Down Expand Up @@ -1884,7 +1885,9 @@ describe('Collection router', () => {
describe('and the collection is a Decentraland collection', () => {
beforeEach(() => {
url = `/collections/${dbTPCollection.id}/publish`
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(dbCollection)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbCollection,
])
mockExistsMiddleware(Collection, dbCollection.id)
})

Expand Down Expand Up @@ -1974,7 +1977,7 @@ describe('Collection router', () => {
>).mockResolvedValueOnce([dbItemMock, anotherDBItem])
;(Collection.findByIds as jest.MockedFunction<
typeof Collection.findByIds
>).mockResolvedValueOnce([dbCollection])
>).mockResolvedValueOnce([{ ...dbCollection, item_count: 1 }])
;(peerAPI.fetchWearables as jest.MockedFunction<
typeof peerAPI.fetchWearables
>).mockResolvedValueOnce([])
Expand Down Expand Up @@ -2046,9 +2049,9 @@ describe('Collection router', () => {
true,
false
)
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
dbTPCollection
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollection,
])
})

it('should respond with a 401 and a message signaling that the user is not authorized to upsert the collection', () => {
Expand Down Expand Up @@ -2100,9 +2103,9 @@ describe('Collection router', () => {
true,
true
)
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
dbTPCollection
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollection,
])
;(Item.findDBApprovalDataByCollectionId as jest.Mock).mockResolvedValueOnce(
[]
)
Expand Down Expand Up @@ -2136,9 +2139,9 @@ describe('Collection router', () => {
true,
true
)
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
dbTPCollection
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollection,
])
;(Item.findDBApprovalDataByCollectionId as jest.Mock).mockResolvedValueOnce(
[{}]
)
Expand Down Expand Up @@ -2187,9 +2190,9 @@ describe('Collection router', () => {
true,
true
)
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
dbTPCollection
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollection,
])
;(Item.findDBApprovalDataByCollectionId as jest.Mock).mockResolvedValueOnce(
itemApprovalData
)
Expand Down Expand Up @@ -2247,9 +2250,9 @@ describe('Collection router', () => {
true,
true
)
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
dbTPCollection
)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollection,
])
;(Item.findDBApprovalDataByCollectionId as jest.Mock).mockResolvedValueOnce(
itemApprovalData
)
Expand Down Expand Up @@ -2339,7 +2342,7 @@ describe('Collection router', () => {
true,
true
)
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(dbCollection)
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([dbCollection])
;(SlotUsageCheque.findLastByCollectionId as jest.Mock).mockResolvedValueOnce(
{}
)
Expand Down
Loading

0 comments on commit 80b6f99

Please sign in to comment.