chore: final solana purge
This commit is contained in:
@@ -6,24 +6,18 @@ import { asyncHandler } from "../middleware/errorHandler";
|
||||
import { prisma } from "../utils/prisma";
|
||||
|
||||
const router = Router();
|
||||
|
||||
const OVERSEER_URL = process.env.OVERSEER_URL || "";
|
||||
const OVERSEER_API_KEY = process.env.OVERSEER_API_KEY || "";
|
||||
|
||||
const overseerClient = axios.create({
|
||||
baseURL: `${OVERSEER_URL}/api/v1`,
|
||||
headers: {
|
||||
"X-Api-Key": OVERSEER_API_KEY,
|
||||
},
|
||||
headers: { "X-Api-Key": OVERSEER_API_KEY },
|
||||
});
|
||||
|
||||
// Get request costs
|
||||
router.get(
|
||||
"/costs",
|
||||
authenticate,
|
||||
asyncHandler(async (_req: AuthenticatedRequest, res) => {
|
||||
const settings = await prisma.systemSettings.findFirst();
|
||||
|
||||
res.json({
|
||||
movie: settings?.movieRequestCost || 500,
|
||||
tv: settings?.tvRequestCost || 1000,
|
||||
@@ -31,27 +25,20 @@ router.get(
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
// Get user's balance and request availability
|
||||
router.get(
|
||||
"/balance",
|
||||
authenticate,
|
||||
asyncHandler(async (req: AuthenticatedRequest, res) => {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: req.user!.id },
|
||||
});
|
||||
|
||||
if (!user?.walletAddress) {
|
||||
const user = await prisma.user.findUnique({ where: { id: req.user!.id } });
|
||||
if (!user)
|
||||
return res.json({
|
||||
hasWallet: false,
|
||||
balance: 0,
|
||||
canRequest: false,
|
||||
canRequestMovie: false,
|
||||
canRequestTV: false,
|
||||
});
|
||||
}
|
||||
|
||||
const settings = await prisma.systemSettings.findFirst();
|
||||
const dbBalance = user.totalEarned - user.totalSpent;
|
||||
|
||||
res.json({
|
||||
hasWallet: true,
|
||||
balance: dbBalance,
|
||||
@@ -64,76 +51,44 @@ router.get(
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
// Search for content
|
||||
router.get(
|
||||
"/search",
|
||||
authenticate,
|
||||
asyncHandler(async (req: AuthenticatedRequest, res) => {
|
||||
const { query } = req.query;
|
||||
|
||||
if (!query) {
|
||||
return res.status(400).json({ error: "Query required" });
|
||||
}
|
||||
|
||||
const response = await overseerClient.get("/search", {
|
||||
params: { query },
|
||||
});
|
||||
|
||||
if (!query) return res.status(400).json({ error: "Query required" });
|
||||
const response = await overseerClient.get("/search", { params: { query } });
|
||||
res.json(response.data);
|
||||
}),
|
||||
);
|
||||
|
||||
// Request content
|
||||
router.post(
|
||||
"/request",
|
||||
authenticate,
|
||||
asyncHandler(async (req: AuthenticatedRequest, res) => {
|
||||
const { mediaType, mediaId, title, seasons } = req.body;
|
||||
|
||||
if (!mediaType || !mediaId || !title) {
|
||||
if (!mediaType || !mediaId || !title)
|
||||
return res.status(400).json({ error: "Missing required fields" });
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: req.user!.id },
|
||||
});
|
||||
|
||||
if (!user?.walletAddress) {
|
||||
return res.status(400).json({ error: "Wallet required" });
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({ where: { id: req.user!.id } });
|
||||
if (!user) return res.status(400).json({ error: "User required" });
|
||||
const settings = await prisma.systemSettings.findFirst();
|
||||
|
||||
// Calculate cost
|
||||
let cost = 0;
|
||||
if (mediaType === "movie") {
|
||||
cost = settings?.movieRequestCost || 500;
|
||||
} else if (mediaType === "tv") {
|
||||
cost = settings?.tvRequestCost || 1000;
|
||||
if (seasons && seasons.length > 1) {
|
||||
cost += (seasons.length - 1) * (settings?.tvPerSeasonCost || 250);
|
||||
}
|
||||
}
|
||||
|
||||
// Check balance
|
||||
let cost =
|
||||
mediaType === "movie"
|
||||
? settings?.movieRequestCost || 500
|
||||
: settings?.tvRequestCost || 1000;
|
||||
if (mediaType === "tv" && seasons && seasons.length > 1)
|
||||
cost += (seasons.length - 1) * (settings?.tvPerSeasonCost || 250);
|
||||
const dbBalance = user.totalEarned - user.totalSpent;
|
||||
if (dbBalance < cost) {
|
||||
if (dbBalance < cost)
|
||||
return res.status(400).json({
|
||||
error: "Insufficient balance",
|
||||
required: cost,
|
||||
current: dbBalance,
|
||||
});
|
||||
}
|
||||
|
||||
// Create request in Overseer first
|
||||
const overseerRequest = await overseerClient.post("/request", {
|
||||
mediaType,
|
||||
mediaId,
|
||||
...(seasons && { seasons }),
|
||||
});
|
||||
|
||||
// Create local request record
|
||||
const request = await prisma.contentRequest.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
@@ -146,8 +101,6 @@ router.post(
|
||||
requestedAt: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
// Deduct credits immediately to prevent race condition
|
||||
const transaction = await prisma.transaction.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
@@ -158,21 +111,15 @@ router.post(
|
||||
contentTitle: title,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: {
|
||||
totalSpent: { increment: cost },
|
||||
},
|
||||
data: { totalSpent: { increment: cost } },
|
||||
});
|
||||
|
||||
// Emit real-time spend update
|
||||
io.to(`user:${user.id}`).emit("credits_spent", {
|
||||
amount: cost,
|
||||
title,
|
||||
transaction,
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
request,
|
||||
@@ -181,38 +128,23 @@ router.post(
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
// Webhook: Handle Overseer request status changes
|
||||
router.post(
|
||||
"/webhook",
|
||||
asyncHandler(async (req, res) => {
|
||||
const { request_id, status } = req.body;
|
||||
|
||||
if (!request_id || !status) {
|
||||
if (!request_id || !status)
|
||||
return res.status(400).json({ error: "Missing fields" });
|
||||
}
|
||||
|
||||
// Find local request
|
||||
const request = await prisma.contentRequest.findFirst({
|
||||
where: { overseerRequestId: request_id },
|
||||
include: { user: true },
|
||||
});
|
||||
|
||||
if (!request) {
|
||||
return res.status(404).json({ error: "Request not found" });
|
||||
}
|
||||
|
||||
// Update status
|
||||
if (!request) return res.status(404).json({ error: "Request not found" });
|
||||
const updatedRequest = await prisma.contentRequest.update({
|
||||
where: { id: request.id },
|
||||
data: { status },
|
||||
});
|
||||
|
||||
// Credits were already deducted at request time.
|
||||
// On approval: just confirm and link transaction to request.
|
||||
// On decline: refund credits back to user.
|
||||
if (status === "DECLINED") {
|
||||
const refundTransaction = await prisma.transaction.create({
|
||||
await prisma.transaction.create({
|
||||
data: {
|
||||
userId: request.userId,
|
||||
type: "ADJUSTMENT",
|
||||
@@ -221,21 +153,15 @@ router.post(
|
||||
contentTitle: request.title,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.user.update({
|
||||
where: { id: request.userId },
|
||||
data: {
|
||||
totalSpent: { decrement: request.creditsCost },
|
||||
},
|
||||
data: { totalSpent: { decrement: request.creditsCost } },
|
||||
});
|
||||
|
||||
io.to(`user:${request.userId}`).emit("bonus_received", {
|
||||
amount: request.creditsCost,
|
||||
reason: `Refunded: ${request.title}`,
|
||||
transaction: refundTransaction,
|
||||
});
|
||||
}
|
||||
|
||||
res.json({ success: true, request: updatedRequest });
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user