fix: order history
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
import {
|
||||
forwardRef,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Inject,
|
||||
Injectable,
|
||||
} from '@nestjs/common';
|
||||
import { forwardRef, HttpException, HttpStatus, Inject } from '@nestjs/common';
|
||||
import { EntityNotFoundError, Repository } from 'typeorm';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { COA } from './entities/coa.entity';
|
||||
import { balanceType, coaType } from '../helper/enum-list';
|
||||
import { coaType } from '../helper/enum-list';
|
||||
import { InputCoaDto } from './dto/input-coa.dto';
|
||||
import { UsersService } from 'src/users/users.service';
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class AddSaldoSupplier {
|
||||
@IsNotEmpty()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||
import { balanceType, coaType, statusTransaction, typeTransaction } from 'src/helper/enum-list';
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
import { statusTransaction, typeTransaction } from 'src/helper/enum-list';
|
||||
import { EntityManager } from 'typeorm';
|
||||
import { Transactions } from '../entities/transactions.entity';
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class DistributeTransactionDto {
|
||||
@IsNotEmpty()
|
||||
|
||||
7
src/transaction/dto/order-transaction-partner.dto.ts
Normal file
7
src/transaction/dto/order-transaction-partner.dto.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
import { OrderTransactionDto } from './order-transaction.dto';
|
||||
|
||||
export class OrderTransactionPartnerDto extends OrderTransactionDto {
|
||||
@IsNotEmpty()
|
||||
trx_id: string;
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class OrderTransactionDto {
|
||||
@IsNotEmpty()
|
||||
productCode: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
phoneNumber: string;
|
||||
destination: string;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Entity, Column } from 'typeorm';
|
||||
import { Column, Entity } from 'typeorm';
|
||||
import { BaseModel } from '../../config/basemodel.entity';
|
||||
import { coaType, balanceType } from '../../helper/enum-list';
|
||||
import { balanceType, coaType } from '../../helper/enum-list';
|
||||
|
||||
@Entity()
|
||||
export class COA extends BaseModel {
|
||||
|
||||
@@ -1,11 +1,4 @@
|
||||
import {
|
||||
Entity,
|
||||
Column,
|
||||
ManyToOne,
|
||||
ManyToMany,
|
||||
JoinTable,
|
||||
OneToOne,
|
||||
} from 'typeorm';
|
||||
import { Column, Entity, ManyToOne } from 'typeorm';
|
||||
import { BaseModel } from '../../config/basemodel.entity';
|
||||
import { COA } from './coa.entity';
|
||||
import { Transactions } from './transactions.entity';
|
||||
|
||||
@@ -1,20 +1,7 @@
|
||||
import {
|
||||
Entity,
|
||||
Column,
|
||||
PrimaryGeneratedColumn,
|
||||
UpdateDateColumn,
|
||||
DeleteDateColumn,
|
||||
VersionColumn,
|
||||
CreateDateColumn,
|
||||
ManyToOne,
|
||||
ManyToMany,
|
||||
JoinTable,
|
||||
} from 'typeorm';
|
||||
import { Column, Entity, ManyToOne } from 'typeorm';
|
||||
import { BaseModel } from '../../config/basemodel.entity';
|
||||
import { statusTransaction, typeTransaction } from '../../helper/enum-list';
|
||||
import { Partner } from '../../users/entities/partner.entity';
|
||||
import { ProductHistoryPrice } from '../../product/entities/product-history-price.entity';
|
||||
import { User } from '../../users/entities/user.entity';
|
||||
import { UserDetail } from '../../users/entities/user_detail.entity';
|
||||
import { TransactionJournal } from './transaction-journal.entity';
|
||||
|
||||
@@ -48,6 +35,11 @@ export class Transactions extends BaseModel {
|
||||
})
|
||||
image_prove: string;
|
||||
|
||||
@Column({
|
||||
nullable: true,
|
||||
})
|
||||
destination: string;
|
||||
|
||||
@Column({
|
||||
nullable: true,
|
||||
})
|
||||
@@ -56,7 +48,7 @@ export class Transactions extends BaseModel {
|
||||
@Column({
|
||||
nullable: true,
|
||||
})
|
||||
phone_number: string;
|
||||
partner_trx_id: string;
|
||||
|
||||
@Column({
|
||||
nullable: true,
|
||||
|
||||
@@ -1,17 +1,5 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
HttpStatus,
|
||||
Logger,
|
||||
Req,
|
||||
} from '@nestjs/common';
|
||||
import { Controller, Get, HttpStatus, Logger, Req } from '@nestjs/common';
|
||||
import { TransactionService } from './transaction.service';
|
||||
import { DistributeTransactionDto } from './dto/distribute-transaction.dto';
|
||||
import { FastifyRequest } from 'fastify';
|
||||
import { Public } from '../auth/public.decorator';
|
||||
|
||||
|
||||
@@ -1,15 +1,4 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
HttpStatus,
|
||||
Param,
|
||||
ParseUUIDPipe,
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
Request,
|
||||
} from '@nestjs/common';
|
||||
import { Body, Controller, Get, HttpStatus, Param, ParseUUIDPipe, Post, Put, Query, Request } from '@nestjs/common';
|
||||
import { TransactionService } from './transaction.service';
|
||||
import { DistributeTransactionDto } from './dto/distribute-transaction.dto';
|
||||
import { OrderTransactionDto } from './dto/order-transaction.dto';
|
||||
@@ -117,15 +106,15 @@ export class TransactionController {
|
||||
async getHistoryTransactionUser(
|
||||
@Query('page') page: number,
|
||||
@Query('pageSize') pageSize: number,
|
||||
@Query('startDate') startDate: string,
|
||||
@Query('endDate') endDate: string,
|
||||
@Query('start') startDate: string,
|
||||
@Query('end') endDate: string,
|
||||
@Request() req,
|
||||
) {
|
||||
const data = await this.transactionService.transactionHistoryByUser(
|
||||
page,
|
||||
req.user.userId,
|
||||
startDate,
|
||||
endDate,
|
||||
startDate == 'null' ? null : startDate,
|
||||
endDate == 'null' ? null : endDate,
|
||||
pageSize,
|
||||
);
|
||||
|
||||
|
||||
@@ -10,12 +10,7 @@ import { CoaService } from './coa.service';
|
||||
import * as uuid from 'uuid';
|
||||
import { uniq } from 'lodash';
|
||||
import { Decimal } from 'decimal.js';
|
||||
import {
|
||||
balanceType,
|
||||
coaType,
|
||||
statusTransaction,
|
||||
typeTransaction,
|
||||
} from '../helper/enum-list';
|
||||
import { balanceType, coaType, statusTransaction, typeTransaction } from '../helper/enum-list';
|
||||
import { ProductService } from '../product/product.service';
|
||||
import { CreateJournalDto } from './dto/create-journal.dto';
|
||||
import { UsersService } from 'src/users/users.service';
|
||||
@@ -28,12 +23,6 @@ import { UserDetail } from '../users/entities/user_detail.entity';
|
||||
import { doTransaction } from '../helper/irs-api';
|
||||
import { ProductHistoryPrice } from '../product/entities/product-history-price.entity';
|
||||
|
||||
interface JournalEntry {
|
||||
coa_id: string;
|
||||
debit?: string;
|
||||
credit?: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class TransactionService {
|
||||
private readonly logger = new Logger(TransactionService.name);
|
||||
@@ -387,7 +376,7 @@ export class TransactionService {
|
||||
transactionData.status = statusTransaction.SUCCESS;
|
||||
transactionData.type = typeTransaction.ORDER;
|
||||
transactionData.product_price = product_price;
|
||||
transactionData.phone_number = orderTransactionDto.phoneNumber;
|
||||
transactionData.destination = orderTransactionDto.destination;
|
||||
await manager.insert(Transactions, transactionData);
|
||||
|
||||
await this.accountingTransaction({
|
||||
@@ -409,6 +398,7 @@ export class TransactionService {
|
||||
debit: product_price.mark_up_price + product_price.price,
|
||||
},
|
||||
{
|
||||
// eslint-disable-next-line camelcase
|
||||
coa_id: coaSales.id,
|
||||
credit: product_price.mark_up_price + product_price.price,
|
||||
},
|
||||
@@ -444,12 +434,14 @@ export class TransactionService {
|
||||
//TODO HIT API SUPPLIER
|
||||
const trxId = Array(6)
|
||||
.fill(null)
|
||||
.map(() => Math.round(Math.random() * 16).toString(16))
|
||||
.map(() => {
|
||||
return Math.round(Math.random() * 16).toString(16);
|
||||
})
|
||||
.join('');
|
||||
|
||||
const hitSupplier = await doTransaction(
|
||||
orderTransactionDto.productCode,
|
||||
orderTransactionDto.phoneNumber,
|
||||
orderTransactionDto.destination,
|
||||
trxId,
|
||||
);
|
||||
|
||||
@@ -465,7 +457,7 @@ export class TransactionService {
|
||||
transactionData.user = userData.id;
|
||||
transactionData.type = typeTransaction.ORDER;
|
||||
transactionData.product_price = product_price;
|
||||
transactionData.phone_number = orderTransactionDto.phoneNumber;
|
||||
transactionData.destination = orderTransactionDto.destination;
|
||||
transactionData.supplier_trx_id = trxId;
|
||||
|
||||
if (!hitSupplier.success) {
|
||||
@@ -487,6 +479,7 @@ export class TransactionService {
|
||||
product_price.endDate = new Date();
|
||||
|
||||
let newProductPrice = new ProductHistoryPrice();
|
||||
|
||||
newProductPrice = product_price;
|
||||
newProductPrice.id = uuid.v4();
|
||||
newProductPrice.price = hitSupplier.harga;
|
||||
@@ -653,6 +646,7 @@ export class TransactionService {
|
||||
supplier_trx_id: supplier_trx_id,
|
||||
},
|
||||
});
|
||||
|
||||
dataTransaction.status = statusTransaction.FAILED;
|
||||
dataTransaction.callback_json = callback;
|
||||
|
||||
@@ -666,6 +660,7 @@ export class TransactionService {
|
||||
},
|
||||
relations: ['product_price'],
|
||||
});
|
||||
|
||||
dataTransaction.status = statusTransaction.FAILED;
|
||||
dataTransaction.callback_json = callback;
|
||||
|
||||
@@ -762,6 +757,51 @@ export class TransactionService {
|
||||
}
|
||||
}
|
||||
|
||||
async withdrawBenefit(user) {
|
||||
const userData = await this.userService.findExist(user);
|
||||
|
||||
const coaProfit = await this.coaService.findByUser(user, coaType.PROFIT);
|
||||
|
||||
const coaBank = await this.coaService.findByName(
|
||||
`${coaType[coaType.BANK]}-SYSTEM`,
|
||||
);
|
||||
|
||||
try {
|
||||
const transactionData = new Transactions();
|
||||
|
||||
transactionData.id = uuid.v4();
|
||||
transactionData.amount = coaProfit.amount;
|
||||
transactionData.user = userData.id;
|
||||
transactionData.status = statusTransaction.APPROVED;
|
||||
transactionData.type = typeTransaction.WITHDRAW;
|
||||
|
||||
await this.connection.transaction(async (manager) => {
|
||||
await manager.insert(Transactions, transactionData);
|
||||
|
||||
await this.accountingTransaction({
|
||||
createTransaction: false,
|
||||
transactionalEntityManager: manager,
|
||||
transaction: transactionData,
|
||||
amount: transactionData.amount,
|
||||
journals: [
|
||||
{
|
||||
coa_id: coaBank.id,
|
||||
credit: transactionData.amount,
|
||||
},
|
||||
{
|
||||
coa_id: coaProfit.id,
|
||||
debit: transactionData.amount,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
return transactionData;
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async transactionHistoryByUser(
|
||||
page: number,
|
||||
user: string,
|
||||
@@ -770,26 +810,48 @@ export class TransactionService {
|
||||
pageSize?: number,
|
||||
) {
|
||||
const userData = await this.userService.findExist(user);
|
||||
let filterTransactionDate;
|
||||
|
||||
let userBySupperior = [];
|
||||
if(userData.roles.id != 'e4dfb6a3-2338-464a-8fb8-5cbc089d4209' && userData.roles.id != '21dceea2-416e-4b55-b74c-12605e1f8d1b'){
|
||||
const getListUser = await this.userService.findBySuperrior(userData.id,100)
|
||||
userBySupperior = getListUser.data.map(x => x.id)
|
||||
|
||||
if (
|
||||
userData.roles.id != 'e4dfb6a3-2338-464a-8fb8-5cbc089d4209' &&
|
||||
userData.roles.id != '21dceea2-416e-4b55-b74c-12605e1f8d1b'
|
||||
) {
|
||||
let roleNumber;
|
||||
|
||||
if (userData.roles.id == '3196cdf4-ae5f-4677-9bcd-98be35c72321') {
|
||||
roleNumber = 3;
|
||||
} else if (userData.roles.id == '3196cdf4-ae5f-4677-9bcd-98be35c72322') {
|
||||
roleNumber = 2;
|
||||
} else if (userData.roles.id == 'e4dfb6a3-2348-464a-8fb8-5cbc089d4209') {
|
||||
roleNumber = 1;
|
||||
}
|
||||
|
||||
userBySupperior = await this.userService.findAllSubordinate(
|
||||
userData.id,
|
||||
roleNumber,
|
||||
);
|
||||
} else {
|
||||
userBySupperior.push(user)
|
||||
userBySupperior.push(user);
|
||||
}
|
||||
|
||||
const baseQuery = this.transactionRepository
|
||||
.createQueryBuilder('transaction')
|
||||
.select('transaction.id', 'id')
|
||||
.addSelect('transaction.created_at', 'created_at')
|
||||
.where('transaction.user = IN (:...id) and transaction.type = 1', {
|
||||
.where('transaction.user IN (:...id) and transaction.type = 1', {
|
||||
id: userBySupperior,
|
||||
})
|
||||
.leftJoinAndMapOne(
|
||||
'transaction.userData',
|
||||
UserDetail,
|
||||
'userData',
|
||||
'userData.user = transaction.user',
|
||||
)
|
||||
.leftJoin('transaction.product_price', 'product_price')
|
||||
.leftJoin('product_price.product', 'product')
|
||||
.addSelect('transaction.amount', 'mark_up_price')
|
||||
.addSelect('transaction.amount', 'price')
|
||||
.addSelect('userData.name', 'buyer')
|
||||
.addSelect('product.name', 'name')
|
||||
.addSelect('product.id', 'product_id');
|
||||
|
||||
@@ -921,6 +983,7 @@ export class TransactionService {
|
||||
data,
|
||||
count: totalData,
|
||||
};
|
||||
|
||||
return this.transactionRepository.findAndCount({
|
||||
skip: page * (pageSize || 10),
|
||||
take: pageSize || 10,
|
||||
@@ -934,51 +997,6 @@ export class TransactionService {
|
||||
});
|
||||
}
|
||||
|
||||
async withdrawBenefit(user) {
|
||||
const userData = await this.userService.findExist(user);
|
||||
|
||||
const coaProfit = await this.coaService.findByUser(user, coaType.PROFIT);
|
||||
|
||||
const coaBank = await this.coaService.findByName(
|
||||
`${coaType[coaType.BANK]}-SYSTEM`,
|
||||
);
|
||||
|
||||
try {
|
||||
const transactionData = new Transactions();
|
||||
|
||||
transactionData.id = uuid.v4();
|
||||
transactionData.amount = coaProfit.amount;
|
||||
transactionData.user = userData.id;
|
||||
transactionData.status = statusTransaction.APPROVED;
|
||||
transactionData.type = typeTransaction.WITHDRAW;
|
||||
|
||||
await this.connection.transaction(async (manager) => {
|
||||
await manager.insert(Transactions, transactionData);
|
||||
|
||||
await this.accountingTransaction({
|
||||
createTransaction: false,
|
||||
transactionalEntityManager: manager,
|
||||
transaction: transactionData,
|
||||
amount: transactionData.amount,
|
||||
journals: [
|
||||
{
|
||||
coa_id: coaBank.id,
|
||||
credit: transactionData.amount,
|
||||
},
|
||||
{
|
||||
coa_id: coaProfit.id,
|
||||
debit: transactionData.amount,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
return transactionData;
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async getTotalSell() {
|
||||
const { total_amount } = await this.transactionRepository
|
||||
.createQueryBuilder('transactions')
|
||||
|
||||
Reference in New Issue
Block a user