Skip to content

Commit

Permalink
feat: totalFiltered return from stats
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Feb 18, 2022
1 parent 864837d commit 9e8cb2f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/api/helpers/nftHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export async function populateStat(
): Promise<{
totalNft: number,
totalListedNft: number,
totalFiltered: number | null,
totalListedInMarketplace: number,
totalOwnedByRequestingUser: number,
totalOwnedListedByRequestingUser: number,
Expand All @@ -52,7 +53,7 @@ export async function populateStat(
try {
const marketplaceId = query.filter?.marketplaceId;
const owner = query.filter?.owner;
const stat = await NFTService.getStatNFT(NFT.serieId, marketplaceId, owner)
const stat = await NFTService.getStatNFT(NFT.serieId, marketplaceId, owner, query)
return stat
} catch (err) {
L.error({ err }, "NFTs stats could not have been fetched");
Expand Down
3 changes: 2 additions & 1 deletion src/api/services/gqlQueriesBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ countAllListedInMarketplace = (marketplaceId: number) => gql`
}
`;

countTotalFilteredNFTs = (query: getTotalFilteredNFTsQuery) => {
countTotalFilteredNFTs = (query: getTotalFilteredNFTsQuery, seriesId?: string) => {
const {
idsCategories,
idsToExcludeCategories,
Expand All @@ -445,6 +445,7 @@ countTotalFilteredNFTs = (query: getTotalFilteredNFTsQuery) => {
filter: {
and: [
{ timestampBurn: { isNull: true } }
${seriesId!==undefined ? `{ serieId: { equalTo: "${seriesId}" } }` : ""}
${listed!==undefined ? `{ listed: { equalTo: ${!listed ? 0 : 1} } }` : ""}
${marketplaceId!==undefined ? `{ marketplaceId: { equalTo: "${marketplaceId}" } }` : ""}
${idsCategories ? `{id: { in: ${JSON.stringify(idsCategories.map(x => String(x)))} }}` : ""}
Expand Down
10 changes: 7 additions & 3 deletions src/api/services/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,24 @@ export class NFTService {
* @param seriesId - Series Id of nft to get stat for
* @param marketplaceId - marketplace id (optional)
* @param owner - owner (optional)
* @param filterOptionsQuery - filter options (optional)
* @throws Will throw an error if can't request indexer
*/
async getStatNFT(seriesId:string, marketplaceId:number=null, owner:string=null): Promise<{
async getStatNFT(seriesId:string, marketplaceId:number=null, owner:string=null, filterOptionsQuery:getTotalFilteredNFTsQuery=null): Promise<{
totalNft: number,
totalListedNft: number,
totalFiltered: number | null,
totalListedInMarketplace: number,
totalOwnedByRequestingUser: number,
totalOwnedListedByRequestingUser: number,
totalOwnedListedInMarketplaceByRequestingUser: number,
smallestPrice: string
}> {
try {
const [totalRequest, totalListedRequest, totalListedInMarketplaceRequest, totalOwnedByRequestingUserRequest, totalOwnedListedByRequestingUserRequest, totalOwnedListedInMarketplaceByRequestingUserRequest, smallestPriceRequest] = await Promise.all([
const [totalRequest, totalListedRequest, totalFilteredRequest, totalListedInMarketplaceRequest, totalOwnedByRequestingUserRequest, totalOwnedListedByRequestingUserRequest, totalOwnedListedInMarketplaceByRequestingUserRequest, smallestPriceRequest] = await Promise.all([
request(indexerUrl, QueriesBuilder.countTotal(seriesId)),
request(indexerUrl, QueriesBuilder.countTotalListed(seriesId)),
filterOptionsQuery ? request(indexerUrl, QueriesBuilder.countTotalFilteredNFTs(filterOptionsQuery, seriesId)) : null,
marketplaceId!==null ? request(indexerUrl, QueriesBuilder.countTotalListedInMarketplace(seriesId, marketplaceId)) : 0,
owner ? request(indexerUrl, QueriesBuilder.countTotalOwned(seriesId, owner)) : null,
owner ? request(indexerUrl, QueriesBuilder.countTotalOwnedListed(seriesId, owner)) : null,
Expand All @@ -171,6 +174,7 @@ export class NFTService {
const totalNft: number = totalRequest.nftEntities.totalCount;
const totalListedNft: number = totalListedRequest.nftEntities.totalCount;
const totalListedInMarketplace: number = totalListedInMarketplaceRequest ? totalListedInMarketplaceRequest.nftEntities.totalCount : 0;
const totalFiltered: number = totalFilteredRequest ? totalFilteredRequest.nftEntities.totalCount : null;
const totalOwnedByRequestingUser: number = totalOwnedByRequestingUserRequest ? totalOwnedByRequestingUserRequest.nftEntities.totalCount : 0;
const totalOwnedListedByRequestingUser: number = totalOwnedListedByRequestingUserRequest ? totalOwnedListedByRequestingUserRequest.nftEntities.totalCount : 0;
const totalOwnedListedInMarketplaceByRequestingUser: number = totalOwnedListedInMarketplaceByRequestingUserRequest ? totalOwnedListedInMarketplaceByRequestingUserRequest.nftEntities.totalCount : 0;
Expand All @@ -179,7 +183,7 @@ export class NFTService {
:
"0"
;
return { totalNft, totalListedNft, totalListedInMarketplace, totalOwnedByRequestingUser, totalOwnedListedByRequestingUser, totalOwnedListedInMarketplaceByRequestingUser, smallestPrice }
return { totalNft, totalListedNft, totalListedInMarketplace, totalFiltered, totalOwnedByRequestingUser, totalOwnedListedByRequestingUser, totalOwnedListedInMarketplaceByRequestingUser, smallestPrice }
} catch (err) {
throw new Error("Couldn't get NFT stat");
}
Expand Down

0 comments on commit 9e8cb2f

Please sign in to comment.