- fix when product not active can't order
This commit is contained in:
44
src/product/history-status/history-status.service.ts
Normal file
44
src/product/history-status/history-status.service.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { EntityNotFoundError, IsNull, Repository } from 'typeorm';
|
||||
import { ProductHistoryStatus } from '../entities/product-history-status.entity';
|
||||
|
||||
@Injectable()
|
||||
export class ProductHistoryStatusService {
|
||||
constructor(
|
||||
@InjectRepository(ProductHistoryStatus)
|
||||
private productHistoryStatusRepository: Repository<ProductHistoryStatus>,
|
||||
) {}
|
||||
|
||||
async create(dataProduct: ProductHistoryStatus) {
|
||||
const result = await this.productHistoryStatusRepository.insert(
|
||||
dataProduct,
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async findOneActive(productId: string, partnerId: string) {
|
||||
try {
|
||||
return await this.productHistoryStatusRepository.findOneOrFail({
|
||||
relations: ['product'],
|
||||
where: {
|
||||
product: productId,
|
||||
partner: partnerId,
|
||||
},
|
||||
});
|
||||
} catch (e) {
|
||||
if (e instanceof EntityNotFoundError) {
|
||||
throw new HttpException(
|
||||
{
|
||||
statusCode: HttpStatus.NOT_FOUND,
|
||||
error: 'Product not active',
|
||||
},
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import { ProductSubCategoriesService } from './product-sub-categories.service';
|
||||
import { UsersModule } from '../users/users.module';
|
||||
import { ProductHistoryPriceService } from './history-price/history-price.service';
|
||||
import {ProductHistoryStatus} from "./entities/product-history-status.entity";
|
||||
import {ProductHistoryStatusService} from "./history-status/history-status.service";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -29,7 +30,8 @@ import {ProductHistoryStatus} from "./entities/product-history-status.entity";
|
||||
ProductCategoriesService,
|
||||
ProductSubCategoriesService,
|
||||
ProductHistoryPriceService,
|
||||
ProductHistoryStatusService,
|
||||
],
|
||||
exports: [ProductService, ProductHistoryPriceService],
|
||||
exports: [ProductService, ProductHistoryPriceService, ProductHistoryStatusService],
|
||||
})
|
||||
export class ProductModule {}
|
||||
|
||||
Reference in New Issue
Block a user