mirror of
https://github.com/juherr/kill-the-news.git
synced 2026-06-20 22:03:48 +00:00
refactor: separate Feed domain state from persistence DTO
Move four DDD tensions on the Feed aggregate to ground: - #1 The aggregate now holds a domain FeedState (camelCase) instead of the snake_case FeedConfig DTO; infrastructure/feed-mapper.ts owns the FeedState<->FeedConfig/FeedListItem translation as the sole snake_case site outside the HTTP edge. - #3 Replace the edit() recomputeExpiry control flag with a Lifetime VO: passing a lifetime recomputes expiry, omitting it preserves the current one (the dashboard quick-edit path). - #4 Domain events carry their own feedId; dispatchFeedEvents centralizes the drain+dispatch in the application layer (no more manual pullEvents at call sites), keeping infra->application dependency direction intact. - #6 Rename FeedId.fromTrusted to FeedId.unchecked to make the absence of revalidation explicit. Adds Lifetime + feed-mapper round-trip tests. 353 tests green, tsc clean, wrangler dry-run OK. Docs (CLAUDE.md) synced. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { fromConfigDTO, toConfigDTO, toListItemDTO } from "./feed-mapper";
|
||||
import { FeedId } from "../domain/value-objects/feed-id";
|
||||
import type { FeedConfig } from "../types";
|
||||
|
||||
const fullConfig: FeedConfig = {
|
||||
title: "News",
|
||||
description: "desc",
|
||||
language: "en",
|
||||
author: "Jane",
|
||||
allowed_senders: ["a@x.com"],
|
||||
blocked_senders: ["b@y.com"],
|
||||
created_at: 1000,
|
||||
updated_at: 2000,
|
||||
expires_at: 3000,
|
||||
};
|
||||
|
||||
describe("feed-mapper", () => {
|
||||
it("round-trips a full config DTO through domain state unchanged", () => {
|
||||
expect(toConfigDTO(fromConfigDTO(fullConfig))).toEqual(fullConfig);
|
||||
});
|
||||
|
||||
it("defaults absent sender lists to empty arrays on the domain side", () => {
|
||||
const state = fromConfigDTO({
|
||||
title: "T",
|
||||
language: "en",
|
||||
created_at: 1,
|
||||
});
|
||||
expect(state.allowedSenders).toEqual([]);
|
||||
expect(state.blockedSenders).toEqual([]);
|
||||
});
|
||||
|
||||
it("projects the feeds:list item from domain state", () => {
|
||||
const item = toListItemDTO(
|
||||
FeedId.unchecked("a.b.42"),
|
||||
fromConfigDTO(fullConfig),
|
||||
);
|
||||
expect(item).toEqual({
|
||||
id: "a.b.42",
|
||||
title: "News",
|
||||
description: "desc",
|
||||
expires_at: 3000,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import { FeedConfig, FeedListItem } from "../types";
|
||||
import { FeedState } from "../domain/feed-state";
|
||||
import { FeedId } from "../domain/value-objects/feed-id";
|
||||
|
||||
/**
|
||||
* The translation seam between the Feed aggregate's domain state (camelCase) and
|
||||
* the persistence/edge DTOs (`FeedConfig`/`FeedListItem`, snake_case). This is
|
||||
* the ONLY place outside the HTTP edge that knows the stored field names — the
|
||||
* domain stays free of the storage dialect, and the repository round-trips
|
||||
* through here on every load/save.
|
||||
*/
|
||||
|
||||
/** Persisted config DTO → domain state (used by `FeedRepository.load`). */
|
||||
export function fromConfigDTO(dto: FeedConfig): FeedState {
|
||||
return {
|
||||
title: dto.title,
|
||||
description: dto.description,
|
||||
language: dto.language,
|
||||
author: dto.author,
|
||||
allowedSenders: dto.allowed_senders ?? [],
|
||||
blockedSenders: dto.blocked_senders ?? [],
|
||||
createdAt: dto.created_at,
|
||||
updatedAt: dto.updated_at,
|
||||
expiresAt: dto.expires_at,
|
||||
};
|
||||
}
|
||||
|
||||
/** Domain state → persisted config DTO (used by `FeedRepository.save`). */
|
||||
export function toConfigDTO(state: FeedState): FeedConfig {
|
||||
return {
|
||||
title: state.title,
|
||||
description: state.description,
|
||||
language: state.language,
|
||||
author: state.author,
|
||||
allowed_senders: state.allowedSenders,
|
||||
blocked_senders: state.blockedSenders,
|
||||
created_at: state.createdAt,
|
||||
updated_at: state.updatedAt,
|
||||
expires_at: state.expiresAt,
|
||||
};
|
||||
}
|
||||
|
||||
/** Domain state → the projection cached in the global `feeds:list` registry. */
|
||||
export function toListItemDTO(id: FeedId, state: FeedState): FeedListItem {
|
||||
return {
|
||||
id: id.value,
|
||||
title: state.title,
|
||||
description: state.description,
|
||||
expires_at: state.expiresAt,
|
||||
};
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import { FeedId } from "../domain/value-objects/feed-id";
|
||||
import type { Env, FeedConfig, FeedMetadata, EmailData } from "../types";
|
||||
|
||||
const mockEnv = () => createMockEnv() as unknown as Env;
|
||||
const fid = (value: string) => FeedId.fromTrusted(value);
|
||||
const fid = (value: string) => FeedId.unchecked(value);
|
||||
|
||||
const sampleConfig = (overrides: Partial<FeedConfig> = {}): FeedConfig => ({
|
||||
title: "Test Feed",
|
||||
@@ -106,9 +106,11 @@ describe("FeedRepository feed list", () => {
|
||||
{
|
||||
title,
|
||||
language: "en",
|
||||
created_at: 1000,
|
||||
allowedSenders: [],
|
||||
blockedSenders: [],
|
||||
createdAt: 1000,
|
||||
description: opts.description,
|
||||
expires_at: opts.expires_at,
|
||||
expiresAt: opts.expires_at,
|
||||
},
|
||||
{ emails: [] },
|
||||
);
|
||||
|
||||
@@ -10,6 +10,7 @@ import { FEEDS_LIST_KEY } from "../config/constants";
|
||||
import { feedKeys } from "../domain/feed-keys";
|
||||
import { Feed } from "../domain/feed.aggregate";
|
||||
import { FeedId } from "../domain/value-objects/feed-id";
|
||||
import { fromConfigDTO, toConfigDTO, toListItemDTO } from "./feed-mapper";
|
||||
import { logger } from "./logger";
|
||||
|
||||
/**
|
||||
@@ -69,19 +70,23 @@ export class FeedRepository {
|
||||
this.getMetadata(feedId),
|
||||
]);
|
||||
if (!config) return null;
|
||||
return Feed.reconstitute(feedId, config, metadata ?? { emails: [] });
|
||||
return Feed.reconstitute(
|
||||
feedId,
|
||||
fromConfigDTO(config),
|
||||
metadata ?? { emails: [] },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist both keys the aggregate owns (config + metadata) and keep the global
|
||||
* `feeds:list` entry in sync. The registry projection is derived from
|
||||
* `feed.summary()` here, so no caller has to remember to mirror it.
|
||||
* `feeds:list` entry in sync. Config/list DTOs are derived from the aggregate's
|
||||
* domain `state()` via `feed-mapper`, so no caller has to mirror snake_case.
|
||||
*/
|
||||
async save(feed: Feed): Promise<void> {
|
||||
await Promise.all([
|
||||
this.putConfig(feed.id, feed.toConfigSnapshot()),
|
||||
this.putConfig(feed.id, toConfigDTO(feed.state())),
|
||||
this.putMetadata(feed.id, feed.toMetadataSnapshot()),
|
||||
this.upsertListEntry(feed.summary()),
|
||||
this.upsertListEntry(toListItemDTO(feed.id, feed.state())),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -101,8 +106,8 @@ export class FeedRepository {
|
||||
*/
|
||||
async saveConfig(feed: Feed): Promise<void> {
|
||||
await Promise.all([
|
||||
this.putConfig(feed.id, feed.toConfigSnapshot()),
|
||||
this.upsertListEntry(feed.summary()),
|
||||
this.putConfig(feed.id, toConfigDTO(feed.state())),
|
||||
this.upsertListEntry(toListItemDTO(feed.id, feed.state())),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import { FeedId } from "../domain/value-objects/feed-id";
|
||||
import type { Env, WebSubSubscription } from "../types";
|
||||
|
||||
const mockEnv = () => createMockEnv() as unknown as Env;
|
||||
const fid = FeedId.fromTrusted("a.b.42");
|
||||
const fid = FeedId.unchecked("a.b.42");
|
||||
|
||||
describe("WebSubSubscriptionRepository", () => {
|
||||
it("round-trips subscriptions and counts feeds with subscribers", async () => {
|
||||
|
||||
@@ -13,7 +13,7 @@ import { FeedId } from "../domain/value-objects/feed-id";
|
||||
import type { Env, WebSubSubscription } from "../types";
|
||||
|
||||
const mockEnv = () => createMockEnv() as unknown as Env;
|
||||
const fid = (value: string) => FeedId.fromTrusted(value);
|
||||
const fid = (value: string) => FeedId.unchecked(value);
|
||||
|
||||
describe("buildHmacSignature", () => {
|
||||
it("returns sha256= prefixed hex", async () => {
|
||||
|
||||
Reference in New Issue
Block a user