Files
archie/internal/webui/workflow_enablement.go
sam 2fb8c09a00
Quality / check (push) Successful in 4m23s
deploy / gate (push) Successful in 4m37s
deploy / archied-image (push) Successful in 2m51s
deploy / agent-image (push) Successful in 11s
deploy / dist-zip (push) Skipped
refactor(webui): extract the workflows module (#1778)
2026-11-03 08:39:40 +11:00

89 lines
2.8 KiB
Go

package webui
import (
"context"
"encoding/json"
"errors"
"net/http"
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
)
const workflowEnablementKind = "workflow-enablement"
// workflowEnablement returns which workflows each org has disabled, and the
// resource version a replacement must name.
func (a *workflowsAPI) workflowEnablement(ctx context.Context) (task.WorkflowEnablement, int64, error) {
if a.controlPlane == nil {
return task.WorkflowEnablement{}, 0, nil
}
response, err := a.controlPlane.Query(ctx, &controlpb.QueryRequest{Kind: workflowEnablementKind})
if err != nil {
return task.WorkflowEnablement{}, 0, err
}
if response.Resource == nil {
return task.WorkflowEnablement{}, 0, errors.New("workflow enablement missing")
}
var enablement task.WorkflowEnablement
if err := json.Unmarshal(response.Resource.ValueJson, &enablement); err != nil {
return task.WorkflowEnablement{}, 0, err
}
return enablement, response.Resource.Version, nil
}
type workflowEnabledRequest struct {
Enabled bool `json:"enabled"`
}
// handleWorkflowEnabled enables or disables a workflow for the caller's org
// (org.OrgFromContext).
func (a *workflowsAPI) handleWorkflowEnabled(w http.ResponseWriter, r *http.Request) {
if !a.authorize(w, r) {
return
}
if a.controlPlane == nil {
http.Error(w, "control plane unavailable", http.StatusServiceUnavailable)
return
}
request, ok := decodeBody[workflowEnabledRequest](w, r)
if !ok {
return
}
id := r.PathValue("id")
found, err := a.hasWorkflowDefinition(r.Context(), id)
if err != nil {
http.Error(w, "workflow definitions unavailable", http.StatusServiceUnavailable)
return
}
if !found {
http.Error(w, "workflow not found", http.StatusNotFound)
return
}
enablement, version, err := a.workflowEnablement(r.Context())
if err != nil {
http.Error(w, "workflow enablement unavailable", http.StatusServiceUnavailable)
return
}
value, err := json.Marshal(enablement.SetEnabled(org.OrgFromContext(r.Context()), id, request.Enabled))
if err != nil {
http.Error(w, "encode workflow enablement", http.StatusInternalServerError)
return
}
audit, err := webAudit(r.Context())
if err != nil {
http.Error(w, "cannot create request ID", http.StatusInternalServerError)
return
}
if _, err := a.controlPlane.Command(r.Context(), &controlpb.CommandRequest{
Kind: workflowEnablementKind, Command: "replace", ValueJson: value,
ExpectedVersion: version, Actor: string(audit.ActorID),
Source: audit.Source, RequestId: audit.RequestID,
}); err != nil {
writeControlPlaneError(w, err)
return
}
writeJSON(w, map[string]any{"id": id, "enabled": request.Enabled})
}