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 #610 Fetch revisions by objectId and schemaName #611

Merged
merged 17 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
6c167d7
Fix #610 Fetch revisions by objectId and schemaName in revision pipeline
albinpa Jan 5, 2024
b987927
Fix #610 Fetch revisions by objectId and schemaName in config delete …
albinpa Jan 5, 2024
2a3c788
Fix #610 Fetch revisions by objectId and schemaName in config list da…
albinpa Jan 5, 2024
e4860ce
Fix #610 Fetch revisions by objectId and schemaName in config list da…
albinpa Jan 5, 2024
0bdec5e
Fix #610 Fetch revisions by objectId and schemaName in config read da…
albinpa Jan 5, 2024
c4a49c6
Fix #610 Fetch revisions by objectId and schemaName in config delete …
albinpa Jan 5, 2024
7d10896
Fix #610 Fetch revisions by objectId and schemaName in config list po…
albinpa Jan 5, 2024
131f49e
Fix #610 Fetch revisions by objectId and schemaName in config read po…
albinpa Jan 5, 2024
cdcbc85
Fix #610 Fetch revisions by objectId and schemaName in service create…
albinpa Jan 5, 2024
e31b192
Fix #610 Fetch revisions by objectId and schemaName in service create…
albinpa Jan 5, 2024
2de0cd4
Fix #610 Fetch revisions by objectId and schemaName in service create…
albinpa Jan 5, 2024
e149796
Fix #610 Fetch revisions by objectId and schemaName in service list d…
albinpa Jan 5, 2024
15e0ef3
Fix #610 Fetch revisions by objectId and schemaName in service read d…
albinpa Jan 5, 2024
04e529b
Fix #610 Fetch revisions by objectId and schemaName in service update…
albinpa Jan 5, 2024
9c5bd1b
Fix #610 Fetch revisions by objectId and schemaName in service update…
albinpa Jan 5, 2024
3ca9d42
Fix #610 Fetch revisions by objectId and schemaName in verification f…
albinpa Jan 5, 2024
81ea1e9
Fix #610 Fetch revisions by objectId and schemaName in service read p…
albinpa Jan 5, 2024
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
11 changes: 9 additions & 2 deletions internal/dataagreement/dataagreements.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/bb-consent/api/internal/common"
"github.com/bb-consent/api/internal/config"
"github.com/bb-consent/api/internal/database"
"github.com/bb-consent/api/internal/policy"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -157,7 +158,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", config.DataAgreement}},
},
},
},
},
Expand Down Expand Up @@ -210,7 +214,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", config.DataAgreement}},
},
},
},
},
Expand Down
21 changes: 17 additions & 4 deletions internal/dataagreement_record/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/bb-consent/api/internal/common"
"github.com/bb-consent/api/internal/config"
"github.com/bb-consent/api/internal/database"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
Expand Down Expand Up @@ -123,7 +124,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", config.DataAgreementRecord}},
},
},
},
},
Expand Down Expand Up @@ -167,7 +171,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", config.DataAgreementRecord}},
},
},
},
},
Expand Down Expand Up @@ -218,7 +225,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", config.DataAgreementRecord}},
},
},
},
},
Expand Down Expand Up @@ -269,7 +279,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", config.DataAgreementRecord}},
},
},
},
},
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
6 changes: 5 additions & 1 deletion internal/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package policy
import (
"context"

"github.com/bb-consent/api/internal/config"
"github.com/bb-consent/api/internal/database"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
Expand Down Expand Up @@ -105,7 +106,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", config.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