| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "time" |
| 10 | "unicode/utf8" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | "gorm.io/gorm" |
| 14 | ) |
| 15 | |
| 16 | // Job threads in Equipo's pages (jobs design, J6). A hand-off's |
| 17 | // Coordinator thread (lane job:<id>) never shows in the person's own |
| 18 | // conversation; these are the reads and the send the "From Base" pages |
| 19 | // use. Only the hand-off's person sees its thread, and only a hand-off |
| 20 | // with a thread (not a legacy one, whose words went to their |
| 21 | // conversation) has one. |
| 22 | |
| 23 | // auditJobSend is a reply the person posted in a job thread, audited by |
| 24 | // the message's id alone. |
| 25 | const auditJobSend = "job.send" |
| 26 | |
| 27 | // JobRow is one of the person's job threads as /jobs lists it: Label is |
| 28 | // its title, Status its job card's ("" before it has one). |
| 29 | type JobRow struct { |
| 30 | ID, Label, Status string |
| 31 | UpdatedAt time.Time |
| 32 | } |
| 33 | |
| 34 | // Jobs are the person's job threads, newest first. A legacy hand-off has |
| 35 | // none, so is not one of them. |
| 36 | func (s *Store) Jobs(ctx context.Context, person string) (out []JobRow, err error) { |
| 37 | err = s.read(ctx, func(tx *gorm.DB) error { |
| 38 | if err := s.authority.CurrentPerson(ctx, tx); err != nil { |
| 39 | return err |
| 40 | } |
| 41 | var rows []struct { |
| 42 | ID, Title, Text, Status string |
| 43 | UpdatedAt time.Time |
| 44 | } |
| 45 | if err := tx.Table("chat_handoffs AS h").Select("h.id, coalesce(h.title, '') AS title, coalesce(m.text, '') AS text, coalesce(c.result, '') AS status, h.updated_at").Joins("LEFT JOIN chat_messages m ON m.id = h.message_id").Joins("LEFT JOIN chat_cards c ON c.handoff_id = h.id AND c.person = h.person AND c.kind = ? AND c.lane = ?", wire.CardJob, wire.LaneAssistant).Where("h.person = ? AND h.lane = 'job:' || h.id", person).Order("h.created_at DESC, h.id DESC").Scan(&rows).Error; err != nil { |
| 46 | return err |
| 47 | } |
| 48 | out = make([]JobRow, 0, len(rows)) |
| 49 | for _, r := range rows { |
| 50 | // A hand-off made before titles is named as an untitled one. |
| 51 | if r.Title == "" { |
| 52 | r.Title = fallbackTitle(r.Text) |
| 53 | } |
| 54 | out = append(out, JobRow{ID: r.ID, Label: r.Title, Status: r.Status, UpdatedAt: r.UpdatedAt}) |
| 55 | } |
| 56 | return nil |
| 57 | }) |
| 58 | return out, err |
| 59 | } |
| 60 | |
| 61 | // job is the person's hand-off id with a thread of its own: ErrNotFound |
| 62 | // for another person's, an unknown or a legacy one. |
| 63 | func job(tx *gorm.DB, person, id string) (handoffRecord, error) { |
| 64 | var h handoffRecord |
| 65 | if _, ok := wire.JobOf(wire.JobLane(id)); !ok { |
| 66 | return h, ErrNotFound |
| 67 | } |
| 68 | if err := tx.Where("id = ? AND person = ? AND lane = ?", id, person, wire.JobLane(id)).Limit(1).Find(&h).Error; err != nil { |
| 69 | return h, err |
| 70 | } |
| 71 | if h.ID == "" { |
| 72 | return h, ErrNotFound |
| 73 | } |
| 74 | return h, nil |
| 75 | } |
| 76 | |
| 77 | // JobThread reads one of the person's job threads as their conversation |
| 78 | // reads: its newest messages, its turns, the runner, its cards and the |
| 79 | // tasks they and the hand-off name. |
| 80 | func (s *Store) JobThread(ctx context.Context, person, id string) (out wire.Snapshot, err error) { |
| 81 | err = s.read(ctx, func(tx *gorm.DB) error { |
| 82 | if err := s.authority.CurrentPerson(ctx, tx); err != nil { |
| 83 | return err |
| 84 | } |
| 85 | h, e := job(tx, person, id) |
| 86 | if e != nil { |
| 87 | return e |
| 88 | } |
| 89 | if out.Me, e = s.authority.Me(tx, person); e != nil { |
| 90 | return e |
| 91 | } |
| 92 | if out.Cursor, e = maxEvent(tx); e != nil { |
| 93 | return e |
| 94 | } |
| 95 | if out.Messages, out.Turns, e = laneTail(tx, h.Lane, person); e != nil { |
| 96 | return e |
| 97 | } |
| 98 | if out.Runner, e = s.runnerView(tx, person); e != nil { |
| 99 | return e |
| 100 | } |
| 101 | t := &txn{s: s, db: tx} |
| 102 | rows, e := t.laneCards(person, h.Lane) |
| 103 | if e != nil { |
| 104 | return e |
| 105 | } |
| 106 | names := map[string]bool{} |
| 107 | out.Cards = make([]wire.Card, 0, len(rows)) |
| 108 | for i := len(rows) - 1; i >= 0; i-- { |
| 109 | v, e := t.cardView(rows[i]) |
| 110 | if e != nil { |
| 111 | return e |
| 112 | } |
| 113 | out.Cards = append(out.Cards, v) |
| 114 | if v.TaskID != "" { |
| 115 | names[v.TaskID] = true |
| 116 | } |
| 117 | } |
| 118 | var followed []string |
| 119 | if e = tx.Model(&handoffTaskRecord{}).Where("handoff_id = ?", h.ID).Pluck("task_id", &followed).Error; e != nil { |
| 120 | return e |
| 121 | } |
| 122 | for _, k := range followed { |
| 123 | names[k] = true |
| 124 | } |
| 125 | out.Tasks, e = t.tasksNamed(names) |
| 126 | return e |
| 127 | }) |
| 128 | return out, err |
| 129 | } |
| 130 | |
| 131 | // SendJob records a person message in one of their job threads: the |
| 132 | // roster checked in the write's transaction, a primary able to carry the |
| 133 | // thread (protocol 22), the message into the thread's lane, audited by |
| 134 | // its id. The thread takes words only; a message already sent answers as |
| 135 | // it did. |
| 136 | func (s *Store) SendJob(ctx context.Context, person, id string, in wire.SendMessage) (out wire.Sent, created bool, err error) { |
| 137 | if in.Text = strings.TrimSpace(in.Text); in.Text == "" || len(in.Text) > wire.MaxMessageBytes || !utf8.ValidString(in.Text) || strings.ContainsRune(in.Text, 0) || len(in.Attachments) > 0 || in.ProjectID != "" || !validKey(in.IdempotencyKey) { |
| 138 | return out, false, ErrInvalid |
| 139 | } |
| 140 | err = s.tx(ctx, func(t *txn) error { |
| 141 | if err := s.authority.CurrentPerson(ctx, t.db); err != nil { |
| 142 | return err |
| 143 | } |
| 144 | h, err := job(t.db, person, id) |
| 145 | if err != nil { |
| 146 | return err |
| 147 | } |
| 148 | var sent int64 |
| 149 | if err := t.db.Model(&messageRecord{}).Where("person = ? AND idempotency_key = ? AND lane = ?", person, in.IdempotencyKey, h.Lane).Count(&sent).Error; err != nil { |
| 150 | return err |
| 151 | } |
| 152 | if sent == 0 { |
| 153 | if err := t.jobRunner(person); err != nil { |
| 154 | return err |
| 155 | } |
| 156 | } |
| 157 | if out, created, err = t.composerSend(h.Lane, person, in); err != nil || !created { |
| 158 | return err |
| 159 | } |
| 160 | return t.s.authority.Audit(t.db, person, auditJobSend, out.Message.ID) |
| 161 | }) |
| 162 | return out, created, err |
| 163 | } |
| 164 | |
| 165 | // The job card (jobs design §3; D1 to D5). Each hand-off shows in the |
| 166 | // Assistant lane as one card of kind job, found by its handoff_id: its |
| 167 | // question the job's title, its result a short status and its detail one |
| 168 | // sentence more; its links what the person can try or open now (a |
| 169 | // preview, the published site) and, for a job with a thread, that thread |
| 170 | // in Equipo. projectJob rewrites it as the work moves; nobody answers it. |
| 171 | // It follows the work and starts nothing, so it runs under housekeeping |
| 172 | // too. |
| 173 | |
| 174 | // jobSteps are a working job's status keys, slowest first. |
| 175 | var jobSteps = []string{"starting", "planning", "building", "checking", "finishing"} |
| 176 | |
| 177 | // jobStep is the index in jobSteps of a task at stage (D4). A stage this |
| 178 | // instance does not know is building; a task that ended (done while its |
| 179 | // site goes live, or cancelled beside work that goes on) is finishing. |
| 180 | func jobStep(stage string) int { |
| 181 | switch stage { |
| 182 | case wire.StageNoted: |
| 183 | return 0 |
| 184 | case wire.StageInvestigated: |
| 185 | return 1 |
| 186 | case wire.StageInReview: |
| 187 | return 3 |
| 188 | case wire.StageReadyToMerge, wire.StageMerging, wire.StageDone, wire.StageCancelled: |
| 189 | return 4 |
| 190 | } |
| 191 | return 2 |
| 192 | } |
| 193 | |
| 194 | // jobView is the hand-off's job card as it stands: its state (D5, first |
| 195 | // match wins), status, detail and links, never nil. A cancelled |
| 196 | // hand-off is stopped; a go-ahead or Publish open in front of the person |
| 197 | // is ready, with its preview while it is up; work that ended with |
| 198 | // something done is done, with the published site; then, once no |
| 199 | // team_update turn of the job is queued or running (ruling P5j), an |
| 200 | // open question is asking and a stopped job (stopReason) stopped; else |
| 201 | // working, by the slowest followed task's stage. A job with a thread |
| 202 | // links to it last. |
| 203 | func (t *txn) jobView(h handoffRecord) (state, result, detail, carryOn string, links []wire.CardLink, err error) { |
| 204 | state, key, first, err := t.jobState(h) |
| 205 | if err != nil { |
| 206 | return "", "", "", "", nil, err |
| 207 | } |
| 208 | links = []wire.CardLink{} |
| 209 | if first != nil { |
| 210 | links = append(links, *first) |
| 211 | } |
| 212 | if _, ok := wire.JobOf(h.Lane); ok && t.s.authority.Origin != "" { |
| 213 | links = append(links, wire.CardLink{Label: t.copy("assistant.job.open"), URL: strings.TrimSuffix(t.s.authority.Origin, "/") + "/jobs/" + h.ID, Rel: wire.LinkEquipo}) |
| 214 | } |
| 215 | if h.AbilityHeldAt != nil && h.State == handoffOpen && state != wire.JobDone { |
| 216 | // Held because building was off for its person (§6.3). |
| 217 | result, detail, carryOn, err := t.heldView(h) |
| 218 | return wire.JobStopped, result, detail, carryOn, links, err |
| 219 | } |
| 220 | if state == wire.JobWorking { |
| 221 | // Working, unless all its open work is paused (job_paused.go). |
| 222 | p, err := t.jobPaused(h) |
| 223 | if err != nil { |
| 224 | return "", "", "", "", nil, err |
| 225 | } |
| 226 | if p != nil { |
| 227 | result, detail := t.pausedWords(h.Person, p) |
| 228 | return wire.JobPaused, result, detail, p.conn, links, nil |
| 229 | } |
| 230 | return state, t.copy("assistant.job.status." + key), t.copy("assistant.job.detail." + key), "", links, nil |
| 231 | } |
| 232 | detail = t.copy("assistant.job.detail." + state) |
| 233 | if state == wire.JobStopped && key != "" { |
| 234 | detail = t.copy("assistant.job.detail." + key) |
| 235 | } |
| 236 | if state == wire.JobReady { |
| 237 | // Ready, but its person can't put it live: says so (§6.4). |
| 238 | sw, err := t.switches(h.Person) |
| 239 | if err != nil { |
| 240 | return "", "", "", "", nil, err |
| 241 | } |
| 242 | if !sw.Can(wire.AbilityGoLive) && sw.Ask() != "" { |
| 243 | detail = fmt.Sprintf(t.copy("assistant.job.detail.go_live_off"), sw.Ask()) |
| 244 | } |
| 245 | } |
| 246 | return state, t.copy("assistant.job." + state), detail, "", links, nil |
| 247 | } |
| 248 | |
| 249 | // jobState is jobView's state, a working job's status key (a stopped |
| 250 | // job's detail key when it has one of its own) and the link the state |
| 251 | // offers first, if any. |
| 252 | func (t *txn) jobState(h handoffRecord) (state, key string, first *wire.CardLink, err error) { |
| 253 | if h.State == handoffCancelled { |
| 254 | return wire.JobStopped, "", nil, nil |
| 255 | } |
| 256 | settled, err := t.handoffSettled(h) |
| 257 | if err != nil { |
| 258 | return "", "", nil, err |
| 259 | } |
| 260 | // The job's note's stop (jobStop): a blocker only the person clears |
| 261 | // stops it before its work settles too. |
| 262 | reason, cause, ended, err := t.jobStop(h, settled) |
| 263 | if err != nil { |
| 264 | return "", "", nil, err |
| 265 | } |
| 266 | // Any go-ahead or Publish open in front of the person is ready, a |
| 267 | // Publish asked again because going live failed too (ruling R19). |
| 268 | src, ready, err := t.readyCard(h) |
| 269 | if err != nil { |
| 270 | return "", "", nil, err |
| 271 | } |
| 272 | if ready { |
| 273 | preview, err := t.taskPreview(src.TaskID) |
| 274 | if err != nil || preview == "" { |
| 275 | return wire.JobReady, "", nil, err |
| 276 | } |
| 277 | return wire.JobReady, "", &wire.CardLink{Label: t.copy("assistant.outcome.preview"), URL: preview}, nil |
| 278 | } |
| 279 | if ended { |
| 280 | site, err := t.jobSite(h) |
| 281 | if err != nil || site == "" { |
| 282 | return wire.JobDone, "", nil, err |
| 283 | } |
| 284 | return wire.JobDone, "", &wire.CardLink{Label: t.copy("assistant.outcome.publish_site"), URL: site}, nil |
| 285 | } |
| 286 | busy, err := t.jobUpdating(h) |
| 287 | if err != nil { |
| 288 | return "", "", nil, err |
| 289 | } |
| 290 | if !busy { |
| 291 | _, asking, err := t.openQuestion(h) |
| 292 | if err != nil || asking { |
| 293 | return wire.JobAsking, "", nil, err |
| 294 | } |
| 295 | if reason != "" && cause == causeNoAccess { |
| 296 | // Going live refused for want of access: its own sentence. |
| 297 | return wire.JobStopped, causeNoAccess, nil, nil |
| 298 | } |
| 299 | if reason != "" { |
| 300 | return wire.JobStopped, "", nil, nil |
| 301 | } |
| 302 | } |
| 303 | tasks, err := t.handoffTasks(h) |
| 304 | if err != nil { |
| 305 | return "", "", nil, err |
| 306 | } |
| 307 | step := 0 |
| 308 | for i, k := range tasks { |
| 309 | if s := jobStep(k.Stage); i == 0 || s < step { |
| 310 | step = s |
| 311 | } |
| 312 | } |
| 313 | return wire.JobWorking, jobSteps[step], nil, nil |
| 314 | } |
| 315 | |
| 316 | // jobUpdating: a team_update turn about the hand-off is queued or |
| 317 | // running: the Assistant has yet to act on what it was told. |
| 318 | func (t *txn) jobUpdating(h handoffRecord) (bool, error) { |
| 319 | var n int64 |
| 320 | err := t.db.Model(&turnRecord{}).Where("id IN (SELECT turn_id FROM chat_handoff_turns WHERE handoff_id = ?) AND person = ? AND lane = ? AND kind = ? AND state IN ?", h.ID, h.Person, wire.LaneAssistant, wire.TurnTeamUpdate, wire.InFlight).Count(&n).Error |
| 321 | return n > 0, err |
| 322 | } |
| 323 | |
| 324 | // jobSite is the address of a site the hand-off's work published ("" for |
| 325 | // none). |
| 326 | func (t *txn) jobSite(h handoffRecord) (string, error) { |
| 327 | tasks, err := t.handoffTasks(h) |
| 328 | if err != nil { |
| 329 | return "", err |
| 330 | } |
| 331 | for _, k := range tasks { |
| 332 | steps, err := t.steps(k.ID) |
| 333 | if err != nil { |
| 334 | return "", err |
| 335 | } |
| 336 | site, err := t.siteView(k, steps) |
| 337 | if err != nil { |
| 338 | return "", err |
| 339 | } |
| 340 | if site != nil && site.State == wire.SitePublished && site.URL != "" { |
| 341 | return site.URL, nil |
| 342 | } |
| 343 | } |
| 344 | return "", nil |
| 345 | } |
| 346 | |
| 347 | // jobCard is the hand-off's job card (ok false while it has none). |
| 348 | func (t *txn) jobCard(h handoffRecord) (cardRecord, bool, error) { |
| 349 | var c cardRecord |
| 350 | err := t.db.Where("handoff_id = ? AND person = ? AND kind = ? AND lane = ?", h.ID, h.Person, wire.CardJob, wire.LaneAssistant).Limit(1).Find(&c).Error |
| 351 | return c, err == nil && c.ID != "", err |
| 352 | } |
| 353 | |
| 354 | // projectJob brings the hand-off's job card up to date: made on its |
| 355 | // first call, of the Assistant turn that handed the work over, named for |
| 356 | // the job; afterwards rewritten, its revision moved and its event sent |
| 357 | // only when its state, status, detail or links changed. |
| 358 | func (t *txn) projectJob(h handoffRecord) error { |
| 359 | c, ok, err := t.jobCard(h) |
| 360 | if err != nil { |
| 361 | return err |
| 362 | } |
| 363 | state, result, detail, carryOn, links, err := t.jobView(h) |
| 364 | if err != nil { |
| 365 | return err |
| 366 | } |
| 367 | raw, err := json.Marshal(links) |
| 368 | if err != nil { |
| 369 | return err |
| 370 | } |
| 371 | now := t.now() |
| 372 | if !ok { |
| 373 | title, _, err := t.jobTitle(h) |
| 374 | if err != nil { |
| 375 | return err |
| 376 | } |
| 377 | c = cardRecord{ID: newID("c_"), Person: h.Person, Kind: wire.CardJob, State: state, TurnID: h.AssistantTurn, Question: title, Options: "[]", Links: string(raw), Result: result, Detail: detail, CarryOn: carryOn != "", CarryOnConnection: connectionOf(carryOn), HandoffID: h.ID, Revision: 1, CreatedAt: now, UpdatedAt: now, Lane: wire.LaneAssistant} |
| 378 | if err := t.db.Create(&c).Error; err != nil { |
| 379 | return err |
| 380 | } |
| 381 | return t.cardEvent(c) |
| 382 | } |
| 383 | if c.State == state && c.Result == result && c.Detail == detail && c.CarryOn == (carryOn != "") && c.CarryOnConnection == connectionOf(carryOn) && c.Links == string(raw) { |
| 384 | return nil |
| 385 | } |
| 386 | c.State, c.Result, c.Detail, c.CarryOn, c.CarryOnConnection, c.Links = state, result, detail, carryOn != "", connectionOf(carryOn), string(raw) |
| 387 | c.Revision++ |
| 388 | c.UpdatedAt = now |
| 389 | if err := t.db.Model(&cardRecord{}).Where("id = ?", c.ID).Select("state", "result", "detail", "carry_on", "carry_on_connection", "links", "revision", "updated_at").Updates(&c).Error; err != nil { |
| 390 | return err |
| 391 | } |
| 392 | return t.cardEvent(c) |
| 393 | } |
| 394 | |
| 395 | // updateEnded projects the job a team_update turn that ended was about: |
| 396 | // what the Assistant was told may now show (asking, stopped). |
| 397 | func (t *txn) updateEnded(turnID string) error { |
| 398 | h, ok, err := t.handoffOf(turnID, "") |
| 399 | if err != nil || !ok { |
| 400 | return err |
| 401 | } |
| 402 | return t.projectJob(h) |
| 403 | } |
| 404 | |
| 405 | // jobCardView fills an Assistant job card: its status, detail and links as |
| 406 | // stored, and when its hand-off was made. A ready job's preview is read |
| 407 | // as it is now (jobPreview): one that expired or went since the card was |
| 408 | // written is offered no more, before the minute's tick moves the card |
| 409 | // (expirePreviews). |
| 410 | func (t *txn) jobCardView(v *wire.AssistantCard, c cardRecord) error { |
| 411 | v.Result, v.Detail, v.CarryOn = c.Result, c.Detail, c.CarryOn && (c.State == wire.JobPaused || c.State == wire.JobStopped) |
| 412 | if c.Links != "" { |
| 413 | if err := json.Unmarshal([]byte(c.Links), &v.Links); err != nil { |
| 414 | return err |
| 415 | } |
| 416 | } |
| 417 | if v.Links == nil { |
| 418 | v.Links = []wire.CardLink{} |
| 419 | } |
| 420 | var h handoffRecord |
| 421 | if err := t.db.Where("id = ? AND person = ?", c.HandoffID, c.Person).Limit(1).Find(&h).Error; err != nil || h.ID == "" { |
| 422 | return err |
| 423 | } |
| 424 | at := h.CreatedAt |
| 425 | v.StartedAt = &at |
| 426 | if c.State != wire.JobReady || !slices.ContainsFunc(v.Links, isPreview) { |
| 427 | return nil |
| 428 | } |
| 429 | preview, err := t.jobPreview(h) |
| 430 | v.Links = slices.DeleteFunc(v.Links, func(l wire.CardLink) bool { return isPreview(l) && l.URL != preview }) |
| 431 | return err |
| 432 | } |
| 433 | |
| 434 | // isPreview: a ready job card's link to its preview (its only link with |
| 435 | // no rel). |
| 436 | func isPreview(l wire.CardLink) bool { return l.Rel == "" } |
| 437 | |
| 438 | // jobPreview is the preview of the go-ahead or Publish the hand-off's |
| 439 | // job waits on while it is up (taskPreview), "" for none. |
| 440 | func (t *txn) jobPreview(h handoffRecord) (string, error) { |
| 441 | src, ready, err := t.readyCard(h) |
| 442 | if err != nil || !ready { |
| 443 | return "", err |
| 444 | } |
| 445 | return t.taskPreview(src.TaskID) |
| 446 | } |
| 447 | |
| 448 | // expirePreviews moves each ready job card whose preview expired or went |
| 449 | // with nothing else changing (the minute's tick): projected afresh, and |
| 450 | // the go-ahead or Publish open under it moved too, so a client that only |
| 451 | // follows the stream offers the preview no more. |
| 452 | func (t *txn) expirePreviews() error { |
| 453 | var cards []cardRecord |
| 454 | if err := t.db.Where("lane = ? AND kind = ? AND state = ?", wire.LaneAssistant, wire.CardJob, wire.JobReady).Order("created_at, id").Find(&cards).Error; err != nil { |
| 455 | return err |
| 456 | } |
| 457 | for _, c := range cards { |
| 458 | var links []wire.CardLink |
| 459 | if err := json.Unmarshal([]byte(c.Links), &links); err != nil { |
| 460 | return err |
| 461 | } |
| 462 | i := slices.IndexFunc(links, isPreview) |
| 463 | if i < 0 { |
| 464 | continue |
| 465 | } |
| 466 | var h handoffRecord |
| 467 | if err := t.db.Where("id = ? AND person = ?", c.HandoffID, c.Person).Limit(1).Find(&h).Error; err != nil { |
| 468 | return err |
| 469 | } |
| 470 | if h.ID == "" { |
| 471 | continue |
| 472 | } |
| 473 | preview, err := t.jobPreview(h) |
| 474 | if err != nil { |
| 475 | return err |
| 476 | } |
| 477 | if preview == links[i].URL { |
| 478 | continue |
| 479 | } |
| 480 | if err := t.projectJob(h); err != nil { |
| 481 | return err |
| 482 | } |
| 483 | var mirrors []cardRecord |
| 484 | if err := t.db.Where("handoff_id = ? AND person = ? AND lane = ? AND state = ? AND coalesce(source_card, '') <> ''", h.ID, h.Person, wire.LaneAssistant, wire.CardOpen).Find(&mirrors).Error; err != nil { |
| 485 | return err |
| 486 | } |
| 487 | for _, m := range mirrors { |
| 488 | m.Revision++ |
| 489 | m.UpdatedAt = t.now() |
| 490 | if err := t.db.Model(&cardRecord{}).Where("id = ?", m.ID).Select("revision", "updated_at").Updates(&m).Error; err != nil { |
| 491 | return err |
| 492 | } |
| 493 | if err := t.cardEvent(m); err != nil { |
| 494 | return err |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | return nil |
| 499 | } |
| 500 | |
| 501 | // jobOf is the id of the job card of a go-ahead or Publish card's |
| 502 | // hand-off ("" for none). |
| 503 | func (t *txn) jobOf(c cardRecord) (string, error) { |
| 504 | if c.HandoffID == "" { |
| 505 | return "", nil |
| 506 | } |
| 507 | var ids []string |
| 508 | err := t.db.Model(&cardRecord{}).Where("handoff_id = ? AND person = ? AND kind = ? AND lane = ?", c.HandoffID, c.Person, wire.CardJob, wire.LaneAssistant).Limit(1).Pluck("id", &ids).Error |
| 509 | if err != nil || len(ids) == 0 { |
| 510 | return "", err |
| 511 | } |
| 512 | return ids[0], nil |
| 513 | } |
| 514 | |
| 515 | // connectionOf is a job card's carry_on_connection for jobView's carryOn: |
| 516 | // none for a held job's, whose Carry on now resumes the hold. |
| 517 | func connectionOf(carryOn string) string { |
| 518 | if carryOn == heldCarryOn { |
| 519 | return "" |
| 520 | } |
| 521 | return carryOn |
| 522 | } |
| 523 | |