Compare commits
No commits in common. "841f8889ec04559ebaec4f5bec7f66c9e4b8ad12" and "bc8476a56e0901b5cea5e9268a12577675aec67f" have entirely different histories.
841f8889ec
...
bc8476a56e
|
@ -1,25 +1,12 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { EventBus } from '@nestjs/cqrs';
|
||||||
import { mappingMidtransTransaction } from '../../domain/usecases/helpers/mapping-transaction.helper';
|
import { mappingMidtransTransaction } from '../../domain/usecases/helpers/mapping-transaction.helper';
|
||||||
import { Snap } from 'midtrans-client';
|
import { Snap } from 'midtrans-client';
|
||||||
import { MidtransStatus } from '../../domain/entities/midtrans-callback.event';
|
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()
|
@Injectable()
|
||||||
export class MidtransService {
|
export class MidtransService {
|
||||||
constructor(private transaction: TransactionReadService) {}
|
constructor(private eventBus: EventBus) {}
|
||||||
|
|
||||||
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() {
|
get midtransInstance() {
|
||||||
return new Snap({
|
return new Snap({
|
||||||
|
@ -33,28 +20,6 @@ export class MidtransService {
|
||||||
return await this.midtransInstance.transaction.status(orderId);
|
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> {
|
async changeStatus(orderId: string, action: MidtransStatus): Promise<any> {
|
||||||
return await this.midtransInstance.transaction[action](orderId);
|
return await this.midtransInstance.transaction[action](orderId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,27 +46,6 @@ 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')
|
@Get(':id/change-status')
|
||||||
@ApiQuery({ name: 'status', enum: MidtransStatus })
|
@ApiQuery({ name: 'status', enum: MidtransStatus })
|
||||||
async cancel(
|
async cancel(
|
||||||
|
|
|
@ -3,20 +3,12 @@ import { CqrsModule } from '@nestjs/cqrs';
|
||||||
import { MidtransController } from './infrastructure/midtrans.controller';
|
import { MidtransController } from './infrastructure/midtrans.controller';
|
||||||
import { MidtransService } from './data/services/midtrans.service';
|
import { MidtransService } from './data/services/midtrans.service';
|
||||||
import { Global, Module } from '@nestjs/common';
|
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()
|
@Global()
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [ConfigModule.forRoot(), CqrsModule],
|
||||||
ConfigModule.forRoot(),
|
|
||||||
CqrsModule,
|
|
||||||
TypeOrmModule.forFeature([TransactionModel], CONNECTION_NAME.DEFAULT),
|
|
||||||
],
|
|
||||||
controllers: [MidtransController],
|
controllers: [MidtransController],
|
||||||
providers: [MidtransService, TransactionReadService],
|
providers: [MidtransService],
|
||||||
exports: [MidtransService],
|
exports: [MidtransService],
|
||||||
})
|
})
|
||||||
export class MidtransModule {}
|
export class MidtransModule {}
|
||||||
|
|
|
@ -2,13 +2,9 @@ import { Injectable } from '@nestjs/common';
|
||||||
import { TransactionEntity } from '../../domain/entities/transaction.entity';
|
import { TransactionEntity } from '../../domain/entities/transaction.entity';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { TransactionModel } from '../models/transaction.model';
|
import { TransactionModel } from '../models/transaction.model';
|
||||||
import {
|
import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants';
|
||||||
CONNECTION_NAME,
|
|
||||||
STATUS,
|
|
||||||
} from 'src/core/strings/constants/base.constants';
|
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { BaseReadService } from 'src/core/modules/data/service/base-read.service';
|
import { BaseReadService } from 'src/core/modules/data/service/base-read.service';
|
||||||
import { TransactionPaymentType } from '../../constants';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TransactionReadService extends BaseReadService<TransactionEntity> {
|
export class TransactionReadService extends BaseReadService<TransactionEntity> {
|
||||||
|
@ -18,16 +14,4 @@ export class TransactionReadService extends BaseReadService<TransactionEntity> {
|
||||||
) {
|
) {
|
||||||
super(repo);
|
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,7 +37,6 @@ import { PaymentMethodDataService } from '../payment-method/data/services/paymen
|
||||||
import { PaymentMethodModel } from '../payment-method/data/models/payment-method.model';
|
import { PaymentMethodModel } from '../payment-method/data/models/payment-method.model';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
exports: [TransactionReadService],
|
|
||||||
imports: [
|
imports: [
|
||||||
ConfigModule.forRoot(),
|
ConfigModule.forRoot(),
|
||||||
TypeOrmModule.forFeature(
|
TypeOrmModule.forFeature(
|
||||||
|
|
Loading…
Reference in New Issue