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.
73 lines
2.9 KiB
Go
73 lines
2.9 KiB
Go
// Package tracking implements the day and week closing rules, the CSV export and
|
|
// the default settings of the wotra time tracking on top of PocketBase records.
|
|
package tracking
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/pocketbase/pocketbase/apis"
|
|
"github.com/pocketbase/pocketbase/core"
|
|
)
|
|
|
|
// Sentinel errors returned by the tracking functions.
|
|
var (
|
|
ErrDayAlreadyClosed = errors.New("day is already closed")
|
|
ErrDayNotClosed = errors.New("day is not closed")
|
|
ErrRunningEntryOnDay = errors.New("a running entry exists for this day; stop it first")
|
|
ErrDayHasNoEntries = errors.New("no completed entries found for this day")
|
|
ErrWeekAlreadyClosed = errors.New("week is already closed")
|
|
ErrWeekNotClosed = errors.New("week is not closed")
|
|
ErrWeekHasUnclosedDays = errors.New("all workdays in the week must be closed before closing the week")
|
|
ErrNoSettings = errors.New("no settings found")
|
|
ErrInvalidKind = errors.New("kind must be one of: holiday, vacation, sick")
|
|
ErrInvalidDayKey = errors.New("invalid day key, expected YYYY-MM-DD")
|
|
ErrInvalidWeekKey = errors.New("invalid week key, expected YYYY-Www")
|
|
)
|
|
|
|
// Register wires the tracking routes, the default settings hook and the midnight cron.
|
|
func Register(app core.App) {
|
|
app.OnServe().BindFunc(func(se *core.ServeEvent) error {
|
|
se.Router.GET("/healthz", func(e *core.RequestEvent) error {
|
|
return e.JSON(http.StatusOK, map[string]string{"status": "ok"})
|
|
})
|
|
g := se.Router.Group("/api")
|
|
g.Bind(apis.RequireAuth("users"))
|
|
g.POST("/days/{dayKey}/close", handleCloseDay)
|
|
g.POST("/days/{dayKey}/mark", handleMarkDay)
|
|
g.DELETE("/days/{dayKey}/close", handleReopenDay)
|
|
g.POST("/weeks/{weekKey}/close", handleCloseWeek)
|
|
g.DELETE("/weeks/{weekKey}/close", handleReopenWeek)
|
|
g.GET("/export/entries.csv", handleExportEntries)
|
|
g.GET("/export/days.csv", handleExportDays)
|
|
g.GET("/export/weeks.csv", handleExportWeeks)
|
|
return se.Next()
|
|
})
|
|
app.OnRecordAfterCreateSuccess("users").BindFunc(seedDefaultSettings)
|
|
app.Cron().MustAdd("wotra_midnight", "* * * * *", func() {
|
|
if _, err := AutoStopStalled(app, time.Now()); err != nil {
|
|
app.Logger().Error("midnight auto-stop failed", "error", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
// writeError answers err in the PocketBase error envelope plus the legacy "error"
|
|
// key, so that the old and the new client read the same message. A server error
|
|
// is logged with its cause and answered with a generic message, because the
|
|
// error text can contain SQL and other internals.
|
|
func writeError(e *core.RequestEvent, status int, err error) error {
|
|
message := err.Error()
|
|
if status >= http.StatusInternalServerError {
|
|
e.App.Logger().Error("tracking request failed",
|
|
"method", e.Request.Method, "path", e.Request.URL.Path, "status", status, "error", err)
|
|
message = "internal server error"
|
|
}
|
|
return e.JSON(status, map[string]any{
|
|
"status": status,
|
|
"message": message,
|
|
"data": map[string]any{},
|
|
"error": message,
|
|
})
|
|
}
|