Skip to content

Commit

Permalink
Fix #569 Data agreement with existing Usage purpose (name) shouldn't …
Browse files Browse the repository at this point in the history
…allowed again
  • Loading branch information
albinpa authored and georgepadayatti committed Nov 15, 2023
1 parent 01ab11c commit fbc0aae
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
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

0 comments on commit fbc0aae

Please sign in to comment.