Marking a day answered every error except an invalid day key with 422, including database errors. Only an invalid kind is a 422 now. Server errors are logged with their cause and answered with a generic message instead of the error text, which can contain SQL, and the timezone lookup logs at warn before it falls back to UTC.
97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
package tracking
|
|
|
|
import (
|
|
"math/bits"
|
|
"time"
|
|
|
|
"github.com/pocketbase/dbx"
|
|
"github.com/pocketbase/pocketbase/core"
|
|
)
|
|
|
|
const (
|
|
seedEffectiveFrom = "2000-01-01"
|
|
seedHoursPerWeek = 40.0
|
|
seedWorkdaysMask = 31
|
|
seedTimezone = "UTC"
|
|
)
|
|
|
|
func seedDefaultSettings(e *core.RecordEvent) error {
|
|
count, err := e.App.CountRecords("settings_history", dbx.HashExp{"owner": e.Record.Id})
|
|
if err == nil && count == 0 {
|
|
var rec *core.Record
|
|
rec, err = newRecord(e.App, "settings_history", e.Record.Id)
|
|
if err == nil {
|
|
rec.Set("effective_from", seedEffectiveFrom)
|
|
rec.Set("hours_per_week", seedHoursPerWeek)
|
|
rec.Set("workdays_mask", seedWorkdaysMask)
|
|
rec.Set("timezone", seedTimezone)
|
|
bump(rec, time.Now())
|
|
err = e.App.Save(rec)
|
|
}
|
|
}
|
|
if err != nil {
|
|
// The user is created regardless; it just starts without settings.
|
|
e.App.Logger().Error("seeding default settings failed", "owner", e.Record.Id, "error", err)
|
|
}
|
|
return e.Next()
|
|
}
|
|
|
|
func settingsAt(tx core.App, owner, dayKey string) (*core.Record, error) {
|
|
rows, err := tx.FindRecordsByFilter("settings_history",
|
|
liveFilter+" && effective_from <= {:day}",
|
|
"-effective_from,-id",
|
|
1, 0,
|
|
dbx.Params{"owner": owner, "day": dayKey},
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(rows) == 0 {
|
|
return nil, nil
|
|
}
|
|
return rows[0], nil
|
|
}
|
|
|
|
// location returns the timezone of the settings effective on dayKey, or UTC when
|
|
// there are none or they cannot be read. A failed lookup is logged because UTC
|
|
// is only a guess at what the owner meant.
|
|
func location(tx core.App, owner, dayKey string) *time.Location {
|
|
set, err := settingsAt(tx, owner, dayKey)
|
|
if err != nil {
|
|
tx.Logger().Warn("cannot read the settings for the timezone, using UTC",
|
|
"owner", owner, "dayKey", dayKey, "error", err)
|
|
return time.UTC
|
|
}
|
|
if set == nil {
|
|
return time.UTC
|
|
}
|
|
timezone := set.GetString("timezone")
|
|
loc, err := time.LoadLocation(timezone)
|
|
if err != nil {
|
|
tx.Logger().Warn("unknown timezone in the settings, using UTC",
|
|
"owner", owner, "dayKey", dayKey, "timezone", timezone, "error", err)
|
|
return time.UTC
|
|
}
|
|
return loc
|
|
}
|
|
|
|
func weekExpectedMs(set *core.Record) int64 {
|
|
return int64(set.GetFloat("hours_per_week") * 3_600_000)
|
|
}
|
|
|
|
func dayExpectedMs(set *core.Record) int64 {
|
|
days := bits.OnesCount(uint(workdaysMask(set)))
|
|
if days == 0 {
|
|
return 0
|
|
}
|
|
return weekExpectedMs(set) / int64(days)
|
|
}
|
|
|
|
func isWorkday(set *core.Record, wd time.Weekday) bool {
|
|
return workdaysMask(set)&(1<<((int(wd)+6)%7)) != 0
|
|
}
|
|
|
|
func workdaysMask(set *core.Record) int {
|
|
return set.GetInt("workdays_mask")
|
|
}
|