forked from longtao.wu/droboticflow
59 lines
2.1 KiB
Go
59 lines
2.1 KiB
Go
package api
|
|
|
|
import (
|
|
"droboticflow/internal/domain"
|
|
"time"
|
|
)
|
|
|
|
// A failed slice generation is editable, but its confirmed input remains frozen.
|
|
// Running jobs and failures of other processing kinds are not draft-edit lanes.
|
|
func failedSliceEditingAvailable(item *domain.HumanItemRecord) bool {
|
|
if stringValue(mapValue(item.Subject.Payload["sliceWorkflow"])["status"]) != "failed" {
|
|
return false
|
|
}
|
|
job := item.ProcessingJob
|
|
return job == nil || (job.Kind == "slice" && job.Status == "failed")
|
|
}
|
|
|
|
// Give canonicalization the same owned saved draft that the editor restores.
|
|
// This is an isolated validation copy, never a change to generation state.
|
|
func sliceEditBaseline(original *domain.HumanItemRecord) (*domain.HumanItemRecord, error) {
|
|
item := *original
|
|
item.Subject.Payload = mapValue(original.Subject.Payload)
|
|
if failedSliceEditingAvailable(original) {
|
|
// A draft saved here must remain orderable against the frozen input on reload.
|
|
frozen := mapValue(original.Subject.Payload["annotationWorkspace"])
|
|
if len(frozen) > 0 {
|
|
stamp := stringValue(frozen["confirmedAt"])
|
|
if stamp == "" {
|
|
stamp = stringValue(frozen["savedAt"])
|
|
}
|
|
if _, err := time.Parse(time.RFC3339Nano, stamp); err != nil {
|
|
return nil, errSliceSnapshotConflict
|
|
}
|
|
}
|
|
selected, _, err := retainedSliceRecoveryInput(original, original.Version)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
item.Subject.Payload["annotationWorkspace"] = mapValue(selected.RetryWorkspace)
|
|
delete(item.Subject.Payload, "annotationDraft")
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func storeSliceEditWorkspace(item, original *domain.HumanItemRecord, workspace map[string]any) {
|
|
if failedSliceEditingAvailable(original) {
|
|
workspace["savedVersion"] = original.Version + 1
|
|
item.Subject.Payload["annotationDraft"] = workspace
|
|
if frozen, exists := original.Subject.Payload["annotationWorkspace"]; exists {
|
|
item.Subject.Payload["annotationWorkspace"] = frozen
|
|
} else {
|
|
delete(item.Subject.Payload, "annotationWorkspace")
|
|
}
|
|
return
|
|
}
|
|
item.Subject.Payload["annotationWorkspace"] = workspace
|
|
delete(item.Subject.Payload, "annotationDraft")
|
|
}
|