forked from jorge07/billing-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInMemoryWriteRepository.ts
More file actions
38 lines (34 loc) · 1.35 KB
/
Copy pathInMemoryWriteRepository.ts
File metadata and controls
38 lines (34 loc) · 1.35 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
import { EventSourcing } from "hollywood-js";
import { inject, injectable } from "inversify";
import Transaction from "@Transaction/Domain/Transaction";
import ITransactionWriteRepository from "@Transaction/Domain/WriteRepository";
import TransactionId from "@Transaction/Domain/ValueObject/TransactionId";
/**
* In-memory ITransactionWriteRepository for tests.
* Shares the same InMemoryEventStore as InMemoryTransactionRepository
* so both the write-side (handlers) and the test assertions see the same state.
*/
@injectable()
export class InMemoryWriteRepository implements ITransactionWriteRepository {
constructor(
@inject("infrastructure.transaction.eventStore")
private readonly eventStore: EventSourcing.EventStore<Transaction>,
) {}
public async exists(id: TransactionId): Promise<boolean> {
try {
await this.eventStore.load(id.toIdentity());
return true;
} catch (err) {
if (err instanceof EventSourcing.AggregateRootNotFoundException) {
return false;
}
throw err;
}
}
public async load(id: TransactionId): Promise<Transaction> {
return await this.eventStore.load(id.toIdentity()) as Transaction;
}
public async save(transaction: Transaction): Promise<void> {
await this.eventStore.save(transaction);
}
}