Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/routes/v2/peruserver/trucky/live-jobs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1200,8 +1200,27 @@ router.get('/', async (req, res) => {
errors: options.includeErrors ? snapshot.errors : [],
};

// Etiqueta de estado del backup
let backup_status = 'empty';
if (responsePayload.jobs && Array.isArray(responsePayload.jobs)) {
if (responsePayload.jobs.length === 0) {
backup_status = 'empty';
} else if (responsePayload.errors && responsePayload.errors.length === responsePayload.jobs.length) {
backup_status = 'corrupt';
} else if (responsePayload.jobs.length > 0) {
backup_status = 'valid';
} else {
backup_status = 'invalid';
}
} else if (responsePayload.error) {
backup_status = 'error';
}

// Guardar snapshot en Supabase
try { await saveSnapshotInSupabase({ days: options.days, companiesSource: options.companiesSource }, responsePayload); } catch (e) { /* ignora error de backup */ }

res.set('Cache-Control', 'public, s-maxage=30, stale-while-revalidate=15');
return res.json(responsePayload);
return res.json({ ...responsePayload, backup_status });
} catch (error) {
const message = error instanceof Error ? error.message : 'Error desconocido';
res.set('Cache-Control', 'no-store, no-cache');
Expand Down
20 changes: 20 additions & 0 deletions src/routes/v2/peruserver/trucky/top-km/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,28 @@ router.get('/', async (req, res) => {
}

if (entry.payload) {
// Etiqueta de estado del backup
let backup_status = 'empty';
if (entry.payload.items && Array.isArray(entry.payload.items)) {
if (entry.payload.items.length === 0) {
backup_status = 'empty';
} else if (entry.payload.count_companies_errors === entry.payload.items.length) {
backup_status = 'corrupt';
} else if (entry.payload.count_companies_processed > 0) {
backup_status = 'valid';
} else {
backup_status = 'invalid';
}
} else if (entry.payload.ok === false) {
backup_status = 'error';
}

// Guardar payload en Supabase
try { await saveBackupPayload(params, entry.payload); } catch (e) { /* ignora error de backup */ }

return res.json({
...entry.payload,
backup_status,
cache: {
hasPayload: true,
refreshing: Boolean(entry.inFlight),
Expand Down
19 changes: 19 additions & 0 deletions src/routes/v2/peruserver/trucky/top-km/monthly.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,9 +588,28 @@ router.get('/', async (req, res) => {
}
}

// Etiqueta de estado del backup
let backup_status = 'empty';
if (entry.payload && Array.isArray(entry.payload.items)) {
if (entry.payload.items.length === 0) {
backup_status = 'empty';
} else if (entry.payload.count_companies_errors === entry.payload.items.length) {
backup_status = 'corrupt';
} else if (entry.payload.count_companies_processed > 0) {
backup_status = 'valid';
} else {
backup_status = 'invalid';
}
} else if (entry.payload && entry.payload.ok === false) {
backup_status = 'error';
}

// Siempre guardar el payload retornado en Supabase
if (entry.payload) {
try { await saveBackupPayload(params, entry.payload); } catch (e) { /* ignora error de backup */ }
return res.json({
...entry.payload,
backup_status,
cache: {
hasPayload: true,
refreshing: Boolean(entry.inFlight),
Expand Down
Loading