docs: update README, NEXT_STEPS, and technical description for category feature and auth
This commit is contained in:
+117
-26
@@ -53,16 +53,22 @@ docker exec recipe-db mariadb -uroot -p"LÖSENORD" recipe_app -e "SHOW TABLES;"
|
||||
- **Framework:** Next.js 16.2 (App Router, server + client components)
|
||||
- **Språk:** TypeScript 5.4.5
|
||||
- **UI:** React 19.2, ingen CSS-ramverk (ren CSS-in-JS och inline-stilar)
|
||||
- **Autentisering:** Auth.js v5 (next-auth beta), JWT-session, `auth()` i server components
|
||||
- **Bygg:** Standalone output, körs i Docker-container
|
||||
- **API-anrop:** Fetch mot backend och Next.js API routes
|
||||
- **API-anrop:** `fetchJson` (server-side med auth-headers) + Next.js API route-proxies (client-side)
|
||||
- **Felhantering:** Global parseErrorResponse utility, svenska felmeddelanden
|
||||
|
||||
> **Viktigt:** `Navigation.tsx` är en async server component som anropar `auth()`. Den får aldrig importeras av client components — rendera den alltid i `page.tsx` (server component).
|
||||
|
||||
### Frontend-sidor och komponenter
|
||||
|
||||
| Sida | Fil | Funktionalitet |
|
||||
|------|-----|---|
|
||||
| **Hem** | `app/page.tsx` | Startsida |
|
||||
| **Navigering** | `app/Navigation.tsx` | Huvudmeny |
|
||||
| **Navigering** | `app/Navigation.tsx` | Huvudmeny, inloggad användare, länk till profil |
|
||||
| **Inloggning** | `app/login/page.tsx` | Inloggningssida med Auth.js Credentials |
|
||||
| **Profil** | `app/profil/page.tsx` | Redigera firstName, lastName, email |
|
||||
| | `app/profil/ProfileClient.tsx` | Klientkomponent för profilformulär |
|
||||
| **Inventorie** | `app/inventory/page.tsx` | Lista, filtrera, sortera varor |
|
||||
| | `InventoryList.tsx` | Ritning av inventarieföremål |
|
||||
| | `InventoryForm.tsx` | Skapa nytt inventarieföremål |
|
||||
@@ -73,17 +79,21 @@ docker exec recipe-db mariadb -uroot -p"LÖSENORD" recipe_app -e "SHOW TABLES;"
|
||||
| | `actions.ts` | Server actions för inventarie |
|
||||
| **Recept** | `app/recipes/page.tsx` | Lista recept |
|
||||
| | `RecipePreview.tsx` | Receptförhandsvisning med inventariestatus |
|
||||
| **Lägg till recept** | `app/recipes/create/page.tsx` | Meny för receptskaping (val mellan två vägar) |
|
||||
| **Skriv in recept** | `app/recipes/write/page.tsx` | Startpunkt för Markdown-inmatning |
|
||||
| | `app/recipes/write/WriteRecipePage.tsx` | Komponenter för receptskapande (Markdown-baserat, 3-steg) |
|
||||
| **Lägg till recept** | `app/recipes/create/page.tsx` | Server component med Navigation |
|
||||
| | `app/recipes/create/CreateRecipeClient.tsx` | Klientkomponent: snabbimport + metodval |
|
||||
| **Skriv in recept** | `app/recipes/write/page.tsx` | Server component med Navigation |
|
||||
| | `app/recipes/write/WriteRecipePage.tsx` | Markdown-baserat receptskapande (3-steg) |
|
||||
| **Importera från fil** | `app/recipes/import/page.tsx` | Startpunkt för fil/länk-import |
|
||||
| | `app/recipes/import/ImportFilePage.tsx` | Komponenter för fil-/länk-import (PDF, URL, etc) |
|
||||
| | `app/recipes/import/ImportFilePage.tsx` | Fil-/länk-import (PDF, URL, etc) |
|
||||
| **Import (flikar)** | `app/import/page.tsx` | Server component med Navigation + flikvy |
|
||||
| | `app/import/ImportTabsClient.tsx` | Klientkomponent: kvitto/recept-flikar |
|
||||
| **Recipe detail** | `app/recipes/[id]/` | Enskilt recept (detaljer, redigering) |
|
||||
| **Admin: Produkter** | `app/admin/products/page.tsx` | Produktadmin-panel |
|
||||
| | `AdminProductList.tsx` | Lista produkter, sök, sortera |
|
||||
| | `EditProductForm.tsx` | Inline redigering av name, canonicalName, category + soft-delete |
|
||||
| | `EditProductForm.tsx` | Inline redigering: name, canonicalName, kategori (hierarkisk dropdown), brand, taggar |
|
||||
| | `ResetProductsButton.tsx` | Knapp för att rensa all produktdata |
|
||||
| | `MergePreviewForm.tsx` | Förhandsgranska merge |
|
||||
| | `actions.ts` | Server actions: updateProduct, deleteProduct |
|
||||
| | `actions.ts` | Server actions: updateProduct, deleteProduct, resetAllProducts |
|
||||
| **Baslager** | `app/baslager/page.tsx` | Visa och hantera baslager (server component) |
|
||||
| | `AddToPantryForm.tsx` | Lägg till produkt i baslager (dropdown) |
|
||||
| | `PantryList.tsx` | Visa baslager grupperat per kategori |
|
||||
@@ -91,15 +101,29 @@ docker exec recipe-db mariadb -uroot -p"LÖSENORD" recipe_app -e "SHOW TABLES;"
|
||||
|
||||
### API-proxy routes (Next.js)
|
||||
|
||||
Alla proxy-routes läser auth-token via `auth()` (Auth.js v5) och vidarebefordrar `Authorization: Bearer <token>` till backend.
|
||||
|
||||
| Route | Metod | Syfte |
|
||||
|-------|-------|-------|
|
||||
| `/api/quick-import-proxy` | POST | Proxies `POST /api/quick-import` för URL-, PDF- och bildimport |
|
||||
| `/api/parse-markdown-proxy` | POST | Proxies `POST /api/recipes/parse-markdown` (Markdown-tolkning för skriv-in-recept) |
|
||||
| `/api/inventory-history-proxy` | GET | Proxies konsumtionshistorik |
|
||||
| `/api/recipe-preview-proxy` | GET | Proxies receptförhandsvisning |
|
||||
| `/api/admin/merge-preview-proxy` | GET | Proxies produktmerge-preview |
|
||||
| `/api/products` | GET | Lista/proxies produkter |
|
||||
| `/api/recipes` | GET, POST | Lista recept + spara nytt recept (proxy till backend) |
|
||||
| `/api/auth/[...nextauth]` | GET, POST | Auth.js handlers (login, logout, session) |
|
||||
| `/api/products` | GET | Produktlista (auth-wrappat med `auth(req)`) |
|
||||
| `/api/categories` | GET | Kategorihierarki (publik, proxies `/api/categories/tree`) |
|
||||
| `/api/profile` | GET, PATCH | Hämta/uppdatera användarprofil |
|
||||
| `/api/recipes` | GET, POST | Lista recept + spara nytt |
|
||||
| `/api/quick-import-proxy` | POST | URL-, PDF- och bildimport |
|
||||
| `/api/parse-markdown-proxy` | POST | Markdown-tolkning för skriv-in-recept |
|
||||
| `/api/inventory-history-proxy` | GET | Konsumtionshistorik |
|
||||
| `/api/recipe-preview-proxy` | GET | Receptförhandsvisning |
|
||||
| `/api/admin/merge-preview-proxy` | GET | Produktmerge-preview |
|
||||
| `/api/receipt-import-proxy` | POST | Kvittoimport via Mistral AI |
|
||||
| `/api/user-products` | GET, POST, DELETE | Användarspecifika produkter |
|
||||
|
||||
### Autentisering (Auth.js v5)
|
||||
|
||||
- `auth.ts` — NextAuth-konfiguration med Credentials provider
|
||||
- `middleware.ts` — Skyddar alla routes utom `/login`, `/register` och `/api/auth`
|
||||
- `lib/auth-headers.ts` — `getAuthHeaders()` hämtar Bearer-token från session (server-side)
|
||||
- `lib/api.ts` — `fetchJson()` lägger automatiskt till auth-headers server-side, redirectar till `/login` vid 401
|
||||
|
||||
### Frontend utbyggbarhet
|
||||
|
||||
@@ -115,24 +139,41 @@ docker exec recipe-db mariadb -uroot -p"LÖSENORD" recipe_app -e "SHOW TABLES;"
|
||||
- **Språk:** TypeScript 5.4.5
|
||||
- **Databas:** MariaDB 11 (via Prisma 6.12.0 ORM)
|
||||
- **API:** REST, validering med class-validator
|
||||
- **Autentisering:** JWT (7 dagars token), JwtAuthGuard skyddar alla routes, `@Public()` dekorator för öppna endpoints
|
||||
- **Felhantering:** GlobalExceptionFilter (svenska felmeddelanden)
|
||||
- **Hälsokontroll:** /health endpoints
|
||||
- **Bygg:** `nest build`, körs i Docker-container
|
||||
|
||||
### Backend-moduler och strukturen läsa
|
||||
### Backend-moduler och strukturen
|
||||
|
||||
```
|
||||
backend/src/
|
||||
├── app.module.ts # Root module
|
||||
├── main.ts # Startpunkt (port 8080)
|
||||
├── main.ts # Startpunkt (port 8080, global prefix "api")
|
||||
├── auth/
|
||||
│ ├── auth.controller.ts # POST /api/auth/login
|
||||
│ ├── auth.service.ts # validateUser, login (JWT-signering)
|
||||
│ ├── auth.module.ts
|
||||
│ ├── jwt.strategy.ts # Passport JWT-strategi
|
||||
│ ├── jwt-auth.guard.ts # Global guard (skyddar allt utom @Public)
|
||||
│ └── decorators/
|
||||
│ └── public.decorator.ts # @Public() – markerar öppen endpoint
|
||||
├── users/
|
||||
│ ├── users.controller.ts # GET/PATCH /api/users/me
|
||||
│ ├── users.service.ts # findByUsername, create, updateProfile
|
||||
│ └── users.module.ts
|
||||
├── categories/
|
||||
│ ├── categories.controller.ts # GET /api/categories, GET /api/categories/tree (@Public)
|
||||
│ ├── categories.service.ts # findAll (flat), findTree (hierarkisk)
|
||||
│ └── categories.module.ts
|
||||
├── common/
|
||||
│ ├── filters/
|
||||
│ │ └── global-exception.filter.ts # Centraliserad felhantering
|
||||
│ └── utils/
|
||||
│ └── normalize-name.ts # Namnormalisering
|
||||
├── health/
|
||||
│ ├── health.controller.ts # GET /health, /health/db
|
||||
│ ├── health.service.ts # Hälsotillstånd-logik
|
||||
│ ├── health.controller.ts # GET /health, /health/db (@Public)
|
||||
│ ├── health.service.ts
|
||||
│ └── health.module.ts
|
||||
├── inventory/
|
||||
│ ├── inventory.controller.ts # CRUD endpoints
|
||||
@@ -146,8 +187,8 @@ backend/src/
|
||||
│ ├── prisma.service.ts # PrismaClient wrapper
|
||||
│ └── prisma.module.ts
|
||||
├── products/
|
||||
│ ├── products.controller.ts # CRUD, merge, duplicates
|
||||
│ ├── products.service.ts # Produktlogik
|
||||
│ ├── products.controller.ts # CRUD, merge, duplicates, reset-all
|
||||
│ ├── products.service.ts # Produktlogik inkl. resetAll()
|
||||
│ ├── products.module.ts
|
||||
│ └── dto/
|
||||
│ ├── create-product.dto.ts
|
||||
@@ -291,9 +332,23 @@ GET /api/products/merge-preview Förhandsgranska merge
|
||||
POST /api/products/merge Slå ihop två produkter
|
||||
PATCH /api/products/:id/canonical-name Uppdatera canonical name
|
||||
POST /api/products/backfill-canonical Backfill canonical names (admin)
|
||||
POST /api/products/reset-all Rensa all produktdata (admin)
|
||||
```
|
||||
|
||||
### 🛀 Baslager-endpoints
|
||||
### Kategori-endpoints
|
||||
```
|
||||
GET /api/categories Flat lista av alla kategorier (@Public)
|
||||
GET /api/categories/tree Hierarkiskt träd (@Public)
|
||||
```
|
||||
|
||||
### Användar-endpoints
|
||||
```
|
||||
POST /api/auth/login Logga in, returnerar JWT (@Public)
|
||||
GET /api/users/me Hämta inloggad användares profil
|
||||
PATCH /api/users/me Uppdatera firstName, lastName, email
|
||||
```
|
||||
|
||||
### Baslager-endpoints
|
||||
```
|
||||
GET /api/pantry Lista alla baslagerartiklar (inkl. produktinfo)
|
||||
POST /api/pantry Lägg till produkt i baslagret
|
||||
@@ -304,6 +359,36 @@ DELETE /api/pantry/:id Ta bort produkt från baslagret
|
||||
|
||||
## Datamodell (Prisma ORM)
|
||||
|
||||
### User
|
||||
```prisma
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
username String @unique
|
||||
email String @unique
|
||||
firstName String?
|
||||
lastName String?
|
||||
passwordHash String
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
```
|
||||
|
||||
### Category
|
||||
```prisma
|
||||
model Category {
|
||||
id Int @id @default(autoincrement())
|
||||
name String
|
||||
parentId Int?
|
||||
parent Category? @relation("CategoryTree", ...) # Förälder (null = toppnivå)
|
||||
children Category[] @relation("CategoryTree") # Underkategorier
|
||||
products Product[]
|
||||
|
||||
@@unique([name, parentId])
|
||||
}
|
||||
```
|
||||
Hierarkin har 3 nivåer: **Huvudkategori → Underkategori → Typ**
|
||||
Exempelträd: `Mejeri, ost & ägg → Mjölk → Laktosfri mjölk`
|
||||
|
||||
### Product
|
||||
```prisma
|
||||
model Product {
|
||||
@@ -311,14 +396,20 @@ model Product {
|
||||
name String # Visningsnamn
|
||||
normalizedName String @unique # Normaliserat namn (lowercase, utan skiljetecken)
|
||||
canonicalName String? # Canonical namn för receptmatchning
|
||||
category String? # Produktkategori
|
||||
isActive Boolean @default(true) # Soft-delete flag
|
||||
deletedAt DateTime? # Tidpunkt för soft-delete
|
||||
category String? # Fritext-kategori (äldre fält, ersätts av categoryRef)
|
||||
subcategory String? # Fritext-underkategori (äldre fält)
|
||||
brand String? # Varumärke
|
||||
categoryId Int? # FK till Category (ny hierarki)
|
||||
categoryRef Category? # Relation till Category
|
||||
isActive Boolean @default(true)
|
||||
deletedAt DateTime?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
inventoryItems InventoryItem[]
|
||||
inventoryItems InventoryItem[]
|
||||
recipeIngredients RecipeIngredient[]
|
||||
tags ProductTag[]
|
||||
nutrition Nutrition?
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user