remove inventory
This commit is contained in:
@@ -10,7 +10,6 @@ import { CategoryMessage, FoodMessage, RestMessage } from 'src/common/enums/mess
|
||||
import { RestRepository } from ../../..shops/repositories/rest.repository';
|
||||
import { CacheService } from '../../../utils/cache.service';
|
||||
import { Favorite } from '../entities/favorite.entity';
|
||||
import { Inventory } from 'src/modules/inventory/entities/inventory.entity';
|
||||
|
||||
@Injectable()
|
||||
export class FoodService {
|
||||
@@ -22,7 +21,7 @@ export class FoodService {
|
||||
) { }
|
||||
|
||||
async create(restId: string, createFoodDto: CreateFoodDto) {
|
||||
const { categoryId, dailyStock = 0, ...rest } = createFoodDto;
|
||||
const { categoryId, ...rest } = createFoodDto;
|
||||
const shop = await this.restRepository.findOne({ id: restId });
|
||||
if (!shop) {
|
||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||
@@ -32,7 +31,7 @@ export class FoodService {
|
||||
throw new NotFoundException(CategoryMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
const { product, inventory } = await this.em.transactional(async em => {
|
||||
const product = await this.em.transactional(async em => {
|
||||
// prepare data with defaults for required fields so repository.create typing is satisfied
|
||||
const data: RequiredEntityData<Product> = {
|
||||
desc: rest.desc,
|
||||
@@ -51,23 +50,16 @@ export class FoodService {
|
||||
};
|
||||
|
||||
const product = em.create(Product, data);
|
||||
const newInventoryRecord = em.create(Inventory, {
|
||||
product: product,
|
||||
availableStock: dailyStock,
|
||||
totalStock: dailyStock,
|
||||
});
|
||||
|
||||
await em.flush();
|
||||
return { product, inventory: newInventoryRecord };
|
||||
return product;
|
||||
});
|
||||
|
||||
// Re-load created entities with the primary EM to ensure they're attached
|
||||
const savedFood = product?.id
|
||||
? await this.foodRepository.findOne({ id: product.id }, { populate: ['category', 'shop'] })
|
||||
: null;
|
||||
const savedInventory = inventory?.id ? await this.em.findOne(Inventory, { id: inventory.id }) : inventory;
|
||||
|
||||
return { product: savedFood ?? product, inventory: savedInventory ?? inventory };
|
||||
return savedFood ?? product;
|
||||
}
|
||||
|
||||
findAll(restId: string, dto: FindFoodsDto) {
|
||||
@@ -78,7 +70,7 @@ export class FoodService {
|
||||
* Public product detail (only active products are visible).
|
||||
*/
|
||||
async findPublicById(foodId: string, userId?: string): Promise<any> {
|
||||
const product = await this.foodRepository.findOne({ id: foodId, isActive: true }, { populate: ['category', 'inventory'] });
|
||||
const product = await this.foodRepository.findOne({ id: foodId, isActive: true }, { populate: ['category'] });
|
||||
if (!product) throw new NotFoundException(FoodMessage.NOT_FOUND);
|
||||
let isFavorite = false;
|
||||
if (userId) {
|
||||
@@ -94,7 +86,7 @@ export class FoodService {
|
||||
* Admin product detail (scoped to the authenticated shop).
|
||||
*/
|
||||
async findAdminById(restId: string, id: string): Promise<Product> {
|
||||
const product = await this.foodRepository.findOne({ id, shop: { id: restId } }, { populate: ['category', 'inventory'] });
|
||||
const product = await this.foodRepository.findOne({ id, shop: { id: restId } }, { populate: ['category'] });
|
||||
|
||||
if (!product) throw new NotFoundException(FoodMessage.NOT_FOUND);
|
||||
return product;
|
||||
@@ -123,7 +115,7 @@ export class FoodService {
|
||||
}
|
||||
|
||||
async update(restId: string, id: string, dto: Partial<CreateFoodDto>): Promise<Product> {
|
||||
const { categoryId, dailyStock, ...rest } = dto;
|
||||
const { categoryId, ...rest } = dto;
|
||||
const product = await this.foodRepository.findOne({ id, shop: { id: restId } }, { populate: ['shop'] });
|
||||
if (!product) {
|
||||
throw new NotFoundException(FoodMessage.NOT_FOUND);
|
||||
@@ -143,25 +135,7 @@ export class FoodService {
|
||||
// assign other fields from DTO (excluding dailyStock handled below)
|
||||
this.em.assign(product, rest);
|
||||
|
||||
// Persist product and update/create related inventory atomically
|
||||
await this.em.transactional(async em => {
|
||||
await em.persistAndFlush(product);
|
||||
|
||||
if (typeof dailyStock !== 'undefined') {
|
||||
const inventoryRecord = await em.findOne(Inventory, { product: product });
|
||||
if (inventoryRecord) {
|
||||
inventoryRecord.totalStock = dailyStock;
|
||||
} else {
|
||||
em.create(Inventory, {
|
||||
product: product,
|
||||
availableStock: dailyStock,
|
||||
totalStock: dailyStock,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await em.flush();
|
||||
});
|
||||
await this.em.persistAndFlush(product);
|
||||
|
||||
// Re-load the product to ensure populated relations and stable instance
|
||||
const savedFood = await this.foodRepository.findOne({ id: product.id }, { populate: ['category', 'shop'] });
|
||||
|
||||
Reference in New Issue
Block a user