mirror of
https://github.com/Xevion/bus-reminder.git
synced 2025-12-14 06:11:10 -06:00
Add winston, winston-loki
This commit is contained in:
@@ -1,19 +1,28 @@
|
||||
import { getMatchingTime } from '@/timing';
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { getDistance } from '@/location';
|
||||
import { env } from '@/env/server.mjs';
|
||||
import monitorAsync from '@/monitor';
|
||||
import { sendNotification } from '@/notify';
|
||||
import { checkIdentifier, fetchConfiguration, markIdentifier } from '@/db';
|
||||
import {
|
||||
checkIdentifier,
|
||||
fetchConfiguration,
|
||||
getKey,
|
||||
markIdentifier
|
||||
} from '@/db';
|
||||
import { localNow } from '@/utils/timezone';
|
||||
import logger from '@/logger';
|
||||
import { unauthorized } from '@/utils/helpers';
|
||||
|
||||
type ResponseData = {
|
||||
diff: number;
|
||||
inRange: boolean;
|
||||
};
|
||||
|
||||
type StatusData = { status: ResponseStatus; key?: string };
|
||||
type StatusData = {
|
||||
status: ResponseStatus;
|
||||
identifier?: { name: string; key?: string };
|
||||
};
|
||||
|
||||
type ResponseStatus =
|
||||
| 'unauthorized'
|
||||
@@ -32,15 +41,11 @@ export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<(ResponseData & StatusData) | StatusData>
|
||||
) {
|
||||
if (req.query.key != env.API_KEY) {
|
||||
// auth failed
|
||||
res.status(401).json({ status: 'unauthorized' });
|
||||
return;
|
||||
}
|
||||
if (unauthorized(req, res)) return;
|
||||
|
||||
async function innerFunction(): Promise<{
|
||||
status: ResponseStatus;
|
||||
key?: string;
|
||||
identifier?: { name: string; key?: string };
|
||||
}> {
|
||||
const now = localNow();
|
||||
|
||||
@@ -50,32 +55,53 @@ export default async function handler(
|
||||
// No matching time - no notification to send.
|
||||
if (matching == null) return { status: 'no-matching-time' };
|
||||
|
||||
// Get the key for this notification (name + time)
|
||||
const key = getKey(matching.name, now);
|
||||
|
||||
// Check if I am in range of the center
|
||||
const distanceToCenter = await getDistance();
|
||||
if (distanceToCenter > 280) return { status: 'out-of-range' };
|
||||
// TODO: Properly draw from environment MAX_DISTANCE
|
||||
if (distanceToCenter > 280)
|
||||
return {
|
||||
status: 'out-of-range',
|
||||
identifier: { name: matching.name, key }
|
||||
};
|
||||
|
||||
// Check if I have already been notified
|
||||
const { marked, key } = await checkIdentifier(matching.name, now);
|
||||
if (marked) return { status: 'already-notified', key };
|
||||
const marked = await checkIdentifier(key);
|
||||
if (marked)
|
||||
return {
|
||||
status: 'already-notified',
|
||||
identifier: {
|
||||
name: matching.name,
|
||||
key
|
||||
}
|
||||
};
|
||||
|
||||
// Send notification, mark
|
||||
await sendNotification(`${matching.message} (${matching.name})`);
|
||||
await markIdentifier(matching.name, true, 60 * 60 * 24 * 31, now);
|
||||
|
||||
return { status: 'notified', key };
|
||||
return { status: 'notified', identifier: { name: matching.name, key } };
|
||||
}
|
||||
|
||||
try {
|
||||
let result;
|
||||
if (
|
||||
process.env.NODE_ENV === 'production' &&
|
||||
// TODO: Proper boolean parsing
|
||||
(req.query.report ?? 'true') === 'true'
|
||||
)
|
||||
result = await monitorAsync(innerFunction);
|
||||
else result = await innerFunction();
|
||||
res.status(200).json({ status: result.status, key: result.key });
|
||||
|
||||
logger.info('Cron evaluated.', { result });
|
||||
|
||||
res
|
||||
.status(200)
|
||||
.json({ status: result.status, identifier: result.identifier });
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
logger.error(e);
|
||||
res.status(500).json({ status: 'error' });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user