Skip to content

Commit

Permalink
Add #246 Integrate data attribute endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa authored and georgepadayatti committed Oct 17, 2023
1 parent 2531481 commit 6d58c52
Show file tree
Hide file tree
Showing 22 changed files with 1,050 additions and 268 deletions.
1 change: 1 addition & 0 deletions src/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
DataAgreement = "dataAgreement"
Policy = "policy"
DataAgreementRecord = "dataAgreementRecord"
DataAttribute = "dataAttribute"
)

// Data Agreement Method of Use
Expand Down
15 changes: 13 additions & 2 deletions src/dataagreement/dataagreements.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func (darepo *DataAgreementRepository) Get(dataAgreementId string) (DataAgreemen

var result DataAgreement
err := Collection().FindOne(context.TODO(), filter).Decode(&result)

return result, err
}

Expand All @@ -94,5 +93,17 @@ func (darepo *DataAgreementRepository) Update(dataAgreement DataAgreement) (Data
if err != nil {
return dataAgreement, err
}
return dataAgreement, err
return dataAgreement, nil
}

// IsDataAgreementExist Check if data agreement with given id exists
func (darepo *DataAgreementRepository) IsDataAgreementExist(dataAgreementId string) (int64, error) {

filter := common.CombineFilters(darepo.DefaultFilter, bson.M{"_id": dataAgreementId})

exists, err := Collection().CountDocuments(context.TODO(), filter)
if err != nil {
return exists, err
}
return exists, nil
}
140 changes: 140 additions & 0 deletions src/dataattribute/dataattributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package dataattribute

