import { EventsHandler, IEventHandler } from '@nestjs/cqrs'; import { TransactionDataService } from 'src/modules/transaction/transaction/data/services/transaction-data.service'; import { TransactionChangeStatusEvent } from 'src/modules/transaction/transaction/domain/entities/event/transaction-change-status.event'; import { CouchService } from '../../data/services/couch.service'; import { STATUS } from 'src/core/strings/constants/base.constants'; import { TransactionPaymentType } from 'src/modules/transaction/transaction/constants'; import { mappingTransaction } from 'src/modules/transaction/transaction/domain/usecases/managers/helpers/mapping-transaction.helper'; import { TransactionDeletedEvent } from 'src/modules/transaction/transaction/domain/entities/event/transaction-deleted.event'; import { TransactionUpdatedEvent } from 'src/modules/transaction/transaction/domain/entities/event/transaction-updated.event'; @EventsHandler(TransactionDeletedEvent) export class BookingDeletedEvent implements IEventHandler { constructor(private couchService: CouchService) {} async handle(event: TransactionDeletedEvent) { await this.couchService.deleteDoc( { _id: event.data.id, ...event.data.data, }, 'item', ); } } @EventsHandler(TransactionChangeStatusEvent, TransactionUpdatedEvent) export class BookingHandler implements IEventHandler { constructor( private bookingService: TransactionDataService, private couchService: CouchService, ) {} async handle(event: TransactionChangeStatusEvent) { const old_data = event.data.old; const data = event.data.data; if (data.payment_type != TransactionPaymentType.COUNTER) return; const booking = await this.bookingService.getOneByOptions({ where: { id: data.id, }, relations: ['items'], }); mappingTransaction(booking); if ( old_data?.status != data.status && [STATUS.PENDING, STATUS.ACTIVE].includes(data.status) ) { await this.couchService.createDoc( { _id: booking.id, ...booking, }, 'booking', ); } else { await this.couchService.updateDoc( { _id: booking.id, ...booking, }, 'booking', ); } } }