Skip to content

Commit

Permalink
Fix #610 Fetch revisions by objectId and schemaName
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa committed Jan 5, 2024
1 parent 0b0ff0d commit 9d41714
Show file tree
Hide file tree
Showing 21 changed files with 60 additions and 115 deletions.
10 changes: 8 additions & 2 deletions internal/dataagreement/dataagreements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
},
},
},
},
Expand Down Expand Up @@ -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"}},
},
},
},
},
Expand Down
20 changes: 16 additions & 4 deletions internal/dataagreement_record/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
},
},
},
},
Expand Down Expand Up @@ -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"}},
},
},
},
},
Expand Down Expand Up @@ -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"}},
},
},
},
},
Expand Down Expand Up @@ -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"}},
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/v2/config/policy/config_delete_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/v2/config/policy/config_read_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/v2/service/service_read_dataagreement.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/v2/service/service_read_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion internal/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
},
},
},
},
Expand Down
100 changes: 12 additions & 88 deletions internal/revision/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -81,56 +49,37 @@ 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
}

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
}
Expand All @@ -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
}
Loading

1 comment on commit 9d41714

@georgepadayatti
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@albinpa Can we split this PR into 13 commits - #610 (comment). This is too big a PR for me to review and approve.

Please sign in to comment.