From 0f801483a3a90fab33c4bbd2b4a84060acae972b Mon Sep 17 00:00:00 2001 From: ilham Date: Thu, 16 Dec 2021 19:04:38 +0700 Subject: [PATCH] fix: add code in categories and subcategories --- .../create-categories-product.dto.ts | 3 ++ .../create-sub-categories-product.dto.ts | 3 ++ .../entities/product-category.entity.ts | 3 ++ .../entities/product-sub-category.entity.ts | 3 ++ src/product/product-categories.service.ts | 36 +++++++++++++++++++ src/product/product-sub-categories.service.ts | 15 ++++++++ 6 files changed, 63 insertions(+) diff --git a/src/product/dto/categories/create-categories-product.dto.ts b/src/product/dto/categories/create-categories-product.dto.ts index e878820..78203f5 100644 --- a/src/product/dto/categories/create-categories-product.dto.ts +++ b/src/product/dto/categories/create-categories-product.dto.ts @@ -3,4 +3,7 @@ import { IsNotEmpty, IsUUID } from 'class-validator'; export class CreateCategoriesProductDto { @IsNotEmpty() name: string; + + @IsNotEmpty() + code: string; } diff --git a/src/product/dto/sub-categories/create-sub-categories-product.dto.ts b/src/product/dto/sub-categories/create-sub-categories-product.dto.ts index 385db53..a451dd3 100644 --- a/src/product/dto/sub-categories/create-sub-categories-product.dto.ts +++ b/src/product/dto/sub-categories/create-sub-categories-product.dto.ts @@ -5,6 +5,9 @@ export class CreateSubCategoriesProductDto extends CreateCategoriesProductDto { @IsNotEmpty() name: string; + @IsNotEmpty() + code: string; + @IsUUID() categoryId: string; } diff --git a/src/product/entities/product-category.entity.ts b/src/product/entities/product-category.entity.ts index 0964848..3da2720 100644 --- a/src/product/entities/product-category.entity.ts +++ b/src/product/entities/product-category.entity.ts @@ -19,6 +19,9 @@ export class ProductCategories extends BaseModel { @Column() name: string; + @Column() + code: string; + @OneToMany( () => ProductSubCategories, (subCategories) => subCategories.category, diff --git a/src/product/entities/product-sub-category.entity.ts b/src/product/entities/product-sub-category.entity.ts index 6b129a2..a5b1aad 100644 --- a/src/product/entities/product-sub-category.entity.ts +++ b/src/product/entities/product-sub-category.entity.ts @@ -17,6 +17,9 @@ export class ProductSubCategories extends BaseModel { @Column() name: string; + @Column() + code: string; + @ManyToOne(() => ProductCategories, (categories) => categories.sub_categories) category: ProductCategories; diff --git a/src/product/product-categories.service.ts b/src/product/product-categories.service.ts index 081a411..d3c2cdc 100644 --- a/src/product/product-categories.service.ts +++ b/src/product/product-categories.service.ts @@ -13,6 +13,20 @@ export class ProductCategoriesService { ) {} async create(CreateCategoriesProductDto: CreateCategoriesProductDto) { + const check = await this.productCategoriesRepository.findOne({ + code: CreateCategoriesProductDto.code, + }); + + if (check) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_ACCEPTABLE, + error: 'Category Already Exist', + }, + HttpStatus.NOT_FOUND, + ); + } + const result = await this.productCategoriesRepository.insert( CreateCategoriesProductDto, ); @@ -50,6 +64,28 @@ export class ProductCategoriesService { } } + async findByCode(code: string) { + try { + return await this.productCategoriesRepository.findOneOrFail({ + where: { + code: code, + }, + }); + } catch (e) { + if (e instanceof EntityNotFoundError) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_FOUND, + error: 'Data not found', + }, + HttpStatus.NOT_FOUND, + ); + } else { + throw e; + } + } + } + async update( id: string, updateCategoriesProductDto: UpdateCategoriesProductDto, diff --git a/src/product/product-sub-categories.service.ts b/src/product/product-sub-categories.service.ts index cd36294..f5b30a3 100644 --- a/src/product/product-sub-categories.service.ts +++ b/src/product/product-sub-categories.service.ts @@ -15,12 +15,27 @@ export class ProductSubCategoriesService { ) {} async create(createSubCategoriesProductDto: CreateSubCategoriesProductDto) { + const check = await this.productSubCategoriesRepository.findOne({ + code: createSubCategoriesProductDto.code, + }); + + if (check) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_ACCEPTABLE, + error: 'Sub Category Already Exist', + }, + HttpStatus.NOT_FOUND, + ); + } + const categories = await this.productCategoriesService.findOne( createSubCategoriesProductDto.categoryId, ); const result = await this.productSubCategoriesRepository.insert({ name: createSubCategoriesProductDto.name, + code: createSubCategoriesProductDto.code, category: categories, });