Skip to content

Commit e33183a

Browse files
committed
fix: acknowledge OOO request
2 parents 9ca4038 + ee881bb commit e33183a

File tree

9 files changed

+349
-79
lines changed

9 files changed

+349
-79
lines changed

constants/requests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const REQUEST_ALREADY_REJECTED = "Request already rejected";
4040
export const ERROR_WHILE_FETCHING_REQUEST = "Error while fetching request";
4141
export const ERROR_WHILE_CREATING_REQUEST = "Error while creating request";
4242
export const ERROR_WHILE_UPDATING_REQUEST = "Error while updating request";
43+
export const ERROR_WHILE_ACKNOWLEDGING_REQUEST = "Error while acknowledging request";
4344

4445
export const REQUEST_DOES_NOT_EXIST = "Request does not exist";
4546
export const REQUEST_ALREADY_PENDING = "Request already exists please wait for approval or rejection";

controllers/oooRequests.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { NextFunction } from "express";
21
import {
32
REQUEST_LOG_TYPE,
43
LOG_ACTION,
@@ -11,6 +10,11 @@ import {
1110
REQUEST_APPROVED_SUCCESSFULLY,
1211
REQUEST_REJECTED_SUCCESSFULLY,
1312
UNAUTHORIZED_TO_ACKNOWLEDGE_OOO_REQUEST,
13+
ERROR_WHILE_ACKNOWLEDGING_REQUEST,
14+
REQUEST_DOES_NOT_EXIST,
15+
INVALID_REQUEST_TYPE,
16+
REQUEST_ALREADY_APPROVED,
17+
REQUEST_ALREADY_REJECTED,
1418
} from "../constants/requests";
1519
import { statusState } from "../constants/userStatus";
1620
import { addLog } from "../models/logs";
@@ -134,7 +138,6 @@ export const updateOooRequestController = async (req: UpdateRequest, res: Custom
134138
export const acknowledgeOOORequestController = async (
135139
req: AcknowledgeOOORequest,
136140
res: OooRequestResponse,
137-
next: NextFunction,
138141
)
139142
: Promise<OooRequestResponse> => {
140143

@@ -155,12 +158,25 @@ export const acknowledgeOOORequestController = async (
155158

156159
const response = await acknowledgeOOORequest(requestId, requestBody, superUserId);
157160

161+
if (response.error === REQUEST_DOES_NOT_EXIST) {
162+
return res.boom.notFound(REQUEST_DOES_NOT_EXIST);
163+
}
164+
if (response.error === INVALID_REQUEST_TYPE) {
165+
return res.boom.badRequest(INVALID_REQUEST_TYPE);
166+
}
167+
if (response.error === REQUEST_ALREADY_APPROVED) {
168+
return res.boom.conflict(REQUEST_ALREADY_APPROVED);
169+
}
170+
if (response.error === REQUEST_ALREADY_REJECTED) {
171+
return res.boom.conflict(REQUEST_ALREADY_REJECTED);
172+
}
173+
158174
return res.status(200).json({
159175
message: response.message,
160176
});
161177
}
162178
catch(error){
163-
logger.error(ERROR_WHILE_UPDATING_REQUEST, error);
164-
next(error);
179+
logger.error(ERROR_WHILE_ACKNOWLEDGING_REQUEST, error);
180+
return res.boom.badImplementation(ERROR_WHILE_ACKNOWLEDGING_REQUEST);
165181
}
166182
};

controllers/requests.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { createTaskRequestController } from "./taskRequestsv2";
1616
import { OnboardingExtensionCreateRequest, OnboardingExtensionResponse, UpdateOnboardingExtensionStateRequest } from "../types/onboardingExtension";
1717
import { createOnboardingExtensionRequestController, updateOnboardingExtensionRequestController, updateOnboardingExtensionRequestState } from "./onboardingExtension";
1818
import { UpdateOnboardingExtensionRequest } from "../types/onboardingExtension";
19-
import { NextFunction, Request } from "express";
19+
import { Request } from "express";
2020

2121
export const createRequestController = async (
2222
req: OooRequestCreateRequest | ExtensionRequestRequest | TaskRequestRequest | OnboardingExtensionCreateRequest,
@@ -120,12 +120,12 @@ export const getRequestsController = async (req: any, res: any) => {
120120
* @param {CustomResponse} res - The response object.
121121
* @returns {Promise<void>} Resolves or sends an error for invalid types.
122122
*/
123-
export const updateRequestBeforeAcknowledgedController = async (req: Request, res: CustomResponse, next: NextFunction) => {
123+
export const updateRequestBeforeAcknowledgedController = async (req: Request, res: CustomResponse) => {
124124
const type = req.body.type;
125125

126126
switch(type){
127127
case REQUEST_TYPE.OOO:
128-
await acknowledgeOOORequestController(req as AcknowledgeOOORequest, res as OooRequestResponse, next);
128+
await acknowledgeOOORequestController(req as AcknowledgeOOORequest, res as OooRequestResponse);
129129
break;
130130
case REQUEST_TYPE.ONBOARDING:
131131
await updateOnboardingExtensionRequestController(req as UpdateOnboardingExtensionRequest, res as OnboardingExtensionResponse);

middlewares/validators/oooRequests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const acknowledgeOOORequestsValidator = async (
7676
await schema.validateAsync(req.body, { abortEarly: false });
7777
next();
7878
} catch (error) {
79-
const errorMessages = error.details.map((detail:any) => detail.message);
79+
const errorMessages = error.details.map((detail:{message: string}) => detail.message);
8080
logger.error(`Error while validating request payload : ${errorMessages}`);
8181
return res.boom.badRequest(errorMessages);
8282
}

services/oooRequest.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { addLog } from "./logService";
22
import firestore from "../utils/firestore";
3-
import { NotFound, BadRequest } from "http-errors";
3+
import { BadRequest } from "http-errors";
44
import { logType } from "../constants/logs";
55
import {
66
REQUEST_STATE,
@@ -14,8 +14,8 @@ import {
1414
LOG_ACTION,
1515
INVALID_REQUEST_TYPE,
1616
} from "../constants/requests";
17-
import { getRequests, updateRequest } from "../models/requests";
18-
import { AcknowledgeOOORequestBody, OooStatusRequest } from "../types/oooRequest";
17+
import { updateRequest } from "../models/requests";
18+
import { AcknowledgeOOORequestBody } from "../types/oooRequest";
1919
import { addFutureStatus } from "../models/userStatus";
2020
const requestModel = firestore.collection("requests");
2121

@@ -40,23 +40,29 @@ export const validateOOOAcknowledgeRequest = async (
4040
{ requestId, type: requestType },
4141
{ message: INVALID_REQUEST_TYPE }
4242
);
43-
throw BadRequest(INVALID_REQUEST_TYPE);
43+
return {
44+
error: INVALID_REQUEST_TYPE
45+
};
4446
}
4547

4648
if (requestStatus === REQUEST_STATE.APPROVED) {
4749
await addLog(logType.REQUEST_ALREADY_APPROVED,
4850
{ oooRequestId: requestId },
4951
{ message: REQUEST_ALREADY_APPROVED }
5052
);
51-
throw BadRequest(REQUEST_ALREADY_APPROVED);
53+
return {
54+
error: REQUEST_ALREADY_APPROVED
55+
};
5256
}
5357

5458
if (requestStatus === REQUEST_STATE.REJECTED) {
5559
await addLog(logType.REQUEST_ALREADY_REJECTED,
5660
{ oooRequestId: requestId },
5761
{ message: REQUEST_ALREADY_REJECTED }
5862
);
59-
throw BadRequest(REQUEST_ALREADY_REJECTED);
63+
return {
64+
error: REQUEST_ALREADY_REJECTED
65+
};
6066
}
6167
} catch (error) {
6268
logger.error("Error while validating OOO acknowledge request", error);
@@ -69,7 +75,7 @@ export const validateOOOAcknowledgeRequest = async (
6975
*
7076
* @param {string} requestId - The ID of the OOO request to acknowledge.
7177
* @param {AcknowledgeOOORequestBody} body - The acknowledgement body containing acknowledging details.
72-
* @param {string} userId - The unique identifier of the superuser user.
78+
* @param {string} superUserId - The unique identifier of the superuser user.
7379
* @returns {Promise<object>} The acknowledged OOO request.
7480
* @throws {Error} Throws an error if an issue occurs during acknowledgment process.
7581
*/
@@ -86,12 +92,20 @@ export const acknowledgeOOORequest = async (
8692
{ oooRequestId: requestId },
8793
{ message: REQUEST_DOES_NOT_EXIST }
8894
);
89-
throw NotFound(REQUEST_DOES_NOT_EXIST);
95+
return {
96+
error: REQUEST_DOES_NOT_EXIST
97+
};
9098
}
9199

92100
const requestData = request.data();
93101

94-
await validateOOOAcknowledgeRequest(requestId, requestData.type, requestData.status);
102+
const validationResponse = await validateOOOAcknowledgeRequest(requestId, requestData.type, requestData.status);
103+
104+
if (validationResponse) {
105+
return {
106+
error: validationResponse.error
107+
};
108+
}
95109

96110
const requestResult = await updateRequest(requestId, body, superUserId, REQUEST_TYPE.OOO);
97111

@@ -118,18 +132,16 @@ export const acknowledgeOOORequest = async (
118132
await addLog(requestLog.type, requestLog.meta, requestLog.body);
119133

120134
if (requestResult.status === REQUEST_STATE.APPROVED) {
121-
if (requestData) {
122-
const { from, until, userId } = requestData;
123-
const userFutureStatusData = {
124-
requestId,
125-
state: REQUEST_TYPE.OOO,
126-
from,
127-
endsOn: until,
128-
userId,
129-
message: body.comment,
130-
};
131-
await addFutureStatus(userFutureStatusData);
132-
}
135+
const { from, until, userId } = requestData;
136+
const userFutureStatusData = {
137+
requestId,
138+
state: REQUEST_TYPE.OOO,
139+
from,
140+
endsOn: until,
141+
userId,
142+
message: body.comment,
143+
};
144+
await addFutureStatus(userFutureStatusData);
133145
}
134146

135147
return {

test/fixtures/oooRequest/oooRequest.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { REQUEST_STATE, REQUEST_TYPE } from "../../../constants/requests";
2+
// import { UserStatus } from "../../../types/userStatus";
23

34
export const createOooStatusRequests = {
45
type: "OOO",
@@ -19,6 +20,35 @@ export const validOooStatusRequests = {
1920
state: REQUEST_STATE.PENDING,
2021
};
2122

23+
// export const createdOOORequest = {
24+
// id: "Js7JnT6uRBLjGvSJM5X5",
25+
// type: validOooStatusRequests.type,
26+
// from: validOooStatusRequests.from,
27+
// until: validOooStatusRequests.until,
28+
// reason: validOooStatusRequests.reason,
29+
// status: "PENDING",
30+
// lastModifiedBy: null,
31+
// requestedBy: "suraj-maity-1",
32+
// userId: "jCqqOYCnm93mcmaYuSsQ",
33+
// comment: null
34+
// };
35+
36+
export const validUserCurrentStatus = {
37+
from: Date.now(),
38+
until: Date.now() + 1 * 24 * 60 * 60 * 1000,
39+
message: "",
40+
state: "ACTIVE",
41+
updatedAt: Date.now(),
42+
};
43+
44+
// export const testUserStatus: UserStatus = {
45+
// id: "wcl0ZLsnngKUNZY9GkCo",
46+
// data: {
47+
// currentStatus: validUserCurrentStatus
48+
// },
49+
// userStatusExists: true
50+
// };
51+
2252
export const invalidOooStatusRequests = {
2353
type: "OOO",
2454
from: Date.now() + 100000,
@@ -128,4 +158,4 @@ export const updateOooStatusRequest = [
128158
updatedAt: 1234567890,
129159
reason: "Approval granted.",
130160
},
131-
];
161+
];

0 commit comments

Comments
 (0)