Skip to content

Commit

Permalink
Fix #523 Create a revision when updating signature for the consent re…
Browse files Browse the repository at this point in the history
…cord
  • Loading branch information
albinpa authored and georgepadayatti committed Nov 13, 2023
1 parent 085531c commit c152ad5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func ServiceUpdateDataAgreementRecord(w http.ResponseWriter, r *http.Request) {
}

// Create new revision
newRevision, err := revision.UpdateRevisionForDataAgreementRecord(toBeUpdatedDaRecord, &currentDataAgreementRecordRevision, individualId, currentDataAgreementRevision)
newRevision, err := revision.UpdateRevisionForDataAgreementRecord(toBeUpdatedDaRecord, individualId, currentDataAgreementRevision)
if err != nil {
m := "Failed to create revision for new data agreement record"
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
Expand Down
25 changes: 25 additions & 0 deletions internal/handler/v2/service/service_update_signature_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/bb-consent/api/internal/common"
"github.com/bb-consent/api/internal/config"
daRecord "github.com/bb-consent/api/internal/dataagreement_record"
"github.com/bb-consent/api/internal/revision"
"github.com/bb-consent/api/internal/signature"
"github.com/gorilla/mux"
)
Expand Down Expand Up @@ -46,6 +47,7 @@ func ServiceUpdateSignatureObject(w http.ResponseWriter, r *http.Request) {

// Headers
organisationId := common.Sanitize(r.Header.Get(config.OrganizationId))
individualId := common.Sanitize(r.Header.Get(config.IndividualHeaderKey))

dataAgreementRecordId := common.Sanitize(mux.Vars(r)[config.DataAgreementRecordId])

Expand Down Expand Up @@ -74,6 +76,13 @@ func ServiceUpdateSignatureObject(w http.ResponseWriter, r *http.Request) {
return
}

currentDataAgreementRevision, err := revision.GetLatestByObjectId(toBeUpdatedDaRecord.DataAgreementId)
if err != nil {
m := fmt.Sprintf("Failed to fetch latest revision for data agreement: %v", toBeUpdatedDaRecord.DataAgreementId)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

toBeUpdatedSignatureObject, err := signature.Get(toBeUpdatedDaRecord.SignatureId)
if err != nil {
m := "Failed to fetch signature for data agreement record"
Expand Down Expand Up @@ -102,6 +111,22 @@ func ServiceUpdateSignatureObject(w http.ResponseWriter, r *http.Request) {
return
}

// Create new revision
newRevision, err := revision.UpdateRevisionForDataAgreementRecord(toBeUpdatedDaRecord, individualId, currentDataAgreementRevision)
if err != nil {
m := "Failed to create revision for new data agreement record"
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

// Save the revision to db
_, err = revision.Add(newRevision)
if err != nil {
m := fmt.Sprintf("Failed to create new revision: %v", newRevision.Id)
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

resp := updateSignatureforDataAgreementRecordResp{
Signature: savedSignature,
}
Expand Down
18 changes: 16 additions & 2 deletions internal/revision/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func CreateRevisionForDataAgreementRecord(newDataAgreementRecord daRecord.DataAg
}

// UpdateRevisionForDataAgreementRecord
func UpdateRevisionForDataAgreementRecord(updatedDataAgreementRecord daRecord.DataAgreementRecord, previousRevision *Revision, orgAdminId string, dataAgreementRevision Revision) (Revision, error) {
func UpdateRevisionForDataAgreementRecord(updatedDataAgreementRecord daRecord.DataAgreementRecord, orgAdminId string, dataAgreementRevision Revision) (Revision, error) {
// Object data
objectData := dataAgreementRecordForObjectData{
Id: updatedDataAgreementRecord.Id,
Expand All @@ -472,7 +472,21 @@ func UpdateRevisionForDataAgreementRecord(updatedDataAgreementRecord daRecord.Da
// Update revision
revision := Revision{}
revision.Init(objectData.Id.Hex(), orgAdminId, config.DataAgreementRecord)
err := revision.UpdateRevision(previousRevision, objectData)
// Query for previous revisions
previousRevision, err := GetLatestByObjectId(updatedDataAgreementRecord.Id.Hex())
if err != nil {
return revision, err
}

err = revision.UpdateRevision(&previousRevision, objectData)
if err != nil {
return revision, err
}
// Save the previous revision to db
_, err = Update(previousRevision)
if err != nil {
return revision, err
}

return revision, err
}
Expand Down

0 comments on commit c152ad5

Please sign in to comment.