| 221 | } |
| 222 | |
| 223 | fn validate_deduplication_key( |
| 224 | deduplication_key: &Option<String>, |
| 225 | status: &RequestStatus, |
| 226 | ) -> ModelValidatorResult<RequestError> { |
| 227 | // Only check for duplicates when a request is created |
| 228 | let should_be_checked_for_duplicates = match status { |
| 229 | RequestStatus::Created => true, |
| 230 | RequestStatus::Approved |
| 231 | | RequestStatus::Rejected |
| 232 | | RequestStatus::Scheduled { .. } |
| 233 | | RequestStatus::Cancelled { .. } |
| 234 | | RequestStatus::Processing { .. } |
| 235 | | RequestStatus::Completed { .. } |
| 236 | | RequestStatus::Failed { .. } => false, |
| 237 | }; |
| 238 | |
| 239 | if let Some(deduplication_key) = deduplication_key { |
| 240 | if deduplication_key.len() > Request::MAX_DEDUPLICATION_KEY_LEN as usize { |
| 241 | return Err(RequestError::ValidationError { |
| 242 | info: format!( |
| 243 | "The deduplication key length exceeds the maximum allowed: {}", |
| 244 | Request::MAX_DEDUPLICATION_KEY_LEN |
| 245 | ), |
| 246 | }); |
| 247 | } |
| 248 | |
| 249 | if !should_be_checked_for_duplicates { |
| 250 | return Ok(()); |
| 251 | } |
| 252 | |
| 253 | if deduplication_key.trim().is_empty() { |
| 254 | return Err(RequestError::ValidationError { |
| 255 | info: "The deduplication key must not be empty".to_owned(), |
| 256 | }); |
| 257 | } |
| 258 | let is_not_unique = REQUEST_REPOSITORY |
| 259 | .find_by_deduplication_key(deduplication_key.clone()) |
| 260 | .iter() |
| 261 | .any(|request| { |
| 262 | matches!( |
| 263 | request.status, |
| 264 | RequestStatus::Created |
| 265 | | RequestStatus::Scheduled { .. } |
| 266 | | RequestStatus::Processing { .. } |
| 267 | | RequestStatus::Approved |
| 268 | ) |
| 269 | }); |
| 270 | if is_not_unique { |
| 271 | return Err(RequestError::ValidationError { |
| 272 | info: "A request with the same deduplication key already exists".to_owned(), |
| 273 | }); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | Ok(()) |
| 278 | } |
| 279 | |
| 280 | fn validate_requested_by(requested_by: &UserId) -> ModelValidatorResult<RequestError> { |