Skip to content

Commit

Permalink
Add #207 align audit apis
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa authored and georgepadayatti committed Oct 24, 2023
1 parent 6f973da commit e70d3ba
Show file tree
Hide file tree
Showing 13 changed files with 585 additions and 64 deletions.
1 change: 1 addition & 0 deletions src/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
ApiKeyId = "apiKeyId"
IndividualHeaderKey = "X-ConsentBB-IndividualId"
RevisionId = "revisionId"
LawfulBasis = "lawfulBasis"
)

// Schemas
Expand Down
24 changes: 24 additions & 0 deletions src/v2/dataagreement_record/dataagreement_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ type DataAttributeForDataAgreementRecord struct {
OptIn bool `json:"optIn"`
}

type DataAgreementForListDataAgreementRecord struct {
Id string `json:"id" bson:"_id,omitempty"`
Purpose string `json:"purpose"`
}

type DataAgreementRecordForAuditList struct {
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
DataAgreementId string `json:"dataAgreementId"`
DataAgreementRevisionId string `json:"dataAgreementRevisionId"`
DataAgreementRevisionHash string `json:"dataAgreementRevisionHash"`
DataAttributes []DataAttributeForDataAgreementRecord `json:"dataAttributes"`
IndividualId string `json:"individualId"`
OptIn bool `json:"optIn"`
State string `json:"state" valid:"required"`
SignatureId string `json:"signatureId"`
AgreementData []DataAgreementForListDataAgreementRecord `json:"dataAgreement"`
}

// DataAgreementRecordError is an error enumeration for create consent record API.
type DataAgreementRecordError int

Expand All @@ -36,6 +54,8 @@ const (
IndividualIdIsMissingError DataAgreementRecordError = iota
DataAgreementIdIsMissingError
RevisionIdIsMissingError
DataAgreementRecordIdIsMissingError
LawfulBasisIsMissingError
)

// Error returns the string representation of the error.
Expand All @@ -47,6 +67,10 @@ func (e DataAgreementRecordError) Error() string {
return "Query param dataAgreementId is missing!"
case RevisionIdIsMissingError:
return "Query param revisionId is missing!"
case DataAgreementRecordIdIsMissingError:
return "Query param dataAgreementRecordId is missing!"
case LawfulBasisIsMissingError:
return "Query param lawfulbasis is missing!"
default:
return "Unknown error!"
}
Expand Down
240 changes: 240 additions & 0 deletions src/v2/dataagreement_record/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,243 @@ func (darRepo *DataAgreementRecordRepository) DeleteAllRecordsForIndividual(indi

return err
}

