diff --git a/internal/handler/v2/config/dataagreement/config_create_dataagreement.go b/internal/handler/v2/config/dataagreement/config_create_dataagreement.go index e0e4791..d2a5bf5 100644 --- a/internal/handler/v2/config/dataagreement/config_create_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_create_dataagreement.go @@ -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) @@ -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 { diff --git a/internal/handler/v2/config/dataagreement/config_update_dataagreement.go b/internal/handler/v2/config/dataagreement/config_update_dataagreement.go index ede3a8e..2a9a941 100644 --- a/internal/handler/v2/config/dataagreement/config_update_dataagreement.go +++ b/internal/handler/v2/config/dataagreement/config_update_dataagreement.go @@ -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 @@ -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 @@ -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 @@ -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 } @@ -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) diff --git a/internal/handler/v2/config/dataattribute/config_update_dataattribute.go b/internal/handler/v2/config/dataattribute/config_update_dataattribute.go index 8a31087..6639f12 100644 --- a/internal/handler/v2/config/dataattribute/config_update_dataattribute.go +++ b/internal/handler/v2/config/dataattribute/config_update_dataattribute.go @@ -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)