generated from RealDevSquad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 279
feat: add API to fetch impersonation requests #2442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
prakashchoudhary07
merged 12 commits into
RealDevSquad:develop
from
Suvidh-kaushik:feat/get_impersonation_requests
Jun 25, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
478c3fc
added functionality for featching impersonation requests
Suvidh-kaushik 0901c70
fixed spacing and removed any type
Suvidh-kaushik 561db31
fixed: controller req types and jsdoc
Suvidh-kaushik cd63722
chore:fixed jsdoc in validator
Suvidh-kaushik f737e05
Merge branch 'develop' into feat/get_impersonation_requests
Suvidh-kaushik d5bd3de
updated page to nextPage and added constaraints
Suvidh-kaushik ef37a78
Merge branch 'feat/get_impersonation_requests' of https://github.com/…
Suvidh-kaushik 2a53bca
fixed error message
Suvidh-kaushik d1f2ef8
Merge branch 'develop' of https://github.com/Suvidh-kaushik/website-b…
Suvidh-kaushik 3adabf5
removed page based pagination
Suvidh-kaushik f3b0e01
added new route to fetchById
Suvidh-kaushik 78279d0
added try-catch for controller
Suvidh-kaushik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,14 +3,16 @@ import { | |
| ERROR_WHILE_CREATING_REQUEST, | ||
| IMPERSONATION_NOT_COMPLETED, | ||
| REQUEST_ALREADY_PENDING, | ||
| REQUEST_STATE | ||
| REQUEST_STATE, | ||
| ERROR_WHILE_FETCHING_REQUEST | ||
| } from "../constants/requests"; | ||
| import { Timestamp } from "firebase-admin/firestore"; | ||
| import { CreateImpersonationRequestModelDto, ImpersonationRequest } from "../types/impersonationRequest"; | ||
| import { Query, CollectionReference } from '@google-cloud/firestore'; | ||
| import { CreateImpersonationRequestModelDto, ImpersonationRequest, PaginatedImpersonationRequests,ImpersonationRequestQuery} from "../types/impersonationRequest"; | ||
| import { Forbidden } from "http-errors"; | ||
| const logger = require("../utils/logger"); | ||
|
|
||
| const impersonationRequestModel = firestore.collection("impersonationRequests"); | ||
| const DEFAULT_PAGE_SIZE = 5; | ||
|
|
||
| /** | ||
| * Creates a new impersonation request in Firestore. | ||
|
|
@@ -55,4 +57,115 @@ export const createImpersonationRequest = async ( | |
| logger.error(ERROR_WHILE_CREATING_REQUEST, { error, requestData: body }); | ||
| throw error; | ||
| } | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Retrieves an impersonation request by its ID. | ||
| * @param {string} id - The ID of the impersonation request to retrieve. | ||
| * @returns {Promise<ImpersonationRequest|null>} The found impersonation request or null if not found. | ||
| * @throws {Error} Logs and rethrows any error encountered during fetch. | ||
| */ | ||
| export const getImpersonationRequestById = async ( | ||
| id: string | ||
| ): Promise<ImpersonationRequest | null> => { | ||
| try { | ||
| const requestDoc = await impersonationRequestModel.doc(id).get(); | ||
| if (!requestDoc.exists) { | ||
| return null; | ||
| } | ||
| const data = requestDoc.data() as ImpersonationRequest; | ||
| return { | ||
| id: requestDoc.id, | ||
| ...data, | ||
| }; | ||
| } catch (error) { | ||
| logger.error(`${ERROR_WHILE_FETCHING_REQUEST} for ID: ${id}`, error); | ||
| throw error; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Retrieves a paginated list of impersonation requests based on query filters. | ||
| * @param {object} query - The query filters. | ||
| * @param {string} [query.createdBy] - Filter by the username of the request creator. | ||
| * @param {string} [query.createdFor] - Filter by the username of the user the request is created for. | ||
| * @param {string} [query.status] - Filter by request status (e.g., "APPROVED", "PENDING", "REJECTED"). | ||
| * @param {string} [query.prev] - Document ID to use as the ending point for backward pagination. | ||
| * @param {string} [query.next] - Document ID to use as the starting point for forward pagination. | ||
| * @param {string} [query.size] - Number of results per page. | ||
| * @returns {Promise<PaginatedImpersonationRequests|null>} The paginated impersonation requests or null if none found. | ||
| * @throws Logs and rethrows any error encountered during fetch. | ||
| */ | ||
| export const getImpersonationRequests = async ( | ||
| query | ||
| ): Promise<PaginatedImpersonationRequests | null> => { | ||
|
|
||
| let { createdBy, createdFor, status, prev, next, size = DEFAULT_PAGE_SIZE } = query; | ||
|
|
||
| size = size ? Number.parseInt(size) : DEFAULT_PAGE_SIZE; | ||
|
|
||
|
|
||
| try { | ||
| let requestQuery: Query<ImpersonationRequest> = impersonationRequestModel as CollectionReference<ImpersonationRequest>; | ||
|
|
||
| if (createdBy) { | ||
| requestQuery = requestQuery.where("createdBy", "==", createdBy); | ||
| } | ||
| if (status) { | ||
| requestQuery = requestQuery.where("status", "==", status); | ||
| } | ||
| if (createdFor) { | ||
| requestQuery = requestQuery.where("createdFor", "==", createdFor); | ||
|
Comment on lines
+111
to
+118
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will we need to create index for the queries?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, These queries require indexes I have created an issue link for this and tagged you - ISSUE LINK |
||
| } | ||
|
|
||
| requestQuery = requestQuery.orderBy("createdAt", "desc"); | ||
Achintya-Chatterjee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let requestQueryDoc = requestQuery; | ||
|
|
||
| if (prev) { | ||
| requestQueryDoc = requestQueryDoc.limitToLast(size); | ||
| } else { | ||
| requestQueryDoc = requestQueryDoc.limit(size); | ||
| } | ||
|
|
||
| if (next) { | ||
| const doc = await impersonationRequestModel.doc(next).get(); | ||
| requestQueryDoc = requestQueryDoc.startAt(doc); | ||
Achintya-Chatterjee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else if (prev) { | ||
| const doc = await impersonationRequestModel.doc(prev).get(); | ||
| requestQueryDoc = requestQueryDoc.endAt(doc); | ||
Achintya-Chatterjee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| const snapshot = await requestQueryDoc.get(); | ||
| let nextDoc; | ||
| let prevDoc; | ||
Suvidh-kaushik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (!snapshot.empty) { | ||
| const first = snapshot.docs[0]; | ||
| prevDoc = await requestQuery.endBefore(first).limitToLast(1).get(); | ||
| const last = snapshot.docs[snapshot.docs.length - 1]; | ||
| nextDoc = await requestQuery.startAfter(last).limit(1).get(); | ||
| } | ||
Achintya-Chatterjee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const allRequests = snapshot.empty | ||
| ? [] | ||
| : snapshot.docs.map(doc => ({ | ||
| id: doc.id, | ||
| ...doc.data() | ||
| })); | ||
|
|
||
| if (allRequests.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const count = allRequests.length; | ||
| return { | ||
| allRequests, | ||
| prev: prevDoc && !prevDoc.empty ? prevDoc.docs[0].id : null, | ||
| next: nextDoc && !nextDoc.empty ? nextDoc.docs[0].id : null, | ||
| count, | ||
| }; | ||
| } catch (error) { | ||
| logger.error(ERROR_WHILE_FETCHING_REQUEST, error); | ||
| throw error; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.