diff --git a/backend/db/migrations/002_stores.sql b/backend/db/migrations/002_stores.sql new file mode 100644 index 0000000..997b57b --- /dev/null +++ b/backend/db/migrations/002_stores.sql @@ -0,0 +1,28 @@ +-- 002_stores.sql +-- Butikknavn for kjedene i Kassalapp-dataene. +-- Dumpen har bare storeCode, ikke navnet, så kjedene legges inn som startdata. +-- import.ts oppdaterer navnene senere når det kjøres en ny import. +-- Fila tåler å kjøres flere ganger (IF NOT EXISTS og ON CONFLICT). + +CREATE TABLE IF NOT EXISTS stores ( + code TEXT PRIMARY KEY, -- samme kode som prices.store_code, f.eks. KIWI + name TEXT NOT NULL -- navnet som vises i appen, f.eks. Kiwi +); + +INSERT INTO stores (code, name) VALUES + -- Dagligvarekjeder + ('BUNNPRIS', 'Bunnpris'), + ('COOP_NO', 'Coop'), + ('JOKER_NO', 'Joker'), + ('KIWI', 'Kiwi'), + ('MENY_NO', 'Meny'), + ('SPAR_NO', 'Spar'), + -- Nettbutikker og andre + ('ENGROSSNETT_NO', 'Engrossnett'), + ('EUROPRIS_NO', 'Europris'), + ('FUDI', 'Fudi'), + ('HAVARISTEN', 'Havaristen'), + ('HOLDBART', 'Holdbart'), + ('ODA_NO', 'Oda'), + ('PERREROBERT', 'Pierre Robert') -- koden er stavet slik hos Kassalapp +ON CONFLICT (code) DO NOTHING; \ No newline at end of file diff --git a/backend/src/resolvers/filters.ts b/backend/src/resolvers/filters.ts index f6b69a3..c43174b 100644 --- a/backend/src/resolvers/filters.ts +++ b/backend/src/resolvers/filters.ts @@ -1,4 +1,4 @@ -import { pool } from '../db.js'; +import { pool } from "../db.js"; export const categoryFilterCounts = async ( _parent: unknown, @@ -91,11 +91,13 @@ export const storeFilterCounts = async ( ) SELECT pr.store_code AS value, - pr.store_code AS label, + COALESCE(s.name, pr.store_code) AS label, COUNT(DISTINCT p.ean)::integer AS count FROM prices pr INNER JOIN products p ON p.ean = pr.ean + LEFT JOIN stores s + ON s.code = pr.store_code WHERE ( $1::text IS NULL @@ -107,7 +109,7 @@ export const storeFilterCounts = async ( SELECT id FROM selected_categories ) ) - GROUP BY pr.store_code + GROUP BY pr.store_code, s.name HAVING COUNT(DISTINCT p.ean) > 0 ORDER BY count DESC, value `, diff --git a/backend/src/resolvers/products.ts b/backend/src/resolvers/products.ts index 681b1ee..4023bfc 100644 --- a/backend/src/resolvers/products.ts +++ b/backend/src/resolvers/products.ts @@ -238,17 +238,22 @@ export const productCategory = async (product: { categoryId?: number | null }) = return result.rows[0] ?? null; }; +// Prisene for én vare, billigst først. +// Butikknavnet hentes fra stores. Mangler butikken der, vises koden i stedet. export const productPrices = async (product: { ean: string }) => { const result = await pool.query( ` SELECT - store_code AS "storeCode", - price, - unit_price AS "unitPrice", - url - FROM prices - WHERE ean = $1 - ORDER BY price + pr.store_code AS "storeCode", + COALESCE(s.name, pr.store_code) AS "storeName", + pr.price, + pr.unit_price AS "unitPrice", + pr.url + FROM prices pr + LEFT JOIN stores s + ON s.code = pr.store_code + WHERE pr.ean = $1 + ORDER BY pr.price `, [product.ean], ); diff --git a/backend/src/resolvers/resolvers.ts b/backend/src/resolvers/resolvers.ts index e78abd6..7b231a6 100644 --- a/backend/src/resolvers/resolvers.ts +++ b/backend/src/resolvers/resolvers.ts @@ -29,6 +29,7 @@ import { updateItemNote, updateItemQuantity, } from './shoppingLists.js'; +import { stores } from './stores.js'; // Samler resolverne fra hvert område, så alle spørringer og mutasjoner står på ett sted export const resolvers = { @@ -37,6 +38,7 @@ export const resolvers = { productsCount, product, categories, + stores, shoppingList, myShoppingLists, categoryFilterCounts, diff --git a/backend/src/resolvers/stores.ts b/backend/src/resolvers/stores.ts new file mode 100644 index 0000000..f05bf29 --- /dev/null +++ b/backend/src/resolvers/stores.ts @@ -0,0 +1,13 @@ +import { pool } from "../db.js"; + +// Alle butikkjedene med navn, sortert alfabetisk. +// Brukes av frontend til å vise navn der den bare har butikkoden. +export const stores = async () => { + const result = await pool.query(` + SELECT code, name + FROM stores + ORDER BY name + `); + + return result.rows; +}; diff --git a/backend/src/schema.ts b/backend/src/schema.ts index e707c2c..4f728af 100644 --- a/backend/src/schema.ts +++ b/backend/src/schema.ts @@ -11,8 +11,14 @@ export const typeDefs = `#graphql count: Int! } + type Store { + code: String! + name: String! + } + type Price { storeCode: String! + storeName: String! price: Float! unitPrice: Float url: String @@ -26,7 +32,6 @@ export const typeDefs = `#graphql categoryId: ID category: Category prices: [Price!]! - ingredients: String nutrition: [Nutrition!]! allergens: [Allergen!]! @@ -95,6 +100,7 @@ export const typeDefs = `#graphql product(ean: String!): Product categories: [Category!]! + stores: [Store!]! shoppingList(id: ID!): ShoppingList myShoppingLists(status: ShoppingListStatus): [ShoppingList!]! diff --git a/frontend/src/hooks/useStoreNames.ts b/frontend/src/hooks/useStoreNames.ts new file mode 100644 index 0000000..9bbbecc --- /dev/null +++ b/frontend/src/hooks/useStoreNames.ts @@ -0,0 +1,44 @@ +import { useMemo } from "react"; +import { gql } from "@apollo/client"; +import type { TypedDocumentNode } from "@apollo/client"; +import { useQuery } from "@apollo/client/react"; + +// ---------- Spørring ---------- + +type Store = { + code: string; + name: string; +}; + +type StoresData = { + stores: Store[]; +}; + +const STORES_QUERY: TypedDocumentNode = gql` + query Stores { + stores { + code + name + } + } +`; + +// ---------- Hook ---------- + +// Gir navnet på en butikk ut fra koden, f.eks. "MENY_NO" → "Meny". +// Navnene hentes fra databasen (tabellen stores). Apollo husker svaret, +// så spørringen kjøres bare én gang selv om hooken brukes mange steder. +// Mangler navnet (mens den laster, eller ukjent butikk), vises koden. +export function useStoreNames() { + const { data } = useQuery(STORES_QUERY); + + // Oppslag fra kode til navn. Bygges bare på nytt når dataene endres. + const storeNames = useMemo( + () => new Map((data?.stores ?? []).map((store) => [store.code, store.name])), + [data], + ); + + const getStoreName = (code: string) => storeNames.get(code) ?? code; + + return { getStoreName }; +} \ No newline at end of file diff --git a/frontend/src/pages/SearchResults/SearchEmptyState.tsx b/frontend/src/pages/SearchResults/SearchEmptyState.tsx index 9264e0f..1bcbda5 100644 --- a/frontend/src/pages/SearchResults/SearchEmptyState.tsx +++ b/frontend/src/pages/SearchResults/SearchEmptyState.tsx @@ -1,5 +1,6 @@ import type { FilterCount } from "../../types/filters"; +import { useStoreNames } from "../../hooks/useStoreNames"; type SearchEmptyStateProps = { searchTerm: string; @@ -10,19 +11,6 @@ type SearchEmptyStateProps = { onResetFilters: () => void; }; -const storeNames: Record = { - KIWI: "Kiwi", - MENY_NO: "Meny", - SPAR_NO: "Spar", - BUNNPRIS: "Bunnpris", - COOP_NO: "Coop", - JOKER_NO: "Joker", - ODA_NO: "Oda", - EUROPRIS_NO: "Europris", - ENGROSSNETT_NO: "Engrosnett", - HAVARISTEN: "Havaristen", -}; - export default function SearchEmptyState({ searchTerm, hasActiveFilters, @@ -31,9 +19,9 @@ export default function SearchEmptyState({ onSelectStore, onResetFilters, }: SearchEmptyStateProps) { - const selectedStoreNames = selectedStores.map( - (code) => storeNames[code] ?? code, - ); + const { getStoreName } = useStoreNames(); + + const selectedStoreNames = selectedStores.map(getStoreName); const storeDescription = selectedStoreNames.length === 1 @@ -108,8 +96,7 @@ export default function SearchEmptyState({ } > - {storeNames[store.value] ?? - store.label} + {store.label} diff --git a/frontend/src/pages/SearchResults/SearchFilters.tsx b/frontend/src/pages/SearchResults/SearchFilters.tsx index e5b00c2..9abc61c 100644 --- a/frontend/src/pages/SearchResults/SearchFilters.tsx +++ b/frontend/src/pages/SearchResults/SearchFilters.tsx @@ -11,18 +11,6 @@ type SearchFiltersProps = { onStoresChange: (values: string[]) => void; }; -const storeNames: Record = { - KIWI: "Kiwi", - MENY_NO: "Meny", - SPAR_NO: "Spar", - BUNNPRIS: "Bunnpris", - COOP_NO: "Coop", - JOKER_NO: "Joker", - ODA_NO: "Oda", - EUROPRIS_NO: "Europris", - ENGROSSNETT_NO: "Engrosnett", -}; - const collator = new Intl.Collator("nb-NO", { sensitivity: "base", }); @@ -43,8 +31,8 @@ export default function SearchFilters({ const sortedStores = [...stores].sort((a, b) => collator.compare( - storeNames[a.value] ?? a.label, - storeNames[b.value] ?? b.label, + a.label, + b.label, ), ); @@ -94,7 +82,7 @@ export default function SearchFilters({ {sortedStores.map((store) => ( diff --git a/frontend/src/pages/SearchResults/SearchProductList.tsx b/frontend/src/pages/SearchResults/SearchProductList.tsx index 49cd793..cb9dfee 100644 --- a/frontend/src/pages/SearchResults/SearchProductList.tsx +++ b/frontend/src/pages/SearchResults/SearchProductList.tsx @@ -16,18 +16,6 @@ const formatter = new Intl.NumberFormat("nb-NO", { maximumFractionDigits: 2, }); -const storeNames: Record = { - KIWI: "Kiwi", - MENY_NO: "Meny", - SPAR_NO: "Spar", - BUNNPRIS: "Bunnpris", - COOP_NO: "Coop", - JOKER_NO: "Joker", - ODA_NO: "Oda", - EUROPRIS_NO: "Europris", - ENGROSSNETT_NO: "Engrosnett", -}; - function getDisplayPrice( prices: ProductPrice[], selectedStores: string[], @@ -90,8 +78,7 @@ export default function SearchProductList({ } chain={ displayPrice - ? (storeNames[displayPrice.storeCode] ?? - displayPrice.storeCode) + ? displayPrice.storeName : undefined } onAdd={onAddProduct ? () => onAddProduct(product) : undefined diff --git a/frontend/src/pages/SearchResults/SearchResultsPage.tsx b/frontend/src/pages/SearchResults/SearchResultsPage.tsx index bf6a522..ca427e0 100644 --- a/frontend/src/pages/SearchResults/SearchResultsPage.tsx +++ b/frontend/src/pages/SearchResults/SearchResultsPage.tsx @@ -32,6 +32,7 @@ import type { SearchSort, FilterCount } from "../../types/filters"; import "./SearchResultsPage.css"; import SearchEmptyState from "./SearchEmptyState"; +import { useStoreNames } from "../../hooks/useStoreNames"; import { useSearchParams } from "react-router"; @@ -53,22 +54,6 @@ const CATEGORY_NAMES_QUERY: TypedDocumentNode = gql` } `; -const storeNames: Record = { - KIWI: "Kiwi", - MENY_NO: "Meny", - SPAR_NO: "Spar", - BUNNPRIS: "Bunnpris", - COOP_NO: "Coop", - JOKER_NO: "Joker", - ODA_NO: "Oda", - EUROPRIS_NO: "Europris", - ENGROSSNETT_NO: "Engrosnett", - HOLDBART: "Holdbart", - HAVARISTEN: "Havaristen", - FUDI: "Fudi", - PERREROBERT: "Perrerobert", -}; - const collator = new Intl.Collator("nb-NO", { sensitivity: "base", }); @@ -110,6 +95,7 @@ function normalizeProducts( categoryId?: string | null; prices?: ReadonlyArray<{ storeCode?: string; + storeName?: string; price?: number; unitPrice?: number | null; } | null>; @@ -136,6 +122,10 @@ function normalizeProducts( return [{ storeCode: price.storeCode, + storeName: + typeof price.storeName === "string" + ? price.storeName + : price.storeCode, price: price.price, unitPrice: typeof price.unitPrice === "number" @@ -158,6 +148,8 @@ function normalizeProducts( export default function SearchResultsPage() { const [searchParams] = useSearchParams(); + // Butikknavn fra databasen, f.eks. "MENY_NO" → "Meny" + const { getStoreName } = useStoreNames(); const urlSearchTerm = searchParams.get("q"); // Hent lagrede innstillinger kun ved første rendering. @@ -417,16 +409,15 @@ export default function SearchResultsPage() { ...selectedStores, ].sort((a, b) => collator.compare( - storeNames[a] ?? a, - storeNames[b] ?? b, + getStoreName(a), + getStoreName(b), ), ); const storeHeading = selectedStores.length === 1 ? ` hos ${ - storeNames[selectedStores[0]] ?? - selectedStores[0] + getStoreName(selectedStores[0]) }` : selectedStores.length > 1 ? ` hos ${selectedStores.length} butikker` @@ -547,8 +538,8 @@ export default function SearchResultsPage() { {sortedSelectedStores.map((value) => ( { preservePosition(); handleStoresChange( diff --git a/frontend/src/pages/SearchResults/searchQueries.ts b/frontend/src/pages/SearchResults/searchQueries.ts index e16b259..14c53ea 100644 --- a/frontend/src/pages/SearchResults/searchQueries.ts +++ b/frontend/src/pages/SearchResults/searchQueries.ts @@ -57,6 +57,7 @@ export const SEARCH_PRODUCTS_QUERY: TypedDocumentNode< categoryId prices { storeCode + storeName price unitPrice } diff --git a/frontend/src/pages/productDetails/ProductDetailsPage.tsx b/frontend/src/pages/productDetails/ProductDetailsPage.tsx index 5528189..9eaa420 100644 --- a/frontend/src/pages/productDetails/ProductDetailsPage.tsx +++ b/frontend/src/pages/productDetails/ProductDetailsPage.tsx @@ -8,21 +8,6 @@ type ProductDetailsPageProps = { ean: string; }; -const storeNames: Record = { - KIWI: "Kiwi", - MENY_NO: "Meny", - SPAR_NO: "Spar", - BUNNPRIS: "Bunnpris", - COOP_NO: "Coop", - JOKER_NO: "Joker", - ODA_NO: "Oda", - EUROPRIS_NO: "Europris", - ENGROSSNETT_NO: "Engrosnett", -}; - -const getStoreName = (storeCode: string) => - storeNames[storeCode] ?? storeCode; - export default function ProductDetailsPage({ ean, }: ProductDetailsPageProps) { @@ -166,7 +151,7 @@ export default function ProductDetailsPage({

- BILLIGST HOS {getStoreName(cheapest.storeCode).toUpperCase()} + BILLIGST HOS {cheapest.storeName.toUpperCase()}

) : ( @@ -187,7 +172,7 @@ export default function ProductDetailsPage({ & { unit: string; }[]; }; - -