Compare commits
7 Commits
6e54efe5e4
...
b1eaf41681
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b1eaf41681 | ||
|
|
bd7e499ab7 | ||
|
|
44144ebf9d | ||
|
|
314a74982c | ||
|
|
76d3f00f29 | ||
|
|
214410d2fb | ||
|
|
199922272f |
@@ -187,11 +187,13 @@ class TenantConnectionManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove idle pools (not accessed in last 5 minutes).
|
||||
* Remove idle pools (not accessed in last 12 hours).
|
||||
* SAT syncs (initial/daily) can run for hours in background;
|
||||
* a 5-minute timeout caused 'pool already ended' errors mid-sync.
|
||||
*/
|
||||
private cleanupIdlePools(): void {
|
||||
const now = Date.now();
|
||||
const maxIdle = 5 * 60 * 1000;
|
||||
const maxIdle = 12 * 60 * 60 * 1000;
|
||||
|
||||
for (const [tenantId, entry] of this.pools.entries()) {
|
||||
if (now - entry.lastAccess.getTime() > maxIdle) {
|
||||
|
||||
@@ -125,7 +125,9 @@ export async function resolverAlertaManual(req: Request, res: Response, next: Ne
|
||||
export async function getAlertasAutomaticas(req: Request, res: Response, next: NextFunction) {
|
||||
try {
|
||||
const contribuyenteId = req.query.contribuyenteId as string | undefined;
|
||||
console.log(`[AlertasCtrl] GET /automaticas tenant=${req.user!.tenantId} contribuyente=${contribuyenteId || 'null'} user=${req.user!.userId} role=${req.user!.role}`);
|
||||
const alertas = await generarAlertasAutomaticas(req.tenantPool!, req.user!.tenantId, contribuyenteId || null);
|
||||
console.log(`[AlertasCtrl] GET /automaticas devuelve ${alertas.length} alertas: ${alertas.map(a => a.id).join(', ') || 'ninguna'}`);
|
||||
res.json(alertas);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
|
||||
@@ -10,6 +10,21 @@ import { despachoPlanTieneDualidadDb } from '../services/plan-catalogo.service.j
|
||||
import { emailService } from '../services/email/email.service.js';
|
||||
import { getTenantOwnerEmail } from '../utils/memberships.js';
|
||||
|
||||
/**
|
||||
* Calcula la siguiente fecha de fin de período según la frecuencia.
|
||||
* Usa el mismo algoritmo que Mercado Pago: mismo día del mes siguiente,
|
||||
* ajustando al último día si el mes destino tiene menos días.
|
||||
*/
|
||||
function computeNextPeriodEnd(date: Date, frequency: string): Date {
|
||||
const d = new Date(date);
|
||||
if (frequency === 'monthly') {
|
||||
d.setMonth(d.getMonth() + 1);
|
||||
} else if (frequency === 'annual' || frequency === 'yearly') {
|
||||
d.setFullYear(d.getFullYear() + 1);
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
export async function handleMercadoPagoWebhook(req: Request, res: Response, next: NextFunction) {
|
||||
try {
|
||||
const { type, data } = req.body;
|
||||
@@ -187,9 +202,20 @@ async function handlePaymentNotification(paymentId: string) {
|
||||
// precio de renewal. Se detecta comparando el monto cobrado contra lo que
|
||||
// `getPlanPrice(phase='firstYear')` devolvería para este plan.
|
||||
const esPrimerPago = subscription.status === 'pending';
|
||||
const updateData: { status: string; currentPeriodEnd?: Date } = { status: 'authorized' };
|
||||
|
||||
// Extender currentPeriodEnd para renovaciones recurrentes.
|
||||
// El primer pago ya tiene currentPeriodEnd establecido al crear la suscripción;
|
||||
// solo extendemos en pagos subsecuentes para reflejar el nuevo período cobrado.
|
||||
if (!esPrimerPago && subscription.currentPeriodEnd) {
|
||||
const nextPeriodEnd = computeNextPeriodEnd(subscription.currentPeriodEnd, subscription.frequency);
|
||||
updateData.currentPeriodEnd = nextPeriodEnd;
|
||||
console.log(`[WEBHOOK] Subscription ${subscription.id} extended to ${nextPeriodEnd.toISOString()} (${subscription.frequency})`);
|
||||
}
|
||||
|
||||
await prisma.subscription.update({
|
||||
where: { id: subscription.id },
|
||||
data: { status: 'authorized' },
|
||||
data: updateData,
|
||||
});
|
||||
subscriptionService.invalidateSubscriptionCache(tenantId);
|
||||
|
||||
|
||||
@@ -21,19 +21,50 @@ const EXPIRY_REMINDERS_CRON = '0 9 * * *'; // 9:00 AM diario — avisos p
|
||||
let isRunning = false;
|
||||
let isIncrementalRunning = false;
|
||||
|
||||
/**
|
||||
* Verifica si un tenant tiene FIEL a nivel tenant (legacy Horux 360)
|
||||
* o a nivel contribuyente (modelo despacho).
|
||||
*/
|
||||
async function hasAnyFielConfigured(tenantId: string, databaseName?: string | null): Promise<boolean> {
|
||||
// 1) FIEL legacy a nivel tenant
|
||||
const hasLegacy = await hasFielConfigured(tenantId);
|
||||
if (hasLegacy) return true;
|
||||
|
||||
// 2) FIEL por contribuyente (modelo despacho)
|
||||
if (!databaseName) {
|
||||
const tenant = await prisma.tenant.findUnique({
|
||||
where: { id: tenantId },
|
||||
select: { databaseName: true },
|
||||
});
|
||||
databaseName = tenant?.databaseName;
|
||||
}
|
||||
if (!databaseName) return false;
|
||||
|
||||
try {
|
||||
const pool = await tenantDb.getPool(tenantId, databaseName);
|
||||
const { rows } = await pool.query(
|
||||
`SELECT 1 FROM fiel_contribuyente WHERE is_active = true LIMIT 1`
|
||||
);
|
||||
return rows.length > 0;
|
||||
} catch (err: any) {
|
||||
console.error(`[SAT Cron] Error verificando FIEL contribuyente para tenant ${tenantId}:`, err.message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtiene los tenants que tienen FIEL configurada y activa
|
||||
*/
|
||||
async function getTenantsWithFiel(): Promise<string[]> {
|
||||
const tenants = await prisma.tenant.findMany({
|
||||
where: { active: true },
|
||||
select: { id: true },
|
||||
select: { id: true, databaseName: true },
|
||||
});
|
||||
|
||||
const tenantsWithFiel: string[] = [];
|
||||
|
||||
for (const tenant of tenants) {
|
||||
const hasFiel = await hasFielConfigured(tenant.id);
|
||||
const hasFiel = await hasAnyFielConfigured(tenant.id, tenant.databaseName);
|
||||
if (hasFiel) {
|
||||
tenantsWithFiel.push(tenant.id);
|
||||
}
|
||||
@@ -172,12 +203,12 @@ async function getTenantsConSatIncremental(): Promise<string[]> {
|
||||
|
||||
const tenants = await prisma.tenant.findMany({
|
||||
where: { active: true, plan: { in: planNames as any } },
|
||||
select: { id: true },
|
||||
select: { id: true, databaseName: true },
|
||||
});
|
||||
|
||||
const result: string[] = [];
|
||||
for (const tenant of tenants) {
|
||||
if (await hasFielConfigured(tenant.id)) {
|
||||
if (await hasAnyFielConfigured(tenant.id, tenant.databaseName)) {
|
||||
result.push(tenant.id);
|
||||
}
|
||||
}
|
||||
@@ -351,6 +382,8 @@ async function runCsfJob(): Promise<void> {
|
||||
console.error(`[CSF Cron] Error para ${tenant.rfc}:`, error.message);
|
||||
failed++;
|
||||
}
|
||||
// Delay entre tenants para no saturar al SAT y reducir bloqueos por IP
|
||||
await new Promise(r => setTimeout(r, 30_000));
|
||||
}
|
||||
console.log(`[CSF Cron] Completado — éxito: ${success}, fallidos: ${failed}, sin FIEL: ${skipped}`);
|
||||
}
|
||||
|
||||
@@ -66,11 +66,13 @@ export async function getClientesStats(range: ClientesStatsRange): Promise<Clien
|
||||
paymentsCount: payments._count,
|
||||
};
|
||||
|
||||
// 3) Clientes que NO renovaron: subs cuyo currentPeriodEnd cae en el rango
|
||||
// y que están en status terminal (cancelled, trial_expired, paused) o sin
|
||||
// payment posterior aprobado. Nota: un sub `authorized` con periodEnd
|
||||
// pasado es un "se renovó automáticamente" — para detectar no-renovaciones
|
||||
// miramos status efectivo + ausencia de payment en los siguientes 7 días.
|
||||
// 3) Clientes que NO renovaron:
|
||||
// a) Subs cuyo currentPeriodEnd cae en el rango y están en status terminal
|
||||
// (cancelled, trial_expired, paused).
|
||||
// b) Tenants cuyo trialEndsAt ya pasó y NO tienen suscripción authorized
|
||||
// (incluye trials que nunca convirtieron o cuya sub fue borrada).
|
||||
// c) Tenants con sub trial vencida (currentPeriodEnd < ahora) que nunca
|
||||
// fue marcada trial_expired por el cron.
|
||||
const subsExpiradas = await prisma.subscription.findMany({
|
||||
where: {
|
||||
currentPeriodEnd: { gte: range.from, lte: range.to },
|
||||
@@ -84,14 +86,99 @@ export async function getClientesStats(range: ClientesStatsRange): Promise<Clien
|
||||
tenant: { select: { id: true, nombre: true, rfc: true } },
|
||||
},
|
||||
});
|
||||
const noRenovaciones = subsExpiradas.map(s => ({
|
||||
tenantId: s.tenantId,
|
||||
tenantNombre: s.tenant?.nombre ?? '',
|
||||
rfc: s.tenant?.rfc ?? '',
|
||||
plan: String(s.plan),
|
||||
currentPeriodEnd: s.currentPeriodEnd?.toISOString() ?? '',
|
||||
statusActual: s.status,
|
||||
}));
|
||||
|
||||
const noRenovacionesMap = new Map<string, ClientesStats['noRenovaciones'][number]>();
|
||||
for (const s of subsExpiradas) {
|
||||
noRenovacionesMap.set(s.tenantId, {
|
||||
tenantId: s.tenantId,
|
||||
tenantNombre: s.tenant?.nombre ?? '',
|
||||
rfc: s.tenant?.rfc ?? '',
|
||||
plan: String(s.plan),
|
||||
currentPeriodEnd: s.currentPeriodEnd?.toISOString() ?? '',
|
||||
statusActual: s.status,
|
||||
});
|
||||
}
|
||||
|
||||
// b + c) Trials vencidos / sin suscripción activa / subs borradas
|
||||
const now = new Date();
|
||||
const tenantsConSubAutorizada = new Set(
|
||||
(await prisma.subscription.findMany({
|
||||
where: { status: 'authorized' },
|
||||
select: { tenantId: true },
|
||||
})).map(s => s.tenantId)
|
||||
);
|
||||
const excluded = Array.from(tenantsConSubAutorizada);
|
||||
|
||||
// Tenants con trialEndsAt pasado y sin sub authorized
|
||||
const tenantsTrialsVencidos = await prisma.tenant.findMany({
|
||||
where: {
|
||||
trialEndsAt: { lt: now },
|
||||
id: { notIn: excluded },
|
||||
},
|
||||
select: { id: true, nombre: true, rfc: true, plan: true, trialEndsAt: true },
|
||||
});
|
||||
for (const t of tenantsTrialsVencidos) {
|
||||
if (noRenovacionesMap.has(t.id)) continue;
|
||||
noRenovacionesMap.set(t.id, {
|
||||
tenantId: t.id,
|
||||
tenantNombre: t.nombre,
|
||||
rfc: t.rfc ?? '',
|
||||
plan: String(t.plan ?? 'trial'),
|
||||
currentPeriodEnd: t.trialEndsAt?.toISOString() ?? '',
|
||||
statusActual: 'trial_expired',
|
||||
});
|
||||
}
|
||||
|
||||
// Tenants con sub trial vencida (currentPeriodEnd < ahora) que nunca fue
|
||||
// marcada trial_expired por el cron, y no tienen otra sub authorized.
|
||||
const subsTrialVencidas = await prisma.subscription.findMany({
|
||||
where: {
|
||||
status: 'trial',
|
||||
currentPeriodEnd: { lt: now },
|
||||
tenantId: { notIn: excluded },
|
||||
},
|
||||
select: {
|
||||
tenantId: true,
|
||||
plan: true,
|
||||
currentPeriodEnd: true,
|
||||
tenant: { select: { id: true, nombre: true, rfc: true } },
|
||||
},
|
||||
});
|
||||
for (const s of subsTrialVencidas) {
|
||||
if (noRenovacionesMap.has(s.tenantId)) continue;
|
||||
noRenovacionesMap.set(s.tenantId, {
|
||||
tenantId: s.tenantId,
|
||||
tenantNombre: s.tenant?.nombre ?? '',
|
||||
rfc: s.tenant?.rfc ?? '',
|
||||
plan: String(s.plan),
|
||||
currentPeriodEnd: s.currentPeriodEnd?.toISOString() ?? '',
|
||||
statusActual: 'trial_expired',
|
||||
});
|
||||
}
|
||||
|
||||
// Tenants con plan de pago asignado manualmente (plan != 'trial') pero
|
||||
// sin NINGUNA suscripción. Indica que nunca iniciaron el flujo de pago.
|
||||
const tenantsConPlanPeroSinSub = await prisma.tenant.findMany({
|
||||
where: {
|
||||
plan: { not: 'trial' },
|
||||
id: { notIn: excluded },
|
||||
subscriptions: { none: {} },
|
||||
},
|
||||
select: { id: true, nombre: true, rfc: true, plan: true, createdAt: true },
|
||||
});
|
||||
for (const t of tenantsConPlanPeroSinSub) {
|
||||
if (noRenovacionesMap.has(t.id)) continue;
|
||||
noRenovacionesMap.set(t.id, {
|
||||
tenantId: t.id,
|
||||
tenantNombre: t.nombre,
|
||||
rfc: t.rfc ?? '',
|
||||
plan: String(t.plan),
|
||||
currentPeriodEnd: t.createdAt.toISOString(),
|
||||
statusActual: 'sin_suscripcion',
|
||||
});
|
||||
}
|
||||
|
||||
const noRenovaciones = Array.from(noRenovacionesMap.values());
|
||||
|
||||
// 4) Usuarios por cliente (memberships activos por tenant)
|
||||
const memberships = await prisma.tenantMembership.findMany({
|
||||
|
||||
@@ -609,30 +609,46 @@ async function alertaOpinionCumplimiento(pool: Pool, contribuyenteId?: string |
|
||||
|
||||
/**
|
||||
* Genera todas las alertas automáticas para un tenant.
|
||||
* Cada alerta se envuelve en try/catch para que un fallo en una no
|
||||
* bloquee el resto (robustez ante timeouts o errores transitorios).
|
||||
*/
|
||||
export async function generarAlertasAutomaticas(
|
||||
pool: Pool,
|
||||
tenantId: string,
|
||||
contribuyenteId?: string | null,
|
||||
): Promise<AlertaAuto[]> {
|
||||
const alertas = await Promise.all([
|
||||
alertaListaNegraPropia(pool, tenantId, contribuyenteId),
|
||||
alertaClienteListaNegra(pool, contribuyenteId),
|
||||
alertaProveedorListaNegra(pool, contribuyenteId),
|
||||
alertaDiscrepanciaRegimen(pool, tenantId, contribuyenteId),
|
||||
alertaConcentracionClientes(pool, contribuyenteId),
|
||||
alertaConcentracionProveedores(pool, contribuyenteId),
|
||||
alertaRiesgoCambiario(pool, contribuyenteId),
|
||||
alertaRiesgoCancelaciones(pool, contribuyenteId),
|
||||
alertaRiesgoTransaccional(pool, contribuyenteId),
|
||||
alertaCancelacionPeriodoAnterior(pool, contribuyenteId),
|
||||
alertaOpinionCumplimiento(pool, contribuyenteId),
|
||||
alertaTipoRelacionSospechosa(pool, contribuyenteId),
|
||||
alertaTareasProximasVencer(pool, contribuyenteId),
|
||||
alertaResicoPfLimiteIngresos(pool, contribuyenteId),
|
||||
]);
|
||||
const generadores: { name: string; fn: () => Promise<AlertaAuto | null> }[] = [
|
||||
{ name: 'lista-negra-propia', fn: () => alertaListaNegraPropia(pool, tenantId, contribuyenteId) },
|
||||
{ name: 'lista-negra-clientes', fn: () => alertaClienteListaNegra(pool, contribuyenteId) },
|
||||
{ name: 'lista-negra-proveedores', fn: () => alertaProveedorListaNegra(pool, contribuyenteId) },
|
||||
{ name: 'discrepancia-regimen', fn: () => alertaDiscrepanciaRegimen(pool, tenantId, contribuyenteId) },
|
||||
{ name: 'concentracion-clientes', fn: () => alertaConcentracionClientes(pool, contribuyenteId) },
|
||||
{ name: 'concentracion-proveedores', fn: () => alertaConcentracionProveedores(pool, contribuyenteId) },
|
||||
{ name: 'riesgo-cambiario', fn: () => alertaRiesgoCambiario(pool, contribuyenteId) },
|
||||
{ name: 'riesgo-cancelaciones', fn: () => alertaRiesgoCancelaciones(pool, contribuyenteId) },
|
||||
{ name: 'riesgo-transaccional', fn: () => alertaRiesgoTransaccional(pool, contribuyenteId) },
|
||||
{ name: 'cancelacion-periodo-anterior', fn: () => alertaCancelacionPeriodoAnterior(pool, contribuyenteId) },
|
||||
{ name: 'opinion-cumplimiento', fn: () => alertaOpinionCumplimiento(pool, contribuyenteId) },
|
||||
{ name: 'tipo-relacion-sospechosa', fn: () => alertaTipoRelacionSospechosa(pool, contribuyenteId) },
|
||||
{ name: 'tareas-proximas-vencer', fn: () => alertaTareasProximasVencer(pool, contribuyenteId) },
|
||||
{ name: 'resico-pf-limite-ingresos', fn: () => alertaResicoPfLimiteIngresos(pool, contribuyenteId) },
|
||||
];
|
||||
|
||||
return alertas.filter((a): a is AlertaAuto => a !== null);
|
||||
const alertas: AlertaAuto[] = [];
|
||||
for (const g of generadores) {
|
||||
try {
|
||||
const a = await g.fn();
|
||||
if (a) alertas.push(a);
|
||||
} catch (err: any) {
|
||||
console.error(`[AlertasAuto] Fallo ${g.name} (tenant=${tenantId}, contribuyente=${contribuyenteId}):`, err.message || err);
|
||||
}
|
||||
}
|
||||
|
||||
if (alertas.length > 0) {
|
||||
console.log(`[AlertasAuto] tenant=${tenantId} contribuyente=${contribuyenteId || 'null'} generadas=${alertas.map(a => a.id).join(', ')}`);
|
||||
}
|
||||
|
||||
return alertas;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -44,6 +44,9 @@ function rowToConstancia(r: any): ConstanciaRow {
|
||||
* sincroniza automáticamente domicilio + regímenes activos con lo que reporta
|
||||
* el SAT. El auto-fill NO es destructivo para datos custom del usuario:
|
||||
* solo sobreescribe campos si la CSF tiene un valor no-vacío.
|
||||
*
|
||||
* Incluye retry con backoff (3 intentos) para robustez ante timeouts
|
||||
* transitorios del portal SAT (mantenimiento nocturno, congestión, etc.).
|
||||
*/
|
||||
export async function consultarConstancia(tenantId: string): Promise<ConstanciaRow> {
|
||||
const fiel = await getDecryptedFiel(tenantId);
|
||||
@@ -55,72 +58,78 @@ export async function consultarConstancia(tenantId: string): Promise<ConstanciaR
|
||||
});
|
||||
if (!tenant) throw new Error('Tenant no encontrado');
|
||||
|
||||
const tempId = randomUUID();
|
||||
const tempDir = join(tmpdir(), `horux-csf-${tempId}`);
|
||||
mkdirSync(tempDir, { recursive: true, mode: 0o700 });
|
||||
const cerPath = join(tempDir, 'cert.cer');
|
||||
const keyPath = join(tempDir, 'key.key');
|
||||
const MAX_RETRIES = 3;
|
||||
const RETRY_DELAYS = [5_000, 15_000, 30_000]; // backoff
|
||||
|
||||
try {
|
||||
writeFileSync(cerPath, Buffer.from(fiel.cerContent, 'binary'), { mode: 0o600 });
|
||||
writeFileSync(keyPath, Buffer.from(fiel.keyContent, 'binary'), { mode: 0o600 });
|
||||
for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
|
||||
const tempId = randomUUID();
|
||||
const tempDir = join(tmpdir(), `horux-csf-${tempId}`);
|
||||
mkdirSync(tempDir, { recursive: true, mode: 0o700 });
|
||||
const cerPath = join(tempDir, 'cert.cer');
|
||||
const keyPath = join(tempDir, 'key.key');
|
||||
|
||||
// Headless por default. El fix de dispatchEvent en sat-csf-login cubre el
|
||||
// caso donde el click sintético no dispara el handler del SAT. Si algún
|
||||
// ambiente necesita ver el browser (debug), setear SAT_HEADLESS=false.
|
||||
const headless = process.env.SAT_HEADLESS !== 'false';
|
||||
const browser = await chromium.launch({
|
||||
headless,
|
||||
args: ['--disable-blink-features=AutomationControlled'],
|
||||
ignoreDefaultArgs: ['--enable-automation'],
|
||||
});
|
||||
try {
|
||||
const timeoutPromise = new Promise<never>((_, reject) =>
|
||||
setTimeout(() => reject(new Error('Timeout: proceso de CSF excedió 3 minutos')), PROCESS_TIMEOUT),
|
||||
);
|
||||
writeFileSync(cerPath, Buffer.from(fiel.cerContent, 'binary'), { mode: 0o600 });
|
||||
writeFileSync(keyPath, Buffer.from(fiel.keyContent, 'binary'), { mode: 0o600 });
|
||||
|
||||
const resultPromise = (async () => {
|
||||
const session = await loginSatCsf(browser, cerPath, keyPath, fiel.password, fiel.rfc);
|
||||
const pdfBuffer = await extractCsfPdf(session);
|
||||
const csf = await parseCsfPdf(pdfBuffer);
|
||||
|
||||
const pool = await tenantDb.getPool(tenantId, tenant.databaseName);
|
||||
const { rows } = await pool.query(
|
||||
`INSERT INTO constancias_situacion_fiscal
|
||||
(rfc, id_cif, razon_social, estatus_padron, fecha_emision, datos, pdf)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id, rfc, id_cif, razon_social, estatus_padron, fecha_emision,
|
||||
datos, fecha_consulta, created_at`,
|
||||
[
|
||||
csf.rfc,
|
||||
csf.idCIF,
|
||||
csf.razonSocial ?? [csf.nombre, csf.primerApellido, csf.segundoApellido].filter(Boolean).join(' ') ?? null,
|
||||
csf.estatusPadron,
|
||||
csf.lugarFechaEmision,
|
||||
JSON.stringify(csf),
|
||||
pdfBuffer,
|
||||
],
|
||||
const headless = process.env.SAT_HEADLESS !== 'false';
|
||||
const browser = await chromium.launch({
|
||||
headless,
|
||||
args: ['--disable-blink-features=AutomationControlled'],
|
||||
ignoreDefaultArgs: ['--enable-automation'],
|
||||
});
|
||||
try {
|
||||
const timeoutPromise = new Promise<never>((_, reject) =>
|
||||
setTimeout(() => reject(new Error('Timeout: proceso de CSF excedió 5 minutos')), 300_000),
|
||||
);
|
||||
|
||||
// Auto-fill domicilio del tenant + regímenes activos desde el CSF.
|
||||
// Se hace después del INSERT para que si algo falla en la sincronización
|
||||
// la CSF ya quedó guardada y el usuario puede verla.
|
||||
await sincronizarDatosFiscales(tenantId, csf).catch(err => {
|
||||
console.error(`[CSF] Error sincronizando datos fiscales para tenant ${tenantId}:`, err);
|
||||
});
|
||||
const resultPromise = (async () => {
|
||||
const session = await loginSatCsf(browser, cerPath, keyPath, fiel.password, fiel.rfc);
|
||||
const pdfBuffer = await extractCsfPdf(session);
|
||||
const csf = await parseCsfPdf(pdfBuffer);
|
||||
|
||||
return rowToConstancia(rows[0]);
|
||||
})();
|
||||
const pool = await tenantDb.getPool(tenantId, tenant.databaseName);
|
||||
const { rows } = await pool.query(
|
||||
`INSERT INTO constancias_situacion_fiscal
|
||||
(rfc, id_cif, razon_social, estatus_padron, fecha_emision, datos, pdf)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id, rfc, id_cif, razon_social, estatus_padron, fecha_emision,
|
||||
datos, fecha_consulta, created_at`,
|
||||
[
|
||||
csf.rfc,
|
||||
csf.idCIF,
|
||||
csf.razonSocial ?? [csf.nombre, csf.primerApellido, csf.segundoApellido].filter(Boolean).join(' ') ?? null,
|
||||
csf.estatusPadron,
|
||||
csf.lugarFechaEmision,
|
||||
JSON.stringify(csf),
|
||||
pdfBuffer,
|
||||
],
|
||||
);
|
||||
|
||||
return await Promise.race([resultPromise, timeoutPromise]);
|
||||
await sincronizarDatosFiscales(tenantId, csf).catch(err => {
|
||||
console.error(`[CSF] Error sincronizando datos fiscales para tenant ${tenantId}:`, err);
|
||||
});
|
||||
|
||||
return rowToConstancia(rows[0]);
|
||||
})();
|
||||
|
||||
return await Promise.race([resultPromise, timeoutPromise]);
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
} catch (err: any) {
|
||||
const willRetry = attempt < MAX_RETRIES - 1;
|
||||
console.error(`[CSF] Intento ${attempt + 1}/${MAX_RETRIES} falló para tenant ${tenantId}: ${err.message}${willRetry ? ` — reintentando en ${RETRY_DELAYS[attempt]}ms...` : ''}`);
|
||||
if (!willRetry) throw err;
|
||||
await new Promise(r => setTimeout(r, RETRY_DELAYS[attempt]));
|
||||
} finally {
|
||||
await browser.close();
|
||||
try { unlinkSync(cerPath); } catch { /* ok */ }
|
||||
try { unlinkSync(keyPath); } catch { /* ok */ }
|
||||
try { rmdirSync(tempDir); } catch { /* ok */ }
|
||||
}
|
||||
} finally {
|
||||
try { unlinkSync(cerPath); } catch { /* ok */ }
|
||||
try { unlinkSync(keyPath); } catch { /* ok */ }
|
||||
try { rmdirSync(tempDir); } catch { /* ok */ }
|
||||
}
|
||||
|
||||
throw new Error('No debería llegar aquí');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -348,6 +348,17 @@ export async function emitInvoiceIfApplicable(paymentId: string): Promise<void>
|
||||
data: { facturapiInvoiceId: invoice.id },
|
||||
});
|
||||
|
||||
// Enviar factura por email al cliente cuando se factura con datos reales
|
||||
// (no público en general). Fail-soft: si el envío falla, no bloquea.
|
||||
if (customer?.email) {
|
||||
try {
|
||||
await facturapiService.sendInvoiceByEmail(emitter.id, invoice.id, customer.email);
|
||||
console.log(`[Invoicing] Factura ${invoice.id} enviada a ${customer.email}`);
|
||||
} catch (emailErr: any) {
|
||||
console.error(`[Invoicing] Error enviando factura ${invoice.id} a ${customer.email}:`, emailErr.message || emailErr);
|
||||
}
|
||||
}
|
||||
|
||||
auditLog({
|
||||
tenantId: payment.tenantId,
|
||||
action: 'invoice.emitted_auto',
|
||||
|
||||
@@ -45,7 +45,10 @@ export async function getRegimenesActivosClaves(tenantId: string): Promise<strin
|
||||
/**
|
||||
* Resuelve las claves de regímenes activos para la alerta de discrepancia.
|
||||
* Si hay contribuyenteId, lee de contribuyentes.regimen_fiscal (comma-separated).
|
||||
* Si no, fallback a TenantRegimenActivo (tabla central).
|
||||
* Si no, combina TenantRegimenActivo (tabla central) con los regímenes de
|
||||
* todos los contribuyentes activos del tenant. Esto evita que la alerta
|
||||
* aparezca en el correo por-contribuyente pero desaparezca en el dashboard
|
||||
* cuando no hay un contribuyente seleccionado.
|
||||
*/
|
||||
export async function getRegimenesActivosClavesEfectivos(
|
||||
tenantId: string,
|
||||
@@ -61,9 +64,49 @@ export async function getRegimenesActivosClavesEfectivos(
|
||||
if (rows.length > 0 && rows[0].regimen_fiscal) {
|
||||
return rows[0].regimen_fiscal.split(',').map((c: string) => c.trim()).filter(Boolean);
|
||||
}
|
||||
return [];
|
||||
// Fallback: si el contribuyente no tiene regimen_fiscal, usamos los del tenant
|
||||
// para no perder la alerta si el campo quedó vacío accidentalmente.
|
||||
const tenantRegimenes = await getRegimenesActivosClaves(tenantId);
|
||||
if (tenantRegimenes.length > 0) return tenantRegimenes;
|
||||
|
||||
const { rows: allRows } = await pool.query(
|
||||
`SELECT DISTINCT regimen_fiscal FROM contribuyentes WHERE regimen_fiscal IS NOT NULL AND regimen_fiscal <> ''`,
|
||||
);
|
||||
const set = new Set<string>();
|
||||
for (const row of allRows) {
|
||||
if (row.regimen_fiscal) {
|
||||
for (const clave of row.regimen_fiscal.split(',')) {
|
||||
const trimmed = clave.trim();
|
||||
if (trimmed) set.add(trimmed);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Array.from(set);
|
||||
}
|
||||
return getRegimenesActivosClaves(tenantId);
|
||||
|
||||
const tenantRegimenes = await getRegimenesActivosClaves(tenantId);
|
||||
|
||||
// Fallback: si no hay regímenes configurados a nivel tenant, usamos los
|
||||
// regímenes de todos los contribuyentes activos del tenant.
|
||||
if (tenantRegimenes.length > 0) {
|
||||
return tenantRegimenes;
|
||||
}
|
||||
|
||||
const { rows } = await pool.query(
|
||||
`SELECT DISTINCT regimen_fiscal FROM contribuyentes WHERE regimen_fiscal IS NOT NULL AND regimen_fiscal <> ''`,
|
||||
);
|
||||
|
||||
const set = new Set<string>();
|
||||
for (const row of rows) {
|
||||
if (row.regimen_fiscal) {
|
||||
for (const clave of row.regimen_fiscal.split(',')) {
|
||||
const trimmed = clave.trim();
|
||||
if (trimmed) set.add(trimmed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Array.from(set);
|
||||
}
|
||||
|
||||
export async function setRegimenesActivos(tenantId: string, regimenIds: number[]) {
|
||||
|
||||
@@ -72,9 +72,17 @@ export async function querySat(
|
||||
requestType: 'metadata' | 'cfdi' = 'cfdi'
|
||||
): Promise<QueryResult> {
|
||||
try {
|
||||
// El SAT rechaza fechaInicial >= fechaFinal. Como formatDateForSat trunca
|
||||
// a medianoche, dos fechas dentro del mismo día calendario resultan iguales.
|
||||
// Ajustamos fechaFin al día siguiente para evitar el error.
|
||||
let adjustedFechaFin = fechaFin;
|
||||
if (formatDateForSat(fechaInicio) === formatDateForSat(fechaFin)) {
|
||||
adjustedFechaFin = new Date(fechaFin.getTime() + 24 * 60 * 60 * 1000);
|
||||
}
|
||||
|
||||
const period = DateTimePeriod.createFromValues(
|
||||
formatDateForSat(fechaInicio),
|
||||
formatDateForSat(fechaFin)
|
||||
formatDateForSat(adjustedFechaFin)
|
||||
);
|
||||
|
||||
const downloadType = new DownloadType(tipo === 'emitidos' ? 'issued' : 'received');
|
||||
@@ -239,10 +247,11 @@ export async function downloadSatPackage(
|
||||
}
|
||||
|
||||
/**
|
||||
* Formatea una fecha para el SAT (YYYY-MM-DD HH:mm:ss)
|
||||
* Formatea una fecha para el SAT (YYYY-MM-DD HH:mm:ss).
|
||||
* El SAT requiere hora 00:00:00; cualquier otra hora causa
|
||||
* "Fecha final invalida" / "Fecha inicial invalida".
|
||||
*/
|
||||
function formatDateForSat(date: Date): string {
|
||||
const pad = (n: number) => n.toString().padStart(2, '0');
|
||||
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ` +
|
||||
`${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
|
||||
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} 00:00:00`;
|
||||
}
|
||||
|
||||
@@ -30,20 +30,20 @@ export async function loginSatCsf(
|
||||
const publicPage = await context.newPage();
|
||||
publicPage.setDefaultTimeout(60_000);
|
||||
|
||||
await publicPage.goto(PUBLIC_URL, { waitUntil: 'networkidle' });
|
||||
await publicPage.waitForTimeout(2000);
|
||||
await publicPage.goto(PUBLIC_URL, { waitUntil: 'networkidle', timeout: 120_000 });
|
||||
await publicPage.waitForTimeout(3000);
|
||||
|
||||
// Click acordeón "Obtén tu constancia" / "Obtener constancia"
|
||||
const obtenerLocator = publicPage.locator(
|
||||
'text=/Obt[eé]n\\s+la\\s+constancia|Obt[eé]n\\s+tu\\s+constancia|Obtener\\s+constancia|Obtener\\s+la\\s+constancia/i',
|
||||
).first();
|
||||
await obtenerLocator.waitFor({ state: 'visible', timeout: 60_000 });
|
||||
await obtenerLocator.waitFor({ state: 'visible', timeout: 120_000 });
|
||||
await obtenerLocator.scrollIntoViewIfNeeded();
|
||||
await obtenerLocator.click();
|
||||
await publicPage.waitForTimeout(1500);
|
||||
|
||||
// Click "SERVICIO" → popup
|
||||
const popupPromise = context.waitForEvent('page', { timeout: 60_000 });
|
||||
const popupPromise = context.waitForEvent('page', { timeout: 120_000 });
|
||||
await publicPage.locator('text=/^\\s*SERVICIO\\s*$/i').first().click();
|
||||
const loginPage = await popupPromise;
|
||||
await loginPage.waitForLoadState('domcontentloaded');
|
||||
@@ -56,7 +56,7 @@ export async function loginSatCsf(
|
||||
const efirmaBtn = loginPage
|
||||
.locator('button:has-text("e.firma"):not(:has-text("portable")), input[type="button"][value="e.firma" i], input[type="submit"][value="e.firma" i]')
|
||||
.first();
|
||||
await efirmaBtn.waitFor({ state: 'visible', timeout: 30_000 });
|
||||
await efirmaBtn.waitFor({ state: 'visible', timeout: 60_000 });
|
||||
await efirmaBtn.scrollIntoViewIfNeeded();
|
||||
await efirmaBtn.click();
|
||||
|
||||
@@ -82,7 +82,7 @@ export async function loginSatCsf(
|
||||
return rfc !== null && rfc.value.length >= 12;
|
||||
},
|
||||
null,
|
||||
{ timeout: 60_000 },
|
||||
{ timeout: 120_000 },
|
||||
);
|
||||
rfcPopulated = true;
|
||||
} catch {
|
||||
@@ -121,7 +121,7 @@ export async function loginSatCsf(
|
||||
// Esperar a que salga del dominio de login y aterrice en el portal SAT
|
||||
await loginPage.waitForURL(
|
||||
url => url.toString().includes('wwwmat.sat.gob.mx/operacion/'),
|
||||
{ timeout: 60_000 },
|
||||
{ timeout: 120_000 },
|
||||
);
|
||||
await loginPage.waitForLoadState('networkidle').catch(() => undefined);
|
||||
await loginPage.waitForTimeout(2000);
|
||||
|
||||
@@ -14,10 +14,10 @@ export interface SweepResult {
|
||||
}
|
||||
|
||||
const DEFAULT_RUNNING_HOURS_BY_TYPE: Record<string, number> = {
|
||||
initial: 8,
|
||||
initial: 24,
|
||||
daily: 4,
|
||||
incremental: 2,
|
||||
custom: 4,
|
||||
custom: 24,
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,6 +33,16 @@ export function ContribuyenteSelector() {
|
||||
}
|
||||
}, [contribuyentes, selectedContribuyenteId, setSelectedContribuyente]);
|
||||
|
||||
// Clear invalid selection (e.g. stale localStorage from another tenant/session)
|
||||
useEffect(() => {
|
||||
if (contribuyentes && contribuyentes.length > 0 && selectedContribuyenteId) {
|
||||
const exists = contribuyentes.some(c => c.id === selectedContribuyenteId);
|
||||
if (!exists) {
|
||||
clearSelectedContribuyente();
|
||||
}
|
||||
}
|
||||
}, [contribuyentes, selectedContribuyenteId, clearSelectedContribuyente]);
|
||||
|
||||
if (isLoading || !contribuyentes || contribuyentes.length === 0) return null;
|
||||
if (pathname && HIDDEN_PATHS.some(p => pathname === p || pathname.startsWith(`${p}/`))) return null;
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ const navigation: NavItem[] = [
|
||||
{ name: 'Documentos', href: '/documentos', icon: FileCheck, feature: 'documentos' },
|
||||
{ name: 'Carteras', href: '/carteras', icon: ClipboardList, roles: ['supervisor', 'auxiliar'] },
|
||||
{ name: 'Contribuyentes', href: '/contribuyentes', icon: Building2, roles: ['owner', 'cfo', 'supervisor', 'contador', 'auxiliar'] },
|
||||
{ name: 'Usuarios', href: '/usuarios', icon: Users, roles: ['owner', 'cfo'] },
|
||||
{ name: 'Usuarios', href: '/usuarios', icon: Users, roles: ['owner', 'cfo', 'supervisor', 'auxiliar'] },
|
||||
{ name: 'Tareas', href: '/tareas', icon: CheckSquare2, roles: ['owner', 'cfo', 'contador', 'auxiliar', 'supervisor'] },
|
||||
{ name: 'Planes', href: '/configuracion/planes-despacho', icon: CreditCard, roles: ['owner', 'cfo'] },
|
||||
{ name: 'Configuracion', href: '/configuracion', icon: Settings, roles: ['owner', 'cfo', 'supervisor', 'auxiliar', 'cliente'] },
|
||||
|
||||
Reference in New Issue
Block a user