Skip to content

Commit

Permalink
Update errorhandling
Browse files Browse the repository at this point in the history
  • Loading branch information
KomelT committed Aug 28, 2024
1 parent 632aef2 commit 3213e9f
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 286 deletions.
169 changes: 85 additions & 84 deletions api/src/routes/text-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,95 +3,96 @@ import { prisma } from "../db.js";
import express from "../express.js";

express.get("/api/v1/text-messages", async (req, res) => {
try {
const directMessageIds = req.query.direct_message_node_ids as string;
const lastI = req.query.last_id as string;
const coun = req.query.count as string;
try {
const directMessageIds = req.query.direct_message_node_ids as string;
const lastI = req.query.last_id as string;
const coun = req.query.count as string;

// get query params
const to = req.query.to ?? undefined;
const from = req.query.from ?? undefined;
const channelId = (req.query.channel_id as string) ?? undefined;
const gatewayId =
Number.parseInt(req.query.gateway_id as string) ?? undefined;
const directMessageNodeIds = directMessageIds.split(",") ?? undefined;
const lastId = lastI ? Number.parseInt(lastI) : undefined;
const count = req.query.count ? Number.parseInt(coun) : 50;
const order = req.query.order ?? "asc";
// get query params
const to = req.query.to ?? undefined;
const from = req.query.from ?? undefined;
const channelId = (req.query.channel_id as string) ?? undefined;
const gatewayId =
Number.parseInt(req.query.gateway_id as string) ?? undefined;
const directMessageNodeIds = directMessageIds.split(",") ?? undefined;
const lastId = lastI ? Number.parseInt(lastI) : undefined;
const count = req.query.count ? Number.parseInt(coun) : 50;
const order = req.query.order ?? "asc";

// if direct message node ids are provided, there should be exactly two node ids
if (
directMessageNodeIds !== undefined &&
directMessageNodeIds.length !== 2
) {
res.status(400).json({
message:
"direct_message_node_ids requires 2 node ids separated by a comma.",
});
return;
}
// if direct message node ids are provided, there should be exactly two node ids
if (
directMessageNodeIds !== undefined &&
directMessageNodeIds.length !== 2
) {
res.status(400).json({
message:
"direct_message_node_ids requires 2 node ids separated by a comma.",
});
return;
}

// default where clauses that should always be used for filtering
let where: Prisma.TextMessageWhereInput = {
channel_id: channelId,
gateway_id: gatewayId,
// when ordered oldest to newest (asc), only get records after last id
// when ordered newest to oldest (desc), only get records before last id
id:
order === "asc"
? {
gt: lastId,
}
: {
lt: lastId,
},
};
// default where clauses that should always be used for filtering
let where: Prisma.TextMessageWhereInput = {
channel_id: channelId,
gateway_id: gatewayId,
// when ordered oldest to newest (asc), only get records after last id
// when ordered newest to oldest (desc), only get records before last id
id:
order === "asc"
? {
gt: lastId,
}
: {
lt: lastId,
},
};

// if direct message node ids are provided, we expect exactly 2 node ids
if (
directMessageNodeIds !== undefined &&
directMessageNodeIds.length === 2
) {
// filter message by "to -> from" or "from -> to"
const [firstNodeId, secondNodeId] = directMessageNodeIds;
where = {
AND: where,
OR: [
{
to: Number.parseInt(firstNodeId),
from: Number.parseInt(secondNodeId),
},
{
to: Number.parseInt(secondNodeId),
from: Number.parseInt(firstNodeId),
},
],
};
} else {
// filter by to and from
where = {
...where,
to: Number.parseInt(to as string),
from: Number.parseInt(from as string),
};
}
// if direct message node ids are provided, we expect exactly 2 node ids
if (
directMessageNodeIds !== undefined &&
directMessageNodeIds.length === 2
) {
// filter message by "to -> from" or "from -> to"
const [firstNodeId, secondNodeId] = directMessageNodeIds;
where = {
AND: where,
OR: [
{
to: Number.parseInt(firstNodeId),
from: Number.parseInt(secondNodeId),
},
{
to: Number.parseInt(secondNodeId),
from: Number.parseInt(firstNodeId),
},
],
};
} else {
// filter by to and from
where = {
...where,
to: Number.parseInt(to as string),
from: Number.parseInt(from as string),
};
}

// get text messages from db
const textMessages = await prisma.textMessage.findMany({
where: where,
orderBy: {
id: order as Prisma.SortOrder,
},
take: count,
});
// get text messages from db
const textMessages = await prisma.textMessage.findMany({
where: where,
orderBy: {
id: order as Prisma.SortOrder,
},
take: count,
});

res.json({
text_messages: textMessages,
});
} catch (err) {
res.status(500).json({
message: "Something went wrong, try again later.",
});
}
res.json({
text_messages: textMessages,
});
} catch (err) {
console.error(err);
res.status(500).json({
message: "Something went wrong, try again later.",
});
}
});
console.log("API:EXPRESS registered route GET:/api/v1/text-messages");
73 changes: 37 additions & 36 deletions api/src/routes/waypoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,46 @@ import { prisma } from "../db.js";
import express from "../express.js";

