refactor: centralizar SimulatedResponseQueue y actualizar TaskItem

Co-authored-by: aider (openrouter/openai/gpt-5) <aider@aider.chat>
main
brobert 1 month ago
parent 8a7c8b7a5f
commit 1ad36ee898

@ -95,6 +95,11 @@
}
}
function toIsoSqlLocal(d: Date = new Date()): string {
const iso = d.toISOString();
return iso.substring(0, 23).replace('T', ' ');
}
async function doComplete() {
if (busy || completed) return;
busy = true;
@ -103,7 +108,7 @@
const res = await fetch(`/api/tasks/${id}/complete`, { method: "POST" });
if (res.ok) {
const data = await res.json().catch(() => null);
const newCompletedAt: string | null = data?.task?.completed_at ? String(data.task.completed_at) : new Date().toISOString().replace('T', ' ').replace('Z', '');
const newCompletedAt: string | null = data?.task?.completed_at ? String(data.task.completed_at) : toIsoSqlLocal(new Date());
// Si no tenía responsables, el backend te auto-asigna: reflejarlo localmente
if (hadNoAssignees && currentUserId) {
const set = new Set<string>(assignees || []);

@ -572,11 +572,10 @@ export const ResponseQueue = {
const startedAt = Date.now();
try {
const toIso = (d: Date) => d.toISOString().replace('T', ' ').replace('Z', '');
const msPerDay = 24 * 60 * 60 * 1000;
const sentThresholdIso = toIso(new Date(now.getTime() - this.RETENTION_DAYS_SENT * msPerDay));
const failedThresholdIso = toIso(new Date(now.getTime() - this.RETENTION_DAYS_FAILED * msPerDay));
const sentThresholdIso = toIsoSqlUTC(new Date(now.getTime() - this.RETENTION_DAYS_SENT * msPerDay));
const failedThresholdIso = toIsoSqlUTC(new Date(now.getTime() - this.RETENTION_DAYS_FAILED * msPerDay));
const cleanStatus = (status: 'sent' | 'failed', thresholdIso: string): number => {
let deleted = 0;

@ -5,11 +5,12 @@ import { TaskService } from '../../../src/tasks/service';
import { RemindersService } from '../../../src/services/reminders';
import { AllowedGroups } from '../../../src/services/allowed-groups';
import { ResponseQueue } from '../../../src/services/response-queue';
import { toIsoSql } from '../../helpers/dates';
function seedGroup(db: Database, groupId: string) {
const cols = db.query(`PRAGMA table_info(groups)`).all() as any[];
const values: Record<string, any> = {};
const nowIso = new Date().toISOString().replace('T', ' ').replace('Z', '');
const nowIso = toIsoSql(new Date());
for (const c of cols) {
const name = String(c.name);
@ -58,7 +59,7 @@ describe('RemindersService - gating por grupos en modo enforce', () => {
sent = [];
// Asegurar usuario receptor para satisfacer la FK de user_preferences
const iso = new Date().toISOString().replace('T', ' ').replace('Z', '');
const iso = toIsoSql(new Date());
memdb.exec(`
INSERT INTO users (id, first_seen, last_seen)
VALUES ('34600123456', '${iso}', '${iso}')

@ -5,9 +5,7 @@ import { TaskService } from '../../../src/tasks/service';
import { RemindersService } from '../../../src/services/reminders';
import { ResponseQueue } from '../../../src/services/response-queue';
function toIso(dt: Date): string {
return dt.toISOString().replace('T', ' ').replace('Z', '');
}
import { toIsoSql as toIso } from '../../helpers/dates';
describe('RemindersService', () => {
let memdb: Database;

@ -2,6 +2,7 @@ import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:tes
import { Database } from 'bun:sqlite';
import { initializeDatabase } from '../../../src/db';
import { ResponseQueue } from '../../../src/services/response-queue';
import { toIsoSql } from '../../helpers/dates';
let testDb: Database;
let envBackup: NodeJS.ProcessEnv;
@ -121,10 +122,10 @@ describe('ResponseQueue (retries/backoff)', () => {
});
function isoNow(): string {
return new Date().toISOString().replace('T', ' ').replace('Z', '');
return toIsoSql(new Date());
}
function isoFuture(ms: number): string {
return new Date(Date.now() + ms).toISOString().replace('T', ' ').replace('Z', '');
return toIsoSql(new Date(Date.now() + ms));
}
test('claimNextBatch should respect next_attempt_at (only eligible items are claimed)', () => {

@ -3,12 +3,13 @@ import Database from 'bun:sqlite';
import { initializeDatabase } from '../../../src/db';
import { TaskService } from '../../../src/tasks/service';
import { AllowedGroups } from '../../../src/services/allowed-groups';
import { toIsoSql } from '../../helpers/dates';
function seedGroup(db: Database, groupId: string) {
// Sembrado robusto: cubrir columnas NOT NULL sin valor por defecto
const cols = db.query(`PRAGMA table_info(groups)`).all() as any[];
const values: Record<string, any> = {};
const nowIso = new Date().toISOString().replace('T', ' ').replace('Z', '');
const nowIso = toIsoSql(new Date());
for (const c of cols) {
const name = String(c.name);

@ -3,18 +3,9 @@ import Database from 'bun:sqlite';
import { startWebServer } from './helpers/server';
import { createTempDb } from './helpers/db';
async function sha256Hex(input: string): Promise<string> {
const enc = new TextEncoder().encode(input);
const buf = await crypto.subtle.digest('SHA-256', enc);
const bytes = new Uint8Array(buf);
return Array.from(bytes)
.map((b) => b.toString(16).padStart(2, '0'))
.join('');
}
import { sha256Hex } from '../helpers/crypto';
function toIsoSql(d = new Date()): string {
return d.toISOString().replace('T', ' ').replace('Z', '');
}
import { toIsoSql } from '../helpers/dates';
function ymdUTC(date = new Date()): string {
const yyyy = String(date.getUTCFullYear()).padStart(4, '0');

@ -3,18 +3,9 @@ import Database from 'bun:sqlite';
import { startWebServer } from './helpers/server';
import { createTempDb } from './helpers/db';
async function sha256Hex(input: string): Promise<string> {
const enc = new TextEncoder().encode(input);
const buf = await crypto.subtle.digest('SHA-256', enc);
const bytes = new Uint8Array(buf);
return Array.from(bytes)
.map((b) => b.toString(16).padStart(2, '0'))
.join('');
}
import { sha256Hex } from '../helpers/crypto';
function toIsoSql(d = new Date()): string {
return d.toISOString().replace('T', ' ').replace('Z', '');
}
import { toIsoSql } from '../helpers/dates';
function ymdUTC(date = new Date()): string {
const yyyy = String(date.getUTCFullYear()).padStart(4, '0');

@ -3,18 +3,9 @@ import Database from 'bun:sqlite';
import { startWebServer } from './helpers/server';
import { createTempDb } from './helpers/db';
async function sha256Hex(input: string): Promise<string> {
const enc = new TextEncoder().encode(input);
const buf = await crypto.subtle.digest('SHA-256', enc);
const bytes = new Uint8Array(buf);
return Array.from(bytes)
.map((b) => b.toString(16).padStart(2, '0'))
.join('');
}
import { sha256Hex } from '../helpers/crypto';
function toIsoSql(d = new Date()): string {
return d.toISOString().replace('T', ' ').replace('Z', '');
}
import { toIsoSql } from '../helpers/dates';
function ymdUTC(date = new Date()): string {
const yyyy = String(date.getUTCFullYear()).padStart(4, '0');

Loading…
Cancel
Save