Skip to content

Commit

Permalink
Fix #471 Recent webhook deliveries api returns empty response
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa authored and georgepadayatti committed Nov 7, 2023
1 parent df50ffb commit c1578dd
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"errors"
"fmt"
"net/http"
"strconv"
"time"

"github.com/bb-consent/api/internal/common"
"github.com/bb-consent/api/internal/config"
Expand Down Expand Up @@ -131,7 +129,7 @@ func ServiceCreateDataAgreementRecord(w http.ResponseWriter, r *http.Request) {
consentedAttributes = append(consentedAttributes, pConsent.Id.Hex())
}

go webhook.TriggerConsentWebhookEvent(individualId, dataAgreementId, savedDaRecord.Id.Hex(), organisationId, webhook.EventTypes[30], strconv.FormatInt(time.Now().UTC().Unix(), 10), 0, consentedAttributes)
go webhook.TriggerConsentWebhookEvent(individualId, dataAgreementId, savedDaRecord.Id.Hex(), organisationId, webhook.EventTypes[30], 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,8 +5,6 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"time"

"github.com/bb-consent/api/internal/common"
"github.com/bb-consent/api/internal/config"
Expand Down Expand Up @@ -125,7 +123,7 @@ func ServiceCreatePairedDataAgreementRecord(w http.ResponseWriter, r *http.Reque
eventType = webhook.EventTypes[31]
}

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

// Add data agreement record history
darH := daRecordHistory.DataAgreementRecordsHistory{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"time"

"github.com/asaskevich/govalidator"
"github.com/bb-consent/api/internal/common"
Expand Down Expand Up @@ -128,7 +126,7 @@ func ServiceUpdateDataAgreementRecord(w http.ResponseWriter, r *http.Request) {
eventType = webhook.EventTypes[31]
}

go webhook.TriggerConsentWebhookEvent(individualId, dataAgreementId, dataAgreementRecordId, organisationId, eventType, strconv.FormatInt(time.Now().UTC().Unix(), 10), 0, consentedAttributes)
go webhook.TriggerConsentWebhookEvent(individualId, dataAgreementId, dataAgreementRecordId, organisationId, eventType, 0, consentedAttributes)
// Add data agreement record history
darH := daRecordHistory.DataAgreementRecordsHistory{}
darH.DataAgreementId = savedDaRecord.DataAgreementId
Expand Down
51 changes: 29 additions & 22 deletions internal/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/bb-consent/api/internal/actionlog"
"github.com/bb-consent/api/internal/config"
"github.com/bb-consent/api/internal/user"
"github.com/bb-consent/api/internal/individual"
"github.com/bb-consent/api/internal/webhook_dispatcher"
)

Expand Down Expand Up @@ -117,13 +117,13 @@ var DeliveryStatus = map[int]string{

// ConsentWebhookEvent Details of consent events
type ConsentWebhookEvent struct {
OrganisationID string `json:"organisationID"`
UserID string `json:"userID"`
ConsentID string `json:"consentID"`
PurposeID string `json:"purposeID"`
Attributes []string `json:"attribute"`
Days int `json:"days"`
TimeStamp string `json:"timestamp"`
OrganisationID string `json:"organisationID"`
UserID string `json:"userID"`
ConsentRecordID string `json:"consentRecordID"`
DataAgreementID string `json:"dataAgreementID"`
Attributes []string `json:"attribute"`
Days int `json:"days"`
TimeStamp string `json:"timestamp"`
}

// GetOrganisationID Returns organisation ID
Expand Down Expand Up @@ -199,8 +199,12 @@ func PingWebhook(webhook Webhook) (req *http.Request, resp *http.Response, execu
// TriggerWebhooks Trigger webhooks based on event type
func TriggerWebhooks(webhookEventData WebhookEventData, webhookEventType string) {

// Repository
individualRepo := individual.IndividualRepository{}
individualRepo.Init(webhookEventData.GetOrganisationID())

// Get the user
u, err := user.Get(webhookEventData.GetUserID())
individual, err := individualRepo.Get(webhookEventData.GetUserID())
if err != nil {
log.Printf("Failed to fetch user details;Failed to trigger webhook for event:<%s>, org:<%s>", webhookEventType, webhookEventData.GetOrganisationID())
return
Expand All @@ -209,7 +213,7 @@ func TriggerWebhooks(webhookEventData WebhookEventData, webhookEventType string)
// Get the active webhooks for the organisation
activeWebhooks, err := GetActiveWebhooksByOrgID(webhookEventData.GetOrganisationID())
if err != nil {
log.Printf("Failed to fetch active webhooks;Failed to trigger webhook for event:<%s>, user:<%s>, org:<%s>", webhookEventType, u.ID.Hex(), webhookEventData.GetOrganisationID())
log.Printf("Failed to fetch active webhooks;Failed to trigger webhook for event:<%s>, user:<%s>, org:<%s>", webhookEventType, individual.Id.Hex(), webhookEventData.GetOrganisationID())
return
}

Expand All @@ -228,23 +232,23 @@ func TriggerWebhooks(webhookEventData WebhookEventData, webhookEventType string)
// Constructing webhook payload
we := WebhookEvent{
WebhookID: toBeProcessedWebhook.ID.Hex(),
Timestamp: strconv.FormatInt(time.Now().UTC().Unix(), 10),
Timestamp: time.Now().UTC().Format("2006-01-02T15:04:05Z"),
Data: webhookEventData,
Type: webhookEventType,
}

// Converting the webhook event data to bytes
b, err := json.Marshal(we)
if err != nil {
log.Printf("Failed to convert webhook event data to bytes, error:%v, Failed to trigger webhook for event:<%s>, user:<%s>, org:<%s>", err.Error(), webhookEventType, u.ID.Hex(), webhookEventData.GetOrganisationID())
log.Printf("Failed to convert webhook event data to bytes, error:%v, Failed to trigger webhook for event:<%s>, user:<%s>, org:<%s>", err.Error(), webhookEventType, individual.Id.Hex(), webhookEventData.GetOrganisationID())
return
}

go webhook_dispatcher.ProcessWebhooks(webhookEventType, b)

// Log webhook calls in webhooks category
aLog := fmt.Sprintf("Organization webhook: %v triggered by user: %v by event: %v", toBeProcessedWebhook.PayloadURL, u.Email, webhookEventType)
actionlog.LogOrgWebhookCalls(u.ID.Hex(), u.Email, webhookEventData.GetOrganisationID(), aLog)
aLog := fmt.Sprintf("Organization webhook: %v triggered by user: %v by event: %v", toBeProcessedWebhook.PayloadURL, individual.Email, webhookEventType)
actionlog.LogOrgWebhookCalls(individual.Id.Hex(), individual.Email, webhookEventData.GetOrganisationID(), aLog)
}

}
Expand All @@ -263,17 +267,20 @@ func TriggerOrgSubscriptionWebhookEvent(userID, orgID string, eventType string)
}

// TriggerConsentWebhookEvent Trigger webhook for consent related events
func TriggerConsentWebhookEvent(userID, purposeID, consentID, orgID, eventType, timeStamp string, days int, attributes []string) {
func TriggerConsentWebhookEvent(userID, DataAgreementID, ConsentRecordID, orgID, eventType string, days int, attributes []string) {
if days <= 0 {
days = 30
}

// Constructing webhook event data attribute
consentWebhookEvent := ConsentWebhookEvent{
OrganisationID: orgID,
UserID: userID,
ConsentID: consentID,
PurposeID: purposeID,
Attributes: attributes,
Days: days,
TimeStamp: timeStamp,
OrganisationID: orgID,
UserID: userID,
ConsentRecordID: ConsentRecordID,
DataAgreementID: DataAgreementID,
Attributes: attributes,
Days: days,
TimeStamp: time.Now().UTC().Format("2006-01-02T15:04:05Z"),
}
for _, e := range WebhooksConfiguration.Events {
if e == eventType {
Expand Down
11 changes: 5 additions & 6 deletions internal/webhook_dispatcher/webhookdispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -95,7 +94,7 @@ func ProcessWebhooks(webhookEventType string, value []byte) {
var webhookDelivery WebhookDelivery

// Recording webhook processing start timestamp
executionStartTimeStamp = strconv.FormatInt(time.Now().UTC().Unix(), 10)
executionStartTimeStamp = time.Now().UTC().Format("2006-01-02T15:04:05Z")

// To store incoming webhook events
var webhookEvent WebhookEvent
Expand Down Expand Up @@ -155,7 +154,7 @@ func ProcessWebhooks(webhookEventType string, value []byte) {
requestPayload, _ := json.Marshal(&webhookEvent)

// Current UTC timestamp
timestamp := strconv.FormatInt(time.Now().UTC().Unix(), 10)
timestamp := time.Now().UTC().Format("2006-01-02T15:04:05Z")

// Constructing SHA256 payload
sha256Payload := timestamp + "." + string(requestPayload)
Expand Down Expand Up @@ -202,7 +201,7 @@ func ProcessWebhooks(webhookEventType string, value []byte) {
log.Printf("HTTP POST request failed err:%v;Failed processing webhook:%s triggered by user:%s of org:%s for event:%s", err.Error(), webhookEvent.WebhookID, userID, orgID, webhookEventType)

// Recording webhook processing end timestamp
executionEndTimeStamp = strconv.FormatInt(time.Now().UTC().Unix(), 10)
executionEndTimeStamp = time.Now().UTC().Format("2006-01-02T15:04:05Z")

// Recording webhook delivery details to db
webhookDelivery.RequestHeaders = req.Header
Expand All @@ -225,7 +224,7 @@ func ProcessWebhooks(webhookEventType string, value []byte) {
if err != nil {

// Recording webhook processing end timestamp
executionEndTimeStamp = strconv.FormatInt(time.Now().UTC().Unix(), 10)
executionEndTimeStamp = time.Now().UTC().Format("2006-01-02T15:04:05Z")

// Recording webhook delivery details to db
webhookDelivery.RequestHeaders = req.Header
Expand All @@ -247,7 +246,7 @@ func ProcessWebhooks(webhookEventType string, value []byte) {
}

// Recording webhook processing end timestamp
executionEndTimeStamp = strconv.FormatInt(time.Now().UTC().Unix(), 10)
executionEndTimeStamp = time.Now().UTC().Format("2006-01-02T15:04:05Z")

// Recording webhook delivery details to db
webhookDelivery.RequestHeaders = req.Header
Expand Down

0 comments on commit c1578dd

Please sign in to comment.