Move bizdev/freedoc/intent/projectworkflow/squad giant files into ≤800-line use-case siblings without behavior change; short tests green.
103 lines
3.4 KiB
Go
103 lines
3.4 KiB
Go
package bizdev
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"chat-agent-system/internal/docduty"
|
|
"chat-agent-system/internal/docgov"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func resolveTaskDocumentDuties(ctx context.Context, pool *pgxpool.Pool, req TaskRunRequest, instanceDuty InstanceDutySource) (docduty.Binding, string, bool) {
|
|
if instanceDuty != nil && req.ProjectID > 0 && strings.TrimSpace(req.TaskKey) != "" {
|
|
authorID, reviewerID, approverID, dutyPolicy, governed, err := instanceDuty.ResolveGovernedDuty(ctx, req.ProjectID, req.TaskKey)
|
|
if err == nil && governed {
|
|
bound := docduty.Binding{
|
|
Author: docduty.Holder{UserID: authorID},
|
|
Reviewer: docduty.Holder{UserID: reviewerID},
|
|
Approver: docduty.Holder{UserID: approverID},
|
|
}
|
|
if pool != nil {
|
|
if members, creator, loadErr := docduty.LoadProjectMembers(ctx, pool, req.ProjectID); loadErr == nil {
|
|
bound.Author.Name = holderName(members, creator, authorID, req.AuthorName)
|
|
bound.Reviewer.Name = holderName(members, creator, reviewerID, req.ReviewerName)
|
|
bound.Approver.Name = holderName(members, creator, approverID, req.ApproverName)
|
|
}
|
|
}
|
|
if bound.Author.Name == "" {
|
|
bound.Author.Name = strings.TrimSpace(req.AuthorName)
|
|
}
|
|
if bound.Reviewer.Name == "" {
|
|
bound.Reviewer.Name = strings.TrimSpace(req.ReviewerName)
|
|
}
|
|
if bound.Approver.Name == "" {
|
|
bound.Approver.Name = strings.TrimSpace(req.ApproverName)
|
|
}
|
|
return bound, dutyPolicy, true
|
|
}
|
|
}
|
|
|
|
reqDuties := docduty.RequestDuties{
|
|
AuthorID: req.AuthorID, AuthorName: req.AuthorName,
|
|
ReviewerID: req.ReviewerID, ReviewerName: req.ReviewerName,
|
|
ApproverID: req.ApproverID, ApproverName: req.ApproverName,
|
|
}
|
|
if reqDuties.AuthorID == 0 {
|
|
reqDuties.AuthorID = req.UserID
|
|
}
|
|
if pool != nil && req.ProjectID > 0 {
|
|
if bound, err := docduty.ResolveProject(ctx, pool, req.ProjectID, reqDuties); err == nil {
|
|
if bound.Author.UserID == 0 && req.UserID > 0 {
|
|
bound.Author = docduty.Holder{UserID: req.UserID, Name: strings.TrimSpace(req.AuthorName)}
|
|
}
|
|
return bound, "", false
|
|
}
|
|
}
|
|
creator := docduty.Holder{UserID: req.UserID, Name: strings.TrimSpace(req.AuthorName)}
|
|
return docduty.Resolve(nil, creator, reqDuties), "", false
|
|
}
|
|
|
|
func holderName(members []docduty.Member, creator docduty.Holder, userID int64, fallback string) string {
|
|
if userID <= 0 {
|
|
return strings.TrimSpace(fallback)
|
|
}
|
|
if creator.UserID == userID && strings.TrimSpace(creator.Name) != "" {
|
|
return strings.TrimSpace(creator.Name)
|
|
}
|
|
for _, m := range members {
|
|
if m.UserID == userID && strings.TrimSpace(m.Name) != "" {
|
|
return strings.TrimSpace(m.Name)
|
|
}
|
|
}
|
|
return strings.TrimSpace(fallback)
|
|
}
|
|
|
|
func lineageVersion(doc *docgov.DocumentRecord) int {
|
|
if doc != nil && doc.Version > 0 {
|
|
return doc.Version
|
|
}
|
|
return 1
|
|
}
|
|
|
|
func bindingFromDoc(doc *docgov.DocumentRecord) docduty.Binding {
|
|
if doc == nil {
|
|
return docduty.Binding{}
|
|
}
|
|
return docduty.Binding{
|
|
Author: docduty.Holder{UserID: doc.AuthorID, Name: doc.AuthorName},
|
|
Reviewer: docduty.Holder{UserID: doc.ReviewerID, Name: doc.ReviewerName},
|
|
Approver: docduty.Holder{UserID: doc.ApproverID, Name: doc.ApproverName},
|
|
}
|
|
}
|
|
|
|
func writeBindingToDoc(doc *docgov.DocumentRecord, bound docduty.Binding) {
|
|
if doc == nil {
|
|
return
|
|
}
|
|
doc.AuthorID, doc.AuthorName = bound.Author.UserID, bound.Author.Name
|
|
doc.ReviewerID, doc.ReviewerName = bound.Reviewer.UserID, bound.Reviewer.Name
|
|
doc.ApproverID, doc.ApproverName = bound.Approver.UserID, bound.Approver.Name
|
|
}
|