From fb1b12650869510499a96a7783ee0579c2748f2b Mon Sep 17 00:00:00 2001 From: Albin Antony Date: Wed, 25 Sep 2024 14:51:13 +0530 Subject: [PATCH] Fix #631: Ability to configure data sources when defining data agreement --- internal/dataagreement/dataagreements.go | 8 +++++ .../config_create_dataagreement.go | 32 +++++++++++++++++++ .../config_update_dataagreement.go | 16 ++++++++++ internal/revision/revisions.go | 4 +++ 4 files changed, 60 insertions(+) diff --git a/internal/dataagreement/dataagreements.go b/internal/dataagreement/dataagreements.go index 217debc..bf7dd40 100644 --- a/internal/dataagreement/dataagreements.go +++ b/internal/dataagreement/dataagreements.go @@ -49,6 +49,13 @@ type Controller struct { Url string `json:"url"` } +type DataSource struct { + Name string `json:"name" validate:"required"` + Sector string `json:"sector" validate:"required"` + Location string `json:"location" validate:"required"` + PrivacyDashboardUrl string `json:"privacyDashboardUrl"` +} + type DataAgreement struct { Id string `json:"id" bson:"_id,omitempty"` Version string `json:"version"` @@ -75,6 +82,7 @@ type DataAgreement struct { Dpia string `json:"dpia"` CompatibleWithVersion string `json:"compatibleWithVersion"` Controller Controller `json:"controller"` + DataSources []DataSource `json:"dataSources"` } type DataAgreementWithObjectData struct { diff --git a/internal/handler/v2/config/dataagreement/config_create_dataagreement.go b/internal/handler/v2/config/dataagreement/config_create_dataagreement.go index 6ecbedf..98eca78 100644 --- a/internal/handler/v2/config/dataagreement/config_create_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_create_dataagreement.go @@ -102,6 +102,7 @@ type dataAgreement struct { Dpia string `json:"dpia"` CompatibleWithVersion string `json:"compatibleWithVersion"` Controller dataagreement.Controller `json:"controller"` + DataSources []dataagreement.DataSource `json:"dataSources"` } type addDataAgreementReq struct { @@ -163,6 +164,14 @@ func validateAddDataAgreementRequestBody(dataAgreementReq addDataAgreementReq) e return errors.New("invalid data use provided") } + if strings.TrimSpace(dataAgreementReq.DataAgreement.DataUse) == "data_using_service" || strings.TrimSpace(dataAgreementReq.DataAgreement.MethodOfUse) == "data_using_service" { + for _, dataSource := range dataAgreementReq.DataAgreement.DataSources { + if err := validate.Struct(dataSource); err != nil { + return err + } + } + } + return nil } @@ -176,6 +185,22 @@ func setDataAgreementLifecycle(active bool) string { return lifecycle } +func setDataAgreementDusDataSource(dataSourcesReq []dataagreement.DataSource) []dataagreement.DataSource { + dataSources := []dataagreement.DataSource{} + + for _, dataSource := range dataSourcesReq { + var tempDataSource dataagreement.DataSource + + tempDataSource.Name = dataSource.Name + tempDataSource.Sector = dataSource.Sector + tempDataSource.Location = dataSource.Location + tempDataSource.PrivacyDashboardUrl = dataSource.PrivacyDashboardUrl + dataSources = append(dataSources, tempDataSource) + + } + return dataSources +} + func setDataAttributesFromReq(requestBody addDataAgreementReq) []dataagreement.DataAttribute { var newDataAttributes []dataagreement.DataAttribute @@ -246,6 +271,13 @@ func setDataAgreementFromReq(requestBody addDataAgreementReq, newDataAgreement d newDataAgreement.DataUse = requestBody.DataAgreement.MethodOfUse } + if newDataAgreement.DataUse == "data_using_service" { + dataSources := setDataAgreementDusDataSource(requestBody.DataAgreement.DataSources) + newDataAgreement.DataSources = dataSources + } else { + newDataAgreement.DataSources = []dataagreement.DataSource{} + } + return newDataAgreement } diff --git a/internal/handler/v2/config/dataagreement/config_update_dataagreement.go b/internal/handler/v2/config/dataagreement/config_update_dataagreement.go index a805ba2..f398bbc 100644 --- a/internal/handler/v2/config/dataagreement/config_update_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_update_dataagreement.go @@ -53,6 +53,14 @@ func validateUpdateDataAgreementRequestBody(dataAgreementReq updateDataAgreement return errors.New("invalid data use provided") } + if strings.TrimSpace(dataAgreementReq.DataAgreement.DataUse) == "data_using_service" || strings.TrimSpace(dataAgreementReq.DataAgreement.MethodOfUse) == "data_using_service" { + for _, dataSource := range dataAgreementReq.DataAgreement.DataSources { + if err := validate.Struct(dataSource); err != nil { + return err + } + } + } + return nil } @@ -154,6 +162,14 @@ func updateDataAgreementFromRequestBody(requestBody updateDataAgreementReq, toBe toBeUpdatedDataAgreement.DataUse = requestBody.DataAgreement.MethodOfUse } + if toBeUpdatedDataAgreement.DataUse == "data_using_service" { + dataSources := setDataAgreementDusDataSource(requestBody.DataAgreement.DataSources) + toBeUpdatedDataAgreement.DataSources = dataSources + } else { + dataSources := []dataagreement.DataSource{} + toBeUpdatedDataAgreement.DataSources = dataSources + } + return toBeUpdatedDataAgreement } diff --git a/internal/revision/revisions.go b/internal/revision/revisions.go index edff36b..b880a67 100644 --- a/internal/revision/revisions.go +++ b/internal/revision/revisions.go @@ -278,6 +278,7 @@ type dataAgreementForObjectData struct { Dpia string `json:"dpia"` CompatibleWithVersion string `json:"compatibleWithVersion"` Controller dataagreement.Controller `json:"controller"` + DataSources []dataagreement.DataSource `json:"dataSources"` } // InitForDraftDataAgreement @@ -317,6 +318,7 @@ func CreateRevisionForDataAgreement(newDataAgreement dataagreement.DataAgreement CompatibleWithVersion: newDataAgreement.CompatibleWithVersion, ControllerName: newDataAgreement.ControllerName, Controller: newDataAgreement.Controller, + DataSources: newDataAgreement.DataSources, } // Create revision @@ -353,6 +355,7 @@ func UpdateRevisionForDataAgreement(updatedDataAgreement dataagreement.DataAgree CompatibleWithVersion: updatedDataAgreement.CompatibleWithVersion, ControllerName: updatedDataAgreement.ControllerName, Controller: updatedDataAgreement.Controller, + DataSources: updatedDataAgreement.DataSources, } // Initialise revision @@ -435,6 +438,7 @@ func CreateRevisionForDraftDataAgreement(newDataAgreement dataagreement.DataAgre CompatibleWithVersion: newDataAgreement.CompatibleWithVersion, ControllerName: newDataAgreement.ControllerName, Controller: newDataAgreement.Controller, + DataSources: newDataAgreement.DataSources, } // Create revision