Register workflow approval callback on server start and expose GET /projects/{id}/notifications.
Co-authored-by: Cursor <cursoragent@cursor.com>
46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
|
|
"chat-agent-system/internal/docgov"
|
|
"chat-agent-system/internal/notification"
|
|
)
|
|
|
|
func (s *Server) registerWorkflowApprovalHook() {
|
|
if s == nil || s.svc == nil || s.svc.DocGov() == nil || s.instSvc == nil || !s.instSvc.Available() {
|
|
return
|
|
}
|
|
s.svc.DocGov().AddOnApprovedHook(s.onWorkflowDocumentApproved)
|
|
}
|
|
|
|
func (s *Server) onWorkflowDocumentApproved(ctx context.Context, doc *docgov.DocumentRecord) {
|
|
if doc == nil || doc.TaskKey == "" || doc.ProjectID <= 0 || s.instSvc == nil {
|
|
return
|
|
}
|
|
|
|
result, err := s.instSvc.HandleDocumentApproved(ctx, doc.ProjectID, doc.TaskKey)
|
|
if err != nil || result == nil || !result.StageReady {
|
|
return
|
|
}
|
|
if s.notifySvc == nil || !s.notifySvc.Available() {
|
|
return
|
|
}
|
|
|
|
project, err := s.pwSvc.GetProject(ctx, doc.ProjectID, 0)
|
|
if err != nil || project == nil || project.CreatorUserID <= 0 {
|
|
return
|
|
}
|
|
|
|
_, _ = s.notifySvc.EmitStageReadyToAdvance(ctx, project.CreatorUserID, notification.StageReadyPayload{
|
|
ProjectID: doc.ProjectID,
|
|
WorkflowInstanceID: result.InstanceID,
|
|
StageInstanceID: result.StageInstanceID,
|
|
StageKey: result.StageKey,
|
|
StageName: result.StageName,
|
|
ConfigRevision: result.ConfigRevision,
|
|
RequiredDocCount: result.RequiredDocCount,
|
|
ApprovedDocCount: result.ApprovedDocCount,
|
|
})
|
|
}
|