Skip to content

Commit

Permalink
Fix #331 Config: Purpose field is required in the response of list al…
Browse files Browse the repository at this point in the history
…l data attributes
  • Loading branch information
albinpa authored and georgepadayatti committed Oct 24, 2023
1 parent 8b2dc7f commit 977ed5d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 50 deletions.
53 changes: 46 additions & 7 deletions src/v2/dataattribute/dataattributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ type DataAgreementForDataAttribute struct {
type DataAttributeForLists struct {
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Version string `json:"version"`
AgreementIds string `json:"-"`
AgreementIds []string `json:"agreementIds"`
Name string `json:"name" valid:"required"`
Description string `json:"description" valid:"required"`
Sensitivity bool `json:"sensitivity"`
Category string `json:"category"`
OrganisationId string `json:"-"`
IsDeleted bool `json:"-"`
DataAgreements interface{} `json:"agreementIds"`
AgreementData []DataAgreementForDataAttribute `json:"agreements"`
AgreementData []DataAgreementForDataAttribute `json:"dataAgreements"`
}
type DataAttributeRepository struct {
DefaultFilter bson.M
Expand Down Expand Up @@ -168,16 +167,18 @@ func ListDataAttributesBasedOnMethodOfUse(methodOfUse string, organisationId str

pipeline := []bson.M{
{"$match": bson.M{"organisationid": organisationId, "isdeleted": false}},
{"$addFields": bson.M{"dataAgreements": "$agreementids"}},
{"$unwind": "$agreementids"},
{"$lookup": bson.M{
"from": "dataAgreements",
"let": bson.M{"localId": "$agreementids"},
"let": bson.M{"localIds": "$agreementids"},
"pipeline": bson.A{
bson.M{
"$match": bson.M{
"$expr": bson.M{
"$eq": []interface{}{"$_id", bson.M{"$toObjectId": "$$localId"}},
"$in": bson.A{"$_id", bson.M{"$map": bson.M{
"input": "$$localIds",
"as": "r",
"in": bson.M{"$toObjectId": "$$r"},
}}},
},
},
},
Expand All @@ -198,3 +199,41 @@ func ListDataAttributesBasedOnMethodOfUse(methodOfUse string, organisationId str
}
return results, nil
}

// ListDataAttributesWithDataAgreement lists data attributes with data agreements
func ListDataAttributesWithDataAgreement(organisationId string) ([]DataAttributeForLists, error) {
var results []DataAttributeForLists

pipeline := []bson.M{
{"$match": bson.M{"organisationid": organisationId, "isdeleted": false}},
{"$lookup": bson.M{
"from": "dataAgreements",
"let": bson.M{"localIds": "$agreementids"},
"pipeline": bson.A{
bson.M{
"$match": bson.M{
"$expr": bson.M{
"$in": bson.A{"$_id", bson.M{"$map": bson.M{
"input": "$$localIds",
"as": "r",
"in": bson.M{"$toObjectId": "$$r"},
}}},
},
},
},
},
"as": "agreementData",
}},
}

cursor, err := Collection().Aggregate(context.TODO(), pipeline)
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
}
80 changes: 37 additions & 43 deletions src/v2/handler/dataattribute/config_list_dataattributes.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dataattribute

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -10,6 +9,7 @@ import (

"github.com/bb-consent/api/src/common"
"github.com/bb-consent/api/src/config"
"github.com/bb-consent/api/src/v2/dataagreement"
"github.com/bb-consent/api/src/v2/dataattribute"
"github.com/bb-consent/api/src/v2/paginate"
"github.com/bb-consent/api/src/v2/revision"
Expand Down Expand Up @@ -99,65 +99,39 @@ func ConfigListDataAttributes(w http.ResponseWriter, r *http.Request) {

methodOfUse, err := ParseMethodOfUseDataAttributesQueryParams(r)
methodOfUse = common.Sanitize(methodOfUse)
var res []dataattribute.DataAttributeForLists
if err != nil && errors.Is(err, MethodOfUseIsMissingError) {
// Repository
dataAttributeRepo := dataattribute.DataAttributeRepository{}
dataAttributeRepo.Init(organisationId)

// Return all data attributes
var dataAttributes []dataattribute.DataAttribute
query := paginate.PaginateDBObjectsQuery{
Filter: dataAttributeRepo.DefaultFilter,
Collection: dataattribute.Collection(),
Context: context.Background(),
Limit: limit,
Offset: offset,
}
result, err := paginate.PaginateDBObjects(query, &dataAttributes)
res, err = dataattribute.ListDataAttributesWithDataAgreement(organisationId)
if err != nil {
if errors.Is(err, paginate.EmptyDBError) {
emptyDataAttributes := make([]interface{}, 0)
resp = listDataAttributesResp{
DataAttributes: emptyDataAttributes,
Pagination: result.Pagination,
}
returnHTTPResponse(resp, w)
return
}
m := "Failed to paginate data attribute"
m := fmt.Sprintf("Failed to fetch data attribute by method of use: %v", methodOfUse)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return

}
resp = listDataAttributesResp{
DataAttributes: result.Items,
Pagination: result.Pagination,
}
returnHTTPResponse(resp, w)
return

} else {
// List by method of use
res, err := dataattribute.ListDataAttributesBasedOnMethodOfUse(methodOfUse, organisationId)
res, err = dataattribute.ListDataAttributesBasedOnMethodOfUse(methodOfUse, organisationId)
if err != nil {
m := fmt.Sprintf("Failed to fetch data attribute by method of use: %v", methodOfUse)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

query := paginate.PaginateObjectsQuery{
Limit: limit,
Offset: offset,
}
interfaceSlice := dataAttributesToInterfaceSlice(res)
result := paginate.PaginateObjects(query, interfaceSlice)
resp = listDataAttributesResp{
DataAttributes: result.Items,
Pagination: result.Pagination,
}
returnHTTPResponse(resp, w)
return
}
query := paginate.PaginateObjectsQuery{
Limit: limit,
Offset: offset,
}
interfaceSlice := dataAttributesToInterfaceSlice(res)
result := paginate.PaginateObjects(query, interfaceSlice)
resp = listDataAttributesResp{
DataAttributes: result.Items,
Pagination: result.Pagination,
}
returnHTTPResponse(resp, w)
return

} else {
// Fetch revision by id
Expand All @@ -175,6 +149,26 @@ func ConfigListDataAttributes(w http.ResponseWriter, r *http.Request) {
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}
// Repository
darepo := dataagreement.DataAgreementRepository{}
darepo.Init(organisationId)

var dataAgreements []dataattribute.DataAgreementForDataAttribute
for _, a := range da.AgreementIds {
var dA dataattribute.DataAgreementForDataAttribute
dataAgreement, err := darepo.Get(a)
if err != nil {
m := fmt.Sprintf("Failed to fetch data agreement by revision: %v", revisionId)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}
dA.Id = dataAgreement.Id.Hex()
dA.Purpose = dataAgreement.Purpose
dataAgreements = append(dataAgreements, dA)
}

var dataAttributes dataattribute.DataAttributeForLists
dataAttributes.AgreementData = dataAgreements

interfaceSlice := make([]interface{}, 0)
interfaceSlice = append(interfaceSlice, da)
Expand Down

0 comments on commit 977ed5d

Please sign in to comment.