Files
portal/app/resources/js/views/admin/AdminImpersonationView.vue
T
Дмитрий 768628d914 phase2(7-features): bulk-actions / new-deal / tenant-card / system-edit / webhook / smart-filters / impersonation-list
7-фичный auto-mode пакет согласно «карте что осталось» (после v1.54).

(1) Bulk-actions DealsView:
- dealsState reactive-копия MOCK_DEALS (deep-clone) для безопасного bulk-edit.
- Bulk-bar (sticky, теало-нуар, theme=dark) при selected.length > 0:
  count + Сменить статус (v-menu × 14 lead_statuses) + Экспорт (snackbar) +
  Удалить (v-dialog confirm) + ✕ clear.
- На production: smart status-transition с проверкой allowed-переходов;
  soft-delete (архив 30 дней); реальный CSV/XLSX export через xlsx-lib.

(2) NewDealDialog (used in DealsView+KanbanView):
- 6 полей: name/phone/project (MOCK_PROJECTS) / manager (MOCK_MANAGERS) /
  cost / status (default 'new' или presetStatus). Phone-валидация ≥10 цифр.
- emit('created', deal) → DealsView push в начало dealsState; KanbanView push
  в правильную колонку по statusSlug + totalDeals++.

(3) AdminTenantDetailView (/admin/tenants/:code):
- 4 KPI cards (Баланс/runway / Тариф+MRR/мес / Лиды сегодня+неделя+месяц /
  Средняя цена). 4 v-tabs: Финансы (balance-history) / Пользователи /
  Проекты / Активность с event-кодами.
- Кнопка «Войти как клиент» (использует ImpersonationDialog из v1.54).
  404-fallback. composables/mockTenantDetail.ts с expandTenantDetail.
- AdminTenantsView получил @click:row → router.push.

(4) Edit-flow AdminSystemView (audit-log + 2-step):
- Backend: SystemSetting + SaasAdminAuditLog Eloquent (append-only,
  payload_before/after JSONB casts).
- AdminSystemSettingsController с GET (list) + PUT (update в DB::transaction
  + INSERT в saas_admin_audit_log; hash-chain trigger BEFORE INSERT
  заполняет log_hash).
- Type-validation: int/decimal/bool/json. Reason ≥30 chars. No-op → 422.
- Frontend SystemSettingEditDialog — 3-step (edit → confirm с diff
  before/after → done).

(5) Webhook receive endpoint (POST /api/webhook/{token}):
- WebhookReceiveController::receive. Token = tenants.webhook_token.
- 404 unknown / 422 bad payload / 202 success + dispatch ProcessWebhookJob.
- Stub-INSERT в webhook_log через DB::table обёрнут в DB::transaction +
  SET LOCAL app.current_tenant_id для RLS.
