All checks were successful
- ключи бота перенесены в секреты Actions репозитория (не в файлах) - /api/settings отдаёт telegramProblem; экран показывает причину и «Подробнее» - привязка-пример сбрасывается при старте с настоящим ботом Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
65 lines
2.2 KiB
TypeScript
65 lines
2.2 KiB
TypeScript
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
|
|
/** Итог последнего запуска по расписанию. */
|
|
export interface ScheduledRunResult {
|
|
at: string;
|
|
status: "sent" | "nothing_new" | "error";
|
|
/** Сколько стажировок отправлено (для sent). */
|
|
sent: number;
|
|
error?: string;
|
|
}
|
|
|
|
export interface NotificationSettings {
|
|
/** Чат Телеграма, привязанный к сервису; null — не привязан. */
|
|
chatId: number | null;
|
|
/** Через какого бота привязан чат: пример или настоящий. Старые данные без поля — по chatId. */
|
|
linkedVia?: "mock" | "telegram";
|
|
enabled: boolean;
|
|
/** Ожидающий код привязки. */
|
|
linkCode: string | null;
|
|
linkCodeExpiresAt: string | null;
|
|
/** Смещение для getUpdates. */
|
|
updateOffset: number;
|
|
lastRun: ScheduledRunResult | null;
|
|
}
|
|
|
|
export const emptySettings = (): NotificationSettings => ({
|
|
chatId: null,
|
|
enabled: false,
|
|
linkCode: null,
|
|
linkCodeExpiresAt: null,
|
|
updateOffset: 0,
|
|
lastRun: null,
|
|
});
|
|
|
|
export interface SettingsStore {
|
|
load(): Promise<NotificationSettings>;
|
|
save(settings: NotificationSettings): Promise<void>;
|
|
}
|
|
|
|
/** Настройки в `<dir>/notifications.json`; запись атомарная. */
|
|
export class JsonSettingsStore implements SettingsStore {
|
|
private readonly file: string;
|
|
|
|
constructor(private readonly dir: string) {
|
|
this.file = path.join(dir, "notifications.json");
|
|
}
|
|
|
|
async load(): Promise<NotificationSettings> {
|
|
try {
|
|
return { ...emptySettings(), ...(JSON.parse(await readFile(this.file, "utf8")) as Partial<NotificationSettings>) };
|
|
} catch (error) {
|
|
if ((error as NodeJS.ErrnoException).code === "ENOENT") return emptySettings();
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async save(settings: NotificationSettings): Promise<void> {
|
|
await mkdir(this.dir, { recursive: true });
|
|
const tmp = `${this.file}.${process.pid}.tmp`;
|
|
await writeFile(tmp, JSON.stringify(settings, null, 2), "utf8");
|
|
await rename(tmp, this.file);
|
|
}
|
|
}
|