Skip to content

Commit

Permalink
✨ New endpoint to expose the global product info
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Oct 23, 2024
1 parent cb14fed commit 41dd947
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 10 deletions.
3 changes: 1 addition & 2 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
"maxAllowedComplexity": 16
}
},
"noUselessTernary": "error",
"useSimplifiedLogicExpression": "error"
"noUselessTernary": "error"
},
"correctness": {
"useHookAtTopLevel": "error",
Expand Down
6 changes: 0 additions & 6 deletions packages/ponder/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
import { ponder } from "@/generated";
import { graphql } from "@ponder/core";

// todo: api key middleware

// This is the entry point for the Graphql api
ponder.use("/graphql", graphql());
5 changes: 5 additions & 0 deletions packages/ponder/src/api/members.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ ponder.post("/members/:productAdmin", async (ctx) => {
.innerJoin(Product, eq(ProductAdministrator.productId, Product.id))
.where(eq(ProductAdministrator.user, wallet));

// If no product found, early exit
if (!productIds.length) {
return ctx.json({ totalResult: 0, members: [], users: [] });
}

const { whereClauses, havingClauses } = getFilterClauses({
tables: ctx.tables,
filter,
Expand Down
83 changes: 81 additions & 2 deletions packages/ponder/src/api/products.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ponder } from "@/generated";
import { eq } from "@ponder/core";
import { type Hex, isHex } from "viem";
import { eq, inArray } from "@ponder/core";
import { type Hex, isHex, keccak256, toHex } from "viem";

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Unreachable code error
Expand Down Expand Up @@ -71,3 +71,82 @@ ponder.get("/products/:id/banks", async (ctx) => {
// Return the result as json
return ctx.json(administrators);
});

/**
* Get the overall product info
*/
ponder.get("/products/info", async ({ req, db, tables, json }) => {
// Extract the product id
const domain = req.query("domain");
let productId = req.query("id") as Hex | undefined;

// If no id provided, recompute it from the domain
if (!productId && domain) {
productId = keccak256(toHex(domain));
}

if (!productId || !isHex(productId)) {
return json({ msg: "Invalid product id", productId, domain });
}

// Get the product from the db
const products = await db
.select()
.from(tables.Product)
.where(eq(tables.Product.id, BigInt(productId)));
const product = products?.[0];

// If not found, early exit
if (!product) {
return json({ msg: "Product not found", productId, domain });
}

// Get all the admninistrators
const administrators = await db
.select()
.from(tables.ProductAdministrator)
.where(eq(tables.ProductAdministrator.productId, BigInt(productId)));

// Get all the banks
const banks = await db
.select()
.from(tables.BankingContract)
.where(eq(tables.BankingContract.productId, BigInt(productId)));

// Get the interaction contracts
const interactionContracts = await db
.select()
.from(tables.ProductInteractionContract)
.where(
eq(tables.ProductInteractionContract.productId, BigInt(productId))
);

// Get the campaigns
const campaigns = await db
.select()
.from(tables.Campaign)
.where(eq(tables.Campaign.productId, BigInt(productId)));

// Get the campaigns tats
const campaignStats = campaigns.length
? await db
.select()
.from(tables.ReferralCampaignStats)
.where(
inArray(
tables.ReferralCampaignStats.campaignId,
campaigns.map((c) => c.id)
)
)
: [];

// Return all the data related to the product
return json({
product,
banks,
interactionContracts,
administrators,
campaigns,
campaignStats,
});
});

0 comments on commit 41dd947

Please sign in to comment.