Implemented mail settings.
This commit is contained in:
+29
-7
@@ -538,19 +538,41 @@ pub struct ResSumWithItems {
|
||||
pub reservations: Vec<ResWithProperty>
|
||||
}
|
||||
|
||||
/*
|
||||
#[derive(Clone, Serialize, Deserialize, Debug, Default)]
|
||||
#[cfg_attr(feature = "ssr", derive(sqlx::Type))]
|
||||
#[cfg_attr(feature = "ssr", sqlx(type_name = "message_type"))]
|
||||
pub enum MessageType {
|
||||
#[default]
|
||||
NewReservation,
|
||||
NewReservationCust,
|
||||
ReservationApp,
|
||||
ReservationCanceled,
|
||||
}
|
||||
|
||||
pub struct Message {
|
||||
id: u16,
|
||||
msg_type: MessageType,
|
||||
subject: String,
|
||||
text: String,
|
||||
impl Display for MessageType {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", match self {
|
||||
MessageType::NewReservation => {"NewReservation"}
|
||||
MessageType::NewReservationCust => {"NewReservationCust"}
|
||||
MessageType::ReservationApp => {"ReservationApp"}
|
||||
MessageType::ReservationCanceled => {"ReservationCanceled"}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
#[derive(Clone, Serialize, Deserialize, Debug, Default, Validate)]
|
||||
#[cfg_attr(feature = "ssr", derive(sqlx::FromRow))]
|
||||
pub struct Message {
|
||||
id: i32,
|
||||
pub msg_type: MessageType,
|
||||
#[validate(length(min = 1,message = "Enter mail subject"))]
|
||||
pub subject: String,
|
||||
#[validate(length(min = 1,message = "Enter text"))]
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub fn id(&self) -> i32 {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
use cfg_if::cfg_if;
|
||||
use leptos::*;
|
||||
use validator::Validate;
|
||||
use crate::backend::data::{ApiResponse, Message, MessageType};
|
||||
use crate::components::data_form::ForValidation;
|
||||
|
||||
cfg_if! { if #[cfg(feature = "ssr")] {
|
||||
use sqlx::{PgPool, query, query_as};
|
||||
use sqlx::Error;
|
||||
use crate::backend::get_pool;
|
||||
use crate::error::AppError;
|
||||
use log::info;
|
||||
|
||||
pub async fn message_for_type(msg_type: &MessageType, pool: &PgPool) -> Result<Message, Error> {
|
||||
Ok(query_as::<_, Message>("SELECT * FROM message WHERE msg_type = $1")
|
||||
.bind(msg_type)
|
||||
.fetch_one(pool)
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub async fn init_message(msg_type: &MessageType, pool: &PgPool) -> Result<(), Error> {
|
||||
query("INSERT INTO message(msg_type, subject, text) VALUES($1, '', '')")
|
||||
.bind(msg_type)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn check_messages(pool: &PgPool) -> Result<(), AppError> {
|
||||
let types = [
|
||||
MessageType::NewReservation,
|
||||
MessageType::NewReservationCust,
|
||||
MessageType::ReservationApp,
|
||||
MessageType::ReservationCanceled];
|
||||
|
||||
for msg_type in types {
|
||||
let msg = message_for_type(&msg_type, pool).await;
|
||||
if let Err(e) = msg {
|
||||
if matches!(e, Error::RowNotFound) {
|
||||
info!("Creating initial message for type {:?}", msg_type);
|
||||
init_message(&msg_type, pool).await?;
|
||||
} else {
|
||||
return Err(e.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}}
|
||||
|
||||
#[server]
|
||||
pub async fn get_message(msg_type: MessageType) -> Result<Message, ServerFnError> {
|
||||
let pool = get_pool().await?;
|
||||
|
||||
Ok(message_for_type(&msg_type, &pool).await?)
|
||||
}
|
||||
|
||||
#[server]
|
||||
pub async fn update_message(message: Message) -> Result<ApiResponse<()>, ServerFnError> {
|
||||
use crate::perm_check;
|
||||
|
||||
perm_check!(is_admin);
|
||||
let pool = get_pool().await?;
|
||||
|
||||
query("UPDATE message SET subject = $1, text = $2 WHERE msg_type = $3")
|
||||
.bind(message.subject)
|
||||
.bind(message.text)
|
||||
.bind(message.msg_type)
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
|
||||
Ok(ApiResponse::Data(()))
|
||||
}
|
||||
|
||||
impl ForValidation for UpdateMessage {
|
||||
fn entity(&self) -> &dyn Validate {
|
||||
&self.message
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ pub mod opening_hours;
|
||||
pub mod property;
|
||||
pub mod reservation;
|
||||
pub mod customer;
|
||||
pub mod mail;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! perm_check {
|
||||
|
||||
Reference in New Issue
Block a user