Skip to content

Commit

Permalink
Add #612 Revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa committed Jan 11, 2024
1 parent 95c9ed8 commit 08f66ff
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 16 deletions.
8 changes: 4 additions & 4 deletions internal/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ const (

// Schemas
const (
DataAgreement = "dataAgreement"
Policy = "policy"
DataAgreementRecord = "consentRecord"
DataAttribute = "dataAttribute"
DataAgreement = "DataAgreement"
Policy = "Policy"
DataAgreementRecord = "ConsentRecord"
DataAttribute = "DataAttribute"
)

// Data Agreement Method of Use
Expand Down
47 changes: 47 additions & 0 deletions internal/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package migrate
import (
"context"
"fmt"
"strings"
"time"

"github.com/bb-consent/api/internal/actionlog"
"github.com/bb-consent/api/internal/apikey"
"github.com/bb-consent/api/internal/config"
"github.com/bb-consent/api/internal/dataagreement"
dataagreementrecord "github.com/bb-consent/api/internal/dataagreement_record"
dataagreementrecordhistory "github.com/bb-consent/api/internal/dataagreement_record_history"
Expand Down Expand Up @@ -53,6 +55,7 @@ func Migrate() {
migrateIdToStringInDataAgreementsCollection()
migrateIdToStringInSignaturesCollection()
migrateIdToStringInRevisionsCollection()
migrateSchemaNameAndAuthorizedByOtherInRevisionCollection()
}

func migrateThirdPartyDataSharingToTrueInPolicyCollection() {
Expand Down Expand Up @@ -1024,3 +1027,47 @@ func migrateIdToStringInOrganisationCollection() {
}

}

func migrateSchemaNameAndAuthorizedByOtherInRevisionCollection() {
revisionCollection := revision.Collection()

var results []revision.Revision

cursor, err := revisionCollection.Find(context.TODO(), bson.M{})
if err != nil {
fmt.Println(err)
}
defer cursor.Close(context.TODO())

if err := cursor.All(context.TODO(), &results); err != nil {
fmt.Println(err)
}

for _, revision := range results {

schemaName := ""

switch revision.SchemaName {
case "policy":
schemaName = config.Policy
case "dataAgreement":
schemaName = config.DataAgreement
case "consentRecord":
schemaName = config.DataAgreementRecord
}

if strings.TrimSpace(schemaName) != "" {
filter := bson.M{"_id": revision.Id}
update := bson.M{
"$set": bson.M{"schemaname": schemaName},
"$rename": bson.M{"authorizedbyotherid": "authorizedbyother"},
}

_, err = revisionCollection.UpdateOne(context.TODO(), filter, update)
if err != nil {
fmt.Println(err)
}
}

}
}
40 changes: 28 additions & 12 deletions internal/revision/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,34 @@ type Revision struct {
SignedWithoutObjectId bool `json:"signedWithoutObjectId"`
Timestamp string `json:"timestamp"`
AuthorizedByIndividualId string `json:"authorizedByIndividualId"`
AuthorizedByOtherId string `json:"authorizedByOtherId"`
AuthorizedByOther string `json:"authorizedByOther"`
PredecessorHash string `json:"predecessorHash"`
PredecessorSignature string `json:"predecessorSignature"`
ObjectData string `json:"objectData"`
SuccessorId string `json:"-"`
SerializedHash string `json:"-"`
SerializedSnapshot string `json:"-"`
SuccessorId string `json:"successorId"`
SerializedHash string `json:"serializedHash"`
SerializedSnapshot string `json:"serializedSnapshot"`
}

type RevisionForSerializedSnapshot struct {
SchemaName string `json:"schemaName"`
ObjectId string `json:"objectId"`
SignedWithoutObjectId bool `json:"signedWithoutObjectId"`
Timestamp string `json:"timestamp"`
AuthorizedByIndividualId string `json:"authorizedByIndividualId"`
AuthorizedByOther string `json:"authorizedByOther"`
ObjectData string `json:"objectData"`
}

// Init
func (r *Revision) Init(objectId string, authorisedByOtherId string, schemaName string) {
func (r *Revision) Init(objectId string, authorisedByOther string, schemaName string) {
r.Id = primitive.NewObjectID().Hex()
r.SchemaName = schemaName
r.ObjectId = objectId
r.SignedWithoutObjectId = false
r.Timestamp = time.Now().UTC().Format("2006-01-02T15:04:05Z")
r.AuthorizedByIndividualId = ""
r.AuthorizedByOtherId = authorisedByOtherId
r.AuthorizedByOther = authorisedByOther
}
func (r *Revision) updateSuccessorId(successorId string) {
r.SuccessorId = successorId
Expand All @@ -62,9 +72,18 @@ func (r *Revision) CreateRevision(objectData interface{}) error {
}
r.ObjectData = string(objectDataSerialised)

var revisionForSerializedSnapshot RevisionForSerializedSnapshot
revisionForSerializedSnapshot.SchemaName = r.SchemaName
revisionForSerializedSnapshot.ObjectId = r.ObjectId
revisionForSerializedSnapshot.SignedWithoutObjectId = r.SignedWithoutObjectId
revisionForSerializedSnapshot.Timestamp = r.Timestamp
revisionForSerializedSnapshot.AuthorizedByIndividualId = r.AuthorizedByIndividualId
revisionForSerializedSnapshot.AuthorizedByOther = r.AuthorizedByOther
revisionForSerializedSnapshot.ObjectData = r.ObjectData

// Serialised snapshot
// TODO: Use a standard json normalisation algorithm for .e.g JCS
serialisedSnapshot, err := json.Marshal(r)
serialisedSnapshot, err := json.Marshal(revisionForSerializedSnapshot)
if err != nil {
return err
}
Expand Down Expand Up @@ -211,9 +230,6 @@ func RecreatePolicyFromRevision(revision Revision) (policy.Policy, error) {
// RevisionForHTTPResponse
type RevisionForHTTPResponse struct {
Revision
SuccessorId string `json:"successorId"`
SerializedHash string `json:"serializedHash"`
SerializedSnapshot string `json:"serizalizedSnapshot"`
}

// Init
Expand All @@ -228,7 +244,7 @@ func (r *RevisionForHTTPResponse) Init(revision Revision) {
r.SignedWithoutObjectId = revision.SignedWithoutObjectId
r.Timestamp = revision.Timestamp
r.AuthorizedByIndividualId = revision.AuthorizedByIndividualId
r.AuthorizedByOtherId = revision.AuthorizedByOtherId
r.AuthorizedByOther = revision.AuthorizedByOther
r.PredecessorHash = revision.PredecessorHash
r.PredecessorSignature = revision.PredecessorSignature
r.ObjectData = revision.ObjectData
Expand Down Expand Up @@ -272,7 +288,7 @@ func (r *Revision) InitForDraftDataAgreement(objectId string, authorisedByOtherI
r.SignedWithoutObjectId = false
r.Timestamp = time.Now().UTC().Format("2006-01-02T15:04:05Z")
r.AuthorizedByIndividualId = ""
r.AuthorizedByOtherId = authorisedByOtherId
r.AuthorizedByOther = authorisedByOtherId
}

// CreateRevisionForDataAgreement
Expand Down

0 comments on commit 08f66ff

Please sign in to comment.