Skip to content

Commit

Permalink
Add #594 Data agreement
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa committed Dec 15, 2023
1 parent 3244c88 commit 3abb612
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 20 deletions.
20 changes: 13 additions & 7 deletions internal/dataagreement/dataagreements.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ type Signature struct {
ObjectReference string `json:"objectReference"`
}

type Controller struct {
Id string `json:"id"`
Name string `json:"name"`
Url string `json:"url"`
}

type DataAgreement struct {
Id string `json:"id" bson:"_id,omitempty"`
Version string `json:"version"`
Expand All @@ -52,7 +58,7 @@ type DataAgreement struct {
Purpose string `json:"purpose" valid:"required"`
PurposeDescription string `json:"purposeDescription" valid:"required"`
LawfulBasis string `json:"lawfulBasis" valid:"required"`
MethodOfUse string `json:"methodOfUse" valid:"required"`
MethodOfUse string `json:"methodOfUse"`
DpiaDate string `json:"dpiaDate"`
DpiaSummaryUrl string `json:"dpiaSummaryUrl"`
Signature Signature `json:"signature"`
Expand All @@ -64,6 +70,10 @@ type DataAgreement struct {
OrganisationId string `json:"-"`
IsDeleted bool `json:"-"`
Timestamp string `json:"-"`
DataUse string `json:"dataUse"`
Dpia string `json:"dpia"`
CompatibleWithVersion string `json:"compatibleWithVersion"`
Controller Controller `json:"controller"`
}

type DataAgreementWithObjectData struct {
Expand Down Expand Up @@ -118,16 +128,12 @@ func (darepo *DataAgreementRepository) Update(dataAgreement DataAgreement) (Data
}

// IsDataAgreementExist Check if data agreement with given id exists
func (darepo *DataAgreementRepository) IsDataAgreementExist(dataAgreementID string) (int64, error) {
func (darepo *DataAgreementRepository) IsDataAgreementExist(dataAgreementId string) (int64, error) {
var exists int64
dataAgreementId, err := primitive.ObjectIDFromHex(dataAgreementID)
if err != nil {
return exists, err
}

filter := common.CombineFilters(darepo.DefaultFilter, bson.M{"_id": dataAgreementId})

exists, err = Collection().CountDocuments(context.TODO(), filter)
exists, err := Collection().CountDocuments(context.TODO(), filter)
if err != nil {
return exists, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type dataAgreement struct {
Purpose string `json:"purpose" validate:"required_if=Active true"`
PurposeDescription string `json:"purposeDescription" validate:"required_if=Active true,max=500"`
LawfulBasis string `json:"lawfulBasis" validate:"required_if=Active true"`
MethodOfUse string `json:"methodOfUse" validate:"required_if=Active true"`
MethodOfUse string `json:"methodOfUse"`
DpiaDate string `json:"dpiaDate"`
DpiaSummaryUrl string `json:"dpiaSummaryUrl"`
Signature dataagreement.Signature `json:"signature"`
Expand All @@ -98,6 +98,10 @@ type dataAgreement struct {
DataAttributes []dataAttributeForDataAgreement `json:"dataAttributes" validate:"required_if=Active true"`
OrganisationId string `json:"-"`
IsDeleted bool `json:"-"`
DataUse string `json:"dataUse"`
Dpia string `json:"dpia"`
CompatibleWithVersion string `json:"compatibleWithVersion"`
Controller dataagreement.Controller `json:"controller"`
}

type addDataAgreementReq struct {
Expand Down Expand Up @@ -147,11 +151,6 @@ func validateAddDataAgreementRequestBody(dataAgreementReq addDataAgreementReq) e
return errors.New("invalid lawful basis provided")
}

// Proceed if method of use is valid
if !isValidMethodOfUse(dataAgreementReq.DataAgreement.MethodOfUse) {
return errors.New("invalid method of use provided")
}

return nil
}

Expand Down Expand Up @@ -222,8 +221,19 @@ func setDataAgreementFromReq(requestBody addDataAgreementReq, newDataAgreement d
newDataAgreement.Forgettable = requestBody.DataAgreement.Forgettable
newDataAgreement.CompatibleWithVersionId = requestBody.DataAgreement.CompatibleWithVersionId
newDataAgreement.DataAttributes = setDataAttributesFromReq(requestBody)
newDataAgreement.DataUse = requestBody.DataAgreement.DataUse
newDataAgreement.Dpia = requestBody.DataAgreement.Dpia
newDataAgreement.CompatibleWithVersion = requestBody.DataAgreement.CompatibleWithVersion

newDataAgreement.Lifecycle = setDataAgreementLifecycle(requestBody.DataAgreement.Active)

// update method of use if data use not empty and is valid
if len(strings.TrimSpace(requestBody.DataAgreement.DataUse)) > 0 && isValidMethodOfUse(requestBody.DataAgreement.DataUse) {
newDataAgreement.MethodOfUse = requestBody.DataAgreement.DataUse
} else {
newDataAgreement.DataUse = requestBody.DataAgreement.MethodOfUse
}

return newDataAgreement
}

Expand All @@ -232,6 +242,10 @@ func setControllerFromReq(o org.Organization, newDataAgreement dataagreement.Dat
newDataAgreement.ControllerId = o.ID
newDataAgreement.ControllerName = o.Name
newDataAgreement.ControllerUrl = o.EulaURL

newDataAgreement.Controller.Id = o.ID
newDataAgreement.Controller.Name = o.Name
newDataAgreement.Controller.Url = o.EulaURL
return newDataAgreement
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func setDataAgreementWithRevisions(dataAgreement dataagreement.DataAgreement, re
dataAgreementWithRevision.ControllerUrl = dataAgreement.ControllerUrl
dataAgreementWithRevision.Version = dataAgreement.Version
dataAgreementWithRevision.Revisions = revisions
dataAgreementWithRevision.Controller = dataAgreement.Controller
dataAgreementWithRevision.Dpia = dataAgreement.Dpia
dataAgreementWithRevision.DataUse = dataAgreement.DataUse
dataAgreementWithRevision.CompatibleWithVersion = dataAgreement.CompatibleWithVersion

return dataAgreementWithRevision
}
Expand Down Expand Up @@ -236,6 +240,10 @@ type dataAgreementWithRevisions struct {
IsDeleted bool `json:"-"`
Timestamp string `json:"-"`
Revisions []revision.Revision `json:"revisions"`
DataUse string `json:"dataUse"`
Dpia string `json:"dpia"`
CompatibleWithVersion string `json:"compatibleWithVersion"`
Controller dataagreement.Controller `json:"controller"`
}

func dataAgreementsWithRevisionsToInterfaceSlice(dataAgreements []dataAgreementWithRevisions) []interface{} {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ func validateUpdateDataAgreementRequestBody(dataAgreementReq updateDataAgreement
return errors.New("invalid lawful basis provided")
}

// Proceed if method of use is valid
if !isValidMethodOfUse(dataAgreementReq.DataAgreement.MethodOfUse) {
return errors.New("invalid method of use provided")
}

return nil
}

Expand Down Expand Up @@ -82,6 +77,17 @@ func updateDataAttributeFromUpdateDataAgreementRequestBody(requestBody updateDat
return newDataAttributes
}

func updateControllerFromReq(o org.Organization, toBeUpdatedDataAgreement dataagreement.DataAgreement) dataagreement.DataAgreement {
toBeUpdatedDataAgreement.ControllerId = o.ID
toBeUpdatedDataAgreement.ControllerName = o.Name
toBeUpdatedDataAgreement.ControllerUrl = o.EulaURL

toBeUpdatedDataAgreement.Controller.Id = o.ID
toBeUpdatedDataAgreement.Controller.Name = o.Name
toBeUpdatedDataAgreement.Controller.Url = o.EulaURL
return toBeUpdatedDataAgreement
}

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

toBeUpdatedDataAgreement.Policy.Name = requestBody.DataAgreement.Policy.Name
Expand All @@ -100,6 +106,9 @@ func updateDataAgreementFromRequestBody(requestBody updateDataAgreementReq, toBe
toBeUpdatedDataAgreement.MethodOfUse = requestBody.DataAgreement.MethodOfUse
toBeUpdatedDataAgreement.DpiaDate = requestBody.DataAgreement.DpiaDate
toBeUpdatedDataAgreement.DpiaSummaryUrl = requestBody.DataAgreement.DpiaSummaryUrl
toBeUpdatedDataAgreement.DataUse = requestBody.DataAgreement.DataUse
toBeUpdatedDataAgreement.Dpia = requestBody.DataAgreement.Dpia
toBeUpdatedDataAgreement.CompatibleWithVersion = requestBody.DataAgreement.CompatibleWithVersion

toBeUpdatedDataAgreement.Signature.Payload = requestBody.DataAgreement.Signature.Payload
toBeUpdatedDataAgreement.Signature.Signature = requestBody.DataAgreement.Signature.Signature
Expand All @@ -126,6 +135,13 @@ func updateDataAgreementFromRequestBody(requestBody updateDataAgreementReq, toBe

toBeUpdatedDataAgreement.DataAttributes = dataAttributes

// update method of use if data use not empty and is valid
if len(strings.TrimSpace(requestBody.DataAgreement.DataUse)) > 0 && isValidMethodOfUse(requestBody.DataAgreement.DataUse) {
toBeUpdatedDataAgreement.MethodOfUse = requestBody.DataAgreement.DataUse
} else {
toBeUpdatedDataAgreement.DataUse = requestBody.DataAgreement.MethodOfUse
}

return toBeUpdatedDataAgreement
}

Expand Down Expand Up @@ -156,7 +172,7 @@ func ConfigUpdateDataAgreement(w http.ResponseWriter, r *http.Request) {
}

// Query organisation by Id
_, err = org.Get(organisationId)
o, err := org.Get(organisationId)
if err != nil {
m := fmt.Sprintf("Failed to get organization by ID :%v", organisationId)
common.HandleErrorV2(w, http.StatusNotFound, m, err)
Expand Down Expand Up @@ -192,6 +208,7 @@ func ConfigUpdateDataAgreement(w http.ResponseWriter, r *http.Request) {

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

// Bump major version for data agreement
updatedVersion, err := common.BumpMajorVersion(toBeUpdatedDataAgreement.Version)
Expand Down
19 changes: 19 additions & 0 deletions internal/revision/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ type dataAgreementForObjectData struct {
DataAttributes []dataagreement.DataAttribute `json:"dataAttributes" valid:"required"`
OrganisationId string `json:"-"`
IsDeleted bool `json:"-"`
DataUse string `json:"dataUse"`
Dpia string `json:"dpia"`
CompatibleWithVersion string `json:"compatibleWithVersion"`
Controller dataagreement.Controller `json:"controller"`
}

// InitForDraftDataAgreement
Expand Down Expand Up @@ -292,6 +296,11 @@ func CreateRevisionForDataAgreement(newDataAgreement dataagreement.DataAgreement
CompatibleWithVersionId: newDataAgreement.CompatibleWithVersionId,
Lifecycle: newDataAgreement.Lifecycle,
DataAttributes: newDataAgreement.DataAttributes,
DataUse: newDataAgreement.DataUse,
Dpia: newDataAgreement.Dpia,
CompatibleWithVersion: newDataAgreement.CompatibleWithVersion,
ControllerName: newDataAgreement.ControllerName,
Controller: newDataAgreement.Controller,
}

// Create revision
Expand Down Expand Up @@ -323,6 +332,11 @@ func UpdateRevisionForDataAgreement(updatedDataAgreement dataagreement.DataAgree
CompatibleWithVersionId: updatedDataAgreement.CompatibleWithVersionId,
Lifecycle: updatedDataAgreement.Lifecycle,
DataAttributes: updatedDataAgreement.DataAttributes,
DataUse: updatedDataAgreement.DataUse,
Dpia: updatedDataAgreement.Dpia,
CompatibleWithVersion: updatedDataAgreement.CompatibleWithVersion,
ControllerName: updatedDataAgreement.ControllerName,
Controller: updatedDataAgreement.Controller,
}

// Initialise revision
Expand Down Expand Up @@ -400,6 +414,11 @@ func CreateRevisionForDraftDataAgreement(newDataAgreement dataagreement.DataAgre
CompatibleWithVersionId: newDataAgreement.CompatibleWithVersionId,
Lifecycle: newDataAgreement.Lifecycle,
DataAttributes: newDataAgreement.DataAttributes,
DataUse: newDataAgreement.DataUse,
Dpia: newDataAgreement.Dpia,
CompatibleWithVersion: newDataAgreement.CompatibleWithVersion,
ControllerName: newDataAgreement.ControllerName,
Controller: newDataAgreement.Controller,
}

// Create revision
Expand Down
2 changes: 1 addition & 1 deletion resources/config

0 comments on commit 3abb612

Please sign in to comment.