Merge branch 'development' into 'devops-staging'

Development

See merge request empatnusabangsa/ppob/ppob-backend!68
This commit is contained in:
ilham dwi pratama 2021-12-26 16:02:45 +00:00
commit 645eb45996
8 changed files with 149 additions and 68 deletions

View File

@ -42,7 +42,12 @@ export class ProductHistoryPriceService {
async findById(id: string) { async findById(id: string) {
try { try {
return await this.productHistoryPriceService.findOneOrFail(id); return await this.productHistoryPriceService.findOneOrFail({
where: {
id: id,
},
relations: ['product'],
});
} catch (e) { } catch (e) {
if (e instanceof EntityNotFoundError) { if (e instanceof EntityNotFoundError) {
throw new HttpException( throw new HttpException(

View File

@ -1,5 +1,5 @@
import { HttpException, HttpStatus } from '@nestjs/common'; import { HttpException, HttpStatus } from '@nestjs/common';
import { EntityNotFoundError, Repository } from 'typeorm'; import { EntityNotFoundError, IsNull, Repository } from 'typeorm';
import { Product } from './entities/product.entity'; import { Product } from './entities/product.entity';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { CreateProductDto } from './dto/product/create-product.dto'; import { CreateProductDto } from './dto/product/create-product.dto';
@ -87,16 +87,25 @@ export class ProductService {
partner: partnerData.id, partner: partnerData.id,
}, },
}); });
} else { } else {
dataHistoryPrice = await this.productHistoryPrice.findOne({ dataHistoryPrice = await this.productHistoryPrice.findOne({
product: productData, product: productData,
partner: IsNull(),
}); });
} }
if (!dataHistoryPrice) { if (!dataHistoryPrice) {
return; await this.productHistoryPrice.insert({
} product: productData,
mark_up_price: it[4],
price: it[3],
type: productType.NORMAL,
startDate: new Date(),
partner: it[6] != '-' ? partnerData : null,
});
} else {
dataHistoryPrice.endDate = new Date(); dataHistoryPrice.endDate = new Date();
await this.productHistoryPrice.save(dataHistoryPrice); await this.productHistoryPrice.save(dataHistoryPrice);
@ -108,6 +117,7 @@ export class ProductService {
startDate: new Date(), startDate: new Date(),
partner: it[6] != '-' ? partnerData : null, partner: it[6] != '-' ? partnerData : null,
}); });
}
} else { } else {
let partnerData; let partnerData;
if (it[6] != '-' && it[6] != '') { if (it[6] != '-' && it[6] != '') {

View File

@ -58,6 +58,16 @@ export class Transactions extends BaseModel {
}) })
phone_number: string; phone_number: string;
@Column({
nullable: true,
})
request_json: string;
@Column({
nullable: true,
})
callback_json: string;
mark_up_price: number; mark_up_price: number;
userData: UserDetail; userData: UserDetail;

View File

