add stock avaiable
deploy to danak / build_and_deploy (push) Has been cancelled

This commit is contained in:
2026-06-18 18:49:05 +03:30
parent ec1f7c407a
commit e1823d2722
2 changed files with 36 additions and 10 deletions
+7
View File
@@ -106,6 +106,13 @@ export class CreateFoodDto {
@ApiProperty({ example: 50 }) @ApiProperty({ example: 50 })
dailyStock: number; dailyStock: number;
@IsOptional()
@IsInt()
@Min(0)
@Type(() => Number)
@ApiPropertyOptional({ example: 50 })
availableStock?: number;
@IsOptional() @IsOptional()
@IsBoolean() @IsBoolean()
@ApiPropertyOptional({ example: false }) @ApiPropertyOptional({ example: false })
+28 -9
View File
@@ -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 { CreateFoodDto } from '../dto/create-food.dto';
import { FindFoodsDto } from '../dto/find-foods.dto'; import { FindFoodsDto } from '../dto/find-foods.dto';
import { FoodRepository } from '../repositories/food.repository'; import { FoodRepository } from '../repositories/food.repository';
@@ -25,7 +25,10 @@ export class FoodService {
) { } ) { }
async create(restId: string, createFoodDto: CreateFoodDto) { 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 restaurant = await this.restService.findOneOrFail(restId);
const category = await this.categoryRepository.findOne({ id: categoryId, restaurant: { id: 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 food = em.create(Food, data);
const newInventoryRecord = em.create(Inventory, { const newInventoryRecord = em.create(Inventory, {
food: food, food: food,
availableStock: dailyStock, availableStock: resolvedAvailableStock,
totalStock: dailyStock, totalStock: totalStock,
}); });
await em.flush(); await em.flush();
@@ -271,7 +274,7 @@ export class FoodService {
} }
async update(restId: string, id: string, dto: Partial<CreateFoodDto>): Promise<Food> { 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'] }); const food = await this.foodRepository.findOne({ id, restaurant: { id: restId } }, { populate: ['restaurant'] });
if (!food) { if (!food) {
throw new NotFoundException(FoodMessage.NOT_FOUND); throw new NotFoundException(FoodMessage.NOT_FOUND);
@@ -288,22 +291,38 @@ export class FoodService {
this.em.assign(food, { category: category }); 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); this.em.assign(food, rest);
// Persist food and update/create related inventory atomically // Persist food and update/create related inventory atomically
await this.em.transactional(async em => { await this.em.transactional(async em => {
await em.persistAndFlush(food); await em.persistAndFlush(food);
if (typeof dailyStock !== 'undefined') { if (typeof dailyStock !== 'undefined' || typeof availableStock !== 'undefined') {
const inventoryRecord = await em.findOne(Inventory, { food: food }); 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) { if (inventoryRecord) {
if (typeof dailyStock !== 'undefined') {
inventoryRecord.totalStock = dailyStock; inventoryRecord.totalStock = dailyStock;
}
if (typeof availableStock !== 'undefined') {
inventoryRecord.availableStock = availableStock;
}
} else { } else {
em.create(Inventory, { em.create(Inventory, {
food: food, food: food,
availableStock: dailyStock, availableStock: newAvailableStock,
totalStock: dailyStock, totalStock: newTotalStock,
}); });
} }
} }