@@ -28,36 +28,149 @@ const retrieveUserProfile = async (req, res, next) => {
28
28
} ;
29
29
30
30
/**
31
- * Fetches user profile data based on the provided user ID and Reset Token.
31
+ * Processes user actions such as addquota, removequota, updaterole, banuser and updatetoken
32
32
*
33
33
* @param {Object } req - Express request object.
34
34
* @param {Object } res - Express response object.
35
35
* @param {Function } next - Express next middleware function.
36
- * @returns {Object } - User profile data .
36
+ * @returns {Object } - Response with action results or errors .
37
37
*/
38
- const updateUserToken = async ( req , res , next ) => {
38
+ const processUserAction = async ( req , res , next ) => {
39
39
const key = req . headers . key ;
40
+
40
41
// Check for valid access key in headers
41
42
if ( ! key || key !== process . env . ACCESS_KEY ) {
42
43
return res . status ( 401 ) . json ( {
43
44
message : 'Unauthorized' ,
44
45
} ) ;
45
46
}
46
- const user = await Users . findById ( req . params . id ) ;
47
- if ( ! user ) {
48
- return res . status ( 404 ) . json ( { message : 'User not found' } ) ; // User not found
49
- }
50
47
51
- // Update user's token in the database
52
- await Users . updateOne (
53
- { _id : { $eq : req . params . id } } ,
54
- { $set : { token : generateToken ( req . params . id , process . env . HMAC_KEY ) } } ,
55
- ) ;
48
+ const userId = req . params . id ;
49
+ const { action, amount, reason, executor, expiry } = req . body ; // Extract fields from the request body
56
50
57
- // This will return the data however it won't be the latest one after updating the token
58
- return res . status ( 200 ) . json ( {
59
- message : 'Token reset successfully.' ,
60
- } ) ;
51
+ try {
52
+ // Fetch user by ID
53
+ const user = await Users . findById ( userId ) ;
54
+ if ( ! user ) {
55
+ return res . status ( 404 ) . json ( { message : 'User not found' } ) ; // User not found
56
+ }
57
+
58
+ let updatedUser ;
59
+
60
+ // Handle different actions
61
+ switch ( action ) {
62
+ case 'addquota' :
63
+ if ( ! amount || amount <= 0 ) {
64
+ return res . status ( 400 ) . json ( { message : 'Invalid quota amount' } ) ;
65
+ }
66
+ user . req_quota = ( user . req_quota || 0 ) + Number ( amount ) ;
67
+
68
+ // Update status history
69
+ user . status_history . push ( {
70
+ _id : user . status_history . length + 1 ,
71
+ timestamp : new Date ( ) ,
72
+ reason : reason || 'Quota added' ,
73
+ value : `+${ amount } quota` ,
74
+ executor : executor || 'system' ,
75
+ } ) ;
76
+
77
+ updatedUser = await user . save ( ) ;
78
+ break ;
79
+
80
+ case 'removequota' :
81
+ if ( ! amount || amount <= 0 ) {
82
+ return res . status ( 400 ) . json ( { message : 'Invalid quota amount' } ) ;
83
+ }
84
+ if ( ( user . req_quota || 0 ) < amount ) {
85
+ return res . status ( 400 ) . json ( { message : 'Insufficient quota' } ) ;
86
+ }
87
+ user . req_quota = ( user . req_quota || 0 ) - Number ( amount ) ;
88
+
89
+ // Update status history
90
+ user . status_history . push ( {
91
+ _id : user . status_history . length + 1 ,
92
+ timestamp : new Date ( ) ,
93
+ reason : reason || 'Quota removed' ,
94
+ value : `-${ amount } quota` ,
95
+ executor : executor || 'system' ,
96
+ } ) ;
97
+
98
+ updatedUser = await user . save ( ) ;
99
+ break ;
100
+
101
+ case 'ban' :
102
+ if ( ! reason ) {
103
+ return res . status ( 400 ) . json ( { message : 'Ban reason is required' } ) ;
104
+ }
105
+ user . banned = true ;
106
+
107
+ // Update status history
108
+ user . status_history . push ( {
109
+ _id : user . status_history . length + 1 ,
110
+ timestamp : new Date ( ) ,
111
+ expiry : expiry || null ,
112
+ reason,
113
+ isBanned : true ,
114
+ executor : executor || 'system' ,
115
+ } ) ;
116
+
117
+ updatedUser = await user . save ( ) ;
118
+ break ;
119
+ case 'unban' :
120
+ if ( ! reason ) {
121
+ return res . status ( 400 ) . json ( { message : 'Unban reason is required' } ) ;
122
+ }
123
+ user . banned = false ;
124
+
125
+ // Update status history
126
+ user . status_history . push ( {
127
+ _id : user . status_history . length + 1 ,
128
+ timestamp : new Date ( ) ,
129
+ expiry : expiry || null ,
130
+ reason,
131
+ isBanned : false ,
132
+ executor : executor || 'system' ,
133
+ } ) ;
134
+
135
+ updatedUser = await user . save ( ) ;
136
+ break ;
137
+
138
+ case 'updatetoken' :
139
+ if ( ! reason ) {
140
+ return res . status ( 400 ) . json ( { message : 'Token update reason is required' } ) ;
141
+ }
142
+ const token = generateToken ( userId , process . env . HMAC_KEY ) ;
143
+ user . token = token ;
144
+
145
+ // Update status history
146
+ user . status_history . push ( {
147
+ _id : user . status_history . length + 1 ,
148
+ timestamp : new Date ( ) ,
149
+ reason : reason || 'Token updated' ,
150
+ value : token ,
151
+ executor : executor || 'system' ,
152
+ } ) ;
153
+
154
+ updatedUser = await user . save ( ) ;
155
+ break ;
156
+
157
+ default :
158
+ return res . status ( 400 ) . json ( { message : `Invalid action: ${ action } ` } ) ;
159
+ }
160
+
161
+ // Respond with updated user data
162
+ return res . status ( 200 ) . json ( {
163
+ success : true ,
164
+ message : `${ action } executed successfully` ,
165
+ user : updatedUser ,
166
+ } ) ;
167
+ } catch ( error ) {
168
+ // Handle server errors
169
+ return res . status ( 500 ) . json ( {
170
+ message : 'An error occurred while processing the action' ,
171
+ error : error . message ,
172
+ } ) ;
173
+ }
61
174
} ;
62
175
63
176
/**
@@ -178,4 +291,4 @@ const getUser = async (req, res, next) => {
178
291
}
179
292
} ;
180
293
181
- export { retrieveUserProfile , updateUserToken , processUserSessionAndUpdate , getUser } ;
294
+ export { retrieveUserProfile , processUserAction , processUserSessionAndUpdate , getUser } ;
0 commit comments