Skip to content

Commit

Permalink
Fix #444 In privacy dashboard only the latest published version is shown
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa authored and georgepadayatti committed Nov 3, 2023
1 parent d5c4908 commit 44ecc4c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 49 deletions.
57 changes: 50 additions & 7 deletions internal/dataagreement/dataagreements.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ type DataAgreement struct {
IsDeleted bool `json:"-"`
}

type DataAgreementWithObjectData struct {
DataAgreement
ObjectData string `json:"objectData"`
}

type DataAgreementRepository struct {
DefaultFilter bson.M
}
Expand Down Expand Up @@ -133,7 +138,7 @@ func (darepo *DataAgreementRepository) IsDataAgreementExist(dataAgreementID stri
}

// CreatePipelineForFilteringDataAgreements This pipeline is used for filtering data agreements
func CreatePipelineForFilteringDataAgreements(organisationId string) ([]primitive.M, error) {
func CreatePipelineForFilteringDataAgreements(organisationId string, removeRevisions bool) ([]primitive.M, error) {

var pipeline []bson.M

Expand Down Expand Up @@ -173,12 +178,14 @@ func CreatePipelineForFilteringDataAgreements(organisationId string) ([]primitiv
},
}}})

// Stage 4 - Remove revisions field
pipeline = append(pipeline, bson.M{
"$project": bson.M{
"revisions": 0,
},
})
if removeRevisions {
// Stage 4 - Remove revisions field
pipeline = append(pipeline, bson.M{
"$project": bson.M{
"revisions": 0,
},
})
}

return pipeline, nil
}
Expand Down Expand Up @@ -283,3 +290,39 @@ func (darepo *DataAgreementRepository) GetAll() ([]DataAgreement, error) {
}
return results, nil
}

