From ee42cc9acdef86b9fbf4959644aae46e240b5a38 Mon Sep 17 00:00:00 2001 From: Ilham Dwi Pratama S Date: Wed, 22 Dec 2021 07:33:07 +0700 Subject: [PATCH 01/12] fix product --- src/product/product.service.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/product/product.service.ts b/src/product/product.service.ts index a850649..5077910 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -77,9 +77,9 @@ export class ProductService { 'product.code', 'sub_categories.name', 'category.name', - ]); - // .addSelect('current_price.price') - // .addSelect('(current_price.price + current_price.mark_up_price) as mark_up_price'); + ]) + .addSelect('current_price.price') + .addSelect('(current_price.price + current_price.mark_up_price) as mark_up_price'); if (subCategories != 'null' && subCategories) { baseQuery.andWhere('product.sub_categories_id = :id', { @@ -94,9 +94,9 @@ export class ProductService { // } const data = await baseQuery - .skip(page * 10) - .take(10) - .getMany(); + .offset(page * 10) + .limit(10) + .getRawMany(); const totalData = await baseQuery.getCount(); From d8b9202fb77456f616cb7c4ad3e3ae9b958bdb6c Mon Sep 17 00:00:00 2001 From: ilham Date: Wed, 22 Dec 2021 09:53:46 +0700 Subject: [PATCH 02/12] add: unfinish product --- src/product/product.service.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 061d23b..a850649 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -70,7 +70,16 @@ export class ProductService { 'product.priceHistory', 'current_price', 'current_price.partner_id is null', - ); + ) + .select(['product.id']) + .addSelect([ + 'product.name', + 'product.code', + 'sub_categories.name', + 'category.name', + ]); + // .addSelect('current_price.price') + // .addSelect('(current_price.price + current_price.mark_up_price) as mark_up_price'); if (subCategories != 'null' && subCategories) { baseQuery.andWhere('product.sub_categories_id = :id', { From ad81712bfa65414212345e9455850ad38f5f082a Mon Sep 17 00:00:00 2001 From: caturbgs Date: Wed, 22 Dec 2021 13:35:59 +0700 Subject: [PATCH 03/12] feat: add filter on endpoint product all --- src/product/product.controller.ts | 8 +---- src/product/product.service.ts | 48 ++++++++++++++++---------- src/users/supplier/supplier.service.ts | 20 +++++++++++ 3 files changed, 51 insertions(+), 25 deletions(-) diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index 1dcc540..52d3c0e 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -73,15 +73,9 @@ export class ProductController { async findAll( @Query('page') page: number, @Query('sub-category') subcategory: string, - @Query('category') category: string, @Query('supplier') supplier: string, ) { - const data = await this.productService.findAll( - page, - supplier, - category, - subcategory, - ); + const data = await this.productService.findAll(page, supplier, subcategory); return { ...data, diff --git a/src/product/product.service.ts b/src/product/product.service.ts index a850649..007a995 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -1,9 +1,4 @@ -import { - HttpException, - HttpStatus, - Injectable, - UnauthorizedException, -} from '@nestjs/common'; +import { HttpException, HttpStatus } from '@nestjs/common'; import { EntityNotFoundError, Repository } from 'typeorm'; import { Product } from './entities/product.entity'; import { InjectRepository } from '@nestjs/typeorm'; @@ -15,7 +10,6 @@ import { productType } from '../helper/enum-list'; import { UpdatePriceProductDto } from './dto/product/update-price-product.dto'; import { UsersService } from '../users/users.service'; import { SupplierService } from '../users/supplier/supplier.service'; -import { type } from 'os'; export class ProductService { constructor( @@ -53,18 +47,30 @@ export class ProductService { return this.productRepository.findOneOrFail(result.identifiers[0].id); } - async findAll(page, supplier, categories, subCategories) { - if (supplier == 'null' || !supplier) { - supplier = (await this.supplierService.findByActive()).id; + async findAll(page: number, supplier: string, subCategories: string) { + let filterSupplier = []; + let filterSubCategories = []; + + if (supplier !== 'null') { + filterSupplier = supplier.split(',').map((data) => data.trim()); } + if (subCategories !== 'null') { + filterSubCategories = subCategories.split(',').map((data) => data.trim()); + } + // if (supplier.length > 0) { + // const dataSupplier = await this.supplierService.findByActiveAll(); + // supplier = dataSupplier.map((item) => item.id); + // } const baseQuery = this.productRepository .createQueryBuilder('product') .leftJoin('product.sub_categories', 'sub_categories') .leftJoin('sub_categories.category', 'category') - .where(`product.supplier_id = :supplier_id`, { - supplier_id: supplier, - }) + .leftJoin('product.supplier', 'supplier') + .where('supplier.status = :status', { status: true }) + // .where(`product.supplier_id = :supplier_id`, { + // supplier_id: In(supplier), + // }) .leftJoinAndMapOne( 'product.currentPrice', 'product.priceHistory', @@ -78,12 +84,18 @@ export class ProductService { 'sub_categories.name', 'category.name', ]); - // .addSelect('current_price.price') - // .addSelect('(current_price.price + current_price.mark_up_price) as mark_up_price'); + // .addSelect('current_price.price') + // .addSelect('(current_price.price + current_price.mark_up_price) as mark_up_price'); - if (subCategories != 'null' && subCategories) { - baseQuery.andWhere('product.sub_categories_id = :id', { - id: subCategories, + if (filterSubCategories.length > 0) { + baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', { + subCategoryId: filterSubCategories, + }); + } + + if (filterSupplier.length > 0) { + baseQuery.where('supplier.id IN (:...supplierId)', { + supplierId: filterSupplier, }); } diff --git a/src/users/supplier/supplier.service.ts b/src/users/supplier/supplier.service.ts index 00ff8c0..13699cf 100644 --- a/src/users/supplier/supplier.service.ts +++ b/src/users/supplier/supplier.service.ts @@ -199,4 +199,24 @@ export class SupplierService { } } } + + async findByActiveAll() { + try { + return await this.supplierRepository.find({ + status: true, + }); + } catch (e) { + if (e instanceof EntityNotFoundError) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_FOUND, + error: 'Supplier Data not found', + }, + HttpStatus.NOT_FOUND, + ); + } else { + throw e; + } + } + } } From 41856f7a02e912f3f43eaeecc32d515ca3bf4108 Mon Sep 17 00:00:00 2001 From: caturbgs Date: Wed, 22 Dec 2021 13:42:38 +0700 Subject: [PATCH 04/12] feat: add filter on endpoint product by sub category --- src/product/product-sub-categories.service.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/product/product-sub-categories.service.ts b/src/product/product-sub-categories.service.ts index ffc0492..2d87321 100644 --- a/src/product/product-sub-categories.service.ts +++ b/src/product/product-sub-categories.service.ts @@ -1,5 +1,5 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; -import { EntityNotFoundError, Repository } from 'typeorm'; +import { EntityNotFoundError, In, Repository } from 'typeorm'; import { ProductSubCategories } from './entities/product-sub-category.entity'; import { InjectRepository } from '@nestjs/typeorm'; import { CreateSubCategoriesProductDto } from './dto/sub-categories/create-sub-categories-product.dto'; @@ -45,13 +45,19 @@ export class ProductSubCategoriesService { } async findAll(page, category) { + let filterCategories = []; + + if (category !== 'null') { + filterCategories = category.split(',').map((data) => data.trim()); + } + const baseQuery = this.productSubCategoriesRepository .createQueryBuilder('product_sub_categories') .leftJoinAndSelect('product_sub_categories.category', 'category'); - if (category != 'null' && category) { + if (filterCategories.length > 0) { baseQuery.where({ - category: category, + category: In(filterCategories), }); } From ff2205ea03b122a0d5d530ff2a6a58900c5c7c82 Mon Sep 17 00:00:00 2001 From: caturbgs Date: Wed, 22 Dec 2021 14:03:57 +0700 Subject: [PATCH 05/12] feat: rewrite filter for product --- src/product/product-sub-categories.service.ts | 12 +++--------- src/product/product.controller.ts | 11 +++++++++-- src/product/product.service.ts | 19 +++++-------------- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/product/product-sub-categories.service.ts b/src/product/product-sub-categories.service.ts index 2d87321..be2c2e7 100644 --- a/src/product/product-sub-categories.service.ts +++ b/src/product/product-sub-categories.service.ts @@ -44,20 +44,14 @@ export class ProductSubCategoriesService { ); } - async findAll(page, category) { - let filterCategories = []; - - if (category !== 'null') { - filterCategories = category.split(',').map((data) => data.trim()); - } - + async findAll(page, category: []) { const baseQuery = this.productSubCategoriesRepository .createQueryBuilder('product_sub_categories') .leftJoinAndSelect('product_sub_categories.category', 'category'); - if (filterCategories.length > 0) { + if (category.length > 0) { baseQuery.where({ - category: In(filterCategories), + category: In(category), }); } diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index 52d3c0e..0539d81 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -75,7 +75,11 @@ export class ProductController { @Query('sub-category') subcategory: string, @Query('supplier') supplier: string, ) { - const data = await this.productService.findAll(page, supplier, subcategory); + const data = await this.productService.findAll( + page, + JSON.parse(supplier), + JSON.parse(subcategory), + ); return { ...data, @@ -155,7 +159,10 @@ export class ProductController { @Query('page') page: number, @Query('category') category: string, ) { - const data = await this.productSubCategoriesService.findAll(page, category); + const data = await this.productSubCategoriesService.findAll( + page, + JSON.parse(category), + ); return { ...data, diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 007a995..2a0cb36 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -47,16 +47,7 @@ export class ProductService { return this.productRepository.findOneOrFail(result.identifiers[0].id); } - async findAll(page: number, supplier: string, subCategories: string) { - let filterSupplier = []; - let filterSubCategories = []; - - if (supplier !== 'null') { - filterSupplier = supplier.split(',').map((data) => data.trim()); - } - if (subCategories !== 'null') { - filterSubCategories = subCategories.split(',').map((data) => data.trim()); - } + async findAll(page: number, supplier: [], subCategories: []) { // if (supplier.length > 0) { // const dataSupplier = await this.supplierService.findByActiveAll(); // supplier = dataSupplier.map((item) => item.id); @@ -87,15 +78,15 @@ export class ProductService { // .addSelect('current_price.price') // .addSelect('(current_price.price + current_price.mark_up_price) as mark_up_price'); - if (filterSubCategories.length > 0) { + if (subCategories.length > 0) { baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', { - subCategoryId: filterSubCategories, + subCategoryId: subCategories, }); } - if (filterSupplier.length > 0) { + if (supplier.length > 0) { baseQuery.where('supplier.id IN (:...supplierId)', { - supplierId: filterSupplier, + supplierId: supplier, }); } From d9ca0888e7d44f9564decc7a01d36303afc91f64 Mon Sep 17 00:00:00 2001 From: caturbgs Date: Wed, 22 Dec 2021 14:19:58 +0700 Subject: [PATCH 06/12] feat: rewrite filter for product again --- src/product/product-sub-categories.service.ts | 11 ++++++++--- src/product/product.controller.ts | 11 ++--------- src/product/product.service.ts | 17 ++++++++++++----- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/product/product-sub-categories.service.ts b/src/product/product-sub-categories.service.ts index be2c2e7..6ad2644 100644 --- a/src/product/product-sub-categories.service.ts +++ b/src/product/product-sub-categories.service.ts @@ -44,14 +44,19 @@ export class ProductSubCategoriesService { ); } - async findAll(page, category: []) { + async findAll(page, category: string) { + let filterCategories; + if (category) { + filterCategories = category.split(',').map((data) => data.trim()); + } + const baseQuery = this.productSubCategoriesRepository .createQueryBuilder('product_sub_categories') .leftJoinAndSelect('product_sub_categories.category', 'category'); - if (category.length > 0) { + if (category && filterCategories.length > 0) { baseQuery.where({ - category: In(category), + category: In(filterCategories), }); } diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index 0539d81..52d3c0e 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -75,11 +75,7 @@ export class ProductController { @Query('sub-category') subcategory: string, @Query('supplier') supplier: string, ) { - const data = await this.productService.findAll( - page, - JSON.parse(supplier), - JSON.parse(subcategory), - ); + const data = await this.productService.findAll(page, supplier, subcategory); return { ...data, @@ -159,10 +155,7 @@ export class ProductController { @Query('page') page: number, @Query('category') category: string, ) { - const data = await this.productSubCategoriesService.findAll( - page, - JSON.parse(category), - ); + const data = await this.productSubCategoriesService.findAll(page, category); return { ...data, diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 2a0cb36..7642e6b 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -47,7 +47,14 @@ export class ProductService { return this.productRepository.findOneOrFail(result.identifiers[0].id); } - async findAll(page: number, supplier: [], subCategories: []) { + async findAll(page: number, supplier: string, subCategories: string) { + let filterSupplier, filterSubCategories; + if (supplier) { + filterSupplier = supplier.split(',').map((data) => data.trim()); + } + if (subCategories) { + filterSubCategories = subCategories.split(',').map((data) => data.trim()); + } // if (supplier.length > 0) { // const dataSupplier = await this.supplierService.findByActiveAll(); // supplier = dataSupplier.map((item) => item.id); @@ -78,15 +85,15 @@ export class ProductService { // .addSelect('current_price.price') // .addSelect('(current_price.price + current_price.mark_up_price) as mark_up_price'); - if (subCategories.length > 0) { + if (subCategories && filterSubCategories.length > 0) { baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', { - subCategoryId: subCategories, + subCategoryId: filterSubCategories, }); } - if (supplier.length > 0) { + if (supplier && filterSupplier.length > 0) { baseQuery.where('supplier.id IN (:...supplierId)', { - supplierId: supplier, + supplierId: filterSupplier, }); } From 15945fa408503825601ae035f7f6cee703058e3e Mon Sep 17 00:00:00 2001 From: caturbgs Date: Wed, 22 Dec 2021 14:24:03 +0700 Subject: [PATCH 07/12] feat: rewrite filter for product again --- src/product/product.controller.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index 52d3c0e..0539d81 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -75,7 +75,11 @@ export class ProductController { @Query('sub-category') subcategory: string, @Query('supplier') supplier: string, ) { - const data = await this.productService.findAll(page, supplier, subcategory); + const data = await this.productService.findAll( + page, + JSON.parse(supplier), + JSON.parse(subcategory), + ); return { ...data, @@ -155,7 +159,10 @@ export class ProductController { @Query('page') page: number, @Query('category') category: string, ) { - const data = await this.productSubCategoriesService.findAll(page, category); + const data = await this.productSubCategoriesService.findAll( + page, + JSON.parse(category), + ); return { ...data, From 16058db62c6fdca60e9b0f13895e266d1b64f472 Mon Sep 17 00:00:00 2001 From: caturbgs Date: Wed, 22 Dec 2021 14:34:49 +0700 Subject: [PATCH 08/12] feat: rewrite filter for product again --- src/product/product.controller.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index 0539d81..f26f7e0 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -77,8 +77,8 @@ export class ProductController { ) { const data = await this.productService.findAll( page, - JSON.parse(supplier), - JSON.parse(subcategory), + supplier == 'null' ? null : supplier, + subcategory == 'null' ? null : subcategory, ); return { @@ -161,7 +161,7 @@ export class ProductController { ) { const data = await this.productSubCategoriesService.findAll( page, - JSON.parse(category), + category == 'null' ? null : category, ); return { From 0bb1800696973137952de22eb76fb5c9b82efeda Mon Sep 17 00:00:00 2001 From: ilham Date: Wed, 22 Dec 2021 14:56:31 +0700 Subject: [PATCH 09/12] fix: create partner --- src/users/dto/create-partner.dto.ts | 3 +++ src/users/partner/partner.service.ts | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/users/dto/create-partner.dto.ts b/src/users/dto/create-partner.dto.ts index 2fbac10..a53785f 100644 --- a/src/users/dto/create-partner.dto.ts +++ b/src/users/dto/create-partner.dto.ts @@ -15,4 +15,7 @@ export class CreatePartnerDto { @IsNotEmpty() password_account: string; + + @IsNotEmpty() + phone_number: string; } diff --git a/src/users/partner/partner.service.ts b/src/users/partner/partner.service.ts index 2373826..b956648 100644 --- a/src/users/partner/partner.service.ts +++ b/src/users/partner/partner.service.ts @@ -59,12 +59,13 @@ export class PartnerService { const dataUser = new CreateUserDto(); dataUser.username = `admin_${partnerData.name}`; - dataUser.username = partnerData.name; + dataUser.name = partnerData.name; dataUser.phone_number = partnerData.phone_number; dataUser.roleId = '21dceea2-416e-4b55-b74c-12605e1f8d1b'; dataUser.superior = false; dataUser.partner = partnerData; dataUser.password = createPartnerDto.password_account; + dataUser.phone_number = createPartnerDto.phone_number; await this.userService.create(dataUser, currentUser); From a0ee6b9040d9efda7acc0d7d658c1dfc44913505 Mon Sep 17 00:00:00 2001 From: ilham Date: Wed, 22 Dec 2021 15:17:07 +0700 Subject: [PATCH 10/12] fix: get product for partner and for get --- src/product/product.controller.ts | 4 +-- src/product/product.service.ts | 41 ++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index f26f7e0..557857a 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -125,13 +125,13 @@ export class ProductController { async findByCategories( @Query('page') page: number, @Query('pageSize') pageSize: number, - @Query('categories') categories: string, + @Query('sub-category') subcategory: string, @Request() req, ) { const data = await this.productService.findAllForPartner( page, pageSize, - categories, + subcategory, req.user.username, ); diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 89aad86..f27966b 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -73,7 +73,7 @@ export class ProductService { 'product.currentPrice', 'product.priceHistory', 'current_price', - 'current_price.partner_id is null', + 'current_price.partner_id is null and current_price.end_date is NULL', ) .select(['product.id']) .addSelect([ @@ -83,7 +83,9 @@ export class ProductService { 'category.name', ]) .addSelect('current_price.price') - .addSelect('(current_price.price + current_price.mark_up_price) as mark_up_price'); + .addSelect( + '(current_price.price + current_price.mark_up_price) as mark_up_price', + ); if (subCategories && filterSubCategories.length > 0) { baseQuery.where('product.sub_categories_id IN (:...subCategoryId)', { @@ -190,19 +192,33 @@ export class ProductService { async findAllForPartner( page: number, pageSize: number, - categories: string, + subCategories: string, username: string, ) { const user = await this.usersService.findOneByUsername(username); const supplier = await this.supplierService.findByActive(); + let filterSupplier, filterSubCategories; + + if (subCategories) { + filterSubCategories = subCategories.split(',').map((data) => data.trim()); + } else { + throw new HttpException( + { + statusCode: HttpStatus.NOT_FOUND, + error: 'Sub Categories not inlcude', + }, + HttpStatus.NOT_FOUND, + ); + } + const baseQuery = this.productRepository .createQueryBuilder('product') .leftJoin('product.sub_categories', 'sub_categories') .where( - `sub_categories.category_id = :id and product.supplier_id = :supplier_id and product.status = 'ACTIVE'`, + `product.sub_categories_id IN (:...subCategoryId) and product.supplier_id = :supplier_id and product.status = 'ACTIVE'`, { - id: categories, + subCategoryId: filterSubCategories, supplier_id: supplier.id, }, ) @@ -211,13 +227,20 @@ export class ProductService { 'product.priceHistory', 'current_price', 'current_price.partner_id = :id_partner and current_price.end_date is NULL', + { + id_partner: user.partner.id, + }, ) - .setParameter('id_partner', user.partner.id); + .select(['product.id']) + .addSelect(['product.name', 'product.code', 'sub_categories.name']) + .addSelect( + '(current_price.price + current_price.mark_up_price) as price', + ); const data = await baseQuery - .skip(page * pageSize) - .take(pageSize) - .getMany(); + .offset(page * 10) + .limit(10) + .getRawMany(); const totalData = await baseQuery.getCount(); From cdd6d39c90b2879da2a3a0ff99f1ad8193d200c7 Mon Sep 17 00:00:00 2001 From: caturbgs Date: Wed, 22 Dec 2021 14:47:04 +0700 Subject: [PATCH 11/12] feat: add status product column --- src/product/product.service.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/product/product.service.ts b/src/product/product.service.ts index f27966b..e1c1b0a 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -49,11 +49,17 @@ export class ProductService { async findAll(page: number, supplier: string, subCategories: string) { let filterSupplier, filterSubCategories; + if (supplier) { - filterSupplier = supplier.split(',').map((data) => data.trim()); + filterSupplier = supplier.split(',').map((data) => { + return data.trim(); + }); } + if (subCategories) { - filterSubCategories = subCategories.split(',').map((data) => data.trim()); + filterSubCategories = subCategories.split(',').map((data) => { + return data.trim(); + }); } // if (supplier.length > 0) { // const dataSupplier = await this.supplierService.findByActiveAll(); @@ -81,6 +87,7 @@ export class ProductService { 'product.code', 'sub_categories.name', 'category.name', + 'product.status', ]) .addSelect('current_price.price') .addSelect( From c3df45dc9a3afe84ddb3b1cd32f18df4bcb82e27 Mon Sep 17 00:00:00 2001 From: caturbgs Date: Wed, 22 Dec 2021 15:23:05 +0700 Subject: [PATCH 12/12] feat: add page size in all the rest API --- src/configurable/commission.service.ts | 8 ++-- src/configurable/configurable.controller.ts | 23 +++++++--- src/configurable/roles.service.ts | 12 ++--- .../history-price/history-price.service.ts | 5 ++- src/product/product-categories.service.ts | 6 +-- src/product/product-sub-categories.service.ts | 6 +-- src/product/product.controller.ts | 18 +++++++- src/product/product.service.ts | 30 +++++-------- src/transaction/transaction.controller.ts | 31 +++++++------ src/transaction/transaction.service.ts | 44 ++++++++++--------- src/users/partner/partner.service.ts | 14 ++---- src/users/supplier/supplier.service.ts | 14 ++---- src/users/users.controller.ts | 26 ++++++++--- src/users/users.service.ts | 22 +++------- 14 files changed, 135 insertions(+), 124 deletions(-) diff --git a/src/configurable/commission.service.ts b/src/configurable/commission.service.ts index 4b605a4..cf10d3b 100644 --- a/src/configurable/commission.service.ts +++ b/src/configurable/commission.service.ts @@ -1,8 +1,6 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { EntityNotFoundError, Repository } from 'typeorm'; -import { Roles } from './entities/roles.entity'; import { InjectRepository } from '@nestjs/typeorm'; -import { UpdateUserDto } from '../users/dto/update-user.dto'; import { CommissionSetting } from './entities/commission_setting.entity'; @Injectable() @@ -12,10 +10,10 @@ export class CommissionService { private commissionRepository: Repository, ) {} - findAllCommission(page) { + findAllCommission(page, pageSize?) { return this.commissionRepository.findAndCount({ - skip: page * 10, - take: 10, + skip: page * (pageSize || 10), + take: pageSize || 10, order: { version: 'DESC', }, diff --git a/src/configurable/configurable.controller.ts b/src/configurable/configurable.controller.ts index e4474da..c814ebe 100644 --- a/src/configurable/configurable.controller.ts +++ b/src/configurable/configurable.controller.ts @@ -30,8 +30,11 @@ export class ConfigurableController { ) {} @Get('/roles') - async findAll(@Query('page') page: number) { - const [data, count] = await this.roleService.findAllRoles(page); + async findAll( + @Query('page') page: number, + @Query('pageSize') pageSize: number, + ) { + const [data, count] = await this.roleService.findAllRoles(page, pageSize); return { data, @@ -42,8 +45,14 @@ export class ConfigurableController { } @Get('/commission') - async findCommission(@Query('page') page: number) { - const [data, count] = await this.commissionService.findAllCommission(page); + async findCommission( + @Query('page') page: number, + @Query('pageSize') pageSize: number, + ) { + const [data, count] = await this.commissionService.findAllCommission( + page, + pageSize, + ); return { data, @@ -54,9 +63,13 @@ export class ConfigurableController { } @Get('/roles/for-membership') - async findAllForMembership(@Query('page') page: number) { + async findAllForMembership( + @Query('page') page: number, + @Query('pageSize') pageSize: number, + ) { const [data, count] = await this.roleService.findAllRolesForCreateMember( page, + pageSize, ); return { diff --git a/src/configurable/roles.service.ts b/src/configurable/roles.service.ts index 2f2424e..75b46bd 100644 --- a/src/configurable/roles.service.ts +++ b/src/configurable/roles.service.ts @@ -10,20 +10,20 @@ export class RoleService { private rolesRepository: Repository, ) {} - findAllRoles(page) { + findAllRoles(page, pageSize?) { return this.rolesRepository.findAndCount({ - skip: page * 10, - take: 10, + skip: page * (pageSize || 10), + take: pageSize || 10, order: { version: 'DESC', }, }); } - findAllRolesForCreateMember(page) { + findAllRolesForCreateMember(page, pageSize?) { return this.rolesRepository.findAndCount({ - skip: page * 10, - take: 10, + skip: page * (pageSize || 10), + take: pageSize || 10, where: { id: Not( In([ diff --git a/src/product/history-price/history-price.service.ts b/src/product/history-price/history-price.service.ts index a259ec2..ee1325f 100644 --- a/src/product/history-price/history-price.service.ts +++ b/src/product/history-price/history-price.service.ts @@ -38,6 +38,7 @@ export class ProductHistoryPriceService { page: number, productId: string, supplierId: string, + pageSize?: number, ) { try { const query = this.productHistoryPriceService @@ -54,8 +55,8 @@ export class ProductHistoryPriceService { const data = await query .orderBy('product_history_price.createdAt', 'DESC') - .skip(page * 10) - .take(10) + .skip(page * (pageSize || 10)) + .take(pageSize || 10) .getMany(); const totalData = await query.getCount(); diff --git a/src/product/product-categories.service.ts b/src/product/product-categories.service.ts index 6aa1e30..5afab21 100644 --- a/src/product/product-categories.service.ts +++ b/src/product/product-categories.service.ts @@ -36,10 +36,10 @@ export class ProductCategoriesService { ); } - findAll(page) { + findAll(page, pageSize?) { return this.productCategoriesRepository.findAndCount({ - skip: page * 10, - take: 10, + skip: page * (pageSize || 10), + take: pageSize || 10, order: { version: 'DESC', }, diff --git a/src/product/product-sub-categories.service.ts b/src/product/product-sub-categories.service.ts index 6ad2644..98a5b22 100644 --- a/src/product/product-sub-categories.service.ts +++ b/src/product/product-sub-categories.service.ts @@ -44,7 +44,7 @@ export class ProductSubCategoriesService { ); } - async findAll(page, category: string) { + async findAll(page, category: string, pageSize?) { let filterCategories; if (category) { filterCategories = category.split(',').map((data) => data.trim()); @@ -61,8 +61,8 @@ export class ProductSubCategoriesService { } const data = await baseQuery - .skip(page * 10) - .take(10) + .skip(page * (pageSize || 10)) + .take(pageSize || 10) .getMany(); const totalData = await baseQuery.getCount(); diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index 557857a..6c012c4 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -72,6 +72,7 @@ export class ProductController { @Get('all') async findAll( @Query('page') page: number, + @Query('pageSize') pageSize: number, @Query('sub-category') subcategory: string, @Query('supplier') supplier: string, ) { @@ -79,6 +80,7 @@ export class ProductController { page, supplier == 'null' ? null : supplier, subcategory == 'null' ? null : subcategory, + pageSize, ); return { @@ -105,6 +107,7 @@ export class ProductController { @Get('by-categories-all') async findByCategoriesAll( @Query('page') page: number, + @Query('pageSize') pageSize: number, @Query('sub-category') subcategory: string, @Query('supplier') supplier: string, ) { @@ -112,6 +115,7 @@ export class ProductController { page, subcategory, supplier, + pageSize, ); return { @@ -143,8 +147,14 @@ export class ProductController { } @Get('categories') - async findAllCategories(@Query('page') page: number) { - const [data, count] = await this.productCategoriesService.findAll(page); + async findAllCategories( + @Query('page') page: number, + @Query('pageSize') pageSize: number, + ) { + const [data, count] = await this.productCategoriesService.findAll( + page, + pageSize, + ); return { data, @@ -157,11 +167,13 @@ export class ProductController { @Get('sub-categories') async findAllSubCategories( @Query('page') page: number, + @Query('pageSize') pageSize: number, @Query('category') category: string, ) { const data = await this.productSubCategoriesService.findAll( page, category == 'null' ? null : category, + pageSize, ); return { @@ -184,12 +196,14 @@ export class ProductController { async findPriceHistoryByProductId( @Param('id', ParseUUIDPipe) id: string, @Query('page') page: number, + @Query('pageSize') pageSize: number, @Query('supplier') supplier: string, ) { const data = await this.productHistoryPriceService.findOneByProductId( page, id, supplier, + pageSize, ); return { diff --git a/src/product/product.service.ts b/src/product/product.service.ts index e1c1b0a..e1fa8db 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -47,7 +47,12 @@ export class ProductService { return this.productRepository.findOneOrFail(result.identifiers[0].id); } - async findAll(page: number, supplier: string, subCategories: string) { + async findAll( + page: number, + supplier: string, + subCategories: string, + pageSize?: number, + ) { let filterSupplier, filterSubCategories; if (supplier) { @@ -61,10 +66,6 @@ export class ProductService { return data.trim(); }); } - // if (supplier.length > 0) { - // const dataSupplier = await this.supplierService.findByActiveAll(); - // supplier = dataSupplier.map((item) => item.id); - // } const baseQuery = this.productRepository .createQueryBuilder('product') @@ -72,9 +73,6 @@ export class ProductService { .leftJoin('sub_categories.category', 'category') .leftJoin('product.supplier', 'supplier') .where('supplier.status = :status', { status: true }) - // .where(`product.supplier_id = :supplier_id`, { - // supplier_id: In(supplier), - // }) .leftJoinAndMapOne( 'product.currentPrice', 'product.priceHistory', @@ -106,15 +104,9 @@ export class ProductService { }); } - // if (categories != 'null' && categories) { - // baseQuery.andWhere('sub_categories.category_id = :id', { - // id: categories, - // }); - // } - const data = await baseQuery - .offset(page * 10) - .limit(10) + .offset(page * (pageSize || 10)) + .limit(pageSize || 10) .getRawMany(); const totalData = await baseQuery.getCount(); @@ -156,7 +148,7 @@ export class ProductService { }; } - async findAllBySubCategories(page, subCategories, supplier) { + async findAllBySubCategories(page, subCategories, supplier, pageSize?) { if (supplier != 'null' && !supplier) { supplier = (await this.supplierService.findByActive()).id; } @@ -184,8 +176,8 @@ export class ProductService { } const data = await baseQuery - .skip(page * 10) - .take(10) + .skip(page * (pageSize || 10)) + .take(pageSize || 10) .getMany(); const totalData = await baseQuery.getCount(); diff --git a/src/transaction/transaction.controller.ts b/src/transaction/transaction.controller.ts index 25118c4..da3363e 100644 --- a/src/transaction/transaction.controller.ts +++ b/src/transaction/transaction.controller.ts @@ -1,17 +1,4 @@ -import { - Controller, - Get, - Post, - Body, - Patch, - Param, - Delete, - Request, - HttpStatus, - Query, - Put, - ParseUUIDPipe, -} 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'; @@ -101,10 +88,15 @@ export class TransactionController { } @Get('history') - async findByCategories(@Query('page') page: number, @Request() req) { + async findByCategories( + @Query('page') page: number, + @Query('pageSize') pageSize: number, + @Request() req, + ) { const data = await this.transactionService.transactionHistoryByUser( page, req.user.userId, + pageSize, ); return { @@ -115,11 +107,16 @@ export class TransactionController { } @Get('deposit-return') - async findDepositReturn(@Query('page') page: number, @Request() req) { + async findDepositReturn( + @Query('page') page: number, + @Query('pageSize') pageSize: number, + @Request() req, + ) { const [data, count] = await this.transactionService.getAllDepositReturnFromUser( req.user.userId, page, + pageSize, ); return { @@ -133,12 +130,14 @@ export class TransactionController { @Get('deposit-return/confirmation') async findDepositReturnConfirmation( @Query('page') page: number, + @Query('pageSize') pageSize: number, @Request() req, ) { const [data, count] = await this.transactionService.getAllDepositReturnToUser( req.user.userId, page, + pageSize, ); return { diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index 1d6ae06..1c12a1a 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -3,24 +3,14 @@ import { DistributeTransactionDto } from './dto/distribute-transaction.dto'; import { OrderTransactionDto } from './dto/order-transaction.dto'; import { InjectRepository } from '@nestjs/typeorm'; import { Transactions } from './entities/transactions.entity'; -import { - Connection, - EntityManager, - EntityNotFoundError, - Repository, -} from 'typeorm'; +import { Connection, EntityNotFoundError, Repository } from 'typeorm'; import { COA } from './entities/coa.entity'; import { TransactionJournal } from './entities/transaction-journal.entity'; 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'; @@ -560,7 +550,11 @@ export class TransactionService { return transactionData; } - async transactionHistoryByUser(page: number, user: string) { + async transactionHistoryByUser( + page: number, + user: string, + pageSize?: number, + ) { const baseQuery = this.transactionRepository .createQueryBuilder('transaction') .select('transaction.id', 'id') @@ -578,8 +572,8 @@ export class TransactionService { // .leftJoinAndSelect('product_price.product', 'product'); const data = await baseQuery - .skip(page * 10) - .take(10) + .offset(page * (pageSize || 10)) + .limit(pageSize || 10) .getRawMany(); const totalData = await baseQuery.getCount(); @@ -614,10 +608,14 @@ export class TransactionService { } } - async getAllDepositReturnFromUser(user: string, page: number) { + async getAllDepositReturnFromUser( + user: string, + page: number, + pageSize?: number, + ) { return this.transactionRepository.findAndCount({ - skip: page * 10, - take: 10, + skip: page * (pageSize || 10), + take: pageSize || 10, where: { user: user, type: typeTransaction.DEPOSIT_RETURN, @@ -628,10 +626,14 @@ export class TransactionService { }); } - async getAllDepositReturnToUser(user: string, page: number) { + async getAllDepositReturnToUser( + user: string, + page: number, + pageSize?: number, + ) { return this.transactionRepository.findAndCount({ - skip: page * 10, - take: 10, + skip: page * (pageSize || 10), + take: pageSize || 10, where: { user_destination: user, type: typeTransaction.DEPOSIT_RETURN, diff --git a/src/users/partner/partner.service.ts b/src/users/partner/partner.service.ts index b956648..1cae2f5 100644 --- a/src/users/partner/partner.service.ts +++ b/src/users/partner/partner.service.ts @@ -1,10 +1,4 @@ -import { - forwardRef, - HttpException, - HttpStatus, - Inject, - Injectable, -} from '@nestjs/common'; +import { forwardRef, HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Connection, Not, Repository } from 'typeorm'; import { CoaService } from '../../transaction/coa.service'; @@ -135,10 +129,10 @@ export class PartnerService { return partnerData; }; - findAllPartner(page) { + findAllPartner(page, pageSize?) { return this.partnerRepository.findAndCount({ - skip: page * 10, - take: 10, + skip: page * (pageSize || 10), + take: pageSize || 10, order: { version: 'DESC', }, diff --git a/src/users/supplier/supplier.service.ts b/src/users/supplier/supplier.service.ts index 13699cf..e322890 100644 --- a/src/users/supplier/supplier.service.ts +++ b/src/users/supplier/supplier.service.ts @@ -1,10 +1,4 @@ -import { - forwardRef, - HttpException, - HttpStatus, - Inject, - Injectable, -} from '@nestjs/common'; +import { forwardRef, HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Connection, EntityNotFoundError, Not, Repository } from 'typeorm'; import { Supplier } from '../entities/supplier.entity'; @@ -130,7 +124,7 @@ export class SupplierService { return supplierData; }; - async findAllSupplier(page) { + async findAllSupplier(page, pageSize?) { const baseQuery = this.supplierRepository .createQueryBuilder('supplier') .leftJoinAndMapOne( @@ -148,8 +142,8 @@ export class SupplierService { .select(['supplier', 'coa.amount', 'coa_undistribute.amount']); const data = await baseQuery - .skip(page * 10) - .take(10) + .skip(page * (pageSize || 10)) + .take(pageSize || 10) .getMany(); const totalData = await baseQuery.getCount(); diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 42c581f..a127b27 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -125,8 +125,12 @@ export class UsersController { @Public() @Get('supplier') - async findAllSupplier(@Query('page') page: number) { - const data = await this.supplierService.findAllSupplier(page); + async findAllSupplier( + @Query('page') page: number, + @Query('pageSize') pageSize: number, + @Request() req, + ) { + const data = await this.supplierService.findAllSupplier(page, pageSize); return { ...data, @@ -136,8 +140,15 @@ export class UsersController { } @Get('partner') - async findAllPartner(@Query('page') page: number) { - const [data, count] = await this.partnerService.findAllPartner(page); + async findAllPartner( + @Query('page') page: number, + @Query('pageSize') pageSize: number, + @Request() req, + ) { + const [data, count] = await this.partnerService.findAllPartner( + page, + pageSize, + ); return { data, @@ -162,8 +173,13 @@ export class UsersController { async findByRoles( @Param('id', ParseUUIDPipe) id: string, @Query('page') page: number, + @Query('pageSize') pageSize: number, ) { - const [data, count] = await this.usersService.findByRoles(id, page); + const [data, count] = await this.usersService.findByRoles( + id, + page, + pageSize, + ); return { data, diff --git a/src/users/users.service.ts b/src/users/users.service.ts index eb97d80..b823a26 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -1,19 +1,7 @@ -import { - forwardRef, - HttpException, - HttpStatus, - Inject, - Injectable, -} from '@nestjs/common'; +import { forwardRef, HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common'; import { CreateUserDto } from './dto/create-user.dto'; import { UpdateUserDto } from './dto/update-user.dto'; -import { - Connection, - EntityNotFoundError, - Equal, - Not, - Repository, -} from 'typeorm'; +import { Connection, EntityNotFoundError, Not, Repository } from 'typeorm'; import { User } from './entities/user.entity'; import { InjectRepository } from '@nestjs/typeorm'; import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util'; @@ -155,10 +143,10 @@ export class UsersService { }; } - findByRoles(relationId: string, page: number) { + findByRoles(relationId: string, page: number, pageSize?: number) { return this.usersRepository.findAndCount({ - skip: page * 10, - take: 10, + skip: page * (pageSize || 10), + take: pageSize || 10, where: { roles: relationId, },