Skip to content
This repository has been archived by the owner on Aug 31, 2022. It is now read-only.

Commit

Permalink
Merge pull request #168 from capsule-corp-ternoa/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
Leouarz authored Jan 10, 2022
2 parents db70d26 + 435903b commit 2019e14
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 147 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nft-marketplace-api",
"version": "1.2.1",
"version": "1.3.0",
"description": "ternoa nft marketplace server",
"main": "dist/index.js",
"scripts": {
Expand Down
17 changes: 15 additions & 2 deletions src/api/controllers/nfts/controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import NFTService from "../../services/nft";
import { NextFunction, Request, Response } from "express";
import { validationGetNFTs, validationGetNFT, validationGetStatNFTsUser, validationNFTsBySeries, validationGetSeries, validationCanAddToSeries, validationGetHistory, validationAddCategoriesNFTs } from "../../validators/nftValidators";
import { validationGetNFTs, validationGetNFT, validationGetStatNFTsUser, validationNFTsBySeries, validationGetSeries, validationCanAddToSeries, validationGetHistory, validationAddCategoriesNFTs, validationGetTotalOnSale } from "../../validators/nftValidators";

export class Controller {
async getNFTs(
Expand Down Expand Up @@ -96,12 +96,25 @@ export class Controller {
next: NextFunction
): Promise<void>{
try {
const queryValues = validationGetHistory({ ...req.query })
const queryValues = validationGetHistory(req.query)
res.json(await NFTService.getHistory(queryValues));
} catch (err) {
next(err);
}
}

async getTotalOnSale(
req: Request,
res: Response,
next: NextFunction
): Promise<void>{
try {
const queryValues = validationGetTotalOnSale(req.query)
res.json(await NFTService.getTotalOnSale(queryValues));
} catch (err) {
next(err);
}
}
}

export default new Controller();
1 change: 1 addition & 0 deletions src/api/controllers/nfts/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default express
.Router()
.get("/", controller.getNFTs)
.get("/history", controller.getHistory)
.get("/total-on-sale", controller.getTotalOnSale)
.get("/:id", controller.getNFT)
.get("/stat/:id", controller.getStatNFTsUser)
.get("/series/data", controller.getNFTsBySeries)
Expand Down
71 changes: 13 additions & 58 deletions src/api/helpers/nftHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,83 +24,38 @@ const ipfsGatewayUri =
*/
export async function populateNFT(
NFT: INFT,
seriesData: CustomResponse<INFT>,
query: NFTsQuery
): Promise<ICompleteNFT | INFT> {
const [serieData, creatorData, ownerData, info, categories, seriesLocked] =
const [stat, creatorData, ownerData, info, categories, seriesLocked] =
await Promise.all([
populateSerieData(NFT, seriesData, query),
populateStat(NFT, query),
populateNFTCreator(NFT),
populateNFTOwner(NFT),
populateNFTIpfs(NFT),
populateNFTCategories(NFT),
populateNFTSeriesObject(NFT.serieId)
]);
return { ...NFT, ...serieData, creatorData, ownerData, ...info, categories, seriesLocked};
return { ...NFT, ...stat, creatorData, ownerData, ...info, categories, seriesLocked};
}

export async function populateSerieData(
export async function populateStat(
NFT: INFT,
seriesData: CustomResponse<INFT>,
query: NFTsQuery
): Promise<{
serieData: INFT[];
totalNft: number;
totalListedNft: number;
totalListedInMarketplace: number;
totalOwnedByRequestingUser: number;
totalOwnedListedByRequestingUser: number;
smallestPrice: string;
totalNft: number,
totalListedNft: number,
totalListedInMarketplace: number,
totalOwnedByRequestingUser: number,
totalOwnedListedByRequestingUser: number,
smallestPrice: string
}> {
try {
const marketplaceId = query.filter?.marketplaceId;
const owner = query.filter?.owner;
if (NFT.serieId === "0")
return {
serieData: [
{
id: NFT.id,
owner: NFT.owner,
listed: NFT.listed,
price: NFT.price,
marketplaceId: NFT.marketplaceId,
},
],
totalNft: 1,
totalListedNft: NFT.listed,
totalListedInMarketplace: NFT.listed,
totalOwnedByRequestingUser: 1,
totalOwnedListedByRequestingUser: NFT.listed,
smallestPrice: NFT.price,
};
const result = seriesData.data.filter((x) => x.serieId === NFT.serieId).map(({serieId, ...rest}) => rest);
const serieData = result.sort(
(a, b) =>
(a.isCapsule !== b.isCapsule && (a.isCapsule ? 1 : -1)) || // capsule last
b.listed - a.listed || // listed first
(marketplaceId && Number(a.marketplaceId) !== Number(b.marketplaceId) && (Number(a.marketplaceId) === marketplaceId || Number(b.marketplaceId) === marketplaceId) &&
marketplaceId === Number(a.marketplaceId) ? -1 : 1) || // marketplace id corresponding to request first
Number(a.price) - Number(b.price) // smallest price first
);
const listedNft = serieData.filter((x) => x.listed);
return {
serieData: !query.filter?.noSeriesData ? serieData : [],
totalNft: serieData.length,
totalListedNft: listedNft.length,
totalListedInMarketplace: marketplaceId
? listedNft.filter((x) => Number(x.marketplaceId) === marketplaceId)
.length
: listedNft.length,
totalOwnedByRequestingUser: owner
? serieData.filter((x) => x.owner === owner).length
: 0,
totalOwnedListedByRequestingUser: owner
? listedNft.filter((x) => x.owner === owner).length
: 0,
smallestPrice: serieData.length > 0 ? serieData[0].price : NFT.price
};
const stat = await NFTService.getStatNFT(NFT.serieId, marketplaceId, owner)
return stat
} catch (err) {
L.error({ err }, "NFTs with same serie could not have been fetched");
L.error({ err }, "NFTs stats could not have been fetched");
return null;
}
}
Expand Down
145 changes: 137 additions & 8 deletions src/api/services/gqlQueriesBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { gql } from "graphql-request";
import { convertSortString, LIMIT_MAX_PAGINATION } from "../../utils";
import { getHistoryQuery, getSeriesStatusQuery, NFTBySeriesQuery, NFTQuery, NFTsQuery, statNFTsUserQuery } from "../validators/nftValidators";
// import L from '../../common/logger';

const nodes = `
nodes {
Expand Down Expand Up @@ -29,7 +30,6 @@ export class GQLQueriesBuilder {
offset: ${query.pagination?.page && query.pagination?.limit ? (query.pagination.page - 1) * query.pagination.limit : 0}
filter:{
and:[
{ serieId: { notEqualTo: "Ternoa Xmas 2021" } }
${query.filter?.ids ? `{id: { in: ${JSON.stringify(query.filter.ids.map(x => String(x)))} }}` : ""}
${query.filter?.idsToExclude ? `{id: { notIn: ${JSON.stringify(query.filter.idsToExclude.map(x => String(x)))} }}` : ""}
${query.filter?.idsCategories ? `{id: { in: ${JSON.stringify(query.filter.idsCategories.map(x => String(x)))} }}` : ""}
Expand Down Expand Up @@ -73,7 +73,6 @@ export class GQLQueriesBuilder {
nftEntities(
filter: {
and: [
{ serieId: { notEqualTo: "Ternoa Xmas 2021" } }
{ timestampBurn: { isNull: true } }
{ id: { equalTo: "${query.id}" } }
]
Expand All @@ -92,7 +91,6 @@ export class GQLQueriesBuilder {
listed
price
marketplaceId
serieId
isCapsule
}
`;
Expand All @@ -105,11 +103,12 @@ export class GQLQueriesBuilder {
` : ""}
filter: {
and : [
{ serieId: { notEqualTo: "Ternoa Xmas 2021" } }
{ timestampBurn: { isNull: true } }
{ serieId:{ in:${JSON.stringify(query.seriesIds)} } }
${query.filter?.owner ? `{ owner: { equalTo: "${query.filter.owner}" } }` : ""}
]
}
orderBy: [IS_CAPSULE_ASC, LISTED_DESC]
)
{
totalCount
Expand All @@ -128,7 +127,6 @@ export class GQLQueriesBuilder {
nftEntities(
filter: {
and: [
{ serieId: { notEqualTo: "Ternoa Xmas 2021" } }
{ timestampBurn: { isNull: true } }
{ owner: { equalTo: "${query.id}" } }
]
Expand All @@ -143,7 +141,6 @@ export class GQLQueriesBuilder {
nftEntities(
filter: {
and: [
{ serieId: { notEqualTo: "Ternoa Xmas 2021" } }
{ timestampBurn: { isNull: true } }
${query.filter?.marketplaceId ? `{ marketplaceId: { equalTo: "${query.filter.marketplaceId}"} }` : ""}
{ owner: { equalTo: "${query.id}" } }
Expand All @@ -161,7 +158,6 @@ export class GQLQueriesBuilder {
nftEntities(
filter: {
and: [
{ serieId: { notEqualTo: "Ternoa Xmas 2021" } }
{ timestampBurn: { isNull: true } }
{ owner: { equalTo: "${query.id}" } }
{listed: { equalTo: 0}}
Expand All @@ -178,7 +174,6 @@ export class GQLQueriesBuilder {
nftEntities(
filter: {
and: [
{ serieId: { notEqualTo: "Ternoa Xmas 2021" } }
{ timestampBurn: { isNull: true } }
{ creator: { equalTo: "${query.id}" } }
]
Expand Down Expand Up @@ -276,6 +271,140 @@ export class GQLQueriesBuilder {
}
`;

countTotal = (seriesId: string) => gql`
{
nftEntities(
filter: {
and: [
{ timestampBurn: { isNull: true } }
{ serieId: { equalTo: "${seriesId}" } }
]
}
) {
totalCount
}
}
`;

countTotalListed = (seriesId: string) => gql`
{
nftEntities(
filter: {
and: [
{ timestampBurn: { isNull: true } }
{ serieId: { equalTo: "${seriesId}" } }
{ listed: { equalTo: 1} }
]
}
) {
totalCount
}
}
`;

countTotalListedInMarketplace = (seriesId: string, marketplaceId: number) => gql`
{
nftEntities(
filter: {
and: [
{ timestampBurn: { isNull: true } }
{ serieId: { equalTo: "${seriesId}" } }
{ listed: { equalTo: 1} }
{ marketplaceId: { equalTo: "${marketplaceId}" } }
]
}
) {
totalCount
}
}
`;

countTotalOwned = (seriesId: string, owner: string) => gql`
{
nftEntities(
filter: {
and: [
{ timestampBurn: { isNull: true } }
{ serieId: { equalTo: "${seriesId}" } }
{ owner: { equalTo: "${owner}" } }
]
}
) {
totalCount
}
}
`;

countTotalOwnedListed = (seriesId: string, owner: string) => gql`
{
nftEntities(
filter: {
and: [
{ timestampBurn: { isNull: true } }
{ serieId: { equalTo: "${seriesId}" } }
{ listed: { equalTo: 1} }
{ owner: { equalTo: "${owner}" } }
]
}
) {
totalCount
}
}
`;

countTotalOwnedListedInMarketplace = (seriesId: string, owner: string, marketplaceId: number) => gql`
{
nftEntities(
filter: {
and: [
{ timestampBurn: { isNull: true } }
{ serieId: { equalTo: "${seriesId}" } }
{ listed: { equalTo: 1} }
{ owner: { equalTo: "${owner}" } }
{ marketplaceId: { equalTo: "${marketplaceId}" } }
]
}
) {
totalCount
}
}
`;

countSmallestPrice = (seriesId: string, marketplaceId: number=null) => gql`
{
nftEntities(
filter: {
and: [
{ timestampBurn: { isNull: true } }
{ serieId: { equalTo: "${seriesId}" } }
{ listed: { equalTo: 1} }
${marketplaceId ? `{ marketplaceId: {equalTo: ${marketplaceId}} }` : ``}
]
}
) {
nodes{
price
}
}
}
`;

countAllListedInMarketplace = (marketplaceId: number) => gql`
{
nftEntities(
filter: {
and: [
{ timestampBurn: { isNull: true } }
{ listed: { equalTo: 1} }
{ marketplaceId: { equalTo: "${marketplaceId}" } }
]
}
) {
totalCount
}
}
`;

}

export default new GQLQueriesBuilder();
Loading

0 comments on commit 2019e14

Please sign in to comment.