This commit is contained in:
@@ -106,6 +106,13 @@ export class CreateFoodDto {
|
||||
@ApiProperty({ example: 50 })
|
||||
dailyStock: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
@Min(0)
|
||||
@Type(() => Number)
|
||||
@ApiPropertyOptional({ example: 50 })
|
||||
availableStock?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
@ApiPropertyOptional({ example: false })
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { CreateFoodDto } from '../dto/create-food.dto';
|
||||
import { FindFoodsDto } from '../dto/find-foods.dto';
|
||||
import { FoodRepository } from '../repositories/food.repository';
|
||||
@@ -25,7 +25,10 @@ export class FoodService {
|
||||
) { }
|
||||
|
||||
async create(restId: string, createFoodDto: CreateFoodDto) {
|
||||
const { categoryId, dailyStock = 0, ...rest } = createFoodDto;
|
||||
const { categoryId, dailyStock = 0, availableStock, ...rest } = createFoodDto;
|
||||
const totalStock = dailyStock;
|
||||
const resolvedAvailableStock = availableStock ?? dailyStock ?? 0;
|
||||
|
||||
const restaurant = await this.restService.findOneOrFail(restId);
|
||||
|
||||
const category = await this.categoryRepository.findOne({ id: categoryId, restaurant: { id: restId } });
|
||||
@@ -60,8 +63,8 @@ export class FoodService {
|
||||
const food = em.create(Food, data);
|
||||
const newInventoryRecord = em.create(Inventory, {
|
||||
food: food,
|
||||
availableStock: dailyStock,
|
||||
totalStock: dailyStock,
|
||||
availableStock: resolvedAvailableStock,
|
||||
totalStock: totalStock,
|
||||
});
|
||||
|
||||
await em.flush();
|
||||
@@ -271,7 +274,7 @@ export class FoodService {
|
||||
}
|
||||
|
||||
async update(restId: string, id: string, dto: Partial<CreateFoodDto>): Promise<Food> {
|
||||
const { categoryId, dailyStock, ...rest } = dto;
|
||||
const { categoryId, dailyStock, availableStock, ...rest } = dto;
|
||||
const food = await this.foodRepository.findOne({ id, restaurant: { id: restId } }, { populate: ['restaurant'] });
|
||||
if (!food) {
|
||||
throw new NotFoundException(FoodMessage.NOT_FOUND);
|
||||
@@ -288,22 +291,38 @@ export class FoodService {
|
||||
this.em.assign(food, { category: category });
|
||||
}
|
||||
|
||||
// assign other fields from DTO (excluding dailyStock handled below)
|
||||
// assign other fields from DTO (excluding stock fields handled below)
|
||||
this.em.assign(food, rest);
|
||||
|
||||
// Persist food and update/create related inventory atomically
|
||||
await this.em.transactional(async em => {
|
||||
await em.persistAndFlush(food);
|
||||
|
||||
if (typeof dailyStock !== 'undefined') {
|
||||
if (typeof dailyStock !== 'undefined' || typeof availableStock !== 'undefined') {
|
||||
const inventoryRecord = await em.findOne(Inventory, { food: food });
|
||||
const newTotalStock = typeof dailyStock !== 'undefined'
|
||||
? dailyStock
|
||||
: (inventoryRecord?.totalStock ?? 0);
|
||||
const newAvailableStock = typeof availableStock !== 'undefined'
|
||||
? availableStock
|
||||
: (inventoryRecord?.availableStock ?? 0);
|
||||
|
||||
if (newAvailableStock > newTotalStock) {
|
||||
throw new BadRequestException('Available stock cannot exceed total stock');
|
||||
}
|
||||
|
||||
if (inventoryRecord) {
|
||||
inventoryRecord.totalStock = dailyStock;
|
||||
if (typeof dailyStock !== 'undefined') {
|
||||
inventoryRecord.totalStock = dailyStock;
|
||||
}
|
||||
if (typeof availableStock !== 'undefined') {
|
||||
inventoryRecord.availableStock = availableStock;
|
||||
}
|
||||
} else {
|
||||
em.create(Inventory, {
|
||||
food: food,
|
||||
availableStock: dailyStock,
|
||||
totalStock: dailyStock,
|
||||
availableStock: newAvailableStock,
|
||||
totalStock: newTotalStock,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user