Skip to content

Commit

Permalink
Add #614 Update consent record
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa authored and georgepadayatti committed Jan 11, 2024
1 parent e511fcd commit 2470a2f
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions internal/handler/v2/service/service_update_dataagreement_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package service

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"

"github.com/asaskevich/govalidator"
"github.com/bb-consent/api/internal/common"
"github.com/bb-consent/api/internal/config"
"github.com/bb-consent/api/internal/dataagreement"
Expand All @@ -27,6 +25,14 @@ type updateDataAgreementRecordReq struct {
OptIn bool `json:"optIn"`
}

type updateconsentRecordReq struct {
ConsentRecord consentRecord `json:"consentRecord"`
}

type consentRecord struct {
OptIn bool `json:"optIn"`
}

func ServiceUpdateDataAgreementRecord(w http.ResponseWriter, r *http.Request) {
// Headers
organisationId := common.Sanitize(r.Header.Get(config.OrganizationId))
Expand All @@ -45,37 +51,25 @@ func ServiceUpdateDataAgreementRecord(w http.ResponseWriter, r *http.Request) {
return
}

// Parse query params
dataAgreementId, err := daRecord.ParseQueryParams(r, config.DataAgreementId, daRecord.DataAgreementIdIsMissingError)
dataAgreementId = common.Sanitize(dataAgreementId)
if err != nil && errors.Is(err, daRecord.DataAgreementIdIsMissingError) {
m := "Query param dataAgreementId is required"
common.HandleErrorV2(w, http.StatusBadRequest, m, err)
return
}

// Request body
var dataAgreementRecordReq updateDataAgreementRecordReq
var updateConsentReq updateconsentRecordReq
b, _ := io.ReadAll(r.Body)
defer r.Body.Close()
json.Unmarshal(b, &dataAgreementRecordReq)

// validating request payload
valid, err := govalidator.ValidateStruct(dataAgreementRecordReq)
if !valid {
m := fmt.Sprintf("Failed to validate request body: %v", dataAgreementRecordId)
common.HandleErrorV2(w, http.StatusBadRequest, m, err)
return
}
// Repository
daRepo := dataagreement.DataAgreementRepository{}
daRepo.Init(organisationId)

_, err = daRepo.Get(dataAgreementId)
if err != nil {
m := fmt.Sprintf("Failed to fetch data agreement: %v", dataAgreementId)
common.HandleErrorV2(w, http.StatusBadRequest, m, err)
return
var optIn bool
// Unmarshal data agreement record req
json.Unmarshal(b, &dataAgreementRecordReq)
if dataAgreementRecordReq.OptIn {
optIn = dataAgreementRecordReq.OptIn
} else {
// Unmarshal update consent record req
json.Unmarshal(b, &updateConsentReq)
if updateConsentReq.ConsentRecord.OptIn {
optIn = updateConsentReq.ConsentRecord.OptIn
} else {
optIn = false
}
}

// Repository
Expand All @@ -88,6 +82,16 @@ func ServiceUpdateDataAgreementRecord(w http.ResponseWriter, r *http.Request) {
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}
// Repository
daRepo := dataagreement.DataAgreementRepository{}
daRepo.Init(organisationId)

_, err = daRepo.Get(toBeUpdatedDaRecord.DataAgreementId)
if err != nil {
m := fmt.Sprintf("Failed to fetch data agreement: %v", toBeUpdatedDaRecord.DataAgreementId)
common.HandleErrorV2(w, http.StatusBadRequest, m, err)
return
}

currentDataAgreementRecordRevision, err := revision.GetLatestByObjectIdAndSchemaName(dataAgreementRecordId, config.DataAgreementRecord)
if err != nil {
Expand All @@ -96,7 +100,7 @@ func ServiceUpdateDataAgreementRecord(w http.ResponseWriter, r *http.Request) {
return
}

if toBeUpdatedDaRecord.OptIn == dataAgreementRecordReq.OptIn {
if toBeUpdatedDaRecord.OptIn == optIn {
// response
resp := updateDataAgreementRecordResp{
DataAgreementRecord: toBeUpdatedDaRecord,
Expand All @@ -105,11 +109,11 @@ func ServiceUpdateDataAgreementRecord(w http.ResponseWriter, r *http.Request) {
common.ReturnHTTPResponse(resp, w)
return
}
toBeUpdatedDaRecord.OptIn = dataAgreementRecordReq.OptIn
toBeUpdatedDaRecord.OptIn = optIn

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

0 comments on commit 2470a2f

Please sign in to comment.