Make webhook work.

This commit is contained in:
Gucheng Wang
2021-11-07 23:53:17 +08:00
parent 87e6fb63e1
commit 30a2fdef37
6 changed files with 91 additions and 5 deletions

View File

@ -48,6 +48,7 @@ func NewRecord(ctx *context.Context) *Record {
RequestUri: ctx.Request.RequestURI,
User: "",
Action: action,
IsTriggered: false,
}
return &record
}
@ -55,11 +56,20 @@ func NewRecord(ctx *context.Context) *Record {
func AddRecord(record *Record) bool {
record.Owner = record.Organization
errWebhook := SendWebhooks(record)
if errWebhook == nil {
record.IsTriggered = true
}
affected, err := adapter.Engine.Insert(record)
if err != nil {
panic(err)
}
if errWebhook != nil {
panic(errWebhook)
}
return affected != 0
}
@ -101,3 +111,25 @@ func GetRecordsByField(record *Record) []*Record {
return records
}
func SendWebhooks(record *Record) error {
webhooks := getWebhooksByOrganization(record.Organization)
for _, webhook := range webhooks {
matched := false
for _, event := range webhook.Events {
if record.Action == event {
matched = true
break
}
}
if matched {
err := sendWebhook(webhook, record)
if err != nil {
return err
}
}
}
return nil
}