feat: make pantry items and meal plan entries user-scoped; update related services and controllers

This commit is contained in:
Nils-Johan Gynther
2026-04-22 18:38:04 +02:00
parent 44b4e7ad73
commit 4482129fca
6 changed files with 123 additions and 35 deletions
@@ -0,0 +1,25 @@
-- PantryItem: make user-scoped
ALTER TABLE `PantryItem` ADD COLUMN `userId` INTEGER NULL;
UPDATE `PantryItem`
SET `userId` = (SELECT `id` FROM `User` ORDER BY `id` ASC LIMIT 1)
WHERE `userId` IS NULL;
ALTER TABLE `PantryItem` DROP INDEX `PantryItem_productId_key`;
ALTER TABLE `PantryItem` MODIFY `userId` INTEGER NOT NULL;
ALTER TABLE `PantryItem` ADD INDEX `PantryItem_userId_idx`(`userId`);
ALTER TABLE `PantryItem` ADD UNIQUE INDEX `PantryItem_userId_productId_key`(`userId`, `productId`);
ALTER TABLE `PantryItem` ADD CONSTRAINT `PantryItem_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
-- MealPlanEntry: make user-scoped
ALTER TABLE `MealPlanEntry` ADD COLUMN `userId` INTEGER NULL;
UPDATE `MealPlanEntry`
SET `userId` = (SELECT `id` FROM `User` ORDER BY `id` ASC LIMIT 1)
WHERE `userId` IS NULL;
ALTER TABLE `MealPlanEntry` DROP INDEX `MealPlanEntry_date_key`;
ALTER TABLE `MealPlanEntry` MODIFY `userId` INTEGER NOT NULL;
ALTER TABLE `MealPlanEntry` ADD INDEX `MealPlanEntry_userId_idx`(`userId`);
ALTER TABLE `MealPlanEntry` ADD UNIQUE INDEX `MealPlanEntry_userId_date_key`(`userId`, `date`);
ALTER TABLE `MealPlanEntry` ADD CONSTRAINT `MealPlanEntry_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
+12 -2
View File
@@ -23,6 +23,8 @@ model User {
ownedRecipes Recipe[] @relation("RecipeOwner")
sharedRecipes RecipeShare[]
ownedProducts Product[]
pantryItems PantryItem[]
mealPlanEntries MealPlanEntry[]
}
model Product {
@@ -160,10 +162,15 @@ model RecipeIngredient {
model PantryItem {
id Int @id @default(autoincrement())
productId Int @unique
userId Int
productId Int
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([userId, productId])
@@index([userId])
}
model ReceiptAlias {
@@ -176,14 +183,17 @@ model ReceiptAlias {
model MealPlanEntry {
id Int @id @default(autoincrement())
userId Int
date DateTime @db.Date
recipe Recipe @relation(fields: [recipeId], references: [id], onDelete: Cascade)
recipeId Int
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
servings Int?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([date])
@@unique([userId, date]) # Bara ett recept per dag och användare
@@index([userId])
@@index([date])
}