Repository navigation
Expand file tree
/
Copy pathadmin.ts
More file actions
220 lines (195 loc) · 6.3 KB
/
Copy pathadmin.ts
File metadata and controls
220 lines (195 loc) · 6.3 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { sql } from "../utils/db.js";
import ErrorHandler from "../utils/errorHandler.js";
import { TryCatch } from "../utils/TryCatch.js";
// Get all users with pagination
export const getAllUsers = TryCatch(async (req, res, next) => {
const { page = 1, limit = 10, role, search } = req.query;
const offset = (Number(page) - 1) * Number(limit);
let users;
if (role && search) {
const searchTerm = `%${search}%`;
users = await sql`
SELECT
user_id,
name,
email,
phone_number,
role,
subscription,
created_at
FROM users
WHERE role = ${role} AND (name ILIKE ${searchTerm} OR email ILIKE ${searchTerm})
ORDER BY created_at DESC
LIMIT ${Number(limit)} OFFSET ${offset}
`;
} else if (role) {
users = await sql`
SELECT
user_id,
name,
email,
phone_number,
role,
subscription,
created_at
FROM users
WHERE role = ${role}
ORDER BY created_at DESC
LIMIT ${Number(limit)} OFFSET ${offset}
`;
} else if (search) {
const searchTerm = `%${search}%`;
users = await sql`
SELECT
user_id,
name,
email,
phone_number,
role,
subscription,
created_at
FROM users
WHERE name ILIKE ${searchTerm} OR email ILIKE ${searchTerm}
ORDER BY created_at DESC
LIMIT ${Number(limit)} OFFSET ${offset}
`;
} else {
users = await sql`
SELECT
user_id,
name,
email,
phone_number,
role,
subscription,
created_at
FROM users
ORDER BY created_at DESC
LIMIT ${Number(limit)} OFFSET ${offset}
`;
}
// Get total count
let countResult;
if (role && search) {
const searchTerm = `%${search}%`;
countResult =
await sql`SELECT COUNT(*) as total FROM users WHERE role = ${role} AND (name ILIKE ${searchTerm} OR email ILIKE ${searchTerm})`;
} else if (role) {
countResult =
await sql`SELECT COUNT(*) as total FROM users WHERE role = ${role}`;
} else if (search) {
const searchTerm = `%${search}%`;
countResult =
await sql`SELECT COUNT(*) as total FROM users WHERE name ILIKE ${searchTerm} OR email ILIKE ${searchTerm}`;
} else {
countResult = await sql`SELECT COUNT(*) as total FROM users`;
}
const total = countResult[0].total;
res.json({
users,
pagination: {
page: Number(page),
limit: Number(limit),
total,
pages: Math.ceil(total / Number(limit)),
},
});
});
// Get user by ID
export const getUserById = TryCatch(async (req, res, next) => {
const { userId } = req.params;
const users = await sql`
SELECT
user_id,
name,
email,
phone_number,
role,
bio,
profile_pic,
subscription,
created_at
FROM users
WHERE user_id = ${userId}
`;
if (users.length === 0) {
throw new ErrorHandler(404, "User not found");
}
res.json(users[0]);
});
// Delete user
export const deleteUser = TryCatch(async (req, res, next) => {
const { userId } = req.params;
const user = await sql`SELECT user_id FROM users WHERE user_id = ${userId}`;
if (user.length === 0) {
throw new ErrorHandler(404, "User not found");
}
// Delete user skills first (foreign key)
await sql`DELETE FROM user_skills WHERE user_id = ${userId}`;
// Delete user
await sql`DELETE FROM users WHERE user_id = ${userId}`;
res.json({
message: "User deleted successfully",
userId,
});
});
// Update user
export const updateUserAdmin = TryCatch(async (req, res, next) => {
const { userId } = req.params;
const { name, email, phone_number, subscription } = req.body;
const user = await sql`SELECT user_id FROM users WHERE user_id = ${userId}`;
if (user.length === 0) {
throw new ErrorHandler(404, "User not found");
}
const updated = await sql`
UPDATE users
SET
name = COALESCE(${name || null}, name),
email = COALESCE(${email || null}, email),
phone_number = COALESCE(${phone_number || null}, phone_number),
subscription = COALESCE(${subscription || null}, subscription)
WHERE user_id = ${userId}
RETURNING user_id, name, email, phone_number, role, subscription
`;
res.json({
message: "User updated successfully",
user: updated[0],
});
});
// Get user statistics
export const getUserStats = TryCatch(async (req, res, next) => {
const totalUsers = await sql`SELECT COUNT(*) as count FROM users`;
const recruiterCount = await sql`SELECT COUNT(*) as count FROM users WHERE role = 'recruiter'`;
const jobseekerCount = await sql`SELECT COUNT(*) as count FROM users WHERE role = 'jobseeker'`;
const subscribedUsers = await sql`SELECT COUNT(*) as count FROM users WHERE subscription IS NOT NULL AND subscription > NOW()`;
return res.json({
totalUsers: totalUsers[0]?.count || 0,
recruiters: recruiterCount[0]?.count || 0,
jobseekers: jobseekerCount[0]?.count || 0,
subscribedUsers: subscribedUsers[0]?.count || 0,
});
});
// Get users by role
export const getUsersByRole = TryCatch(async (req, res, next) => {
const { role } = req.params;
if (role !== "recruiter" && role !== "jobseeker") {
throw new ErrorHandler(400, "Invalid role");
}
const users = await sql`
SELECT
user_id,
name,
email,
phone_number,
subscription,
created_at
FROM users
WHERE role = ${role}
ORDER BY created_at DESC
`;
res.json({
role,
count: users.length,
users,
});
});