generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 273
Add feature to approve or reject OOO request #2384
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
surajmaity1
wants to merge
11
commits into
Real-Dev-Squad:develop
Choose a base branch
from
surajmaity1:feat/user-ooo-approve-reject
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.
+403
−122
Open
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ebb4ee4
feat: acknowledge OOO request
surajmaity1 0e60e6d
feat: acknowledge OOO request
surajmaity1 5540a2e
feat: acknowledge OOO request
surajmaity1 9ca4038
feat: add test cases for creating OOO request
surajmaity1 e33183a
fix: acknowledge OOO request
surajmaity1 a87f032
feat: acknowledge OOO request
surajmaity1 f171027
Merge branch 'develop' into feat/user-ooo-approve-reject
surajmaity1 398d1f9
feat: acknowledge OOO request
surajmaity1 aacbff2
refactor: controller & service layer
surajmaity1 b22e969
fix: mocked dependencies for ooo request service
surajmaity1 680fe42
refactor: ooo acknowledge controller
surajmaity1 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { NextFunction } from "express"; | ||
import { | ||
REQUEST_LOG_TYPE, | ||
LOG_ACTION, | ||
|
@@ -9,14 +10,16 @@ import { | |
ERROR_WHILE_UPDATING_REQUEST, | ||
REQUEST_APPROVED_SUCCESSFULLY, | ||
REQUEST_REJECTED_SUCCESSFULLY, | ||
UNAUTHORIZED_TO_ACKNOWLEDGE_OOO_REQUEST, | ||
} from "../constants/requests"; | ||
import { statusState } from "../constants/userStatus"; | ||
import { addLog } from "../models/logs"; | ||
import { createRequest, getRequestByKeyValues, getRequests, updateRequest } from "../models/requests"; | ||
import { createUserFutureStatus } from "../models/userFutureStatus"; | ||
import { addFutureStatus } from "../models/userStatus"; | ||
import { acknowledgeOOORequest } from "../services/oooRequest"; | ||
import { CustomResponse } from "../typeDefinitions/global"; | ||
import { OooRequestCreateRequest, OooStatusRequest } from "../types/oooRequest"; | ||
import { AcknowledgeOOORequest, OooRequestCreateRequest, OooRequestResponse, OooStatusRequest } from "../types/oooRequest"; | ||
import { UpdateRequest } from "../types/requests"; | ||
|
||
export const createOooRequestController = async (req: OooRequestCreateRequest, res: CustomResponse) => { | ||
|
@@ -30,7 +33,7 @@ export const createOooRequestController = async (req: OooRequestCreateRequest, r | |
try { | ||
const latestOooRequest:OooStatusRequest = await getRequestByKeyValues({ requestedBy: userId, type: REQUEST_TYPE.OOO , state: REQUEST_STATE.PENDING }); | ||
|
||
if (latestOooRequest && latestOooRequest.state === REQUEST_STATE.PENDING) { | ||
if (latestOooRequest && latestOooRequest.status === REQUEST_STATE.PENDING) { | ||
surajmaity1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return res.boom.badRequest(REQUEST_ALREADY_PENDING); | ||
} | ||
|
||
|
@@ -120,3 +123,37 @@ export const updateOooRequestController = async (req: UpdateRequest, res: Custom | |
return res.boom.badImplementation(ERROR_WHILE_UPDATING_REQUEST); | ||
} | ||
}; | ||
|
||
export const acknowledgeOOORequestController = async ( | ||
pankajjs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
req: AcknowledgeOOORequest, | ||
res: OooRequestResponse, | ||
next: NextFunction, | ||
) | ||
: Promise<OooRequestResponse> => { | ||
|
||
const dev = req.query.dev === "true"; | ||
|
||
if(!dev) return res.boom.notImplemented("Feature not implemented"); | ||
AnujChhikara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
const requestBody = req.body; | ||
const userId = req.userData.id; | ||
pankajjs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const requestId = req.params.id; | ||
|
||
const isSuperuser = req.userData.roles.super_user === true; | ||
AnujChhikara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
if (isSuperuser === false) { | ||
AnujChhikara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return res.boom.unauthorized(UNAUTHORIZED_TO_ACKNOWLEDGE_OOO_REQUEST); | ||
} | ||
|
||
try { | ||
|
||
const response = acknowledgeOOORequest(requestId, requestBody, userId); | ||
MayankBansal12 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
return res.status(200).json({ | ||
message: (await response).message, | ||
}); | ||
} | ||
surajmaity1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
catch(error){ | ||
logger.error(ERROR_WHILE_UPDATING_REQUEST, error); | ||
next(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
vinit717 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
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,135 @@ | ||
import { addLog } from "./logService"; | ||
import firestore from "../utils/firestore"; | ||
import { NotFound, BadRequest } from "http-errors"; | ||
import { logType } from "../constants/logs"; | ||
import { | ||
REQUEST_STATE, | ||
REQUEST_TYPE, | ||
REQUEST_DOES_NOT_EXIST, | ||
REQUEST_ALREADY_APPROVED, | ||
REQUEST_ALREADY_REJECTED, | ||
REQUEST_LOG_TYPE, | ||
REQUEST_APPROVED_SUCCESSFULLY, | ||
REQUEST_REJECTED_SUCCESSFULLY, | ||
LOG_ACTION, | ||
INVALID_REQUEST_TYPE, | ||
} from "../constants/requests"; | ||
import { getRequests, updateRequest } from "../models/requests"; | ||
import { AcknowledgeOOORequestBody } from "../types/oooRequest"; | ||
import { statusState } from "../constants/userStatus"; | ||
import { createUserFutureStatus } from "../models/userFutureStatus"; | ||
import { addFutureStatus } from "../models/userStatus"; | ||
const requestModel = firestore.collection("requests"); | ||
yesyash marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
export const validateOOOAcknowledgeRequest = async ( | ||
MayankBansal12 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
requestId: string, | ||
requestType: string, | ||
requestStatus: string, | ||
) => { | ||
|
||
try { | ||
|
||
if (requestType !== REQUEST_TYPE.OOO) { | ||
await addLog(logType.INVALID_REQUEST_TYPE, | ||
{ requestId, type: requestType }, | ||
{ message: INVALID_REQUEST_TYPE } | ||
); | ||
yesyash marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
throw BadRequest(INVALID_REQUEST_TYPE); | ||
} | ||
|
||
if (requestStatus === REQUEST_STATE.APPROVED) { | ||
await addLog(logType.REQUEST_ALREADY_APPROVED, | ||
{ oooRequestId: requestId }, | ||
{ message: REQUEST_ALREADY_APPROVED } | ||
); | ||
throw BadRequest(REQUEST_ALREADY_APPROVED); | ||
} | ||
|
||
if (requestStatus === REQUEST_STATE.REJECTED) { | ||
await addLog(logType.REQUEST_ALREADY_REJECTED, | ||
surajmaity1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ oooRequestId: requestId }, | ||
{ message: REQUEST_ALREADY_REJECTED } | ||
); | ||
throw BadRequest(REQUEST_ALREADY_REJECTED); | ||
} | ||
AnujChhikara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (error) { | ||
logger.error("Error while validating OOO acknowledge request", error); | ||
MayankBansal12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw error; | ||
surajmaity1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
export const acknowledgeOOORequest = async ( | ||
requestId: string, | ||
body: AcknowledgeOOORequestBody, | ||
userId: string, | ||
) => { | ||
try { | ||
const request = await requestModel.doc(requestId).get(); | ||
|
||
if (!request.exists) { | ||
await addLog(logType.REQUEST_DOES_NOT_EXIST, | ||
yesyash marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ oooRequestId: requestId }, | ||
{ message: REQUEST_DOES_NOT_EXIST } | ||
); | ||
throw NotFound(REQUEST_DOES_NOT_EXIST); | ||
} | ||
|
||
const requestData = request.data(); | ||
|
||
await validateOOOAcknowledgeRequest(requestId, requestData.type, requestData.status); | ||
surajmaity1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
const requestResult = await updateRequest(requestId, body, userId, REQUEST_TYPE.OOO); | ||
|
||
if ("error" in requestResult) { | ||
throw BadRequest(requestResult.error); | ||
} | ||
AnujChhikara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const [acknowledgeLogType, returnMessage] = | ||
requestResult.status === REQUEST_STATE.APPROVED | ||
AnujChhikara marked this conversation as resolved.
Show resolved
Hide resolved
|
||
? [REQUEST_LOG_TYPE.REQUEST_APPROVED, REQUEST_APPROVED_SUCCESSFULLY] | ||
: [REQUEST_LOG_TYPE.REQUEST_REJECTED, REQUEST_REJECTED_SUCCESSFULLY]; | ||
|
||
const requestLog = { | ||
type: acknowledgeLogType, | ||
meta: { | ||
requestId: requestId, | ||
action: LOG_ACTION.UPDATE, | ||
userId: userId, | ||
createdAt: Date.now(), | ||
pankajjs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}, | ||
body: requestResult, | ||
}; | ||
|
||
await addLog(requestLog.type, requestLog.meta, requestLog.body); | ||
|
||
if (requestResult.status === REQUEST_STATE.APPROVED) { | ||
const requestData = await getRequests({ id: requestId }); | ||
|
||
pankajjs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (requestData) { | ||
surajmaity1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const { from, until, requestedBy, comment } = requestData as any; | ||
const userFutureStatusData = { | ||
requestId, | ||
status: REQUEST_TYPE.OOO, | ||
state: statusState.UPCOMING, | ||
AnujChhikara marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
from, | ||
endsOn: until, | ||
userId: requestedBy, | ||
message: comment, | ||
}; | ||
await createUserFutureStatus(userFutureStatusData); | ||
await addFutureStatus(userFutureStatusData); | ||
} | ||
} | ||
|
||
return { | ||
message: returnMessage, | ||
data: { | ||
id: requestResult.id, | ||
...requestResult, | ||
}, | ||
pankajjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
} catch (error) { | ||
logger.error("Error while acknowledging OOO request", error); | ||
surajmaity1 marked this conversation as resolved.
Show resolved
Hide resolved
MayankBansal12 marked this conversation as resolved.
Show resolved
Hide resolved
RishiChaubey31 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
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.
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.