1use std::collections::BTreeMap;
4
5use laravel::Enum;
6use laravel::db::Json;
7use laravel::model::model;
8use laravel::time::Timestamp;
9use serde::{Deserialize, Serialize};
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Enum)]
13#[serde(rename_all = "snake_case")]
14#[non_exhaustive]
15pub enum Kind {
16 Mail,
18 Document,
20 Notification,
22}
23
24impl Kind {
25 #[must_use]
27 pub fn as_str(self) -> &'static str {
28 match self {
29 Kind::Mail => "mail",
30 Kind::Document => "document",
31 Kind::Notification => "notification",
32 }
33 }
34}
35
36#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
38pub struct Content {
39 #[serde(default)]
41 pub subject: String,
42 #[serde(default)]
44 pub body: String,
45}
46
47impl Content {
48 #[must_use]
50 pub fn new(subject: impl Into<String>, body: impl Into<String>) -> Self {
51 Self {
52 subject: subject.into(),
53 body: body.into(),
54 }
55 }
56}
57
58#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
64#[serde(default, deny_unknown_fields)]
65pub struct MailSettings {
66 pub from: Option<String>,
69 pub reply_to: Option<String>,
71 pub to: Vec<String>,
73 pub cc: Vec<String>,
75 pub bcc: Vec<String>,
77 pub attach_documents: Vec<i64>,
79 pub layout: Option<String>,
81}
82
83#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
85#[serde(rename_all = "snake_case")]
86#[non_exhaustive]
87pub enum DocumentFormat {
88 #[default]
90 Pdf,
91 Html,
93}
94
95#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
97#[serde(rename_all = "snake_case")]
98pub enum Orientation {
99 #[default]
101 Portrait,
102 Landscape,
104}
105
106#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
108#[serde(default, deny_unknown_fields)]
109pub struct Margins {
110 pub top: f32,
112 pub right: f32,
114 pub bottom: f32,
116 pub left: f32,
118}
119
120impl Default for Margins {
121 fn default() -> Self {
122 Self::all(15.0)
123 }
124}
125
126impl Margins {
127 #[must_use]
129 pub fn all(millimeters: f32) -> Self {
130 Self {
131 top: millimeters,
132 right: millimeters,
133 bottom: millimeters,
134 left: millimeters,
135 }
136 }
137}
138
139#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
141#[serde(default, deny_unknown_fields)]
142pub struct DocumentSettings {
143 pub format: DocumentFormat,
145 pub paper: String,
147 pub orientation: Orientation,
149 pub filename: String,
152 pub layout: Option<String>,
154 pub margins: Margins,
156 pub font_family: Option<String>,
158 pub font_size: Option<f32>,
160 pub line_height: Option<f32>,
162}
163
164impl Default for DocumentSettings {
165 fn default() -> Self {
166 Self {
167 format: DocumentFormat::Pdf,
168 paper: "a4".to_owned(),
169 orientation: Orientation::Portrait,
170 filename: "document".to_owned(),
171 layout: None,
172 margins: Margins::default(),
173 font_family: None,
174 font_size: None,
175 line_height: None,
176 }
177 }
178}
179
180#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
182#[serde(default, deny_unknown_fields)]
183pub struct NotificationSettings {
184 pub channel: String,
187}
188
189#[derive(Debug, Clone, PartialEq)]
191#[non_exhaustive]
192pub enum Settings {
193 Mail(MailSettings),
195 Document(DocumentSettings),
197 Notification(NotificationSettings),
199}
200
201impl Settings {
202 #[must_use]
204 pub fn default_for(kind: Kind) -> Self {
205 match kind {
206 Kind::Mail => Settings::Mail(MailSettings::default()),
207 Kind::Document => Settings::Document(DocumentSettings::default()),
208 Kind::Notification => Settings::Notification(NotificationSettings::default()),
209 }
210 }
211
212 pub fn from_json(kind: Kind, value: &serde_json::Value) -> Result<Self, serde_json::Error> {
218 let value = if value.is_null() {
219 serde_json::json!({})
220 } else {
221 value.clone()
222 };
223 Ok(match kind {
224 Kind::Mail => Settings::Mail(serde_json::from_value(value)?),
225 Kind::Document => Settings::Document(serde_json::from_value(value)?),
226 Kind::Notification => Settings::Notification(serde_json::from_value(value)?),
227 })
228 }
229
230 #[must_use]
232 pub fn to_json(&self) -> serde_json::Value {
233 match self {
234 Settings::Mail(settings) => serde_json::to_value(settings),
235 Settings::Document(settings) => serde_json::to_value(settings),
236 Settings::Notification(settings) => serde_json::to_value(settings),
237 }
238 .unwrap_or_default()
239 }
240
241 #[must_use]
243 pub fn kind(&self) -> Kind {
244 match self {
245 Settings::Mail(_) => Kind::Mail,
246 Settings::Document(_) => Kind::Document,
247 Settings::Notification(_) => Kind::Notification,
248 }
249 }
250}
251
252#[model(table = "template_engine_templates")]
258pub struct Template {
259 pub id: i64,
261 pub type_key: String,
263 pub kind: Kind,
265 pub name: String,
267 #[default]
269 pub is_active: bool,
270 pub content: Json<BTreeMap<String, Content>>,
272 pub settings: serde_json::Value,
274 pub created_at: Option<Timestamp>,
276 pub updated_at: Option<Timestamp>,
278}
279
280impl Template {
281 pub fn typed_settings(&self) -> laravel::Result<Settings> {
287 Settings::from_json(self.kind, &self.settings)
288 .map_err(|e| laravel::Error::msg(format!("the settings of template {} are invalid: {e}", self.id)))
289 }
290
291 pub fn to_input(&self) -> laravel::Result<TemplateInput> {
298 Ok(TemplateInput {
299 type_key: self.type_key.clone(),
300 name: self.name.clone(),
301 is_active: self.is_active,
302 content: self.content.0.clone(),
303 settings: self.typed_settings()?,
304 })
305 }
306}
307
308#[derive(Debug, Clone, PartialEq)]
310pub struct TemplateInput {
311 pub type_key: String,
313 pub name: String,
315 pub is_active: bool,
317 pub content: BTreeMap<String, Content>,
319 pub settings: Settings,
321}
322
323impl TemplateInput {
324 #[must_use]
326 pub fn mail(type_key: impl Into<String>, name: impl Into<String>) -> Self {
327 Self::new(type_key, name, Settings::Mail(MailSettings::default()))
328 }
329
330 #[must_use]
332 pub fn document(type_key: impl Into<String>, name: impl Into<String>) -> Self {
333 Self::new(type_key, name, Settings::Document(DocumentSettings::default()))
334 }
335
336 #[must_use]
338 pub fn notification(type_key: impl Into<String>, name: impl Into<String>, channel: impl Into<String>) -> Self {
339 let settings = NotificationSettings {
340 channel: channel.into(),
341 };
342 Self::new(type_key, name, Settings::Notification(settings))
343 }
344
345 fn new(type_key: impl Into<String>, name: impl Into<String>, settings: Settings) -> Self {
346 Self {
347 type_key: type_key.into(),
348 name: name.into(),
349 is_active: true,
350 content: BTreeMap::new(),
351 settings,
352 }
353 }
354
355 #[must_use]
357 pub fn content(mut self, locale: impl Into<String>, subject: impl Into<String>, body: impl Into<String>) -> Self {
358 self.content.insert(locale.into(), Content::new(subject, body));
359 self
360 }
361
362 #[must_use]
364 pub fn kind(&self) -> Kind {
365 self.settings.kind()
366 }
367}