// ListByIdIncludingDataAgreement lists data agreement record by id
func ListByIdIncludingDataAgreement(dataAgreementRecordID string, organisationId string) ([]DataAgreementRecordForAuditList, error) {
var results []DataAgreementRecordForAuditList

dataAgreementRecordId, err := primitive.ObjectIDFromHex(dataAgreementRecordID)
if err != nil {
return results, err
}

pipeline := []bson.M{
{"$match": bson.M{"organisationid": organisationId, "isdeleted": false, "_id": dataAgreementRecordId}},
{"$lookup": bson.M{
"from": "dataAgreements",
"let": bson.M{"localId": "$dataagreementid"},
"pipeline": bson.A{
bson.M{
"$match": bson.M{
"$expr": bson.M{
"$eq": []interface{}{"$_id", bson.M{"$toObjectId": "$$localId"}},
},
},
},
},
"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
}

// ListByIdAndLawfulBasis lists data attributes based on data agreement record and lawfulbasis
func ListByIdAndLawfulBasis(dataAgreementRecordID string, organisationId string, lawfulBasis string) ([]DataAgreementRecordForAuditList, error) {
var results []DataAgreementRecordForAuditList

dataAgreementRecordId, err := primitive.ObjectIDFromHex(dataAgreementRecordID)
if err != nil {
return results, err
}

pipeline := []bson.M{
{"$match": bson.M{"organisationid": organisationId, "isdeleted": false, "_id": dataAgreementRecordId}},
{"$lookup": bson.M{
"from": "dataAgreements",
"let": bson.M{"localId": "$dataagreementid"},
"pipeline": bson.A{
bson.M{
"$match": bson.M{
"$expr": bson.M{
"$eq": []interface{}{"$_id", bson.M{"$toObjectId": "$$localId"}},
},
},
},
},
"as": "agreementData",
}},
{
"$match": bson.M{
"agreementData": bson.M{
"$elemMatch": bson.M{
"lawfulbasis": lawfulBasis,
},
},
},
},
}

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
}

// ListByDataAgreementIdIncludingDataAgreement lists data agreement record based on data agreement id
func ListByDataAgreementIdIncludingDataAgreement(dataAgreementId string, organisationId string) ([]DataAgreementRecordForAuditList, error) {
var results []DataAgreementRecordForAuditList

pipeline := []bson.M{
{"$match": bson.M{"organisationid": organisationId, "isdeleted": false, "dataagreementid": dataAgreementId}},
{"$lookup": bson.M{
"from": "dataAgreements",
"let": bson.M{"localId": "$dataagreementid"},
"pipeline": bson.A{
bson.M{
"$match": bson.M{
"$expr": bson.M{
"$eq": []interface{}{"$_id", bson.M{"$toObjectId": "$$localId"}},
},
},
},
},
"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
}

// ListByDataAgreementIdAndLawfulBasis lists data agreement record based on data agreement id and lawful basis
func ListByDataAgreementIdAndLawfulBasis(dataAgreementId string, organisationId string, lawfulBasis string) ([]DataAgreementRecordForAuditList, error) {
var results []DataAgreementRecordForAuditList

pipeline := []bson.M{
{"$match": bson.M{"organisationid": organisationId, "isdeleted": false, "dataagreementid": dataAgreementId}},
{"$lookup": bson.M{
"from": "dataAgreements",
"let": bson.M{"localId": "$dataagreementid"},
"pipeline": bson.A{
bson.M{
"$match": bson.M{
"$expr": bson.M{
"$eq": []interface{}{"$_id", bson.M{"$toObjectId": "$$localId"}},
},
},
},
},
"as": "agreementData",
}},
{
"$match": bson.M{
"agreementData": bson.M{
"$elemMatch": bson.M{
"lawfulbasis": lawfulBasis,
},
},
},
},
}

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
}

// ListByIndividualIdIncludingDataAgreement
func ListByIndividualIdIncludingDataAgreement(individualId string, organisationId string) ([]DataAgreementRecordForAuditList, error) {
var results []DataAgreementRecordForAuditList

pipeline := []bson.M{
{"$match": bson.M{"organisationid": organisationId, "isdeleted": false, "individualid": individualId}},
{"$lookup": bson.M{
"from": "dataAgreements",
"let": bson.M{"localId": "$dataagreementid"},
"pipeline": bson.A{
bson.M{
"$match": bson.M{
"$expr": bson.M{
"$eq": []interface{}{"$_id", bson.M{"$toObjectId": "$$localId"}},
},
},
},
},
"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
}

func ListByIndividualIdAndLawfulBasis(individualId string, organisationId string, lawfulBasis string) ([]DataAgreementRecordForAuditList, error) {
var results []DataAgreementRecordForAuditList

pipeline := []bson.M{
{"$match": bson.M{"organisationid": organisationId, "isdeleted": false, "individualid": individualId}},
{"$lookup": bson.M{
"from": "dataAgreements",
"let": bson.M{"localId": "$dataagreementid"},
"pipeline": bson.A{
bson.M{
"$match": bson.M{
"$expr": bson.M{
"$eq": []interface{}{"$_id", bson.M{"$toObjectId": "$$localId"}},
},
},
},
},
"as": "agreementData",
}},
{
"$match": bson.M{
"agreementData": bson.M{
"$elemMatch": bson.M{
"lawfulbasis": lawfulBasis,
},
},
},
},
}

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
}
Loading

0 comments on commit e70d3ba

Please sign in to comment.