refactor(blog): factory-style use cases + per-use-case controllers + getArticleBySlug

- Use cases (create-article, get-articles, get-article-by-slug NEW) → factory functions
- Controllers split: articles.controller.ts → 3 single-responsibility files
- DI module wires factories with .toDynamicValue()
- tRPC router resolves controllers via container

Refactor log: §2, §3, §4.1, §4.2, §5.1
Spec: §6.2

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-06 00:08:15 +02:00
parent 780d5cb83b
commit 700d311052
21 changed files with 488 additions and 302 deletions

View File

@@ -94,6 +94,17 @@ Entity model moves (git mv — history preserved):
- `packages/auth/src/infrastructure/services/authentication.service.ts` — real `AuthenticationService` using `node:crypto` for hashing/UUIDs; session methods deferred (see §7)
- `packages/auth/src/infrastructure/services/authentication.service.test.ts` — tests for `generateUserId`, `hashPassword`/`verifyPassword` round-trip, and deferred-method error assertions
### Task 5: Blog factory refactor — new files
- `packages/blog/src/application/use-cases/get-article-by-slug.use-case.ts` — NEW use case factory; throws `ArticleNotFoundError` when slug is not found (previously the controller hit the repo directly, bypassing use-case error handling)
- `packages/blog/src/application/use-cases/get-article-by-slug.use-case.test.ts` — 2 tests (slug found, slug missing → ArticleNotFoundError)
- `packages/blog/src/interface-adapters/controllers/get-articles.controller.ts` — factory controller, replaces the `getArticlesController` function from `articles.controller.ts`
- `packages/blog/src/interface-adapters/controllers/get-articles.controller.test.ts` — 3 tests (valid input, status filter, invalid shape → InputParseError)
- `packages/blog/src/interface-adapters/controllers/create-article.controller.ts` — factory controller, replaces `createArticleController` from `articles.controller.ts`
- `packages/blog/src/interface-adapters/controllers/create-article.controller.test.ts` — 3 tests (valid, missing title, missing authorId)
- `packages/blog/src/interface-adapters/controllers/get-article-by-slug.controller.ts` — factory controller; now delegates to `getArticleBySlugUseCase` (which throws `ArticleNotFoundError`) instead of calling repo directly
- `packages/blog/src/interface-adapters/controllers/get-article-by-slug.controller.test.ts` — 3 tests (found, not found → ArticleNotFoundError, empty slug → InputParseError)
### Task 2: Entities split — new error files
- `packages/auth/src/entities/errors/auth.ts` — AuthenticationError, UnauthenticatedError, UnauthorizedError (split from errors.ts)
@@ -107,6 +118,11 @@ Entity model moves (git mv — history preserved):
## 3. Files deleted (with reason)
### Task 5: Blog factory refactor — deleted files
- `packages/blog/src/interface-adapters/controllers/articles.controller.ts` — multi-method controller replaced by 3 single-responsibility factory files (`get-articles.controller.ts`, `create-article.controller.ts`, `get-article-by-slug.controller.ts`)
- `packages/blog/src/interface-adapters/controllers/articles.controller.test.ts` — deleted with the controller; tests rewritten in the per-controller test files
### Task 2: Entities split — old errors.ts files removed
- `packages/auth/src/entities/errors.ts` — replaced by `errors/auth.ts` + `errors/common.ts`
@@ -118,18 +134,20 @@ Entity model moves (git mv — history preserved):
### 4.1 Use cases — factory function pattern
Applied to all 3 auth use cases (`sign-in`, `sign-up`, `sign-out`):
Applied to all 3 auth use cases (`sign-in`, `sign-up`, `sign-out`) in Task 4, and all 3 blog use cases (`get-articles`, `create-article`, `get-article-by-slug` NEW) in Task 5:
- Use cases are now factory functions: `(deps) => async (input) => result`
- Each file exports `export type I*UseCase = ReturnType<typeof *UseCase>` for DI typing
- Use cases NO LONGER call `authContainer.get()` inside their bodies — all dependencies are passed as factory arguments
- Tests construct mocks directly: `const useCase = signInUseCase(mockUsers, mockAuth); await useCase(input);`
- Use cases NO LONGER call `*Container.get()` inside their bodies — all dependencies are passed as factory arguments
- Tests construct mocks directly: `const useCase = getArticlesUseCase(repo); await useCase({ status: "draft" });`
- NEW `getArticleBySlugUseCase`: previously the slug lookup bypassed the use case layer (controller called repo directly); now the use case owns the `ArticleNotFoundError` throw
### 4.2 Controllers — one per use case
Applied to all 3 auth controllers (`sign-in`, `sign-up`, `sign-out`):
Applied to all 3 auth controllers (`sign-in`, `sign-up`, `sign-out`) in Task 4; blog controllers split in Task 5:
- Controllers were already split (one file per use case) — Task 4 refactors them to factory functions
- Controllers were already split for auth (one file per use case) — Task 4 refactors them to factory functions
- Blog: the multi-method `articles.controller.ts` is deleted and replaced by 3 single-responsibility files
- Factory pattern: `(useCase: I*UseCase) => async (input) => result`
- Each exports `export type I*Controller = ReturnType<typeof *Controller>`
- Validation (Zod `safeParse`) stays inside the controller factory; throws `InputParseError` on failure
@@ -149,13 +167,20 @@ Pattern now in place across auth, blog, marketing-pages, navigation (media skipp
### 5.1 Inversify `.toDynamicValue` bindings
Applied to `packages/auth/src/di/module.ts`:
Applied to `packages/auth/src/di/module.ts` (Task 4) and `packages/blog/src/di/module.ts` (Task 5):
**auth:**
- `AUTH_SYMBOLS` expanded with 6 new keys: `ISignInUseCase`, `ISignUpUseCase`, `ISignOutUseCase`, `ISignInController`, `ISignUpController`, `ISignOutController`
- Use cases bound with `.toDynamicValue((ctx) => factoryFn(ctx.container.get(...)))` — dependencies resolved from the container at call time
- Controllers bound identically, taking the corresponding use case symbol from the container
- Repository and service bindings remain `.to(Mock*)` as the default
**blog:**
- `BLOG_SYMBOLS` expanded with 6 new keys: `IGetArticlesUseCase`, `ICreateArticleUseCase`, `IGetArticleBySlugUseCase`, `IGetArticlesController`, `ICreateArticleController`, `IGetArticleBySlugController`
- All use cases and controllers bound with `.toDynamicValue()` — same pattern as auth
- Repository binding remains `.to(MockArticlesRepository)` as the default
- tRPC router (`integrations/api/router.ts`) updated to resolve controllers via `blogContainer.get<IXController>(BLOG_SYMBOLS.IXController)` instead of importing controllers directly
### 5.2 Mock siblings registered as default bindings
- `MockUsersRepository` and `MockAuthenticationService` remain the default bindings in `AuthModule`