fix: respect brief opt-out and report channel delivery failures

This commit is contained in:
2026-09-06 14:28:10 +03:00
parent c55257cade
commit 23512b7a87
5 changed files with 143 additions and 24 deletions
+5 -22
View File
@@ -3,6 +3,7 @@
// Telegram (bağlıysa) + e-posta. Son gönderim data/brief-son.json'da tutulur;
// saatlik kontrol, haftada bir gönderim (node-cron yok, setInterval).
import fs from 'node:fs';
import { createBriefSender } from './brief-delivery.mjs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import * as store from './user-store.mjs';
@@ -69,28 +70,10 @@ export function briefHtml(b) {
<p style="margin-top:20px;font-size:12px;color:#94a3b8">Bu brief her Pazartesi gönderilir; Bildirimler sekmesinden kapatabilirsiniz.</p></div>`;
}
export async function haftalikBriefGonder(tenderGetir, { zorla = false } = {}) {
const tenders = await tenderGetir();
const r = { kullanici: 0, gonderilen: 0, bos: 0, hata: 0 };
for (const u of store.listUsers()) {
r.kullanici++;
try {
const tercih = store.getData(u.email, 'brief');
if (tercih && tercih.kapali && !zorla) continue;
const b = briefOlustur(u.email, tenders);
if (b.bos) { r.bos++; continue; }
const tg = store.getData(u.email, 'telegram');
let gitti = false;
if (telegramConfigured() && tg?.chatId) gitti = await sendTelegram(tg.chatId, briefMetni(b).slice(0, 3900)).then(() => true).catch(() => false);
if (emailConfigured()) gitti = (await sendEmail({ to: u.email, subject: `İhaleist — Haftalık Brief (${b.hafta})`, html: briefHtml(b), text: briefMetni(b) }).then(() => true).catch(() => false)) || gitti;
if (gitti) r.gonderilen++;
} catch (e) {
r.hata++;
console.warn('[brief]', u.email, e.message);
}
}
return r;
}
export const haftalikBriefGonder = createBriefSender({
store, buildBrief:briefOlustur, text:briefMetni, html:briefHtml,
telegramConfigured, sendTelegram, emailConfigured, sendEmail,
});
export function briefScheduleKey(now = new Date()) {
const tr = new Date(now.getTime() + 3 * 3600000);