diff --git a/src/v2/dataagreement_record/db.go b/src/v2/dataagreement_record/db.go index 805fa2f..976cfb7 100644 --- a/src/v2/dataagreement_record/db.go +++ b/src/v2/dataagreement_record/db.go @@ -195,3 +195,19 @@ func PipelineForList(organisationId string, id string, lawfulBasis string, isId return pipeline, nil } + +// Deletes all the data agreement records for data agreement id +func (darRepo *DataAgreementRecordRepository) DeleteDataAgreementRecordsForDataAgreement(dataAgreementId string) error { + filter := common.CombineFilters(darRepo.DefaultFilter, bson.M{"dataagreementid": dataAgreementId}) + + // Update to set IsDeleted to true + update := bson.M{ + "$set": bson.M{ + "isdeleted": true, + }, + } + + _, err := Collection().UpdateMany(context.TODO(), filter, update) + + return err +} diff --git a/src/v2/handler/dataagreement/config_delete_dataagreement.go b/src/v2/handler/dataagreement/config_delete_dataagreement.go index cd89acf..3ed9aa8 100644 --- a/src/v2/handler/dataagreement/config_delete_dataagreement.go +++ b/src/v2/handler/dataagreement/config_delete_dataagreement.go @@ -8,6 +8,7 @@ import ( "github.com/bb-consent/api/src/common" "github.com/bb-consent/api/src/config" "github.com/bb-consent/api/src/v2/dataagreement" + daRecord "github.com/bb-consent/api/src/v2/dataagreement_record" "github.com/bb-consent/api/src/v2/dataattribute" "github.com/bb-consent/api/src/v2/revision" "github.com/gorilla/mux" @@ -61,6 +62,18 @@ func ConfigDeleteDataAgreement(w http.ResponseWriter, r *http.Request) { return } + // Repository + darRepo := daRecord.DataAgreementRecordRepository{} + darRepo.Init(organisationId) + + // Deletes all the data agreement records for data agreement id + err = darRepo.DeleteDataAgreementRecordsForDataAgreement(dataAgreementId) + if err != nil { + m := fmt.Sprintf("Failed to delete data agreement id from data agreement records: %v", dataAgreementId) + common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + return + } + _, err = daRepo.Update(currentDataAgreement) if err != nil { m := fmt.Sprintf("Failed to delete data agreement: %v", dataAgreementId)