algohub_server/models/
organization.rs

1use serde::{Deserialize, Serialize};
2use surrealdb::sql::Thing;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct Organization {
6    pub id: Option<Thing>,
7    pub name: String,
8    pub display_name: Option<String>,
9
10    pub description: Option<String>,
11
12    pub owners: Vec<Thing>,
13    pub members: Vec<Thing>,
14    pub creator: Option<Thing>,
15
16    pub created_at: chrono::NaiveDateTime,
17    pub updated_at: chrono::NaiveDateTime,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
21#[serde(crate = "rocket::serde")]
22pub struct OrganizationData<'c> {
23    pub name: &'c str,
24    pub display_name: Option<&'c str>,
25    pub description: Option<&'c str>,
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
29#[serde(crate = "rocket::serde")]
30pub struct UpdateOrg {
31    pub name: String,
32    pub display_name: Option<String>,
33    pub description: Option<String>,
34}
35
36#[derive(Serialize, Deserialize)]
37#[serde(crate = "rocket::serde")]
38pub struct CreateOrganization<'r> {
39    pub id: &'r str,
40    pub token: &'r str,
41
42    pub org: OrganizationData<'r>,
43}
44
45#[derive(Debug, Clone, Serialize, Deserialize)]
46#[serde(crate = "rocket::serde")]
47pub struct UserOrganization {
48    pub name: String,
49    pub display_name: Option<String>,
50    pub description: Option<String>,
51
52    pub owners: Vec<String>,
53    pub members: Vec<String>,
54
55    pub created_at: chrono::NaiveDateTime,
56    pub updated_at: chrono::NaiveDateTime,
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
60#[serde(crate = "rocket::serde")]
61pub struct ChangeMember<'r> {
62    pub id: &'r str,
63    pub token: &'r str,
64    pub members: Vec<&'r str>,
65}
66
67impl From<CreateOrganization<'_>> for Organization {
68    fn from(val: CreateOrganization) -> Self {
69        Organization {
70            id: None,
71            name: val.org.name.to_string(),
72            display_name: val.org.display_name.map(|s| s.to_string()),
73            description: val.org.description.map(|s| s.to_string()),
74            owners: vec![("account", val.id).into()],
75            members: vec![],
76            creator: Some(("account".to_string(), val.id.to_string()).into()),
77            created_at: chrono::Utc::now().naive_utc(),
78            updated_at: chrono::Utc::now().naive_utc(),
79        }
80    }
81}
82
83impl From<Organization> for UserOrganization {
84    fn from(val: Organization) -> Self {
85        UserOrganization {
86            name: val.name,
87            display_name: val.display_name,
88            description: val.description,
89            owners: val.owners.iter().map(|thing| thing.id.to_raw()).collect(),
90            members: val.members.iter().map(|thing| thing.id.to_raw()).collect(),
91            created_at: val.created_at,
92            updated_at: val.updated_at,
93        }
94    }
95}
96
97impl From<OrganizationData<'_>> for UpdateOrg {
98    fn from(val: OrganizationData) -> Self {
99        UpdateOrg {
100            name: val.name.to_string(),
101            display_name: val.display_name.map(|s| s.to_string()),
102            description: val.description.map(|s| s.to_string()),
103        }
104    }
105}