Files

106 lines
2.7 KiB
Go
Raw Permalink Normal View History

package automation
import (
"database/sql/driver"
"encoding/json"
"fmt"
"time"
)
// UUID is a custom type for PostgreSQL UUID
type UUID string
func (u UUID) String() string {
return string(u)
}
// Value implements the driver.Valuer interface
func (u UUID) Value() (driver.Value, error) {
return string(u), nil
}
// Scan implements the sql.Scanner interface
func (u *UUID) Scan(value interface{}) error {
if value == nil {
*u = ""
return nil
}
switch v := value.(type) {
case string:
*u = UUID(v)
case []byte:
*u = UUID(string(v))
default:
return fmt.Errorf("cannot scan type %T into UUID", value)
}
return nil
}
// JSONMap is a map that can be stored as JSONB
type JSONMap map[string]interface{}
// Value implements the driver.Valuer interface
func (j JSONMap) Value() (driver.Value, error) {
if j == nil {
return []byte("{}"), nil
}
return json.Marshal(j)
}
// Scan implements the sql.Scanner interface
func (j *JSONMap) Scan(value interface{}) error {
if value == nil {
*j = JSONMap{}
return nil
}
var bytes []byte
switch v := value.(type) {
case string:
bytes = []byte(v)
case []byte:
bytes = v
default:
return fmt.Errorf("cannot scan type %T into JSONMap", value)
}
return json.Unmarshal(bytes, j)
}
// ScheduledJob represents a scheduled automation job
type ScheduledJob struct {
ID UUID `json:"id"`
TenantID UUID `json:"tenant_id"`
Name string `json:"name"`
Description string `json:"description"`
CronExpr string `json:"cron_expr"`
Timezone string `json:"timezone"`
JobType string `json:"job_type"`
JobConfig JSONMap `json:"job_config"`
Status string `json:"status"`
LastRunAt *time.Time `json:"last_run_at"`
NextRunAt *time.Time `json:"next_run_at"`
RunCount int `json:"run_count"`
FailCount int `json:"fail_count"`
CreatedBy *UUID `json:"created_by"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// Workflow represents an automation workflow
type Workflow struct {
ID UUID `json:"id"`
TenantID UUID `json:"tenant_id"`
Name string `json:"name"`
Description string `json:"description"`
TriggerType string `json:"trigger_type"`
TriggerConfig JSONMap `json:"trigger_config"`
Actions []JSONMap `json:"actions"`
Status string `json:"status"`
LastRunAt *time.Time `json:"last_run_at"`
NextRunAt *time.Time `json:"next_run_at"`
RunCount int `json:"run_count"`
FailCount int `json:"fail_count"`
CreatedBy *UUID `json:"created_by"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}