import (
"context"
"log"

"github.com/bb-consent/api/src/common"
"github.com/bb-consent/api/src/database"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

func Collection() *mongo.Collection {
return database.DB.Client.Database(database.DB.Name).Collection("dataAttributes")
}

type DataAttribute struct {
Id string `json:"id" bson:"_id,omitempty"`
Version string `json:"version"`
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:"-"`
}

type DataAttributeRepository struct {
DefaultFilter bson.M
}

// Init
func (dataAttributeRepo *DataAttributeRepository) Init(organisationId string) {
dataAttributeRepo.DefaultFilter = bson.M{"organisationid": organisationId, "isdeleted": false}
}

// Add Adds the data attribute to the db
func (dataAttributeRepo *DataAttributeRepository) Add(dataAttribute DataAttribute) (DataAttribute, error) {

_, err := Collection().InsertOne(context.TODO(), dataAttribute)
if err != nil {
return DataAttribute{}, err
}

return dataAttribute, nil
}

// Get Gets a single data attribute by given id
func (dataAttributeRepo *DataAttributeRepository) Get(dataAttributeId string) (DataAttribute, error) {

filter := common.CombineFilters(dataAttributeRepo.DefaultFilter, bson.M{"_id": dataAttributeId})

var result DataAttribute
err := Collection().FindOne(context.TODO(), filter).Decode(&result)

return result, err
}

// Update Updates the data attribute
func (dataAttributeRepo *DataAttributeRepository) Update(dataAttribute DataAttribute) (DataAttribute, error) {

filter := common.CombineFilters(dataAttributeRepo.DefaultFilter, bson.M{"_id": dataAttribute.Id})
update := bson.M{"$set": dataAttribute}

_, err := Collection().UpdateOne(context.TODO(), filter, update)
if err != nil {
return dataAttribute, err
}
return dataAttribute, err
}

// Gets data attributes by data agreement id
func (dataAttributeRepo *DataAttributeRepository) GetDataAttributesByDataAgreementId(dataAgreementId string) ([]DataAttribute, error) {
filter := common.CombineFilters(dataAttributeRepo.DefaultFilter, bson.M{
"agreementids": bson.M{
"$in": []string{dataAgreementId},
},
})

cursor, err := Collection().Find(context.Background(), filter)
if err != nil {
log.Fatal(err)
}
defer cursor.Close(context.Background())

var dataAttributes []DataAttribute
for cursor.Next(context.Background()) {
var dataAttribute DataAttribute
if err := cursor.Decode(&dataAttribute); err != nil {
return dataAttributes, err
}
dataAttributes = append(dataAttributes, dataAttribute)
}

if err := cursor.Err(); err != nil {
return dataAttributes, err
}

return dataAttributes, err
}

// Removes data agreement id from data attributes
func (dataAttributeRepo *DataAttributeRepository) RemoveDataAgreementIdFromDataAttributes(dataAgreementId string) error {
filter := common.CombineFilters(dataAttributeRepo.DefaultFilter, bson.M{"agreementids": dataAgreementId})

update := bson.M{
"$pull": bson.M{"agreementids": dataAgreementId},
}

_, err := Collection().UpdateMany(context.Background(), filter, update)
if err != nil {
return err
}
err = dataAttributeRepo.DeleteDataAttributesIfDataAgreementIdsIsEmpty()
if err != nil {
return err
}

return nil
}

// delete data attributes if data agreement ids is empty
func (dataAttributeRepo *DataAttributeRepository) DeleteDataAttributesIfDataAgreementIdsIsEmpty() error {
filter := common.CombineFilters(dataAttributeRepo.DefaultFilter, bson.M{"agreementids": bson.M{"$exists": true, "$eq": []string{}}})

// Update to set IsDeleted to true
update := bson.M{
"$set": bson.M{
"isdeleted": true,
},
}

_, err := Collection().UpdateMany(context.TODO(), filter, update)
if err != nil {
return err
}

return nil
}
5 changes: 5 additions & 0 deletions src/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ func Init(config *config.Configuration) error {
return err
}

err = initCollection("dataAttributes", []string{"id"}, true)
if err != nil {
return err
}

return nil
}

Expand Down
31 changes: 31 additions & 0 deletions src/revision/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,34 @@ func ListAllByDataAgreementId(dataAgreementId string) ([]Revision, error) {

return results, err
}

// Get Gets revision by data attribute id
func GetLatestByDataAttributeId(dataAttributeId 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)
if err != nil {
return Revision{}, err
}

return result, err
}

// Get Gets revisions by data attribute id
func ListAllByDataAttributeId(dataAttributeId string) ([]Revision, error) {

var results []Revision
cursor, err := Collection().Find(context.TODO(), bson.M{"objectid": dataAttributeId})
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
}
72 changes: 72 additions & 0 deletions src/revision/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/bb-consent/api/src/common"
"github.com/bb-consent/api/src/config"
"github.com/bb-consent/api/src/dataagreement"
"github.com/bb-consent/api/src/dataattribute"
"github.com/bb-consent/api/src/policy"
"go.mongodb.org/mongo-driver/bson/primitive"
)
Expand Down Expand Up @@ -302,3 +303,74 @@ func RecreateDataAgreementFromRevision(revision Revision) (dataagreement.DataAgr

return da, nil
}

type dataAttributeForObjectData struct {
Id string `json:"id"`
Version string `json:"version"`
AgreementIds []string `json:"agreementIds"`
Name string `json:"name" valid:"required"`
Description string `json:"description" valid:"required"`
Sensitivity bool `json:"sensitivity"`
Category string `json:"category"`
}

// CreateRevisionForDataAttribute
func CreateRevisionForDataAttribute(newDataAttribute dataattribute.DataAttribute, orgAdminId string) (Revision, error) {
// Object data
objectData := dataAttributeForObjectData{
Id: newDataAttribute.Id,
Version: newDataAttribute.Version,
AgreementIds: newDataAttribute.AgreementIds,
Name: newDataAttribute.Name,
Description: newDataAttribute.Description,
Sensitivity: newDataAttribute.Sensitivity,
Category: newDataAttribute.Category,
}

// Create revision
revision := Revision{}
revision.Init(objectData.Id, orgAdminId, config.DataAttribute)
err := revision.CreateRevision(objectData)

return revision, err
}

// UpdateRevisionForDataAttribute
func UpdateRevisionForDataAttribute(updatedDataAttribute dataattribute.DataAttribute, previousRevision *Revision, orgAdminId string) (Revision, error) {
// Object data
objectData := dataAttributeForObjectData{
Id: updatedDataAttribute.Id,
Version: updatedDataAttribute.Version,
AgreementIds: updatedDataAttribute.AgreementIds,
Name: updatedDataAttribute.Name,
Description: updatedDataAttribute.Description,
Sensitivity: updatedDataAttribute.Sensitivity,
Category: updatedDataAttribute.Category,
}

// Update revision
revision := Revision{}
revision.Init(objectData.Id, orgAdminId, config.DataAttribute)
err := revision.UpdateRevision(previousRevision, objectData)

return revision, err
}

func RecreateDataAttributeFromRevision(revision Revision) (dataattribute.DataAttribute, error) {

// Deserialise revision snapshot
var r Revision
err := json.Unmarshal([]byte(revision.SerializedSnapshot), &r)
if err != nil {
return dataattribute.DataAttribute{}, err
}

// Deserialise data attribute
var da dataattribute.DataAttribute
err = json.Unmarshal([]byte(r.ObjectData), &da)
if err != nil {
return dataattribute.DataAttribute{}, err
}

return da, nil
}
85 changes: 0 additions & 85 deletions src/v2/handler/adddataattribute_handler.go

This file was deleted.

Loading

0 comments on commit 6d58c52

Please sign in to comment.