# WorkflowDetectorService

**Kind:** Service

**Source:** [`atloria-monorepo/apps/api/src/documentation/services/workflow-detector.service.ts`](https://github.com/sherkety/atloria/blob/main/atloria-monorepo/apps/api/src/documentation/services/workflow-detector.service.ts#L52)

Service for detecting business workflows from various sources

Detection strategies:
1. Business context files (workflow diagrams, process docs)
2. Navigation patterns (sequences of page visits)
3. Form sequences (multi-step processes)
4. User journey analysis (common paths through the app)

`WorkflowDetectorService` identifies business workflows from application and documentation signals, including process artifacts, navigation behavior, multi-step forms, and user journey paths. It consolidates these detection strategies into a normalized list of `DetectedWorkflow` records for downstream documentation, analysis, or workflow visualization features.

## Methods

| Method | Signature | Returns | Description |
|---|---|---|---|
| `detectWorkflows` | `detectWorkflows(appMap: any, businessContext: {
      marketing: string[];
      workflows: string[];
      brand: string[];
      processes: string[];
    })` | `Promise<DetectedWorkflow[]>` | Detect workflows from multiple sources |

## Where it refuses work

- `WorkflowDetectorService` stops the work with an early return when `!appMap.navigation || !appMap.pages`.
- `WorkflowDetectorService` stops the work with an early return when `!appMap.pages`.
- `WorkflowDetectorService` stops the work with an early return when `matchedPage`.
- `WorkflowDetectorService` stops the work with an early return when `field.type === 'select' || field.type === 'dropdown'`.
- `WorkflowDetectorService` stops the work with an early return when `field.type === 'file'`.
- `WorkflowDetectorService` stops the work with an early return when `lower.includes('sale') || lower.includes('order') || lower.includes('purchase')`.

## Diagram

```mermaid
sequenceDiagram
    participant Caller as Documentation/Analysis Caller
    participant Detector as WorkflowDetectorService
    participant Context as Business Context Sources
    participant Navigation as Navigation Patterns
    participant Forms as Form Sequences
    participant Journeys as User Journey Data

    Caller->>Detector: detectWorkflows()
    Detector->>Context: Analyze workflow diagrams and process docs
    Context-->>Detector: Context-based workflows
    Detector->>Navigation: Analyze page visit sequences
    Navigation-->>Detector: Navigation-based workflows
    Detector->>Forms: Identify multi-step form processes
    Forms-->>Detector: Form-based workflows
    Detector->>Journeys: Analyze common application paths
    Journeys-->>Detector: Journey-based workflows
    Detector->>Detector: Normalize and consolidate detections
    Detector-->>Caller: Promise<DetectedWorkflow[]>
```

## Usage

```ts
import { Injectable } from '@nestjs/common';
import { WorkflowDetectorService } from './workflow-detector.service';

@Injectable()
export class WorkflowDocumentationService {
  constructor(
    private readonly workflowDetectorService: WorkflowDetectorService,
  ) {}

  async generateWorkflowDocumentation() {
    const workflows = await this.workflowDetectorService.detectWorkflows();

    return workflows.map((workflow) => ({
      name: workflow.name,
      description: workflow.description,
      steps: workflow.steps,
    }));
  }
}
```

## AI Coding Instructions

- Keep `detectWorkflows()` as the orchestration entry point; add new detection approaches as isolated strategies rather than embedding unrelated logic in callers.
- Normalize results from every source into the shared `DetectedWorkflow` shape before returning them.
- Treat detected workflows as potentially incomplete or duplicated; deduplicate and validate workflow steps when combining results.
- Preserve asynchronous behavior when integrating file analysis, analytics data, or navigation repositories.
- Register the service in the appropriate NestJS module and inject it through NestJS dependency injection rather than constructing it manually.

## Referenced By

- `DocumentationModule` (MODULE_PROVIDES)
- `DocumentationService` (DEPENDS_ON)
