From 43c2d1100c05263506da836f1e047e546a6d1401 Mon Sep 17 00:00:00 2001 From: Xevion Date: Thu, 9 Mar 2023 18:14:38 -0600 Subject: [PATCH] Use localNow, return Redis key identifier inside response to improve debugging capability --- src/db.ts | 18 +++++++++++++----- src/pages/api/cron.ts | 21 +++++++++++++-------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/db.ts b/src/db.ts index 0935f09..3b5d51f 100644 --- a/src/db.ts +++ b/src/db.ts @@ -28,12 +28,17 @@ export function getKey(identifier: string, now: Date) { return format(now, 'yyyy-MM-dd') + ':' + identifier; } +// TODO; Move away from generating key inside these functions while also returning it. + export async function checkIdentifier( identifier: string, now: Date = new Date() -): Promise { +): Promise<{ marked: boolean; key: string }> { const key = getKey(identifier, now); - return (await redis.get(key)) === '1'; + return { + marked: (await redis.get(key)) === '1', + key + }; } export async function markIdentifier( @@ -41,8 +46,11 @@ export async function markIdentifier( value: boolean = true, expiry?: number, now: Date = new Date() -) { +): Promise<{ key: string }> { 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 }; } diff --git a/src/pages/api/cron.ts b/src/pages/api/cron.ts index bca4677..072744a 100644 --- a/src/pages/api/cron.ts +++ b/src/pages/api/cron.ts @@ -6,13 +6,14 @@ import { env } from '@/env/server.mjs'; import monitorAsync from '@/monitor'; import { sendNotification } from '@/notify'; import { fetchConfiguration, checkIdentifier, markIdentifier } from '@/db'; +import { localNow } from '@/utils/timezone'; type ResponseData = { diff: number; inRange: boolean; }; -type StatusData = { status: ResponseStatus }; +type StatusData = { status: ResponseStatus; key?: string }; type ResponseStatus = | 'unauthorized' @@ -37,27 +38,31 @@ export default async function handler( return; } - async function innerFunction(): Promise { - const now = new Date(); + async function innerFunction(): Promise<{ + status: ResponseStatus; + key?: string; + }> { + const now = localNow(); const config = await fetchConfiguration(); const matching = await getMatchingTime(config, now); // 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 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 - 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 await sendNotification(`${matching.message} (${matching.name})`); await markIdentifier(matching.name, true, 60 * 60 * 24 * 31, now); - return 'notified'; + return { status: 'notified', key }; } try { @@ -65,7 +70,7 @@ export default async function handler( if (process.env.NODE_ENV === 'production') result = await monitorAsync(innerFunction); else result = await innerFunction(); - res.status(200).json({ status: result }); + res.status(200).json({ status: result.status, key: result.key }); } catch (e) { console.error(e); res.status(500).json({ status: 'error' });