Merge branch 'development' into 'devops-staging'

fix: add code in categories and subcategories

See merge request empatnusabangsa/ppob/ppob-backend!30
This commit is contained in:
ilham dwi pratama 2021-12-16 12:13:30 +00:00
commit b85f0843ad
6 changed files with 63 additions and 0 deletions

View File

@ -3,4 +3,7 @@ import { IsNotEmpty, IsUUID } from 'class-validator';
export class CreateCategoriesProductDto { export class CreateCategoriesProductDto {
@IsNotEmpty() @IsNotEmpty()
name: string; name: string;
@IsNotEmpty()
code: string;
} }

View File

@ -5,6 +5,9 @@ export class CreateSubCategoriesProductDto extends CreateCategoriesProductDto {
@IsNotEmpty() @IsNotEmpty()
name: string; name: string;
@IsNotEmpty()
code: string;
@IsUUID() @IsUUID()
categoryId: string; categoryId: string;
} }

View File

@ -19,6 +19,9 @@ export class ProductCategories extends BaseModel {
@Column() @Column()
name: string; name: string;
@Column()
code: string;
@OneToMany( @OneToMany(
() => ProductSubCategories, () => ProductSubCategories,
(subCategories) => subCategories.category, (subCategories) => subCategories.category,

View File

@ -17,6 +17,9 @@ export class ProductSubCategories extends BaseModel {
@Column() @Column()
name: string; name: string;
@Column()
code: string;
@ManyToOne(() => ProductCategories, (categories) => categories.sub_categories) @ManyToOne(() => ProductCategories, (categories) => categories.sub_categories)
category: ProductCategories; category: ProductCategories;

View File

@ -13,6 +13,20 @@ export class ProductCategoriesService {
) {} ) {}
async create(CreateCategoriesProductDto: CreateCategoriesProductDto) { 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( const result = await this.productCategoriesRepository.insert(
CreateCategoriesProductDto, 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( async update(
id: string, id: string,
updateCategoriesProductDto: UpdateCategoriesProductDto, updateCategoriesProductDto: UpdateCategoriesProductDto,

View File

@ -15,12 +15,27 @@ export class ProductSubCategoriesService {
) {} ) {}
async create(createSubCategoriesProductDto: CreateSubCategoriesProductDto) { 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( const categories = await this.productCategoriesService.findOne(
createSubCategoriesProductDto.categoryId, createSubCategoriesProductDto.categoryId,
); );
const result = await this.productSubCategoriesRepository.insert({ const result = await this.productSubCategoriesRepository.insert({
name: createSubCategoriesProductDto.name, name: createSubCategoriesProductDto.name,
code: createSubCategoriesProductDto.code,
category: categories, category: categories,
}); });