allow for sending notifications to single users
All checks were successful
CI/CD Pipeline / test (push) Successful in 9m28s
CI/CD Pipeline / deploy-staging (push) Has been skipped
CI/CD Pipeline / deploy-main (push) Has been skipped

This commit is contained in:
philipp 2024-05-21 18:57:24 +02:00
parent 830aa58e7b
commit 29fabb04b0
2 changed files with 63 additions and 9 deletions

View File

@ -4,6 +4,7 @@ use crate::model::{
role::Role, role::Role,
user::{AdminUser, User, UserWithDetails}, user::{AdminUser, User, UserWithDetails},
}; };
use itertools::Itertools;
use rocket::{ use rocket::{
form::Form, form::Form,
get, post, get, post,
@ -28,22 +29,39 @@ async fn index(
"loggedin_user", "loggedin_user",
&UserWithDetails::from_user(user.user, db).await, &UserWithDetails::from_user(user.user, db).await,
); );
let users: Vec<User> = User::all(db)
.await
.into_iter()
.filter(|u| u.last_access.is_some()) // Not useful to send notifications to people who are
// not logging in
.sorted_by_key(|u| u.name.clone())
.collect();
context.insert("roles", &Role::all(db).await); context.insert("roles", &Role::all(db).await);
context.insert("users", &users);
Template::render("admin/notification", context.into_json()) Template::render("admin/notification", context.into_json())
} }
#[derive(FromForm, Debug)] #[derive(FromForm, Debug)]
pub struct NotificationToSend { pub struct NotificationToSendGroup {
pub(crate) role_id: i32, pub(crate) role_id: i32,
pub(crate) category: String, pub(crate) category: String,
pub(crate) message: String, pub(crate) message: String,
} }
#[post("/notification", data = "<data>")] #[derive(FromForm, Debug)]
async fn send( pub struct NotificationToSendUser {
pub(crate) user_id: i32,
pub(crate) category: String,
pub(crate) message: String,
}
#[post("/notification/group", data = "<data>")]
async fn send_group(
db: &State<SqlitePool>, db: &State<SqlitePool>,
data: Form<NotificationToSend>, data: Form<NotificationToSendGroup>,
admin: AdminUser, admin: AdminUser,
) -> Flash<Redirect> { ) -> Flash<Redirect> {
let d = data.into_inner(); let d = data.into_inner();
@ -67,6 +85,32 @@ async fn send(
) )
} }
pub fn routes() -> Vec<Route> { #[post("/notification/user", data = "<data>")]
routes![index, send] async fn send_user(
db: &State<SqlitePool>,
data: Form<NotificationToSendUser>,
admin: AdminUser,
) -> Flash<Redirect> {
let d = data.into_inner();
Log::create(
db,
format!("{admin:?} trying to send this notification: {d:?}"),
)
.await;
let Some(user) = User::find_by_id(db, d.user_id).await else {
return Flash::error(Redirect::to("/admin/notification"), "User gibt's ned");
};
Notification::create(db, &user, &d.message, &d.category, None).await;
Log::create(db, "Notification successfully sent".into()).await;
Flash::success(
Redirect::to("/admin/notification"),
"Nachricht ausgeschickt",
)
}
pub fn routes() -> Vec<Route> {
routes![index, send_user, send_group]
} }

View File

@ -3,18 +3,28 @@
{% extends "base" %} {% extends "base" %}
{% block content %} {% block content %}
<div class="max-w-screen-lg w-full dark:text-white"> <div class="max-w-screen-lg w-full dark:text-white">
<h1 class="h1">Nachricht</h1> <h1 class="h1">Nachricht senden</h1>
<div class="grid "> <div class="grid ">
<div class="bg-white dark:bg-primary-900 text-black dark:text-white rounded-md block shadow mt-5" <div class="bg-white dark:bg-primary-900 text-black dark:text-white rounded-md block shadow mt-5"
role="alert"> role="alert">
<h2 class="h2">Nachricht senden</h2> <h2 class="h2">Gruppe</h2>
<form action="/admin/notification" method="post" class="grid gap-3 p-3"> <form action="/admin/notification/group" method="post" class="grid gap-3 p-3">
{{ macros::select(label="Gruppe", data=roles, name="role_id") }} {{ macros::select(label="Gruppe", data=roles, name="role_id") }}
{{ macros::input(label="Überschrift", name="category", type="text", required=true) }} {{ macros::input(label="Überschrift", name="category", type="text", required=true) }}
{{ macros::input(label="Nachricht", name="message", type="text", required=true) }} {{ macros::input(label="Nachricht", name="message", type="text", required=true) }}
<input type="submit" class="btn btn-primary" value="Abschicken" /> <input type="submit" class="btn btn-primary" value="Abschicken" />
</form> </form>
</div> </div>
<div class="bg-white dark:bg-primary-900 text-black dark:text-white rounded-md block shadow mt-5"
role="alert">
<h2 class="h2">Person</h2>
<form action="/admin/notification/user" method="post" class="grid gap-3 p-3">
{{ macros::select(label="Person", data=users, name="user_id") }}
{{ macros::input(label="Überschrift", name="category", type="text", required=true) }}
{{ macros::input(label="Nachricht", name="message", type="text", required=true) }}
<input type="submit" class="btn btn-primary" value="Abschicken" />
</form>
</div>
</div> </div>
</div> </div>
{% endblock content %} {% endblock content %}