feat(SPG-737): fix filter reconciliation #57
|
@ -34,7 +34,9 @@ import { UserDataService } from 'src/modules/user-related/user/data/services/use
|
|||
import { ItemDataService } from 'src/modules/item-related/item/data/services/item-data.service';
|
||||
import {
|
||||
BookingDeletedEvent,
|
||||
BookingHandler,
|
||||
// BookingHandler,
|
||||
BookingUpdateHandler,
|
||||
ChangeStatusBookingHandler,
|
||||
} from './domain/managers/booking.handler';
|
||||
import { TransactionDataService } from 'src/modules/transaction/transaction/data/services/transaction-data.service';
|
||||
import { TransactionModel } from 'src/modules/transaction/transaction/data/models/transaction.model';
|
||||
|
@ -59,7 +61,9 @@ import { VipCodeCreatedHandler } from './domain/managers/vip-code.handler';
|
|||
],
|
||||
controllers: [CouchDataController],
|
||||
providers: [
|
||||
BookingHandler,
|
||||
// BookingHandler,
|
||||
BookingUpdateHandler,
|
||||
ChangeStatusBookingHandler,
|
||||
BookingDeletedEvent,
|
||||
PaymentMethodDeletedHandler,
|
||||
PaymentMethodUpdatedHandler,
|
||||
|
|
|
@ -68,4 +68,15 @@ export class CouchService {
|
|||
});
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
public async getDoc(id: string, database: string) {
|
||||
try {
|
||||
const nano = this.nanoInstance;
|
||||
const db = nano.use(database);
|
||||
const result = await db.get(id);
|
||||
return result;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ 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 { 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';
|
||||
|
@ -25,8 +25,60 @@ export class BookingDeletedEvent
|
|||
}
|
||||
}
|
||||
|
||||
@EventsHandler(TransactionChangeStatusEvent, TransactionUpdatedEvent)
|
||||
export class BookingHandler
|
||||
// @EventsHandler(TransactionChangeStatusEvent, TransactionUpdatedEvent)
|
||||
// export class BookingHandler
|
||||
// implements IEventHandler<TransactionChangeStatusEvent>
|
||||
// {
|
||||
// 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 ||
|
||||
// ([STATUS.ACTIVE, STATUS.SETTLED].includes(data.status) &&
|
||||
// data.payment_type != TransactionPaymentType.COUNTER)
|
||||
// ) {
|
||||
// const booking = await this.bookingService.getOneByOptions({
|
||||
// where: {
|
||||
// id: data.id,
|
||||
// },
|
||||
// relations: ['items'],
|
||||
// });
|
||||
|
||||
// mappingTransaction(booking);
|
||||
|
||||
// if (
|
||||
// (old_data?.status != data.status ||
|
||||
// data.payment_type != TransactionPaymentType.COUNTER) &&
|
||||
// [STATUS.PENDING, STATUS.ACTIVE, STATUS.SETTLED].includes(data.status)
|
||||
// ) {
|
||||
// await this.couchService.createDoc(
|
||||
// {
|
||||
// _id: booking.id,
|
||||
// ...booking,
|
||||
// },
|
||||
// 'booking',
|
||||
// );
|
||||
// } else {
|
||||
// await this.couchService.updateDoc(
|
||||
// {
|
||||
// _id: booking.id,
|
||||
// ...booking,
|
||||
// },
|
||||
// 'booking',
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
@EventsHandler(TransactionChangeStatusEvent)
|
||||
export class ChangeStatusBookingHandler
|
||||
implements IEventHandler<TransactionChangeStatusEvent>
|
||||
{
|
||||
constructor(
|
||||
|
@ -35,28 +87,22 @@ export class BookingHandler
|
|||
) {}
|
||||
|
||||
async handle(event: TransactionChangeStatusEvent) {
|
||||
const old_data = event.data.old;
|
||||
const data = event.data.data;
|
||||
const isFromCounter = data.payment_type == TransactionPaymentType.COUNTER;
|
||||
const dataID = data?.id;
|
||||
|
||||
if (!isFromCounter) {
|
||||
const couchData = await this.couchService.getDoc(dataID, 'booking');
|
||||
|
||||
if (
|
||||
data.payment_type == TransactionPaymentType.COUNTER ||
|
||||
([STATUS.ACTIVE, STATUS.SETTLED].includes(data.status) &&
|
||||
data.payment_type != TransactionPaymentType.COUNTER)
|
||||
) {
|
||||
const booking = await this.bookingService.getOneByOptions({
|
||||
where: {
|
||||
id: data.id,
|
||||
},
|
||||
relations: ['items'],
|
||||
});
|
||||
|
||||
mappingTransaction(booking);
|
||||
|
||||
if (
|
||||
(old_data?.status != data.status ||
|
||||
data.payment_type != TransactionPaymentType.COUNTER) &&
|
||||
[STATUS.PENDING, STATUS.ACTIVE, STATUS.SETTLED].includes(data.status)
|
||||
) {
|
||||
if (!couchData) {
|
||||
await this.couchService.createDoc(
|
||||
{
|
||||
_id: booking.id,
|
||||
|
@ -76,3 +122,38 @@ export class BookingHandler
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventsHandler(TransactionUpdatedEvent)
|
||||
export class BookingUpdateHandler
|
||||
implements IEventHandler<TransactionUpdatedEvent>
|
||||
{
|
||||
constructor(
|
||||
private bookingService: TransactionDataService,
|
||||
private couchService: CouchService,
|
||||
) {}
|
||||
|
||||
async handle(event: TransactionChangeStatusEvent) {
|
||||
const data = event.data.data;
|
||||
const dataID = data?.id;
|
||||
|
||||
const couchData = await this.couchService.getDoc(dataID, 'booking');
|
||||
|
||||
if (couchData) {
|
||||
const booking = await this.bookingService.getOneByOptions({
|
||||
where: {
|
||||
id: data.id,
|
||||
},
|
||||
relations: ['items'],
|
||||
});
|
||||
|
||||
mappingTransaction(booking);
|
||||
await this.couchService.updateDoc(
|
||||
{
|
||||
_id: booking.id,
|
||||
...booking,
|
||||
},
|
||||
'booking',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -130,18 +130,18 @@ export class IndexReconciliationManager extends BaseIndexManager<TransactionEnti
|
|||
|
||||
if (this.filterParam.payment_via) {
|
||||
queryBuilder.andWhere(
|
||||
`${this.tableName}.payment_type::text In (:...type)`,
|
||||
`${this.tableName}.payment_type_method_name::text LIKE :type`,
|
||||
{
|
||||
type: [`%${this.filterParam.payment_via}%`],
|
||||
type: `%${this.filterParam.payment_via}%`,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
if (this.filterParam.payment_bank) {
|
||||
queryBuilder.andWhere(
|
||||
`${this.tableName}.payment_type_method_name::text In (:...banks)`,
|
||||
`${this.tableName}.payment_type_method_name::text LIKE :banks`,
|
||||
{
|
||||
banks: [`%${this.filterParam.payment_bank}%`],
|
||||
banks: `%${this.filterParam.payment_bank}%`,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
@ -164,12 +164,15 @@ export class IndexReconciliationManager extends BaseIndexManager<TransactionEnti
|
|||
).getQuery();
|
||||
}
|
||||
|
||||
queryBuilder.andWhere(
|
||||
`${this.tableName}.reconciliation_status Not In (:...statuses)`,
|
||||
{
|
||||
statuses: [STATUS.DRAFT],
|
||||
},
|
||||
);
|
||||
if (!this.filterParam.statuses || this.filterParam.statuses?.length === 0) {
|
||||
queryBuilder.andWhere(
|
||||
`${this.tableName}.reconciliation_status Not In (:...statuses)`,
|
||||
{
|
||||
statuses: [STATUS.DRAFT],
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return queryBuilder;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,21 +1,33 @@
|
|||
import { EventsHandler, IEventHandler } from '@nestjs/cqrs';
|
||||
import { EventBus, EventsHandler, IEventHandler } from '@nestjs/cqrs';
|
||||
import { RefundChangeStatusEvent } from 'src/modules/transaction/refund/domain/entities/event/refund-change-status.event';
|
||||
import { TransactionDataService } from '../../../data/services/transaction-data.service';
|
||||
import { OPERATION, STATUS } from 'src/core/strings/constants/base.constants';
|
||||
import {
|
||||
BLANK_USER,
|
||||
OPERATION,
|
||||
STATUS,
|
||||
} from 'src/core/strings/constants/base.constants';
|
||||
import { TransactionModel } from '../../../data/models/transaction.model';
|
||||
import { RefundDeletedEvent } from 'src/modules/transaction/refund/domain/entities/event/refund-deleted.event';
|
||||
import { RefundCreatedEvent } from 'src/modules/transaction/refund/domain/entities/event/refund-created.event';
|
||||
import { TransactionChangeStatusEvent } from '../../entities/event/transaction-change-status.event';
|
||||
import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
|
||||
|
||||
@EventsHandler(RefundChangeStatusEvent, RefundDeletedEvent, RefundCreatedEvent)
|
||||
export class RefundUpdatedHandler
|
||||
implements IEventHandler<RefundChangeStatusEvent>
|
||||
{
|
||||
constructor(private dataService: TransactionDataService) {}
|
||||
constructor(
|
||||
private dataService: TransactionDataService,
|
||||
private eventBus: EventBus,
|
||||
) {}
|
||||
|
||||
async handle(event: RefundChangeStatusEvent) {
|
||||
const current_data = event.data.data;
|
||||
let status: STATUS;
|
||||
|
||||
//GET TRANSACTION DATA BEFORE UPDATE
|
||||
const oldData = current_data?.transaction;
|
||||
|
||||
const queryRunner = this.dataService
|
||||
.getRepository()
|
||||
.manager.connection.createQueryRunner();
|
||||
|
@ -47,5 +59,25 @@ export class RefundUpdatedHandler
|
|||
status: status,
|
||||
},
|
||||
);
|
||||
|
||||
//GET TRANSACTION DATA AFTER UPDATE
|
||||
const newData = await this.dataService.getOneByOptions({
|
||||
where: {
|
||||
id: current_data.transaction_id,
|
||||
},
|
||||
relations: ['items'],
|
||||
});
|
||||
|
||||
this.eventBus.publish(
|
||||
new TransactionChangeStatusEvent({
|
||||
id: current_data.transaction_id,
|
||||
old: oldData,
|
||||
data: newData,
|
||||
user: BLANK_USER,
|
||||
description: 'Refund Callback',
|
||||
module: TABLE_NAME.TRANSACTION,
|
||||
op: OPERATION.UPDATE,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue