Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #569 Data agreement with existing Usage purpose (name) shouldn't … #570

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions internal/dataagreement/dataagreements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down