Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ yet, depending on when the review is made.
| Wine detail page | Done | Facts, food pairing, the shared average, the rating distribution and student reviews at `/vin/:id` |
| Student reviews (stored in the database) | Done | 0.5–5 stars and an optional comment, saved and shown at once on the wine page. They count in the shared average |
| «Mine anmeldelser» page | Done | Shows the «Vil prøve» list. Own reviews come with login in the interface |
| User accounts in the backend | Done | Register, login, logout and «who am I», with sessions in PostgreSQL. Not in the interface yet |
| User accounts | Done | Register, log in and log out at `/konto`, with sessions in PostgreSQL and an `httpOnly` cookie |
| Reviews tied to accounts | Done | Writing a review requires login, and the username is shown as the author. Older anonymous reviews are kept as «Anonym student» |
| Larger dataset on the VM | Done | Slim version: 1 007 wines and 150 000 ratings |
| Continuous integration (CI) | Not yet started | Needs a self-hosted runner |

Expand Down Expand Up @@ -110,8 +111,8 @@ This section describes what users can do. "Backend" means the API supports the f
| Student reviews | Done | Done | 0.5–5 stars in half stars and an optional comment, stored in PostgreSQL. Hover the left half of a star for half a star, or use the arrow keys. The page updates at once without a reload. One review per student per wine |
| Shared average and distribution | Done | Done | Student reviews count in the average together with X-Wines, also when sorting and filtering. The wine page shows how many gave each score |
| Wishlist ("Vil prøve") | Planned | Done | Saved in the browser and shown on «Mine anmeldelser». Moves to the backend when login is in the interface |
| User accounts | Done | Planned | Register, login, logout and `me` in the API. Sessions in PostgreSQL with an `httpOnly` cookie. The interface comes next |
| Anonymous ID | Done | Done | Reviews use a random ID stored in the browser until login is in the interface |
| User accounts | Done | Done | Register, log in and log out at `/konto`. Errors are shown next to the field they are about, in Norwegian. Sessions in PostgreSQL with an `httpOnly` cookie |
| Reviews with login | Done | Done | Only logged-in users can write, change or delete a review, and only their own. Visitors see the reviews and a link to log in, which brings them back to the wine. `User.reviews` lists a user's reviews for the profile page |

## Architecture and technology choices

Expand Down Expand Up @@ -246,7 +247,7 @@ We follow the principle from the course: measure, set a budget and show the numb
### Technical choices