@ -13,6 +13,7 @@ import {
import { TransactionService } from './transaction.service'; import { TransactionService } from './transaction.service';
import { DistributeTransactionDto } from './dto/distribute-transaction.dto'; import { DistributeTransactionDto } from './dto/distribute-transaction.dto';
import { FastifyRequest } from 'fastify'; import { FastifyRequest } from 'fastify';
import { Public } from '../auth/public.decorator';
@Controller({ @Controller({
path: 'ppob_callback', path: 'ppob_callback',
@ -23,6 +24,7 @@ export class PpobCallbackController {
constructor(private readonly transactionService: TransactionService) {} constructor(private readonly transactionService: TransactionService) {}
@Public()
@Get() @Get()
async get(@Req() request: FastifyRequest) { async get(@Req() request: FastifyRequest) {
const response = request.query; const response = request.query;
@ -30,12 +32,16 @@ export class PpobCallbackController {
if (response['statuscode'] == 2) { if (response['statuscode'] == 2) {
//TODO: UPDATE GAGAL //TODO: UPDATE GAGAL
const updateTransaction = const updateTransaction =
await this.transactionService.callbackOrderFailed(response['clientid']); await this.transactionService.callbackOrderFailed(
response['clientid'],
response,
);
} else { } else {
//TODO: UPDATE BERHASIL //TODO: UPDATE BERHASIL
const updateTransaction = const updateTransaction =
await this.transactionService.callbackOrderSuccess( await this.transactionService.callbackOrderSuccess(
response['clientid'], response['clientid'],
response,
); );
} }
this.logger.log({ this.logger.log({

View File

@ -319,25 +319,8 @@ export class TransactionService {
); );
let supervisorData = []; let supervisorData = [];
let profit = product_price.mark_up_price; let profit = product_price.mark_up_price;
if (!userData.partner) {
//GET SALES
supervisorData = await this.calculateCommission(
supervisorData,
profit,
userData,
);
profit = supervisorData
.map((item) => {
return item.credit;
})
.reduce((prev, curr) => {
return prev + curr;
}, 0);
}
//GET COA //GET COA
const coaAccount = await this.coaService.findByUser( const coaAccount = await this.coaService.findByUser(
userData.id, userData.id,
@ -360,6 +343,29 @@ export class TransactionService {
`${coaType[coaType.EXPENSE]}-SYSTEM`, `${coaType[coaType.EXPENSE]}-SYSTEM`,
); );
if (!userData.partner) {
//GET SALES
supervisorData = await this.calculateCommission(
supervisorData,
profit,
userData,
);
profit = supervisorData
.map((item) => {
return item.credit;
})
.reduce((prev, curr) => {
return prev + curr;
}, 0);
supervisorData = supervisorData.concat([
{
coa_id: coaExpense.id,
debit: profit,
},
]);
}
if (coaAccount.amount <= product.price) { if (coaAccount.amount <= product.price) {
throw new HttpException( throw new HttpException(
{ {
@ -406,10 +412,6 @@ export class TransactionService {
coa_id: coaSales.id, coa_id: coaSales.id,
credit: product_price.mark_up_price + product_price.price, credit: product_price.mark_up_price + product_price.price,
}, },
{
coa_id: coaExpense.id,
debit: userData.partner ? 0 : profit,
},
].concat(supervisorData), ].concat(supervisorData),
}); });
}); });
@ -645,24 +647,27 @@ export class TransactionService {
return transactionData; return transactionData;
} }
async callbackOrderFailed(supplier_trx_id: string) { async callbackOrderFailed(supplier_trx_id: string, callback: any) {
const dataTransaction = await this.transactionRepository.findOne({ const dataTransaction = await this.transactionRepository.findOne({
where: { where: {
supplier_trx_id: supplier_trx_id, supplier_trx_id: supplier_trx_id,
}, },
}); });
dataTransaction.status = statusTransaction.FAILED; dataTransaction.status = statusTransaction.FAILED;
dataTransaction.callback_json = callback;
await this.transactionRepository.save(dataTransaction); await this.transactionRepository.save(dataTransaction);
} }
async callbackOrderSuccess(supplier_trx_id: string) { async callbackOrderSuccess(supplier_trx_id: string, callback: any) {
const dataTransaction = await this.transactionRepository.findOne({ const dataTransaction = await this.transactionRepository.findOne({
where: { where: {
supplier_trx_id: supplier_trx_id, supplier_trx_id: supplier_trx_id,
}, },
relations: ['product_price'],
}); });
dataTransaction.status = statusTransaction.FAILED; dataTransaction.status = statusTransaction.FAILED;
dataTransaction.callback_json = callback;
const userData = await this.userService.findExist(dataTransaction.user); const userData = await this.userService.findExist(dataTransaction.user);
@ -678,22 +683,6 @@ export class TransactionService {
let profit = product_price.mark_up_price; let profit = product_price.mark_up_price;
if (!userData.partner) {
//GET SALES
supervisorData = await this.calculateCommission(
supervisorData,
profit,
userData,
);
profit = supervisorData
.map((item) => {
return item.credit;
})
.reduce((prev, curr) => {
return prev + curr;
}, 0);
}
//GET COA //GET COA
const coaAccount = await this.coaService.findByUser( const coaAccount = await this.coaService.findByUser(
userData.id, userData.id,
@ -716,6 +705,29 @@ export class TransactionService {
`${coaType[coaType.EXPENSE]}-SYSTEM`, `${coaType[coaType.EXPENSE]}-SYSTEM`,
); );
if (userData.partner) {
//GET SALES
supervisorData = await this.calculateCommission(
supervisorData,
profit,
userData,
);
profit = supervisorData
.map((item) => {
return item.credit;
})
.reduce((prev, curr) => {
return prev + curr;
}, 0);
supervisorData = supervisorData.concat([
{
coa_id: coaExpense.id,
debit: profit,
},
]);
}
try { try {
await this.connection.transaction(async (manager) => { await this.connection.transaction(async (manager) => {
await manager.save(dataTransaction); await manager.save(dataTransaction);
@ -742,10 +754,6 @@ export class TransactionService {
coa_id: coaSales.id, coa_id: coaSales.id,
credit: product_price.mark_up_price + product_price.price, credit: product_price.mark_up_price + product_price.price,
}, },
{
coa_id: coaExpense.id,
debit: userData.partner ? 0 : profit,
},
].concat(supervisorData), ].concat(supervisorData),
}); });
}); });
@ -768,7 +776,7 @@ export class TransactionService {
}) })
.leftJoin('transaction.product_price', 'product_price') .leftJoin('transaction.product_price', 'product_price')
.leftJoin('product_price.product', 'product') .leftJoin('product_price.product', 'product')
.addSelect('product_price.mark_up_price', 'mark_up_price') .addSelect('transaction.amount', 'mark_up_price')
.addSelect('product.name', 'name') .addSelect('product.name', 'name')
.addSelect('product.id', 'product_id'); .addSelect('product.id', 'product_id');

View File

@ -62,7 +62,7 @@ export class PartnerService {
const dataUser = new CreateUserDto(); const dataUser = new CreateUserDto();
dataUser.username = `admin_${partnerData.name}`; dataUser.username = `admin_${partnerData.code}`;
dataUser.name = partnerData.name; dataUser.name = partnerData.name;
dataUser.phone_number = partnerData.phone_number; dataUser.phone_number = partnerData.phone_number;
dataUser.roleId = '21dceea2-416e-4b55-b74c-12605e1f8d1b'; dataUser.roleId = '21dceea2-416e-4b55-b74c-12605e1f8d1b';

View File

@ -230,7 +230,20 @@ export class UsersController {
@Body() updateUserDto: UpdateUserDto, @Body() updateUserDto: UpdateUserDto,
) { ) {
return { return {
data: await this.usersService.updatePassword(id, updateUserDto, req.user), data: await this.usersService.updatePassword(id, updateUserDto),
statusCode: HttpStatus.OK,
message: 'success',
};
}
@Put('change-password-partner/:id')
async updatePasswordPartner(
@Param('id', ParseUUIDPipe) id: string,
@Request() req,
@Body() updateUserDto: UpdateUserDto,
) {
return {
data: await this.usersService.updatePassword(id, updateUserDto),
statusCode: HttpStatus.OK, statusCode: HttpStatus.OK,
message: 'success', message: 'success',
}; };

View File

@ -220,7 +220,12 @@ export class UsersService {
async findExist(id: string) { async findExist(id: string) {
try { try {
return await this.usersRepository.findOneOrFail(id); return await this.usersRepository.findOneOrFail({
where: {
id: id,
},
relations: ['superior'],
});
} catch (e) { } catch (e) {
if (e instanceof EntityNotFoundError) { if (e instanceof EntityNotFoundError) {
throw new HttpException( throw new HttpException(
@ -358,11 +363,7 @@ export class UsersService {
return userData; return userData;
} }
async updatePassword( async updatePassword(id: string, updateUserDto: UpdateUserDto) {
id: string,
updateUserDto: UpdateUserDto,
currentUser: any,
) {
try { try {
const dataUser = await this.usersRepository.findOneOrFail(id); const dataUser = await this.usersRepository.findOneOrFail(id);
dataUser.password = await hashPassword( dataUser.password = await hashPassword(
@ -386,6 +387,34 @@ export class UsersService {
} }
} }
async updatePasswordPartner(id: string, updateUserDto: UpdateUserDto) {
try {
const dataUser = await this.usersRepository.findOneOrFail({
where: {
partner: id,
},
});
dataUser.password = await hashPassword(
updateUserDto.password,
dataUser.salt,
);
const result = await this.usersRepository.save(dataUser);
return dataUser;
} catch (e) {
if (e instanceof EntityNotFoundError) {
throw new HttpException(
{
statusCode: HttpStatus.NOT_FOUND,
error: 'User not found',
},
HttpStatus.NOT_FOUND,
);
} else {
throw e;
}
}
}
setStatus = async (id: string, type: string) => { setStatus = async (id: string, type: string) => {
const userData = new User(); const userData = new User();