myc_core/domain/dtos/webhook/
mod.rs

1mod responses;
2mod trigger;
3
4pub use responses::*;
5pub use trigger::*;
6
7use super::http_secret::HttpSecret;
8use crate::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    /// The webhook id
20    pub id: Option<Uuid>,
21
22    /// The webhook name
23    pub name: String,
24
25    /// The webhook description
26    pub description: Option<String>,
27
28    /// The webhook url
29    pub url: String,
30
31    /// The webhook trigger
32    pub trigger: WebHookTrigger,
33
34    /// The webhook is active
35    pub is_active: bool,
36
37    /// The webhook created date
38    pub created: DateTime<Local>,
39
40    /// The webhook updated date
41    pub updated: Option<DateTime<Local>>,
42
43    /// The webhook secret
44    ///
45    /// Its important to note that the secret should be encrypted in the
46    /// database and redacted on the response.
47    ///
48    secret: Option<HttpSecret>,
49}
50
51impl WebHook {
52    pub fn new(
53        name: String,
54        description: Option<String>,
55        url: String,
56        trigger: WebHookTrigger,
57        secret: Option<HttpSecret>,
58    ) -> Self {
59        Self {
60            id: None,
61            name,
62            description,
63            url,
64            trigger,
65            is_active: true,
66            created: Local::now(),
67            updated: None,
68            secret,
69        }
70    }
71
72    pub async fn new_encrypted(
73        name: String,
74        description: Option<String>,
75        url: String,
76        trigger: WebHookTrigger,
77        secret: Option<HttpSecret>,
78        config: AccountLifeCycle,
79    ) -> Result<Self, MappedErrors> {
80        let encrypted_secret = match secret {
81            None => None,
82            Some(secret) => Some(secret.encrypt_me(config).await?),
83        };
84
85        Ok(Self {
86            id: None,
87            name,
88            description,
89            url,
90            trigger,
91            is_active: true,
92            created: Local::now(),
93            updated: None,
94            secret: encrypted_secret,
95        })
96    }
97
98    pub fn redact_secret_token(&mut self) {
99        if let Some(secret) = &mut self.secret {
100            secret.redact_token();
101        }
102    }
103
104    pub fn get_secret(&self) -> Option<HttpSecret> {
105        self.secret.clone()
106    }
107
108    pub fn set_secret(&mut self, secret: HttpSecret) {
109        self.secret = Some(secret);
110    }
111}