Repository navigation
Expand file tree
/
Copy pathadmin.ts
More file actions
78 lines (70 loc) · 2.67 KB
/
Copy pathadmin.ts
File metadata and controls
78 lines (70 loc) · 2.67 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
import axios from "axios";
import { sql } from "../utils/db.js";
import ErrorHandler from "../utils/errorHandler.js";
import { TryCatch } from "../utils/TryCatch.js";
// Get payment stats
export const getPaymentStats = TryCatch(async (req, res, next) => {
try {
// Fetch stats from User Service
const userServiceUrl = process.env.USER_SERVICE_URL || "http://localhost:5002";
const statsResponse = await axios.get(`${userServiceUrl}/api/admin/users/stats`, {
headers: {
"x-admin-key": process.env.ADMIN_SECRET_KEY,
},
});
const subscribedUsers = statsResponse.data.subscribedUsers || 0;
const revenue = subscribedUsers * 119;
res.json({
totalSubscriptions: subscribedUsers,
activeSubscriptions: subscribedUsers,
cancelledSubscriptions: 0,
totalRevenue: revenue,
});
} catch (error) {
console.error("Error fetching payment stats:", error);
throw new ErrorHandler(500, "Failed to fetch payment statistics");
}
});
// Get all subscriptions
export const getAllSubscriptions = TryCatch(async (req, res, next) => {
const { page = 1, limit = 10 } = req.query;
try {
// Fetch all users from User Service
const userServiceUrl = process.env.USER_SERVICE_URL || "http://localhost:5002";
const usersResponse = await axios.get(
`${userServiceUrl}/api/admin/users/all?page=${page}&limit=${limit}`,
{
headers: {
"x-admin-key": process.env.ADMIN_SECRET_KEY,
},
}
);
const allUsers = usersResponse.data.users || [];
const pagination = usersResponse.data.pagination || {};
// Filter users with subscriptions
const subscriptions = allUsers
.filter((u: any) => u.subscription)
.map((u: any) => ({
user_id: u.user_id,
user_name: u.name,
email: u.email,
plan_type: "Premium",
amount: 119,
status: new Date(u.subscription) > new Date() ? "active" : "cancelled",
created_at: new Date(),
expires_at: u.subscription,
}));
res.json({
subscriptions,
pagination: {
page: Number(page),
limit: Number(limit),
total: pagination.total,
pages: pagination.pages,
},
});
} catch (error) {
console.error("Error fetching subscriptions:", error);
throw new ErrorHandler(500, "Failed to fetch subscriptions");
}
});