From 6050c8dac50ad892d8ed8e00cd0c930c42dff039 Mon Sep 17 00:00:00 2001 From: Folke Ashberg Date: Wed, 28 Jun 2023 20:37:13 +0200 Subject: [PATCH 1/2] Added-External_id for Collections --- .../down.sql | 0 .../up.sql | 1 + .../down.sql | 0 .../up.sql | 1 + .../down.sql | 0 .../up.sql | 1 + src/api/core/organizations.rs | 9 ++++-- src/db/models/collection.rs | 29 +++++++++++++++---- src/db/schemas/mysql/schema.rs | 1 + src/db/schemas/postgresql/schema.rs | 1 + src/db/schemas/sqlite/schema.rs | 1 + 11 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 migrations/mysql/2023-06-28-133700_add_collection_external_id/down.sql create mode 100644 migrations/mysql/2023-06-28-133700_add_collection_external_id/up.sql create mode 100644 migrations/postgresql/2023-06-28-133700_add_collection_external_id/down.sql create mode 100644 migrations/postgresql/2023-06-28-133700_add_collection_external_id/up.sql create mode 100644 migrations/sqlite/2023-06-28-133700_add_collection_external_id/down.sql create mode 100644 migrations/sqlite/2023-06-28-133700_add_collection_external_id/up.sql diff --git a/migrations/mysql/2023-06-28-133700_add_collection_external_id/down.sql b/migrations/mysql/2023-06-28-133700_add_collection_external_id/down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/mysql/2023-06-28-133700_add_collection_external_id/up.sql b/migrations/mysql/2023-06-28-133700_add_collection_external_id/up.sql new file mode 100644 index 00000000..7f44709b --- /dev/null +++ b/migrations/mysql/2023-06-28-133700_add_collection_external_id/up.sql @@ -0,0 +1 @@ +ALTER TABLE collections ADD COLUMN external_id TEXT; diff --git a/migrations/postgresql/2023-06-28-133700_add_collection_external_id/down.sql b/migrations/postgresql/2023-06-28-133700_add_collection_external_id/down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/postgresql/2023-06-28-133700_add_collection_external_id/up.sql b/migrations/postgresql/2023-06-28-133700_add_collection_external_id/up.sql new file mode 100644 index 00000000..7f44709b --- /dev/null +++ b/migrations/postgresql/2023-06-28-133700_add_collection_external_id/up.sql @@ -0,0 +1 @@ +ALTER TABLE collections ADD COLUMN external_id TEXT; diff --git a/migrations/sqlite/2023-06-28-133700_add_collection_external_id/down.sql b/migrations/sqlite/2023-06-28-133700_add_collection_external_id/down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/sqlite/2023-06-28-133700_add_collection_external_id/up.sql b/migrations/sqlite/2023-06-28-133700_add_collection_external_id/up.sql new file mode 100644 index 00000000..7f44709b --- /dev/null +++ b/migrations/sqlite/2023-06-28-133700_add_collection_external_id/up.sql @@ -0,0 +1 @@ +ALTER TABLE collections ADD COLUMN external_id TEXT; diff --git a/src/api/core/organizations.rs b/src/api/core/organizations.rs index 6eaeeb63..abbcc3f9 100644 --- a/src/api/core/organizations.rs +++ b/src/api/core/organizations.rs @@ -106,6 +106,7 @@ struct OrgData { CollectionName: String, Key: String, Name: String, + ExternalId: String, Keys: Option, #[serde(rename = "PlanType")] _PlanType: NumberOrString, // Ignored, always use the same plan @@ -124,6 +125,7 @@ struct NewCollectionData { Name: String, Groups: Vec, Users: Vec, + ExternalId: Option, } #[derive(Deserialize)] @@ -168,7 +170,7 @@ async fn create_organization(headers: Headers, data: JsonUpcase, mut co let org = Organization::new(data.Name, data.BillingEmail, private_key, public_key); let mut user_org = UserOrganization::new(headers.user.uuid, org.uuid.clone()); - let collection = Collection::new(org.uuid.clone(), data.CollectionName); + let collection = Collection::new(org.uuid.clone(), data.CollectionName, Some(data.ExternalId)); user_org.akey = data.Key; user_org.access_all = true; @@ -390,7 +392,7 @@ async fn post_organization_collections( None => err!("Can't find organization details"), }; - let collection = Collection::new(org.uuid, data.Name); + let collection = Collection::new(org.uuid, data.Name, data.ExternalId); collection.save(&mut conn).await?; log_event( @@ -463,6 +465,7 @@ async fn post_organization_collection_update( } collection.name = data.Name; + collection.external_id = data.ExternalId; collection.save(&mut conn).await?; log_event( @@ -1576,7 +1579,7 @@ async fn post_org_import( let mut collections = Vec::new(); for coll in data.Collections { - let collection = Collection::new(org_id.clone(), coll.Name); + let collection = Collection::new(org_id.clone(), coll.Name, coll.ExternalId); if collection.save(&mut conn).await.is_err() { collections.push(Err(Error::new("Failed to create Collection", "Failed to create Collection"))); } else { diff --git a/src/db/models/collection.rs b/src/db/models/collection.rs index 6a9acab8..c534fa4f 100644 --- a/src/db/models/collection.rs +++ b/src/db/models/collection.rs @@ -10,6 +10,7 @@ db_object! { pub uuid: String, pub org_uuid: String, pub name: String, + pub external_id: Option, } #[derive(Identifiable, Queryable, Insertable)] @@ -33,18 +34,21 @@ db_object! { /// Local methods impl Collection { - pub fn new(org_uuid: String, name: String) -> Self { - Self { + pub fn new(org_uuid: String, name: String, external_id: Option) -> Self { + let mut new_model = Self { uuid: crate::util::get_uuid(), - org_uuid, name, - } + external_id: None, + }; + + new_model.set_external_id(external_id); + new_model } pub fn to_json(&self) -> Value { json!({ - "ExternalId": null, // Not support by us + "ExternalId": self.external_id, "Id": self.uuid, "OrganizationId": self.org_uuid, "Name": self.name, @@ -52,6 +56,21 @@ impl Collection { }) } + 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 async fn to_json_details( &self, user_uuid: &str, diff --git a/src/db/schemas/mysql/schema.rs b/src/db/schemas/mysql/schema.rs index 37803275..695d5bd7 100644 --- a/src/db/schemas/mysql/schema.rs +++ b/src/db/schemas/mysql/schema.rs @@ -38,6 +38,7 @@ table! { uuid -> Text, org_uuid -> Text, name -> Text, + external_id -> Nullable, } } diff --git a/src/db/schemas/postgresql/schema.rs b/src/db/schemas/postgresql/schema.rs index 0b69bbc4..766c03e7 100644 --- a/src/db/schemas/postgresql/schema.rs +++ b/src/db/schemas/postgresql/schema.rs @@ -38,6 +38,7 @@ table! { uuid -> Text, org_uuid -> Text, name -> Text, + external_id -> Nullable, } } diff --git a/src/db/schemas/sqlite/schema.rs b/src/db/schemas/sqlite/schema.rs index 10dd3fe8..031ec7aa 100644 --- a/src/db/schemas/sqlite/schema.rs +++ b/src/db/schemas/sqlite/schema.rs @@ -38,6 +38,7 @@ table! { uuid -> Text, org_uuid -> Text, name -> Text, + external_id -> Nullable, } } From 839b2bc950837999fbcb21c4dda0afc84d68d295 Mon Sep 17 00:00:00 2001 From: Folke Ashberg Date: Wed, 28 Jun 2023 22:11:26 +0200 Subject: [PATCH 2/2] fix format error --- src/db/models/collection.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db/models/collection.rs b/src/db/models/collection.rs index c534fa4f..f2b04ce5 100644 --- a/src/db/models/collection.rs +++ b/src/db/models/collection.rs @@ -41,7 +41,7 @@ impl Collection { name, external_id: None, }; - + new_model.set_external_id(external_id); new_model }