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:37 +00:00
commit 2f30072f20
6 changed files with 63 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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,

View File

@ -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,
});