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
28 changes: 28 additions & 0 deletions backend/db/migrations/002_stores.sql
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 5 additions & 3 deletions backend/src/resolvers/filters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pool } from '../db.js';
import { pool } from "../db.js";

export const categoryFilterCounts = async (
_parent: unknown,
Expand Down Expand Up @@ -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
Expand All @@ -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
`,
Expand Down
19 changes: 12 additions & 7 deletions backend/src/resolvers/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
);
Expand Down
2 changes: 2 additions & 0 deletions backend/src/resolvers/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -37,6 +38,7 @@ export const resolvers = {
productsCount,
product,
categories,
stores,
shoppingList,
myShoppingLists,
categoryFilterCounts,
Expand Down
13 changes: 13 additions & 0 deletions backend/src/resolvers/stores.ts
Original file line number Diff line number Diff line change
@@ -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;
};
8 changes: 7 additions & 1 deletion backend/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,7 +32,6 @@ export const typeDefs = `#graphql
categoryId: ID
category: Category
prices: [Price!]!
ingredients: String
nutrition: [Nutrition!]!
allergens: [Allergen!]!
Expand Down Expand Up @@ -95,6 +100,7 @@ export const typeDefs = `#graphql
product(ean: String!): Product
categories: [Category!]!
stores: [Store!]!
shoppingList(id: ID!): ShoppingList
myShoppingLists(status: ShoppingListStatus): [ShoppingList!]!
Expand Down
44 changes: 44 additions & 0 deletions frontend/src/hooks/useStoreNames.ts
Original file line number Diff line number Diff line change
@@ -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<StoresData> = 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 };
}
23 changes: 5 additions & 18 deletions frontend/src/pages/SearchResults/SearchEmptyState.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import type { FilterCount } from "../../types/filters";
import { useStoreNames } from "../../hooks/useStoreNames";

type SearchEmptyStateProps = {
searchTerm: string;
Expand All @@ -10,19 +11,6 @@ type SearchEmptyStateProps = {
onResetFilters: () => void;
};

const storeNames: Record<string, string> = {
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,
Expand All @@ -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
Expand Down Expand Up @@ -108,8 +96,7 @@ export default function SearchEmptyState({
}
>
<span>
{storeNames[store.value] ??
store.label}
{store.label}
</span>

<span className="search-empty-state-store-count">
Expand Down
18 changes: 3 additions & 15 deletions frontend/src/pages/SearchResults/SearchFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@ type SearchFiltersProps = {
onStoresChange: (values: string[]) => void;
};

const storeNames: Record<string, string> = {
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",
});
Expand All @@ -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,
),
);

Expand Down Expand Up @@ -94,7 +82,7 @@ export default function SearchFilters({
{sortedStores.map((store) => (
<FilterRow
key={store.value}
label={storeNames[store.value] ?? store.label}
label={store.label}
count={store.count}
checked={selectedStores.includes(store.value)}
onChange={(checked) =>
Expand Down
15 changes: 1 addition & 14 deletions frontend/src/pages/SearchResults/SearchProductList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ const formatter = new Intl.NumberFormat("nb-NO", {
maximumFractionDigits: 2,
});

const storeNames: Record<string, string> = {
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[],
Expand Down Expand Up @@ -90,8 +78,7 @@ export default function SearchProductList({
}
chain={
displayPrice
? (storeNames[displayPrice.storeCode] ??
displayPrice.storeCode)
? displayPrice.storeName
: undefined
}
onAdd={onAddProduct ? () => onAddProduct(product) : undefined
Expand Down
Loading