From 818d9fc2da03de596ea145610266f3576bb9615d Mon Sep 17 00:00:00 2001 From: Albin Antony Date: Thu, 26 Oct 2023 14:34:17 +0530 Subject: [PATCH] Add #376 Trigger webhook when consent is allowed and disallowed if webhook is configured by the organisation --- .../service_create_dataagreement_record.go | 12 ++++++++++++ ...ce_create_paired_dataagreement_record.go.go | 18 ++++++++++++++++++ .../service_update_dataagreement_record.go | 18 ++++++++++++++++++ src/v2/webhook/webhook.go | 8 ++++++-- 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/v2/handler/service/service_create_dataagreement_record.go b/src/v2/handler/service/service_create_dataagreement_record.go index 76b7a81..8788d55 100644 --- a/src/v2/handler/service/service_create_dataagreement_record.go +++ b/src/v2/handler/service/service_create_dataagreement_record.go @@ -5,6 +5,8 @@ import ( "errors" "fmt" "net/http" + "strconv" + "time" "github.com/bb-consent/api/src/common" "github.com/bb-consent/api/src/config" @@ -12,6 +14,8 @@ import ( 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" ) @@ -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 diff --git a/src/v2/handler/service/service_create_paired_dataagreement_record.go.go b/src/v2/handler/service/service_create_paired_dataagreement_record.go.go index dc5b97c..d0abe85 100644 --- a/src/v2/handler/service/service_create_paired_dataagreement_record.go.go +++ b/src/v2/handler/service/service_create_paired_dataagreement_record.go.go @@ -5,6 +5,8 @@ import ( "fmt" "io" "net/http" + "strconv" + "time" "github.com/bb-consent/api/src/common" "github.com/bb-consent/api/src/config" @@ -12,6 +14,7 @@ import ( 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" ) @@ -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 diff --git a/src/v2/handler/service/service_update_dataagreement_record.go b/src/v2/handler/service/service_update_dataagreement_record.go index 9df98a2..f360fea 100644 --- a/src/v2/handler/service/service_update_dataagreement_record.go +++ b/src/v2/handler/service/service_update_dataagreement_record.go @@ -6,6 +6,8 @@ import ( "fmt" "io" "net/http" + "strconv" + "time" "github.com/asaskevich/govalidator" "github.com/bb-consent/api/src/common" @@ -13,6 +15,7 @@ import ( 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" ) @@ -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 diff --git a/src/v2/webhook/webhook.go b/src/v2/webhook/webhook.go index 07e5622..08f0b4c 100644 --- a/src/v2/webhook/webhook.go +++ b/src/v2/webhook/webhook.go @@ -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