Skip to content

Commit

Permalink
Add #376 Trigger webhook when consent is allowed and disallowed if we…
Browse files Browse the repository at this point in the history
…bhook is configured by the organisation
  • Loading branch information
albinpa authored and georgepadayatti committed Oct 26, 2023
1 parent 57a1ced commit 818d9fc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/v2/handler/service/service_create_dataagreement_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import (
"errors"
"fmt"
"net/http"
"strconv"
"time"

"github.com/bb-consent/api/src/common"
"github.com/bb-consent/api/src/config"
daRecord "github.com/bb-consent/api/src/v2/dataagreement_record"
daRecordHistory "github.com/bb-consent/api/src/v2/dataagreement_record_history"
"github.com/bb-consent/api/src/v2/dataattribute"
"github.com/bb-consent/api/src/v2/revision"
"github.com/bb-consent/api/src/v2/webhook"
"github.com/bb-consent/api/src/webhooks"
"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson/primitive"
)
Expand Down Expand Up @@ -137,6 +141,14 @@ func ServiceCreateDataAgreementRecord(w http.ResponseWriter, r *http.Request) {
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

// Trigger webhooks
var consentedAttributes []string
for _, pConsent := range savedDaRecord.DataAttributes {
consentedAttributes = append(consentedAttributes, pConsent.DataAttributeId)
}

go webhook.TriggerConsentWebhookEvent(individualId, dataAgreementId, savedDaRecord.Id.Hex(), organisationId, webhooks.EventTypes[30], strconv.FormatInt(time.Now().UTC().Unix(), 10), 0, consentedAttributes)
// Add data agreement record history
darH := daRecordHistory.DataAgreementRecordsHistory{}
darH.DataAgreementId = dataAgreementId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"time"

"github.com/bb-consent/api/src/common"
"github.com/bb-consent/api/src/config"
daRecord "github.com/bb-consent/api/src/v2/dataagreement_record"
daRecordHistory "github.com/bb-consent/api/src/v2/dataagreement_record_history"
"github.com/bb-consent/api/src/v2/revision"
"github.com/bb-consent/api/src/v2/signature"
"github.com/bb-consent/api/src/v2/webhook"
"go.mongodb.org/mongo-driver/bson/primitive"
)

Expand Down Expand Up @@ -84,6 +87,21 @@ func ServiceCreatePairedDataAgreementRecord(w http.ResponseWriter, r *http.Reque
return
}

// Trigger webhooks
var consentedAttributes []string
for _, pConsent := range savedDataAgreementRecord.DataAttributes {
consentedAttributes = append(consentedAttributes, pConsent.DataAttributeId)
}
var eventType string
if savedDataAgreementRecord.OptIn {
eventType = webhook.EventTypes[30]

} else {
eventType = webhook.EventTypes[30]
}

go webhook.TriggerConsentWebhookEvent(individualId, savedDataAgreementRecord.DataAgreementId, savedDataAgreementRecord.Id.Hex(), organisationId, eventType, strconv.FormatInt(time.Now().UTC().Unix(), 10), 0, consentedAttributes)

// Add data agreement record history
darH := daRecordHistory.DataAgreementRecordsHistory{}
darH.DataAgreementId = dataAgreementRecord.DataAgreementId
Expand Down
18 changes: 18 additions & 0 deletions src/v2/handler/service/service_update_dataagreement_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"time"

"github.com/asaskevich/govalidator"
"github.com/bb-consent/api/src/common"
"github.com/bb-consent/api/src/config"
daRecord "github.com/bb-consent/api/src/v2/dataagreement_record"
daRecordHistory "github.com/bb-consent/api/src/v2/dataagreement_record_history"
"github.com/bb-consent/api/src/v2/revision"
"github.com/bb-consent/api/src/v2/webhook"
"github.com/gorilla/mux"
)

Expand Down Expand Up @@ -111,6 +114,21 @@ func ServiceUpdateDataAgreementRecord(w http.ResponseWriter, r *http.Request) {
common.HandleErrorV2(w, http.StatusInternalServerError, m, err)
return
}

// Trigger webhooks
var consentedAttributes []string
for _, pConsent := range savedDaRecord.DataAttributes {
consentedAttributes = append(consentedAttributes, pConsent.DataAttributeId)
}
var eventType string
if savedDaRecord.OptIn {
eventType = webhook.EventTypes[30]

} else {
eventType = webhook.EventTypes[30]
}

go webhook.TriggerConsentWebhookEvent(individualId, dataAgreementId, dataAgreementRecordId, organisationId, eventType, strconv.FormatInt(time.Now().UTC().Unix(), 10), 0, consentedAttributes)
// Add data agreement record history
darH := daRecordHistory.DataAgreementRecordsHistory{}
darH.DataAgreementId = savedDaRecord.DataAgreementId
Expand Down
8 changes: 6 additions & 2 deletions src/v2/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,13 @@ func TriggerConsentWebhookEvent(userID, purposeID, consentID, orgID, eventType,
Days: days,
TimeStamp: timeStamp,
}
for _, e := range WebhooksConfiguration.Events {
if e == eventType {
// triggering the webhook
TriggerWebhooks(consentWebhookEvent, eventType)
}

// triggering the webhook
TriggerWebhooks(consentWebhookEvent, eventType)
}
}

// TriggerDataRequestWebhookEvent Trigger webhook for user data request events
Expand Down

0 comments on commit 818d9fc

Please sign in to comment.