forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversioningControllers.ts
More file actions
125 lines (100 loc) · 4.06 KB
/
Copy pathversioningControllers.ts
File metadata and controls
125 lines (100 loc) · 4.06 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { Request, Response } from "express";
import connectDb from "../db/connectDb";
import Prompt from "../models/Prompt";
import PromptVersion from "../models/PromptVersion";
import Purchase from "../models/Purchase";
import User from "../models/User";
export const PostPromptUpdate = async (req: Request, res: Response): Promise<Response> => {
try {
await connectDb();
const { promptId, walletAddress, content, changeNote } = req.body;
if (!promptId || !walletAddress || !content) {
return res.status(400).json({ error: "promptId, walletAddress, and content are required." });
}
const user = await User.findOne({ walletAddress: walletAddress.toLowerCase() });
if (!user) return res.status(404).json({ error: "User not found." });
const prompt = await Prompt.findOne({ _id: promptId, owner: user._id });
if (!prompt) return res.status(403).json({ error: "Prompt not found or not owned by this wallet." });
const nextVersion = (prompt.currentVersionIndex ?? 1) + 1;
await PromptVersion.create({
promptId: String(prompt._id),
versionIndex: nextVersion,
content,
changeNote: changeNote ?? "",
createdBy: walletAddress.toLowerCase(),
});
await Prompt.findByIdAndUpdate(prompt._id, { currentVersionIndex: nextVersion });
return res.status(201).json({ message: "Version posted.", versionIndex: nextVersion });
} catch (err) {
return res.status(500).json({ error: (err as Error).message });
}
};
export const GetPromptVersions = async (req: Request, res: Response): Promise<Response> => {
try {
await connectDb();
const { promptId } = req.params;
if (!promptId) return res.status(400).json({ error: "promptId is required." });
const versions = await PromptVersion.find({ promptId })
.sort({ versionIndex: -1 })
.select("-content");
return res.json(versions);
} catch (err) {
return res.status(500).json({ error: (err as Error).message });
}
};
export const RecordPurchase = async (req: Request, res: Response): Promise<Response> => {
try {
await connectDb();
const { promptId, buyerWallet, txHash } = req.body;
if (!promptId || !buyerWallet) {
return res.status(400).json({ error: "promptId and buyerWallet are required." });
}
const prompt = await Prompt.findById(promptId);
if (!prompt) return res.status(404).json({ error: "Prompt not found." });
const existing = await Purchase.findOne({
promptId,
buyerWallet: buyerWallet.toLowerCase(),
});
if (existing) {
return res.status(200).json({ message: "Already purchased.", versionIndex: existing.versionIndex });
}
const purchase = await Purchase.create({
promptId,
buyerWallet: buyerWallet.toLowerCase(),
versionIndex: prompt.currentVersionIndex ?? 1,
txHash: txHash ?? "",
});
return res.status(201).json({ message: "Purchase recorded.", versionIndex: purchase.versionIndex });
} catch (err) {
return res.status(500).json({ error: (err as Error).message });
}
};
export const GetBuyerVersion = async (req: Request, res: Response): Promise<Response> => {
try {
await connectDb();
const { promptId, buyerWallet } = req.query;
if (!promptId || !buyerWallet) {
return res.status(400).json({ error: "promptId and buyerWallet query params are required." });
}
const purchase = await Purchase.findOne({
promptId: String(promptId),
buyerWallet: String(buyerWallet).toLowerCase(),
});
if (!purchase) {
return res.status(404).json({ error: "No purchase record found." });
}
const version = await PromptVersion.findOne({
promptId: String(promptId),
versionIndex: purchase.versionIndex,
});
const prompt = await Prompt.findById(promptId).lean();
return res.json({
versionIndex: purchase.versionIndex,
changeNote: version?.changeNote ?? "",
content: version?.content ?? (prompt as any)?.content ?? null,
purchasedAt: purchase.createdAt,
});
} catch (err) {
return res.status(500).json({ error: (err as Error).message });
}
};