phonograph/interim-models/src/rel_invitation.rs

88 lines
2.1 KiB
Rust
Raw Normal View History

2025-05-28 16:35:00 -07:00
use chrono::{DateTime, Utc};
use derive_builder::Builder;
2025-07-08 16:54:51 -07:00
use interim_pgtypes::pg_acl::PgPrivilegeType;
2025-08-04 13:59:42 -07:00
use sqlx::{postgres::types::Oid, query_as};
2025-05-28 16:35:00 -07:00
use uuid::Uuid;
2025-08-04 13:59:42 -07:00
use crate::client::AppDbClient;
2025-05-28 16:35:00 -07:00
#[derive(Clone, Debug)]
pub struct RelInvitation {
pub id: Uuid,
pub email: String,
pub workspace_id: Uuid,
2025-05-28 16:35:00 -07:00
pub class_oid: Oid,
pub created_by: Uuid,
pub privilege: String,
pub expires_at: Option<DateTime<Utc>>,
}
impl RelInvitation {
2025-08-04 13:59:42 -07:00
pub fn belonging_to_rel(rel_oid: Oid) -> BelongingToRelQuery {
BelongingToRelQuery { rel_oid }
}
pub fn upsertable() -> UpsertableRelInvitationBuilder {
UpsertableRelInvitationBuilder::default()
}
}
#[derive(Clone, Debug)]
pub struct BelongingToRelQuery {
rel_oid: Oid,
}
impl BelongingToRelQuery {
pub async fn fetch_all(
self,
app_db: &mut AppDbClient,
) -> Result<Vec<RelInvitation>, sqlx::Error> {
2025-05-28 16:35:00 -07:00
query_as!(
2025-08-04 13:59:42 -07:00
RelInvitation,
2025-05-28 16:35:00 -07:00
"
select * from rel_invitations
where class_oid = $1
",
2025-08-04 13:59:42 -07:00
self.rel_oid
2025-05-28 16:35:00 -07:00
)
2025-08-04 13:59:42 -07:00
.fetch_all(&mut *app_db.conn)
2025-05-28 16:35:00 -07:00
.await
}
}
#[derive(Builder, Clone, Debug)]
pub struct UpsertableRelInvitation {
email: String,
workspace_id: Uuid,
2025-05-28 16:35:00 -07:00
class_oid: Oid,
created_by: Uuid,
privilege: PgPrivilegeType,
#[builder(default, setter(strip_option))]
expires_at: Option<DateTime<Utc>>,
}
impl UpsertableRelInvitation {
2025-08-04 13:59:42 -07:00
pub async fn upsert(self, app_db: &mut AppDbClient) -> Result<RelInvitation, sqlx::Error> {
2025-05-28 16:35:00 -07:00
query_as!(
RelInvitation,
"
insert into rel_invitations
(email, workspace_id, class_oid, privilege, created_by, expires_at)
values ($1, $2, $3, $4, $5, $6)
on conflict (email, workspace_id, class_oid, privilege) do update set
2025-05-28 16:35:00 -07:00
created_by = excluded.created_by,
expires_at = excluded.expires_at
returning *
",
self.email,
self.workspace_id,
2025-05-28 16:35:00 -07:00
self.class_oid,
self.privilege.to_abbrev().to_string(),
self.created_by,
self.expires_at,
)
2025-08-04 13:59:42 -07:00
.fetch_one(&mut *app_db.conn)
2025-05-28 16:35:00 -07:00
.await
}
}