fix: user can create availability for partly existant time slot

This commit is contained in:
Max Hohlfeld 2025-08-17 23:05:33 +02:00
parent c2bc0218f4
commit af7f8a8cc6
2 changed files with 27 additions and 4 deletions

View File

@ -61,13 +61,14 @@ fn time_is_not_already_made_available(
));
}
let free_block_found_for_start = free_slots.iter().any(|s| s.0 <= *start && s.1 >= *start);
let free_block_found_for_end = free_slots.iter().any(|s| s.0 <= *end && s.1 >= *end);
let free_block_found = free_slots
.iter()
.any(|s| s.0 <= *start && *start <= s.1 && s.0 <= *end && *end <= s.1);
let is_already_present_as_is = existing_availabilities
.iter()
.any(|a| a.start == *start && a.end == *end);
if !free_block_found_for_start || !free_block_found_for_end || is_already_present_as_is {
if !free_block_found || is_already_present_as_is {
return Err(AsyncValidateError::new(
"Verfügbarkeit kann nicht erstellt werden, da eine vorhandene Verfügbarkeit überschnitten würde.",
));

View File

@ -131,7 +131,7 @@ mod tests {
}
#[db_test]
async fn cant_create_availability_for_myself_when_time_slot_already_present(
async fn cant_create_availability_for_myself_when_time_slot_already_present_exactly(
context: &DbTestContext,
) {
let app = context.app().await;
@ -151,6 +151,28 @@ mod tests {
assert_eq!(StatusCode::UNPROCESSABLE_ENTITY, response.status());
}
#[db_test]
async fn cant_create_availability_for_myself_when_time_slot_already_present_partly(
context: &DbTestContext,
) {
let app = context.app().await;
let config = RequestConfig::new("/availability/new");
create_test_login_user(&context.db_pool, &config).await;
let date = Faker.fake();
let changeset = AvailabilityChangeset::create_for_test(&date, 10, 20);
Availability::create(&context.db_pool, 1, &changeset)
.await
.unwrap();
let mut form = create_form(&date, 10, 20, None);
form.enddate = form.startdate.succ_opt().unwrap();
let response = test_post(app, &config, Some(form)).await;
assert_eq!(StatusCode::UNPROCESSABLE_ENTITY, response.status());
}
#[db_test]
async fn create_new_updates_existing_when_time_slots_connect(context: &DbTestContext) {
let app = context.app().await;