1. Version Versendung
This commit is contained in:
1 parent
d7113afa5f
commit
781aa20253
31 files changed
+4023
-10
No files matched your search
@@ -0,0 +1,136 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"lektorenplan/domain"
|
||||
)
|
||||
|
||||
// DispatchPreference stores how one person receives the plan.
|
||||
type DispatchPreference struct {
|
||||
Channel string
|
||||
Variant string
|
||||
Cover bool
|
||||
}
|
||||
|
||||
// DispatchLogEntry records one dispatch action for one person.
|
||||
type DispatchLogEntry struct {
|
||||
ID string
|
||||
TenantID string
|
||||
BatchID string
|
||||
RangeStart time.Time
|
||||
RangeEnd time.Time
|
||||
PersonID string
|
||||
PersonName string
|
||||
Channel string
|
||||
Variant string
|
||||
Cover bool
|
||||
Email string
|
||||
Status string
|
||||
Detail string
|
||||
CreatedAt time.Time
|
||||
CreatedBy string
|
||||
}
|
||||
|
||||
// DispatchPreferences loads the saved preferences of a tenant.
|
||||
func (s *Store) DispatchPreferences(ctx context.Context, tenantID string) (map[string]DispatchPreference, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT person_id, channel, variant, cover
|
||||
FROM dispatch_preferences WHERE tenant_id=?`, tenantID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := map[string]DispatchPreference{}
|
||||
for rows.Next() {
|
||||
var personID string
|
||||
var cover int
|
||||
var preference DispatchPreference
|
||||
if err := rows.Scan(&personID, &preference.Channel, &preference.Variant, &cover); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
preference.Cover = cover != 0
|
||||
out[personID] = preference
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// SaveDispatchPreference stores one person's dispatch preference.
|
||||
func (s *Store) SaveDispatchPreference(ctx context.Context, tenantID, personID, channel, variant string, cover bool) error {
|
||||
_, err := s.db.ExecContext(ctx, `
|
||||
INSERT INTO dispatch_preferences(tenant_id, person_id, channel, variant, cover, updated_at)
|
||||
VALUES(?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(tenant_id, person_id) DO UPDATE SET
|
||||
channel=excluded.channel, variant=excluded.variant,
|
||||
cover=excluded.cover, updated_at=excluded.updated_at`,
|
||||
tenantID, personID, channel, variant, boolInt(cover), nowText())
|
||||
return err
|
||||
}
|
||||
|
||||
// DispatchLog returns the most recent dispatch entries of a tenant.
|
||||
func (s *Store) DispatchLog(ctx context.Context, tenantID string, limit int) ([]DispatchLogEntry, error) {
|
||||
if limit < 1 {
|
||||
limit = 1
|
||||
}
|
||||
if limit > 500 {
|
||||
limit = 500
|
||||
}
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT id, tenant_id, batch_id, range_start, range_end, person_id, person_name,
|
||||
channel, variant, cover, email, status, detail, created_at, created_by
|
||||
FROM dispatch_log WHERE tenant_id=?
|
||||
ORDER BY created_at DESC, id DESC LIMIT ?`, tenantID, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []DispatchLogEntry
|
||||
for rows.Next() {
|
||||
var entry DispatchLogEntry
|
||||
var rangeStart, rangeEnd, created string
|
||||
var cover int
|
||||
if err := rows.Scan(&entry.ID, &entry.TenantID, &entry.BatchID, &rangeStart,
|
||||
&rangeEnd, &entry.PersonID, &entry.PersonName, &entry.Channel,
|
||||
&entry.Variant, &cover, &entry.Email, &entry.Status, &entry.Detail,
|
||||
&created, &entry.CreatedBy); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entry.RangeStart = domain.MustDate(rangeStart)
|
||||
entry.RangeEnd = domain.MustDate(rangeEnd)
|
||||
entry.Cover = cover != 0
|
||||
entry.CreatedAt = parseTime(created)
|
||||
out = append(out, entry)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// AppendDispatchLog records a batch of dispatch actions.
|
||||
func (s *Store) AppendDispatchLog(ctx context.Context, entries []DispatchLogEntry) error {
|
||||
if len(entries) == 0 {
|
||||
return nil
|
||||
}
|
||||
return s.transaction(ctx, func(tx *sql.Tx) error {
|
||||
for _, entry := range entries {
|
||||
if entry.ID == "" {
|
||||
entry.ID = domain.NewID()
|
||||
}
|
||||
if entry.CreatedAt.IsZero() {
|
||||
entry.CreatedAt = time.Now().UTC()
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO dispatch_log(id, tenant_id, batch_id, range_start, range_end,
|
||||
person_id, person_name, channel, variant, cover, email, status, detail,
|
||||
created_at, created_by)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
entry.ID, entry.TenantID, entry.BatchID, domain.Key(entry.RangeStart),
|
||||
domain.Key(entry.RangeEnd), entry.PersonID, entry.PersonName, entry.Channel,
|
||||
entry.Variant, boolInt(entry.Cover), entry.Email, entry.Status, entry.Detail,
|
||||
entry.CreatedAt.Format(time.RFC3339), entry.CreatedBy); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user