From e5a286f92e87612c13f856196b77e5581b1c9d04 Mon Sep 17 00:00:00 2001
From: Ingrihka
Date: Mon, 26 Oct 2026 12:24:30 +0100
Subject: [PATCH 1/3] feat: butikknavn i databasen og storeName i GraphQL
(#129)
---
backend/db/migrations/002_stores.sql | 28 +++++++++++++++++++
backend/src/resolvers/filters.ts | 8 ++++--
backend/src/resolvers/products.ts | 19 ++++++++-----
backend/src/resolvers/resolvers.ts | 2 ++
backend/src/resolvers/stores.ts | 13 +++++++++
backend/src/schema.ts | 8 +++++-
.../pages/SearchResults/SearchResultsPage.tsx | 5 ++++
.../src/pages/SearchResults/searchQueries.ts | 1 +
.../pages/productDetails/productQueries.ts | 4 +--
frontend/src/types/product.ts | 3 +-
10 files changed, 76 insertions(+), 15 deletions(-)
create mode 100644 backend/db/migrations/002_stores.sql
create mode 100644 backend/src/resolvers/stores.ts
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 0af48a3..8bdd063 100644
--- a/backend/src/resolvers/resolvers.ts
+++ b/backend/src/resolvers/resolvers.ts
@@ -18,6 +18,7 @@ import {
shoppingList,
shoppingListItems,
} 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 = {
@@ -26,6 +27,7 @@ export const resolvers = {
productsCount,
product,
categories,
+ stores,
shoppingList,
categoryFilterCounts,
storeFilterCounts,
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 019956d..74cf0db 100644
--- a/backend/src/schema.ts
+++ b/backend/src/schema.ts
@@ -10,9 +10,15 @@ export const typeDefs = `#graphql
label: String!
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!]!
@@ -80,6 +85,7 @@ export const typeDefs = `#graphql
product(ean: String!): Product
categories: [Category!]!
+ stores: [Store!]!
shoppingList(id: ID!): ShoppingList
categoryFilterCounts(
diff --git a/frontend/src/pages/SearchResults/SearchResultsPage.tsx b/frontend/src/pages/SearchResults/SearchResultsPage.tsx
index bf6a522..6a7ee06 100644
--- a/frontend/src/pages/SearchResults/SearchResultsPage.tsx
+++ b/frontend/src/pages/SearchResults/SearchResultsPage.tsx
@@ -110,6 +110,7 @@ function normalizeProducts(
categoryId?: string | null;
prices?: ReadonlyArray<{
storeCode?: string;
+ storeName?: string;
price?: number;
unitPrice?: number | null;
} | null>;
@@ -136,6 +137,10 @@ function normalizeProducts(
return [{
storeCode: price.storeCode,
+ storeName:
+ typeof price.storeName === "string"
+ ? price.storeName
+ : price.storeCode,
price: price.price,
unitPrice:
typeof price.unitPrice === "number"
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/productQueries.ts b/frontend/src/pages/productDetails/productQueries.ts
index c127f3d..4ef331b 100644
--- a/frontend/src/pages/productDetails/productQueries.ts
+++ b/frontend/src/pages/productDetails/productQueries.ts
@@ -1,4 +1,3 @@
-
import { gql } from "@apollo/client";
import type { TypedDocumentNode } from "@apollo/client";
import type { Product } from "../../types/product";
@@ -27,6 +26,7 @@ export const PRODUCT_QUERY: TypedDocumentNode<
}
prices {
storeCode
+ storeName
price
unitPrice
url
@@ -43,4 +43,4 @@ export const PRODUCT_QUERY: TypedDocumentNode<
}
}
}
-`;
\ No newline at end of file
+`;
diff --git a/frontend/src/types/product.ts b/frontend/src/types/product.ts
index 2385f4e..dd71a65 100644
--- a/frontend/src/types/product.ts
+++ b/frontend/src/types/product.ts
@@ -1,6 +1,7 @@
// Pris i én butikk
export type ProductPrice = {
storeCode: string;
+ storeName: string; //navn fra backend
price: number;
unitPrice: number | null;
};
@@ -38,5 +39,3 @@ export type Product = Omit & {
unit: string;
}[];
};
-
-
From ba888715b19f9918c63cf58b22a6e4c95a17aab1 Mon Sep 17 00:00:00 2001
From: Ingrihka
Date: Mon, 26 Oct 2026 12:52:36 +0100
Subject: [PATCH 2/3] refactor: butikknavn fra GraphQL i stedet for hardkodede
lister (#129)
---
frontend/src/hooks/useStoreNames.ts | 44 +++++++++++++++++++
.../pages/SearchResults/SearchEmptyState.tsx | 23 +++-------
.../src/pages/SearchResults/SearchFilters.tsx | 18 ++------
.../pages/SearchResults/SearchProductList.tsx | 15 +------
.../pages/SearchResults/SearchResultsPage.tsx | 30 ++++---------
.../productDetails/ProductDetailsPage.tsx | 19 +-------
6 files changed, 63 insertions(+), 86 deletions(-)
create mode 100644 frontend/src/hooks/useStoreNames.ts
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 6a7ee06..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",
});
@@ -163,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.
@@ -422,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`
@@ -552,8 +538,8 @@ export default function SearchResultsPage() {
{sortedSelectedStores.map((value) => (
{
preservePosition();
handleStoresChange(
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({
Date: Mon, 26 Oct 2026 13:41:10 +0100
Subject: [PATCH 3/3] =?UTF-8?q?style:=20fjerne=20mellomrom=20p=C3=A5=20slu?=
=?UTF-8?q?tten=20av=20linje=20i=20schema.ts=20(#129)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
backend/src/schema.ts | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/backend/src/schema.ts b/backend/src/schema.ts
index d7a5038..4f728af 100644
--- a/backend/src/schema.ts
+++ b/backend/src/schema.ts
@@ -10,7 +10,7 @@ export const typeDefs = `#graphql
label: String!
count: Int!
}
-
+
type Store {
code: String!
name: String!