11import Probe from "@Shared/Infrastructure/Audit/Probe" ;
2- import { Application , EventSourcing } from "hollywood-js" ;
2+ import { Application } from "hollywood-js" ;
33import type { IAppError } from "hollywood-js/src/Application/Bus/CallbackArg" ;
44import { inject , injectable } from "inversify" ;
55import type { Counter } from "prom-client" ;
66import ConflictException from "../../../Shared/Domain/Exceptions/ConflictException" ;
77import Transaction from "../../Domain/Transaction" ;
8+ import ITransactionWriteRepository from "../../Domain/WriteRepository" ;
89import CreateCommand from "./Command" ;
910
1011@injectable ( )
@@ -14,9 +15,8 @@ export default class Create implements Application.ICommandHandler {
1415 private readonly success : Counter < string > ;
1516
1617 constructor (
17- @inject (
18- "infrastructure.transaction.eventStore" ,
19- ) private readonly writeModel : EventSourcing . EventStore < Transaction > ,
18+ @inject ( "infrastructure.transaction.writeRepository" )
19+ private readonly writeModel : ITransactionWriteRepository ,
2020 ) {
2121 this . error = Probe . counter ( { name : "transaction_create_error" , help : "Counter of the incremental transaction create errors" } ) ;
2222 this . conflicts = Probe . counter ( { name : "transaction_create_conflict" , help : "Counter of the incremental transaction create conflicts" } ) ;
@@ -25,19 +25,9 @@ export default class Create implements Application.ICommandHandler {
2525
2626 @Application . autowiring
2727 public async handle ( command : CreateCommand ) : Promise < void | IAppError > {
28-
29- try {
30- await this . writeModel . load ( command . uuid . toIdentity ( ) ) ;
28+ if ( await this . writeModel . exists ( command . uuid ) ) {
3129 this . conflicts . inc ( 1 ) ;
3230 throw new ConflictException ( "Already exists" ) ;
33- } catch ( err ) {
34- if ( err instanceof ConflictException ) {
35- throw err ;
36- }
37- if ( ! ( err instanceof EventSourcing . AggregateRootNotFoundException ) ) {
38- this . error . inc ( 1 ) ;
39- throw err ;
40- }
4131 }
4232
4333 const transaction : Transaction = Transaction . create (
@@ -46,7 +36,12 @@ export default class Create implements Application.ICommandHandler {
4636 command . price ,
4737 ) ;
4838
49- await this . writeModel . save ( transaction ) ;
50- this . success . inc ( 1 ) ;
39+ try {
40+ await this . writeModel . save ( transaction ) ;
41+ this . success . inc ( 1 ) ;
42+ } catch ( err ) {
43+ this . error . inc ( 1 ) ;
44+ throw err ;
45+ }
5146 }
5247}
0 commit comments