- Code splitting per page with `React.lazy`. The landing page does not load the code for search and the detail page.
- The landing page loads all its data in one GraphQL query, with only the fields it shows. An end-to-end test checks that only one request is sent.
- The landing page loads all its content in one GraphQL query, with only the fields it shows. The only other request is `Me` (who is logged in), which every page shares and Apollo caches. An end-to-end test checks both.
- Search, filtering, sorting and pagination run in the database. A page returns at most 50 wines.
- The rating average is stored on the wine, so no query counts millions of rows.
- DataLoader batches the lookups a page needs, so a page of wines runs one query per field instead of one per wine.
Expand Down Expand Up @@ -278,7 +279,7 @@ and because the test was run over VPN. These are the "before" numbers. Compressi
| Unit and components (frontend) | Vitest, React Testing Library, user-event, vitest-axe | URL parameters, formatting, mapping of API data, data hooks, components, the landing page, the wine page with reviews, the wishlist page, loading, empty and error states, and axe on each component | 269 in 28 files |
| Unit (backend) | Vitest | Parsing of X-Wines CSV files, building SQL for search, filtering and sorting, review validation, and passwords, sessions and rate limiting | 92 in 11 files (6 skipped: they need a test database) |
| End-to-end | Playwright with axe-core, desktop and mobile | Landing, search and detail pages with a stubbed API: one request per page load, debounce, search, filters, sorting, load more, reload, back button, from search to wine and back, the filter dialog on mobile, no hits, wine not found, API errors, keyboard use, reflow at 320 px, and no axe violations (WCAG 2.1 AA) | 50 (24 tests in 2 browser projects, and 2 for mobile only) |
| API | Not set up yet | Resolvers against a test database: search, filtering, sorting and pagination | Planned |
| API | Vitest with Apollo Server (`executeOperation`) | Login and reviews through real GraphQL requests, with a fake browser per user that keeps the cookie. The review tests run against a separate test database (`TEST_DATABASE_URL`), see [API](docs/api.md#database-tests) | Count again |

Counted on 16 November 2026. The skipped backend tests run the user store against a real database, and need `TEST_DATABASE_URL`.

Expand Down Expand Up @@ -367,7 +368,9 @@ Claude and ChatGPT are the main models used. The usecases for the AI tools are a

### Next: towards the final submission

- [ ] Login in the interface: forms for register and login, and reviews tied to the account instead of the anonymous ID.
- [x] Login in the interface: forms for register and login, and reviews tied to the account instead of the anonymous ID.
- [ ] Favourites with a top 4, «Vil prøve» and «Har prøvd» stored per user.
- [ ] Profile page with the top 4, the lists and the user's reviews.
- [ ] Wishlist stored in the backend, so it follows the user between devices.
- [ ] Calmer landing page: less text, a separate «Om» page, and a burger menu on mobile.
- [ ] CI, an enforced coverage threshold, and eslint-plugin-jsx-a11y.
Expand All @@ -391,7 +394,8 @@ Claude and ChatGPT are the main models used. The usecases for the AI tools are a
- Search matches the text as one phrase, so "noir pinot" finds nothing. Country is not part of the search text, because the country filter covers it.
- The cursors in the API hold an offset, which is slower deep into large results. To be solved together with the larger dataset.
- The database and its user are still called `vinmonopolet` and `vinmonopolet_app`, from before we switched dataset. Renaming would mean everyone rebuilding their database, so we kept the names.
- Anyone can create any number of anonymous IDs, so there is nothing stopping someone from leaving many reviews. Login will fix that.
- Reviews from before login have no account and are shown as «Anonym student». The old `student` table they belong to will be removed later.
- One e2e test, checking «Rødvin» in the filter dialog on mobile, fails since the error handling changes. Tracked in its own issue.

<!--
Remove known issues as they are fixed.
Expand All @@ -402,6 +406,7 @@ Remove known issues as they are fixed.
- [Setup](docs/setup.md): Git, dependencies, `.env` and local start
- [Database](docs/database.md): PostgreSQL on macOS, Windows and Ubuntu
- [VM](docs/vm.md): deployment, Apache and running the backend with `nohup`
- [API](docs/api.md): GraphQL queries, user accounts and the X-Wines citation
- [API](docs/api.md): GraphQL queries, user accounts, reviews, database tests and the X-Wines citation
- [Manual testing](docs/manual-testing.md): how to test login, reviews and other user data by hand
- [Workflow](docs/workflow.md): branches, reviews and checks
- [Tasks](docs/tasks.md): requirements and sketches
30 changes: 30 additions & 0 deletions backend/migrations/1794859847582_tie-reviews-to-users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- Up Migration

-- Reviews now belong to a user account. Reviews written before login keep their anonymous
-- student and are shown as "Anonym student". New reviews always get a user_id.
-- The student table is removed in a later migration, once nothing reads it.
ALTER TABLE review
-- Deleting a user deletes their reviews. If account deletion is added, recalculate the
-- rating stats for the affected wines afterwards (UPDATE_WINE_RATING_STATS in rating-stats.ts).
ADD COLUMN user_id integer REFERENCES app_user (id) ON DELETE CASCADE,
ALTER COLUMN student_id DROP NOT NULL,
-- Every review has exactly one author: an account, or (old reviews) an anonymous student.
ADD CONSTRAINT review_one_author CHECK (num_nonnulls(user_id, student_id) = 1),
-- One review per user per wine, the same rule as for students.
ADD CONSTRAINT review_wine_user_key UNIQUE (wine_id, user_id);

-- For the reviews on a user's profile, newest first.
CREATE INDEX review_user_updated ON review (user_id, updated_at DESC);

-- Down Migration

-- Reviews written with an account have no student, so they cannot be kept when going back.
-- The rating stats on the wine table are not recalculated here. Run the seed script again
-- (it ends with UPDATE_ALL_RATING_STATS) if the averages must be exact after going back.
DELETE FROM review WHERE user_id IS NOT NULL;
DROP INDEX review_user_updated;
ALTER TABLE review
DROP CONSTRAINT review_wine_user_key,
DROP CONSTRAINT review_one_author,
ALTER COLUMN student_id SET NOT NULL,
DROP COLUMN user_id;
22 changes: 17 additions & 5 deletions backend/src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ export interface WineRow {
community_count: number;
}

// A row from the review table.
// A row from the review table. Exactly one of user_id and student_id is set:
// user_id for reviews written with an account, student_id for old anonymous reviews.
export interface ReviewRow {
id: number;
wine_id: number;
student_id: number;
user_id: number | null;
student_id: number | null;
rating: string; // pg returns numeric columns as strings
comment: string | null;
created_at: Date;
Expand Down Expand Up @@ -68,7 +70,7 @@ export function createLoaders() {
});
});

const ratingDistributions = new DataLoader<number, RatingCount[]>(async (wineIds) => {
const ratingDistributions = new DataLoader<number, RatingCount[]>(async (wineIds) => {
const result = await pool.query<{ wine_id: number; rating: number; count: number }>(
`SELECT wine_id, rating::float AS rating, count(*)::int AS count
FROM (
Expand Down Expand Up @@ -96,7 +98,7 @@ export function createLoaders() {
loader = new DataLoader<number, ReviewRow[]>(async (wineIds) => {
// row_number() numbers each wine's reviews, newest first, so we keep the first N per wine.
const result = await pool.query<ReviewRow>(
`SELECT id, wine_id, student_id, rating, comment, created_at, updated_at
`SELECT id, wine_id, user_id, student_id, rating, comment, created_at, updated_at
FROM (
SELECT *, row_number() OVER (PARTITION BY wine_id ORDER BY updated_at DESC, id DESC) AS position
FROM review
Expand All @@ -113,7 +115,17 @@ export function createLoaders() {
return loader;
}

return { wines, studentRatings, ratingDistributions, reviews };
// Only the username is read, never the password hash.
const usernames = new DataLoader<number, string | null>(async (userIds) => {
const result = await pool.query<{ id: number; username: string }>(
'SELECT id, username FROM app_user WHERE id = ANY($1)',
[[...userIds]],
);
const byId = new Map(result.rows.map((row) => [row.id, row.username]));
return userIds.map((id) => byId.get(id) ?? null);
});

return { wines, studentRatings, ratingDistributions, reviews, usernames };
}

export type Loaders = ReturnType<typeof createLoaders>;
Loading