189 lines
7.3 KiB
Go
189 lines
7.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"strings"
|
|
"time"
|
|
"unicode/utf8"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type TaskWorkRevision struct {
|
|
Item TaskWork `json:"item"`
|
|
Action string `json:"action"`
|
|
Reason string `json:"reason"`
|
|
ChangedMS int64 `json:"changed_ms"`
|
|
}
|
|
|
|
func (s *Store) auditTaskWork(ctx context.Context, tx *sql.Tx, domain, actor, action, reason string, v TaskWork, now time.Time) error {
|
|
raw, err := json.Marshal(v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Legacy sessions acquire a baseline lazily. Never rewrite an earlier audit.
|
|
query := `INSERT INTO organizer_work_revisions(work_uuid,version,domain_uuid,owner_uuid,action,reason,changed_ms,data) VALUES(` + s.sqlParam(1) + `,` + s.sqlParam(2) + `,` + s.sqlParam(3) + `,` + s.sqlParam(4) + `,` + s.sqlParam(5) + `,` + s.sqlParam(6) + `,` + s.sqlParam(7) + `,` + s.sqlParam(8) + `)`
|
|
if action == "baseline" {
|
|
query += ` ON CONFLICT(work_uuid,version) DO NOTHING`
|
|
}
|
|
_, err = tx.ExecContext(ctx, query, v.UUID, v.Version, domain, actor, action, reason, now.UnixMilli(), string(raw))
|
|
return err
|
|
}
|
|
|
|
func (s *Store) correctTaskWork(ctx context.Context, tx *sql.Tx, domain, actor, action, reason string, v, old TaskWork, now time.Time) (TaskWork, error) {
|
|
reason = strings.TrimSpace(reason)
|
|
if reason == "" || len(reason) > 500 || !utf8.ValidString(reason) || strings.ContainsAny(reason, "\x00\r\n") {
|
|
return v, ErrTaskInvalid
|
|
}
|
|
if v.Version < 1 || old.EndMS == 0 || old.CancelledMS != 0 {
|
|
return v, ErrTaskConflict
|
|
}
|
|
if old.Version != v.Version {
|
|
return v, ErrTaskConflict
|
|
}
|
|
if v.TaskUUID != "" && v.TaskUUID != old.TaskUUID {
|
|
return v, ErrTaskInvalid
|
|
}
|
|
result := old
|
|
if action == "correct" {
|
|
if v.StartMS <= 0 || v.EndMS <= v.StartMS || v.EndMS > now.UnixMilli() || v.EndMS-v.StartMS > int64(31*24*time.Hour/time.Millisecond) {
|
|
return v, ErrTaskInvalid
|
|
}
|
|
// Adjacent spans are valid; double-counted elapsed time is not. Scope across
|
|
// Domains like the existing one-active-session constraint, without disclosing them.
|
|
var overlap bool
|
|
err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM organizer_work_sessions WHERE owner_uuid=`+s.sqlParam(1)+` AND uuid<>`+s.sqlParam(2)+` AND cancelled_ms=0 AND start_ms<`+s.sqlParam(3)+` AND (end_ms=0 OR end_ms>`+s.sqlParam(4)+`))`, actor, old.UUID, v.EndMS, v.StartMS).Scan(&overlap)
|
|
if err != nil {
|
|
return v, err
|
|
}
|
|
if overlap {
|
|
return v, ErrTaskConflict
|
|
}
|
|
result.StartMS = v.StartMS
|
|
result.EndMS = v.EndMS
|
|
} else {
|
|
result.CancelledMS = now.UnixMilli()
|
|
}
|
|
if err := s.auditTaskWork(ctx, tx, domain, actor, "baseline", "", old, now); err != nil {
|
|
return v, err
|
|
}
|
|
result.Version++
|
|
updated, err := tx.ExecContext(ctx, `UPDATE organizer_work_sessions SET start_ms=`+s.sqlParam(1)+`,end_ms=`+s.sqlParam(2)+`,cancelled_ms=`+s.sqlParam(3)+`,version=`+s.sqlParam(4)+` WHERE uuid=`+s.sqlParam(5)+` AND domain_uuid=`+s.sqlParam(6)+` AND owner_uuid=`+s.sqlParam(7)+` AND version=`+s.sqlParam(8)+``, result.StartMS, result.EndMS, result.CancelledMS, result.Version, old.UUID, domain, actor, old.Version)
|
|
if err != nil {
|
|
return v, err
|
|
}
|
|
if count, err := updated.RowsAffected(); err != nil {
|
|
return v, err
|
|
} else if count != 1 {
|
|
return v, ErrTaskConflict
|
|
}
|
|
if err := s.auditTaskWork(ctx, tx, domain, actor, action, reason, result, now); err != nil {
|
|
return v, err
|
|
}
|
|
return result, tx.Commit()
|
|
}
|
|
|
|
type TaskWorkEntry struct {
|
|
TaskWork
|
|
Title string `json:"title"`
|
|
ProjectUUID string `json:"project_uuid"`
|
|
}
|
|
type TaskWorkPage struct {
|
|
Items []TaskWorkEntry `json:"items"`
|
|
Next string `json:"next"`
|
|
}
|
|
type workCursor struct {
|
|
Start int64 `json:"start"`
|
|
UUID string `json:"uuid"`
|
|
}
|
|
|
|
func (s *Store) TaskWorkHistory(ctx context.Context, domain, actor string, start, end time.Time, after string, limit int) (result0 TaskWorkPage, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("organizer repository", resultErr) }()
|
|
out := TaskWorkPage{Items: []TaskWorkEntry{}}
|
|
if err := s.taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
|
return out, err
|
|
}
|
|
if !end.After(start) || end.Sub(start) > 367*24*time.Hour || limit < 1 || limit > 100 {
|
|
return out, ErrTaskInvalid
|
|
}
|
|
query := `SELECT w.uuid,w.task_uuid,w.start_ms,w.end_ms,w.version,w.cancelled_ms,COALESCE(i.title,''),COALESCE(i.project_uuid,'') FROM organizer_work_sessions w LEFT JOIN organizer_items i ON i.uuid=w.task_uuid AND i.domain_uuid=w.domain_uuid AND i.owner_uuid=w.owner_uuid WHERE w.domain_uuid=` + s.sqlParam(1) + ` AND w.owner_uuid=` + s.sqlParam(2) + ` AND w.start_ms<` + s.sqlParam(3) + ` AND (w.end_ms=0 OR w.end_ms>` + s.sqlParam(4) + `)`
|
|
args := []any{domain, actor, end.UnixMilli(), start.UnixMilli()}
|
|
if after != "" {
|
|
if len(after) > 256 {
|
|
return out, ErrTaskInvalid
|
|
}
|
|
raw, err := base64.RawURLEncoding.DecodeString(after)
|
|
var cursor workCursor
|
|
if err != nil || json.Unmarshal(raw, &cursor) != nil || cursor.Start <= 0 {
|
|
return out, ErrTaskInvalid
|
|
}
|
|
if id, err := uuid.Parse(cursor.UUID); err != nil || id.String() != cursor.UUID {
|
|
return out, ErrTaskInvalid
|
|
}
|
|
query += ` AND (w.start_ms,w.uuid)<(` + s.sqlParam(len(args)+1) + `,` + s.sqlParam(len(args)+2) + `)`
|
|
args = append(args, cursor.Start, cursor.UUID)
|
|
}
|
|
query += ` ORDER BY w.start_ms DESC,w.uuid DESC LIMIT ` + s.sqlParam(len(args)+1) + ``
|
|
args = append(args, limit+1)
|
|
rows, err := s.DB.QueryContext(ctx, query, args...)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var v TaskWorkEntry
|
|
if err = rows.Scan(&v.UUID, &v.TaskUUID, &v.StartMS, &v.EndMS, &v.Version, &v.CancelledMS, &v.Title, &v.ProjectUUID); err != nil {
|
|
return out, err
|
|
}
|
|
out.Items = append(out.Items, v)
|
|
}
|
|
if len(out.Items) > limit {
|
|
out.Items = out.Items[:limit]
|
|
last := out.Items[limit-1]
|
|
raw, _ := json.Marshal(workCursor{Start: last.StartMS, UUID: last.UUID})
|
|
out.Next = base64.RawURLEncoding.EncodeToString(raw)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *Store) TaskWorkRevisions(ctx context.Context, domain, actor, id string, before int64) (result0 []TaskWorkRevision, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("organizer repository", resultErr) }()
|
|
out := []TaskWorkRevision{}
|
|
if err := s.taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
|
return out, err
|
|
}
|
|
if before < 0 {
|
|
return out, ErrTaskInvalid
|
|
}
|
|
current, err := scanTaskWork(s.DB.QueryRowContext(ctx, `SELECT `+taskWorkColumns+` FROM organizer_work_sessions WHERE uuid=`+s.sqlParam(1)+` AND domain_uuid=`+s.sqlParam(2)+` AND owner_uuid=`+s.sqlParam(3)+``, id, domain, actor))
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
if before == 0 {
|
|
before = current.Version + 1
|
|
}
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT action,reason,changed_ms,data FROM organizer_work_revisions WHERE work_uuid=`+s.sqlParam(1)+` AND domain_uuid=`+s.sqlParam(2)+` AND owner_uuid=`+s.sqlParam(3)+` AND version<`+s.sqlParam(4)+` ORDER BY version DESC LIMIT 100`, id, domain, actor, before)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var v TaskWorkRevision
|
|
var raw string
|
|
if err = rows.Scan(&v.Action, &v.Reason, &v.ChangedMS, &raw); err != nil {
|
|
return out, err
|
|
}
|
|
if err = json.Unmarshal([]byte(raw), &v.Item); err != nil {
|
|
return out, err
|
|
}
|
|
out = append(out, v)
|
|
}
|
|
if len(out) == 0 && before > current.Version {
|
|
out = append(out, TaskWorkRevision{Item: current, Action: "baseline"})
|
|
}
|
|
return out, rows.Err()
|
|
}
|