Skip to content

Commit

Permalink
Fix #424 Error in Mongo; no documents in the result for update and pu…
Browse files Browse the repository at this point in the history
…blish data agreements
  • Loading branch information
albinpa authored and georgepadayatti committed Oct 31, 2023
1 parent 38e75f5 commit 1fbab94
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func ConfigCreateDataAgreement(w http.ResponseWriter, r *http.Request) {
}

version := common.IntegerToSemver(1)
draftVersion := common.IntegerToSemver(0)
// Add life cycle based on active field
lifecycle := setDataAgreementLifecycle(dataAgreementReq.DataAgreement.Active)

Expand All @@ -274,9 +275,10 @@ func ConfigCreateDataAgreement(w http.ResponseWriter, r *http.Request) {
newDataAgreement.DataAttributes = newDataAttributes
newDataAgreement.IsDeleted = false
newDataAgreement.Lifecycle = lifecycle
newDataAgreement.Version = draftVersion

var newRevision revision.Revision
if lifecycle == config.Complete {
if dataAgreementReq.DataAgreement.Active {
newDataAgreement.Version = version

// Create new revision
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func ConfigDeleteDataAgreement(w http.ResponseWriter, r *http.Request) {

var currentRevision revision.Revision

if currentDataAgreement.Version == "" {
if currentDataAgreement.Version == "0.0.0" {
currentRevision, err = revision.CreateRevisionForDraftDataAgreement(currentDataAgreement, orgAdminId)
if err != nil {
m := fmt.Sprintf("Failed to create revision for draft data agreement: %v", dataAgreementId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ConfigReadDataAgreement(w http.ResponseWriter, r *http.Request) {
}

} else {
if da.Version == "" {
if da.Version == "0.0.0" {
revisionResp, err = revision.CreateRevisionForDraftDataAgreement(da, orgAdminId)
if err != nil {
m := fmt.Sprintf("Failed to create revision for draft data agreement: %v", dataAgreementId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,8 @@ func ConfigUpdateDataAgreement(w http.ResponseWriter, r *http.Request) {
// Get policy from db
toBeUpdatedDataAgreement, err := daRepo.Get(dataAgreementId)
if err != nil {
common.HandleErrorV2(w, http.StatusInternalServerError, err.Error(), err)
return
}
var currentRevision revision.Revision

// Get revision from db
currentRevision, err = revision.GetLatestByDataAgreementId(dataAgreementId)
if err != nil {
common.HandleErrorV2(w, http.StatusInternalServerError, err.Error(), err)
m := fmt.Sprintf("Failed to fetch data agreement by id: %v", dataAgreementId)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

Expand All @@ -182,30 +175,54 @@ func ConfigUpdateDataAgreement(w http.ResponseWriter, r *http.Request) {
toBeUpdatedDataAttributes := updateDataAttributeFromUpdateDataAgreementRequestBody(dataAgreementReq)
toBeUpdatedDataAgreement.DataAttributes = toBeUpdatedDataAttributes

if lifecycle == config.Complete {
// Bump major version for policy
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
}
toBeUpdatedDataAgreement.Version = updatedVersion

// Update revision
newRevision, err := revision.UpdateRevisionForDataAgreement(toBeUpdatedDataAgreement, &currentRevision, orgAdminId)
if err != nil {
m := fmt.Sprintf("Failed to update revision for data agreement: %v", dataAgreementId)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}
// Bump major version for policy
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
}

// Save the previous revision to db
updatedRevision, err := revision.Update(currentRevision)
if err != nil {
m := fmt.Sprintf("Failed to update revision: %v", updatedRevision.Id)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
var currentRevision revision.Revision
var newRevision revision.Revision

if toBeUpdatedDataAgreement.Active {

if toBeUpdatedDataAgreement.Version == "0.0.0" {
toBeUpdatedDataAgreement.Version = updatedVersion
// Create new revision
newRevision, err = revision.CreateRevisionForDataAgreement(toBeUpdatedDataAgreement, orgAdminId)
if err != nil {
m := fmt.Sprintf("Failed to create revision for data agreement: %v", toBeUpdatedDataAgreement.Id)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}
} else {
// Get revision from db
currentRevision, err = revision.GetLatestByDataAgreementId(dataAgreementId)
if err != nil {
m := fmt.Sprintf("Failed to fetch latest revision by data agreement id: %v", dataAgreementId)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

toBeUpdatedDataAgreement.Version = updatedVersion

// Update revision
newRevision, err = revision.UpdateRevisionForDataAgreement(toBeUpdatedDataAgreement, &currentRevision, orgAdminId)
if err != nil {
m := fmt.Sprintf("Failed to update revision for data agreement: %v", dataAgreementId)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

// Save the previous revision to db
updatedRevision, err := revision.Update(currentRevision)
if err != nil {
m := fmt.Sprintf("Failed to update revision: %v", updatedRevision.Id)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}
}

// Save the revision to db
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,35 +129,44 @@ func ConfigUpdateDataAttribute(w http.ResponseWriter, r *http.Request) {
return
}
var currentRevision revision.Revision
var newRevision revision.Revision

if toBeUpdatedDataAgreement.Active {

toBeUpdatedDataAgreement.Version = updatedVersion

// Get current revision from db
currentRevision, err = revision.GetLatestByDataAgreementId(toBeUpdatedDataAgreement.Id.Hex())
if err != nil {
m := fmt.Sprintf("Failed to fetch latest revision for data agreement id: %v", toBeUpdatedDataAgreement.Id.Hex())
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

// Update revision
newRevision, err := revision.UpdateRevisionForDataAgreement(toBeUpdatedDataAgreement, &currentRevision, orgAdminId)
if err != nil {
m := fmt.Sprintf("Failed to update revision for data attribute: %v", dataAttributeId)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

// Save the previous revision to db
updatedRevision, err := revision.Update(currentRevision)
if err != nil {
m := fmt.Sprintf("Failed to update revision: %v", updatedRevision.Id)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
if toBeUpdatedDataAgreement.Version == "0.0.0" {
toBeUpdatedDataAgreement.Version = updatedVersion
// Create new revision
newRevision, err = revision.CreateRevisionForDataAgreement(toBeUpdatedDataAgreement, orgAdminId)
if err != nil {
m := fmt.Sprintf("Failed to create revision for data agreement: %v", toBeUpdatedDataAgreement.Id)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}
} else {
// Get current revision from db
currentRevision, err = revision.GetLatestByDataAgreementId(toBeUpdatedDataAgreement.Id.Hex())
if err != nil {
m := fmt.Sprintf("Failed to fetch latest revision for data agreement id: %v", toBeUpdatedDataAgreement.Id.Hex())
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}
toBeUpdatedDataAgreement.Version = updatedVersion

// Update revision
newRevision, err = revision.UpdateRevisionForDataAgreement(toBeUpdatedDataAgreement, &currentRevision, orgAdminId)
if err != nil {
m := fmt.Sprintf("Failed to update revision for data attribute: %v", dataAttributeId)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

// Save the previous revision to db
updatedRevision, err := revision.Update(currentRevision)
if err != nil {
m := fmt.Sprintf("Failed to update revision: %v", updatedRevision.Id)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}
}

// Save the revision to db
currentRevision, err = revision.Add(newRevision)
if err != nil {
Expand Down

0 comments on commit 1fbab94

Please sign in to comment.