Skip to content

Commit

Permalink
Add #463 List data agreements filtered 'complete' lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa authored and georgepadayatti committed Nov 7, 2023
1 parent 4141232 commit 81f7691
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 26 deletions.
20 changes: 20 additions & 0 deletions internal/dataagreement/dataagreements.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

func Collection() *mongo.Collection {
Expand Down Expand Up @@ -331,3 +332,22 @@ func GetAllDataAgreementsWithLatestRevisionsObjectData(organisationId string) ([

return results, nil
}

// GetDataAgreementsByLifecycle
func (darepo *DataAgreementRepository) GetDataAgreementsByLifecycle(lifecycle string) ([]DataAgreement, error) {
filter := common.CombineFilters(darepo.DefaultFilter, bson.M{"lifecycle": lifecycle})
options := options.Find().SetSort(bson.D{{Key: "timestamp", Value: -1}})

var results []DataAgreement
cursor, err := Collection().Find(context.TODO(), filter, options)
if err != nil {
return results, err
}

defer cursor.Close(context.TODO())

if err := cursor.All(context.TODO(), &results); err != nil {
return results, err
}
return results, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,60 @@ func ParseListDataAgreementsLifecycleQueryParams(r *http.Request) (lifecycle str
return "", LifecycleIsMissingError
}

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

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

return activeDataAgreements, nil
}

// list data agreements based on lifecycle
func listDataAgreementsBasedOnLifecycle(lifecycle string, organisationId string) ([]interface{}, error) {
darepo := dataagreement.DataAgreementRepository{}
darepo.Init(organisationId)

var dataAgreements []interface{}
var err error

switch lifecycle {
case config.Complete:
dataAgreements, err = activeDataAgreementsFromObjectData(organisationId)
if err != nil {
return dataAgreements, err
}
case config.Draft:
draftDataAgreements, err := darepo.GetDataAgreementsByLifecycle(lifecycle)
if err != nil {
return dataAgreements, err
}
dataAgreements = dataAgreementsToInterfaceSlice(draftDataAgreements)

}
return dataAgreements, nil
}

func dataAgreementsToInterfaceSlice(dataAgreements []dataagreement.DataAgreement) []interface{} {
interfaceSlice := make([]interface{}, len(dataAgreements))
for i, r := range dataAgreements {
interfaceSlice[i] = r
}
return interfaceSlice
}

type listDataAgreementsResp struct {
DataAgreements interface{} `json:"dataAgreements"`
Pagination paginate.Pagination `json:"pagination"`
Expand Down Expand Up @@ -130,44 +184,28 @@ func ConfigListDataAgreements(w http.ResponseWriter, r *http.Request) {
returnHTTPResponse(resp, w)
return
} else {
pipeline, err := dataagreement.CreatePipelineForFilteringDataAgreementsUsingLifecycle(organisationId, lifecycle)

dataAgreements, err := listDataAgreementsBasedOnLifecycle(lifecycle, organisationId)
if err != nil {
m := "Failed to create pipeline"
m := "Failed to fetch data agreements"
common.HandleErrorV2(w, http.StatusInternalServerError, m, 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, dataAgreements)

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

}

} else {
Expand Down

0 comments on commit 81f7691

Please sign in to comment.