Skip to content

Commit

Permalink
Restructure services
Browse files Browse the repository at this point in the history
  • Loading branch information
coocos committed Mar 7, 2021
1 parent 6fcb49a commit 3072be0
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 70 deletions.
4 changes: 2 additions & 2 deletions src/controllers/eventController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Request, Response } from "express";
import * as EventRepo from "../repos/eventRepo";
import { eventService } from "../services";

export async function listEvents(req: Request, res: Response): Promise<void> {
const events = await EventRepo.findAll();
const events = await eventService.findAll();
res.json(
events.map(({ type, location, time }) => ({
type,
Expand Down
38 changes: 0 additions & 38 deletions src/feed/index.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/repos/eventRepo.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/routes/events.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import app from "../app";
import * as eventRepo from "../repos/eventRepo";
import request from "supertest";
import app from "../app";
import eventService from "../services/eventService";

describe("/events", () => {
it("returns list of events", async () => {
eventRepo.add({
eventService.add({
type: "rakennuspalo: keskisuuri",
location: "Tuusula",
time: new Date("2021-01-31T22:00:00.000Z"),
Expand Down
9 changes: 4 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import cron from "node-cron";
import config from "./config";
import logger from "./logger";

import * as eventRepo from "./repos/eventRepo";
import * as feed from "./feed";
import { eventService, feedService } from "./services";

cron.schedule("* * * * *", async () => {
logger.info("Checking feed for new events...");
const rawFeed = await feed.decodeFeed();
const events = await feed.mapFeedToEvents(rawFeed);
const rawFeed = await feedService.decodeFeed();
const events = await feedService.mapFeedToEvents(rawFeed);
for (const event of events) {
eventRepo.add(event);
eventService.add(event);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as eventRepo from "./eventRepo";
import eventService from "./eventService";

describe("EventRepo", () => {
it("adds event to repository", async () => {
eventRepo.add({
eventService.add({
type: "rakennuspalo: keskisuuri",
location: "Tuusula",
time: new Date("2021-01-31T22:00:00.000Z"),
hash: "2a39407ee0570aae8f3ba2842e11aa28ce0f5d9f",
});
expect(await eventRepo.findAll()).toEqual([
expect(await eventService.findAll()).toEqual([
{
type: "rakennuspalo: keskisuuri",
location: "Tuusula",
Expand All @@ -19,14 +19,14 @@ describe("EventRepo", () => {
});
it("does not add duplicate event to repository", async () => {
for (let i = 0; i < 2; i++) {
eventRepo.add({
eventService.add({
type: "rakennuspalo: keskisuuri",
location: "Tuusula",
time: new Date("2021-01-31T22:00:00.000Z"),
hash: "2a39407ee0570aae8f3ba2842e11aa28ce0f5d9f",
});
}
expect(await eventRepo.findAll()).toEqual([
expect(await eventService.findAll()).toEqual([
{
type: "rakennuspalo: keskisuuri",
location: "Tuusula",
Expand Down
18 changes: 18 additions & 0 deletions src/services/eventService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { RescueEvent } from "../types";

const events: {
[hash: string]: RescueEvent;
} = {};

const eventService = {
async findAll(): Promise<RescueEvent[]> {
return Object.values(events);
},
async add(event: RescueEvent): Promise<void> {
if (!(event.hash in events)) {
events[event.hash] = { ...event };
}
},
};

export default eventService;
4 changes: 2 additions & 2 deletions src/feed/index.spec.ts → src/services/feedService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mapFeedToEvents } from "./index";
import feedService from "./feedService";

describe("Feed parser", () => {
it("maps feed items to event objects", async () => {
Expand All @@ -24,7 +24,7 @@ describe("Feed parser", () => {
</channel>
</rss>
`;
const events = await mapFeedToEvents(rawFeed);
const events = await feedService.mapFeedToEvents(rawFeed);
expect(events).toEqual([
{
type: "rakennuspalo: pieni",
Expand Down
41 changes: 41 additions & 0 deletions src/services/feedService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import axios from "axios";
import iconv from "iconv-lite";
import hash from "object-hash";
import Parser from "rss-parser";
import { RescueEvent } from "../types";

const feedService = {
async decodeFeed(): Promise<string> {
const response = await axios.get<ArrayBuffer>(
"http://www.peto-media.fi/tiedotteet/rss.xml",
{
responseType: "arraybuffer",
}
);
return iconv.decode(Buffer.from(response.data), "iso-8859-1");
},
async mapFeedToEvents(rawFeed: string): Promise<RescueEvent[]> {
const parser = new Parser({
headers: { Accept: "application/rss+xml, text/xml; q=0.1" },
});
const feed = await parser.parseString(rawFeed);
return feed.items.map((item) => {
if (item.title === undefined) {
throw new Error(`Failed to parse ${item}`);
}
const [location, type] = item.title?.split(",");
const finnishLocation = location.split("/")[0].trim();
const event = {
location: finnishLocation.trim(),
type: type.trim(),
time: new Date(item.isoDate ?? new Date()),
};
return {
...event,
hash: hash(event),
};
});
},
};

export default feedService;
4 changes: 4 additions & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import eventService from "./eventService";
import feedService from "./feedService";

export { eventService, feedService };

0 comments on commit 3072be0

Please sign in to comment.