Skip to content

brokkr-broker::dal::webhook_subscriptions

Rust

Data Access Layer for WebhookSubscription operations.

This module provides functionality to interact with the webhook_subscriptions table. It includes methods for creating, updating, deleting, and querying webhook subscriptions.

pub

Data Access Layer for WebhookSubscription operations.

Name Type Description
dal &'a DAL Reference to the main DAL instance.

pub

fn create(
&self,
new_subscription: &NewWebhookSubscription,
) -> Result<WebhookSubscription, diesel::result::Error>

Creates a new webhook subscription.

Parameters:

Name Type Description
new_subscription &NewWebhookSubscription The subscription to create.

Returns:

Returns the created WebhookSubscription record.

Source
pub fn create(
&self,
new_subscription: &NewWebhookSubscription,
) -> Result<WebhookSubscription, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
diesel::insert_into(webhook_subscriptions::table)
.values(new_subscription)
.get_result(conn)
}

pub

fn get(
&self,
id: Uuid,
) -> Result<Option<WebhookSubscription>, diesel::result::Error>

Gets a webhook subscription by ID.

Parameters:

Name Type Description
id Uuid The subscription UUID.

Returns:

Returns the subscription if found.

Source
pub fn get(&self, id: Uuid) -> Result<Option<WebhookSubscription>, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
webhook_subscriptions::table
.filter(webhook_subscriptions::id.eq(id))
.first(conn)
.optional()
}

pub

fn list(
&self,
enabled_only: bool,
) -> Result<Vec<WebhookSubscription>, diesel::result::Error>

Lists all webhook subscriptions.

Parameters:

Name Type Description
enabled_only bool If true, only return enabled subscriptions.

Returns:

Returns a list of webhook subscriptions.

Source
pub fn list(
&self,
enabled_only: bool,
) -> Result<Vec<WebhookSubscription>, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
let mut query = webhook_subscriptions::table.into_boxed();
if enabled_only {
query = query.filter(webhook_subscriptions::enabled.eq(true));
}
query
.order(webhook_subscriptions::created_at.desc())
.load(conn)
}

pub

fn get_matching_subscriptions(
&self,
event_type: &str,
) -> Result<Vec<WebhookSubscription>, diesel::result::Error>

Gets all enabled subscriptions that match a given event type.

Parameters:

Name Type Description
event_type &str The event type to match (e.g., “health.degraded”).

Returns:

Returns subscriptions that should receive this event type.

Source
pub fn get_matching_subscriptions(
&self,
event_type: &str,
) -> Result<Vec<WebhookSubscription>, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
// Get all enabled subscriptions
let subscriptions: Vec<WebhookSubscription> = webhook_subscriptions::table
.filter(webhook_subscriptions::enabled.eq(true))
.load(conn)?;
// Filter by event type pattern matching
let matching = subscriptions
.into_iter()
.filter(|sub| {
sub.event_types.iter().any(|pattern| {
if let Some(p) = pattern {
matches_event_pattern(p, event_type)
} else {
false
}
})
})
.collect();
Ok(matching)
}

pub

fn update(
&self,
id: Uuid,
update: &UpdateWebhookSubscription,
) -> Result<WebhookSubscription, diesel::result::Error>

Updates a webhook subscription.

Parameters:

Name Type Description
id Uuid The subscription UUID.
update &UpdateWebhookSubscription The fields to update.

Returns:

Returns the updated subscription.

Source
pub fn update(
&self,
id: Uuid,
update: &UpdateWebhookSubscription,
) -> Result<WebhookSubscription, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
diesel::update(webhook_subscriptions::table.filter(webhook_subscriptions::id.eq(id)))
.set(update)
.get_result(conn)
}

pub

fn delete(&self, id: Uuid) -> Result<usize, diesel::result::Error>

Deletes a webhook subscription.

Parameters:

Name Type Description
id Uuid The subscription UUID.

Returns:

Returns the number of deleted rows.

Source
pub fn delete(&self, id: Uuid) -> Result<usize, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
diesel::delete(webhook_subscriptions::table.filter(webhook_subscriptions::id.eq(id)))
.execute(conn)
}

pub

fn set_enabled(
&self,
id: Uuid,
enabled: bool,
) -> Result<WebhookSubscription, diesel::result::Error>

Enables or disables a subscription.

Parameters:

Name Type Description
id Uuid The subscription UUID.
enabled bool Whether to enable or disable.

Returns:

Returns the updated subscription.

Source
pub fn set_enabled(
&self,
id: Uuid,
enabled: bool,
) -> Result<WebhookSubscription, diesel::result::Error> {
let conn = &mut self.dal.conn()?;
diesel::update(webhook_subscriptions::table.filter(webhook_subscriptions::id.eq(id)))
.set(webhook_subscriptions::enabled.eq(enabled))
.get_result(conn)
}