diff --git a/internal/dataagreement/dataagreements.go b/internal/dataagreement/dataagreements.go index 9741529..c96f6d3 100644 --- a/internal/dataagreement/dataagreements.go +++ b/internal/dataagreement/dataagreements.go @@ -157,7 +157,10 @@ func CreatePipelineForFilteringDataAgreements(organisationId string, removeRevis bson.M{ "$match": bson.M{ "$expr": bson.M{ - "$eq": []interface{}{"$objectid", bson.M{"$toString": "$$localId"}}, + "$and": bson.A{ + bson.M{"$eq": []interface{}{"$objectid", bson.M{"$toString": "$$localId"}}}, + bson.M{"$eq": []interface{}{"$schemaname", "dataAgreement"}}, + }, }, }, }, @@ -210,7 +213,10 @@ func CreatePipelineForFilteringDataAgreementsUsingLifecycle(organisationId strin bson.M{ "$match": bson.M{ "$expr": bson.M{ - "$eq": []interface{}{"$objectid", bson.M{"$toString": "$$localId"}}, + "$and": bson.A{ + bson.M{"$eq": []interface{}{"$objectid", bson.M{"$toString": "$$localId"}}}, + bson.M{"$eq": []interface{}{"$schemaname", "dataAgreement"}}, + }, }, }, }, diff --git a/internal/dataagreement_record/db.go b/internal/dataagreement_record/db.go index 5c99c28..049bc43 100644 --- a/internal/dataagreement_record/db.go +++ b/internal/dataagreement_record/db.go @@ -123,7 +123,10 @@ func DataAgreementRecordsWithRevisionsFilteredById(organisationId string, id str bson.M{ "$match": bson.M{ "$expr": bson.M{ - "$eq": []interface{}{"$objectid", bson.M{"$toString": "$$localId"}}, + "$and": bson.A{ + bson.M{"$eq": []interface{}{"$objectid", bson.M{"$toString": "$$localId"}}}, + bson.M{"$eq": []interface{}{"$schemaname", "consentRecord"}}, + }, }, }, }, @@ -167,7 +170,10 @@ func CreatePipelineForFilteringLatestDataAgreementRecords(organisationId string) bson.M{ "$match": bson.M{ "$expr": bson.M{ - "$eq": []interface{}{"$objectid", bson.M{"$toString": "$$localId"}}, + "$and": bson.A{ + bson.M{"$eq": []interface{}{"$objectid", bson.M{"$toString": "$$localId"}}}, + bson.M{"$eq": []interface{}{"$schemaname", "consentRecord"}}, + }, }, }, }, @@ -218,7 +224,10 @@ func CreatePipelineForFilteringDataAgreementRecordsByIndividualId(organisationId bson.M{ "$match": bson.M{ "$expr": bson.M{ - "$eq": []interface{}{"$objectid", bson.M{"$toString": "$$localId"}}, + "$and": bson.A{ + bson.M{"$eq": []interface{}{"$objectid", bson.M{"$toString": "$$localId"}}}, + bson.M{"$eq": []interface{}{"$schemaname", "consentRecord"}}, + }, }, }, }, @@ -269,7 +278,10 @@ func CreatePipelineForFilteringDataAgreementRecordsByDataAgreementId(organisatio bson.M{ "$match": bson.M{ "$expr": bson.M{ - "$eq": []interface{}{"$objectid", bson.M{"$toString": "$$localId"}}, + "$and": bson.A{ + bson.M{"$eq": []interface{}{"$objectid", bson.M{"$toString": "$$localId"}}}, + bson.M{"$eq": []interface{}{"$schemaname", "consentRecord"}}, + }, }, }, }, diff --git a/internal/handler/v2/config/dataagreement/config_delete_dataagreement.go b/internal/handler/v2/config/dataagreement/config_delete_dataagreement.go index b633046..3d4b729 100644 --- a/internal/handler/v2/config/dataagreement/config_delete_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_delete_dataagreement.go @@ -43,7 +43,7 @@ func ConfigDeleteDataAgreement(w http.ResponseWriter, r *http.Request) { // If data agreement is published then: // a. Fetch latest revision if toBeDeletedDA.Active { - rev, err = revision.GetLatestByDataAgreementId(dataAgreementId) + rev, err = revision.GetLatestByObjectIdAndSchemaName(dataAgreementId, config.DataAgreement) if err != nil { m := fmt.Sprintf("Failed to fetch revision: %v", dataAgreementId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/config/dataagreement/config_list_dataagreement_revisions.go b/internal/handler/v2/config/dataagreement/config_list_dataagreement_revisions.go index 367573c..ac9c355 100644 --- a/internal/handler/v2/config/dataagreement/config_list_dataagreement_revisions.go +++ b/internal/handler/v2/config/dataagreement/config_list_dataagreement_revisions.go @@ -47,7 +47,7 @@ func ConfigListDataAgreementRevisions(w http.ResponseWriter, r *http.Request) { return } - revisions, err := revision.ListAllByDataAgreementId(dataAgreementId) + revisions, err := revision.ListAllByObjectIdAndSchemaName(dataAgreementId, config.DataAgreement) if err != nil { m := fmt.Sprintf("Failed to fetch revision: %v", dataAgreementId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/config/dataagreement/config_list_dataagreements.go b/internal/handler/v2/config/dataagreement/config_list_dataagreements.go index 049315b..aaa686e 100644 --- a/internal/handler/v2/config/dataagreement/config_list_dataagreements.go +++ b/internal/handler/v2/config/dataagreement/config_list_dataagreements.go @@ -181,7 +181,7 @@ func getDataAgreementsWithRevisions(organisationId string, lifecycle string) ([] for _, dataAgreement := range dataAgreements { // list all revisions for data agreement - revisions, err := revision.ListAllByDataAgreementId(dataAgreement.Id) + revisions, err := revision.ListAllByObjectIdAndSchemaName(dataAgreement.Id, config.DataAgreement) if err != nil { return tempDataAgreements, err } diff --git a/internal/handler/v2/config/dataagreement/config_read_dataagreement.go b/internal/handler/v2/config/dataagreement/config_read_dataagreement.go index 1637f63..7afe03f 100644 --- a/internal/handler/v2/config/dataagreement/config_read_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_read_dataagreement.go @@ -64,7 +64,7 @@ func ConfigReadDataAgreement(w http.ResponseWriter, r *http.Request) { // If data agreement is published then: // a. Fetch latest revision if da.Active { - rev, err = revision.GetLatestByDataAgreementId(dataAgreementId) + rev, err = revision.GetLatestByObjectIdAndSchemaName(dataAgreementId, config.DataAgreement) if err != nil { m := fmt.Sprintf("Failed to fetch revision: %v", dataAgreementId) common.HandleErrorV2(w, http.StatusBadRequest, m, err) diff --git a/internal/handler/v2/config/policy/config_delete_policy.go b/internal/handler/v2/config/policy/config_delete_policy.go index ceee922..71ec0b7 100644 --- a/internal/handler/v2/config/policy/config_delete_policy.go +++ b/internal/handler/v2/config/policy/config_delete_policy.go @@ -30,7 +30,7 @@ func ConfigDeletePolicy(w http.ResponseWriter, r *http.Request) { return } - currentRevision, err := revision.GetLatestByPolicyId(policyId) + currentRevision, err := revision.GetLatestByObjectIdAndSchemaName(policyId, config.Policy) if err != nil { m := fmt.Sprintf("Failed to fetch revisions: %v", policyId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/config/policy/config_list_policy_revisions.go b/internal/handler/v2/config/policy/config_list_policy_revisions.go index c437cdc..a959132 100644 --- a/internal/handler/v2/config/policy/config_list_policy_revisions.go +++ b/internal/handler/v2/config/policy/config_list_policy_revisions.go @@ -47,7 +47,7 @@ func ConfigListPolicyRevisions(w http.ResponseWriter, r *http.Request) { return } - revisions, err := revision.ListAllByPolicyId(policyId) + revisions, err := revision.ListAllByObjectIdAndSchemaName(policyId, config.Policy) if err != nil { m := fmt.Sprintf("Failed to fetch revision: %v", policyId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/config/policy/config_read_policy.go b/internal/handler/v2/config/policy/config_read_policy.go index c2ffb95..f61a300 100644 --- a/internal/handler/v2/config/policy/config_read_policy.go +++ b/internal/handler/v2/config/policy/config_read_policy.go @@ -51,7 +51,7 @@ func ConfigReadPolicy(w http.ResponseWriter, r *http.Request) { } } else { - revisionResp, err = revision.GetLatestByPolicyId(policyId) + revisionResp, err = revision.GetLatestByObjectIdAndSchemaName(policyId, config.Policy) if err != nil { m := fmt.Sprintf("Failed to fetch revision: %v", policyId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/service/service_create_blank_signature_object.go b/internal/handler/v2/service/service_create_blank_signature_object.go index 2370285..9668df5 100644 --- a/internal/handler/v2/service/service_create_blank_signature_object.go +++ b/internal/handler/v2/service/service_create_blank_signature_object.go @@ -29,7 +29,7 @@ func ServiceCreateBlankSignature(w http.ResponseWriter, r *http.Request) { darRepo.Init(organisationId) // Get latest revision for data agreement record - daRecordRevision, err := revision.GetLatestByObjectId(dataAgreementRecordId) + daRecordRevision, err := revision.GetLatestByObjectIdAndSchemaName(dataAgreementRecordId, config.DataAgreementRecord) if err != nil { m := fmt.Sprintf("Failed to fetch revision for data agreement record: %v", dataAgreementRecordId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/service/service_create_dataagreement_record.go b/internal/handler/v2/service/service_create_dataagreement_record.go index 3ea364c..b5b66b8 100644 --- a/internal/handler/v2/service/service_create_dataagreement_record.go +++ b/internal/handler/v2/service/service_create_dataagreement_record.go @@ -67,7 +67,7 @@ func ServiceCreateDataAgreementRecord(w http.ResponseWriter, r *http.Request) { // If revision id is missing, fetch latest revision if err != nil && errors.Is(err, daRecord.RevisionIdIsMissingError) { - rev, err = revision.GetLatestByObjectId(dataAgreementId) + rev, err = revision.GetLatestByObjectIdAndSchemaName(dataAgreementId, config.DataAgreement) if err != nil { m := fmt.Sprintf("Failed to fetch revision for data agreement: %v", dataAgreementId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/service/service_create_draft_consent_record.go b/internal/handler/v2/service/service_create_draft_consent_record.go index fa6f8ff..41922ec 100644 --- a/internal/handler/v2/service/service_create_draft_consent_record.go +++ b/internal/handler/v2/service/service_create_draft_consent_record.go @@ -69,7 +69,7 @@ func ServiceCreateDraftConsentRecord(w http.ResponseWriter, r *http.Request) { // If revision id is missing, fetch latest revision if err != nil && errors.Is(err, daRecord.RevisionIdIsMissingError) { - rev, err = revision.GetLatestByDataAgreementId(dataAgreementId) + rev, err = revision.GetLatestByObjectIdAndSchemaName(dataAgreementId, config.DataAgreement) if err != nil { m := fmt.Sprintf("Failed to fetch revision: %v", revisionId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/service/service_list_dataattributes_for_dataagreement.go b/internal/handler/v2/service/service_list_dataattributes_for_dataagreement.go index b06cae9..28c1f0a 100644 --- a/internal/handler/v2/service/service_list_dataattributes_for_dataagreement.go +++ b/internal/handler/v2/service/service_list_dataattributes_for_dataagreement.go @@ -52,7 +52,7 @@ func ServiceListDataAttributesForDataAgreement(w http.ResponseWriter, r *http.Re var daRevision revision.Revision if err != nil && errors.Is(err, RevisionIDIsMissingError) { - daRevision, err = revision.GetLatestByDataAgreementId(da.Id) + daRevision, err = revision.GetLatestByObjectIdAndSchemaName(da.Id, config.DataAgreement) if err != nil { m := fmt.Sprintf("Failed to fetch data agreement revision: %v", dataAgreementId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/service/service_read_dataagreement.go b/internal/handler/v2/service/service_read_dataagreement.go index 179da5a..5051e7a 100644 --- a/internal/handler/v2/service/service_read_dataagreement.go +++ b/internal/handler/v2/service/service_read_dataagreement.go @@ -37,7 +37,7 @@ func ServiceReadDataAgreement(w http.ResponseWriter, r *http.Request) { } var revisionResp revision.Revision - revisionResp, err = revision.GetLatestByDataAgreementId(da.Id) + revisionResp, err = revision.GetLatestByObjectIdAndSchemaName(da.Id, config.DataAgreement) if err != nil { m := fmt.Sprintf("Failed to fetch revision: %v", dataAgreementId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/service/service_read_policy.go b/internal/handler/v2/service/service_read_policy.go index 67e4cf6..ed356e3 100644 --- a/internal/handler/v2/service/service_read_policy.go +++ b/internal/handler/v2/service/service_read_policy.go @@ -51,7 +51,7 @@ func ServiceReadPolicy(w http.ResponseWriter, r *http.Request) { } } else { - revisionResp, err = revision.GetLatestByPolicyId(policyId) + revisionResp, err = revision.GetLatestByObjectIdAndSchemaName(policyId, config.Policy) if err != nil { m := fmt.Sprintf("Failed to fetch revision: %v", policyId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/service/service_update_dataagreement_record.go b/internal/handler/v2/service/service_update_dataagreement_record.go index faa0415..3457e33 100644 --- a/internal/handler/v2/service/service_update_dataagreement_record.go +++ b/internal/handler/v2/service/service_update_dataagreement_record.go @@ -89,7 +89,7 @@ func ServiceUpdateDataAgreementRecord(w http.ResponseWriter, r *http.Request) { return } - currentDataAgreementRecordRevision, err := revision.GetLatestByObjectId(dataAgreementRecordId) + currentDataAgreementRecordRevision, err := revision.GetLatestByObjectIdAndSchemaName(dataAgreementRecordId, config.DataAgreementRecord) if err != nil { m := fmt.Sprintf("Failed to fetch latest revision for data agreement record: %v", dataAgreementRecordId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) @@ -107,7 +107,7 @@ func ServiceUpdateDataAgreementRecord(w http.ResponseWriter, r *http.Request) { } toBeUpdatedDaRecord.OptIn = dataAgreementRecordReq.OptIn - currentDataAgreementRevision, err := revision.GetLatestByObjectId(dataAgreementId) + currentDataAgreementRevision, err := revision.GetLatestByObjectIdAndSchemaName(dataAgreementId, config.DataAgreement) if err != nil { m := fmt.Sprintf("Failed to fetch latest revision for data agreement: %v", dataAgreementId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/service/service_update_signature_object.go b/internal/handler/v2/service/service_update_signature_object.go index 673b351..5242158 100644 --- a/internal/handler/v2/service/service_update_signature_object.go +++ b/internal/handler/v2/service/service_update_signature_object.go @@ -76,7 +76,7 @@ func ServiceUpdateSignatureObject(w http.ResponseWriter, r *http.Request) { return } - currentDataAgreementRevision, err := revision.GetLatestByObjectId(toBeUpdatedDaRecord.DataAgreementId) + currentDataAgreementRevision, err := revision.GetLatestByObjectIdAndSchemaName(toBeUpdatedDaRecord.DataAgreementId, config.DataAgreement) if err != nil { m := fmt.Sprintf("Failed to fetch latest revision for data agreement: %v", toBeUpdatedDaRecord.DataAgreementId) common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/handler/v2/service/service_verification_fetch_dataagreement_record.go b/internal/handler/v2/service/service_verification_fetch_dataagreement_record.go index 91fabbc..b681bf5 100644 --- a/internal/handler/v2/service/service_verification_fetch_dataagreement_record.go +++ b/internal/handler/v2/service/service_verification_fetch_dataagreement_record.go @@ -34,7 +34,7 @@ func ServiceVerificationFetchDataAgreementRecord(w http.ResponseWriter, r *http. return } - currentRevision, err := revision.GetLatestByObjectId(daRecord.Id) + currentRevision, err := revision.GetLatestByObjectIdAndSchemaName(daRecord.Id, config.DataAgreementRecord) if err != nil { m := "Failed to fetch revision for data agreement record" common.HandleErrorV2(w, http.StatusInternalServerError, m, err) diff --git a/internal/policy/policy.go b/internal/policy/policy.go index 7c09988..7827c09 100644 --- a/internal/policy/policy.go +++ b/internal/policy/policy.go @@ -105,7 +105,10 @@ func CreatePipelineForFilteringPolicies(organisationId string) ([]primitive.M, e bson.M{ "$match": bson.M{ "$expr": bson.M{ - "$eq": []interface{}{"$objectid", bson.M{"$toString": "$$localId"}}, + "$and": bson.A{ + bson.M{"$eq": []interface{}{"$objectid", bson.M{"$toString": "$$localId"}}}, + bson.M{"$eq": []interface{}{"$schemaname", "policy"}}, + }, }, }, }, diff --git a/internal/revision/db.go b/internal/revision/db.go index eb0f7f3..168c293 100644 --- a/internal/revision/db.go +++ b/internal/revision/db.go @@ -37,38 +37,6 @@ func Update(revision Revision) (Revision, error) { return revision, err } -// Get Gets revision by policy id -func GetLatestByPolicyId(policyId string) (Revision, error) { - - var result Revision - opts := options.FindOne().SetSort(bson.M{"timestamp": -1}) - err := Collection().FindOne(context.TODO(), bson.M{"objectid": policyId}, opts).Decode(&result) - if err != nil { - return Revision{}, err - } - - return result, err -} - -// Get Gets revisions by policy id -func ListAllByPolicyId(policyId string) ([]Revision, error) { - - var results []Revision - opts := options.Find().SetSort(bson.M{"timestamp": -1}) - cursor, err := Collection().Find(context.TODO(), bson.M{"objectid": policyId}, opts) - if err != nil { - return []Revision{}, err - } - - defer cursor.Close(context.TODO()) - - if err := cursor.All(context.TODO(), &results); err != nil { - return []Revision{}, err - } - - return results, err -} - // GetByRevisionId Get revision by id func GetByRevisionId(revisionId string) (Revision, error) { var result Revision @@ -81,44 +49,24 @@ func GetByRevisionId(revisionId string) (Revision, error) { return result, err } -// Get Gets revision by data agreement id -func GetLatestByDataAgreementId(dataAgreementId string) (Revision, error) { - +// GetByRevisionIdAndSchema gets revision by id and schema +func GetByRevisionIdAndSchema(revisionId string, schemaName string) (Revision, error) { var result Revision - opts := options.FindOne().SetSort(bson.M{"timestamp": -1}) - err := Collection().FindOne(context.TODO(), bson.M{"objectid": dataAgreementId}, opts).Decode(&result) - if err != nil { - return Revision{}, err - } - - return result, err -} - -// Get Gets revisions by data agreement id -func ListAllByDataAgreementId(dataAgreementId string) ([]Revision, error) { - var results []Revision - opts := options.Find().SetSort(bson.M{"timestamp": -1}) - cursor, err := Collection().Find(context.TODO(), bson.M{"objectid": dataAgreementId}, opts) + err := Collection().FindOne(context.TODO(), bson.M{"_id": revisionId, "schemaname": schemaName}).Decode(&result) if err != nil { - return []Revision{}, err - } - - defer cursor.Close(context.TODO()) - - if err := cursor.All(context.TODO(), &results); err != nil { - return []Revision{}, err + return result, err } - return results, err + return result, err } -// Get Gets revision by data attribute id -func GetLatestByDataAttributeId(dataAttributeId string) (Revision, error) { +// GetLatestByObjectIdAndSchemaName Gets latest revision by object id and schema name +func GetLatestByObjectIdAndSchemaName(objectId string, schemaName string) (Revision, error) { var result Revision opts := options.FindOne().SetSort(bson.M{"timestamp": -1}) - err := Collection().FindOne(context.TODO(), bson.M{"objectid": dataAttributeId}, opts).Decode(&result) + err := Collection().FindOne(context.TODO(), bson.M{"objectid": objectId, "schemaname": schemaName}, opts).Decode(&result) if err != nil { return Revision{}, err } @@ -126,11 +74,12 @@ func GetLatestByDataAttributeId(dataAttributeId string) (Revision, error) { return result, err } -// Get Gets revisions by data attribute id -func ListAllByDataAttributeId(dataAttributeId string) ([]Revision, error) { +// ListAllByObjectIdAndSchemaName list revisions by object id and schema name +func ListAllByObjectIdAndSchemaName(objectId string, schemaName string) ([]Revision, error) { var results []Revision - cursor, err := Collection().Find(context.TODO(), bson.M{"objectid": dataAttributeId}) + opts := options.Find().SetSort(bson.M{"timestamp": -1}) + cursor, err := Collection().Find(context.TODO(), bson.M{"objectid": objectId, "schemaname": schemaName}, opts) if err != nil { return []Revision{}, err } @@ -143,28 +92,3 @@ func ListAllByDataAttributeId(dataAttributeId string) ([]Revision, error) { return results, err } - -// GetByRevisionIdAndSchema gets revision by id and schema -func GetByRevisionIdAndSchema(revisionId string, schemaName string) (Revision, error) { - var result Revision - - err := Collection().FindOne(context.TODO(), bson.M{"_id": revisionId, "schemaname": schemaName}).Decode(&result) - if err != nil { - return result, err - } - - return result, err -} - -// Get Gets revision by object id -func GetLatestByObjectId(objectId string) (Revision, error) { - - var result Revision - opts := options.FindOne().SetSort(bson.M{"timestamp": -1}) - err := Collection().FindOne(context.TODO(), bson.M{"objectid": objectId}, opts).Decode(&result) - if err != nil { - return Revision{}, err - } - - return result, err -} diff --git a/internal/revision/revisions.go b/internal/revision/revisions.go index dc712d2..bd838d6 100644 --- a/internal/revision/revisions.go +++ b/internal/revision/revisions.go @@ -159,7 +159,7 @@ func UpdateRevisionForPolicy(updatedPolicy policy.Policy, orgAdminId string) (Re r := Revision{} r.Init(objectData.Id, orgAdminId, config.Policy) // Query for previous revisions - previousRevision, err := GetLatestByPolicyId(updatedPolicy.Id) + previousRevision, err := GetLatestByObjectIdAndSchemaName(updatedPolicy.Id, config.Policy) if err != nil { // Previous revision is not present err = r.UpdateRevision(nil, objectData) @@ -344,7 +344,7 @@ func UpdateRevisionForDataAgreement(updatedDataAgreement dataagreement.DataAgree r.Init(objectData.Id, orgAdminId, config.DataAgreement) // Query for previous revisions - previousRevision, err := GetLatestByDataAgreementId(updatedDataAgreement.Id) + previousRevision, err := GetLatestByObjectIdAndSchemaName(updatedDataAgreement.Id, config.DataAgreement) if err != nil { // Previous revision is not present err = r.UpdateRevision(nil, objectData) @@ -492,7 +492,7 @@ func UpdateRevisionForDataAgreementRecord(updatedDataAgreementRecord daRecord.Da revision := Revision{} revision.Init(objectData.Id, orgAdminId, config.DataAgreementRecord) // Query for previous revisions - previousRevision, err := GetLatestByObjectId(updatedDataAgreementRecord.Id) + previousRevision, err := GetLatestByObjectIdAndSchemaName(updatedDataAgreementRecord.Id, config.DataAgreementRecord) if err != nil { return revision, err }