source: Klonkt/src/services/Scheduler.js@ 9effb80

main
Last change on this file since 9effb80 was 015ce46, checked in by roboburr <roboburr@…>, 3 months ago

fix(inplannen): scheduled posts never went live + timezone bug

(1) Scheduler compared publish_at (ISO 'T..Z') string-wise with SQLite
CURRENT_TIMESTAMP ('YYYY-MM-DD HH:MM:SS'); because 'T' > space the condition
was never true → scheduled posts NEVER flipped to published. Now
datetime(publish_at) <= datetime('now') (both UTC, same format).
(2) datetime-local was stored as UTC without timezone → 18:10 CEST became
18:10 UTC (= 20:10 local). Now: client converts local input to UTC ISO on save,
pre-fills the field from UTC→local, and shows "Scheduled for" in readable local
time instead of raw UTC ISO.

Co-Authored-By: Claude <noreply@…>

  • Property mode set to 100644
File size: 1.5 KB
Line 
1/**
2 * Scheduler — release-planning (premium #3).
3 *
4 * Geplande posts hebben status 'scheduled' + publish_at (toekomst). Een lichte
5 * timer zet ze op 'published' zodra publish_at bereikt is. Zo hoeven de publieke
6 * queries (status='published') NIET aangepast te worden — een geplande post is
7 * gewoon nog niet 'published' en dus nergens publiek zichtbaar tot het moment.
8 */
9
10import db from '../config/database.js';
11import HtmlSanitizerService from './HtmlSanitizerService.js';
12
13export function flipScheduledPosts() {
14 try {
15 const due = db.prepare(`
16 SELECT p.id, p.title, p.content, u.username
17 FROM posts p JOIN users u ON u.id = p.author_id
18 WHERE p.status = 'scheduled' AND p.publish_at IS NOT NULL AND datetime(p.publish_at) <= datetime('now')
19 `).all();
20 if (!due.length) return 0;
21 const upd = db.prepare(
22 "UPDATE posts SET status = 'published', published_at = COALESCE(published_at, publish_at, CURRENT_TIMESTAMP) WHERE id = ?"
23 );
24 const fts = db.prepare('INSERT INTO posts_fts(content, title, author, post_id) VALUES (?, ?, ?, ?)');
25 for (const p of due) {
26 upd.run(p.id);
27 try { fts.run(HtmlSanitizerService.toPlainText(p.content || ''), p.title || '', p.username || '', p.id); } catch { /* FTS niet-fataal */ }
28 }
29 return due.length;
30 } catch { return 0; }
31}
32
33let _timer = null;
34export function startScheduler() {
35 flipScheduledPosts(); // direct bij boot
36 if (_timer) return;
37 _timer = setInterval(flipScheduledPosts, 60 * 1000); // elke minuut
38 if (_timer.unref) _timer.unref();
39}
Note: See TracBrowser for help on using the repository browser.