Skip to content

Commit

Permalink
Fix #461 Version number for draft data agreements
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa authored and georgepadayatti committed Nov 7, 2023
1 parent ac72d14 commit 4141232
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,13 @@ func ConfigCreateDataAgreement(w http.ResponseWriter, r *http.Request) {
// Set controller details
newDataAgreement = setControllerFromReq(o, newDataAgreement)
newDataAgreement.IsDeleted = false
// Set data agreement version
newDataAgreement.Version = common.IntegerToSemver(1)

// If data agreement is published then:
// a. Set data agreement verion as 1.0.0
// b. Add a new revision
// a. Add a new revision
var newRevision revision.Revision
if newDataAgreement.Active {
// Set data agreement version
newDataAgreement.Version = common.IntegerToSemver(1)

// Update revision
newRevision, err = revision.UpdateRevisionForDataAgreement(newDataAgreement, orgAdminId)
Expand All @@ -300,9 +299,6 @@ func ConfigCreateDataAgreement(w http.ResponseWriter, r *http.Request) {

} else {
// Data agreement is draft
// Set data agreement version 0.0.0
newDataAgreement.Version = common.IntegerToSemver(0)

// Create a revision on runtime
newRevision, err = revision.CreateRevisionForDraftDataAgreement(newDataAgreement, orgAdminId)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,29 @@ func validateUpdateDataAgreementRequestBody(dataAgreementReq updateDataAgreement
return nil
}

func updateDataAttributeFromUpdateDataAgreementRequestBody(requestBody updateDataAgreementReq) []dataagreement.DataAttribute {
func dataAttributeIdExists(dataAttributeId primitive.ObjectID, currentDataAttributes []dataagreement.DataAttribute) bool {
for _, dataAttribute := range currentDataAttributes {
if dataAttributeId == dataAttribute.Id {
return true
}
}
return false
}

func updateDataAttributeFromUpdateDataAgreementRequestBody(requestBody updateDataAgreementReq, currentDataAttributes []dataagreement.DataAttribute) []dataagreement.DataAttribute {
var newDataAttributes []dataagreement.DataAttribute

for _, dA := range requestBody.DataAgreement.DataAttributes {
var dataAttribute dataagreement.DataAttribute
if dA.DataAttribute.Id.IsZero() {
dataAttribute.Id = primitive.NewObjectID()

dataAttributeId, _ := primitive.ObjectIDFromHex(dA.Id)
isExistingDataAttribute := dataAttributeIdExists(dataAttributeId, currentDataAttributes)
if isExistingDataAttribute {
dataAttribute.Id = dataAttributeId
} else {
dataAttribute.Id = dA.DataAttribute.Id
dataAttribute.Id = primitive.NewObjectID()
}

dataAttribute.Name = dA.Name
dataAttribute.Description = dA.Description
dataAttribute.Category = dA.Category
Expand All @@ -71,7 +84,6 @@ func updateDataAttributeFromUpdateDataAgreementRequestBody(requestBody updateDat

func updateDataAgreementFromRequestBody(requestBody updateDataAgreementReq, toBeUpdatedDataAgreement dataagreement.DataAgreement) dataagreement.DataAgreement {

toBeUpdatedDataAgreement.Policy.Id = requestBody.DataAgreement.Policy.Policy.Id
toBeUpdatedDataAgreement.Policy.Name = requestBody.DataAgreement.Policy.Name
toBeUpdatedDataAgreement.Policy.Version = requestBody.DataAgreement.Policy.Version
toBeUpdatedDataAgreement.Policy.Url = requestBody.DataAgreement.Policy.Url
Expand All @@ -89,7 +101,6 @@ func updateDataAgreementFromRequestBody(requestBody updateDataAgreementReq, toBe
toBeUpdatedDataAgreement.DpiaDate = requestBody.DataAgreement.DpiaDate
toBeUpdatedDataAgreement.DpiaSummaryUrl = requestBody.DataAgreement.DpiaSummaryUrl

toBeUpdatedDataAgreement.Signature.Id = requestBody.DataAgreement.Signature.Signature.Id
toBeUpdatedDataAgreement.Signature.Payload = requestBody.DataAgreement.Signature.Payload
toBeUpdatedDataAgreement.Signature.Signature = requestBody.DataAgreement.Signature.Signature.Signature
toBeUpdatedDataAgreement.Signature.VerificationMethod = requestBody.DataAgreement.Signature.VerificationMethod
Expand All @@ -108,18 +119,12 @@ func updateDataAgreementFromRequestBody(requestBody updateDataAgreementReq, toBe
toBeUpdatedDataAgreement.Forgettable = requestBody.DataAgreement.Forgettable
toBeUpdatedDataAgreement.CompatibleWithVersionId = requestBody.DataAgreement.CompatibleWithVersionId

if requestBody.DataAgreement.Signature.Signature.Id.IsZero() {
toBeUpdatedDataAgreement.Signature.Id = primitive.NewObjectID()
}

if requestBody.DataAgreement.Policy.Policy.Id.IsZero() {
toBeUpdatedDataAgreement.Policy.Id = primitive.NewObjectID()
}

// Update life cycle based on active field
toBeUpdatedDataAgreement.Lifecycle = setDataAgreementLifecycle(requestBody.DataAgreement.Active)

toBeUpdatedDataAgreement.DataAttributes = updateDataAttributeFromUpdateDataAgreementRequestBody(requestBody)
dataAttributes := updateDataAttributeFromUpdateDataAgreementRequestBody(requestBody, toBeUpdatedDataAgreement.DataAttributes)

toBeUpdatedDataAgreement.DataAttributes = dataAttributes

return toBeUpdatedDataAgreement
}
Expand Down Expand Up @@ -169,26 +174,33 @@ func ConfigUpdateDataAgreement(w http.ResponseWriter, r *http.Request) {
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}
currentVersion := currentDataAgreement.Version

// Update data agreement from request body
toBeUpdatedDataAgreement := updateDataAgreementFromRequestBody(dataAgreementReq, currentDataAgreement)

// Bump major version for data agreement
updatedVersion, err := common.BumpMajorVersion(toBeUpdatedDataAgreement.Version)
if err != nil {
m := fmt.Sprintf("Failed to bump major version for data agreement: %v", dataAgreementId)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

// Increment data agreement version
toBeUpdatedDataAgreement.Version = updatedVersion

var newRevision revision.Revision

// if data agreement is in draft mode then
// version is not incremented
if !currentDataAgreement.Active {
toBeUpdatedDataAgreement.Version = currentVersion
}

// If data agreement is published then:
// a. Update data agreement version
// b. Add a new revision
//a. Add a new revision
if toBeUpdatedDataAgreement.Active {
// Bump major version for data agreement
updatedVersion, err := common.BumpMajorVersion(toBeUpdatedDataAgreement.Version)
if err != nil {
m := fmt.Sprintf("Failed to bump major version for data agreement: %v", dataAgreementId)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

// Increment data agreement version
toBeUpdatedDataAgreement.Version = updatedVersion

// Update revision
newRevision, err = revision.UpdateRevisionForDataAgreement(toBeUpdatedDataAgreement, orgAdminId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,34 @@ func ConfigUpdateDataAttribute(w http.ResponseWriter, r *http.Request) {
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}
currentVersion := toBeUpdatedDataAgreement.Version
currentActiveStatus := toBeUpdatedDataAgreement.Active

// Set data attribute from request body
updatedDataAttributes, matchedIndex := updateDataAttributeFromReq(dataAttributeId, dataAttributeReq, toBeUpdatedDataAgreement.DataAttributes)
toBeUpdatedDataAgreement.DataAttributes = updatedDataAttributes

// Bump major version for data agreement
updatedVersion, err := common.BumpMajorVersion(toBeUpdatedDataAgreement.Version)
if err != nil {
m := fmt.Sprintf("Failed to bump major version for data agreement: %v", toBeUpdatedDataAgreement.Id.Hex())
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

// Increment data agreement version
toBeUpdatedDataAgreement.Version = updatedVersion

// if data agreement is in draft mode then
// version is not incremented
if !currentActiveStatus {
toBeUpdatedDataAgreement.Version = currentVersion
}

// Revision handling for data agreements
// If data agreement is published then:
// a. Update data agreement version
// b. Add a new revision
// a. Add a new revision
if toBeUpdatedDataAgreement.Active {
// Bump major version for data agreement
updatedVersion, err := common.BumpMajorVersion(toBeUpdatedDataAgreement.Version)
if err != nil {
m := fmt.Sprintf("Failed to bump major version for data agreement: %v", toBeUpdatedDataAgreement.Id.Hex())
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

// Increment data agreement version
toBeUpdatedDataAgreement.Version = updatedVersion

// Update revision
_, err = revision.UpdateRevisionForDataAgreement(toBeUpdatedDataAgreement, orgAdminId)
Expand Down

0 comments on commit 4141232

Please sign in to comment.