Files
coop/backend/src/routes/users.ts
T

141 lines
3.3 KiB
TypeScript

import { Router } from "express";
import { type AuthenticatedRequest, authenticate } from "../middleware/auth";
import { asyncHandler } from "../middleware/errorHandler";
import { prisma } from "../utils/prisma";
const router = Router();
router.get(
"/me",
authenticate,
asyncHandler(async (req: AuthenticatedRequest, res) => {
const user = await prisma.user.findUnique({
where: { id: req.user!.id },
select: {
id: true,
plexId: true,
plexUsername: true,
email: true,
isAdmin: true,
totalEarned: true,
totalSpent: true,
watchTimeMinutes: true,
createdAt: true,
},
});
if (!user) return res.status(404).json({ error: "User not found" });
res.json(user);
}),
);
router.put(
"/me",
authenticate,
asyncHandler(async (req: AuthenticatedRequest, res) => {
const { email } = req.body;
const user = await prisma.user.update({
where: { id: req.user!.id },
data: { email },
select: { id: true, plexUsername: true, email: true },
});
res.json(user);
}),
);
router.get(
"/me/watch-history",
authenticate,
asyncHandler(async (req: AuthenticatedRequest, res) => {
const { page = "1", limit = "20" } = req.query;
const pageNum = parseInt(page as string);
const limitNum = Math.min(parseInt(limit as string), 50);
const skip = (pageNum - 1) * limitNum;
const [events, total] = await Promise.all([
prisma.watchEvent.findMany({
where: { userId: req.user!.id },
orderBy: { watchedAt: "desc" },
skip,
take: limitNum,
select: {
id: true,
contentType: true,
title: true,
grandparentTitle: true,
duration: true,
percentComplete: true,
creditsEarned: true,
isProcessed: true,
watchedAt: true,
},
}),
prisma.watchEvent.count({ where: { userId: req.user!.id } }),
]);
res.json({
events,
pagination: {
page: pageNum,
limit: limitNum,
total,
totalPages: Math.ceil(total / limitNum),
},
});
}),
);
router.get(
"/me/requests",
authenticate,
asyncHandler(async (req: AuthenticatedRequest, res) => {
const { page = "1", limit = "20", status } = req.query;
const pageNum = parseInt(page as string);
const limitNum = Math.min(parseInt(limit as string), 50);
const skip = (pageNum - 1) * limitNum;
const where: any = { userId: req.user!.id };
if (status) where.status = status;
const [requests, total] = await Promise.all([
prisma.contentRequest.findMany({
where,
orderBy: { requestedAt: "desc" },
skip,
take: limitNum,
}),
prisma.contentRequest.count({ where }),
]);
res.json({
requests,
pagination: {
page: pageNum,
limit: limitNum,
total,
totalPages: Math.ceil(total / limitNum),
},
});
}),
);
router.get(
"/leaderboard",
authenticate,
asyncHandler(async (_req: AuthenticatedRequest, res) => {
const topEarners = await prisma.user.findMany({
orderBy: { totalEarned: "desc" },
take: 10,
select: {
id: true,
plexUsername: true,
totalEarned: true,
watchTimeMinutes: true,
},
});
const topWatchers = await prisma.user.findMany({
orderBy: { watchTimeMinutes: "desc" },
take: 10,
select: {
id: true,
plexUsername: true,
totalEarned: true,
watchTimeMinutes: true,
},
});
res.json({ topEarners, topWatchers });
}),
);
export { router as userRouter };