Skip to content

Commit

Permalink
Support querying events by location name
Browse files Browse the repository at this point in the history
  • Loading branch information
coocos committed Apr 11, 2021
1 parent 3ea1000 commit 91880d8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { events } from "../services/events";

export async function listEvents(req: Request, res: Response): Promise<void> {
try {
const allEvents = await events.findAll();
const allEvents = await events.find();
res.json(
allEvents.map(({ type, location, time }) => ({
type,
Expand Down
2 changes: 1 addition & 1 deletion src/routes/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jest.mock("../services/events");
describe("/events", () => {
it("returns list of events", async () => {
const mockEvents = events as jest.Mocked<typeof events>;
mockEvents.findAll.mockResolvedValue([
mockEvents.find.mockResolvedValue([
{
type: "rakennuspalo: keskisuuri",
location: "Tuusula",
Expand Down
25 changes: 23 additions & 2 deletions src/services/events.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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({
Expand Down
23 changes: 16 additions & 7 deletions src/services/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,27 @@ type Event = {
hash: string;
};

type QueryFilters = {
location?: string;
};

export const events = {
async findAll(): Promise<RescueEvent[]> {
const events = await db("events")
.join("types", "types.id", "events.type_id")
.join("locations", "locations.id", "events.location_id")
.select<RescueEvent[]>(
async find(filters: QueryFilters = {}): Promise<RescueEvent[]> {
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<boolean> {
const knownEvent = await db<Event>("events")
Expand Down

0 comments on commit 91880d8

Please sign in to comment.