From 03456787803eebef965ac6f7272fe47ea2fa1bd9 Mon Sep 17 00:00:00 2001 From: Juanma Hidalgo Date: Wed, 11 Dec 2024 22:18:56 +0100 Subject: [PATCH] feat: release client if nft query fails --- src/ports/nfts/component.ts | 43 +++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/ports/nfts/component.ts b/src/ports/nfts/component.ts index 9a3cd3d..9561484 100644 --- a/src/ports/nfts/component.ts +++ b/src/ports/nfts/component.ts @@ -1,5 +1,6 @@ import { ListingStatus, NFTCategory, NFTFilters, RentalStatus } from '@dcl/schemas' import { fromNFTsAndOrdersToNFTsResult } from '../../adapters/nfts' +import { HttpError } from '../../logic/http/response' import { AppComponents } from '../../types' import { getOrdersQuery } from '../orders/queries' import { DBOrder } from '../orders/types' @@ -27,20 +28,34 @@ export function createNFTsComponent(components: Pick(nftsQuery) - const nftIds = nfts.rows.map(nft => nft.id) - const query = getOrdersQuery({ nftIds, status: ListingStatus.OPEN, owner }) - const orders = await pg.query(query) - - const landNftIds = nfts.rows - .filter(nft => nft.category === NFTCategory.PARCEL || nft.category === NFTCategory.ESTATE) - .map(nft => nft.id) - const listings = landNftIds.length ? await rentals.getRentalsListingsOfNFTs(landNftIds, RentalStatus.OPEN) : [] - - return { - data: fromNFTsAndOrdersToNFTsResult(nfts.rows, orders.rows, listings), - total: nfts.rowCount > 0 ? Number(nfts.rows[0].count) : 0 + const client = await pg.getPool().connect() + let query + try { + query = getNFTsQuery(nftFilters) + const nfts = await client.query(query) + const nftIds = nfts.rows.map(nft => nft.id) + query = getOrdersQuery({ nftIds, status: ListingStatus.OPEN, owner }) + const orders = await client.query(query) + + const landNftIds = nfts.rows + .filter(nft => nft.category === NFTCategory.PARCEL || nft.category === NFTCategory.ESTATE) + .map(nft => nft.id) + const listings = landNftIds.length ? await rentals.getRentalsListingsOfNFTs(landNftIds, RentalStatus.OPEN) : [] + + return { + data: fromNFTsAndOrdersToNFTsResult(nfts.rows, orders.rows, listings), + total: nfts.rowCount > 0 ? Number(nfts.rows[0].count) : 0 + } + } catch (error) { + if ((error as Error).message === 'Query read timeout') { + console.error('Query timeout exceeded (2 minutes)', { + filters, + query + }) + } + throw new HttpError("Couldn't fetch nfts with the filters provided", 400) + } finally { + await client.release() } }