File size: 2,839 Bytes
e0454ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | // CRUD dos posts agendados + helpers de data (tudo em hora LOCAL do Mac).
import { all, get, run, insert } from '../db.js';
// "2026-06-28T08:00:00" no relogio local (sem fuso, evita confusao de timezone).
export function localNowString() {
const d = new Date();
const p = (n) => String(n).padStart(2, '0');
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}T${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
}
// soma N dias a uma data "YYYY-MM-DD" e devolve "YYYY-MM-DD".
export function addDays(dateStr, n) {
const [y, m, d] = dateStr.split('-').map(Number);
const dt = new Date(y, m - 1, d, 12, 0, 0); // meio-dia evita problemas de horario de verao
dt.setDate(dt.getDate() + n);
const p = (x) => String(x).padStart(2, '0');
return `${dt.getFullYear()}-${p(dt.getMonth() + 1)}-${p(dt.getDate())}`;
}
export function createPost(p) {
return insert(
`INSERT INTO posts (owner_id, workspace_id, campaign_id, account_id, platform, media_path, caption, scheduled_at, status)
VALUES (?,?,?,?,?,?,?,?, 'scheduled')`,
[p.ownerId, p.workspaceId, p.campaignId || null, p.accountId, p.platform, p.mediaPath, p.caption || '', p.scheduledAt]
);
}
export function listPosts(filter = {}) {
let sql = `SELECT p.*, a.display_name AS account_name
FROM posts p LEFT JOIN accounts a ON a.id = p.account_id`;
const where = [], params = [];
if (filter.ownerId) { where.push('p.owner_id = ?'); params.push(filter.ownerId); }
if (filter.campaignId) { where.push('p.campaign_id = ?'); params.push(filter.campaignId); }
if (filter.workspaceId) { where.push('p.workspace_id = ?'); params.push(filter.workspaceId); }
if (filter.status) { where.push('p.status = ?'); params.push(filter.status); }
if (where.length) sql += ' WHERE ' + where.join(' AND ');
sql += ' ORDER BY p.scheduled_at ASC';
return all(sql, params);
}
export function duePosts() {
return all(
"SELECT * FROM posts WHERE status = 'scheduled' AND scheduled_at <= ? ORDER BY scheduled_at ASC LIMIT 20",
[localNowString()]
);
}
export function cancelPost(id, ownerId) {
return run("UPDATE posts SET status = 'canceled' WHERE id = ? AND owner_id = ? AND status = 'scheduled'", [id, ownerId]);
}
export function markPosting(id) {
// marca como 'posting' so se ainda estiver 'scheduled' (evita disparo duplo)
return run("UPDATE posts SET status = 'posting' WHERE id = ? AND status = 'scheduled'", [id]).changes;
}
export function markPosted(id, externalId) {
return run("UPDATE posts SET status = 'posted', external_post_id = ?, posted_at = ? WHERE id = ?",
[externalId || null, localNowString(), id]);
}
export function markFailed(id, error) {
return run("UPDATE posts SET status = 'failed', error = ? WHERE id = ?", [String(error).slice(0, 500), id]);
}
export { get };
|