Fix: stamp semana_iniciada_em when campaign first transitions to 'rodando'
Previously, setCampaignStatus did not set semana_iniciada_em, causing
escalateWeeklyLimits to bail early (if (!campaign.semana_iniciada_em) continue).
Now when a campaign transitions to 'rodando' for the first time, semana_iniciada_em
is set to datetime('now'). Subsequent rodando transitions leave it unchanged
(using COALESCE to preserve first-run timestamp).
Regression tests confirm:
- First transition to 'rodando' stamps semana_iniciada_em
- Subsequent transitions do not reset the timestamp
- resumeCampaign continues to work correctly (pause/resume cycle)
- All 105 tests pass
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
fb2cd75a56
commit
577f48ab19
@@ -44,9 +44,18 @@ export function listVariants(db, campaignId) {
|
||||
// status: 'rascunho' -> 'rodando' -> 'pausado' <-> 'rodando' -> 'concluido'.
|
||||
// pause_reason so fica setado quando o novo status e 'pausado'.
|
||||
export function setCampaignStatus(db, campaignId, status, pauseReason = null) {
|
||||
db.prepare(
|
||||
`UPDATE outreach_campaigns SET status = ?, pause_reason = ?, updated_at = datetime('now') WHERE id = ?`
|
||||
).run(status, status === 'pausado' ? pauseReason : null, campaignId);
|
||||
if (status === 'rodando') {
|
||||
// When transitioning to 'rodando', stamp semana_iniciada_em only on first run (if currently NULL)
|
||||
db.prepare(
|
||||
`UPDATE outreach_campaigns
|
||||
SET status = ?, pause_reason = ?, semana_iniciada_em = COALESCE(semana_iniciada_em, datetime('now')), updated_at = datetime('now')
|
||||
WHERE id = ?`
|
||||
).run(status, status === 'pausado' ? pauseReason : null, campaignId);
|
||||
} else {
|
||||
db.prepare(
|
||||
`UPDATE outreach_campaigns SET status = ?, pause_reason = ?, updated_at = datetime('now') WHERE id = ?`
|
||||
).run(status, status === 'pausado' ? pauseReason : null, campaignId);
|
||||
}
|
||||
return getCampaign(db, campaignId);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user