holiday-api/webhook/model.go

74 lines
1.6 KiB
Go
Raw Normal View History

package webhook
import (
"encoding/json"
"github.com/google/uuid"
"time"
)
type EventType string
const (
TypeCreated EventType = "created"
TypeEdited EventType = "edited"
TypeDeleted EventType = "deleted"
)
type Event struct {
Type EventType `json:"type"`
Holiday struct {
Id uuid.UUID `json:"id"`
Name string `json:"name"`
Country string `json:"country"`
Description string `json:"description"`
Date time.Time `json:"date"`
IsStateHoliday bool `json:"is_state_holiday"`
IsReligiousHoliday bool `json:"is_religious_holiday"`
} `json:"holiday"`
}
func (e Event) Json() string {
content, err := json.Marshal(e)
if err != nil {
panic(err)
}
return string(content)
}
type Webhook struct {
Id uuid.UUID `db:"id"`
Created time.Time `db:"created"`
Url string `db:"url"`
Country string `db:"country"`
RetryCount int `db:"retry_count"`
OnCreated bool `db:"on_created"`
OnEdited bool `db:"on_edited"`
OnDeleted bool `db:"on_deleted"`
}
func (w Webhook) ShouldEmit(e Event) bool {
if w.Country == e.Holiday.Country {
if e.Type == TypeCreated && w.OnCreated {
return true
} else if e.Type == TypeEdited && w.OnEdited {
return true
} else if e.Type == TypeDeleted && w.OnDeleted {
return true
}
}
return false
}
type Job struct {
Id uuid.UUID `db:"id"`
WebhookId uuid.UUID `db:"webhook_id"`
Created time.Time `db:"created"`
Success bool `db:"success"`
SuccessTime *time.Time `db:"success_time"`
RetryCount int `db:"retry_count"`
Content string `db:"content"`
}