Skip to content

Commit

Permalink
Merge pull request #53 from kiwijos/query-stations
Browse files Browse the repository at this point in the history
Query stations
  • Loading branch information
Jolpango authored Oct 23, 2023
2 parents be816b9 + ef56f2f commit 0057dab
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ app.use("/tickets", jwtAuth.checkToken, tickets);
app.use("/codes", jwtAuth.checkToken, codes);

// Make handler a graphql handler
app.use("/auth", graphqlHTTP({ schema: authSchema, rootValue: authResolver, graphiql: true }));
app.use("/auth", graphqlHTTP({ schema: authSchema, rootValue: authResolver, graphiql: false }));

app.use(
"/graphql",
graphqlHTTP({
schema,
rootValue: resolver,
graphiql: true
graphiql: false
})
);

Expand Down
3 changes: 3 additions & 0 deletions src/config/schema/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const query = {
},
ticketCodes: async () => {
return await trainRepository.getTrainCodes();
},
trainStations: async () => {
return await trainRepository.getTrainStations();
}
};

Expand Down
15 changes: 12 additions & 3 deletions src/config/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const schema = buildSchema(`
ticket(id: ID!): Ticket
trainDelays: TrainDelayResponse
ticketCodes: TicketCodeResponse
trainStations: TrainStationResponse
}
type Mutation {
createTicket(code: String!, trainnumber: String!, traindate: String!): TicketResponse
Expand All @@ -22,6 +23,11 @@ const schema = buildSchema(`
error: String
data: [TicketCode]
}
type TrainStationResponse {
ok: Boolean
error: String
data: [TrainStation]
}
type TicketCode {
Code: String
Level1Description: String
Expand All @@ -41,6 +47,12 @@ const schema = buildSchema(`
ToLocation: [TrainLocation]
TrainOwner: String
}
type TrainStation {
AdvertisedLocationName: String!
LocationSignature: String!
Longitude: String!
Latitude: String!
}
type TrainLocation {
LocationName: String!
Order: Int!
Expand All @@ -52,9 +64,6 @@ const schema = buildSchema(`
trainnumber: String!
traindate: String!
}
type Tickets {
tickets: [Ticket]
}
type TicketResponse {
data: Ticket
error: String
Expand Down
91 changes: 76 additions & 15 deletions src/db/repository/trainRepository.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,73 @@
import fetch from "node-fetch";

const API_URL = "https://api.trafikinfo.trafikverket.se/v2/data.json";

// Cache announcements to prevent over-fetching
const cache = new Map();
const delayedCache = new Map();
let changeid = "0";

const stationCache = new Map();

const codeCache = new Map();

const trainRepository = {
getTrainStations: async () => {
if (stationCache.has("stations")) {
return {
ok: true,
data: stationCache.get("stations"),
error: null
};
}

const query = `
<REQUEST>
<LOGIN authenticationkey="d8625fb909f941dd855b6611ee172759" />
<QUERY objecttype="TrainStation" schemaversion="1.4">
<INCLUDE>AdvertisedLocationName</INCLUDE>
<INCLUDE>Geometry</INCLUDE>
<INCLUDE>LocationSignature</INCLUDE>
</QUERY>
</REQUEST>`;

try {
const response = await fetch(API_URL, {
method: "POST",
body: query,
headers: { "Content-Type": "text/xml" }
});
const result = await response.json();

// Replace the Geometry object with longitude and latitude
const stations = result.RESPONSE.RESULT[0].TrainStation.map((station) => {
const [longitude, latitude] = station.Geometry.WGS84.split("POINT ")[1]
.split("(")[1]
.split(")")[0]
.split(" ");

return {
AdvertisedLocationName: station.AdvertisedLocationName,
Longitude: longitude,
Latitude: latitude,
LocationSignature: station.LocationSignature
};
});

stationCache.set("stations", stations);

return {
ok: true,
data: stations,
error: null
};
} catch (err) {
return {
ok: false,
data: null,
error: err.message
};
}
},
getTrainDelays: async () => {
const query = `
<REQUEST>
Expand Down Expand Up @@ -45,37 +105,40 @@ const trainRepository = {
body: query,
headers: { "Content-Type": "text/xml" }
});

const result = await response.json();

// Update id to only fetch newer entries
changeid = result.RESPONSE.RESULT[0].INFO.LASTCHANGEID;
if (cache.size === 0) {
// Initially populate cache with all announcements

if (delayedCache.size === 0) {
// Initially populate delayedCache with all announcements
result.RESPONSE.RESULT[0].TrainAnnouncement.forEach((delay) => {
cache.set(delay.ActivityId, delay);
delayedCache.set(delay.ActivityId, delay);
});
} else if (result.RESPONSE.RESULT[0].TrainAnnouncement.length > 0) {
// Add only new announcements and remove those that have been deleted (if any)
result.RESPONSE.RESULT[0].TrainAnnouncement.forEach((delay) => {
if (cache.has(delay.ActivityId)) {
if (delayedCache.has(delay.ActivityId)) {
if (delay?.Deleted === true) {
cache.delete(delay.ActivityId);
delayedCache.delete(delay.ActivityId);
}
} else {
cache.set(delay.ActivityId, delay);
delayedCache.set(delay.ActivityId, delay);
}
});
}
const data = Array.from(cache.values());

return {
ok: true,
data: data,
data: Array.from(delayedCache.values()),
error: null
};
} catch (err) {
return {
ok: false,
data: null,
error: err
error: err.message
};
}
},
Expand Down Expand Up @@ -119,11 +182,9 @@ const trainRepository = {
console.error(`Error fetching codes: ${err}`);

return {
errors: {
ok: false,
data: null,
error: err.Message
}
ok: false,
data: null,
error: err.Message
};
}
}
Expand Down

0 comments on commit 0057dab

Please sign in to comment.