express.get("/api/v1/waypoints", async (req, res) => {
try {
// get waypoints from db
const waypoints = await prisma.waypoint.findMany({
orderBy: {
id: "desc",
},
});
try {
// get waypoints from db
const waypoints = await prisma.waypoint.findMany({
orderBy: {
id: "desc",
},
});

// ensure we only have the latest unique waypoints
// since ordered by newest first, older entries will be ignored
const uniqueWaypoints: typeof waypoints = [];
for (const waypoint of waypoints) {
// skip if we already have a newer entry for this waypoint
if (
uniqueWaypoints.find(
(w) =>
w.from === waypoint.from && w.waypoint_id === waypoint.waypoint_id
)
) {
continue;
}
// ensure we only have the latest unique waypoints
// since ordered by newest first, older entries will be ignored
const uniqueWaypoints: typeof waypoints = [];
for (const waypoint of waypoints) {
// skip if we already have a newer entry for this waypoint
if (
uniqueWaypoints.find(
(w) =>
w.from === waypoint.from && w.waypoint_id === waypoint.waypoint_id
)
) {
continue;
}

// first time seeing this waypoint, add to unique list
uniqueWaypoints.push(waypoint);
}
// first time seeing this waypoint, add to unique list
uniqueWaypoints.push(waypoint);
}

// we only want waypoints that haven't expired yet
const nonExpiredWayPoints = uniqueWaypoints.filter((waypoint) => {
const nowInSeconds = Math.floor(Date.now() / 1000);
if (waypoint.expire) return waypoint.expire >= nowInSeconds;
});
// we only want waypoints that haven't expired yet
const nonExpiredWayPoints = uniqueWaypoints.filter((waypoint) => {
const nowInSeconds = Math.floor(Date.now() / 1000);
if (waypoint.expire) return waypoint.expire >= nowInSeconds;
});

res.json({
waypoints: nonExpiredWayPoints,
});
} catch (err) {
res.status(500).json({
message: "Something went wrong, try again later.",
});
}
res.json({
waypoints: nonExpiredWayPoints,
});
} catch (err) {
console.error(err);
res.status(500).json({
message: "Something went wrong, try again later.",
});
}
});
console.log("API:EXPRESS registered route GET:/api/v1/waypoints");
4 changes: 3 additions & 1 deletion app/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2771,7 +2771,9 @@ <h2 class="font-bold">
if (value) {
return JSON.parse(value);
}
} catch (e) {}
} catch (e) {
console.error(e);
}

// overlays enabled by default
return ["Legend", "Position History"];
Expand Down
1 change: 1 addition & 0 deletions mqtt/src/messages/service_envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export async function handleServiceEnvelope(
},
});
} catch (e) {
console.error(e);
// don't care if updating mqtt timestamp fails
}

Expand Down
Loading

0 comments on commit 3213e9f

Please sign in to comment.