Skip to content

Commit 19c21ef

Browse files
renamed userId and impersonatedUserId
1 parent 8bff39f commit 19c21ef

File tree

10 files changed

+121
-123
lines changed

10 files changed

+121
-123
lines changed

controllers/impersonationRequests.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ export const createImpersonationRequestController = async (
3737
next: NextFunction
3838
): Promise<ImpersonationRequestResponse | void> => {
3939
try {
40-
const { impersonatedUserId, reason } = req.body as CreateImpersonationRequestBody;
40+
const { createdFor, reason } = req.body as CreateImpersonationRequestBody;
4141
const userId = req.userData?.id;
4242

4343
const impersonationRequest = await createImpersonationRequestService({
44-
userId,
45-
impersonatedUserId,
44+
createdBy: userId,
45+
createdFor,
4646
reason
4747
});
4848

middlewares/validators/impersonationRequests.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export const createImpersonationRequestValidator = async (
1717
next: NextFunction
1818
): Promise<void> => {
1919
const schema = joi.object().strict().keys({
20-
impersonatedUserId: joi.string().required().messages({
21-
"string.empty": "impersonatedUserId cannot be empty",
22-
"any.required": "impersonatedUserId is required"
20+
createdFor: joi.string().required().messages({
21+
"string.empty": "createdFor cannot be empty",
22+
"any.required": "createdFor is required"
2323
}),
2424
reason: joi.string().required().messages({
2525
"string.empty": "reason cannot be empty",

models/impersonationRequests.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export const createImpersonationRequest = async (
3131
): Promise<ImpersonationRequest> => {
3232
try {
3333
const reqQuery = impersonationRequestModel
34-
.where("impersonatedUserId", "==", body.impersonatedUserId)
35-
.where("userId", "==", body.userId)
34+
.where("createdFor", "==", body.createdFor)
35+
.where("createdBy", "==", body.createdBy)
3636
.where("status", "in", ["APPROVED", "PENDING"])
3737
.where("isImpersonationFinished", "==", false).orderBy("createdAt", "desc").limit(1);
3838

@@ -110,13 +110,13 @@ export const getImpersonationRequests = async (
110110
let requestQuery: Query<ImpersonationRequest> = impersonationRequestModel as CollectionReference<ImpersonationRequest>;
111111

112112
if (createdBy) {
113-
requestQuery = requestQuery.where("userId", "==", createdBy);
113+
requestQuery = requestQuery.where("createdBy", "==", createdBy);
114114
}
115115
if (status) {
116116
requestQuery = requestQuery.where("status", "==", status);
117117
}
118118
if (createdFor) {
119-
requestQuery = requestQuery.where("impersonatedUserId", "==", createdFor);
119+
requestQuery = requestQuery.where("createdFor", "==", createdFor);
120120
}
121121

122122
requestQuery = requestQuery.orderBy("createdAt", "desc");

services/impersonationRequests.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ export const createImpersonationRequestService = async (
4141
body: CreateImpersonationRequestServiceBody
4242
): Promise<ImpersonationRequest> => {
4343
try {
44-
const { userExists } = 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

4949

5050
const impersonationRequest = await createImpersonationRequest({
5151
status: REQUEST_STATE.PENDING,
52-
userId: body.userId,
53-
impersonatedUserId: body.impersonatedUserId,
52+
createdBy: body.createdBy,
53+
createdFor: body.createdFor,
5454
isImpersonationFinished: false,
5555
reason: body.reason,
5656
});
@@ -60,7 +60,7 @@ export const createImpersonationRequestService = async (
6060
meta: {
6161
requestId: impersonationRequest.id,
6262
action: LOG_ACTION.CREATE,
63-
userId: body.userId,
63+
userId: body.createdBy,
6464
createdAt: Date.now(),
6565
},
6666
body: {
@@ -100,7 +100,7 @@ export const updateImpersonationRequestService = async (
100100
throw new NotFound(REQUEST_DOES_NOT_EXIST);
101101
}
102102

103-
if (request.impersonatedUserId !== body.lastModifiedBy || request.status !== REQUEST_STATE.PENDING) {
103+
if (request.createdFor !== body.lastModifiedBy || request.status !== REQUEST_STATE.PENDING) {
104104
throw new Forbidden(OPERATION_NOT_ALLOWED);
105105
}
106106

@@ -153,7 +153,7 @@ export const startImpersonationService = async (
153153
}
154154

155155
if (
156-
body.userId !== impersonationRequest.userId ||
156+
body.userId !== impersonationRequest.createdBy ||
157157
impersonationRequest.status !== REQUEST_STATE.APPROVED ||
158158
impersonationRequest.isImpersonationFinished === true
159159
) {
@@ -215,7 +215,7 @@ export const stopImpersonationService = async (
215215
if (!impersonationRequest) {
216216
throw new NotFound(REQUEST_DOES_NOT_EXIST);
217217
}
218-
if ( body.userId !== impersonationRequest.impersonatedUserId ) {
218+
if ( body.userId !== impersonationRequest.createdFor ) {
219219
throw new Forbidden(OPERATION_NOT_ALLOWED);
220220
}
221221

@@ -264,31 +264,28 @@ export const stopImpersonationService = async (
264264
export const generateImpersonationTokenService = async (
265265
requestId: string,
266266
action: string
267-
): Promise<{ name: string, value: string, options: object }> => {
268-
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 {
269272
const request = await getImpersonationRequestById(requestId);
270273
if (!request) {
271274
throw new NotFound(REQUEST_DOES_NOT_EXIST);
272275
}
273276

274-
const { userId, impersonatedUserId } = request;
275-
const cookieName = config.get<string>("userToken.cookieName");
276-
const rdsUiUrl = new URL(config.get<string>("services.rdsUi.baseUrl"));
277-
const ttlInSeconds = Number(config.get("userToken.ttl"));
278-
279-
let token: string;
277+
const { createdBy: userId, createdFor: impersonatedUserId } = request;
280278

281-
switch (action) {
282-
case "START":
283-
token = await authService.generateImpersonationAuthToken({ userId, impersonatedUserId });
284-
break;
285279

286-
case "STOP":
287-
token = await authService.generateAuthToken({ userId });
288-
break;
280+
let token: string;
289281

290-
default:
291-
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 });
292289
}
293290

294291
return {

test/fixtures/impersonation-requests/impersonationRequests.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,50 @@ export const impersonationRequestsBodyData = [
44
{
55
status: REQUEST_STATE.PENDING,
66
isImpersonationFinished: false,
7-
userId: "userId123",
7+
createdBy: "userId123",
88
reason: "User assistance required for account debugging.",
9-
impersonatedUserId: "userId345",
9+
createdFor: "userId345",
1010
},
1111
{
1212
status: REQUEST_STATE.PENDING,
1313
isImpersonationFinished: false,
14-
userId: "userId124",
14+
createdBy: "userId124",
1515
reason: "User assistance required for account debugging.",
16-
impersonatedUserId: "userId445",
16+
createdFor: "userId445",
1717
},
1818
{
1919
status: REQUEST_STATE.PENDING,
2020
isImpersonationFinished: false,
21-
userId: "admin222",
21+
createdBy: "admin222",
2222
reason: "Investigating bug in user workflow.",
23-
impersonatedUserId: "user321",
23+
createdFor: "user321",
2424
},
2525
{
2626
status: REQUEST_STATE.PENDING,
2727
isImpersonationFinished: false,
28-
userId: "admin2223",
28+
createdBy: "admin2223",
2929
reason: "Verifying permissions for support case.",
30-
impersonatedUserId: "user322",
30+
createdFor: "user322",
3131
},
3232
{
3333
status: REQUEST_STATE.PENDING,
3434
isImpersonationFinished: false,
35-
userId: "admin222",
35+
createdBy: "admin222",
3636
reason: "Testing impersonation feature for QA.",
37-
impersonatedUserId: "user321",
37+
createdFor: "user321",
3838
},
3939
{
4040
status: REQUEST_STATE.APPROVED,
4141
isImpersonationFinished: false,
42-
userId: "approverId",
42+
createdBy: "approverId",
4343
reason: "Approved for troubleshooting session.",
44-
impersonatedUserId: "approvedUserId",
44+
createdFor: "approvedUserId",
4545
},
4646
{
4747
status: REQUEST_STATE.REJECTED,
4848
isImpersonationFinished: false,
49-
userId: "reviewerId",
49+
createdBy: "reviewerId",
5050
reason: "Request rejected due to insufficient details.",
51-
impersonatedUserId: "rejectedUserId",
51+
createdFor: "rejectedUserId",
5252
}
5353
];

0 commit comments

Comments
 (0)