-
Notifications
You must be signed in to change notification settings - Fork 279
Fix: feature to create OOO request #2383
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
Changes from all commits
fa9d697
6a1ad47
88657cb
a010d9c
2da2db0
b39769e
fa7f490
ad1420b
9bfb615
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,57 +3,85 @@ import { | |
| LOG_ACTION, | ||
| REQUEST_CREATED_SUCCESSFULLY, | ||
| ERROR_WHILE_CREATING_REQUEST, | ||
| REQUEST_ALREADY_PENDING, | ||
| REQUEST_STATE, | ||
| REQUEST_TYPE, | ||
| ERROR_WHILE_UPDATING_REQUEST, | ||
| REQUEST_APPROVED_SUCCESSFULLY, | ||
| REQUEST_REJECTED_SUCCESSFULLY, | ||
| UNAUTHORIZED_TO_CREATE_OOO_REQUEST, | ||
| REQUEST_ALREADY_PENDING, | ||
| USER_STATUS_NOT_FOUND, | ||
| OOO_STATUS_ALREADY_EXIST, | ||
| } from "../constants/requests"; | ||
| import { statusState } from "../constants/userStatus"; | ||
| import { logType } from "../constants/logs"; | ||
| import { addLog } from "../models/logs"; | ||
| import { createRequest, getRequestByKeyValues, getRequests, updateRequest } from "../models/requests"; | ||
| import { getRequestByKeyValues, getRequests, updateRequest } from "../models/requests"; | ||
| import { createUserFutureStatus } from "../models/userFutureStatus"; | ||
| import { addFutureStatus } from "../models/userStatus"; | ||
| import { getUserStatus, addFutureStatus } from "../models/userStatus"; | ||
| import { createOooRequest, validateUserStatus } from "../services/oooRequest"; | ||
| import { CustomResponse } from "../typeDefinitions/global"; | ||
| import { OooRequestCreateRequest, OooStatusRequest } from "../types/oooRequest"; | ||
| import { OooRequestCreateRequest, OooRequestResponse, OooStatusRequest } from "../types/oooRequest"; | ||
| import { UpdateRequest } from "../types/requests"; | ||
|
|
||
| export const createOooRequestController = async (req: OooRequestCreateRequest, res: CustomResponse) => { | ||
| /** | ||
| * 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. | ||
| */ | ||
| export const createOooRequestController = async ( | ||
| req: OooRequestCreateRequest, | ||
| res: OooRequestResponse | ||
| ): Promise<OooRequestResponse> => { | ||
|
|
||
| const requestBody = req.body; | ||
vikasosmium marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const userId = req?.userData?.id; | ||
| const { id: userId, username } = req.userData; | ||
| const isUserPartOfDiscord = req.userData.roles.in_discord; | ||
| const dev = req.query.dev === "true"; | ||
prakashchoudhary07 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (!userId) { | ||
| return res.boom.unauthorized(); | ||
| if (!dev) return res.boom.notImplemented("Feature not implemented"); | ||
surajmaity1 marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+46
to
+48
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. NIT: we should have checked this condition at the beginning it self
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. I'll fix this. |
||
|
|
||
| if (!isUserPartOfDiscord) { | ||
| return res.boom.forbidden(UNAUTHORIZED_TO_CREATE_OOO_REQUEST); | ||
shubhdevelop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| try { | ||
| const latestOooRequest:OooStatusRequest = await getRequestByKeyValues({ requestedBy: userId, type: REQUEST_TYPE.OOO , state: REQUEST_STATE.PENDING }); | ||
| const userStatus = await getUserStatus(userId); | ||
| const validationResponse = await validateUserStatus(userId, userStatus); | ||
|
|
||
| if (latestOooRequest && latestOooRequest.state === REQUEST_STATE.PENDING) { | ||
| return res.boom.badRequest(REQUEST_ALREADY_PENDING); | ||
| if (validationResponse) { | ||
| if (validationResponse.error === USER_STATUS_NOT_FOUND) { | ||
| return res.boom.notFound(validationResponse.error); | ||
| } | ||
| if (validationResponse.error === OOO_STATUS_ALREADY_EXIST) { | ||
| return res.boom.forbidden(validationResponse.error); | ||
| } | ||
| } | ||
|
|
||
| const requestResult = await createRequest({ requestedBy: userId, ...requestBody }); | ||
| const latestOooRequest: OooStatusRequest = await getRequestByKeyValues({ | ||
| userId, | ||
| type: REQUEST_TYPE.OOO, | ||
| status: REQUEST_STATE.PENDING, | ||
| }); | ||
|
|
||
| const requestLog = { | ||
| type: REQUEST_LOG_TYPE.REQUEST_CREATED, | ||
| meta: { | ||
| requestId: requestResult.id, | ||
| action: LOG_ACTION.CREATE, | ||
| userId: userId, | ||
| createdAt: Date.now(), | ||
| }, | ||
| body: requestResult, | ||
| }; | ||
| await addLog(requestLog.type, requestLog.meta, requestLog.body); | ||
| if (latestOooRequest) { | ||
| 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); | ||
|
|
||
| return res.status(201).json({ | ||
| message: REQUEST_CREATED_SUCCESSFULLY, | ||
|
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. Is response data not required?
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. As discussed at the time of design document review with Yash and Tejas, we don't need response data. You can check the design document. Thank you |
||
| data: { | ||
| id: requestResult.id, | ||
| ...requestResult, | ||
| }, | ||
| }); | ||
| } catch (err) { | ||
| logger.error(ERROR_WHILE_CREATING_REQUEST, err); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,14 +26,14 @@ export const createOooStatusRequestValidator = async ( | |
| "number.min": "until date must be greater than or equal to from date", | ||
| }) | ||
| .required(), | ||
| message: joi.string().required().messages({ | ||
| "any.required": "message is required", | ||
| "string.empty": "message cannot be empty", | ||
| reason: joi.string().required().messages({ | ||
|
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. So here we have changed "message" to "reason", and also "state" to "type" How are we handling the old request that are already created till now? Will this affect how we are storing things in db
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. Hi @prakashchoudhary07, We're not currently using the /requests endpoint to submit OOO requests; it exists solely on the backend. Thanks!
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. @surajmaity1 Please create an issue for it, attach the link here, and track it. So that it won't slip through the gaps
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. Here is the issue ticket - #2418 Thank you! |
||
| "any.required": "reason is required", | ||
| "string.empty": "reason cannot be empty", | ||
| }), | ||
| state: joi.string().valid(REQUEST_STATE.PENDING).required().messages({ | ||
| "any.only": "state must be PENDING", | ||
| type: joi.string().valid(REQUEST_TYPE.OOO).required().messages({ | ||
| "string.empty": "type cannot be empty", | ||
| "any.required": "type is required", | ||
| }), | ||
surajmaity1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| type: joi.string().valid(REQUEST_TYPE.OOO).required(), | ||
| }); | ||
|
|
||
| await schema.validateAsync(req.body, { abortEarly: false }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { logType } from "../constants/logs"; | ||
| import { | ||
| LOG_ACTION, | ||
| OOO_STATUS_ALREADY_EXIST, | ||
| REQUEST_LOG_TYPE, | ||
| REQUEST_STATE, | ||
| USER_STATUS_NOT_FOUND, | ||
| } from "../constants/requests"; | ||
| import { userState } from "../constants/userStatus"; | ||
| import { createRequest } from "../models/requests"; | ||
| import { OooStatusRequest, OooStatusRequestBody } from "../types/oooRequest"; | ||
| import { UserStatus } from "../types/userStatus"; | ||
| import { addLog } from "./logService"; | ||
|
|
||
| /** | ||
| * Validates the user status. | ||
| * | ||
| * @param {string} userId - The unique identifier of the user. | ||
| * @param {UserStatus} userStatus - The status object of the user. | ||
| * @throws {Error} Throws an error if an issue occurs during validation. | ||
| */ | ||
| export const validateUserStatus = async ( | ||
vikasosmium marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| userId: string, | ||
|
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. Where is this function being used apart from this file?
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. Hi @prakashchoudhary07 , Thank you. |
||
| userStatus: UserStatus | ||
| ) => { | ||
| try { | ||
|
|
||
| if (!userStatus.userStatusExists) { | ||
| await addLog(logType.USER_STATUS_NOT_FOUND, { userId }, { message: USER_STATUS_NOT_FOUND }); | ||
| return { | ||
| error: USER_STATUS_NOT_FOUND | ||
| }; | ||
| } | ||
|
|
||
| if (userStatus.data.currentStatus.state === userState.OOO) { | ||
| await addLog(logType.OOO_STATUS_FOUND, | ||
| { userId, userStatus: userState.OOO }, | ||
| { message: OOO_STATUS_ALREADY_EXIST } | ||
| ); | ||
| return { | ||
| error: OOO_STATUS_ALREADY_EXIST | ||
| }; | ||
| } | ||
| } catch (error) { | ||
| logger.error("Error while validating OOO create request", error); | ||
| throw error; | ||
surajmaity1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create an OOO request for a user. | ||
| * | ||
| * @param {OooStatusRequestBody} body - The request body containing OOO details. | ||
| * @param {string} username - The username of the person creating the request. | ||
| * @param {string} userId - The unique identifier of the user. | ||
| * @returns {Promise<object>} The created OOO request. | ||
| * @throws {Error} Throws an error if an issue occurs during validation. | ||
| */ | ||
| export const createOooRequest = async ( | ||
| body: OooStatusRequestBody, | ||
| username: string, | ||
| userId: string | ||
| ) => { | ||
| try { | ||
| const request: OooStatusRequest = await createRequest({ | ||
| from: body.from, | ||
| until: body.until, | ||
| type: body.type, | ||
surajmaity1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| requestedBy: username, | ||
| userId, | ||
| reason: body.reason, | ||
| comment: null, | ||
| status: REQUEST_STATE.PENDING, | ||
| lastModifiedBy: null, | ||
| }); | ||
|
|
||
| const requestLog = { | ||
| type: REQUEST_LOG_TYPE.REQUEST_CREATED, | ||
| meta: { | ||
| requestId: request.id, | ||
| action: LOG_ACTION.CREATE, | ||
| userId, | ||
| createdAt: Date.now(), | ||
| }, | ||
| body: request, | ||
| }; | ||
|
|
||
| await addLog(requestLog.type, requestLog.meta, requestLog.body); | ||
|
|
||
| return request; | ||
| } catch (error) { | ||
| logger.error("Error while creating OOO request", error); | ||
| throw error; | ||
surajmaity1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
AnujChhikara marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.