generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 273
feat: implement OOO approve/reject functionality #2462
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
Open
RishiChaubey31
wants to merge
17
commits into
develop
Choose a base branch
from
feat/ooo-approve-reject-final
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a30d990
feat: implement OOO approve/reject functionality
tejaskh3 83d07c7
feat: enhance OOO request handling and validation
tejaskh3 2a97501
chore/local-testing
RishiChaubey31 e8a0693
fix/removed-userID-field
RishiChaubey31 040055f
fix/test
RishiChaubey31 49af584
fix/integration-test
RishiChaubey31 47a6b98
chore/code-refactor
RishiChaubey31 bb74cdd
fix:reverted-middleware
RishiChaubey31 eb67111
fix-statuscode-tests
RishiChaubey31 308818b
fix-test
RishiChaubey31 8a9f257
fix-handled-state-status-fieldissue
RishiChaubey31 ffd8abf
chore-removed-test
RishiChaubey31 8773599
fixed-createOOO-test
RishiChaubey31 301c5ea
fix-requestedy
RishiChaubey31 1a89944
fix: updated field names for consistency in OOO request handling
RishiChaubey31 debfebe
fix: enhance OOO request handling by adding status field and updating…
RishiChaubey31 7a4cc24
fix: add 'dev' field to OOO requests and update test to reflect changes
RishiChaubey31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,9 @@ import { | |
REQUEST_ALREADY_PENDING, | ||
USER_STATUS_NOT_FOUND, | ||
OOO_STATUS_ALREADY_EXIST, | ||
UNAUTHORIZED_TO_UPDATE_REQUEST, | ||
ERROR_WHILE_ACKNOWLEDGING_REQUEST, | ||
REQUEST_ID_REQUIRED, | ||
} from "../constants/requests"; | ||
import { statusState } from "../constants/userStatus"; | ||
import { logType } from "../constants/logs"; | ||
|
@@ -20,17 +23,24 @@ import { getRequestByKeyValues, getRequests, updateRequest } from "../models/req | |
import { createUserFutureStatus } from "../models/userFutureStatus"; | ||
import { getUserStatus, addFutureStatus } from "../models/userStatus"; | ||
import { createOooRequest, validateUserStatus } from "../services/oooRequest"; | ||
import * as oooRequestService from "../services/oooRequest"; | ||
import { CustomResponse } from "../typeDefinitions/global"; | ||
import { OooRequestCreateRequest, OooRequestResponse, OooStatusRequest } from "../types/oooRequest"; | ||
import { | ||
AcknowledgeOooRequest, | ||
OooRequestCreateRequest, | ||
OooRequestResponse, | ||
OooStatusRequest, | ||
} from "../types/oooRequest"; | ||
import { UpdateRequest } from "../types/requests"; | ||
import { NextFunction } from "express"; | ||
|
||
/** | ||
* Controller to handle the creation of OOO requests. | ||
* | ||
* | ||
* This function processes the request to create an OOO request, | ||
* validates the user status, checks existing requests, | ||
* and stores the new request in the database with logging. | ||
* | ||
* | ||
* @param {OooRequestCreateRequest} req - The Express request object containing the body with OOO details. | ||
* @param {CustomResponse} res - The Express response object used to send back the response. | ||
* @returns {Promise<OooRequestResponse>} Resolves to a response with the success or an error message. | ||
|
@@ -39,7 +49,6 @@ export const createOooRequestController = async ( | |
req: OooRequestCreateRequest, | ||
res: OooRequestResponse | ||
): Promise<OooRequestResponse> => { | ||
|
||
const requestBody = req.body; | ||
const { id: userId, username } = req.userData; | ||
const isUserPartOfDiscord = req.userData.roles.in_discord; | ||
|
@@ -57,25 +66,26 @@ export const createOooRequestController = async ( | |
|
||
if (validationResponse) { | ||
if (validationResponse.error === USER_STATUS_NOT_FOUND) { | ||
return res.boom.notFound(validationResponse.error); | ||
return res.boom.notFound(validationResponse.error); | ||
} | ||
if (validationResponse.error === OOO_STATUS_ALREADY_EXIST) { | ||
return res.boom.forbidden(validationResponse.error); | ||
return res.boom.forbidden(validationResponse.error); | ||
} | ||
} | ||
|
||
const latestOooRequest: OooStatusRequest = await getRequestByKeyValues({ | ||
userId, | ||
type: REQUEST_TYPE.OOO, | ||
status: REQUEST_STATE.PENDING, | ||
requestedBy: userId, | ||
type: REQUEST_TYPE.OOO, | ||
status: REQUEST_STATE.PENDING, | ||
}); | ||
|
||
if (latestOooRequest) { | ||
await addLog(logType.PENDING_REQUEST_FOUND, | ||
{ userId, oooRequestId: latestOooRequest.id }, | ||
{ message: REQUEST_ALREADY_PENDING } | ||
); | ||
return res.boom.conflict(REQUEST_ALREADY_PENDING); | ||
await addLog( | ||
logType.PENDING_REQUEST_FOUND, | ||
{ userId, oooRequestId: latestOooRequest.id }, | ||
{ message: REQUEST_ALREADY_PENDING } | ||
); | ||
return res.boom.conflict(REQUEST_ALREADY_PENDING); | ||
} | ||
|
||
await createOooRequest(requestBody, username, userId); | ||
|
@@ -103,7 +113,7 @@ export const updateOooRequestController = async (req: UpdateRequest, res: Custom | |
return res.boom.badRequest(requestResult.error); | ||
} | ||
const [logType, returnMessage] = | ||
requestResult.state === REQUEST_STATE.APPROVED | ||
requestResult.status === REQUEST_STATE.APPROVED | ||
? [REQUEST_LOG_TYPE.REQUEST_APPROVED, REQUEST_APPROVED_SUCCESSFULLY] | ||
: [REQUEST_LOG_TYPE.REQUEST_REJECTED, REQUEST_REJECTED_SUCCESSFULLY]; | ||
|
||
|
@@ -118,7 +128,7 @@ export const updateOooRequestController = async (req: UpdateRequest, res: Custom | |
body: requestResult, | ||
}; | ||
await addLog(requestLog.type, requestLog.meta, requestLog.body); | ||
if (requestResult.state === REQUEST_STATE.APPROVED) { | ||
if (requestResult.status === REQUEST_STATE.APPROVED) { | ||
const requestData = await getRequests({ id: requestId }); | ||
|
||
if (requestData) { | ||
|
@@ -148,3 +158,54 @@ export const updateOooRequestController = async (req: UpdateRequest, res: Custom | |
return res.boom.badImplementation(ERROR_WHILE_UPDATING_REQUEST); | ||
} | ||
}; | ||
|
||
/** | ||
* Acknowledges an Out-of-Office (OOO) request. | ||
* Devflag and superuser checks are handled by conditionalOooChecks middleware. | ||
* | ||
* @param {AcknowledgeOooRequest} req - The request object containing request parameters and user data | ||
* @param {OooRequestResponse} res - The response object | ||
* @param {NextFunction} next - Express next function for error handling | ||
* @returns {Promise<OooRequestResponse | void>} The response object or void on error | ||
*/ | ||
export const acknowledgeOooRequest = async ( | ||
req: AcknowledgeOooRequest, | ||
res: OooRequestResponse, | ||
next: NextFunction | ||
): Promise<OooRequestResponse> => { | ||
try { | ||
|
||
const requestBody = req.body; | ||
const superUserId = req.userData.id; | ||
const requestId = req.params.id; | ||
|
||
if (!requestId) { | ||
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. we can perform this check if the validator |
||
return res.boom.badRequest(REQUEST_ID_REQUIRED); | ||
} | ||
|
||
const response = await oooRequestService.acknowledgeOooRequest(requestId, requestBody, superUserId); | ||
|
||
return res.status(200).json({ | ||
message: response.message, | ||
}); | ||
} catch (error) { | ||
logger.error(ERROR_WHILE_ACKNOWLEDGING_REQUEST, { | ||
error: error.message, | ||
requestId: req.params.id, | ||
superUserId: req.userData?.id, | ||
requestType: req.body?.type, | ||
userAgent: req.get("User-Agent"), | ||
timestamp: new Date().toISOString(), | ||
}); | ||
|
||
|
||
if (error.statusCode === 409) { | ||
return res.boom.conflict(error.message); | ||
} else if (error.statusCode === 400) { | ||
return res.boom.badRequest(error.message); | ||
} else { | ||
|
||
next(new Error(ERROR_WHILE_ACKNOWLEDGING_REQUEST)); | ||
RishiChaubey31 marked this conversation as resolved.
Show resolved
Hide resolved
RishiChaubey31 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { NextFunction } from "express"; | ||
import { CustomRequest, CustomResponse } from "../types/global"; | ||
import { REQUEST_TYPE } from "../constants/requests"; | ||
import { devFlagMiddleware } from "./devFlag"; | ||
import authorizeRoles from "./authorizeRoles"; | ||
const { SUPERUSER } = require("../constants/roles"); | ||
|
||
/** | ||
* Conditional middleware that applies devFlag and superuser checks only for OOO requests. | ||
* This allows onboarding requests to bypass these checks while maintaining security for OOO operations. | ||
* | ||
* @param {CustomRequest} req - The request object | ||
* @param {CustomResponse} res - The response object | ||
* @param {NextFunction} next - The next middleware function | ||
*/ | ||
export const conditionalOooChecks = (req: CustomRequest, res: CustomResponse, next: NextFunction) => { | ||
const requestType = req.body?.type; | ||
|
||
|
||
if (requestType === REQUEST_TYPE.OOO) { | ||
|
||
devFlagMiddleware(req, res, (err: any) => { | ||
if (err) return next(err); | ||
|
||
|
||
authorizeRoles([SUPERUSER])(req, res, next); | ||
RishiChaubey31 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
} else { | ||
|
||
next(); | ||
} | ||
}; |
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
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.