From 162abb4720d0970612239d41f92ff8fafa9545cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henry=20Gr=C3=A6sberg?= Date: Tue, 27 Oct 2026 20:11:17 +0100 Subject: [PATCH 1/4] Add venue coordinates to schema and seed data --- .../migration.sql | 10 ++++++++++ packages/db/prisma/schema.prisma | 2 ++ packages/db/prisma/seed.ts | 12 ++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 packages/db/prisma/migrations/20261027182459_add_venue_coordinates/migration.sql diff --git a/packages/db/prisma/migrations/20261027182459_add_venue_coordinates/migration.sql b/packages/db/prisma/migrations/20261027182459_add_venue_coordinates/migration.sql new file mode 100644 index 0000000..32321a6 --- /dev/null +++ b/packages/db/prisma/migrations/20261027182459_add_venue_coordinates/migration.sql @@ -0,0 +1,10 @@ +/* + Warnings: + + - Added the required column `lat` to the `venue` table without a default value. This is not possible if the table is not empty. + - Added the required column `lon` to the `venue` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "venue" ADD COLUMN "lat" DOUBLE PRECISION NOT NULL, +ADD COLUMN "lon" DOUBLE PRECISION NOT NULL; diff --git a/packages/db/prisma/schema.prisma b/packages/db/prisma/schema.prisma index f9b09c3..f23583e 100644 --- a/packages/db/prisma/schema.prisma +++ b/packages/db/prisma/schema.prisma @@ -46,6 +46,8 @@ model Venue { id String @id @default(dbgenerated("uuidv7()")) @db.Uuid name String address String + lat Float + lon Float openingHours OpeningHours[] diff --git a/packages/db/prisma/seed.ts b/packages/db/prisma/seed.ts index 1c8d2fe..b39d743 100644 --- a/packages/db/prisma/seed.ts +++ b/packages/db/prisma/seed.ts @@ -25,6 +25,8 @@ async function main() { { name: 'Den Gode Nabo', address: 'Øvre Bakklandet 69', + lat: 63.4295, + lon: 10.3966, openingHours: { create: [ { @@ -67,6 +69,8 @@ async function main() { { name: 'Skjenkestua', address: 'Nedre Bakklandet 24', + lat: 63.4292, + lon: 10.3959, openingHours: { create: [ { @@ -101,6 +105,8 @@ async function main() { { name: 'Baklandet Skydsstation', address: 'Øvre Bakklandet 33', + lat: 63.4291, + lon: 10.3962, openingHours: { create: [ { @@ -130,6 +136,8 @@ async function main() { { name: 'Trondheim Torg Pub', address: 'Olav Tryggvasons gate 6', + lat: 63.4302, + lon: 10.3948, openingHours: { create: [ { @@ -179,6 +187,8 @@ async function main() { { name: 'The Dubliner Folk Pub', address: 'Rådhusgata 13', + lat: 59.9114, + lon: 10.7405, openingHours: { create: [ { @@ -210,6 +220,8 @@ async function main() { { name: 'Tilt', address: 'Øvre Slottsgate 6', + lat: 59.9139, + lon: 10.7432, openingHours: { create: [ { From 2322b0f5cb31f56292f1b7b1c788e3736c44772c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henry=20Gr=C3=A6sberg?= Date: Tue, 27 Oct 2026 20:11:17 +0100 Subject: [PATCH 2/4] Add haversine_distance SQL function --- .../migration.sql | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 packages/db/prisma/migrations/20261027195801_add_haversine_distance_function/migration.sql diff --git a/packages/db/prisma/migrations/20261027195801_add_haversine_distance_function/migration.sql b/packages/db/prisma/migrations/20261027195801_add_haversine_distance_function/migration.sql new file mode 100644 index 0000000..a22fa78 --- /dev/null +++ b/packages/db/prisma/migrations/20261027195801_add_haversine_distance_function/migration.sql @@ -0,0 +1,18 @@ +-- CreateFunction +CREATE FUNCTION haversine_distance( + lat1 double precision, + lon1 double precision, + lat2 double precision, + lon2 double precision +) RETURNS double precision +LANGUAGE sql +IMMUTABLE +PARALLEL SAFE +AS $$ + SELECT 2 * 6371000 * asin( + sqrt( + power(sin(radians(lat2 - lat1) / 2), 2) + + cos(radians(lat1)) * cos(radians(lat2)) * power(sin(radians(lon2 - lon1) / 2), 2) + ) + ); +$$; From 7e01e514a000e9516dd81c73b8a0a709fd082bd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henry=20Gr=C3=A6sberg?= Date: Tue, 27 Oct 2026 20:11:17 +0100 Subject: [PATCH 3/4] Add search, city and proximity-sorted venue queries --- apps/backend/src/schema/price-sightings.ts | 16 +++ apps/backend/src/schema/schema.ts | 155 +++++++++++++++++++-- packages/db/src/index.ts | 1 + 3 files changed, 162 insertions(+), 10 deletions(-) diff --git a/apps/backend/src/schema/price-sightings.ts b/apps/backend/src/schema/price-sightings.ts index c546bd5..066fd36 100644 --- a/apps/backend/src/schema/price-sightings.ts +++ b/apps/backend/src/schema/price-sightings.ts @@ -48,6 +48,22 @@ export function latestPriceSightings( return sightings.filter((sighting) => latest.has(sighting)) } +export function latestUnlimitedSighting( + sightings: T[], +): T | null { + return sightings.reduce((latest, sighting) => { + if (sighting.limitation.length > 0) { + return latest + } + + if (latest === null || sighting.timeSighted > latest.timeSighted) { + return sighting + } + + return latest + }, null) +} + function sightingKey(sighting: PriceSightingLike): string { const category = [...sighting.limitation].sort().join('+') diff --git a/apps/backend/src/schema/schema.ts b/apps/backend/src/schema/schema.ts index cebb2fb..c8c63fe 100644 --- a/apps/backend/src/schema/schema.ts +++ b/apps/backend/src/schema/schema.ts @@ -1,7 +1,11 @@ import { prisma } from '@t09-project-2/db' +import type { OpeningHours, PriceSighting } from '@t09-project-2/db' import { builder } from '../builder.js' -import { latestPriceSightings } from './price-sightings.js' +import { + latestPriceSightings, + latestUnlimitedSighting, +} from './price-sightings.js' import type { VenueWithPriceSightings } from './price-sightings.js' builder.prismaObject('Venue', { @@ -9,13 +13,15 @@ builder.prismaObject('Venue', { id: t.exposeString('id'), name: t.exposeString('name'), address: t.exposeString('address'), + lat: t.exposeFloat('lat'), + lon: t.exposeFloat('lon'), openingHours: t.relation('openingHours'), city: t.relation('city'), pricesSighted: t.relation('pricesSighted'), }), }) -builder.prismaObject('OpeningHours', { +const openingHoursRef = builder.prismaObject('OpeningHours', { fields: (t) => ({ days: t.exposeStringList('days'), opening: t.expose('opening', { type: 'DateTime' }), @@ -32,7 +38,7 @@ builder.prismaObject('City', { }), }) -builder.prismaObject('PriceSighting', { +const priceSightingRef = builder.prismaObject('PriceSighting', { select: { size: true, sizeMl: true, @@ -53,20 +59,149 @@ builder.prismaObject('PriceSighting', { }), }) +type SearchResultItem = { + type: 'venue' | 'city' + name: string + lat: number + lon: number + address: string | null + openingHours: OpeningHours[] | null + latestPriceSighting: PriceSighting | null +} + +const searchResultRef = builder + .objectRef('SearchResult') + .implement({ + fields: (t) => ({ + type: t.exposeString('type'), + name: t.exposeString('name'), + lat: t.exposeFloat('lat'), + lon: t.exposeFloat('lon'), + address: t.exposeString('address', { nullable: true }), + openingHours: t.expose('openingHours', { + type: [openingHoursRef], + nullable: { list: true, items: false }, + }), + latestPriceSighting: t.expose('latestPriceSighting', { + type: priceSightingRef, + nullable: true, + }), + }), + }) + +function applyLatestPriceSightings(venues: VenueWithPriceSightings[]) { + for (const venue of venues) { + if (venue.pricesSighted) { + venue.pricesSighted = latestPriceSightings(venue.pricesSighted) + } + } +} + builder.queryType({ fields: (t) => ({ venue: t.prismaField({ type: ['Venue'], - resolve: async (query) => { - const venues = await prisma.venue.findMany({ ...query }) + args: { + lat: t.arg.float(), + lon: t.arg.float(), + }, + resolve: async (query, _parent, args) => { + if (args.lat == null || args.lon == null) { + const venues = await prisma.venue.findMany({ ...query }) - for (const venue of venues as VenueWithPriceSightings[]) { - if (venue.pricesSighted) { - venue.pricesSighted = latestPriceSightings(venue.pricesSighted) - } + applyLatestPriceSightings(venues as VenueWithPriceSightings[]) + + return venues } - return venues + const nearest = await prisma.$queryRaw<{ id: string }[]>` + SELECT id FROM venue + ORDER BY haversine_distance(${args.lat}, ${args.lon}, lat, lon), name + ` + + const venues = await prisma.venue.findMany({ + ...query, + where: { id: { in: nearest.map((venue) => venue.id) } }, + }) + + const byId = new Map(venues.map((venue) => [venue.id, venue] as const)) + + const sorted = nearest.flatMap((row) => { + const venue = byId.get(row.id) + return venue ? [venue] : [] + }) + + // no-unnecessary-type-assertion eslint rule fires, but without type assertion the ts compiler complains + // eslint-disable-next-line + applyLatestPriceSightings(sorted as VenueWithPriceSightings[]) + + return sorted + }, + }), + city: t.prismaField({ + type: ['City'], + args: { + name: t.arg.string({ required: true }), + }, + resolve: (query, _parent, args) => + prisma.city.findMany({ + ...query, + where: { + name: { + contains: args.name, + mode: 'insensitive', + }, + }, + }), + }), + search: t.field({ + type: [searchResultRef], + args: { + term: t.arg.string({ required: true }), + }, + resolve: async (_parent, args) => { + const contains = { + contains: args.term, + mode: 'insensitive' as const, + } + + const [venues, cities] = await Promise.all([ + prisma.venue.findMany({ + where: { + OR: [{ name: contains }, { address: contains }], + }, + include: { + openingHours: true, + pricesSighted: true, + }, + orderBy: { name: 'asc' }, + }), + prisma.city.findMany({ + where: { name: contains }, + orderBy: { name: 'asc' }, + }), + ]) + + return [ + ...venues.map((venue) => ({ + type: 'venue' as const, + name: venue.name, + lat: venue.lat, + lon: venue.lon, + address: venue.address, + openingHours: venue.openingHours, + latestPriceSighting: latestUnlimitedSighting(venue.pricesSighted), + })), + ...cities.map((city) => ({ + type: 'city' as const, + name: city.name, + lat: city.centreLat, + lon: city.centreLon, + address: null, + openingHours: null, + latestPriceSighting: null, + })), + ] }, }), }), diff --git a/packages/db/src/index.ts b/packages/db/src/index.ts index a5d1d91..50c3291 100644 --- a/packages/db/src/index.ts +++ b/packages/db/src/index.ts @@ -1,5 +1,6 @@ export { getDatabaseUrl } from './database-url.js' export { prisma } from './client.js' export { PrismaClient } from './generated/prisma/client.js' +export type { OpeningHours, PriceSighting } from './generated/prisma/client.js' export type { default as PrismaTypes } from './generated/pothos-types.js' export { getDatamodel } from './generated/pothos-types.js' From c261aa4da37da5bfe90b99958239f9c663530aec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henry=20Gr=C3=A6sberg?= Date: Tue, 27 Oct 2026 20:13:52 +0100 Subject: [PATCH 4/4] lint --- apps/backend/src/schema/schema.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/apps/backend/src/schema/schema.ts b/apps/backend/src/schema/schema.ts index c8c63fe..b008812 100644 --- a/apps/backend/src/schema/schema.ts +++ b/apps/backend/src/schema/schema.ts @@ -131,8 +131,6 @@ builder.queryType({ return venue ? [venue] : [] }) - // no-unnecessary-type-assertion eslint rule fires, but without type assertion the ts compiler complains - // eslint-disable-next-line applyLatestPriceSightings(sorted as VenueWithPriceSightings[]) return sorted