From 4f0b378ec6a1b240e0f397716630daed7e152cc5 Mon Sep 17 00:00:00 2001 From: shancheas Date: Tue, 22 Oct 2024 13:30:49 +0700 Subject: [PATCH] fix: missing transaction handler --- .../handlers/transaction.handler.ts | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/modules/queue/infrastructure/handlers/transaction.handler.ts diff --git a/src/modules/queue/infrastructure/handlers/transaction.handler.ts b/src/modules/queue/infrastructure/handlers/transaction.handler.ts new file mode 100644 index 0000000..9d168e9 --- /dev/null +++ b/src/modules/queue/infrastructure/handlers/transaction.handler.ts @@ -0,0 +1,54 @@ +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 { TicketDataService } from '../../data/services/ticket.service'; +import { QueueOrder } from '../../domain/entities/order.entity'; +import { QueueTicket } from '../../domain/entities/ticket.entity'; +import { QueueItem } from '../../domain/entities/queue-item.entity'; + +@EventsHandler(TransactionChangeStatusEvent) +export class QueueTransactionHandler + implements IEventHandler +{ + constructor( + private dataService: TransactionDataService, + 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 (process_data?.status != 'settled') return; + + const transaction = await this.dataService.getOneByOptions({ + where: { + id: event.data.id, + }, + relations: ['items'], + }); + + const { customer_name, customer_phone, invoice_code } = transaction; + const current_date = new Date().valueOf(); + const customerOrder = { + code: invoice_code, + customer: customer_name, + phone: customer_phone, + date: current_date, + }; + + const items = transaction.items.map((item) => { + return { + item_id: item.item_id, + qty: item.qty, + }; + }); + + const ticket: QueueTicket = { ...customerOrder, items }; + const order: QueueOrder = { ...customerOrder, tickets: [ticket] }; + + this.queueService.createQueueOrder(order); + } +}