import { DbTransaction } from '@/application/contracts' ; import { Controller } from '@/application/controllers' ; import { HttpResponse } from '@/application/helpers' ; export class DbTransactionController extends Controller { constructor ( private readonly decoratee , private readonly db ) { super ( ) ; } async perform ( httpRequest ) { await this . db . openTransaction ( ) ; try { const httpResponse = await this . decoratee . perform ( httpRequest ) ; await this . db . commit ( ) ; return httpResponse ; } catch ( error ) { await this . db . rollback ( ) ; throw error ; } finally { await this . db . closeTransaction ( ) ; } } }