myc_core/domain/dtos/webhook/
mod.rs1mod responses;
2mod trigger;
3
4pub use responses::*;
5pub use trigger::*;
6
7use super::http_secret::HttpSecret;
8use crate::{domain::dtos::written_by::WrittenBy, models::AccountLifeCycle};
9
10use chrono::{DateTime, Local};
11use mycelium_base::utils::errors::MappedErrors;
12use serde::{Deserialize, Serialize};
13use utoipa::ToSchema;
14use uuid::Uuid;
15
16#[derive(Clone, Debug, Deserialize, Serialize, ToSchema)]
17#[serde(rename_all = "camelCase")]
18pub struct WebHook {
19 pub id: Option<Uuid>,
21
22 pub name: String,
24
25 pub description: Option<String>,
27
28 pub url: String,
30
31 pub trigger: WebHookTrigger,
33
34 pub is_active: bool,
36
37 pub created: DateTime<Local>,
39
40 pub created_by: Option<WrittenBy>,
46
47 pub updated: Option<DateTime<Local>>,
49
50 pub updated_by: Option<WrittenBy>,
56
57 secret: Option<HttpSecret>,
63}
64
65impl WebHook {
66 pub fn new(
67 name: String,
68 description: Option<String>,
69 url: String,
70 trigger: WebHookTrigger,
71 secret: Option<HttpSecret>,
72 created_by: Option<WrittenBy>,
73 ) -> Self {
74 Self {
75 id: None,
76 name,
77 description,
78 url,
79 trigger,
80 is_active: true,
81 created: Local::now(),
82 created_by,
83 updated: None,
84 updated_by: None,
85 secret,
86 }
87 }
88
89 pub async fn new_encrypted(
90 name: String,
91 description: Option<String>,
92 url: String,
93 trigger: WebHookTrigger,
94 secret: Option<HttpSecret>,
95 config: AccountLifeCycle,
96 created_by: Option<WrittenBy>,
97 ) -> Result<Self, MappedErrors> {
98 let encrypted_secret = match secret {
99 None => None,
100 Some(secret) => Some(secret.encrypt_me(config).await?),
101 };
102
103 Ok(Self {
104 id: None,
105 name,
106 description,
107 url,
108 trigger,
109 is_active: true,
110 created: Local::now(),
111 created_by,
112 updated: None,
113 updated_by: None,
114 secret: encrypted_secret,
115 })
116 }
117
118 pub fn redact_secret_token(&mut self) {
119 if let Some(secret) = &mut self.secret {
120 secret.redact_token();
121 }
122 }
123
124 pub fn get_secret(&self) -> Option<HttpSecret> {
125 self.secret.clone()
126 }
127
128 pub async fn set_secret(
129 &mut self,
130 secret: HttpSecret,
131 config: AccountLifeCycle,
132 updated_by: Option<WrittenBy>,
133 ) -> Result<(), MappedErrors> {
134 self.secret = Some(secret.encrypt_me(config).await?);
135 self.updated_by = updated_by;
136 Ok(())
137 }
138}