169 lines
5.4 KiB
Go
169 lines
5.4 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// A bounded batch supplies context for children whose parent does not match the
|
|
// day/filter. It never grants access to another owner or Domain.
|
|
func (s *Store) TaskContextParents(ctx context.Context, domain, actor string, items []TaskRecord) ([]TaskRecord, error) {
|
|
if err := s.taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(items) > 100 {
|
|
return nil, ErrTaskInvalid
|
|
}
|
|
ids := map[string]bool{}
|
|
for _, item := range items {
|
|
if item.ParentUUID != "" {
|
|
ids[item.ParentUUID] = true
|
|
}
|
|
}
|
|
parents := []TaskRecord{}
|
|
if len(ids) == 0 {
|
|
return parents, nil
|
|
}
|
|
args := []any{domain, actor}
|
|
marks := []string{}
|
|
for id := range ids {
|
|
marks = append(marks, s.sqlParam(len(args)+1))
|
|
args = append(args, id)
|
|
}
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT data FROM organizer_items WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND kind='task' AND uuid IN (`+strings.Join(marks, ",")+`)`, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var raw string
|
|
var item TaskRecord
|
|
if err = rows.Scan(&raw); err != nil {
|
|
return nil, err
|
|
}
|
|
if err = json.Unmarshal([]byte(raw), &item); err != nil {
|
|
return nil, err
|
|
}
|
|
item.Notes = ""
|
|
item.CalDAV = nil
|
|
parents = append(parents, item)
|
|
}
|
|
return parents, rows.Err()
|
|
}
|
|
|
|
func (s *Store) ensureTaskHierarchySchema() error {
|
|
cols, err := tableColumns(s.DB, "organizer_items")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, col := range []string{"parent_uuid", "start_on", "end_on"} {
|
|
if !cols[col] {
|
|
if _, err = s.DB.Exec(`ALTER TABLE organizer_items ADD COLUMN ` + col + ` TEXT NOT NULL DEFAULT ''`); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
_, err = s.DB.Exec(`CREATE INDEX IF NOT EXISTS organizer_children ON organizer_items(domain_uuid,owner_uuid,parent_uuid,created_at_ms DESC,uuid DESC);
|
|
CREATE INDEX IF NOT EXISTS organizer_schedule ON organizer_items(domain_uuid,owner_uuid,COALESCE(NULLIF(start_on,''),end_on)) WHERE kind='task' AND archived=0`)
|
|
return err
|
|
}
|
|
|
|
func validateTaskHierarchy(item *TaskRecord) error {
|
|
if !validTaskDate(item.StartOn) || !validTaskDate(item.EndOn) {
|
|
return ErrTaskInvalid
|
|
}
|
|
if item.Kind == "project" && (item.ParentUUID != "" || item.StartOn != "" || item.EndOn != "") {
|
|
return ErrTaskInvalid
|
|
}
|
|
if item.ParentUUID != "" {
|
|
u, e := uuid.Parse(item.ParentUUID)
|
|
if e != nil || u.String() != item.ParentUUID || item.ParentUUID == item.UUID {
|
|
return ErrTaskInvalid
|
|
}
|
|
}
|
|
if item.StartOn != "" && item.EndOn != "" {
|
|
a, _ := time.Parse("2006-01-02", item.StartOn)
|
|
b, _ := time.Parse("2006-01-02", item.EndOn)
|
|
if b.Before(a) || b.Sub(a) > 365*24*time.Hour {
|
|
return ErrTaskInvalid
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) validateTaskParentTx(ctx context.Context, tx *sql.Tx, domain, actor string, item, old TaskRecord) error {
|
|
if item.ParentUUID == "" {
|
|
return nil
|
|
}
|
|
if item.ParentUUID == old.ParentUUID {
|
|
return nil
|
|
} // Parent archiving must not disable its children.
|
|
var parent, kind string
|
|
var archived bool
|
|
err := tx.QueryRowContext(ctx, `SELECT parent_uuid,kind,archived FROM organizer_items WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND uuid=`+s.sqlParam(3)+``, domain, actor, item.ParentUUID).Scan(&parent, &kind, &archived)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return ErrTaskInvalid
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if parent != "" || kind != "task" || archived {
|
|
return ErrTaskInvalid
|
|
}
|
|
var children bool
|
|
if err = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM organizer_items WHERE domain_uuid=`+s.sqlParam(1)+` AND owner_uuid=`+s.sqlParam(2)+` AND parent_uuid=`+s.sqlParam(3)+`)`, domain, actor, item.UUID).Scan(&children); err != nil {
|
|
return err
|
|
}
|
|
if children {
|
|
return ErrTaskInvalid
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) TaskSchedule(ctx context.Context, domain, actor, month, project string) ([]TaskRecord, bool, error) {
|
|
if err := s.taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
|
return nil, false, err
|
|
}
|
|
first, err := time.Parse("2006-01", month)
|
|
if err != nil || first.Year() < 1 || first.Format("2006-01") != month {
|
|
return nil, false, ErrTaskInvalid
|
|
}
|
|
q := `SELECT data FROM organizer_items WHERE domain_uuid=` + s.sqlParam(1) + ` AND owner_uuid=` + s.sqlParam(2) + ` AND kind='task' AND archived=0 AND COALESCE(NULLIF(start_on,''),end_on)>=` + s.sqlParam(3) + ` AND COALESCE(NULLIF(start_on,''),end_on)<` + s.sqlParam(4) + ` AND COALESCE(NULLIF(end_on,''),start_on)>=` + s.sqlParam(5) + ``
|
|
args := []any{domain, actor, first.AddDate(0, 0, -365).Format("2006-01-02"), first.AddDate(0, 1, 0).Format("2006-01-02"), first.Format("2006-01-02")}
|
|
if project != "" {
|
|
q += ` AND project_uuid=` + s.sqlParam(len(args)+1) + ``
|
|
args = append(args, project)
|
|
}
|
|
q += ` ORDER BY COALESCE(NULLIF(start_on,''),end_on),uuid LIMIT 201`
|
|
rows, err := s.DB.QueryContext(ctx, q, args...)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
defer rows.Close()
|
|
items := []TaskRecord{}
|
|
for rows.Next() {
|
|
var raw string
|
|
var item TaskRecord
|
|
if err = rows.Scan(&raw); err != nil {
|
|
return nil, false, err
|
|
}
|
|
if err = json.Unmarshal([]byte(raw), &item); err != nil {
|
|
return nil, false, err
|
|
}
|
|
item.Notes = ""
|
|
item.CalDAV = nil
|
|
items = append(items, item)
|
|
}
|
|
more := len(items) > 200
|
|
if more {
|
|
items = items[:200]
|
|
}
|
|
return items, more, rows.Err()
|
|
}
|