0; } public static function listPaged(array $filters): array { $search = trim((string) ($filters['search'] ?? '')); $profileKey = strtolower(trim((string) ($filters['profile_key'] ?? ''))); $status = strtolower(trim((string) ($filters['status'] ?? ''))); $createdFrom = trim((string) ($filters['created_from'] ?? '')); $createdTo = trim((string) ($filters['created_to'] ?? '')); $userId = (int) ($filters['user_id'] ?? 0); [$limit, $offset] = RepoQuery::sanitizeLimitOffset($filters, 20, 1, 200, 0); [$order, $dir] = RepoQuery::sanitizeOrder( $filters, [ 'id', 'started_at', 'finished_at', 'duration_ms', 'rows_total', 'created_count', 'skipped_count', 'failed_count', 'status', 'profile_key', ], 'started_at', 'desc' ); $where = []; $params = []; RepoQuery::addLikeFilter( $where, $params, [ 'import_audit_runs.run_uuid', 'import_audit_runs.source_filename', 'import_audit_runs.error_codes_json', ], $search ); if (in_array($profileKey, ['users', 'departments'], true)) { $where[] = 'import_audit_runs.profile_key = ?'; $params[] = $profileKey; } if (in_array($status, ['running', 'success', 'partial', 'failed'], true)) { $where[] = 'import_audit_runs.status = ?'; $params[] = $status; } if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $createdFrom)) { $where[] = 'import_audit_runs.started_at >= ?'; $params[] = $createdFrom . ' 00:00:00'; } if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $createdTo)) { $where[] = 'import_audit_runs.started_at <= ?'; $params[] = $createdTo . ' 23:59:59'; } if ($userId > 0) { $where[] = 'import_audit_runs.user_id = ?'; $params[] = (string) $userId; } $whereSql = $where ? (' where ' . implode(' and ', $where)) : ''; $fromSql = ' from import_audit_runs ' . 'left join users on users.id = import_audit_runs.user_id ' . 'left join tenants on tenants.id = import_audit_runs.current_tenant_id '; $total = (int) (DB::selectValue('select count(*)' . $fromSql . $whereSql, ...$params) ?? 0); $rows = DB::select( 'select import_audit_runs.id, import_audit_runs.run_uuid, import_audit_runs.profile_key, import_audit_runs.status, import_audit_runs.source_filename, import_audit_runs.mapped_targets_csv, import_audit_runs.rows_total, import_audit_runs.created_count, import_audit_runs.skipped_count, import_audit_runs.failed_count, import_audit_runs.error_codes_json, import_audit_runs.started_at, import_audit_runs.finished_at, import_audit_runs.duration_ms, import_audit_runs.user_id, import_audit_runs.current_tenant_id, users.id, users.uuid, users.display_name, users.email, tenants.id, tenants.uuid, tenants.description ' . $fromSql . $whereSql . sprintf(' order by import_audit_runs.`%s` %s limit ? offset ?', $order, $dir), ...array_merge($params, [(string) $limit, (string) $offset]) ); $normalized = []; if (is_array($rows)) { foreach ($rows as $row) { $item = self::normalizeRow($row); if ($item !== null) { $normalized[] = $item; } } } return [ 'total' => $total, 'rows' => $normalized, ]; } public static function find(int $id): ?array { if ($id <= 0) { return null; } $row = DB::selectOne( 'select import_audit_runs.id, import_audit_runs.run_uuid, import_audit_runs.profile_key, import_audit_runs.status, import_audit_runs.source_filename, import_audit_runs.mapped_targets_csv, import_audit_runs.rows_total, import_audit_runs.created_count, import_audit_runs.skipped_count, import_audit_runs.failed_count, import_audit_runs.error_codes_json, import_audit_runs.started_at, import_audit_runs.finished_at, import_audit_runs.duration_ms, import_audit_runs.user_id, import_audit_runs.current_tenant_id, users.id, users.uuid, users.display_name, users.email, tenants.id, tenants.uuid, tenants.description from import_audit_runs left join users on users.id = import_audit_runs.user_id left join tenants on tenants.id = import_audit_runs.current_tenant_id where import_audit_runs.id = ? limit 1', (string) $id ); return self::normalizeRow($row); } public static function purgeOlderThanDays(int $days): int { if ($days <= 0) { return 0; } $cutoff = (new \DateTimeImmutable('now', new \DateTimeZone('UTC'))) ->modify('-' . $days . ' days') ->format('Y-m-d H:i:s'); $deleted = DB::delete('delete from import_audit_runs where started_at < ?', $cutoff); return is_int($deleted) ? $deleted : 0; } private static function normalizeRow(mixed $row): ?array { if (!is_array($row)) { return null; } $item = $row['import_audit_runs'] ?? []; if (!is_array($item) || !isset($item['id'])) { return null; } $user = is_array($row['users'] ?? null) ? $row['users'] : []; $tenant = is_array($row['tenants'] ?? null) ? $row['tenants'] : []; $item['user_uuid'] = (string) ($user['uuid'] ?? ''); $item['user_display_name'] = (string) ($user['display_name'] ?? ''); $item['user_email'] = (string) ($user['email'] ?? ''); $item['current_tenant_uuid'] = (string) ($tenant['uuid'] ?? ''); $item['current_tenant_description'] = (string) ($tenant['description'] ?? ''); return $item; } }