Skip to content

Commit

Permalink
feat: feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosantangelo committed Mar 8, 2022
1 parent d4d397e commit 7b69f96
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Collection/Collection.router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ describe('Collection router', () => {
describe('when the collection already has a pending ItemCuration', () => {
beforeEach(() => {
;(Item.findByIds as jest.Mock).mockResolvedValueOnce([dbItemMock])
;(ItemCuration.findLastByCollectionId as jest.Mock).mockResolvedValueOnce(
;(ItemCuration.findLastCreatedByCollectionIdAndStatus as jest.Mock).mockResolvedValueOnce(
itemCurationMock
)
jest.spyOn(ethers.utils, 'verifyMessage').mockReturnValue('0x')
Expand Down
11 changes: 7 additions & 4 deletions src/Collection/Collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ export class CollectionService {
}

/**
* It should receive the list of items to be operated on so it can decide where it should create or update the curations for them accordingly
* It will also receive a signed message and its signature, so it can check and store it in the SlotUsageCheque
* Publishes a TP collection by storing the slots cheque and creating the curations for the items to be published.
* It creates or updates the virtual CollectionCuration for the items
* @param dbCollection - Database TP collection
* @param dbItems - Database TP items that belong to the collection
* @param signedMessage - The message we signed
Expand All @@ -153,7 +153,7 @@ export class CollectionService {
// For TP items, creations always exist. PUSH CHANGES should appear if the item has an approved ItemCuration and has changes in the Catalyst
// That should fire /items/:id/curation for each item that changed

// What we should migrate over here is the creation of the virtual CollectionCuration, as there'll always be a publish before a PUSH CHANGES
// There'll always be a publish before a PUSH CHANGES, so this method also creates or updates the virtual CollectionCuration for the items

if (dbItems.length === 0) {
throw new InvalidRequestError('Tried to publish no TP items')
Expand All @@ -177,7 +177,10 @@ export class CollectionService {
)
}

const isPublished = await ItemCuration.findLastByCollectionId(collectionId)
const isPublished = await ItemCuration.findLastCreatedByCollectionIdAndStatus(
collectionId,
CurationStatus.PENDING
)
if (isPublished) {
throw new AlreadyPublishedCollectionError(
collectionId,
Expand Down
20 changes: 18 additions & 2 deletions src/Curation/ItemCuration/ItemCuration.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Model, raw, SQL } from 'decentraland-server'
import { Collection } from '../../Collection'
import { Item } from '../../Item'
import { CurationType } from '../Curation.types'
import { CurationStatus, CurationType } from '../Curation.types'
import { ItemCurationAttributes } from './ItemCuration.types'

export class ItemCuration extends Model<ItemCurationAttributes> {
Expand Down Expand Up @@ -44,7 +44,23 @@ export class ItemCuration extends Model<ItemCurationAttributes> {
return itemCurations[0]
}

static async getItemCurationCountByThirdPartyId(thirdPartyId: string) {
static async findLastCreatedByCollectionIdAndStatus(
collectionId: string,
curationStatus: CurationStatus
): Promise<ItemCurationAttributes | undefined> {
const itemCurations = await this.query<ItemCurationAttributes>(SQL`
SELECT *
FROM ${raw(this.tableName)} c
JOIN ${raw(Item.tableName)} i ON i.id = c.item_id
WHERE i.collection_id = ${collectionId}
AND i.status = ${curationStatus}
ORDER BY created_at DESC
LIMIT 1`)

return itemCurations[0]
}

static async getCountByThirdPartyId(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.getItemCurationCountByThirdPartyId as jest.Mock).mockResolvedValueOnce(
;(ItemCuration.getCountByThirdPartyId 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.getItemCurationCountByThirdPartyId(thirdPartyId),
ItemCuration.getCountByThirdPartyId(thirdPartyId),
])
return maxItems - itemCurationsCount
}
Expand Down

0 comments on commit 7b69f96

Please sign in to comment.