Skip to main content

stormchaser_api/db/
webhooks.rs

1use sqlx::PgPool;
2use stormchaser_model::event_rules::WebhookConfig;
3use stormchaser_model::WebhookId;
4
5/// Creates a new webhook configuration.
6pub async fn create_webhook(
7    pool: &PgPool,
8    id: WebhookId,
9    name: &str,
10    description: &Option<String>,
11    source_type: &str,
12    secret_token: &Option<String>,
13) -> Result<(), sqlx::Error> {
14    sqlx::query(
15        "INSERT INTO webhooks (id, name, description, source_type, secret_token) VALUES ($1, $2, $3, $4, $5)"
16    )
17    .bind(id)
18    .bind(name)
19    .bind(description)
20    .bind(source_type)
21    .bind(secret_token)
22    .execute(pool)
23    .await?;
24    Ok(())
25}
26
27/// Retrieves all configured webhooks
28pub async fn list_webhooks(pool: &PgPool) -> Result<Vec<WebhookConfig>, sqlx::Error> {
29    sqlx::query_as("SELECT * FROM webhooks ORDER BY created_at DESC")
30        .fetch_all(pool)
31        .await
32}
33
34/// Retrieves a specific webhook by ID.
35/// Get webhook.
36pub async fn get_webhook(
37    pool: &PgPool,
38    id: WebhookId,
39) -> Result<Option<WebhookConfig>, sqlx::Error> {
40    sqlx::query_as("SELECT * FROM webhooks WHERE id = $1")
41        .bind(id)
42        .fetch_optional(pool)
43        .await
44}
45
46/// Retrieves an active webhook by ID.
47/// Get active webhook.
48pub async fn get_active_webhook(
49    pool: &PgPool,
50    id: WebhookId,
51) -> Result<Option<WebhookConfig>, sqlx::Error> {
52    sqlx::query_as("SELECT * FROM webhooks WHERE id = $1 AND is_active = TRUE")
53        .bind(id)
54        .fetch_optional(pool)
55        .await
56}
57
58/// Updates an existing webhook configuration.
59pub async fn update_webhook(
60    pool: &PgPool,
61    id: WebhookId,
62    name: Option<String>,
63    description: Option<Option<String>>,
64    source_type: Option<String>,
65    secret_token: Option<Option<String>>,
66    is_active: Option<bool>,
67) -> Result<(), sqlx::Error> {
68    let mut tx = pool.begin().await?;
69
70    let mut webhook =
71        match sqlx::query_as::<_, WebhookConfig>("SELECT * FROM webhooks WHERE id = $1")
72            .bind(id)
73            .fetch_optional(&mut *tx)
74            .await?
75        {
76            Some(w) => w,
77            None => return Err(sqlx::Error::RowNotFound),
78        };
79
80    if let Some(n) = name {
81        webhook.name = n;
82    }
83    if let Some(d) = description {
84        webhook.description = d;
85    }
86    if let Some(st) = source_type {
87        webhook.source_type = st;
88    }
89    if let Some(t) = secret_token {
90        webhook.secret_token = t;
91    }
92    if let Some(a) = is_active {
93        webhook.is_active = a;
94    }
95
96    sqlx::query(
97        "UPDATE webhooks SET name = $1, description = $2, source_type = $3, secret_token = $4, is_active = $5, updated_at = NOW() WHERE id = $6",
98    )
99    .bind(webhook.name)
100    .bind(webhook.description)
101    .bind(webhook.source_type)
102    .bind(webhook.secret_token)
103    .bind(webhook.is_active)
104    .bind(id)
105    .execute(&mut *tx)
106    .await?;
107
108    tx.commit().await?;
109    Ok(())
110}
111
112/// Deletes a webhook from the database.
113/// Delete webhook.
114pub async fn delete_webhook(pool: &PgPool, id: WebhookId) -> Result<(), sqlx::Error> {
115    sqlx::query("DELETE FROM webhooks WHERE id = $1")
116        .bind(id)
117        .execute(pool)
118        .await?;
119    Ok(())
120}
121
122/// Inserts a new webhook.
123pub async fn insert_webhook(
124    pool: &PgPool,
125    id: WebhookId,
126    name: &str,
127    description: &Option<String>,
128    source_type: &str,
129    secret_token: &Option<String>,
130) -> Result<(), sqlx::Error> {
131    sqlx::query(
132        "INSERT INTO webhooks (id, name, description, source_type, secret_token) VALUES ($1, $2, $3, $4, $5)",
133    )
134    .bind(id)
135    .bind(name)
136    .bind(description)
137    .bind(source_type)
138    .bind(secret_token)
139    .execute(pool)
140    .await?;
141    Ok(())
142}