Skip to content

Commit

Permalink
Fix #459 config list data agreements api response not sorting first d…
Browse files Browse the repository at this point in the history
…raft data agreement
  • Loading branch information
albinpa authored and georgepadayatti committed Nov 6, 2023
1 parent cc521b2 commit caf4256
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
5 changes: 5 additions & 0 deletions internal/dataagreement/dataagreements.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dataagreement

import (
"context"
"time"

"github.com/bb-consent/api/internal/common"
"github.com/bb-consent/api/internal/database"
Expand Down Expand Up @@ -66,6 +67,7 @@ type DataAgreement struct {
DataAttributes []DataAttribute `json:"dataAttributes" valid:"required"`
OrganisationId string `json:"-"`
IsDeleted bool `json:"-"`
Timestamp string `json:"-"`
}

type DataAgreementWithObjectData struct {
Expand All @@ -85,6 +87,8 @@ func (darepo *DataAgreementRepository) Init(organisationId string) {
// Add Adds the data agreement to the db
func (darepo *DataAgreementRepository) Add(dataAgreement DataAgreement) (DataAgreement, error) {

dataAgreement.Timestamp = time.Now().UTC().Format("2006-01-02T15:04:05Z")

_, err := Collection().InsertOne(context.TODO(), dataAgreement)
if err != nil {
return DataAgreement{}, err
Expand All @@ -109,6 +113,7 @@ func (darepo *DataAgreementRepository) Get(dataAgreementID string) (DataAgreemen

// Update Updates the data agreement
func (darepo *DataAgreementRepository) Update(dataAgreement DataAgreement) (DataAgreement, error) {
dataAgreement.Timestamp = time.Now().UTC().Format("2006-01-02T15:04:05Z")

filter := common.CombineFilters(darepo.DefaultFilter, bson.M{"_id": dataAgreement.Id})
update := bson.M{"$set": dataAgreement}
Expand Down
10 changes: 3 additions & 7 deletions internal/handler/v2/audit/audit_list_dataagreements.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,12 @@ func AuditListDataAgreements(w http.ResponseWriter, r *http.Request) {

var resp listDataAgreementsResp

pipeline, err := dataagreement.CreatePipelineForFilteringDataAgreements(organisationId, true)
if err != nil {
m := "Failed to create pipeline"
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}
var pipeline []bson.M
pipeline = append(pipeline, bson.M{"$match": bson.M{"organisationid": organisationId, "isdeleted": false}})
pipeline = append(pipeline, bson.M{"$sort": bson.M{"timestamp": -1}})

// Return all data agreements
var dataAgreements []dataagreement.DataAgreement
pipeline = append(pipeline, bson.M{"$sort": bson.M{"timestamp": -1}})
query := paginate.PaginateDBObjectsQueryUsingPipeline{
Pipeline: pipeline,
Collection: dataagreement.Collection(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,12 @@ func ConfigListDataAgreements(w http.ResponseWriter, r *http.Request) {
darepo.Init(organisationId)

if err != nil && errors.Is(err, LifecycleIsMissingError) {
pipeline, err := dataagreement.CreatePipelineForFilteringDataAgreements(organisationId, true)
if err != nil {
m := "Failed to create pipeline"
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

var pipeline []bson.M
pipeline = append(pipeline, bson.M{"$match": bson.M{"organisationid": organisationId, "isdeleted": false}})
pipeline = append(pipeline, bson.M{"$sort": bson.M{"timestamp": -1}})
// Return all data agreements
var dataAgreements []dataagreement.DataAgreement
pipeline = append(pipeline, bson.M{"$sort": bson.M{"timestamp": -1}})

query := paginate.PaginateDBObjectsQueryUsingPipeline{
Pipeline: pipeline,
Collection: dataagreement.Collection(),
Expand Down
52 changes: 52 additions & 0 deletions internal/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ package migrate
import (
"context"
"fmt"
"time"

"github.com/bb-consent/api/internal/apikey"
"github.com/bb-consent/api/internal/dataagreement"
"github.com/bb-consent/api/internal/org"
"github.com/bb-consent/api/internal/policy"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)

func Migrate() {
migrateThirdPartyDataSharingToTrueInPolicyCollection()
migrateThirdPartyDataSharingToTrueInDataAgreementsCollection()
migrateNameInApiKeyCollection()
migrateTimestampInDataAgreementsCollection()
}

func migrateThirdPartyDataSharingToTrueInPolicyCollection() {
Expand Down Expand Up @@ -51,3 +55,51 @@ func migrateNameInApiKeyCollection() {
fmt.Println(err)
}
}

type dataAgreement struct {
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Timestamp string `json:"timestamp"`
}

func migrateTimestampInDataAgreementsCollection() {
dataAgreementCollection := dataagreement.Collection()

// Get first organisation
o, err := org.GetFirstOrganization()
if err != nil {
panic(err)
}

pipeline, err := dataagreement.CreatePipelineForFilteringDataAgreements(o.ID.Hex(), true)
if err != nil {
fmt.Println(err)
}
var results []dataAgreement
cursor, err := dataAgreementCollection.Aggregate(context.Background(), pipeline)
if err != nil {
fmt.Println(err)
}
defer cursor.Close(context.Background())

if err := cursor.All(context.Background(), &results); err != nil {
fmt.Println(err)
}
var timestamp string

for _, dataAgreement := range results {
if len(dataAgreement.Timestamp) >= 1 {
timestamp = dataAgreement.Timestamp
} else {
timestamp = time.Now().UTC().Format("2006-01-02T15:04:05Z")
}

filter := bson.M{"_id": dataAgreement.Id, "timestamp": bson.M{"$exists": false}}
update := bson.M{"$set": bson.M{"timestamp": timestamp}}

_, err := dataAgreementCollection.UpdateOne(context.TODO(), filter, update)
if err != nil {
fmt.Println(err)
}
}

}

0 comments on commit caf4256

Please sign in to comment.