From c152ad5d2d8af589b764ea7fda6ad2854fa1d277 Mon Sep 17 00:00:00 2001 From: Albin Antony Date: Mon, 13 Nov 2023 14:32:57 +0530 Subject: [PATCH] Fix #523 Create a revision when updating signature for the consent record --- .../service_update_dataagreement_record.go | 2 +- .../service_update_signature_object.go | 25 +++++++++++++++++++ internal/revision/revisions.go | 18 +++++++++++-- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/internal/handler/v2/service/service_update_dataagreement_record.go b/internal/handler/v2/service/service_update_dataagreement_record.go index b73fc97..5485104 100644 --- a/internal/handler/v2/service/service_update_dataagreement_record.go +++ b/internal/handler/v2/service/service_update_dataagreement_record.go @@ -115,7 +115,7 @@ func ServiceUpdateDataAgreementRecord(w http.ResponseWriter, r *http.Request) { } // Create new revision - newRevision, err := revision.UpdateRevisionForDataAgreementRecord(toBeUpdatedDaRecord, ¤tDataAgreementRecordRevision, 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) diff --git a/internal/handler/v2/service/service_update_signature_object.go b/internal/handler/v2/service/service_update_signature_object.go index 3ed9e3d..673b351 100644 --- a/internal/handler/v2/service/service_update_signature_object.go +++ b/internal/handler/v2/service/service_update_signature_object.go @@ -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" ) @@ -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]) @@ -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" @@ -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, } diff --git a/internal/revision/revisions.go b/internal/revision/revisions.go index 21fc879..5c4d45a 100644 --- a/internal/revision/revisions.go +++ b/internal/revision/revisions.go @@ -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, @@ -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 }