import { EventsHandler, IEventHandler } from '@nestjs/cqrs';
import { CronMidnightEvent } from 'src/modules/configuration/cron/domain/entities/cron-midnight.event';
import { RecapReconciliationManager } from '../managers/recap-reconciliation.manager';
import { TransactionModel } from 'src/modules/transaction/transaction/data/models/transaction.model';
import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
import { TransactionDataService } from 'src/modules/transaction/transaction/data/services/transaction-data.service';

@EventsHandler(CronMidnightEvent)
export class RecapPosTransactionHandler
  implements IEventHandler<CronMidnightEvent>
{
  constructor(
    private recapManager: RecapReconciliationManager,
    private dataService: TransactionDataService,
  ) {}

  async handle(event: CronMidnightEvent) {
    const data = new TransactionModel();
    this.recapManager.setData(data);
    this.recapManager.setService(this.dataService, TABLE_NAME.TRANSACTION);
    await this.recapManager.execute();
    return this.recapManager.getResult();
  }
}
