1use std::collections::HashMap;
2
3use env_logger::Env;
4use log::{info, warn};
5use reqwest::StatusCode;
6use serde::{Deserialize, Serialize};
7use serde_json::json;
8use uuid::Uuid;
9
10pub struct Notifica;
11
12#[derive(Clone, Debug, Serialize, Deserialize)]
13pub struct NotificaBodyEmail {
14 pub target_email: Option<String>,
15 pub target_name: Option<String>,
16 pub sender_name: Option<String>,
17 pub subject: Option<String>,
18 pub params: HashMap<String, String>,
19}
20
21#[derive(Clone, Debug, Serialize, Deserialize)]
22pub struct NotificaBody {
23 pub push: Option<HashMap<String, String>>,
24 pub email: Option<NotificaBodyEmail>,
25}
26
27impl Notifica {
28 pub async fn send_message(body: NotificaBody, event: &str, tenant_id: Uuid) {
29 env_logger::init_from_env(Env::default().default_filter_or("info"));
30 info!("[NOTIFICA-SDK] Tenant '{tenant_id}' event '{event}'");
31 let client = reqwest::Client::new();
32 let response: reqwest::Response = client
33 .post(format!(
34 "https://notifica.flippi.co/webhook/{tenant_id}/{event}"
35 ))
36 .json(&json!(body))
37 .send()
38 .await
39 .unwrap();
40
41 if response.status() == StatusCode::OK {
42 info!("[NOTIFICA-SDK] Request has been accepted");
43 } else {
44 warn!("[NOTIFICA-SDK] Request hasnt been accepted");
45 }
46 }
47}