Add admin changes to calendar requests
This commit is contained in:
parent
e59f849abd
commit
50bc1f4eb5
|
|
@ -525,6 +525,69 @@ pub async fn update_event(
|
||||||
eventstring
|
eventstring
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn calendar_update_event_state(
|
||||||
|
State(db_pool): State<PgPool>,
|
||||||
|
Extension(user_data): Extension<Option<AccountData>>,
|
||||||
|
Extension(rbac): Extension<RbacService>,
|
||||||
|
request: axum::http::Request<axum::body::Body>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
// Is the user logged in?
|
||||||
|
let logged_in = user_data.is_some();
|
||||||
|
|
||||||
|
// Set default events
|
||||||
|
let mut eventstring: String = "[]".to_string();
|
||||||
|
|
||||||
|
if logged_in {
|
||||||
|
// Extract the user data.
|
||||||
|
let _user = user_data.as_ref().unwrap().clone();
|
||||||
|
let userid = user_data.as_ref().map(|s| s.id.clone()).unwrap_or_default();
|
||||||
|
let personid = user_data
|
||||||
|
.as_ref()
|
||||||
|
.map(|s| s.person_id.clone())
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
if rbac.has_permission(userid, "calendar:*:*").await {
|
||||||
|
let (_parts, body) = request.into_parts();
|
||||||
|
let bytes = axum::body::to_bytes(body, usize::MAX).await.unwrap();
|
||||||
|
let body_str = String::from_utf8(bytes.to_vec()).unwrap();
|
||||||
|
|
||||||
|
let v: Value = serde_json::from_str(&body_str).unwrap();
|
||||||
|
|
||||||
|
// Convert calendar id to UUID
|
||||||
|
let calendar_event_id = Uuid::parse_str(v["id"].as_str().unwrap()).unwrap();
|
||||||
|
|
||||||
|
let event = sqlx::query_scalar::<_, uuid::Uuid>(
|
||||||
|
r#"with cet as (select id from calendar_event_types where name = $1 and state = $2)
|
||||||
|
update calendar_events
|
||||||
|
set updated_by = $3,
|
||||||
|
updated_at = now(),
|
||||||
|
event_type_id = cet.id
|
||||||
|
from cet
|
||||||
|
where calendar_events.id = $4
|
||||||
|
returning calendar_events.id"#
|
||||||
|
)
|
||||||
|
.bind(v["eventType"].as_str().unwrap())
|
||||||
|
.bind(v["state"].as_str().unwrap())
|
||||||
|
.bind(personid)
|
||||||
|
.bind(calendar_event_id)
|
||||||
|
.fetch_one(&db_pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
(
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
format!("Error creating event: {}", e),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
let event_id = event.clone();
|
||||||
|
|
||||||
|
eventstring = get_event(event_id.unwrap(), &db_pool).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
eventstring
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "newevent.html")]
|
#[template(path = "newevent.html")]
|
||||||
struct EventTemplate {
|
struct EventTemplate {
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ use wishlist::{
|
||||||
user_wishlist_returned_item, user_wishlist_save_item, wishlists,
|
user_wishlist_returned_item, user_wishlist_save_item, wishlists,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::calendar::update_event;
|
use crate::calendar::{calendar_update_event_state, update_event};
|
||||||
|
|
||||||
//use email::send_emails;
|
//use email::send_emails;
|
||||||
|
|
||||||
|
|
@ -98,6 +98,7 @@ async fn main() {
|
||||||
.route("/calendar/newevent", get(new_event))
|
.route("/calendar/newevent", get(new_event))
|
||||||
.route("/calendar/newrequest", post(new_request))
|
.route("/calendar/newrequest", post(new_request))
|
||||||
.route("/calendar/updaterequest", post(update_event))
|
.route("/calendar/updaterequest", post(update_event))
|
||||||
|
.route("/calendar/updateeventstate", post(calendar_update_event_state))
|
||||||
// Wishlist
|
// Wishlist
|
||||||
.route("/wishlists", get(wishlists))
|
.route("/wishlists", get(wishlists))
|
||||||
.route("/userwishlist/{user_id}", get(user_wishlist))
|
.route("/userwishlist/{user_id}", get(user_wishlist))
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,11 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="eventStart">Starting</label>
|
<label for="eventTitle">Reservation for</label>
|
||||||
|
<input type="text" id="eventTitle" class="form-control" placeholder="Reservation for">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="eventStart">Arriving</label>
|
||||||
<input type="date" class="form-control" id="eventStart" required>
|
<input type="date" class="form-control" id="eventStart" required>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
|
@ -47,9 +51,29 @@
|
||||||
<h5 class="modal-title">Edit Event</h5>
|
<h5 class="modal-title">Edit Event</h5>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
<input type="text" id="eventEditTitle" class="form-control" placeholder="Event Title">
|
<div class="form-group">
|
||||||
<input type="date" id="eventEditStart" class="form-control" placeholder="Start">
|
<label for="eventEditTitle">Reservation for</label>
|
||||||
<input type="date" id="eventEditEnd" class="form-control" placeholder="End">
|
<input type="text" id="eventEditTitle" class="form-control" placeholder="Event Title">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="eventEditEnd">Arriving</label>
|
||||||
|
<input type="date" id="eventEditStart" class="form-control" placeholder="Start">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="eventEditEnd">Leaving</label>
|
||||||
|
<input type="date" id="eventEditEnd" class="form-control" placeholder="End">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% for user_role in user_roles %}
|
||||||
|
{% if user_role.role_name == "admin" %}
|
||||||
|
<h5>Actions</h5>
|
||||||
|
<ul>
|
||||||
|
<button type="button" id="eventApprove" onclick="updateEventState('Approved')">Approve</button>
|
||||||
|
<button type="button" id="eventReject" onclick="updateEventState('Rejected')">Reject</button>
|
||||||
|
<button type="button" id="eventConfirm" onclick="updateEventState('Confirmed')">Confirm</button>
|
||||||
|
</ul>
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<button type="button" class="btn btn-secondary" id="closeEventEdit"
|
<button type="button" class="btn btn-secondary" id="closeEventEdit"
|
||||||
|
|
@ -194,5 +218,34 @@
|
||||||
$('#eventEditModal').modal('hide');
|
$('#eventEditModal').modal('hide');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function updateEventState(state) {
|
||||||
|
var updatedEvent = {
|
||||||
|
id: window.calEvent.id,
|
||||||
|
eventType: 'Reservation',
|
||||||
|
state: state
|
||||||
|
};
|
||||||
|
|
||||||
|
// Save the updates to the record
|
||||||
|
fetch('/calendar/updateeventstate', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(updatedEvent)
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
if (!response.ok) throw new Error('Network response was not ok');
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
|
.then(data => {
|
||||||
|
// Update the original event object
|
||||||
|
window.calEvent.setProp('backgroundColor', data.backgroundColor);
|
||||||
|
|
||||||
|
e.target.reset();
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error('Error creating event:', error);
|
||||||
|
//alert('An error occurred while creating the event.');
|
||||||
|
});
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
{% endblock scripts %}
|
{% endblock scripts %}
|
||||||
Loading…
Reference in New Issue