generated from RealDevSquad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathoooRequest.ts
More file actions
94 lines (87 loc) · 2.84 KB
/
oooRequest.ts
File metadata and controls
94 lines (87 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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 (
userId: string,
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;
}
}
/**
* 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,
requestedBy: 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;
}
}