feat: implement user-specific inventory management with security checks
Test Suite / test (24.15.0) (push) Has been cancelled

This commit is contained in:
Nils-Johan Gynther
2026-05-07 11:58:00 +02:00
parent 4affb03504
commit 17893824d5
8 changed files with 181 additions and 31 deletions
@@ -0,0 +1,18 @@
ALTER TABLE `InventoryItem`
ADD COLUMN `userId` INT NULL;
UPDATE `InventoryItem` AS ii
JOIN `Product` AS p ON p.`id` = ii.`productId`
SET ii.`userId` = p.`ownerId`
WHERE ii.`userId` IS NULL;
SET @fallback_user_id := (SELECT `id` FROM `User` ORDER BY `id` ASC LIMIT 1);
UPDATE `InventoryItem`
SET `userId` = @fallback_user_id
WHERE `userId` IS NULL;
ALTER TABLE `InventoryItem`
MODIFY COLUMN `userId` INT NOT NULL,
ADD INDEX `InventoryItem_userId_idx` (`userId`),
ADD CONSTRAINT `InventoryItem_userId_fkey`
FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
+4
View File
@@ -25,6 +25,7 @@ model User {
ownedRecipes Recipe[] @relation("RecipeOwner")
sharedRecipes RecipeShare[]
ownedProducts Product[]
inventoryItems InventoryItem[]
pantryItems PantryItem[]
mealPlanEntries MealPlanEntry[]
receiptAliases ReceiptAlias[]
@@ -91,6 +92,7 @@ model UserProduct {
model InventoryItem {
id Int @id @default(autoincrement())
userId Int
productId Int
quantity Decimal @db.Decimal(10, 2)
unit String
@@ -107,9 +109,11 @@ model InventoryItem {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
consumptions InventoryConsumption[]
@@index([userId])
@@index([productId])
}