samling/organizations/
entities.rs1use chrono::{DateTime, FixedOffset};
2use derive_more::Display;
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use url::Url;
6
7use crate::{
8 auth::User,
9 entity_ref::{Id, RefTarget},
10 ImageSource,
11};
12use samling_clorinde::{client::GenericClient, queries::organization::get_organization_id};
13
14#[derive(Debug, Serialize, Deserialize, Clone, Display, JsonSchema)]
16#[display("{id}/{name}")]
17pub struct Organization {
18 pub id: Id<Self>,
19 pub name: String,
20 pub created_by: Option<Id<User>>,
21 pub created_at: DateTime<FixedOffset>,
22 pub updated_at: DateTime<FixedOffset>,
23 pub logo_url: Option<Url>,
24}
25
26#[derive(Debug, Serialize, Deserialize, Clone, Display, JsonSchema)]
28#[display("{id}/{name}")]
29pub struct OrganizationSummary {
30 pub id: Id<Organization>,
31 pub name: String,
32 pub logo_url: Option<Url>,
33}
34
35impl RefTarget for Organization {
36 async fn lookup_id(
37 client: &impl GenericClient,
38 _: Id<Self>,
39 entity_ref: &crate::entity_ref::Ref<Self>,
40 ) -> crate::Result<Option<Id<Self>>> {
41 if let Some(id) = entity_ref.id() {
42 Ok(get_organization_id()
43 .bind(client, &id.into())
44 .opt()
45 .await?
46 .map(Id::new))
47 } else {
48 Err(entity_ref.not_found_error())
49 }
50 }
51}
52
53#[derive(Debug, Deserialize)]
55pub struct CreateOrganization {
56 pub name: String,
57 pub logo: Option<ImageSource>,
58}
59
60#[derive(Debug, Deserialize)]
62pub struct UpdateOrganization {
63 pub name: Option<String>,
64 pub logo: Option<ImageSource>,
65}