From 91880d86223bc5e23573322befc9282334c584fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kalle=20L=C3=A4ms=C3=A4?= <1397804+coocos@users.noreply.github.com> Date: Sun, 11 Apr 2021 19:22:18 +0300 Subject: [PATCH] Support querying events by location name --- package.json | 2 +- src/controllers/events.ts | 2 +- src/routes/events.test.ts | 2 +- src/services/events.integration.test.ts | 25 +++++++++++++++++++++++-- src/services/events.ts | 23 ++++++++++++++++------- 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 8db715e..187f6d4 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "start": "ts-node-dev --respawn --transpile-only --require dotenv/config src/index.ts", "test": "NODE_ENV=test jest --silent --testPathIgnorePatterns integration", - "integration": "NODE_ENV=test jest --silent --testPathPattern integration", + "integration": "NODE_ENV=test jest --silent --runInBand --testPathPattern integration", "migrate": "env $(cat .env) knex migrate:latest", "rollback": "env $(cat .env) knex migrate:rollback" }, diff --git a/src/controllers/events.ts b/src/controllers/events.ts index 9667bdb..dfa6bcf 100644 --- a/src/controllers/events.ts +++ b/src/controllers/events.ts @@ -4,7 +4,7 @@ import { events } from "../services/events"; export async function listEvents(req: Request, res: Response): Promise { try { - const allEvents = await events.findAll(); + const allEvents = await events.find(); res.json( allEvents.map(({ type, location, time }) => ({ type, diff --git a/src/routes/events.test.ts b/src/routes/events.test.ts index 37a7dc1..b4f23a0 100644 --- a/src/routes/events.test.ts +++ b/src/routes/events.test.ts @@ -7,7 +7,7 @@ jest.mock("../services/events"); describe("/events", () => { it("returns list of events", async () => { const mockEvents = events as jest.Mocked; - mockEvents.findAll.mockResolvedValue([ + mockEvents.find.mockResolvedValue([ { type: "rakennuspalo: keskisuuri", location: "Tuusula", diff --git a/src/services/events.integration.test.ts b/src/services/events.integration.test.ts index 50b9c3d..d047232 100644 --- a/src/services/events.integration.test.ts +++ b/src/services/events.integration.test.ts @@ -14,14 +14,14 @@ describe("Service", () => { await db.destroy(); }); - it("adds events", async () => { + it("adds event", async () => { await events.add({ location: "Kemi", type: "tulipalo muu: pieni", time: new Date("2021-01-31T22:00:00.000Z"), hash: "dffab7da0d65580c2c09ee683dd8f18632fa27c1", }); - const allEvents = await events.findAll(); + const allEvents = await events.find(); expect(allEvents).toEqual([ { location: "Kemi", @@ -31,6 +31,27 @@ describe("Service", () => { }, ]); }); + it("filters events based on location", async () => { + const newEvents = [ + { + type: "rakennuspalo: pieni", + location: "Helsinki", + time: new Date("2021-03-01T15:40:52.000Z"), + hash: "169a28a0c1979d7c105490bde4e30ce5b64418a5", + }, + { + location: "Kemi", + type: "tulipalo muu: pieni", + time: new Date("2021-01-31T22:00:00.000Z"), + hash: "dffab7da0d65580c2c09ee683dd8f18632fa27c1", + }, + ]; + for (const event of newEvents) { + await events.add(event); + } + expect(await events.find({ location: "Helsinki" })).toEqual([newEvents[0]]); + expect(await events.find({ location: "Kemi" })).toEqual([newEvents[1]]); + }); it("indicates if event exists", async () => { await events.add({ diff --git a/src/services/events.ts b/src/services/events.ts index 09a7ee4..5805e5b 100644 --- a/src/services/events.ts +++ b/src/services/events.ts @@ -24,18 +24,27 @@ type Event = { hash: string; }; +type QueryFilters = { + location?: string; +}; + export const events = { - async findAll(): Promise { - const events = await db("events") - .join("types", "types.id", "events.type_id") - .join("locations", "locations.id", "events.location_id") - .select( + async find(filters: QueryFilters = {}): Promise { + let events = db("events") + .select( "events.time", "events.hash", "locations.name as location", "types.name as type" - ); - return events; + ) + .join("types", "types.id", "events.type_id") + .join("locations", "locations.id", "events.location_id"); + if (filters.location !== undefined) { + events = events.where({ + "locations.name": filters.location, + }); + } + return (await events) as RescueEvent[]; }, async exists(event: RescueEvent): Promise { const knownEvent = await db("events")