// GetAllDataAgreementsWithLatestRevisionsObjectData
func GetAllDataAgreementsWithLatestRevisionsObjectData(organisationId string) ([]DataAgreementWithObjectData, error) {

var results []DataAgreementWithObjectData

pipeline, err := CreatePipelineForFilteringDataAgreements(organisationId, false)
if err != nil {
return results, err
}
// Stage 4 - Add the object data from revisions
pipeline = append(pipeline, bson.M{"$addFields": bson.M{"objectData": bson.M{
"$let": bson.M{
"vars": bson.M{
"first": bson.M{
"$arrayElemAt": bson.A{"$revisions", 0},
},
},
"in": "$$first.objectdata",
},
}}})
pipeline = append(pipeline, bson.M{"$sort": bson.M{"timestamp": -1}})

// Perform the aggregation
cursor, err := Collection().Aggregate(context.Background(), pipeline)
if err != nil {
return results, err
}
defer cursor.Close(context.Background())

if err := cursor.All(context.Background(), &results); err != nil {
return results, err
}

return results, nil
}
2 changes: 1 addition & 1 deletion internal/handler/v2/audit/audit_list_dataagreements.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func AuditListDataAgreements(w http.ResponseWriter, r *http.Request) {

var resp listDataAgreementsResp

pipeline, err := dataagreement.CreatePipelineForFilteringDataAgreements(organisationId)
pipeline, err := dataagreement.CreatePipelineForFilteringDataAgreements(organisationId, true)
if err != nil {
m := "Failed to create pipeline"
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func ConfigListDataAgreements(w http.ResponseWriter, r *http.Request) {
darepo.Init(organisationId)

if err != nil && errors.Is(err, LifecycleIsMissingError) {
pipeline, err := dataagreement.CreatePipelineForFilteringDataAgreements(organisationId)
pipeline, err := dataagreement.CreatePipelineForFilteringDataAgreements(organisationId, true)
if err != nil {
m := "Failed to create pipeline"
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
Expand Down
67 changes: 27 additions & 40 deletions internal/handler/v2/service/service_list_dataagreements.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package service

import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
Expand All @@ -13,7 +11,6 @@ import (
"github.com/bb-consent/api/internal/dataagreement"
"github.com/bb-consent/api/internal/paginate"
"github.com/bb-consent/api/internal/revision"
"go.mongodb.org/mongo-driver/bson"
)

// ListDataAgreementsError is an error enumeration for list data agreement API.
Expand Down Expand Up @@ -47,18 +44,30 @@ func ParseListDataAgreementsQueryParams(r *http.Request) (revisionId string, err
return "", RevisionIDIsMissingError
}

func activeDataAgreementsFromObjectData(organisationId string) ([]interface{}, error) {
var activeDataAgreements []interface{}
dataAgreements, err := dataagreement.GetAllDataAgreementsWithLatestRevisionsObjectData(organisationId)
if err != nil {
return activeDataAgreements, err
}

for _, dataAgreement := range dataAgreements {
// Recreate data agreement from revision
activeDataAgreement, err := revision.RecreateDataAgreementFromObjectData(dataAgreement.ObjectData)
if err != nil {
return activeDataAgreements, err
}
activeDataAgreements = append(activeDataAgreements, activeDataAgreement)
}

return activeDataAgreements, nil
}

type listDataAgreementsResp struct {
DataAgreements interface{} `json:"dataAgreements"`
Pagination paginate.Pagination `json:"pagination"`
}

func returnHTTPResponse(resp interface{}, w http.ResponseWriter) {
response, _ := json.Marshal(resp)
w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON)
w.WriteHeader(http.StatusOK)
w.Write(response)
}

// ServiceListDataAgreements
func ServiceListDataAgreements(w http.ResponseWriter, r *http.Request) {
// Headers
Expand All @@ -73,49 +82,27 @@ func ServiceListDataAgreements(w http.ResponseWriter, r *http.Request) {
revisionId, err := ParseListDataAgreementsQueryParams(r)
revisionId = common.Sanitize(revisionId)
if err != nil && errors.Is(err, RevisionIDIsMissingError) {
lifecycle := config.Complete

darepo := dataagreement.DataAgreementRepository{}
darepo.Init(organisationId)

pipeline, err := dataagreement.CreatePipelineForFilteringDataAgreementsUsingLifecycle(organisationId, lifecycle)
activeDataAgreements, err := activeDataAgreementsFromObjectData(organisationId)
if err != nil {
m := "Failed to create pipeline"
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
common.HandleErrorV2(w, http.StatusInternalServerError, "Failed to fetch active data agreements", err)
return
}

// Return liecycle filtered data agreements
var dataAgreements []dataagreement.DataAgreement
pipeline = append(pipeline, bson.M{"$sort": bson.M{"timestamp": -1}})
query := paginate.PaginateDBObjectsQueryUsingPipeline{
Pipeline: pipeline,
Collection: dataagreement.Collection(),
Context: context.Background(),
Limit: limit,
Offset: offset,
query := paginate.PaginateObjectsQuery{
Limit: limit,
Offset: offset,
}
result, err := paginate.PaginateDBObjectsUsingPipeline(query, &dataAgreements)
if err != nil {
if errors.Is(err, paginate.EmptyDBError) {
emptyDataAgreements := make([]interface{}, 0)
resp = listDataAgreementsResp{
DataAgreements: emptyDataAgreements,
Pagination: result.Pagination,
}
returnHTTPResponse(resp, w)
return
}
m := "Failed to paginate data agreement"
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
result := paginate.PaginateObjects(query, activeDataAgreements)

}
resp = listDataAgreementsResp{
DataAgreements: result.Items,
Pagination: result.Pagination,
}
returnHTTPResponse(resp, w)
common.ReturnHTTPResponse(resp, w)
return

} else {
Expand Down Expand Up @@ -153,5 +140,5 @@ func ServiceListDataAgreements(w http.ResponseWriter, r *http.Request) {

}

returnHTTPResponse(resp, w)
common.ReturnHTTPResponse(resp, w)
}
12 changes: 12 additions & 0 deletions internal/revision/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,18 @@ func CreateRevisionForDraftDataAgreement(newDataAgreement dataagreement.DataAgre
return revision, err
}

func RecreateDataAgreementFromObjectData(objectData string) (interface{}, error) {

// Deserialise data agreement
var da interface{}
err := json.Unmarshal([]byte(objectData), &da)
if err != nil {
return nil, err
}

return da, nil
}

type dataAgreementRecordForObjectData struct {
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
DataAgreementId string `json:"dataAgreementId"`
Expand Down

0 comments on commit 44ecc4c

Please sign in to comment.