-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathAddRelationCommandHandler.ts
More file actions
57 lines (49 loc) · 1.84 KB
/
Copy pathAddRelationCommandHandler.ts
File metadata and controls
57 lines (49 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { AddRelationCommand } from "./AddRelationCommand.js";
import { IRelationAddedEventWriter } from "./IRelationAddedEventWriter.js";
import { IRelationAddedReader } from "./IRelationAddedReader.js";
import { IEventBus } from "../../../messaging/IEventBus.js";
import { Relation } from "../../../../domain/relations/Relation.js";
/**
* Handles adding a new relation.
* Creates a new relation aggregate, produces RelationAdded event, persists and publishes.
* Implements idempotency: if identical relation exists, returns existing ID.
*/
export class AddRelationCommandHandler {
constructor(
private readonly eventWriter: IRelationAddedEventWriter,
private readonly eventBus: IEventBus,
private readonly reader: IRelationAddedReader
) {}
async execute(command: AddRelationCommand): Promise<{ relationId: string }> {
// Check if identical relation exists (idempotent behavior)
const existingRelation = await this.reader.findByEntities(
command.fromEntityType,
command.fromEntityId,
command.toEntityType,
command.toEntityId,
command.relationType
);
if (existingRelation) {
// Relation already exists - idempotent: just return the existing ID
return { relationId: existingRelation.relationId };
}
// Create new aggregate
const relation = Relation.create();
const relationId = relation.snapshot.id;
// Domain logic produces event
const event = relation.add(
command.fromEntityType,
command.fromEntityId,
command.toEntityType,
command.toEntityId,
command.relationType,
command.description,
command.strength
);
// Persist event to file store
await this.eventWriter.append(event);
// Publish event to bus (projections will update via subscriptions)
await this.eventBus.publish(event);
return { relationId };
}
}