Merge branch 'development' of ssh://git.eigen.co.id:2222/eigen/pos-be into fix/bug-firman
commit
841f8889ec
|
@ -1,12 +1,25 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { EventBus } from '@nestjs/cqrs';
|
||||
import { mappingMidtransTransaction } from '../../domain/usecases/helpers/mapping-transaction.helper';
|
||||
import { Snap } from 'midtrans-client';
|
||||
import { MidtransStatus } from '../../domain/entities/midtrans-callback.event';
|
||||
import { TransactionReadService } from 'src/modules/transaction/transaction/data/services/transaction-read.service';
|
||||
|
||||
import * as moment from 'moment';
|
||||
|
||||
@Injectable()
|
||||
export class MidtransService {
|
||||
constructor(private eventBus: EventBus) {}
|
||||
constructor(private transaction: TransactionReadService) {}
|
||||
|
||||
isMoreThan24HoursAgo(dateString) {
|
||||
const date = moment(dateString, 'YYYY-MM-DD', true);
|
||||
if (!date.isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const now = moment();
|
||||
const diffInHours = now.diff(date, 'hours');
|
||||
return diffInHours > 24;
|
||||
}
|
||||
|
||||
get midtransInstance() {
|
||||
return new Snap({
|
||||
|
@ -20,6 +33,28 @@ export class MidtransService {
|
|||
return await this.midtransInstance.transaction.status(orderId);
|
||||
}
|
||||
|
||||
async syncPendingStatus(): Promise<any[]> {
|
||||
const pendingIds = await this.transaction.getPendingOrderId();
|
||||
const responses = [];
|
||||
|
||||
for (const transaction of pendingIds) {
|
||||
const { id, invoice_date } = transaction;
|
||||
let status;
|
||||
try {
|
||||
status = await this.getStatus(id);
|
||||
} catch (error) {
|
||||
status = {
|
||||
order_id: id,
|
||||
transaction_status: this.isMoreThan24HoursAgo(invoice_date)
|
||||
? 'cancel'
|
||||
: 'pending',
|
||||
};
|
||||
}
|
||||
responses.push(status);
|
||||
}
|
||||
return responses;
|
||||
}
|
||||
|
||||
async changeStatus(orderId: string, action: MidtransStatus): Promise<any> {
|
||||
return await this.midtransInstance.transaction[action](orderId);
|
||||
}
|
||||
|
|
|
@ -46,6 +46,27 @@ export class MidtransController {
|
|||
}
|
||||
}
|
||||
|
||||
@Get('sync')
|
||||
async syncStatus() {
|
||||
try {
|
||||
const results = await this.dataService.syncPendingStatus();
|
||||
|
||||
for (const data of results) {
|
||||
this.eventBus.publishAll([
|
||||
new MidtransCallbackEvent({
|
||||
id: data.order_id,
|
||||
data: data,
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
return 'Berhasil update status transaksi';
|
||||
} catch (error) {
|
||||
console.log(error.message);
|
||||
throw new Error('Gagal update status transaksi');
|
||||
}
|
||||
}
|
||||
|
||||
@Get(':id/change-status')
|
||||
@ApiQuery({ name: 'status', enum: MidtransStatus })
|
||||
async cancel(
|
||||
|
|
|
@ -3,12 +3,20 @@ import { CqrsModule } from '@nestjs/cqrs';
|
|||
import { MidtransController } from './infrastructure/midtrans.controller';
|
||||
import { MidtransService } from './data/services/midtrans.service';
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { TransactionReadService } from 'src/modules/transaction/transaction/data/services/transaction-read.service';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants';
|
||||
import { TransactionModel } from 'src/modules/transaction/transaction/data/models/transaction.model';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [ConfigModule.forRoot(), CqrsModule],
|
||||
imports: [
|
||||
ConfigModule.forRoot(),
|
||||
CqrsModule,
|
||||
TypeOrmModule.forFeature([TransactionModel], CONNECTION_NAME.DEFAULT),
|
||||
],
|
||||
controllers: [MidtransController],
|
||||
providers: [MidtransService],
|
||||
providers: [MidtransService, TransactionReadService],
|
||||
exports: [MidtransService],
|
||||
})
|
||||
export class MidtransModule {}
|
||||
|
|
|
@ -2,9 +2,13 @@ import { Injectable } from '@nestjs/common';
|
|||
import { TransactionEntity } from '../../domain/entities/transaction.entity';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { TransactionModel } from '../models/transaction.model';
|
||||
import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants';
|
||||
import {
|
||||
CONNECTION_NAME,
|
||||
STATUS,
|
||||
} from 'src/core/strings/constants/base.constants';
|
||||
import { Repository } from 'typeorm';
|
||||
import { BaseReadService } from 'src/core/modules/data/service/base-read.service';
|
||||
import { TransactionPaymentType } from '../../constants';
|
||||
|
||||
@Injectable()
|
||||
export class TransactionReadService extends BaseReadService<TransactionEntity> {
|
||||
|
@ -14,4 +18,16 @@ export class TransactionReadService extends BaseReadService<TransactionEntity> {
|
|||
) {
|
||||
super(repo);
|
||||
}
|
||||
|
||||
async getPendingOrderId() {
|
||||
const transactions = await this.repo.find({
|
||||
where: {
|
||||
status: STATUS.PENDING,
|
||||
payment_type: TransactionPaymentType.MIDTRANS,
|
||||
},
|
||||
select: ['id', 'invoice_date'],
|
||||
});
|
||||
|
||||
return transactions;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ import { PaymentMethodDataService } from '../payment-method/data/services/paymen
|
|||
import { PaymentMethodModel } from '../payment-method/data/models/payment-method.model';
|
||||
|
||||
@Module({
|
||||
exports: [TransactionReadService],
|
||||
imports: [
|
||||
ConfigModule.forRoot(),
|
||||
TypeOrmModule.forFeature(
|
||||
|
|
Loading…
Reference in New Issue