Skip to content

Commit

Permalink
wip add fetch entities ids
Browse files Browse the repository at this point in the history
  • Loading branch information
leanmendoza committed Nov 13, 2024
1 parent cd789bc commit 91e8aa5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
38 changes: 38 additions & 0 deletions content/src/controller/handlers/active-entities-ids-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Entity } from '@dcl/catalyst-api-specs/lib/client'
import { HandlerContextWithPath, InvalidRequestError } from '../../types'
import Joi from 'joi'

const schema = Joi.alternatives().try(
Joi.object({
pointers: Joi.array().items(Joi.string()).min(1).required()
})
)

// Method: POST
// Body: { pointers: string[]}
export async function getActiveEntitiesIdsHandler(
context: HandlerContextWithPath<'database' | 'activeEntities' | 'denylist', '/entities/active'>
): Promise<{ status: 200; body: Pick<Entity, 'id' | 'pointers'>[] }> {
const { database, activeEntities, denylist } = context.components
const { error, value: body } = schema.validate(await context.request.json())

if (error) {
throw new InvalidRequestError(
'pointers must be present. They must be arrays and contain at least one element. None of the elements can be empty.'
)
}

const entities: Pick<Entity, 'id' | 'pointers'>[] = (await activeEntities.withPointers(database, body.pointers))
.filter((result) => !denylist.isDenylisted(result.id))
.map((entity) => {
return {
id: entity.id,
pointers: entity.pointers
}
})

return {
status: 200,
body: entities
}
}
2 changes: 2 additions & 0 deletions content/src/controller/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { getERC721EntityHandler } from './handlers/get-erc721-entity-handler'
import { getDeploymentsHandler } from './handlers/get-deployments-handler'
import { getChallengeHandler } from './handlers/get-challenge-handler'
import { getActiveEntityIdsByDeploymentHashHandler } from './handlers/get-active-entities-by-deployment-hash-handler'
import { getActiveEntitiesIdsHandler } from './handlers/active-entities-ids-handler'

// We return the entire router because it will be easier to test than a whole server
export async function setupRouter({ components }: GlobalContext): Promise<Router<GlobalContext>> {
Expand All @@ -42,6 +43,7 @@ export async function setupRouter({ components }: GlobalContext): Promise<Router
router.get('/entities/:type', getEntitiesHandler) // TODO: Deprecate
router.get('/entities/active/collections/:collectionUrn', getEntitiesByPointerPrefixHandler)
router.post('/entities/active', getActiveEntitiesHandler)
router.post('/entities/active/ids', getActiveEntitiesIdsHandler)
router.head('/contents/:hashId', getContentHandler)
router.get('/contents/:hashId', getContentHandler)
router.get('/available-content', getAvailableContentHandler)
Expand Down

0 comments on commit 91e8aa5

Please sign in to comment.