fix: scheduleForArticle picks unique slots, no collisions
This commit is contained in:
@@ -32,15 +32,27 @@ async function pickScheduleTime(channel) {
|
|||||||
return now; // публикуем сразу
|
return now; // публикуем сразу
|
||||||
}
|
}
|
||||||
|
|
||||||
// Сегодня — ближайший слот с временем > now
|
// Получаем уже занятые слоты (pending)
|
||||||
const todayMinutes = now.getHours() * 60 + now.getMinutes();
|
const { rows: taken } = await query(
|
||||||
const futureToday = slots.find(s => s.slot_hour * 60 + s.slot_minute > todayMinutes);
|
`SELECT scheduled_at FROM scheduled_posts WHERE channel_id=$1 AND status='pending'`,
|
||||||
if (futureToday) {
|
[channel.id]
|
||||||
const t = new Date(now);
|
);
|
||||||
t.setHours(futureToday.slot_hour, futureToday.slot_minute, 0, 0);
|
const takenTimes = new Set(taken.map(r => new Date(r.scheduled_at).getTime()));
|
||||||
|
|
||||||
|
// Ищем ближайший незанятый слот на ближайшие 7 дней
|
||||||
|
for (let dayOffset = 0; dayOffset <= 7; dayOffset++) {
|
||||||
|
const base = new Date(now);
|
||||||
|
base.setDate(base.getDate() + dayOffset);
|
||||||
|
for (const s of slots) {
|
||||||
|
const t = new Date(base);
|
||||||
|
t.setHours(s.slot_hour, s.slot_minute, 0, 0);
|
||||||
|
if (t > now && !takenTimes.has(t.getTime())) {
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
// Все слоты на сегодня прошли — берём первый завтрашний
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback — завтра первый слот
|
||||||
const tomorrow = new Date(now);
|
const tomorrow = new Date(now);
|
||||||
tomorrow.setDate(tomorrow.getDate() + 1);
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
||||||
tomorrow.setHours(slots[0].slot_hour, slots[0].slot_minute, 0, 0);
|
tomorrow.setHours(slots[0].slot_hour, slots[0].slot_minute, 0, 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user