297 lines
7.2 KiB
TypeScript
297 lines
7.2 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
HttpStatus,
|
|
Param,
|
|
ParseUUIDPipe,
|
|
Post,
|
|
Put,
|
|
Query,
|
|
Request,
|
|
} from '@nestjs/common';
|
|
import { ProductService } from './product.service';
|
|
import { ProductCategoriesService } from './product-categories.service';
|
|
import { CreateCategoriesProductDto } from './dto/categories/create-categories-product.dto';
|
|
import { UpdateCategoriesProductDto } from '../product/dto/categories/update-categories-product.dto';
|
|
import { UpdateSubCategoriesProductDto } from '../product/dto/sub-categories/update-sub-categories-product.dto';
|
|
import { ProductSubCategoriesService } from './product-sub-categories.service';
|
|
import { CreateSubCategoriesProductDto } from './dto/sub-categories/create-sub-categories-product.dto';
|
|
import { CreateProductDto } from './dto/product/create-product.dto';
|
|
import { UpdateProductDto } from './dto/product/update-product.dto';
|
|
import { ProductHistoryPriceService } from './history-price/history-price.service';
|
|
import { UploadProductDto } from './dto/product/upload-product.dto';
|
|
|
|
@Controller({
|
|
path: 'product',
|
|
version: '1',
|
|
})
|
|
export class ProductController {
|
|
constructor(
|
|
private readonly productService: ProductService,
|
|
private readonly productCategoriesService: ProductCategoriesService,
|
|
private readonly productSubCategoriesService: ProductSubCategoriesService,
|
|
private readonly productHistoryPriceService: ProductHistoryPriceService,
|
|
) {}
|
|
|
|
@Post()
|
|
async create(@Body() createProductDto: CreateProductDto) {
|
|
return {
|
|
data: await this.productService.create(createProductDto),
|
|
statusCode: HttpStatus.CREATED,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Post('categories')
|
|
async createCategories(
|
|
@Body() createCategoriesProductDto: CreateCategoriesProductDto,
|
|
) {
|
|
return {
|
|
data: await this.productCategoriesService.create(
|
|
createCategoriesProductDto,
|
|
),
|
|
statusCode: HttpStatus.CREATED,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Post('sub-categories')
|
|
async createSubCategories(
|
|
@Body() createSubCategoriesProductDto: CreateSubCategoriesProductDto,
|
|
) {
|
|
return {
|
|
data: await this.productSubCategoriesService.create(
|
|
createSubCategoriesProductDto,
|
|
),
|
|
statusCode: HttpStatus.CREATED,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Post('upload-product')
|
|
async createProductBaseOnCSV(@Body() uploadProductDto: UploadProductDto) {
|
|
await this.productService.processUploadCSV(
|
|
uploadProductDto.fileName,
|
|
uploadProductDto.supplierCode,
|
|
);
|
|
|
|
return {
|
|
data: 'Done',
|
|
statusCode: HttpStatus.CREATED,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get('all')
|
|
async findAll(
|
|
@Query('page') page: number,
|
|
@Query('pageSize') pageSize: number,
|
|
@Query('sub-category') subcategory: string,
|
|
@Query('supplier') supplier: string,
|
|
) {
|
|
const data = await this.productService.findAll(
|
|
page,
|
|
supplier == 'null' ? null : supplier,
|
|
subcategory == 'null' ? null : subcategory,
|
|
pageSize,
|
|
);
|
|
|
|
return {
|
|
...data,
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get('test')
|
|
async test(@Request() req) {
|
|
const data = await this.productService.processUploadCSV('','');
|
|
|
|
return {
|
|
data,
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get('by-categories-all')
|
|
async findByCategoriesAll(
|
|
@Query('page') page: number,
|
|
@Query('pageSize') pageSize: number,
|
|
@Query('sub-category') subcategory: string,
|
|
@Query('supplier') supplier: string,
|
|
) {
|
|
const data = await this.productService.findAllBySubCategories(
|
|
page,
|
|
subcategory,
|
|
supplier,
|
|
pageSize,
|
|
);
|
|
|
|
return {
|
|
...data,
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get('by-categories')
|
|
async findByCategories(
|
|
@Query('page') page: number,
|
|
@Query('pageSize') pageSize: number,
|
|
@Query('sub-category') subcategory: string,
|
|
@Request() req,
|
|
) {
|
|
const data = await this.productService.findAllForPartner(
|
|
page,
|
|
pageSize,
|
|
subcategory,
|
|
req.user.username,
|
|
);
|
|
|
|
return {
|
|
...data,
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get('categories')
|
|
async findAllCategories(
|
|
@Query('page') page: number,
|
|
@Query('pageSize') pageSize: number,
|
|
) {
|
|
const [data, count] = await this.productCategoriesService.findAll(
|
|
page,
|
|
pageSize,
|
|
);
|
|
|
|
return {
|
|
data,
|
|
count,
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@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 {
|
|
...data,
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get(':id')
|
|
async findOne(@Param('id', ParseUUIDPipe) id: string) {
|
|
return {
|
|
data: await this.productService.findOneById(id),
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get('price-history/:id')
|
|
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 {
|
|
...data,
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Put(':id')
|
|
async update(
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
@Body() updateProductDto: UpdateProductDto,
|
|
) {
|
|
return {
|
|
data: await this.productService.update(id, updateProductDto),
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Put('categories/:id')
|
|
async updateCategories(
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
@Body() updateCategoriesDto: UpdateCategoriesProductDto,
|
|
) {
|
|
return {
|
|
data: await this.productCategoriesService.update(id, updateCategoriesDto),
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Put('sub-categories/:id')
|
|
async updateSubCategories(
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
@Body() updateSubCategoriesDto: UpdateSubCategoriesProductDto,
|
|
) {
|
|
return {
|
|
data: await this.productSubCategoriesService.update(
|
|
id,
|
|
updateSubCategoriesDto,
|
|
),
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Delete(':id')
|
|
async remove(@Param('id', ParseUUIDPipe) id: string) {
|
|
await this.productService.remove(id);
|
|
|
|
return {
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Delete('categories/:id')
|
|
async removeCategories(@Param('id', ParseUUIDPipe) id: string) {
|
|
await this.productCategoriesService.remove(id);
|
|
|
|
return {
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Delete('sub-categories/:id')
|
|
async removeSubCategories(@Param('id', ParseUUIDPipe) id: string) {
|
|
await this.productSubCategoriesService.remove(id);
|
|
|
|
return {
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
}
|