11import { createErrorHandler , defaultResponder , ResponderFunction } from "./helpersExpress" ;
2- import { ApiError , Request , Response , ErrorCode } from "./types" ;
2+ import { ApiError , Request , Response , ErrorCode , AuthenticatedRequestBody } from "./types" ;
33import { logger } from "../config/logger" ;
44import { Service , Services } from "../config/service" ;
55
@@ -60,6 +60,7 @@ export abstract class Controller {
6060 data : T ,
6161 statusCode : number ,
6262 operationName : string ,
63+ userId : string ,
6364 ) : Promise < Response > {
6465 if ( ! service ) {
6566 this . logger . warn ( `${ operationName } operation not implemented` ) ;
@@ -69,7 +70,7 @@ export abstract class Controller {
6970 } ) ;
7071 }
7172 this . logger . info ( `Executing service: ${ operationName } ` , { statusCode } ) ;
72- const body = await service . execute ( data ) ;
73+ const body = await service . execute ( userId , data ) ;
7374 return this . respond ( statusCode , body ) ;
7475 }
7576
@@ -80,7 +81,13 @@ export abstract class Controller {
8081 * @returns Response with status 200 and entity data
8182 */
8283 public async getOne < T > ( req : Request , data : T ) : Promise < Response > {
83- return this . _executeService ( this . services . get , data , 200 , "Get" ) ;
84+ return this . _executeService (
85+ this . services . get ,
86+ data ,
87+ 200 ,
88+ "Get" ,
89+ ( req . body as AuthenticatedRequestBody ) . authenticatedUserId ,
90+ ) ;
8491 }
8592
8693 /**
@@ -90,7 +97,13 @@ export abstract class Controller {
9097 * @returns Response with status 200 and list of entities
9198 */
9299 public async getAll < T > ( req : Request , data : T ) : Promise < Response > {
93- return this . _executeService ( this . services . getAll , data , 200 , "GetAll" ) ;
100+ return this . _executeService (
101+ this . services . getAll ,
102+ data ,
103+ 200 ,
104+ "GetAll" ,
105+ ( req . body as AuthenticatedRequestBody ) . authenticatedUserId ,
106+ ) ;
94107 }
95108
96109 /**
@@ -101,7 +114,13 @@ export abstract class Controller {
101114 * @returns Response with status 200 and list of child entities
102115 */
103116 public async getChildren < T > ( req : Request , data : T , service : Service < T > ) : Promise < Response > {
104- return this . _executeService ( service , data , 200 , "GetChildren" ) ;
117+ return this . _executeService (
118+ service ,
119+ data ,
120+ 200 ,
121+ "GetChildren" ,
122+ ( req . body as AuthenticatedRequestBody ) . authenticatedUserId ,
123+ ) ;
105124 }
106125
107126 /**
@@ -112,7 +131,13 @@ export abstract class Controller {
112131 * @returns Response with status 201 and created child entity data
113132 */
114133 public async addChild < T > ( req : Request , data : T , service : Service < T > ) : Promise < Response > {
115- return this . _executeService ( service , data , 201 , "AddChild" ) ;
134+ return this . _executeService (
135+ service ,
136+ data ,
137+ 201 ,
138+ "AddChild" ,
139+ ( req . body as AuthenticatedRequestBody ) . authenticatedUserId ,
140+ ) ;
116141 }
117142
118143 /**
@@ -123,7 +148,13 @@ export abstract class Controller {
123148 * @returns Response with status 204 (No Content)
124149 */
125150 public async removeChild < T > ( req : Request , data : T , service : Service < T > ) : Promise < Response > {
126- return this . _executeService ( service , data , 204 , "RemoveChild" ) ;
151+ return this . _executeService (
152+ service ,
153+ data ,
154+ 204 ,
155+ "RemoveChild" ,
156+ ( req . body as AuthenticatedRequestBody ) . authenticatedUserId ,
157+ ) ;
127158 }
128159
129160 /**
@@ -133,7 +164,13 @@ export abstract class Controller {
133164 * @returns Response with status 200 and updated entity data
134165 */
135166 public async update < T > ( req : Request , data : T ) : Promise < Response > {
136- return this . _executeService ( this . services . update , data , 204 , "Update" ) ;
167+ return this . _executeService (
168+ this . services . update ,
169+ data ,
170+ 204 ,
171+ "Update" ,
172+ ( req . body as AuthenticatedRequestBody ) . authenticatedUserId ,
173+ ) ;
137174 }
138175
139176 /**
@@ -143,7 +180,13 @@ export abstract class Controller {
143180 * @returns Response with status 204 (No Content)
144181 */
145182 public async delete < T > ( req : Request , data : T ) : Promise < Response > {
146- return this . _executeService ( this . services . remove , data , 204 , "Delete" ) ;
183+ return this . _executeService (
184+ this . services . remove ,
185+ data ,
186+ 204 ,
187+ "Delete" ,
188+ ( req . body as AuthenticatedRequestBody ) . authenticatedUserId ,
189+ ) ;
147190 }
148191
149192 /**
@@ -153,6 +196,12 @@ export abstract class Controller {
153196 * @returns Response with status 201 and created entity data
154197 */
155198 public async create < T > ( req : Request , data : T ) : Promise < Response > {
156- return this . _executeService ( this . services . create , data , 201 , "Create" ) ;
199+ return this . _executeService (
200+ this . services . create ,
201+ data ,
202+ 201 ,
203+ "Create" ,
204+ ( req . body as AuthenticatedRequestBody ) . authenticatedUserId ,
205+ ) ;
157206 }
158207}
0 commit comments