-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0eb9086
commit feb1555
Showing
14 changed files
with
215 additions
and
9 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
crates/context_aware_config/migrations/2024-12-06-112348_webhooks/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- This file should undo anything in `up.sql` |
22 changes: 22 additions & 0 deletions
22
crates/context_aware_config/migrations/2024-12-06-112348_webhooks/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
-- Your SQL goes here | ||
-- Name: webhooks; Type: TABLE; Schema: public; Owner: - | ||
-- | ||
CREATE TABLE public.webhooks ( | ||
name text PRIMARY KEY, | ||
description text NOT NULL, | ||
enabled boolean NOT NULL DEFAULT true, | ||
url text NOT NULL, | ||
method text NOT NULL DEFAULT 'POST', | ||
version text NOT NULL, | ||
custom_headers json, | ||
events varchar(100)[] CHECK (array_position(events, NULL) IS NULL), | ||
max_retries integer NOT NULL DEFAULT 0, | ||
last_triggered_at timestamp, | ||
created_by text NOT NULL, | ||
created_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
updated_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); | ||
-- | ||
-- Name: webhooks webhooks_audit; Type: TRIGGER; Schema: public; Owner: - | ||
-- | ||
CREATE TRIGGER webhooks_audit AFTER INSERT OR DELETE OR UPDATE ON public.webhooks FOR EACH ROW EXECUTE FUNCTION public.event_logger(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ pub mod default_config; | |
pub mod dimension; | ||
pub mod functions; | ||
pub mod type_templates; | ||
pub mod webhooks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod handlers; | ||
pub mod types; | ||
pub use handlers::endpoints; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use actix_web::{ | ||
post, | ||
web::{self, Data, Json}, | ||
HttpResponse, Scope, | ||
}; | ||
use chrono::Utc; | ||
use service_utils::service::types::{AppState, DbConnection}; | ||
use superposition_macros::{bad_argument, unexpected_error}; | ||
use superposition_types::{result as superposition, webhook, User}; | ||
|
||
use super::types::CreateWebhookRequest; | ||
use diesel::{delete, ExpressionMethods, QueryDsl, RunQueryDsl}; | ||
use superposition_types::cac::models::{self as models, Webhooks}; | ||
use superposition_types::cac::schema::webhooks::dsl::webhooks; | ||
|
||
pub fn endpoints() -> Scope { | ||
Scope::new("").service(create) | ||
} | ||
|
||
#[post("")] | ||
async fn create( | ||
state: Data<AppState>, | ||
request: web::Json<CreateWebhookRequest>, | ||
db_conn: DbConnection, | ||
user: User, | ||
) -> superposition::Result<Json<Webhooks>> { | ||
let DbConnection(mut conn) = db_conn; | ||
let req = request.into_inner(); | ||
|
||
let now = Utc::now().naive_utc(); | ||
let webhook = Webhooks { | ||
name: req.name, | ||
description: req.description, | ||
enabled: req.enabled, | ||
url: req.url, | ||
method: req.method, | ||
version: req.version.unwrap_or("v1".to_owned()), | ||
custom_headers: req.custom_headers, | ||
events: req.events, | ||
max_retries: 0, | ||
last_triggered_at: None, | ||
created_by: user.email, | ||
created_at: now, | ||
updated_at: now, | ||
}; | ||
|
||
let insert: Result<Webhooks, diesel::result::Error> = diesel::insert_into(webhooks) | ||
.values(&webhook) | ||
.get_result(&mut conn); | ||
|
||
match insert { | ||
Ok(res) => Ok(Json(res)), | ||
Err(e) => match e { | ||
diesel::result::Error::DatabaseError(kind, e) => { | ||
log::error!("Function error: {:?}", e); | ||
match kind { | ||
diesel::result::DatabaseErrorKind::UniqueViolation => { | ||
Err(bad_argument!("Webhook already exists.")) | ||
} | ||
_ => Err(unexpected_error!( | ||
"Something went wrong, failed to create the webhook" | ||
)), | ||
} | ||
} | ||
_ => { | ||
log::error!("Webhook creation failed with error: {e}"); | ||
Err(unexpected_error!( | ||
"An error occured please contact the admin." | ||
)) | ||
} | ||
}, | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use serde::Deserialize; | ||
use serde_json::Value; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct CreateWebhookRequest { | ||
pub name: String, | ||
pub description: String, | ||
pub enabled: bool, | ||
pub url: String, | ||
pub method: String, | ||
pub version: Option<String>, | ||
pub custom_headers: Option<Value>, | ||
pub events: Option<Vec<Option<String>>>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!bin/sh | ||
make setup && make tenant TENANT=mjos && make tenant TENANT=sdk_config | ||
npm i |