Skip to content

Commit a2d6c25

Browse files
committed
Feature: added fetch pending transactions via api route
1 parent 6e2257c commit a2d6c25

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { cors, addCorsCacheBustingHeaders } from "@/lib/cors";
2+
import { verifyJwt } from "@/lib/verifyJwt";
3+
import { createCaller } from "@/server/api/root";
4+
import { db } from "@/server/db";
5+
import type { NextApiRequest, NextApiResponse } from "next";
6+
7+
export default async function handler(
8+
req: NextApiRequest,
9+
res: NextApiResponse,
10+
) {
11+
// Add cache-busting headers for CORS
12+
addCorsCacheBustingHeaders(res);
13+
14+
await cors(req, res);
15+
if (req.method === "OPTIONS") {
16+
return res.status(200).end();
17+
}
18+
19+
if (req.method !== "GET") {
20+
return res.status(405).json({ error: "Method Not Allowed" });
21+
}
22+
23+
const authHeader = req.headers.authorization;
24+
const token = authHeader?.startsWith("Bearer ") ? authHeader.slice(7) : null;
25+
26+
if (!token) {
27+
return res.status(401).json({ error: "Unauthorized - Missing token" });
28+
}
29+
30+
const payload = verifyJwt(token);
31+
if (!payload) {
32+
return res.status(401).json({ error: "Invalid or expired token" });
33+
}
34+
35+
const session = {
36+
user: { id: payload.address },
37+
expires: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
38+
} as const;
39+
40+
const { walletId, address } = req.query;
41+
42+
if (typeof address !== "string") {
43+
return res.status(400).json({ error: "Invalid address parameter" });
44+
}
45+
if (payload.address !== address) {
46+
return res.status(403).json({ error: "Address mismatch" });
47+
}
48+
if (typeof walletId !== "string") {
49+
return res.status(400).json({ error: "Invalid walletId parameter" });
50+
}
51+
52+
try {
53+
const caller = createCaller({ db, session });
54+
55+
const wallet = await caller.wallet.getWallet({ walletId, address });
56+
if (!wallet) {
57+
return res.status(404).json({ error: "Wallet not found" });
58+
}
59+
60+
const pendingTransactions =
61+
await caller.transaction.getPendingTransactions({ walletId });
62+
63+
return res.status(200).json(pendingTransactions);
64+
} catch (error) {
65+
console.error("Error in pendingTransactions handler", {
66+
message: (error as Error)?.message,
67+
stack: (error as Error)?.stack,
68+
});
69+
return res.status(500).json({ error: "Internal Server Error" });
70+
}
71+
}
72+

0 commit comments

Comments
 (0)