Merge pull request 'feat(SPG-737): fix filter reconciliation' (#57) from fix/bug-firman into development

Reviewed-on: #57
pull/58/head
firmanr 2024-08-07 10:46:32 +00:00
commit 72e47c2486
5 changed files with 161 additions and 30 deletions

View File

@ -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 { ItemDataService } from 'src/modules/item-related/item/data/services/item-data.service';
import { import {
BookingDeletedEvent, BookingDeletedEvent,
BookingHandler, // BookingHandler,
BookingUpdateHandler,
ChangeStatusBookingHandler,
} from './domain/managers/booking.handler'; } from './domain/managers/booking.handler';
import { TransactionDataService } from 'src/modules/transaction/transaction/data/services/transaction-data.service'; import { TransactionDataService } from 'src/modules/transaction/transaction/data/services/transaction-data.service';
import { TransactionModel } from 'src/modules/transaction/transaction/data/models/transaction.model'; 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], controllers: [CouchDataController],
providers: [ providers: [
BookingHandler, // BookingHandler,
BookingUpdateHandler,
ChangeStatusBookingHandler,
BookingDeletedEvent, BookingDeletedEvent,
PaymentMethodDeletedHandler, PaymentMethodDeletedHandler,
PaymentMethodUpdatedHandler, PaymentMethodUpdatedHandler,

View File

@ -72,4 +72,15 @@ export class CouchService {
}); });
} catch (error) {} } 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;
}
}
} }

View File

@ -2,7 +2,7 @@ import { EventsHandler, IEventHandler } from '@nestjs/cqrs';
import { TransactionDataService } from 'src/modules/transaction/transaction/data/services/transaction-data.service'; 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 { TransactionChangeStatusEvent } from 'src/modules/transaction/transaction/domain/entities/event/transaction-change-status.event';
import { CouchService } from '../../data/services/couch.service'; 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 { TransactionPaymentType } from 'src/modules/transaction/transaction/constants';
import { mappingTransaction } from 'src/modules/transaction/transaction/domain/usecases/managers/helpers/mapping-transaction.helper'; 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 { TransactionDeletedEvent } from 'src/modules/transaction/transaction/domain/entities/event/transaction-deleted.event';
@ -25,8 +25,60 @@ export class BookingDeletedEvent
} }
} }
@EventsHandler(TransactionChangeStatusEvent, TransactionUpdatedEvent) // @EventsHandler(TransactionChangeStatusEvent, TransactionUpdatedEvent)
export class BookingHandler // 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> implements IEventHandler<TransactionChangeStatusEvent>
{ {
constructor( constructor(
@ -35,28 +87,22 @@ export class BookingHandler
) {} ) {}
async handle(event: TransactionChangeStatusEvent) { async handle(event: TransactionChangeStatusEvent) {
const old_data = event.data.old;
const data = event.data.data; 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({ const booking = await this.bookingService.getOneByOptions({
where: { where: {
id: data.id, id: data.id,
}, },
relations: ['items'], relations: ['items'],
}); });
mappingTransaction(booking); mappingTransaction(booking);
if ( if (!couchData) {
(old_data?.status != data.status ||
data.payment_type != TransactionPaymentType.COUNTER) &&
[STATUS.PENDING, STATUS.ACTIVE, STATUS.SETTLED].includes(data.status)
) {
await this.couchService.createDoc( await this.couchService.createDoc(
{ {
_id: booking.id, _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',
);
}
}
}

View File

@ -130,18 +130,18 @@ export class IndexReconciliationManager extends BaseIndexManager<TransactionEnti
if (this.filterParam.payment_via) { if (this.filterParam.payment_via) {
queryBuilder.andWhere( 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) { if (this.filterParam.payment_bank) {
queryBuilder.andWhere( 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(); ).getQuery();
} }
if (!this.filterParam.statuses || this.filterParam.statuses?.length === 0) {
queryBuilder.andWhere( queryBuilder.andWhere(
`${this.tableName}.reconciliation_status Not In (:...statuses)`, `${this.tableName}.reconciliation_status Not In (:...statuses)`,
{ {
statuses: [STATUS.DRAFT], statuses: [STATUS.DRAFT],
}, },
); );
}
return queryBuilder; return queryBuilder;
} }
} }

View File

@ -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 { RefundChangeStatusEvent } from 'src/modules/transaction/refund/domain/entities/event/refund-change-status.event';
import { TransactionDataService } from '../../../data/services/transaction-data.service'; 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 { TransactionModel } from '../../../data/models/transaction.model';
import { RefundDeletedEvent } from 'src/modules/transaction/refund/domain/entities/event/refund-deleted.event'; 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 { 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) @EventsHandler(RefundChangeStatusEvent, RefundDeletedEvent, RefundCreatedEvent)
export class RefundUpdatedHandler export class RefundUpdatedHandler
implements IEventHandler<RefundChangeStatusEvent> implements IEventHandler<RefundChangeStatusEvent>
{ {
constructor(private dataService: TransactionDataService) {} constructor(
private dataService: TransactionDataService,
private eventBus: EventBus,
) {}
async handle(event: RefundChangeStatusEvent) { async handle(event: RefundChangeStatusEvent) {
const current_data = event.data.data; const current_data = event.data.data;
let status: STATUS; let status: STATUS;
//GET TRANSACTION DATA BEFORE UPDATE
const oldData = current_data?.transaction;
const queryRunner = this.dataService const queryRunner = this.dataService
.getRepository() .getRepository()
.manager.connection.createQueryRunner(); .manager.connection.createQueryRunner();
@ -47,5 +59,25 @@ export class RefundUpdatedHandler
status: status, 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,
}),
);
} }
} }