forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhookControllers.ts
More file actions
89 lines (73 loc) · 2.73 KB
/
Copy pathwebhookControllers.ts
File metadata and controls
89 lines (73 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Request, Response } from "express";
import { randomBytes } from "crypto";
import connectDb from "../db/connectDb";
import WebhookSubscription from "../models/WebhookSubscription";
import { ALLOWED_EVENTS } from "../services/webhookDispatcher";
export const RegisterWebhook = async (req: Request, res: Response): Promise<Response> => {
try {
await connectDb();
const { walletAddress, url, events } = req.body;
if (!walletAddress || !url) {
return res.status(400).json({ error: "walletAddress and url are required." });
}
try {
new URL(url);
} catch {
return res.status(400).json({ error: "url must be a valid URL." });
}
const secret = randomBytes(32).toString("hex");
const resolvedEvents = Array.isArray(events)
? events.filter((e: string) => ALLOWED_EVENTS.includes(e as any))
: ["PromptPurchased"];
const existing = await WebhookSubscription.findOne({
walletAddress: walletAddress.toLowerCase(),
});
if (existing) {
existing.url = url;
existing.events = resolvedEvents;
existing.active = true;
existing.failureCount = 0;
await existing.save();
return res.status(200).json({ message: "Webhook updated.", id: existing._id, secret });
}
const sub = new WebhookSubscription({
walletAddress: walletAddress.toLowerCase(),
url,
secret,
events: resolvedEvents,
});
await sub.save();
return res.status(201).json({ message: "Webhook registered.", id: sub._id, secret });
} catch (err) {
return res.status(500).json({ error: (err as Error).message });
}
};
export const GetWebhook = async (req: Request, res: Response): Promise<Response> => {
try {
await connectDb();
const { walletAddress } = req.query;
if (!walletAddress) {
return res.status(400).json({ error: "walletAddress query param is required." });
}
const sub = await WebhookSubscription.findOne({
walletAddress: String(walletAddress).toLowerCase(),
}).select("-secret");
if (!sub) return res.status(404).json({ error: "No webhook registered for this wallet." });
return res.json(sub);
} catch (err) {
return res.status(500).json({ error: (err as Error).message });
}
};
export const DeleteWebhook = async (req: Request, res: Response): Promise<Response> => {
try {
await connectDb();
const { walletAddress } = req.body;
if (!walletAddress) {
return res.status(400).json({ error: "walletAddress is required." });
}
await WebhookSubscription.deleteOne({ walletAddress: walletAddress.toLowerCase() });
return res.status(200).json({ message: "Webhook removed." });
} catch (err) {
return res.status(500).json({ error: (err as Error).message });
}
};