forked from longtao.wu/droboticflow
249 lines
7.4 KiB
Go
249 lines
7.4 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"net/http"
|
|
"slices"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
type reviewTraceEvent struct {
|
|
ID string `json:"id"`
|
|
Kind string `json:"kind"`
|
|
Actor string `json:"actor"`
|
|
At string `json:"at"`
|
|
Revision int `json:"revision"`
|
|
}
|
|
type reviewTrace struct {
|
|
Author string `json:"author"`
|
|
AuthorID string `json:"authorId"`
|
|
Supplier string `json:"supplier"`
|
|
FirstSubmittedAt string `json:"firstSubmittedAt"`
|
|
Revision int `json:"revision"`
|
|
Reviewer string `json:"reviewer"`
|
|
Rejections *int `json:"rejections"`
|
|
QualityRejections *int `json:"qualityRejections"`
|
|
Events []reviewTraceEvent `json:"events"`
|
|
}
|
|
|
|
// This projection reads existing audit evidence; it never repairs or invents
|
|
// historical records. Missing evidence remains absent, including unknown counts.
|
|
func (s *server) handleReviewTrace(w http.ResponseWriter, r *http.Request) {
|
|
item, ok := s.loadHumanItemForRequest(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
ctx := r.Context()
|
|
events, err := s.store.ListHumanTraceEvents(ctx, item.OrganizationID, item.ID)
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
sort.SliceStable(events, func(i, j int) bool {
|
|
return parseISOTime(events[i].CreatedAt).Before(parseISOTime(events[j].CreatedAt))
|
|
})
|
|
names := map[string]string{"": "", "system": ""}
|
|
name := func(id string) string {
|
|
if n, ok := names[id]; ok {
|
|
return n
|
|
}
|
|
u, e := s.store.GetUser(ctx, id)
|
|
if e != nil {
|
|
err = e
|
|
return ""
|
|
}
|
|
n := ""
|
|
if u != nil && normalizedTraceOrg(u.OrganizationID) == normalizedTraceOrg(item.OrganizationID) {
|
|
n = u.Name
|
|
}
|
|
names[id] = n
|
|
return n
|
|
}
|
|
out := reviewTrace{AuthorID: item.LatestAuthorUserID, Author: name(item.LatestAuthorUserID), Revision: item.CurrentRevision, Events: []reviewTraceEvent{}}
|
|
if item.CurrentStageID == domain.FanyuV58StageQualityReview || item.CurrentStageID == fanyuV5PrePackageAcceptanceStageID {
|
|
out.Reviewer = name(item.AssignedUserID)
|
|
}
|
|
// Preserve shared final-acceptance supplier anonymity.
|
|
visible := *item
|
|
s.anonymizeV58AcceptanceItem(ctx, &visible)
|
|
if visible.SupplierID != "" {
|
|
supplier, e := s.store.GetSupplier(ctx, visible.SupplierID)
|
|
if e != nil {
|
|
renderError(w, 500, e)
|
|
return
|
|
}
|
|
if supplier != nil && normalizedTraceOrg(supplier.OrganizationID) == normalizedTraceOrg(item.OrganizationID) {
|
|
out.Supplier = supplier.Name
|
|
}
|
|
}
|
|
complete := false
|
|
rejected := 0
|
|
first := ""
|
|
acceptance := item.CurrentStageID == fanyuV5PrePackageAcceptanceStageID
|
|
for _, e := range events {
|
|
if e.EventType == "created" {
|
|
complete = true
|
|
}
|
|
kind := ""
|
|
switch e.EventType {
|
|
case "submitted", "combined_submitted":
|
|
kind = "提交审核"
|
|
if first == "" {
|
|
first = e.CreatedAt
|
|
} else {
|
|
kind = "重新提交"
|
|
}
|
|
case "approved", "combined_quality_approved":
|
|
kind = "审核通过"
|
|
if acceptance {
|
|
kind = "验收通过"
|
|
}
|
|
case "rejected", "combined_quality_rejected":
|
|
kind = "审核退回"
|
|
if acceptance {
|
|
kind = "验收退回"
|
|
}
|
|
rejected++
|
|
case "aggregate_acceptance_rejected":
|
|
kind = "验收退回"
|
|
rejected++
|
|
case "claimed":
|
|
kind = "领取任务"
|
|
case "released", "admin_returned_to_pool":
|
|
kind = "返回任务池"
|
|
case "admin_reassigned":
|
|
kind = "调整人员"
|
|
}
|
|
if kind == "" {
|
|
continue
|
|
}
|
|
out.Events = append(out.Events, reviewTraceEvent{ID: e.ID, Kind: kind, Actor: name(e.ActorUserID), At: e.CreatedAt, Revision: traceEventRevision(e)})
|
|
}
|
|
if complete {
|
|
out.FirstSubmittedAt = first
|
|
out.Rejections = &rejected
|
|
}
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
slices.Reverse(out.Events)
|
|
if acceptance {
|
|
if err := s.addAcceptanceSourceTrace(ctx, *item, events, r.URL.Query().Get("sourceItemId"), &out); err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
}
|
|
sort.SliceStable(out.Events, func(i, j int) bool {
|
|
return parseISOTime(out.Events[j].At).Before(parseISOTime(out.Events[i].At))
|
|
})
|
|
renderJSON(w, http.StatusOK, out)
|
|
}
|
|
|
|
func traceEventRevision(event domain.HumanEventRecord) int {
|
|
for _, key := range []string{"revision", "annotationRevision", "reviewedRevision"} {
|
|
if revision := intValue(event.Payload[key]); revision > 0 {
|
|
return revision
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func (s *server) addAcceptanceSourceTrace(ctx context.Context, item domain.HumanItemRecord, events []domain.HumanEventRecord, sourceID string, out *reviewTrace) error {
|
|
entries, _ := normalizeStructuredValue(item.Subject.Payload["items"]).([]any)
|
|
var entry map[string]any
|
|
for _, raw := range entries {
|
|
candidate, _ := raw.(map[string]any)
|
|
if (sourceID != "" && stringValue(candidate["sourceHumanItemId"]) == sourceID) || (sourceID == "" && len(entries) == 1) {
|
|
entry = candidate
|
|
break
|
|
}
|
|
}
|
|
sourceID = stringValue(entry["sourceHumanItemId"])
|
|
taskID := stringValue(entry["taskId"])
|
|
if sourceID == "" || sourceID == item.ID || taskID == "" || stringValue(entry["runId"]) != item.RunID {
|
|
return nil
|
|
}
|
|
batch, err := s.humanBatchForListRead(ctx, item.BatchID)
|
|
if err != nil || batch == nil || strings.TrimSpace(batch.SourceBatchID) == "" {
|
|
return err
|
|
}
|
|
sources, err := s.store.ListHumanItems(ctx, item.OrganizationID, domain.HumanItemQuery{
|
|
IDs: []string{sourceID}, BatchID: batch.SourceBatchID, ProjectID: item.ProjectID, WorkflowID: item.WorkflowID,
|
|
RunID: item.RunID, LegacyTaskID: taskID, IdentityProjection: true, Limit: 1,
|
|
})
|
|
if err != nil || len(sources) == 0 {
|
|
return err
|
|
}
|
|
// The cutoff is saved with the evidence. The separate audit event can fail
|
|
// after the evidence is already persisted.
|
|
cutoff := parseISOTime(stringValue(item.Subject.Payload[acceptanceAssembledAtKey]))
|
|
if cutoff.IsZero() {
|
|
cutoff = parseISOTime(item.CreatedAt)
|
|
for _, event := range events {
|
|
if at := parseISOTime(event.CreatedAt); event.EventType == "created" && at.After(cutoff) {
|
|
cutoff = at
|
|
}
|
|
}
|
|
}
|
|
if cutoff.IsZero() {
|
|
return nil
|
|
}
|
|
sourceEvents, err := s.store.ListHumanTraceEvents(ctx, item.OrganizationID, sourceID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sort.SliceStable(sourceEvents, func(i, j int) bool {
|
|
return parseISOTime(sourceEvents[i].CreatedAt).Before(parseISOTime(sourceEvents[j].CreatedAt))
|
|
})
|
|
complete := false
|
|
rejections := 0
|
|
first := ""
|
|
revision := 0
|
|
for _, event := range sourceEvents {
|
|
if at := parseISOTime(event.CreatedAt); at.IsZero() || at.After(cutoff) {
|
|
continue
|
|
}
|
|
eventRevision := traceEventRevision(event)
|
|
var kind string
|
|
switch event.EventType {
|
|
case "created":
|
|
complete = true
|
|
case "combined_submitted":
|
|
kind = "提交审核"
|
|
if first == "" {
|
|
first = event.CreatedAt
|
|
} else {
|
|
kind = "重新提交"
|
|
}
|
|
case "combined_quality_approved":
|
|
kind = "质检通过"
|
|
revision = eventRevision
|
|
case "combined_quality_rejected":
|
|
kind = "质检退回"
|
|
rejections++
|
|
}
|
|
if kind != "" {
|
|
// Source actors and supplier details stay private in acceptance.
|
|
out.Events = append(out.Events, reviewTraceEvent{ID: event.ID, Kind: kind, At: event.CreatedAt, Revision: eventRevision})
|
|
}
|
|
}
|
|
out.Revision = intValue(entry["annotationRevision"])
|
|
if out.Revision <= 0 {
|
|
out.Revision = revision
|
|
}
|
|
if complete {
|
|
out.FirstSubmittedAt = first
|
|
out.QualityRejections = &rejections
|
|
}
|
|
return nil
|
|
}
|
|
func normalizedTraceOrg(org string) string {
|
|
if org == "" {
|
|
return domain.DefaultOrganizationID
|
|
}
|
|
return org
|
|
}
|