Spaces:
Runtime error
Runtime error
| ; | |
| var __importDefault = (this && this.__importDefault) || function (mod) { | |
| return (mod && mod.__esModule) ? mod : { "default": mod }; | |
| }; | |
| Object.defineProperty(exports, "__esModule", { value: true }); | |
| exports.getAdminAnalytics = exports.getUserAnalytics = void 0; | |
| const client_1 = __importDefault(require("../../prisma/client")); | |
| const getUserAnalytics = async (req, res, next) => { | |
| var _a; | |
| const userId = (_a = req.user) === null || _a === void 0 ? void 0 : _a.id; | |
| try { | |
| const user = await client_1.default.user.findUnique({ | |
| where: { id: userId }, | |
| select: { | |
| xp: true, | |
| streak: true, | |
| _count: { | |
| select: { | |
| projects: true, | |
| submissions: true, | |
| achievements: true | |
| } | |
| } | |
| } | |
| }); | |
| const projectCompletion = await client_1.default.userProject.groupBy({ | |
| by: ["status"], | |
| where: { userId }, | |
| _count: true | |
| }); | |
| res.json({ | |
| success: true, | |
| data: { | |
| stats: user, | |
| projectCompletion | |
| } | |
| }); | |
| } | |
| catch (error) { | |
| next(error); | |
| } | |
| }; | |
| exports.getUserAnalytics = getUserAnalytics; | |
| const getAdminAnalytics = async (req, res, next) => { | |
| var _a; | |
| const userId = (_a = req.user) === null || _a === void 0 ? void 0 : _a.id; | |
| try { | |
| const user = await client_1.default.user.findUnique({ where: { id: userId } }); | |
| if ((user === null || user === void 0 ? void 0 : user.role) !== "ADMIN") { | |
| return res.status(403).json({ success: false, message: "Forbidden" }); | |
| } | |
| const totalUsers = await client_1.default.user.count(); | |
| const totalProjects = await client_1.default.project.count(); | |
| const totalSubmissions = await client_1.default.submission.count(); | |
| const popularProjects = await client_1.default.project.findMany({ | |
| orderBy: { submissionCount: "desc" }, | |
| take: 5, | |
| select: { title: true, submissionCount: true } | |
| }); | |
| res.json({ | |
| success: true, | |
| data: { | |
| totalUsers, | |
| totalProjects, | |
| totalSubmissions, | |
| popularProjects | |
| } | |
| }); | |
| } | |
| catch (error) { | |
| next(error); | |
| } | |
| }; | |
| exports.getAdminAnalytics = getAdminAnalytics; | |