@@ -5,12 +5,22 @@ import {
5
5
REQUEST_LOG_TYPE ,
6
6
REQUEST_STATE ,
7
7
USER_STATUS_NOT_FOUND ,
8
+ REQUEST_TYPE ,
9
+ REQUEST_ALREADY_APPROVED ,
10
+ REQUEST_ALREADY_REJECTED ,
11
+ REQUEST_APPROVED_SUCCESSFULLY ,
12
+ REQUEST_REJECTED_SUCCESSFULLY ,
13
+ INVALID_REQUEST_TYPE ,
8
14
} from "../constants/requests" ;
9
15
import { userState } from "../constants/userStatus" ;
10
- import { createRequest } from "../models/requests" ;
16
+ import { createRequest , getRequestById } from "../models/requests" ;
11
17
import { OooStatusRequest , OooStatusRequestBody } from "../types/oooRequest" ;
12
18
import { UserStatus } from "../types/userStatus" ;
13
19
import { addLog } from "./logService" ;
20
+ import { BadRequest , Conflict } from "http-errors" ;
21
+ import { updateRequest } from "../models/requests" ;
22
+ import { AcknowledgeOooRequestBody } from "../types/oooRequest" ;
23
+ import { addFutureStatus } from "../models/userStatus" ;
14
24
15
25
/**
16
26
* Validates the user status.
@@ -93,3 +103,101 @@ export const createOooRequest = async (
93
103
throw error ;
94
104
}
95
105
}
106
+
107
+ /**
108
+ * Validates an Out-Of-Office (OOO) acknowledge request
109
+ *
110
+ * @param {string } requestId - The unique identifier of the request.
111
+ * @param {string } requestType - The type of the request (expected to be 'OOO').
112
+ * @param {string } requestStatus - The current status of the request.
113
+ * @throws {Error } Throws an error if an issue occurs during validation.
114
+ */
115
+ export const validateOooAcknowledgeRequest = async (
116
+ requestType : string ,
117
+ requestStatus : string ,
118
+ ) => {
119
+
120
+ try {
121
+
122
+ if ( requestType !== REQUEST_TYPE . OOO ) {
123
+ throw new BadRequest ( INVALID_REQUEST_TYPE ) ;
124
+ }
125
+
126
+ if ( requestStatus === REQUEST_STATE . APPROVED ) {
127
+ throw new Conflict ( REQUEST_ALREADY_APPROVED ) ;
128
+ }
129
+
130
+ if ( requestStatus === REQUEST_STATE . REJECTED ) {
131
+ throw new Conflict ( REQUEST_ALREADY_REJECTED ) ;
132
+ }
133
+ } catch ( error ) {
134
+ logger . error ( "Error while validating OOO acknowledge request" , error ) ;
135
+ throw error ;
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Acknowledges an Out-of-Office (OOO) request
141
+ *
142
+ * @param {string } requestId - The ID of the OOO request to acknowledge.
143
+ * @param {AcknowledgeOooRequestBody } body - The acknowledgement body containing acknowledging details.
144
+ * @param {string } superUserId - The unique identifier of the superuser user.
145
+ * @returns {Promise<object> } The acknowledged OOO request.
146
+ * @throws {Error } Throws an error if an issue occurs during acknowledgment process.
147
+ */
148
+ export const acknowledgeOooRequest = async (
149
+ requestId : string ,
150
+ body : AcknowledgeOooRequestBody ,
151
+ superUserId : string ,
152
+ ) => {
153
+ try {
154
+ const requestData = await getRequestById ( requestId ) ;
155
+
156
+ await validateOooAcknowledgeRequest ( requestData . type , requestData . status ) ;
157
+
158
+ const requestResult = await updateRequest ( requestId , body , superUserId , REQUEST_TYPE . OOO ) ;
159
+
160
+ if ( "error" in requestResult ) {
161
+ throw new BadRequest ( requestResult . error ) ;
162
+ }
163
+
164
+ const [ acknowledgeLogType , returnMessage ] =
165
+ requestResult . status === REQUEST_STATE . APPROVED
166
+ ? [ REQUEST_LOG_TYPE . REQUEST_APPROVED , REQUEST_APPROVED_SUCCESSFULLY ]
167
+ : [ REQUEST_LOG_TYPE . REQUEST_REJECTED , REQUEST_REJECTED_SUCCESSFULLY ] ;
168
+
169
+ const requestLog = {
170
+ type : acknowledgeLogType ,
171
+ meta : {
172
+ requestId,
173
+ action : LOG_ACTION . UPDATE ,
174
+ userId : superUserId ,
175
+ } ,
176
+ body : requestResult ,
177
+ } ;
178
+
179
+ await addLog ( requestLog . type , requestLog . meta , requestLog . body ) ;
180
+
181
+ if ( requestResult . status === REQUEST_STATE . APPROVED ) {
182
+ await addFutureStatus ( {
183
+ requestId,
184
+ state : REQUEST_TYPE . OOO ,
185
+ from : requestData . from ,
186
+ endsOn : requestData . until ,
187
+ userId : requestData . userId ,
188
+ message : body . comment ,
189
+ } ) ;
190
+ }
191
+
192
+ return {
193
+ message : returnMessage ,
194
+ data : {
195
+ id : requestResult . id ,
196
+ ...requestResult ,
197
+ } ,
198
+ } ;
199
+ } catch ( error ) {
200
+ logger . error ( "Error while acknowledging OOO request" , error ) ;
201
+ throw error ;
202
+ }
203
+ }
0 commit comments