cala_server/integration/
mod.rs

1use sqlx::PgPool;
2
3use cala_ledger::LedgerOperation;
4
5es_entity::entity_id! { IntegrationId }
6
7pub struct Integration {
8    pub id: IntegrationId,
9    pub name: String,
10    data: serde_json::Value,
11}
12
13impl Integration {
14    fn new(id: IntegrationId, name: String, data: impl serde::Serialize) -> Self {
15        Self {
16            id,
17            name,
18            data: serde_json::to_value(data).expect("Could not serialize data"),
19        }
20    }
21
22    pub fn data<T: serde::de::DeserializeOwned>(&self) -> Result<T, serde_json::Error> {
23        serde_json::from_value(self.data.clone())
24    }
25}
26
27pub struct Integrations {
28    pool: PgPool,
29}
30
31impl Integrations {
32    pub(crate) fn new(pool: &PgPool) -> Self {
33        Self { pool: pool.clone() }
34    }
35
36    pub async fn create_in_op(
37        &self,
38        op: &mut LedgerOperation<'_>,
39        id: impl Into<IntegrationId> + std::fmt::Debug,
40        name: String,
41        data: impl serde::Serialize,
42    ) -> Result<Integration, sqlx::Error> {
43        let integration = Integration::new(id.into(), name, data);
44        sqlx::query!(
45            r#"INSERT INTO integrations (id, name, data)
46            VALUES ($1, $2, $3)"#,
47            integration.id as IntegrationId,
48            integration.name,
49            integration.data
50        )
51        .execute(&mut **op.tx())
52        .await?;
53        Ok(integration)
54    }
55
56    pub async fn find_by_id(
57        &self,
58        id: impl Into<IntegrationId>,
59    ) -> Result<Integration, sqlx::Error> {
60        let id = id.into();
61        let row = sqlx::query_as!(
62            Integration,
63            r#"SELECT id, name, data
64            FROM integrations
65            WHERE id = $1"#,
66            id as IntegrationId
67        )
68        .fetch_one(&self.pool)
69        .await?;
70        Ok(row)
71    }
72}