feat: create database tables and queries for notifications (#13536)

This commit is contained in:
Danny Kopping
2024-06-28 09:21:25 +00:00
committed by GitHub
parent 4213560b7a
commit 0a221e8d5b
17 changed files with 999 additions and 0 deletions
+155
View File
@@ -660,6 +660,134 @@ func AllLoginTypeValues() []LoginType {
}
}
type NotificationMessageStatus string
const (
NotificationMessageStatusPending NotificationMessageStatus = "pending"
NotificationMessageStatusLeased NotificationMessageStatus = "leased"
NotificationMessageStatusSent NotificationMessageStatus = "sent"
NotificationMessageStatusPermanentFailure NotificationMessageStatus = "permanent_failure"
NotificationMessageStatusTemporaryFailure NotificationMessageStatus = "temporary_failure"
NotificationMessageStatusUnknown NotificationMessageStatus = "unknown"
)
func (e *NotificationMessageStatus) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
*e = NotificationMessageStatus(s)
case string:
*e = NotificationMessageStatus(s)
default:
return fmt.Errorf("unsupported scan type for NotificationMessageStatus: %T", src)
}
return nil
}
type NullNotificationMessageStatus struct {
NotificationMessageStatus NotificationMessageStatus `json:"notification_message_status"`
Valid bool `json:"valid"` // Valid is true if NotificationMessageStatus is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullNotificationMessageStatus) Scan(value interface{}) error {
if value == nil {
ns.NotificationMessageStatus, ns.Valid = "", false
return nil
}
ns.Valid = true
return ns.NotificationMessageStatus.Scan(value)
}
// Value implements the driver Valuer interface.
func (ns NullNotificationMessageStatus) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return string(ns.NotificationMessageStatus), nil
}
func (e NotificationMessageStatus) Valid() bool {
switch e {
case NotificationMessageStatusPending,
NotificationMessageStatusLeased,
NotificationMessageStatusSent,
NotificationMessageStatusPermanentFailure,
NotificationMessageStatusTemporaryFailure,
NotificationMessageStatusUnknown:
return true
}
return false
}
func AllNotificationMessageStatusValues() []NotificationMessageStatus {
return []NotificationMessageStatus{
NotificationMessageStatusPending,
NotificationMessageStatusLeased,
NotificationMessageStatusSent,
NotificationMessageStatusPermanentFailure,
NotificationMessageStatusTemporaryFailure,
NotificationMessageStatusUnknown,
}
}
type NotificationMethod string
const (
NotificationMethodSmtp NotificationMethod = "smtp"
NotificationMethodWebhook NotificationMethod = "webhook"
)
func (e *NotificationMethod) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
*e = NotificationMethod(s)
case string:
*e = NotificationMethod(s)
default:
return fmt.Errorf("unsupported scan type for NotificationMethod: %T", src)
}
return nil
}
type NullNotificationMethod struct {
NotificationMethod NotificationMethod `json:"notification_method"`
Valid bool `json:"valid"` // Valid is true if NotificationMethod is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullNotificationMethod) Scan(value interface{}) error {
if value == nil {
ns.NotificationMethod, ns.Valid = "", false
return nil
}
ns.Valid = true
return ns.NotificationMethod.Scan(value)
}
// Value implements the driver Valuer interface.
func (ns NullNotificationMethod) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return string(ns.NotificationMethod), nil
}
func (e NotificationMethod) Valid() bool {
switch e {
case NotificationMethodSmtp,
NotificationMethodWebhook:
return true
}
return false
}
func AllNotificationMethodValues() []NotificationMethod {
return []NotificationMethod{
NotificationMethodSmtp,
NotificationMethodWebhook,
}
}
type ParameterDestinationScheme string
const (
@@ -1885,6 +2013,33 @@ type License struct {
UUID uuid.UUID `db:"uuid" json:"uuid"`
}
type NotificationMessage struct {
ID uuid.UUID `db:"id" json:"id"`
NotificationTemplateID uuid.UUID `db:"notification_template_id" json:"notification_template_id"`
UserID uuid.UUID `db:"user_id" json:"user_id"`
Method NotificationMethod `db:"method" json:"method"`
Status NotificationMessageStatus `db:"status" json:"status"`
StatusReason sql.NullString `db:"status_reason" json:"status_reason"`
CreatedBy string `db:"created_by" json:"created_by"`
Payload []byte `db:"payload" json:"payload"`
AttemptCount sql.NullInt32 `db:"attempt_count" json:"attempt_count"`
Targets []uuid.UUID `db:"targets" json:"targets"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
UpdatedAt sql.NullTime `db:"updated_at" json:"updated_at"`
LeasedUntil sql.NullTime `db:"leased_until" json:"leased_until"`
NextRetryAfter sql.NullTime `db:"next_retry_after" json:"next_retry_after"`
}
// Templates from which to create notification messages.
type NotificationTemplate struct {
ID uuid.UUID `db:"id" json:"id"`
Name string `db:"name" json:"name"`
TitleTemplate string `db:"title_template" json:"title_template"`
BodyTemplate string `db:"body_template" json:"body_template"`
Actions []byte `db:"actions" json:"actions"`
Group sql.NullString `db:"group" json:"group"`
}
// A table used to configure apps that can use Coder as an OAuth2 provider, the reverse of what we are calling external authentication.
type OAuth2ProviderApp struct {
ID uuid.UUID `db:"id" json:"id"`