---
name: erp-kit-module-5-impl
description: Implement features in modules using TDD. Use after plan review (step 4) to implement database models, commands, and queries with test-driven development.
metadata:
  erp-kit-version: "0.23.0"
---

# TDD Implementation for Module Development

Implement features in modules using Test-Driven Development with per-item agent delegation.

## Version Check

Run `npx erp-kit --version` and compare against this skill's `metadata.erp-kit-version`. If they differ, the local skills are outdated — tell the user to run `pnpm erp-kit update skills` and stop.

## When to Use

- Implementing new domain commands in any module
- Implementing read-only queries in any module
- Adding database models with `@tailor-platform/sdk`
- Creating tests with `@tailor-platform/erp-kit` testing utilities

## Workflow

```
SETUP → GENERATE → DB MODELS (agents) → COMMANDS (agents) → QUERIES (agents) → EXPORT → VERIFY
```

## Step 1: Setup

Define shared context:

- `MODULES_ROOT`: glob `**/modules/*/README.md` and derive the parent directory
- `MODULE_NAME`: from argument or detect from current working directory
- `MODEL_DOCS`: glob `<MODULES_ROOT>/<MODULE_NAME>/docs/models/*.md`
- `COMMAND_DOCS`: glob `<MODULES_ROOT>/<MODULE_NAME>/docs/commands/*.md`
- `QUERY_DOCS`: glob `<MODULES_ROOT>/<MODULE_NAME>/docs/queries/*.md`

Verify at least one doc type exists. If none, stop with: "No docs found for module <MODULE_NAME>. Run steps 1-4 first."

## Step 2: Generate Code from Docs

Run generation first to create `.generated.ts` files (error classes, permission definitions, command/query shells) before implementing any code:

```bash
npx erp-kit module generate code -p <MODULES_ROOT>/<module>
```

Never edit these generated files.

## Step 3: DB Models — Agent per model

**Read [models rules](references/models.md), [field builder API](../erp-kit-shared/references/db-field-api.md), and [db-relations rules](references/db-relations.md) before dispatching.**

For each model doc in `MODEL_DOCS`, launch an Agent:

| Agent per model | Prompt Template                                      | Inputs                             |
| --------------- | ---------------------------------------------------- | ---------------------------------- |
| N               | [references/model-impl.md](references/model-impl.md) | MODULE_NAME, single MODEL_DOC path |

Parallelize across models when they have no cross-dependencies.
For models with foreign key dependencies, implement the referenced model first.

After all model agents complete, run:

```bash
pnpm generate
```

## Step 4: Commands — Agent per command

**Read [commands rules](../erp-kit-shared/references/commands.md) and [testing rules](../erp-kit-shared/references/testing.md) before dispatching.**

For each command doc in `COMMAND_DOCS`, launch an Agent:

| Agent per command | Prompt Template                                          | Inputs                               |
| ----------------- | -------------------------------------------------------- | ------------------------------------ |
| N                 | [references/command-impl.md](references/command-impl.md) | MODULE_NAME, single COMMAND_DOC path |

Each agent implements:

1. Test fixtures in `testing/fixtures.ts` (if not already created)
2. Tests in `command/<name>.test.ts`
3. Implementation in `command/<name>.ts` (export `run` function)

Parallelize across commands when they have no shared fixture dependencies.

## Step 5: Queries — Agent per query

**Read [queries rules](../erp-kit-shared/references/queries.md) before dispatching.**

For each query doc in `QUERY_DOCS`, launch an Agent:

| Agent per query | Prompt Template                                      | Inputs                             |
| --------------- | ---------------------------------------------------- | ---------------------------------- |
| N               | [references/query-impl.md](references/query-impl.md) | MODULE_NAME, single QUERY_DOC path |

Each agent implements the query in `query/<name>.ts`.
Parallelize across all queries.

## Step 6: Export

**Read [exports rules](references/exports.md) for export order.**

Update `index.ts` with all new exports.

## Step 7: Verify

```bash
pnpm generate   # Regenerate types if models changed
pnpm lint       # Check code style
pnpm typecheck  # Verify TypeScript types
pnpm test       # Run all tests
pnpm run module:doc:sync-check # Verify docs compliance
```

## References

- [Module structure](../erp-kit-shared/references/structure.md)
- [Model patterns](references/models.md)
- [Field builder API](../erp-kit-shared/references/db-field-api.md)
- [Database relations](references/db-relations.md)
- [Cross-module dependencies](references/cross-module-dependency.md)
- [Command patterns](../erp-kit-shared/references/commands.md)
- [Query patterns](../erp-kit-shared/references/queries.md)
- [Generated code](references/generated-code.md)
- [Testing patterns](../erp-kit-shared/references/testing.md)
- [Export order](references/exports.md)
