Skip to content

Commit

Permalink
fix: third party endpoint (#131)
Browse files Browse the repository at this point in the history
* fix: add fix

* chore: improve message

* chore: remove comment
  • Loading branch information
pedrotambo authored Mar 22, 2023
1 parent cb0867d commit 552ea3a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
22 changes: 9 additions & 13 deletions src/adapters/third-party-wearables-fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import LRU from 'lru-cache'
import { IBaseComponent } from '@well-known-components/interfaces'
import { AppComponents, Limits, ThirdPartyAsset, ThirdPartyWearable } from '../types'
import { BlockchainCollectionThirdPartyCollection } from '@dcl/urn-resolver'
import { BlockchainCollectionThirdPartyName } from '@dcl/urn-resolver'
import { findAsync, parseUrn } from '../logic/utils'

// TODO cache metrics
Expand Down Expand Up @@ -38,7 +38,7 @@ export type ThirdPartyWearablesFetcher = IBaseComponent & {
fetchByOwner(address: string, limits: Limits): Promise<ThirdPartyWearablesResult>
fetchCollectionByOwner(
address: string,
collectionUrn: BlockchainCollectionThirdPartyCollection,
thirdPartyNameUrn: BlockchainCollectionThirdPartyName,
limits: Limits
): Promise<ThirdPartyWearablesResult>
}
Expand Down Expand Up @@ -202,7 +202,7 @@ export async function createThirdPartyWearablesFetcherComponent({

async function fetchCollectionByOwner(
address: string,
collectionUrn: BlockchainCollectionThirdPartyCollection,
thirdPartyNameUrn: BlockchainCollectionThirdPartyName,
{ offset, limit }: Limits
): Promise<ThirdPartyWearablesResult> {
let results: ThirdPartyWearable[] = []
Expand All @@ -215,8 +215,7 @@ export async function createThirdPartyWearablesFetcherComponent({
if (
wearableUrn &&
wearableUrn.type === URN_THIRD_PARTY_ASSET_TYPE &&
wearableUrn.collectionId === collectionUrn.collectionId &&
wearableUrn.thirdPartyName === collectionUrn.thirdPartyName
wearableUrn.thirdPartyName === thirdPartyNameUrn.thirdPartyName
) {
results.push(wearable)
}
Expand All @@ -226,25 +225,22 @@ export async function createThirdPartyWearablesFetcherComponent({
(await thirdPartiesCache.fetch(0))!,
async (thirdParty: ThirdParty): Promise<boolean> => {
const urn = await parseUrn(thirdParty.id)
return !!urn && urn.type === URN_THIRD_PARTY_NAME_TYPE && urn.thirdPartyName === collectionUrn.thirdPartyName
return (
!!urn && urn.type === URN_THIRD_PARTY_NAME_TYPE && urn.thirdPartyName === thirdPartyNameUrn.thirdPartyName
)
}
)

if (!thirdParty) {
// NOTE: currently lambdas return an empty array with status code 200 for this case
throw new ThirdPartyFetcherError(
ThirdPartyFetcherErrorCode.THIRD_PARTY_NOT_FOUND,
`Third Party not found ${collectionUrn.thirdPartyName}`
`Third Party not found ${thirdPartyNameUrn.thirdPartyName}`
)
}

const assets = await fetchAssets(address, thirdParty)
results = groupThirdPartyWearablesByURN(
assets.filter((asset: ThirdPartyAsset) => {
const [collectionId, _] = asset.id.split(':')
return collectionId === collectionUrn.collectionId
})
)
results = groupThirdPartyWearablesByURN(assets)

const totalAmount = results.length
return {
Expand Down
12 changes: 8 additions & 4 deletions src/controllers/handlers/wearables-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ export async function thirdPartyCollectionWearablesHandler(
const logger = logs.getLogger('third-party-collections-handler')
const { address, collectionId } = context.params

const urn = await parseUrn(collectionId)
// Strip the last part (the 6th part) if a collection contract id is specified
const collectionIdCleaned = collectionId.split(':').slice(0, 5).join(':')

const urn = await parseUrn(collectionIdCleaned)
if (!urn) {
return {
status: 400,
Expand All @@ -173,11 +176,12 @@ export async function thirdPartyCollectionWearablesHandler(
}
}

if (urn.type !== 'blockchain-collection-third-party-collection') {
if (urn.type !== 'blockchain-collection-third-party-name') {
return {
status: 400,
body: {
error: 'Invalid collection id: not a blockchain-collection-third-party-collection URN'
error:
'Invalid collection id: not a blockchain-collection-third-party-name nor blockchain-collection-third-party-collection URN'
}
}
}
Expand Down Expand Up @@ -227,7 +231,7 @@ export async function thirdPartyCollectionWearablesHandler(
return {
status: 502,
body: {
error: 'Cannot fetch third parties right now'
error: 'Cannot fetch third parties right now. Third party not found'
}
}
}
Expand Down

0 comments on commit 552ea3a

Please sign in to comment.