Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #614 Update consent record #615

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading