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 #2461
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
Closed
Closed
Changes from 1 commit
Commits
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
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
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 |
---|---|---|
|
@@ -5,12 +5,22 @@ import { | |
REQUEST_LOG_TYPE, | ||
REQUEST_STATE, | ||
USER_STATUS_NOT_FOUND, | ||
REQUEST_TYPE, | ||
REQUEST_ALREADY_APPROVED, | ||
REQUEST_ALREADY_REJECTED, | ||
REQUEST_APPROVED_SUCCESSFULLY, | ||
REQUEST_REJECTED_SUCCESSFULLY, | ||
INVALID_REQUEST_TYPE, | ||
} from "../constants/requests"; | ||
import { userState } from "../constants/userStatus"; | ||
import { createRequest } from "../models/requests"; | ||
import { createRequest, getRequestById } from "../models/requests"; | ||
import { OooStatusRequest, OooStatusRequestBody } from "../types/oooRequest"; | ||
import { UserStatus } from "../types/userStatus"; | ||
import { addLog } from "./logService"; | ||
import { BadRequest, Conflict } from "http-errors"; | ||
import { updateRequest } from "../models/requests"; | ||
import { AcknowledgeOooRequestBody } from "../types/oooRequest"; | ||
import { addFutureStatus } from "../models/userStatus"; | ||
|
||
/** | ||
* Validates the user status. | ||
|
@@ -93,3 +103,101 @@ export const createOooRequest = async ( | |
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Validates an Out-Of-Office (OOO) acknowledge request | ||
* | ||
* @param {string} requestId - The unique identifier of the request. | ||
* @param {string} requestType - The type of the request (expected to be 'OOO'). | ||
* @param {string} requestStatus - The current status of the request. | ||
* @throws {Error} Throws an error if an issue occurs during validation. | ||
*/ | ||
export const validateOooAcknowledgeRequest = async ( | ||
requestType: string, | ||
requestStatus: string, | ||
) => { | ||
|
||
try { | ||
|
||
if (requestType !== REQUEST_TYPE.OOO) { | ||
throw new BadRequest(INVALID_REQUEST_TYPE); | ||
} | ||
|
||
if (requestStatus === REQUEST_STATE.APPROVED) { | ||
throw new Conflict(REQUEST_ALREADY_APPROVED); | ||
} | ||
|
||
if (requestStatus === REQUEST_STATE.REJECTED) { | ||
throw new Conflict(REQUEST_ALREADY_REJECTED); | ||
} | ||
} catch (error) { | ||
logger.error("Error while validating OOO acknowledge request", error); | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Acknowledges an Out-of-Office (OOO) request | ||
* | ||
* @param {string} requestId - The ID of the OOO request to acknowledge. | ||
* @param {AcknowledgeOooRequestBody} body - The acknowledgement body containing acknowledging details. | ||
* @param {string} superUserId - The unique identifier of the superuser user. | ||
* @returns {Promise<object>} The acknowledged OOO request. | ||
* @throws {Error} Throws an error if an issue occurs during acknowledgment process. | ||
*/ | ||
export const acknowledgeOooRequest = async ( | ||
requestId: string, | ||
body: AcknowledgeOooRequestBody, | ||
superUserId: string, | ||
) => { | ||
try { | ||
const requestData = await getRequestById(requestId); | ||
|
||
await validateOooAcknowledgeRequest(requestData.type, requestData.status); | ||
|
||
const requestResult = await updateRequest(requestId, body, superUserId, REQUEST_TYPE.OOO); | ||
|
||
if ("error" in requestResult) { | ||
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. why are we doing this ? |
||
throw new BadRequest(requestResult.error); | ||
} | ||
|
||
const [acknowledgeLogType, returnMessage] = | ||
requestResult.status === REQUEST_STATE.APPROVED | ||
? [REQUEST_LOG_TYPE.REQUEST_APPROVED, REQUEST_APPROVED_SUCCESSFULLY] | ||
: [REQUEST_LOG_TYPE.REQUEST_REJECTED, REQUEST_REJECTED_SUCCESSFULLY]; | ||
|
||
const requestLog = { | ||
type: acknowledgeLogType, | ||
meta: { | ||
requestId, | ||
action: LOG_ACTION.UPDATE, | ||
userId: superUserId, | ||
}, | ||
body: requestResult, | ||
}; | ||
|
||
await addLog(requestLog.type, requestLog.meta, requestLog.body); | ||
|
||
if (requestResult.status === REQUEST_STATE.APPROVED) { | ||
await addFutureStatus({ | ||
requestId, | ||
state: REQUEST_TYPE.OOO, | ||
from: requestData.from, | ||
endsOn: requestData.until, | ||
userId: requestData.userId, | ||
message: body.comment, | ||
}); | ||
} | ||
|
||
return { | ||
message: returnMessage, | ||
data: { | ||
id: requestResult.id, | ||
...requestResult, | ||
}, | ||
}; | ||
} catch (error) { | ||
logger.error("Error while acknowledging OOO 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.