1- import { HttpErrors , handleHttpError } from "@/helpers/HttpError .ts" ;
2- import { gatewayResponse , logger } from "@/helpers/index .ts" ;
1+ import { HttpErrors , HttpStatusCode } from "@/helpers/Http .ts" ;
2+ import { apiResponse } from "@/helpers/response .ts" ;
33import { asyncHandler } from "@/helpers/request.ts" ;
44import { accounts , workspaceMemberships , workspaces } from "@/schema.ts" ;
55import { AUDIT_ACTIONS , ENTITY_TYPES , auditHelpers , createAuditLog } from "@/services/auditLog.ts" ;
@@ -17,8 +17,6 @@ export const listAllAccounts = asyncHandler(async (req: Request, res: Response):
1717 const limit = parseInt ( req . query . limit as string ) || 20 ;
1818 const offset = ( page - 1 ) * limit ;
1919
20- logger . info ( { msg : `SuperAdmin listing all accounts - page: ${ page } , limit: ${ limit } ` } ) ;
21-
2220 // Get paginated accounts with total count in single query
2321 const accountsWithCount = await db
2422 . select ( {
@@ -40,8 +38,8 @@ export const listAllAccounts = asyncHandler(async (req: Request, res: Response):
4038 // Clean data by removing totalCount from individual records
4139 const accountsList = accountsWithCount . map ( ( { totalCount : _ , ...account } ) => account ) ;
4240
43- const response = gatewayResponse ( ) . success (
44- 200 ,
41+ const response = apiResponse . success (
42+ HttpStatusCode . OK ,
4543 {
4644 accounts : accountsList ,
4745 pagination : {
@@ -66,18 +64,17 @@ export const createAccountForUser = asyncHandler(async (req: Request, res: Respo
6664 const { accountId } = req ;
6765
6866 if ( ! accountId ) {
69- handleHttpError ( HttpErrors . Unauthorized ( ) , res , gatewayResponse ) ;
67+ const response = apiResponse . error ( HttpErrors . Unauthorized ( ) ) ;
68+ res . status ( response . code ) . send ( response ) ;
7069 return ;
7170 }
7271
73- logger . info ( { msg : `SuperAdmin creating account for ${ email } ` } ) ;
74-
7572 // Check if account already exists
7673 const [ existingAccount ] = await db . select ( ) . from ( accounts ) . where ( eq ( accounts . email , email ) ) . limit ( 1 ) ;
7774
7875 if ( existingAccount ) {
79- logger . error ( { email } , `Account with email ${ email } already exists` ) ;
80- handleHttpError ( HttpErrors . BadRequest ( "Unable to create account with provided information" ) , res , gatewayResponse ) ;
76+ const response = apiResponse . error ( HttpErrors . BadRequest ( "Unable to create account with provided information" ) ) ;
77+ res . status ( response . code ) . send ( response ) ;
8178 return ;
8279 }
8380
@@ -121,11 +118,12 @@ export const createAccountForUser = asyncHandler(async (req: Request, res: Respo
121118 } ) ;
122119
123120 if ( ! newAccount ) {
124- handleHttpError ( HttpErrors . DatabaseError ( "Failed to create account" ) , res , gatewayResponse ) ;
121+ const response = apiResponse . error ( HttpErrors . DatabaseError ( "Failed to create account" ) ) ;
122+ res . status ( response . code ) . send ( response ) ;
125123 return ;
126124 }
127125
128- const response = gatewayResponse ( ) . success ( 201 , { account : newAccount } , "Account created successfully" ) ;
126+ const response = apiResponse . success ( HttpStatusCode . CREATED , { account : newAccount } , "Account created successfully" ) ;
129127
130128 res . status ( response . code ) . send ( response ) ;
131129} ) ;
@@ -139,24 +137,25 @@ export const updateAccountRole = asyncHandler(async (req: Request, res: Response
139137 const { accountId } = req ;
140138
141139 if ( ! targetAccountId ) {
142- handleHttpError ( HttpErrors . MissingParameter ( "Account ID" ) , res , gatewayResponse ) ;
140+ const response = apiResponse . error ( HttpErrors . MissingParameter ( "Account ID" ) ) ;
141+ res . status ( response . code ) . send ( response ) ;
143142 return ;
144143 }
145144
146145 if ( ! accountId ) {
147- handleHttpError ( HttpErrors . Unauthorized ( ) , res , gatewayResponse ) ;
146+ const response = apiResponse . error ( HttpErrors . Unauthorized ( ) ) ;
147+ res . status ( response . code ) . send ( response ) ;
148148 return ;
149149 }
150150
151151 const { isSuperAdmin } = req . body ;
152152
153153 if ( typeof isSuperAdmin !== "boolean" ) {
154- handleHttpError ( HttpErrors . ValidationFailed ( "isSuperAdmin must be a boolean value" ) , res , gatewayResponse ) ;
154+ const response = apiResponse . error ( HttpErrors . ValidationFailed ( "isSuperAdmin must be a boolean value" ) ) ;
155+ res . status ( response . code ) . send ( response ) ;
155156 return ;
156157 }
157158
158- logger . info ( { msg : `SuperAdmin updating account ${ targetAccountId } role to isSuperAdmin: ${ isSuperAdmin } ` } ) ;
159-
160159 // Get current account to track the change (outside transaction)
161160 const [ currentAccount ] = await db
162161 . select ( { isSuperAdmin : accounts . isSuperAdmin } )
@@ -165,7 +164,8 @@ export const updateAccountRole = asyncHandler(async (req: Request, res: Response
165164 . limit ( 1 ) ;
166165
167166 if ( ! currentAccount ) {
168- handleHttpError ( HttpErrors . AccountNotFound ( ) , res , gatewayResponse ) ;
167+ const response = apiResponse . error ( HttpErrors . NotFound ( "Account" ) ) ;
168+ res . status ( response . code ) . send ( response ) ;
169169 return ;
170170 }
171171
@@ -195,12 +195,13 @@ export const updateAccountRole = asyncHandler(async (req: Request, res: Response
195195 } ) ;
196196
197197 if ( ! updatedAccount ) {
198- handleHttpError ( HttpErrors . AccountNotFound ( ) , res , gatewayResponse ) ;
198+ const response = apiResponse . error ( HttpErrors . NotFound ( "Account" ) ) ;
199+ res . status ( response . code ) . send ( response ) ;
199200 return ;
200201 }
201202
202- const response = gatewayResponse ( ) . success (
203- 200 ,
203+ const response = apiResponse . success (
204+ HttpStatusCode . OK ,
204205 { account : updatedAccount } ,
205206 `Account role updated to SuperAdmin: ${ isSuperAdmin } `
206207 ) ;
@@ -217,29 +218,28 @@ export const updateAccountStatus = asyncHandler(async (req: Request, res: Respon
217218 const { accountId } = req ;
218219
219220 if ( ! targetAccountId ) {
220- handleHttpError ( HttpErrors . MissingParameter ( "Account ID" ) , res , gatewayResponse ) ;
221+ const response = apiResponse . error ( HttpErrors . MissingParameter ( "Account ID" ) ) ;
222+ res . status ( response . code ) . send ( response ) ;
221223 return ;
222224 }
223225
224226 if ( ! accountId ) {
225- handleHttpError ( HttpErrors . Unauthorized ( ) , res , gatewayResponse ) ;
227+ const response = apiResponse . error ( HttpErrors . Unauthorized ( ) ) ;
228+ res . status ( response . code ) . send ( response ) ;
226229 return ;
227230 }
228231
229232 const { status } = req . body ;
230233 const validStatuses = [ "active" , "inactive" , "suspended" ] ;
231234
232235 if ( ! status || ! validStatuses . includes ( status ) ) {
233- handleHttpError (
234- HttpErrors . ValidationFailed ( `Status must be one of: ${ validStatuses . join ( ", " ) } ` ) ,
235- res ,
236- gatewayResponse
236+ const response = apiResponse . error (
237+ HttpErrors . ValidationFailed ( `Status must be one of: ${ validStatuses . join ( ", " ) } ` )
237238 ) ;
239+ res . status ( response . code ) . send ( response ) ;
238240 return ;
239241 }
240242
241- logger . info ( { msg : `SuperAdmin updating account ${ targetAccountId } status to: ${ status } ` } ) ;
242-
243243 // Get current account to track the change (outside transaction)
244244 const [ currentAccount ] = await db
245245 . select ( { status : accounts . status } )
@@ -248,7 +248,8 @@ export const updateAccountStatus = asyncHandler(async (req: Request, res: Respon
248248 . limit ( 1 ) ;
249249
250250 if ( ! currentAccount ) {
251- handleHttpError ( HttpErrors . AccountNotFound ( ) , res , gatewayResponse ) ;
251+ const response = apiResponse . error ( HttpErrors . NotFound ( "Account" ) ) ;
252+ res . status ( response . code ) . send ( response ) ;
252253 return ;
253254 }
254255
@@ -267,11 +268,16 @@ export const updateAccountStatus = asyncHandler(async (req: Request, res: Respon
267268 } ) ;
268269
269270 if ( ! updatedAccount ) {
270- handleHttpError ( HttpErrors . AccountNotFound ( ) , res , gatewayResponse ) ;
271+ const response = apiResponse . error ( HttpErrors . NotFound ( "Account" ) ) ;
272+ res . status ( response . code ) . send ( response ) ;
271273 return ;
272274 }
273275
274- const response = gatewayResponse ( ) . success ( 200 , { account : updatedAccount } , `Account status updated to: ${ status } ` ) ;
276+ const response = apiResponse . success (
277+ HttpStatusCode . OK ,
278+ { account : updatedAccount } ,
279+ `Account status updated to: ${ status } `
280+ ) ;
275281
276282 res . status ( response . code ) . send ( response ) ;
277283} ) ;
@@ -285,8 +291,6 @@ export const listAllWorkspaces = asyncHandler(async (req: Request, res: Response
285291 const limit = parseInt ( req . query . limit as string ) || 20 ;
286292 const offset = ( page - 1 ) * limit ;
287293
288- logger . info ( { msg : `SuperAdmin listing all workspaces - page: ${ page } , limit: ${ limit } ` } ) ;
289-
290294 // Get paginated workspaces with owner info, member counts, and total count in single query
291295 const workspacesWithCount = await db
292296 . select ( {
@@ -303,8 +307,8 @@ export const listAllWorkspaces = asyncHandler(async (req: Request, res: Response
303307 email : accounts . email
304308 } ,
305309 memberCount : sql < number > `(
306- SELECT COUNT(*)
307- FROM workspace_memberships
310+ SELECT COUNT(*)
311+ FROM workspace_memberships
308312 WHERE workspace_memberships.workspace_id = ${ workspaces . uuid }
309313 )` ,
310314 totalCount : sql < number > `count(*) over()`
@@ -319,8 +323,8 @@ export const listAllWorkspaces = asyncHandler(async (req: Request, res: Response
319323 // Clean data by removing totalCount from individual records
320324 const workspacesList = workspacesWithCount . map ( ( { totalCount : _ , ...workspace } ) => workspace ) ;
321325
322- const response = gatewayResponse ( ) . success (
323- 200 ,
326+ const response = apiResponse . success (
327+ HttpStatusCode . OK ,
324328 {
325329 workspaces : workspacesList ,
326330 pagination : {
@@ -349,8 +353,6 @@ export const listAllMemberships = asyncHandler(async (req: Request, res: Respons
349353 const workspaceId = req . query . workspaceId as string ;
350354 const accountId = req . query . accountId as string ;
351355
352- logger . info ( { msg : `SuperAdmin listing memberships - filters: workspace=${ workspaceId } , account=${ accountId } ` } ) ;
353-
354356 // Build conditions for filtering
355357 const conditions = [ ] ;
356358 if ( workspaceId ) {
@@ -401,8 +403,8 @@ export const listAllMemberships = asyncHandler(async (req: Request, res: Respons
401403 const [ countResult ] = await countQuery ;
402404 const count = countResult ?. count || 0 ;
403405
404- const response = gatewayResponse ( ) . success (
405- 200 ,
406+ const response = apiResponse . success (
407+ HttpStatusCode . OK ,
406408 {
407409 memberships : membershipsList ,
408410 pagination : {
0 commit comments