mirror of
https://github.com/Xevion/bus-reminder.git
synced 2025-12-06 09:14:32 -06:00
Use localNow, return Redis key identifier inside response to improve debugging capability
This commit is contained in:
18
src/db.ts
18
src/db.ts
@@ -28,12 +28,17 @@ export function getKey(identifier: string, now: Date) {
|
|||||||
return format(now, 'yyyy-MM-dd') + ':' + identifier;
|
return format(now, 'yyyy-MM-dd') + ':' + identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO; Move away from generating key inside these functions while also returning it.
|
||||||
|
|
||||||
export async function checkIdentifier(
|
export async function checkIdentifier(
|
||||||
identifier: string,
|
identifier: string,
|
||||||
now: Date = new Date()
|
now: Date = new Date()
|
||||||
): Promise<boolean> {
|
): Promise<{ marked: boolean; key: string }> {
|
||||||
const key = getKey(identifier, now);
|
const key = getKey(identifier, now);
|
||||||
return (await redis.get(key)) === '1';
|
return {
|
||||||
|
marked: (await redis.get(key)) === '1',
|
||||||
|
key
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function markIdentifier(
|
export async function markIdentifier(
|
||||||
@@ -41,8 +46,11 @@ export async function markIdentifier(
|
|||||||
value: boolean = true,
|
value: boolean = true,
|
||||||
expiry?: number,
|
expiry?: number,
|
||||||
now: Date = new Date()
|
now: Date = new Date()
|
||||||
) {
|
): Promise<{ key: string }> {
|
||||||
const key = getKey(identifier, now);
|
const key = getKey(identifier, now);
|
||||||
if (expiry == undefined) return await redis.set(key, value ? '1' : '0');
|
|
||||||
return await redis.set(key, value ? '1' : '0', 'EX', expiry);
|
if (expiry == undefined) await redis.set(key, value ? '1' : '0');
|
||||||
|
else await redis.set(key, value ? '1' : '0', 'EX', expiry);
|
||||||
|
|
||||||
|
return { key };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,13 +6,14 @@ import { env } from '@/env/server.mjs';
|
|||||||
import monitorAsync from '@/monitor';
|
import monitorAsync from '@/monitor';
|
||||||
import { sendNotification } from '@/notify';
|
import { sendNotification } from '@/notify';
|
||||||
import { fetchConfiguration, checkIdentifier, markIdentifier } from '@/db';
|
import { fetchConfiguration, checkIdentifier, markIdentifier } from '@/db';
|
||||||
|
import { localNow } from '@/utils/timezone';
|
||||||
|
|
||||||
type ResponseData = {
|
type ResponseData = {
|
||||||
diff: number;
|
diff: number;
|
||||||
inRange: boolean;
|
inRange: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type StatusData = { status: ResponseStatus };
|
type StatusData = { status: ResponseStatus; key?: string };
|
||||||
|
|
||||||
type ResponseStatus =
|
type ResponseStatus =
|
||||||
| 'unauthorized'
|
| 'unauthorized'
|
||||||
@@ -37,27 +38,31 @@ export default async function handler(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function innerFunction(): Promise<ResponseStatus> {
|
async function innerFunction(): Promise<{
|
||||||
const now = new Date();
|
status: ResponseStatus;
|
||||||
|
key?: string;
|
||||||
|
}> {
|
||||||
|
const now = localNow();
|
||||||
|
|
||||||
const config = await fetchConfiguration();
|
const config = await fetchConfiguration();
|
||||||
const matching = await getMatchingTime(config, now);
|
const matching = await getMatchingTime(config, now);
|
||||||
|
|
||||||
// No matching time - no notification to send.
|
// No matching time - no notification to send.
|
||||||
if (matching == null) return 'no-matching-time';
|
if (matching == null) return { status: 'no-matching-time' };
|
||||||
|
|
||||||
// Check if I am in range of the center
|
// Check if I am in range of the center
|
||||||
const distanceToCenter = await getDistance();
|
const distanceToCenter = await getDistance();
|
||||||
if (distanceToCenter > 280) return 'out-of-range';
|
if (distanceToCenter > 280) return { status: 'out-of-range' };
|
||||||
|
|
||||||
// Check if I have already been notified
|
// Check if I have already been notified
|
||||||
if (await checkIdentifier(matching.name, now)) return 'already-notified';
|
const { marked, key } = await checkIdentifier(matching.name, now);
|
||||||
|
if (marked) return { status: 'already-notified', key };
|
||||||
|
|
||||||
// Send notification, mark
|
// Send notification, mark
|
||||||
await sendNotification(`${matching.message} (${matching.name})`);
|
await sendNotification(`${matching.message} (${matching.name})`);
|
||||||
await markIdentifier(matching.name, true, 60 * 60 * 24 * 31, now);
|
await markIdentifier(matching.name, true, 60 * 60 * 24 * 31, now);
|
||||||
|
|
||||||
return 'notified';
|
return { status: 'notified', key };
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -65,7 +70,7 @@ export default async function handler(
|
|||||||
if (process.env.NODE_ENV === 'production')
|
if (process.env.NODE_ENV === 'production')
|
||||||
result = await monitorAsync(innerFunction);
|
result = await monitorAsync(innerFunction);
|
||||||
else result = await innerFunction();
|
else result = await innerFunction();
|
||||||
res.status(200).json({ status: result });
|
res.status(200).json({ status: result.status, key: result.key });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
res.status(500).json({ status: 'error' });
|
res.status(500).json({ status: 'error' });
|
||||||
|
|||||||
Reference in New Issue
Block a user