125 lines
3.3 KiB
TypeScript
125 lines
3.3 KiB
TypeScript
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|
import { EntityNotFoundError, Repository } from 'typeorm';
|
|
import { Product } from './entities/product.entity';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { CreateProductDto } from '../product/dto/product/create-product.dto';
|
|
import { ProductSubCategoriesService } from './product-sub-categories.service';
|
|
import { UpdateProductDto } from './dto/product/update-product.dto';
|
|
import { ProductHistoryPrice } from './entities/product-history-price.entity';
|
|
|
|
enum Type {
|
|
NORMAL,
|
|
PROMO,
|
|
}
|
|
|
|
export class ProductService {
|
|
constructor(
|
|
@InjectRepository(Product)
|
|
private productRepository: Repository<Product>,
|
|
private productHistoryPrice: Repository<ProductHistoryPrice>,
|
|
private productSubCategoriesService: ProductSubCategoriesService,
|
|
) {}
|
|
|
|
async create(createProductDto: CreateProductDto) {
|
|
const subCategories = await this.productSubCategoriesService.findOne(
|
|
createProductDto.subCategoriesId,
|
|
);
|
|
|
|
const result = await this.productRepository.insert({
|
|
name: createProductDto.name,
|
|
code: createProductDto.code,
|
|
status: createProductDto.status,
|
|
subCategories: subCategories,
|
|
price: createProductDto.price,
|
|
});
|
|
|
|
await this.productHistoryPrice.insert({
|
|
product: result.identifiers[0],
|
|
type: Type.NORMAL,
|
|
startDate: new Date(),
|
|
endDate: null,
|
|
});
|
|
|
|
return this.productRepository.findOneOrFail(result.identifiers[0].id);
|
|
}
|
|
|
|
findAll(page) {
|
|
return this.productRepository.findAndCount({
|
|
skip: page * 10,
|
|
take: 10,
|
|
order: {
|
|
version: 'DESC',
|
|
},
|
|
});
|
|
}
|
|
|
|
async findOne(code: string) {
|
|
try {
|
|
return await this.productRepository.findOneOrFail({ 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, updateProductDto: UpdateProductDto) {
|
|
try {
|
|
await this.productRepository.findOneOrFail(id);
|
|
} catch (e) {
|
|
if (e instanceof EntityNotFoundError) {
|
|
throw new HttpException(
|
|
{
|
|
statusCode: HttpStatus.NOT_FOUND,
|
|
error: 'Data not found',
|
|
},
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
const subCategories = await this.productSubCategoriesService.findOne(
|
|
updateProductDto.subCategoriesId,
|
|
);
|
|
|
|
const result = await this.productRepository.update(id, {
|
|
name: updateProductDto.name,
|
|
code: updateProductDto.code,
|
|
status: updateProductDto.status,
|
|
subCategories: subCategories,
|
|
price: updateProductDto.price,
|
|
});
|
|
|
|
return this.productRepository.findOneOrFail(id);
|
|
}
|
|
|
|
async remove(id: string) {
|
|
try {
|
|
await this.productRepository.findOneOrFail(id);
|
|
} catch (e) {
|
|
if (e instanceof EntityNotFoundError) {
|
|
throw new HttpException(
|
|
{
|
|
statusCode: HttpStatus.NOT_FOUND,
|
|
error: 'Data not found',
|
|
},
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
await this.productRepository.delete(id);
|
|
}
|
|
}
|