- CSRF-исключение для api/webhook/* в bootstrap/app.php.
- На prod: + HMAC X-Webhook-Signature + per-token rate-limit.

(6) Smart-filters:
- DealsView: multi-select v-select Проект+Менеджер с auto availableProjects/
  availableManagers computed.
- AdminTenantsView: filterStatuses (4 STATUS_OPTIONS) + filterTariffs
  (computed availableTariffs).
- Кнопка «Сбросить» появляется только когда фильтры активны.

(7) AdminImpersonationView (/admin/impersonation):
- Backend +2 GET endpoints: /active (used_at != null AND session_ended_at
  == null) + /recent (last 20 завершённых с duration_seconds через
  abs(diffInSeconds) — Carbon signed по умолчанию).
- ImpersonationToken получил belongsTo(Tenant).
- Frontend view: 2 секции (Активные с end-кнопкой / Недавно завершённые
  read-only) + refresh + onMounted load.
- Маршрут /admin/impersonation + 5-й nav-пункт «Impersonation» в AdminLayout.

Vitest +48 (всего 238/238 за 15.31 сек).
Pest +16 (всего 136/136 за 15.8 сек, 495 assertions).
PHPStan baseline регенерирован (0 errors после фикса nullsafe.neverNull).

Регресс: lint+type-check+format ; vite build 937 ms; Pint+PHPStan passed;
Pest 136/136. Реестр v1.54→v1.55, CLAUDE.md v1.45→v1.46.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-09 05:33:21 +03:00

237 lines
8.5 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
/**
* Админка SaaS → Impersonation: активные и недавно завершённые сессии.
*
* 2 секции:
* - «Активные» (used_at != null AND session_ended_at == null) — каждую можно
* завершить кнопкой «Завершить» (POST /api/admin/impersonation/end).
* - «Последние 20 завершённых» — read-only лог (для аудита).
*
* На MVP без auth-middleware (см. routes/web.php). Production: middleware
* 'auth:saas-admin' + role super_admin.
*/
import * as adminApi from '../../api/admin';
import { extractErrorMessage } from '../../api/client';
import { onMounted, ref } from 'vue';
const active = ref<adminApi.ImpersonationActiveSession[]>([]);
const recent = ref<adminApi.ImpersonationRecentSession[]>([]);
const loadingActive = ref(false);
const loadingRecent = ref(false);
const errorMessage = ref<string | null>(null);
const endingTokenId = ref<number | null>(null);
async function loadActive() {
loadingActive.value = true;
try {
active.value = await adminApi.impersonationActive();
} catch (err) {
errorMessage.value = extractErrorMessage(err, 'Не удалось загрузить активные сессии.');
} finally {
loadingActive.value = false;
}
}
async function loadRecent() {
loadingRecent.value = true;
try {
recent.value = await adminApi.impersonationRecent();
} catch (err) {
errorMessage.value = extractErrorMessage(err, 'Не удалось загрузить недавние сессии.');
} finally {
loadingRecent.value = false;
}
}
async function endSession(tokenId: number) {
endingTokenId.value = tokenId;
try {
await adminApi.impersonationEnd(tokenId);
// Перегружаем оба списка — завершённый перейдёт из active в recent.
await Promise.all([loadActive(), loadRecent()]);
} catch (err) {
errorMessage.value = extractErrorMessage(err, 'Не удалось завершить сессию.');
} finally {
endingTokenId.value = null;
}
}
function formatDateTime(iso: string): string {
return new Date(iso).toLocaleString('ru-RU', { dateStyle: 'short', timeStyle: 'short' });
}
function formatDuration(s: number | null): string {
if (s === null) return '—';
if (s < 60) return `${s} сек`;
if (s < 3600) return `${Math.floor(s / 60)} мин ${s % 60} сек`;
return `${Math.floor(s / 3600)} ч ${Math.floor((s % 3600) / 60)} мин`;
}
function expiresIn(iso: string): string {
const ms = new Date(iso).getTime() - Date.now();
if (ms <= 0) return 'истёк';
const min = Math.floor(ms / 60000);
if (min < 60) return `через ${min} мин`;
return `через ${Math.floor(min / 60)} ч`;
}
onMounted(() => {
loadActive();
loadRecent();
});
defineExpose({ active, recent, loadActive, loadRecent, endSession });
</script>
<template>
<v-container fluid class="admin-impersonation pa-6">
<header class="page-head mb-4">
<div>
<h1 class="text-h4 page-title">Impersonation</h1>
<p class="text-body-2 text-medium-emphasis ma-0">Активные сессии «вход как клиент» (Ю-1 / ТЗ §22.7).</p>
</div>
<v-btn
variant="outlined"
size="small"
prepend-icon="mdi-refresh"
:loading="loadingActive"
data-testid="refresh-btn"
@click="
loadActive();
loadRecent();
"
>
Обновить
</v-btn>
</header>
<v-alert
v-if="errorMessage"
type="error"
variant="tonal"
density="compact"
class="mb-4"
data-testid="error-alert"
>
{{ errorMessage }}
</v-alert>
<!-- ACTIVE -->
<v-card variant="outlined" class="pa-4 mb-4" data-testid="active-section">
<h2 class="text-h6 mb-3">Активные ({{ active.length }})</h2>
<div v-if="loadingActive" class="text-center py-4">
<v-progress-circular indeterminate color="primary" />
</div>
<div
v-else-if="active.length === 0"
class="text-medium-emphasis text-center py-4"
data-testid="active-empty"
>
Нет активных impersonation-сессий.
</div>
<v-table v-else density="comfortable">
<thead>
<tr>
<th>Тенант</th>
<th>Admin ID</th>
<th>Email клиента</th>
<th>Основание</th>
<th>Активна с</th>
<th>TTL</th>
<th class="text-end">Действие</th>
</tr>
</thead>
<tbody>
<tr v-for="s in active" :key="s.token_id" data-testid="active-row">
<td>{{ s.tenant_name ?? `#${s.tenant_id}` }}</td>
<td class="num">{{ s.requested_by }}</td>
<td class="text-caption">{{ s.sent_to_email }}</td>
<td class="reason-cell">{{ s.reason }}</td>
<td class="num text-medium-emphasis">{{ formatDateTime(s.used_at) }}</td>
<td class="num text-medium-emphasis">{{ expiresIn(s.expires_at) }}</td>
<td class="text-end">
<v-btn
size="small"
color="error"
variant="tonal"
prepend-icon="mdi-stop-circle-outline"
:loading="endingTokenId === s.token_id"
:data-testid="`end-btn-${s.token_id}`"
@click="endSession(s.token_id)"
>
Завершить
</v-btn>
</td>
</tr>
</tbody>
</v-table>
</v-card>
<!-- RECENT -->
<v-card variant="outlined" class="pa-4" data-testid="recent-section">
<h2 class="text-h6 mb-3">Недавно завершённые ({{ recent.length }})</h2>
<div v-if="loadingRecent" class="text-center py-4">
<v-progress-circular indeterminate color="primary" />
</div>
<div
v-else-if="recent.length === 0"
class="text-medium-emphasis text-center py-4"
data-testid="recent-empty"
>
История impersonation-сессий пуста.
</div>
<v-table v-else density="comfortable">
<thead>
<tr>
<th>Тенант</th>
<th>Admin</th>
<th>Основание</th>
<th>Длилась</th>
<th>Завершена</th>
</tr>
</thead>
<tbody>
<tr v-for="s in recent" :key="s.token_id" data-testid="recent-row">
<td>{{ s.tenant_name ?? `#${s.tenant_id}` }}</td>
<td class="num">{{ s.requested_by }}</td>
<td class="reason-cell">{{ s.reason }}</td>
<td class="num">{{ formatDuration(s.duration_seconds) }}</td>
<td class="num text-medium-emphasis">{{ formatDateTime(s.session_ended_at) }}</td>
</tr>
</tbody>
</v-table>
</v-card>
</v-container>
</template>
<style scoped>
.admin-impersonation {
max-width: 1440px;
}
.page-head {
display: flex;
justify-content: space-between;
align-items: flex-start;
flex-wrap: wrap;
gap: 12px;
}
.page-title {
font-variation-settings: 'opsz' 28;
letter-spacing: -0.018em;
}
.num {
font-family: 'JetBrains Mono', ui-monospace, monospace;
font-feature-settings: 'tnum';
font-weight: 500;
}
.reason-cell {
max-width: 320px;
white-space: normal;
line-height: 1.4;
color: #081319;
font-size: 13px;
}
</style>