import { EventsHandler, IEventHandler } from '@nestjs/cqrs';
import { TransactionChangeStatusEvent } from 'src/modules/transaction/transaction/domain/entities/event/transaction-change-status.event';
import { TicketDataService } from '../../data/services/ticket.service';

@EventsHandler(TransactionChangeStatusEvent)
export class QueueTransactionCancelHandler
  implements IEventHandler<TransactionChangeStatusEvent>
{
  constructor(private queueService: TicketDataService) {}

  async handle(event: TransactionChangeStatusEvent) {
    const process_data = event.data.data;

    /**
     * If data still in process (not settled) then don't create the queue order
     */
    if (!['cancel', 'rejected'].includes(process_data?.status)) return;

    this.queueService.deleteQueue(event.data.id);
  }
}
