diff --git a/src/api/core/organizations.rs b/src/api/core/organizations.rs index 82e24861..8a55329d 100644 --- a/src/api/core/organizations.rs +++ b/src/api/core/organizations.rs @@ -1755,7 +1755,7 @@ async fn get_groups(org_id: String, _headers: AdminHeaders, conn: DbConn) -> Jso struct GroupRequest { Name: String, AccessAll: Option, - ExternalId: String, + ExternalId: Option, Collections: Vec, } @@ -1777,7 +1777,7 @@ impl GroupRequest { group.name = self.Name.clone(); group.access_all = access_all_value; - group.external_id = self.ExternalId.clone(); + group.set_external_id(self.ExternalId.clone()); Ok(group) } @@ -1882,7 +1882,7 @@ async fn add_update_group(mut group: Group, collections: Vec, "OrganizationId": group.organizations_uuid, "Name": group.name, "AccessAll": group.access_all, - "ExternalId": group.external_id + "ExternalId": group.get_external_id() }))) } @@ -1904,7 +1904,7 @@ async fn get_group_details(_org_id: String, group_id: String, _headers: AdminHea "OrganizationId": group.organizations_uuid, "Name": group.name, "AccessAll": group.access_all, - "ExternalId": group.external_id, + "ExternalId": group.get_external_id(), "Collections": collections_groups }))) } diff --git a/src/db/models/group.rs b/src/db/models/group.rs index 721b3f85..41d65615 100644 --- a/src/db/models/group.rs +++ b/src/db/models/group.rs @@ -10,7 +10,7 @@ db_object! { pub organizations_uuid: String, pub name: String, pub access_all: bool, - pub external_id: String, + external_id: Option, pub creation_date: NaiveDateTime, pub revision_date: NaiveDateTime, } @@ -36,18 +36,22 @@ db_object! { /// Local methods impl Group { - pub fn new(organizations_uuid: String, name: String, access_all: bool, external_id: String) -> Self { + pub fn new(organizations_uuid: String, name: String, access_all: bool, external_id: Option) -> Self { let now = Utc::now().naive_utc(); - Self { + let mut new_model = Self { uuid: crate::util::get_uuid(), organizations_uuid, name, access_all, - external_id, + external_id: None, creation_date: now, revision_date: now, - } + }; + + new_model.set_external_id(external_id); + + new_model } pub fn to_json(&self) -> Value { @@ -63,6 +67,25 @@ impl Group { "RevisionDate": format_date(&self.revision_date) }) } + + pub fn set_external_id(&mut self, external_id: Option) { + //Check if external id is empty. We don't want to have + //empty strings in the database + match external_id { + Some(external_id) => { + if external_id.is_empty() { + self.external_id = None; + } else { + self.external_id = Some(external_id) + } + }, + None => self.external_id = None + } + } + + pub fn get_external_id(&self) -> Option { + self.external_id.clone() + } } impl CollectionGroup { diff --git a/src/db/schemas/mysql/schema.rs b/src/db/schemas/mysql/schema.rs index 51875e11..514bc67a 100644 --- a/src/db/schemas/mysql/schema.rs +++ b/src/db/schemas/mysql/schema.rs @@ -226,7 +226,7 @@ table! { organizations_uuid -> Text, name -> Text, access_all -> Bool, - external_id -> Text, + external_id -> Nullable, creation_date -> Timestamp, revision_date -> Timestamp, } diff --git a/src/db/schemas/postgresql/schema.rs b/src/db/schemas/postgresql/schema.rs index b2874f5e..23f9af7e 100644 --- a/src/db/schemas/postgresql/schema.rs +++ b/src/db/schemas/postgresql/schema.rs @@ -226,7 +226,7 @@ table! { organizations_uuid -> Text, name -> Text, access_all -> Bool, - external_id -> Text, + external_id -> Nullable, creation_date -> Timestamp, revision_date -> Timestamp, } diff --git a/src/db/schemas/sqlite/schema.rs b/src/db/schemas/sqlite/schema.rs index b2874f5e..23f9af7e 100644 --- a/src/db/schemas/sqlite/schema.rs +++ b/src/db/schemas/sqlite/schema.rs @@ -226,7 +226,7 @@ table! { organizations_uuid -> Text, name -> Text, access_all -> Bool, - external_id -> Text, + external_id -> Nullable, creation_date -> Timestamp, revision_date -> Timestamp, }