crone for failed payment orders
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsArray, ValidateNested, ArrayMinSize, IsString, IsNumber, Min, IsDate } from 'class-validator';
|
||||
import { IsNotEmpty, IsArray, ValidateNested, ArrayMinSize, IsString, IsNumber, Min } from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
export class BulkReserveFoodItemDto {
|
||||
@@ -14,7 +14,6 @@ export class BulkReserveFoodItemDto {
|
||||
@Min(1)
|
||||
@Type(() => Number)
|
||||
quantity!: number;
|
||||
|
||||
}
|
||||
|
||||
export class BulkReserveFoodDto {
|
||||
@@ -22,7 +21,7 @@ export class BulkReserveFoodDto {
|
||||
description: 'Array of food reservations to create',
|
||||
type: [BulkReserveFoodItemDto],
|
||||
example: [
|
||||
{ foodId: 'food-123', quantity: 5 },
|
||||
{ foodId: 'food-123', quantity: 5 },
|
||||
{ foodId: 'food-789', quantity: 3 },
|
||||
],
|
||||
})
|
||||
|
||||
@@ -6,12 +6,10 @@ import { BulkReserveFoodDto } from './dto/bulk-reserve-food.dto';
|
||||
import { Inventory } from './entities/inventory.entity';
|
||||
import { Food } from '../foods/entities/food.entity';
|
||||
import { Restaurant } from '../restaurants/entities/restaurant.entity';
|
||||
import { LockMode } from '@mikro-orm/core';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class InventoryService {
|
||||
constructor(private readonly em: EntityManager) { }
|
||||
constructor(private readonly em: EntityManager) {}
|
||||
|
||||
async setStockForFood(foodId: string, restaurantId: string, setStockDto: SetStockDto): Promise<Inventory> {
|
||||
// Validate that availableStock doesn't exceed totalStock
|
||||
@@ -58,7 +56,6 @@ export class InventoryService {
|
||||
}
|
||||
|
||||
async bulkSetStockForFoods(restaurantId: string, bulkSetStockDto: BulkSetStockDto): Promise<Inventory[]> {
|
||||
|
||||
const { items } = bulkSetStockDto;
|
||||
// Validate all items first
|
||||
for (const item of items) {
|
||||
@@ -132,12 +129,8 @@ export class InventoryService {
|
||||
return results;
|
||||
}
|
||||
|
||||
async deductFromInventory(
|
||||
em: EntityManager,
|
||||
bulkReserveFoodDto: BulkReserveFoodDto,
|
||||
): Promise<Inventory[]> {
|
||||
async deductFromInventory(em: EntityManager, bulkReserveFoodDto: BulkReserveFoodDto): Promise<Inventory[]> {
|
||||
return em.transactional(async em => {
|
||||
|
||||
const { items } = bulkReserveFoodDto;
|
||||
// Get all unique food IDs
|
||||
const foodIds = [...new Set(items.map(item => item.foodId))];
|
||||
@@ -189,7 +182,46 @@ export class InventoryService {
|
||||
}
|
||||
return inventories;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
async restoreToInventory(em: EntityManager, bulkRestoreDto: BulkReserveFoodDto): Promise<Inventory[]> {
|
||||
return em.transactional(async em => {
|
||||
const { items } = bulkRestoreDto;
|
||||
|
||||
const foodIds = [...new Set(items.map(item => item.foodId))];
|
||||
|
||||
const foods = await em.find(Food, { id: { $in: foodIds } });
|
||||
const foodMap = new Map<string, Food>();
|
||||
for (const food of foods) {
|
||||
foodMap.set(food.id, food);
|
||||
}
|
||||
|
||||
const missingFoodIds = foodIds.filter(id => !foodMap.has(id));
|
||||
if (missingFoodIds.length > 0) {
|
||||
throw new NotFoundException(`Foods not found: ${missingFoodIds.join(', ')}`);
|
||||
}
|
||||
|
||||
const existingInventories = await em.find(Inventory, {
|
||||
food: { id: { $in: foodIds } },
|
||||
});
|
||||
|
||||
const inventoryMap = new Map<string, Inventory>();
|
||||
for (const inventory of existingInventories) {
|
||||
inventoryMap.set(inventory.food.id, inventory);
|
||||
}
|
||||
|
||||
const inventories: Inventory[] = [];
|
||||
for (const item of items) {
|
||||
const inventory = inventoryMap.get(item.foodId);
|
||||
if (!inventory) {
|
||||
throw new NotFoundException(`Inventory not found for food ${item.foodId}`);
|
||||
}
|
||||
inventory.availableStock += item.quantity;
|
||||
inventories.push(inventory);
|
||||
em.persist(inventory);
|
||||
}
|
||||
|
||||
return inventories;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user