diff --git a/internal/dataagreement/dataagreements.go b/internal/dataagreement/dataagreements.go index 448f766..5d2039d 100644 --- a/internal/dataagreement/dataagreements.go +++ b/internal/dataagreement/dataagreements.go @@ -352,3 +352,33 @@ func (darepo *DataAgreementRepository) GetDataAgreementsByLifecycle(lifecycle st } return results, nil } + +func (darepo *DataAgreementRepository) CountDocumentsByPurpose(purpose string) (int64, error) { + + filter := common.CombineFilters(darepo.DefaultFilter, bson.M{"purpose": purpose}) + + exists, err := Collection().CountDocuments(context.TODO(), filter) + if err != nil { + return exists, err + } + return exists, nil +} + +func (darepo *DataAgreementRepository) CountDocumentsByPurposeExeptOneDataAgreement(purpose string, dataAgreementID string) (int64, error) { + + dataAgreementId, err := primitive.ObjectIDFromHex(dataAgreementID) + if err != nil { + return 0, err + } + + filter := common.CombineFilters(darepo.DefaultFilter, bson.M{ + "purpose": purpose, + "_id": bson.M{"$ne": dataAgreementId}, + }) + + exists, err := Collection().CountDocuments(context.TODO(), filter) + if err != nil { + return exists, err + } + return exists, nil +} diff --git a/internal/handler/v2/config/dataagreement/config_create_dataagreement.go b/internal/handler/v2/config/dataagreement/config_create_dataagreement.go index d2a5bf5..81f52f4 100644 --- a/internal/handler/v2/config/dataagreement/config_create_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_create_dataagreement.go @@ -270,6 +270,21 @@ func ConfigCreateDataAgreement(w http.ResponseWriter, r *http.Request) { common.HandleErrorV2(w, http.StatusNotFound, m, err) return } + // Repository + darepo := dataagreement.DataAgreementRepository{} + darepo.Init(organisationId) + + count, err := darepo.CountDocumentsByPurpose(dataAgreementReq.DataAgreement.Purpose) + if err != nil { + m := "Failed to count data agreement by purpose" + common.HandleErrorV2(w, http.StatusNotFound, m, err) + return + } + if count >= 1 { + m := "Data agreement purpose exists" + common.HandleErrorV2(w, http.StatusBadRequest, m, err) + return + } // Initialise data agreement var newDataAgreement dataagreement.DataAgreement @@ -309,10 +324,6 @@ func ConfigCreateDataAgreement(w http.ResponseWriter, r *http.Request) { } - // Repository - darepo := dataagreement.DataAgreementRepository{} - darepo.Init(organisationId) - // Save the data agreement to db savedDataAgreement, err := darepo.Add(newDataAgreement) if err != nil { diff --git a/internal/handler/v2/config/dataagreement/config_update_dataagreement.go b/internal/handler/v2/config/dataagreement/config_update_dataagreement.go index 2a9a941..2c571f3 100644 --- a/internal/handler/v2/config/dataagreement/config_update_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_update_dataagreement.go @@ -167,6 +167,20 @@ func ConfigUpdateDataAgreement(w http.ResponseWriter, r *http.Request) { daRepo := dataagreement.DataAgreementRepository{} daRepo.Init(organisationId) + count, err := daRepo.CountDocumentsByPurposeExeptOneDataAgreement(dataAgreementReq.DataAgreement.Purpose, dataAgreementId) + if err != nil { + m := "Failed to count data agreements by purpose" + common.HandleErrorV2(w, http.StatusNotFound, m, err) + return + } + if count >= 1 { + m := "Data agreement purpose exists" + common.HandleErrorV2(w, http.StatusBadRequest, m, err) + return + } + + daRepo.Init(organisationId) + // Get data agreement from db currentDataAgreement, err := daRepo.Get(dataAgreementId) if err != nil {