@@ -41,20 +41,17 @@ export const createImpersonationRequestService = async (
4141 body : CreateImpersonationRequestServiceBody
4242) : Promise < ImpersonationRequest > => {
4343 try {
44- const { userExists, user : impersonatedUser } = await fetchUser ( { userId : body . impersonatedUserId } ) ;
44+ const { userExists } = await fetchUser ( { userId : body . createdFor } ) ;
4545 if ( ! userExists ) {
4646 throw new NotFound ( TASK_REQUEST_MESSAGES . USER_NOT_FOUND ) ;
4747 }
4848
49- const { username : createdFor } = impersonatedUser as User ;
5049
5150 const impersonationRequest = await createImpersonationRequest ( {
5251 status : REQUEST_STATE . PENDING ,
53- userId : body . userId ,
54- impersonatedUserId : body . impersonatedUserId ,
55- isImpersonationFinished : false ,
5652 createdBy : body . createdBy ,
57- createdFor : createdFor ,
53+ createdFor : body . createdFor ,
54+ isImpersonationFinished : false ,
5855 reason : body . reason ,
5956 } ) ;
6057
@@ -63,7 +60,7 @@ export const createImpersonationRequestService = async (
6360 meta : {
6461 requestId : impersonationRequest . id ,
6562 action : LOG_ACTION . CREATE ,
66- userId : body . userId ,
63+ userId : body . createdBy ,
6764 createdAt : Date . now ( ) ,
6865 } ,
6966 body : {
@@ -103,7 +100,7 @@ export const updateImpersonationRequestService = async (
103100 throw new NotFound ( REQUEST_DOES_NOT_EXIST ) ;
104101 }
105102
106- if ( request . impersonatedUserId !== body . lastModifiedBy || request . status !== REQUEST_STATE . PENDING ) {
103+ if ( request . createdFor !== body . lastModifiedBy || request . status !== REQUEST_STATE . PENDING ) {
107104 throw new Forbidden ( OPERATION_NOT_ALLOWED ) ;
108105 }
109106
@@ -156,7 +153,7 @@ export const startImpersonationService = async (
156153 }
157154
158155 if (
159- body . userId !== impersonationRequest . userId ||
156+ body . userId !== impersonationRequest . createdBy ||
160157 impersonationRequest . status !== REQUEST_STATE . APPROVED ||
161158 impersonationRequest . isImpersonationFinished === true
162159 ) {
@@ -218,7 +215,7 @@ export const stopImpersonationService = async (
218215 if ( ! impersonationRequest ) {
219216 throw new NotFound ( REQUEST_DOES_NOT_EXIST ) ;
220217 }
221- if ( body . userId !== impersonationRequest . impersonatedUserId ) {
218+ if ( body . userId !== impersonationRequest . createdFor ) {
222219 throw new Forbidden ( OPERATION_NOT_ALLOWED ) ;
223220 }
224221
@@ -267,31 +264,28 @@ export const stopImpersonationService = async (
267264export const generateImpersonationTokenService = async (
268265 requestId : string ,
269266 action : string
270- ) : Promise < { name : string , value : string , options : object } > => {
271- try {
267+ ) : Promise < { name : string ; value : string ; options : object } > => {
268+ const cookieName = config . get < string > ( "userToken.cookieName" ) ;
269+ const rdsUiUrl = new URL ( config . get < string > ( "services.rdsUi.baseUrl" ) ) ;
270+ const ttlInSeconds = Number ( config . get ( "userToken.ttl" ) ) ;
271+ try {
272272 const request = await getImpersonationRequestById ( requestId ) ;
273273 if ( ! request ) {
274274 throw new NotFound ( REQUEST_DOES_NOT_EXIST ) ;
275275 }
276276
277- const { userId, impersonatedUserId } = request ;
278- const cookieName = config . get < string > ( "userToken.cookieName" ) ;
279- const rdsUiUrl = new URL ( config . get < string > ( "services.rdsUi.baseUrl" ) ) ;
280- const ttlInSeconds = Number ( config . get ( "userToken.ttl" ) ) ;
277+ const { createdBy : userId , createdFor : impersonatedUserId } = request ;
281278
282- let token : string ;
283-
284- switch ( action ) {
285- case "START" :
286- token = await authService . generateImpersonationAuthToken ( { userId, impersonatedUserId } ) ;
287- break ;
288279
289- case "STOP" :
290- token = await authService . generateAuthToken ( { userId } ) ;
291- break ;
280+ let token : string ;
292281
293- default :
294- throw new BadRequest ( INVALID_ACTION_PARAM ) ;
282+ if ( action === "START" ) {
283+ token = await authService . generateImpersonationAuthToken ( {
284+ userId,
285+ impersonatedUserId,
286+ } ) ;
287+ } else if ( action === "STOP" ) {
288+ token = await authService . generateAuthToken ( { userId } ) ;
295289 }
296290
297291 return {
0 commit comments