From c2568ef2e07562871deb02d4dafc91ad20e6bb61 Mon Sep 17 00:00:00 2001 From: Albin Antony Date: Thu, 14 Dec 2023 11:11:03 +0530 Subject: [PATCH] Fix #599 Policy --- .../v2/config/policy/config_create_policy.go | 14 +++++++++++--- .../v2/config/policy/config_update_policy.go | 15 ++++++++++++--- internal/tenant/single.go | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/internal/handler/v2/config/policy/config_create_policy.go b/internal/handler/v2/config/policy/config_create_policy.go index 9f1018b..3ce7f4d 100644 --- a/internal/handler/v2/config/policy/config_create_policy.go +++ b/internal/handler/v2/config/policy/config_create_policy.go @@ -144,13 +144,21 @@ func ConfigCreatePolicy(w http.ResponseWriter, r *http.Request) { return } - // updates organisation policy url - err = updateOrganisationPolicyUrl(savedPolicy.Url, organisationId) + count, err := prepo.GetPolicyCountByOrganisation() if err != nil { - m := fmt.Sprintf("Failed to update organisation policy url: %v", organisationId) + m := "Failed to count policies" common.HandleErrorV2(w, http.StatusInternalServerError, m, err) return } + if count == 1 { + // updates organisation policy url + err = updateOrganisationPolicyUrl(savedPolicy.Url, organisationId) + if err != nil { + m := fmt.Sprintf("Failed to update organisation policy url: %v", organisationId) + common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + return + } + } // Constructing the response var resp addPolicyResp diff --git a/internal/handler/v2/config/policy/config_update_policy.go b/internal/handler/v2/config/policy/config_update_policy.go index 44a0cd0..c9adee8 100644 --- a/internal/handler/v2/config/policy/config_update_policy.go +++ b/internal/handler/v2/config/policy/config_update_policy.go @@ -126,13 +126,22 @@ func ConfigUpdatePolicy(w http.ResponseWriter, r *http.Request) { return } - // updates organisation policy url - err = updateOrganisationPolicyUrl(savedPolicy.Url, organisationId) + policyRepo.Init(organisationId) + first_policy, err := policyRepo.GetFirstPolicy() if err != nil { - m := fmt.Sprintf("Failed to update organisation policy url: %v", organisationId) + m := "Failed to fetch first policy" common.HandleErrorV2(w, http.StatusInternalServerError, m, err) return } + if first_policy.Id == toBeUpdatedPolicy.Id { + // updates organisation policy url + err = updateOrganisationPolicyUrl(savedPolicy.Url, organisationId) + if err != nil { + m := fmt.Sprintf("Failed to update organisation policy url: %v", organisationId) + common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + return + } + } // Constructing the response var resp updatePolicyResp diff --git a/internal/tenant/single.go b/internal/tenant/single.go index ddf78d0..c437a72 100644 --- a/internal/tenant/single.go +++ b/internal/tenant/single.go @@ -237,7 +237,7 @@ func SingleTenantConfiguration(config *config.Configuration) { createOrganisation(config, orgType, organisationAdminId) // delete all policies - deleteAllPolicies() + // deleteAllPolicies() // Load image assets for organisation err := fixture.LoadImageAssetsForSingleTenantConfiguration()