mirror of
https://github.com/Xevion/bus-reminder.git
synced 2025-12-15 02:11:15 -06:00
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { Configuration, ConfigurationSchema } from './timing';
|
|
import { Redis } from 'ioredis';
|
|
import { env } from '@/env/server.mjs';
|
|
import { format } from 'date-fns';
|
|
|
|
const redis = new Redis(env.REDIS_URL, { maxRetriesPerRequest: 2 });
|
|
|
|
export async function fetchConfiguration(
|
|
defaultConfig?: object
|
|
): Promise<Configuration> {
|
|
const config = await redis.get('config');
|
|
if (config == null)
|
|
if (defaultConfig != undefined)
|
|
return ConfigurationSchema.parse(defaultConfig);
|
|
else throw new Error('Configuration not found in Redis');
|
|
return ConfigurationSchema.parse(JSON.parse(config));
|
|
}
|
|
|
|
export function getKey(identifier: string, now: Date) {
|
|
return format(now, 'yyyy-MM-dd') + ':' + identifier;
|
|
}
|
|
|
|
export async function checkIdentifier(
|
|
identifier: string,
|
|
now: Date = new Date()
|
|
): Promise<boolean> {
|
|
const key = getKey(identifier, now);
|
|
return (await redis.get(key)) === '1';
|
|
}
|
|
|
|
export async function markIdentifier(
|
|
identifier: string,
|
|
value: boolean = true,
|
|
expiry?: number,
|
|
now: Date = new Date()
|
|
) {
|
|
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);
|
|
}
|