From 68eb1b609ba339d5a11dba12d6b8c7584b1e59ea Mon Sep 17 00:00:00 2001 From: Albin Antony Date: Wed, 3 Jan 2024 14:18:21 +0530 Subject: [PATCH] Fix #607 Update data agreement endpoint to return 400 status code if not present --- .../dataagreement/config_create_dataagreement.go | 10 +++++----- .../dataagreement/config_delete_dataagreement.go | 8 ++++---- .../config_list_dataagreement_revisions.go | 4 ++-- .../dataagreement/config_list_dataagreements.go | 14 +++++++------- ...config_list_dataattributes_for_dataagreement.go | 2 +- .../dataagreement/config_read_dataagreement.go | 8 ++++---- .../dataagreement/config_update_dataagreement.go | 14 +++++++------- 7 files changed, 30 insertions(+), 30 deletions(-) diff --git a/internal/handler/v2/config/dataagreement/config_create_dataagreement.go b/internal/handler/v2/config/dataagreement/config_create_dataagreement.go index 6ecbedf..751d5fc 100644 --- a/internal/handler/v2/config/dataagreement/config_create_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_create_dataagreement.go @@ -287,7 +287,7 @@ func ConfigCreateDataAgreement(w http.ResponseWriter, r *http.Request) { o, err := org.Get(organisationId) if err != nil { m := fmt.Sprintf("Failed to get organization by ID :%v", organisationId) - common.HandleErrorV2(w, http.StatusNotFound, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } // Repository @@ -297,7 +297,7 @@ func ConfigCreateDataAgreement(w http.ResponseWriter, r *http.Request) { count, err := darepo.CountDocumentsByPurpose(strings.TrimSpace(dataAgreementReq.DataAgreement.Purpose)) if err != nil { m := "Failed to count data agreement by purpose" - common.HandleErrorV2(w, http.StatusNotFound, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } if count >= 1 { @@ -328,7 +328,7 @@ func ConfigCreateDataAgreement(w http.ResponseWriter, r *http.Request) { newRevision, err = revision.UpdateRevisionForDataAgreement(newDataAgreement, orgAdminId) if err != nil { m := "Failed to create data agreement" - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -338,7 +338,7 @@ func ConfigCreateDataAgreement(w http.ResponseWriter, r *http.Request) { newRevision, err = revision.CreateRevisionForDraftDataAgreement(newDataAgreement, orgAdminId) if err != nil { m := "Failed to create revision for draft data agreement" - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -348,7 +348,7 @@ func ConfigCreateDataAgreement(w http.ResponseWriter, r *http.Request) { savedDataAgreement, err := darepo.Add(newDataAgreement) if err != nil { m := fmt.Sprintf("Failed to create new data agreement: %v", newDataAgreement.Id) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } diff --git a/internal/handler/v2/config/dataagreement/config_delete_dataagreement.go b/internal/handler/v2/config/dataagreement/config_delete_dataagreement.go index b633046..811bf64 100644 --- a/internal/handler/v2/config/dataagreement/config_delete_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_delete_dataagreement.go @@ -34,7 +34,7 @@ func ConfigDeleteDataAgreement(w http.ResponseWriter, r *http.Request) { toBeDeletedDA, err := daRepo.Get(dataAgreementId) if err != nil { m := fmt.Sprintf("Failed to fetch data agreement: %v", dataAgreementId) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -46,7 +46,7 @@ func ConfigDeleteDataAgreement(w http.ResponseWriter, r *http.Request) { rev, err = revision.GetLatestByDataAgreementId(dataAgreementId) if err != nil { m := fmt.Sprintf("Failed to fetch revision: %v", dataAgreementId) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -56,7 +56,7 @@ func ConfigDeleteDataAgreement(w http.ResponseWriter, r *http.Request) { rev, err = revision.CreateRevisionForDraftDataAgreement(toBeDeletedDA, orgAdminId) if err != nil { m := "Failed to create revision in run time" - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -73,7 +73,7 @@ func ConfigDeleteDataAgreement(w http.ResponseWriter, r *http.Request) { _, err = daRepo.Update(toBeDeletedDA) if err != nil { m := fmt.Sprintf("Failed to delete data agreement: %v", dataAgreementId) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } diff --git a/internal/handler/v2/config/dataagreement/config_list_dataagreement_revisions.go b/internal/handler/v2/config/dataagreement/config_list_dataagreement_revisions.go index 367573c..b4bbc3e 100644 --- a/internal/handler/v2/config/dataagreement/config_list_dataagreement_revisions.go +++ b/internal/handler/v2/config/dataagreement/config_list_dataagreement_revisions.go @@ -43,14 +43,14 @@ func ConfigListDataAgreementRevisions(w http.ResponseWriter, r *http.Request) { da, err := daRepo.Get(dataAgreementId) if err != nil { m := fmt.Sprintf("Failed to fetch data agreement: %v", dataAgreementId) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } revisions, err := revision.ListAllByDataAgreementId(dataAgreementId) if err != nil { m := fmt.Sprintf("Failed to fetch revision: %v", dataAgreementId) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } diff --git a/internal/handler/v2/config/dataagreement/config_list_dataagreements.go b/internal/handler/v2/config/dataagreement/config_list_dataagreements.go index 049315b..aa1f777 100644 --- a/internal/handler/v2/config/dataagreement/config_list_dataagreements.go +++ b/internal/handler/v2/config/dataagreement/config_list_dataagreements.go @@ -281,7 +281,7 @@ func ConfigListDataAgreements(w http.ResponseWriter, r *http.Request) { includeRevisions, err := parseIncludeRevisionsQueryParams(r) if err != nil { m := "Failed to parse query param includeRevisions" - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -300,7 +300,7 @@ func ConfigListDataAgreements(w http.ResponseWriter, r *http.Request) { dataAgreements, err := getDataAgreementsWithRevisions(organisationId, lifecycle) if err != nil { m := "Failed to fetch data agreements" - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -346,7 +346,7 @@ func ConfigListDataAgreements(w http.ResponseWriter, r *http.Request) { return } m := "Failed to paginate data agreement" - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -362,7 +362,7 @@ func ConfigListDataAgreements(w http.ResponseWriter, r *http.Request) { dataAgreements, err := getDataAgreementsWithRevisions(organisationId, lifecycle) if err != nil { m := "Failed to fetch data agreements" - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -387,7 +387,7 @@ func ConfigListDataAgreements(w http.ResponseWriter, r *http.Request) { dataAgreements, err := listDataAgreementsBasedOnLifecycle(lifecycle, organisationId) if err != nil { m := "Failed to fetch data agreements" - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -412,7 +412,7 @@ func ConfigListDataAgreements(w http.ResponseWriter, r *http.Request) { revisionResp, err := revision.GetByRevisionId(revisionId) if err != nil { m := fmt.Sprintf("Failed to fetch revision by id: %v", revisionId) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -420,7 +420,7 @@ func ConfigListDataAgreements(w http.ResponseWriter, r *http.Request) { da, err := revision.RecreateDataAgreementFromRevision(revisionResp) if err != nil { m := fmt.Sprintf("Failed to fetch data agreement by revision: %v", revisionId) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } diff --git a/internal/handler/v2/config/dataagreement/config_list_dataattributes_for_dataagreement.go b/internal/handler/v2/config/dataagreement/config_list_dataattributes_for_dataagreement.go index bd4f881..3efee08 100644 --- a/internal/handler/v2/config/dataagreement/config_list_dataattributes_for_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_list_dataattributes_for_dataagreement.go @@ -74,7 +74,7 @@ func ConfigListDataAttributesForDataAgreement(w http.ResponseWriter, r *http.Req da, err := daRepo.Get(dataAgreementId) if err != nil { m := fmt.Sprintf("Failed to fetch data agreement: %v", dataAgreementId) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } diff --git a/internal/handler/v2/config/dataagreement/config_read_dataagreement.go b/internal/handler/v2/config/dataagreement/config_read_dataagreement.go index 7d73241..1637f63 100644 --- a/internal/handler/v2/config/dataagreement/config_read_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_read_dataagreement.go @@ -42,7 +42,7 @@ func ConfigReadDataAgreement(w http.ResponseWriter, r *http.Request) { da, err := daRepo.Get(dataAgreementId) if err != nil { m := fmt.Sprintf("Failed to fetch data agreement: %v", dataAgreementId) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -55,7 +55,7 @@ func ConfigReadDataAgreement(w http.ResponseWriter, r *http.Request) { rev, err = revision.GetByRevisionId(revisionId) if err != nil { m := fmt.Sprintf("Failed to fetch revision: %v", dataAgreementId) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -67,7 +67,7 @@ func ConfigReadDataAgreement(w http.ResponseWriter, r *http.Request) { rev, err = revision.GetLatestByDataAgreementId(dataAgreementId) if err != nil { m := fmt.Sprintf("Failed to fetch revision: %v", dataAgreementId) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -77,7 +77,7 @@ func ConfigReadDataAgreement(w http.ResponseWriter, r *http.Request) { rev, err = revision.CreateRevisionForDraftDataAgreement(da, orgAdminId) if err != nil { m := "Failed to create revision in run time" - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } diff --git a/internal/handler/v2/config/dataagreement/config_update_dataagreement.go b/internal/handler/v2/config/dataagreement/config_update_dataagreement.go index a805ba2..5bec26e 100644 --- a/internal/handler/v2/config/dataagreement/config_update_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_update_dataagreement.go @@ -187,7 +187,7 @@ func ConfigUpdateDataAgreement(w http.ResponseWriter, r *http.Request) { o, err := org.Get(organisationId) if err != nil { m := fmt.Sprintf("Failed to get organization by ID :%v", organisationId) - common.HandleErrorV2(w, http.StatusNotFound, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -198,7 +198,7 @@ func ConfigUpdateDataAgreement(w http.ResponseWriter, r *http.Request) { count, err := daRepo.CountDocumentsByPurposeExeptOneDataAgreement(strings.TrimSpace(dataAgreementReq.DataAgreement.Purpose), dataAgreementId) if err != nil { m := "Failed to count data agreements by purpose" - common.HandleErrorV2(w, http.StatusNotFound, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } if count >= 1 { @@ -213,7 +213,7 @@ func ConfigUpdateDataAgreement(w http.ResponseWriter, r *http.Request) { currentDataAgreement, err := daRepo.Get(dataAgreementId) if err != nil { m := fmt.Sprintf("Failed to fetch data agreement by id: %v", dataAgreementId) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } currentVersion := currentDataAgreement.Version @@ -226,7 +226,7 @@ func ConfigUpdateDataAgreement(w http.ResponseWriter, r *http.Request) { updatedVersion, err := common.BumpMajorVersion(toBeUpdatedDataAgreement.Version) if err != nil { m := fmt.Sprintf("Failed to bump major version for data agreement: %v", dataAgreementId) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -249,7 +249,7 @@ func ConfigUpdateDataAgreement(w http.ResponseWriter, r *http.Request) { newRevision, err = revision.UpdateRevisionForDataAgreement(toBeUpdatedDataAgreement, orgAdminId) if err != nil { m := fmt.Sprintf("Failed to update data agreement: %v", dataAgreementId) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } @@ -259,7 +259,7 @@ func ConfigUpdateDataAgreement(w http.ResponseWriter, r *http.Request) { newRevision, err = revision.CreateRevisionForDraftDataAgreement(toBeUpdatedDataAgreement, orgAdminId) if err != nil { m := fmt.Sprintf("Failed to create revision for draft data agreement: %v", dataAgreementId) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return } } @@ -268,7 +268,7 @@ func ConfigUpdateDataAgreement(w http.ResponseWriter, r *http.Request) { savedDataAgreement, err := daRepo.Update(toBeUpdatedDataAgreement) if err != nil { m := fmt.Sprintf("Failed to update data agreement: %v", toBeUpdatedDataAgreement.Id) - common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + common.HandleErrorV2(w, http.StatusBadRequest, m, err) return }