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:
commit
b85f0843ad
|
@ -3,4 +3,7 @@ import { IsNotEmpty, IsUUID } from 'class-validator';
|
|||
export class CreateCategoriesProductDto {
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
code: string;
|
||||
}
|
||||
|
|
|
@ -5,6 +5,9 @@ export class CreateSubCategoriesProductDto extends CreateCategoriesProductDto {
|
|||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
code: string;
|
||||
|
||||
@IsUUID()
|
||||
categoryId: string;
|
||||
}
|
||||
|
|
|
@ -19,6 +19,9 @@ export class ProductCategories extends BaseModel {
|
|||
@Column()
|
||||
name: string;
|
||||
|
||||
@Column()
|
||||
code: string;
|
||||
|
||||
@OneToMany(
|
||||
() => ProductSubCategories,
|
||||
(subCategories) => subCategories.category,
|
||||
|
|
|
@ -17,6 +17,9 @@ export class ProductSubCategories extends BaseModel {
|
|||
@Column()
|
||||
name: string;
|
||||
|
||||
@Column()
|
||||
code: string;
|
||||
|
||||
@ManyToOne(() => ProductCategories, (categories) => categories.sub_categories)
|
||||
category: ProductCategories;
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user