# core/factories/operation.factory.ts

> 35 lines of code and 7 definitions.

Tree: GovLab Patterns
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/patterns#file-patterns-core-factories-operation-factory-ts
Source text: https://banes-lab.com/source/patterns/core/factories/operation.factory.ts.txt

Listed in [core/factories](https://banes-lab.com/api/source/patterns/core/factories.md), after [core/factories/node.factory.ts](https://banes-lab.com/source/patterns/core/factories/node.factory.ts.md) and before [core/factories/pattern.factory.ts](https://banes-lab.com/source/patterns/core/factories/pattern.factory.ts.md).

## Definitions

- `noProposal` (lexical_declaration, line 5)
- `noApply` (lexical_declaration, line 9)
- `proposerFromRules` (lexical_declaration, line 14, exported)
- `action` (lexical_declaration, line 18, exported)
- `createInterventionBridge` (lexical_declaration, line 26, exported)
- `apply` (method_definition, line 32, exported)
- `propose` (method_definition, line 35, exported)

## Contained in

- [core/factories](https://banes-lab.com/anatomy/patterns/folder-patterns-core-factories.md)

## Source

```typescript
import type { BridgeConfig, Intervention, InterventionBridge, InterventionResult } from "#types/operation.types";
import type { Finding } from "#types/finding.types";
import { NO_APPLY_HANDLER } from "#configuration/strings/operation.strings";

const noProposal = function noProposal(): null {
    return null;
};

const noApply = async function noApply(intervention: Intervention): Promise<InterventionResult> {
    await Promise.resolve();
    return { applied: false, detail: NO_APPLY_HANDLER, intervention };
};

export const proposerFromRules = function proposerFromRules(
    rules: ReadonlyMap<string, string>,
): (finding: Finding) => Intervention | null {
    return function propose(finding: Finding): Intervention | null {
        const action = rules.get(finding.name);
        if (action === undefined) {
            return null;
        }
        return { action, field: finding.field, finding: finding.name, rationale: finding.narrative.explanation };
    };
};

export const createInterventionBridge = function createInterventionBridge(
    config: BridgeConfig = {},
): InterventionBridge {
    const propose = config.propose ?? noProposal;
    const apply = config.apply ?? noApply;
    return {
        async apply(interventions: readonly Intervention[]): Promise<InterventionResult[]> {
            return Promise.all(interventions.map(apply));
        },
        propose(findings: readonly Finding[]): Intervention[] {
            return findings.map(propose).filter((value): value is Intervention => value !== null);
        },
    };
};
```
