fix: order history and add filter

This commit is contained in:
Ilham Dwi Pratama S 2021-12-29 09:23:33 +07:00
parent e882237a8a
commit 2948462ff8
3 changed files with 29 additions and 5 deletions

View File

@ -117,11 +117,13 @@ export class TransactionController {
async getHistoryTransactionUser( async getHistoryTransactionUser(
@Query('page') page: number, @Query('page') page: number,
@Query('pageSize') pageSize: number, @Query('pageSize') pageSize: number,
@Query('transactionDate') transactionDate: string,
@Request() req, @Request() req,
) { ) {
const data = await this.transactionService.transactionHistoryByUser( const data = await this.transactionService.transactionHistoryByUser(
page, page,
req.user.userId, req.user.userId,
transactionDate,
pageSize, pageSize,
); );

View File

@ -765,14 +765,32 @@ export class TransactionService {
async transactionHistoryByUser( async transactionHistoryByUser(
page: number, page: number,
user: string, user: string,
transactionDate: string,
pageSize?: number, 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)
} else {
userBySupperior.push(user)
}
if (transactionDate) {
filterTransactionDate = transactionDate.split(',').map((data) => {
return data.trim();
});
}
const baseQuery = this.transactionRepository const baseQuery = this.transactionRepository
.createQueryBuilder('transaction') .createQueryBuilder('transaction')
.select('transaction.id', 'id') .select('transaction.id', 'id')
.addSelect('transaction.created_at', 'created_at') .addSelect('transaction.created_at', 'created_at')
.where('transaction.user = :id and transaction.type = 1', { .where('transaction.user = IN (:...id) and transaction.type = 1', {
id: user, id: userBySupperior,
}) })
.leftJoin('transaction.product_price', 'product_price') .leftJoin('transaction.product_price', 'product_price')
.leftJoin('product_price.product', 'product') .leftJoin('product_price.product', 'product')
@ -780,8 +798,12 @@ export class TransactionService {
.addSelect('product.name', 'name') .addSelect('product.name', 'name')
.addSelect('product.id', 'product_id'); .addSelect('product.id', 'product_id');
// .leftJoinAndSelect('transaction.product_price', 'product_price') if (transactionDate && filterTransactionDate.length > 0) {
// .leftJoinAndSelect('product_price.product', 'product'); baseQuery.where('transaction.created_at between :startDate and :enDate', {
startDate: filterTransactionDate[0],
enDate: filterTransactionDate[0],
});
}
const data = await baseQuery const data = await baseQuery
.offset(page * (pageSize || 10)) .offset(page * (pageSize || 10))

View File

@ -238,7 +238,7 @@ export class UsersService {
where: { where: {
id: id, id: id,
}, },
relations: ['superior'], relations: ['superior','roles'],
}); });
} catch (e) { } catch (e) {
if (e instanceof EntityNotFoundError) { if (e instanceof EntityNotFoundError) {