feat: draft review flow — autogen→draft, auto-approve 07:00 MSK, /api/drafts routes
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
// draftAutoApprove.js
|
||||
// Каждый день в 07:00 МСК переводит все статьи status='draft' → 'published'
|
||||
// и ставит их на ближайшие publish_slots канала 1.
|
||||
|
||||
const { query } = require('./src/config/db');
|
||||
const { scheduleForArticle } = require('./src/services/articleAutoPublish');
|
||||
|
||||
const AUTO_APPROVE_HOUR_MSK = 7;
|
||||
let lastRunDate = null;
|
||||
|
||||
async function runDraftAutoApprove() {
|
||||
try {
|
||||
const { rows: drafts } = await query(
|
||||
`SELECT id, title, category FROM articles WHERE status='draft' ORDER BY created_at ASC`
|
||||
);
|
||||
|
||||
if (!drafts.length) {
|
||||
console.log('[DraftApprove] no drafts to approve');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[DraftApprove] approving ${drafts.length} drafts`);
|
||||
|
||||
for (const draft of drafts) {
|
||||
await query(
|
||||
`UPDATE articles SET status='published', published_at=NOW() WHERE id=$1`,
|
||||
[draft.id]
|
||||
);
|
||||
await scheduleForArticle(draft.id);
|
||||
console.log(`[DraftApprove] approved article=${draft.id} "${draft.title.slice(0, 50)}"`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[DraftApprove] error:', err.message);
|
||||
}
|
||||
}
|
||||
|
||||
function startDraftAutoApproveScheduler() {
|
||||
console.log('[DraftApprove] scheduler started (auto-approve at 07:00 MSK)');
|
||||
|
||||
setInterval(() => {
|
||||
const now = new Date();
|
||||
const msk = new Date(now.getTime() + 3 * 60 * 60 * 1000);
|
||||
const hour = msk.getUTCHours();
|
||||
const minute = msk.getUTCMinutes();
|
||||
const dateStr = msk.toISOString().slice(0, 10);
|
||||
|
||||
if (hour === AUTO_APPROVE_HOUR_MSK && minute === 0 && lastRunDate !== dateStr) {
|
||||
lastRunDate = dateStr;
|
||||
console.log(`[DraftApprove] triggered at ${msk.toISOString()}`);
|
||||
runDraftAutoApprove();
|
||||
}
|
||||
}, 60_000);
|
||||
}
|
||||
|
||||
module.exports = { startDraftAutoApproveScheduler, runDraftAutoApprove };
|
||||
Reference in New Issue
Block a user