Compare commits
83 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e6beff6aeb | |||
| 6933ddc538 | |||
| 2a34ee880a | |||
| 1dc696cef6 | |||
| b29bfe2ac6 | |||
| 3fc5501dc5 | |||
| 55684e80b2 | |||
| 1345ce2ddf | |||
| 3280aad059 | |||
| 4ccb06c900 | |||
| a27b31efa6 | |||
| ca292d44a9 | |||
| 08d3ae35d8 | |||
| 2138270af0 | |||
| eef21ba04b | |||
| 05437ba79a | |||
| 1933129497 | |||
| 1bbedf2f95 | |||
| b35a8c4311 | |||
| 68f42ad385 | |||
| 83613b4509 | |||
| cf0be8ac0f | |||
| 5e3d20fa61 | |||
| 65722c76cb | |||
| 906ae4f587 | |||
| 20cc132777 | |||
| 4d7e9ca0e4 | |||
| 6174830311 | |||
| 3ef1e625eb | |||
| 2c28f1cb86 | |||
| 6dec34403f | |||
| 4f16cc3c83 | |||
| 45691d0324 | |||
| 8c350572df | |||
| 22e81cc896 | |||
| 3bbd7787d8 | |||
| 07d73870ba | |||
| 7408bc4232 | |||
| 9d68fc0ad6 | |||
| e2fb20ef05 | |||
| 5427cdc740 | |||
| f3250ce178 | |||
| 472ea8c75c | |||
| b053796182 | |||
| 3b6992d8e9 | |||
| 233f9984fc | |||
| 54b1de78b8 | |||
| ee5bc56f2d | |||
| df2d091174 | |||
| 4c9a1e9ccb | |||
| 65c2c5e471 | |||
| f6ba9bc1e7 | |||
| 05076c4f1d | |||
| f943b229c0 | |||
| 28671cb012 | |||
| d86d375ce4 | |||
| 4f5cf263f6 | |||
| af15f24de7 | |||
| b757f22b97 | |||
| 31b53557ac | |||
| be27713f6e | |||
| 60dd3e70b1 | |||
| 54967147d7 | |||
| 1a02b4b5f2 | |||
| 76ea9bbb04 | |||
| 62b5306548 | |||
| 01562afd31 | |||
| b7466ebfbd | |||
| 17e3c04f24 | |||
| ba49805689 | |||
| 95ee6644f7 | |||
| a0e18a1dd8 | |||
| 9e0490c328 | |||
| 80275c6417 | |||
| 36c71ecb1e | |||
| c99362a3e5 | |||
| 9331465c26 | |||
| 9d9bcf7847 | |||
| c7fd90c08d | |||
| e35fc6c938 | |||
| f1a3e9f02f | |||
| d0eecbbf79 | |||
| 01d292f5a9 |
@@ -38,5 +38,7 @@ See `references/aggregation-template.md`.
|
||||
|
||||
## Behavioral rule reminders
|
||||
|
||||
- **«Не использован ≠ проблема»** — when reporting node usage counts, NEVER mark unused nodes as «zombie» / «removal candidate». Cite `memory/feedback_brain_unused_tools_not_problem.md`.
|
||||
- **«Не использован ≠ проблема» (условное, Pravila §16.4 v1.36)** — when reporting node usage counts, distinguish two cases:
|
||||
1. **Unused + no profile task in episodes** → capability-readiness, do NOT flag.
|
||||
2. **Unused + profile task present (missed activation)** → mandatory section in the report. Cite `tools/observer-classification-map.json` for the classification→node mapping and `tools/.node-dormancy.json` for DEFERRED exclusions. NEVER mark unused-by-design nodes as «zombie» / «removal candidate».
|
||||
- **No auto-edit** — every regulatory suggestion is a candidate, not an action.
|
||||
|
||||
@@ -55,6 +55,32 @@ For each factor below, render a table: factor value × outcome counts
|
||||
|
||||
(one table each — same columns)
|
||||
|
||||
## Missed Activations (Pravila §16.4 v1.36)
|
||||
|
||||
Surface candidates where a profile-classified task ran with `node_chosen === 'direct'` and at least one non-dormant recommended node was available. The analyzer returns `missedActivations: { totalMissed, byNode, byClassification }` — render the two breakdowns below.
|
||||
|
||||
**Source:** `analyze(episodes, { classificationMap, dormancy }).missedActivations`.
|
||||
|
||||
### By node
|
||||
|
||||
| Node | Episodes missed | Classifications hit |
|
||||
|---|---|---|
|
||||
| #NN | N | refactor (a), bugfix (b) |
|
||||
|
||||
### By classification
|
||||
|
||||
| Classification | Missed episodes | Top recommended nodes (non-dormant) |
|
||||
|---|---|---|
|
||||
| refactor | N | #11, #12, #43 |
|
||||
|
||||
**Interpretation guide:**
|
||||
|
||||
- High count on one node → router-miss pattern. Suggest updating `tools/observer-classification-map.json` or a workflow nudge.
|
||||
- Spread across many nodes with classification leaning to `other` → the classification dictionary may need refinement (separate concern, not a missed activation).
|
||||
- All zero → either no profile work this period, or the router is operating cleanly.
|
||||
|
||||
**NOT to be auto-applied:** these are candidates for human review in retro, not commits or hook blocks.
|
||||
|
||||
## Episodes → tasks (from analyzer `tasks`)
|
||||
|
||||
| task_ref | episodes | turns that are rework |
|
||||
@@ -70,10 +96,14 @@ For each factor below, render a table: factor value × outcome counts
|
||||
- `observerErrorCount` from the analyzer — observer_error markers in the period.
|
||||
Non-zero = the observer failed silently somewhere; investigate.
|
||||
|
||||
## Canonical chains L1–L12 hit rate
|
||||
## Canonical chains L1–L13+ hit rate (from analyzer `factorMatrix.chain_ref`)
|
||||
|
||||
| chain | times | notes |
|
||||
|---|---|---|
|
||||
| chain | times | outcome split | notes |
|
||||
|---|---|---|---|
|
||||
|
||||
Each node may belong to several L (a multi-chain episode is counted in each).
|
||||
`null` = episodes outside any chain (`direct` + nodes not in L1–L13+) — **not a
|
||||
problem** per `memory/feedback_brain_unused_tools_not_problem`.
|
||||
|
||||
## Improvised chains (path_type=improvised, repeated ≥2)
|
||||
|
||||
@@ -109,4 +139,4 @@ For each factor below, render a table: factor value × outcome counts
|
||||
## Informational metrics (NOT alerts)
|
||||
|
||||
- Nodes used at least once this period: K / 60+
|
||||
- Nodes never used since beginning of observer logs: L / 60+ — **not a problem** per [feedback_brain_unused_tools_not_problem](../../../memory/feedback_brain_unused_tools_not_problem.md)
|
||||
- Nodes never used since beginning of observer logs: L / 67 — **not a problem if there was no profile task** per Pravila §16.4 v1.36 and [feedback_brain_unused_tools_not_problem](../../../memory/feedback_brain_unused_tools_not_problem.md). See `## Missed Activations` above for profile-task-present cases.
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
---
|
||||
name: laravel-backend-patterns
|
||||
description: Backend-конвенции Лидерры (Laravel 13) — как писать controller→service→job, RLS-aware Eloquent, деньги через bcmath/LedgerService, идемпотентные джобы, partition-aware запросы. Используй при «как писать backend в Лидерре», «паттерн контроллера/сервиса/джоба», scaffolding новой backend-фичи. НЕ для generic-паттернов (architecture-patterns #38), аудита денег (billing-audit #62), РСБУ/налогов (ru-tax-accounting), security-аудита (D3).
|
||||
---
|
||||
|
||||
# Laravel Backend Patterns — конвенции backend-кода Лидерры
|
||||
|
||||
Проектный скил, который описывает **как здесь пишут backend**, а не как рекомендует generic-Laravel.
|
||||
При scaffolding новой фичи или ревью кода — сверяться с пятью конвенциями ниже.
|
||||
Детальные примеры с образцами кода и антипаттернами — в `references/conventions.md`.
|
||||
|
||||
## 1. Слоистость: Controller → FormRequest → Service → Job
|
||||
|
||||
Контроллер тонкий: принимает FormRequest, делегирует Service, возвращает JSON-ответ.
|
||||
Бизнес-логика — в Service; асинхронная работа — в Job.
|
||||
Слои зафиксированы в `app/deptrac.yaml` (13 слоёв, pre-commit gate job 10).
|
||||
|
||||
Подробнее: `references/conventions.md` §1.
|
||||
|
||||
## 2. RLS-aware Eloquent и middleware `tenant`
|
||||
|
||||
Middleware `SetTenantContext` оборачивает HTTP-запрос в транзакцию и выполняет
|
||||
`SET LOCAL app.current_tenant_id = X`, обеспечивая RLS-изоляцию между tenant'ами.
|
||||
**КРИТИЧНО**: очередные джобы выполняются под ролью `crm_supplier_worker` (BYPASSRLS),
|
||||
поэтому RLS не фильтрует. Каждый запрос в джобе **обязан** содержать явный
|
||||
`where('tenant_id', $tenantId)` или устанавливать `SET LOCAL` вручную внутри транзакции.
|
||||
|
||||
Подробнее: `references/conventions.md` §2.
|
||||
|
||||
## 3. Деньги — только через bcmath и LedgerService
|
||||
|
||||
Все денежные операции — `bcadd` / `bcsub` / `bcmul` / `bcdiv` / `bccomp` со строковыми операндами
|
||||
и фиксированным `scale`. Никаких операторов `+` / `-` / `*` / `/` над деньгами, никакого `float`.
|
||||
Точка входа для биллингового списания — `LedgerService::chargeForDelivery()`.
|
||||
Аудит денежных инвариантов кода — скил `billing-audit` (#62); здесь — только конвенция написания.
|
||||
|
||||
Подробнее: `references/conventions.md` §3.
|
||||
|
||||
## 4. Идемпотентные джобы через advisory lock
|
||||
|
||||
Повторный запуск джоба не должен дублировать результат.
|
||||
Паттерн: `pg_advisory_xact_lock(composite_bigint)` внутри транзакции — сериализует
|
||||
конкурентные обработки одного (tenant_id, source_crm_id). Дополнительно: `lockForUpdate`
|
||||
на строку Tenant защищает баланс от TOCTOU при конкурентных списаниях.
|
||||
|
||||
Подробнее: `references/conventions.md` §4.
|
||||
|
||||
## 5. Partition-aware запросы для `deals` и `supplier_lead_costs`
|
||||
|
||||
Таблицы `deals` и `supplier_lead_costs` секционированы по `RANGE (received_at)`.
|
||||
Запросы к этим таблицам должны включать условие по `received_at` (или `created_at`
|
||||
для `supplier_lead_costs`) — это включает pruning и предотвращает full-scan всех партиций.
|
||||
|
||||
Подробнее: `references/conventions.md` §5.
|
||||
|
||||
## Связано
|
||||
|
||||
- `billing-audit` #62 — аудит денежной корректности (I1–I5 инварианты).
|
||||
- `architecture-patterns` #38 — общие паттерны архитектуры (не Лидерра-специфика).
|
||||
- Boost #10 — Eloquent introspection, документация Laravel 13.
|
||||
- Larastan #12 — статанализ PHP (ловит float-арифметику на деньгах).
|
||||
- ADR-005 — deptrac architecture-fitness gate.
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"skill": "laravel-backend-patterns",
|
||||
"cases": [
|
||||
{"prompt": "как написать контроллер для новой backend-фичи в Лидерре", "should_trigger": true},
|
||||
{"prompt": "как правильно списать деньги в джобе под crm_supplier_worker", "should_trigger": true},
|
||||
{"prompt": "проверь, не теряются ли копейки в списании", "should_trigger": false, "expected": "billing-audit"},
|
||||
{"prompt": "опиши Clean Architecture в общем", "should_trigger": false, "expected": "architecture-patterns"},
|
||||
{"prompt": "учёт выручки по РСБУ", "should_trigger": false, "expected": "ru-tax-accounting"}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
# Backend-конвенции Лидерры — детальный справочник
|
||||
|
||||
Образцы ниже — реальный код из `app/` (Laravel 13, PHP 8.3).
|
||||
Указаны конкретные `file:line` на момент 20.05.2026.
|
||||
|
||||
---
|
||||
|
||||
## §1. Слоистость: Controller → FormRequest → Service → Job
|
||||
|
||||
### Правило
|
||||
|
||||
Контроллер принимает FormRequest (валидация), делегирует Service (бизнес-логика),
|
||||
при необходимости Service dispatch'ит Job (асинхрон). Контроллер не содержит бизнес-логики.
|
||||
Слои задокументированы в `app/deptrac.yaml` — 13 слоёв:
|
||||
Controller, Request, Resource, Middleware, Service, Job, Console, Repository,
|
||||
Model, Mail, Rule, Exception, Provider.
|
||||
Допустимые направления зависимостей — только вниз по иерархии (deptrac gate, lefthook job 10).
|
||||
|
||||
### Образец из кода
|
||||
|
||||
`app/app/Http/Controllers/Api/ProjectController.php:87–90` — контроллер тонкий:
|
||||
|
||||
```php
|
||||
/** POST /api/projects */
|
||||
public function store(StoreProjectRequest $request): JsonResponse
|
||||
{
|
||||
$project = $this->projects->create($request->user()->tenant, $request->validated());
|
||||
|
||||
return response()->json(['data' => new ProjectResource($project)], 201);
|
||||
}
|
||||
```
|
||||
|
||||
`app/app/Http/Requests/StoreProjectRequest.php:18–44` — вся валидация в FormRequest:
|
||||
|
||||
```php
|
||||
public function rules(): array
|
||||
{
|
||||
$base = [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'signal_type' => ['required', Rule::in(['site', 'call', 'sms'])],
|
||||
'daily_limit_target' => ['required', 'integer', 'min:1', 'max:10000'],
|
||||
'regions' => ['present', 'array'],
|
||||
'regions.*' => ['integer', 'between:1,89'],
|
||||
'delivery_days_mask' => ['required', 'integer', 'min:1', 'max:127'],
|
||||
];
|
||||
// ... conditional rules by signal_type
|
||||
return $base;
|
||||
}
|
||||
```
|
||||
|
||||
`app/app/Services/Billing/LedgerService.php` — бизнес-логика в Service.
|
||||
`app/app/Jobs/ProcessWebhookJob.php` — асинхрон в Job.
|
||||
|
||||
### Антипаттерн
|
||||
|
||||
```php
|
||||
// ПЛОХО: бизнес-логика в контроллере
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$tier = PricingTier::where('min_leads', '<=', $count)->orderBy('min_leads', 'desc')->first();
|
||||
$price = $tier->price_per_lead_kopecks * $count; // float-арифметика + логика тира прямо здесь
|
||||
Deal::create([...]);
|
||||
return response()->json(['ok' => true]);
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## §2. RLS-aware Eloquent и middleware `tenant`
|
||||
|
||||
### Правило
|
||||
|
||||
Middleware `SetTenantContext` (`app/app/Http/Middleware/SetTenantContext.php`) оборачивает
|
||||
каждый HTTP-запрос в транзакцию и выполняет `SET LOCAL app.current_tenant_id = X`,
|
||||
после чего RLS-политики PostgreSQL автоматически фильтруют строки по tenant.
|
||||
|
||||
**КРИТИЧНО для джобов**: очередные джобы Laravel выполняются в отдельном процессе вне
|
||||
HTTP-стека. Роль `crm_supplier_worker` (connection `pgsql_supplier`) имеет атрибут
|
||||
BYPASSRLS — RLS-политики для неё **не применяются**. Любой запрос в таком джобе без
|
||||
явного `where('tenant_id', $tenantId)` вернёт строки всех tenant'ов.
|
||||
|
||||
Правило: в каждом джобе либо устанавливай `SET LOCAL` внутри транзакции (паттерн
|
||||
`ProcessWebhookJob`/`ImportLeadsJob`), либо добавляй явный `where('tenant_id', ...)`.
|
||||
|
||||
### Образец из кода
|
||||
|
||||
`app/app/Http/Middleware/SetTenantContext.php:36–43` — HTTP-путь:
|
||||
|
||||
```php
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
DB::statement('SET LOCAL app.current_tenant_id = ' . $tenantId);
|
||||
$response = $next($request);
|
||||
DB::commit();
|
||||
return $response;
|
||||
} catch (\Throwable $e) {
|
||||
DB::rollBack();
|
||||
throw $e;
|
||||
}
|
||||
```
|
||||
|
||||
`app/app/Jobs/ImportLeadsJob.php:92–96` — джоб устанавливает `SET LOCAL` вручную:
|
||||
|
||||
```php
|
||||
return DB::transaction(function (): ?ImportLog {
|
||||
DB::statement('SET LOCAL app.current_tenant_id = ' . $this->tenantId);
|
||||
return ImportLog::query()->find($this->importLogId);
|
||||
});
|
||||
```
|
||||
|
||||
`app/app/Jobs/ProcessWebhookJob.php:80–86` — аналогичный паттерн в webhook-джобе:
|
||||
|
||||
```php
|
||||
DB::transaction(function () use ($duplicateDetector): void {
|
||||
DB::statement('SET LOCAL app.current_tenant_id = ' . $this->tenantId);
|
||||
$tenant = Tenant::query()
|
||||
->whereKey($this->tenantId)
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
```
|
||||
|
||||
### Антипаттерн
|
||||
|
||||
```php
|
||||
// ПЛОХО: джоб под crm_supplier_worker без SET LOCAL и без where tenant_id
|
||||
// → вернёт все строки всех tenant'ов (BYPASSRLS не фильтрует)
|
||||
public function handle(): void
|
||||
{
|
||||
$logs = ImportLog::query()->where('status', 'pending')->get(); // ВСЕ tenant'ы!
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## §3. Деньги — только через bcmath и LedgerService
|
||||
|
||||
### Правило
|
||||
|
||||
Все арифметические операции с деньгами (рубли, копейки) — исключительно через
|
||||
функции `bcmath` с явным `scale`. Операнды передаются строками.
|
||||
Никаких PHP `float`, никакого `+` / `-` / `*` / `/` над денежными значениями.
|
||||
|
||||
Точка входа для списания за лид — `LedgerService::chargeForDelivery()`.
|
||||
Этот метод реализует dual-balance flow (prepaid-лиды → `balance_leads`, рубли → `balance_rub`).
|
||||
Вызывается **внутри открытой транзакции** с `lockForUpdate(Tenant)` — см. §4.
|
||||
|
||||
Аудит денежных инвариантов (I1–I5) — скил `billing-audit` (#62). Здесь — конвенция написания.
|
||||
|
||||
### Образец из кода
|
||||
|
||||
`app/app/Services/Billing/LedgerService.php:64–65` — конвертация копеек в рубли:
|
||||
|
||||
```php
|
||||
$amountRub = bcdiv((string) $priceKopecks, '100', 2);
|
||||
$newBalanceRub = bcsub((string) $lockedTenant->balance_rub, $amountRub, 2);
|
||||
```
|
||||
|
||||
`app/app/Services/Billing/LedgerService.php:124–125` — сравнение балансов:
|
||||
|
||||
```php
|
||||
$balanceKopecks = bcmul((string) $tenant->balance_rub, '100', 0);
|
||||
if (bccomp($balanceKopecks, (string) $priceKopecks, 0) >= 0) {
|
||||
return 'rub';
|
||||
}
|
||||
```
|
||||
|
||||
### Антипаттерн
|
||||
|
||||
```php
|
||||
// ПЛОХО: float-арифметика теряет копейки
|
||||
$price = $tier->price_per_lead_kopecks / 100; // float
|
||||
$newBalance = $tenant->balance_rub - $price; // потеря точности при накоплении
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## §4. Идемпотентные джобы через advisory lock
|
||||
|
||||
### Правило
|
||||
|
||||
Повторный запуск джоба (ретрай, краш, дубль cron) не должен создавать дублирующие
|
||||
записи. Паттерн: `pg_advisory_xact_lock(bigint)` внутри транзакции сериализует все
|
||||
конкурентные обработки одного (tenant_id, source_crm_id).
|
||||
|
||||
Дополнительно для мутаций баланса: `lockForUpdate` на строку Tenant — защита от
|
||||
TOCTOU (между чтением баланса и его обновлением другой воркер не должен изменить значение).
|
||||
|
||||
### Образец из кода
|
||||
|
||||
`app/app/Jobs/ProcessWebhookJob.php:293–296` — advisory lock перед upsert:
|
||||
|
||||
```php
|
||||
// pg_advisory_xact_lock(bigint): верхние 32 бита = tenant_id, нижние 32 = source_crm_id
|
||||
$lockKey = (($tenant->id & 0xFFFFFFFF) << 32) | ($sourceCrmId & 0xFFFFFFFF);
|
||||
DB::statement('SELECT pg_advisory_xact_lock(?)', [$lockKey]);
|
||||
```
|
||||
|
||||
`app/app/Services/Import/HistoricalImportService.php:145–147` — тот же паттерн в сервисе:
|
||||
|
||||
```php
|
||||
// advisory lock (tenant_id, source_crm_id) — сериализует upsert (§6.5)
|
||||
$lockKey = (($tenantId & 0xFFFFFFFF) << 32) | ($row->sourceCrmId & 0xFFFFFFFF);
|
||||
DB::statement('SELECT pg_advisory_xact_lock(?)', [$lockKey]);
|
||||
```
|
||||
|
||||
`app/app/Jobs/RouteSupplierLeadJob.php:210–213` — lockForUpdate на Tenant перед списанием:
|
||||
|
||||
```php
|
||||
$tenant = Tenant::query()
|
||||
->whereKey($project->tenant_id)
|
||||
->lockForUpdate()
|
||||
->firstOrFail();
|
||||
```
|
||||
|
||||
Для overlap-защиты долгоживущих джобов (cron) — `Cache::lock` (Redis):
|
||||
`app/app/Jobs/Supplier/CsvReconcileJob.php:69–74`:
|
||||
|
||||
```php
|
||||
$lock = $lockStore->lock(self::LOCK_NAME, self::LOCK_TTL_SECONDS);
|
||||
if (! $lock->get()) {
|
||||
Log::info('csv_reconcile.skipped_overlap');
|
||||
return;
|
||||
}
|
||||
```
|
||||
|
||||
### Антипаттерн
|
||||
|
||||
```php
|
||||
// ПЛОХО: нет lock — два конкурентных воркера создают два deal для одного vid
|
||||
$existing = Deal::where('source_crm_id', $vid)->where('tenant_id', $tenantId)->first();
|
||||
if (!$existing) {
|
||||
Deal::create([...]); // race condition: оба воркера видят null и оба создают
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## §5. Partition-aware запросы для `deals` и `supplier_lead_costs`
|
||||
|
||||
### Правило
|
||||
|
||||
Таблицы `deals` и `supplier_lead_costs` секционированы по `PARTITION BY RANGE (received_at)`.
|
||||
Запросы должны содержать условие по `received_at` (ключ партиционирования) — это позволяет
|
||||
PostgreSQL выполнять partition pruning и не сканировать все партиции.
|
||||
Запрос без `WHERE received_at ...` делает full-scan всех партиций.
|
||||
|
||||
### Образец из кода
|
||||
|
||||
`db/schema.sql:1658` — партиционирование `deals`:
|
||||
|
||||
```sql
|
||||
) PARTITION BY RANGE (received_at);
|
||||
```
|
||||
|
||||
`db/schema.sql:2361` — партиционирование `supplier_lead_costs`:
|
||||
|
||||
```sql
|
||||
) PARTITION BY RANGE (received_at);
|
||||
```
|
||||
|
||||
`app/app/Services/DuplicateDetector.php:49` — запрос к `deals` с ключом партиции:
|
||||
|
||||
```php
|
||||
->where('received_at', '>=', $windowStart)
|
||||
```
|
||||
|
||||
`app/app/Jobs/Supplier/CsvReconcileJob.php:113` — запрос к `supplier_leads` с ключом:
|
||||
|
||||
```php
|
||||
->where('received_at', '>=', $windowStart)
|
||||
```
|
||||
|
||||
### Антипаттерн
|
||||
|
||||
```php
|
||||
// ПЛОХО: запрос к deals без received_at — full-scan всех партиций
|
||||
$deals = Deal::where('tenant_id', $tenantId)
|
||||
->where('phone', $phone)
|
||||
->get(); // сканирует deals_2026_05, deals_2026_06, ... все партиции
|
||||
```
|
||||
@@ -0,0 +1,66 @@
|
||||
---
|
||||
name: pdn-152fz-audit
|
||||
description: Аудит защиты персональных данных Лидерры и соответствие 152-ФЗ. Режим 1 — техника (где лежат ПДн в схеме/коде, RLS, маскирование pg_anonymizer, утечки в логах/Sentry/CSV-экспортах, шифрование). Режим 2 — закон (хранение в РФ, согласия, сроки/удаление, реестр обработки, уведомление РКН, права субъекта pd_subject_request). Используй при «проверь ПДн», «утекают ли персональные данные», «соответствие 152-ФЗ», «где хранятся телефоны лидов», «маскируются ли данные в дампах». НЕ для денежной корректности (billing-audit), security-аудита кода (D3/Semgrep), юридического оформления договоров/политик (D2 право), generic-угроз (threat-model #72).
|
||||
---
|
||||
|
||||
# ПДн 152-ФЗ Аудит — защита персональных данных Лидерры
|
||||
|
||||
Проектный скил раздела A8 карты «Информационная безопасность». Проверяет
|
||||
**защиту персональных данных** и соответствие Федеральному закону №152-ФЗ
|
||||
«О персональных данных» для SaaS-портала, обрабатывающего телефоны лидов
|
||||
и данные клиентов-компаний перед выходом в продакшен.
|
||||
|
||||
## Когда использовать
|
||||
|
||||
- Вопрос «не утекают ли ПДн в логи / Sentry / CSV-экспорты?»
|
||||
- Проверка технической защиты ПДн перед запуском (RLS, маскирование, шифрование).
|
||||
- Оценка соответствия 152-ФЗ: хранение в РФ, согласия, права субъекта, реестр.
|
||||
- Ревью кода, затрагивающего `deals`, `users`, `pd_subject_requests`,
|
||||
`pd_processing_log`, `supplier_leads` или CSV-импорт/экспорт лидов.
|
||||
|
||||
## Два режима
|
||||
|
||||
### Режим 1 — Технический аудит ПДн
|
||||
|
||||
Проверяет, что персональные данные физически защищены в коде и схеме БД.
|
||||
|
||||
Вопросы:
|
||||
|
||||
- Какие таблицы/колонки содержат ПДн? Под RLS ли они?
|
||||
- Маскируются ли ПДн в дампах (pg_anonymizer)?
|
||||
- Не утекают ли phone/email/ФИО в Laravel-логи, Sentry, `activity_log.context`,
|
||||
`auth_log`, `supplier_leads.raw_payload`?
|
||||
- Зашифрованы ли чувствительные поля в покое (totp_secret)?
|
||||
- Защищены ли CSV-экспорты лидов (signed URL + аудит в `pd_processing_log`)?
|
||||
|
||||
**Запустить:** пройти по чек-листу `references/checklist.md` → Раздел 1.
|
||||
|
||||
### Режим 2 — Соответствие 152-ФЗ
|
||||
|
||||
Проверяет правовую и процессную сторону обработки ПДн.
|
||||
|
||||
Вопросы:
|
||||
|
||||
- Хранятся ли ПДн на территории РФ?
|
||||
- Зафиксированы ли согласия субъектов ПДн (`tenant_consents`)?
|
||||
- Есть ли механизм обращений субъектов (`pd_subject_requests` + дедлайн 30 дней)?
|
||||
- Ведётся ли журнал обработки ПДн (`pd_processing_log`)?
|
||||
- Уведомлен ли РКН? Есть ли реестр обработки?
|
||||
- Реализовано ли право на ограничение обработки (`processing_restricted`)?
|
||||
|
||||
**Запустить:** пройти по чек-листу `references/checklist.md` → Раздел 2.
|
||||
|
||||
## Границы
|
||||
|
||||
- ≠ `billing-audit` #62 — тот про *денежную корректность начислений*; pdn-152fz-audit про *персональные данные*.
|
||||
- ≠ D3 «audit-security» (#39/#40 Trail of Bits / Semgrep) — те про *security-уязвимости кода*; pdn-152fz-audit про *данные субъектов ПДн*.
|
||||
- ≠ D2 «Право / договоры» — там юридическое оформление (политика обработки, договор с оператором); pdn-152fz-audit про *технику и процедуры*.
|
||||
- ≠ `threat-model` #72 — тот про *моделирование угроз*; pdn-152fz-audit про *конкретные ПДн в конкретных таблицах*.
|
||||
|
||||
## Связано
|
||||
|
||||
- Reuse: Boost #10 (SQL-запросы к схеме), Semgrep #25 (статанализ кода на утечки),
|
||||
Sentry MCP #34 (проверка runtime-маскирования), pg_anonymizer #29 (дампы).
|
||||
- ADR-013 (infosec-tooling A8).
|
||||
- Нормативная основа: ФЗ-152 ст.18 (уведомление РКН), ст.21 ч.5 (ограничение
|
||||
обработки), ст.22 (реестр операторов), ст.14 (права субъекта).
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"skill": "pdn-152fz-audit",
|
||||
"cases": [
|
||||
{"prompt": "проверь, не утекают ли телефоны лидов в логи", "should_trigger": true},
|
||||
{"prompt": "соответствует ли портал 152-ФЗ перед запуском", "should_trigger": true},
|
||||
{"prompt": "проверь, не теряются ли копейки в списании", "should_trigger": false, "expected": "billing-audit"},
|
||||
{"prompt": "смоделируй угрозы при выходе портала в интернет", "should_trigger": false, "expected": "threat-model"},
|
||||
{"prompt": "составь договор обработки персональных данных", "should_trigger": false, "expected": "D2 право"}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
# ПДн 152-ФЗ — чек-лист аудита Лидерры
|
||||
|
||||
Основан на реальных артефактах проекта (db/schema.sql v8.26, 21.05.2026).
|
||||
|
||||
## Таблицы-носители ПДн (инвентарь)
|
||||
|
||||
| Таблица | ПДн-колонки | Тип субъекта |
|
||||
|---|---|---|
|
||||
| `deals` | `phone`, `phones` (JSONB), `contact_name`, `city` | лид (физлицо) |
|
||||
| `supplier_leads` | `phone`, `raw_payload` (JSONB — весь payload поставщика) | лид (физлицо) |
|
||||
| `users` | `email`, `first_name`, `last_name`, `phone`, `totp_secret` | пользователь-клиент |
|
||||
| `tenants` | `contact_email`, `organization_name` | организация-клиент |
|
||||
| `auth_log` | `email` (при login_failed для неизвестного пользователя) | пользователь |
|
||||
| `pd_subject_requests` | `subject_email`, `subject_phone`, `subject_full_name` | субъект ПДн |
|
||||
| `impersonation_tokens` | косвенно (связь user — admin) | пользователь |
|
||||
| `import_log` | `filename`, `file_path` (может содержать имя файла с ПДн) | лид (косвенно) |
|
||||
|
||||
---
|
||||
|
||||
## Раздел 1 — Технический аудит ПДн
|
||||
|
||||
### Т1. RLS на таблицах-носителях ПДн
|
||||
|
||||
- [ ] `deals` — `ENABLE ROW LEVEL SECURITY` ✅ (подтверждено schema.sql:2780).
|
||||
Проверить: `FORCE ROW LEVEL SECURITY` не выставлен (только у `lead_charges`
|
||||
— там сильнее). Убедиться, что `crm_app_user` не BYPASSRLS.
|
||||
- [ ] `users` — RLS включён (schema.sql:2778). Политика `tenant_isolation` по
|
||||
`tenant_id`. Проверить: нет прямого SELECT * без `SET LOCAL app.current_tenant_id`.
|
||||
- [ ] `supplier_leads` — **RLS не включён** (таблица SaaS-уровня, schema.sql:1948).
|
||||
Это осознанное решение. Проверить: доступ только из воркера
|
||||
(`crm_supplier_worker` BYPASSRLS) с явным `WHERE tenant_id`.
|
||||
- [ ] `pd_subject_requests` — **RLS не включён** намеренно (saas-уровневая,
|
||||
schema.sql:2483). Доступ только через `crm_admin_user` BYPASSRLS.
|
||||
Проверить: tenant-приложение к таблице не обращается.
|
||||
- [ ] `auth_log` — RLS включён (schema.sql:2810). Политика `tenant_isolation`.
|
||||
Проверить: поле `email` в строке `login_failed` — не утекает ли email
|
||||
несуществующего пользователя в посторонний тенант.
|
||||
- [ ] `import_log` — RLS включён (schema.sql:2790).
|
||||
|
||||
### Т2. Маскирование ПДн в дампах (pg_anonymizer #29)
|
||||
|
||||
- [ ] **Проверить вручную:** OPEN-И-24 (schema.sql:113) — «pg_anonymizer процедура,
|
||||
документация в Прил. И, без изменений схемы». Расширение ставится в фазе 3
|
||||
(db/CHANGELOG_schema.md:625). На момент аудита — **расширение может быть не
|
||||
установлено**. Выполнить: `psql -c "SELECT extname FROM pg_extension WHERE extname='anon';"`.
|
||||
- [ ] Если pg_anonymizer установлен: проверить наличие `SECURITY LABEL` /
|
||||
`anon.mask_column` на колонках `deals.phone`, `deals.contact_name`,
|
||||
`users.email`, `users.first_name`, `users.last_name`.
|
||||
- [ ] Если pg_anonymizer **не установлен**: дампы (`pg_dump`) содержат ПДн в открытом
|
||||
виде — критический риск перед продакшеном. Требуется: либо установить
|
||||
расширение и настроить маски, либо запретить дампы с ПДн вне зашифрованного
|
||||
хранилища.
|
||||
|
||||
### Т3. Утечки ПДн в логи и Sentry
|
||||
|
||||
- [ ] **Sentry PII-scrubbing** (OPEN-И-16, schema.sql:68): конфигурация в
|
||||
`app/config/sentry.php` (narrative §22 «Sentry PII-scrubbing»).
|
||||
Проверить: whitelist событий задан; regex-маска `phone`/`email`/`password`/
|
||||
`secret`/`token`/`api_key` включена. Тест: намеренно вызвать ошибку с
|
||||
телефоном в payload и проверить Sentry-событие.
|
||||
- [ ] **Laravel-логи (`storage/logs/`)**: нет ли `Log::info`/`Log::debug` с
|
||||
`$deal->phone`, `$lead->phone`, `request()->all()` в необработанном виде.
|
||||
Grep: `Log::` + `phone\|email\|contact_name` в `app/app/`.
|
||||
- [ ] **`activity_log.context`** (JSONB, schema.sql:1775): поле `context` журнала
|
||||
действий по сделкам. Проверить: не пишется ли туда `phone`/`contact_name`
|
||||
полностью (должны быть только ID и маскированные значения).
|
||||
- [ ] **`supplier_leads.raw_payload`** (JSONB, schema.sql:1966): хранит весь
|
||||
webhook-payload от поставщика, включая телефон. Это осознанное хранение
|
||||
(нужно для дебага/реконсайла). Проверить: доступ ограничен только
|
||||
`crm_supplier_worker` + `crm_admin_user`; не отдаётся в tenant API.
|
||||
- [ ] **`auth_log.email`** (schema.sql:1458): email попадает в лог при `login_failed`
|
||||
для неизвестного адреса. Проверить: колонка не индексируется publicly,
|
||||
доступна только под RLS tenant-политикой.
|
||||
|
||||
### Т4. Шифрование чувствительных полей в покое
|
||||
|
||||
- [ ] **`users.totp_secret`** (schema.sql:723): комментарий «ШИФРУЕТСЯ `Crypt::encrypt`».
|
||||
Проверить: в коде Laravel используется `Crypt::encrypt`/`decrypt`, не plain TEXT.
|
||||
Grep: `totp_secret` в моделях/сервисах — нет ли прямого assignment без encrypt.
|
||||
- [ ] **`tenants.webhook_token`** (schema.sql:628): хранится в открытом виде как
|
||||
уникальный токен. Допустимо (по дизайну — это API-ключ, не пароль), но
|
||||
проверить: не логируется ли при ротации (`webhook_token_rotated_at`).
|
||||
- [ ] **Encryption at rest (диск/облако)**: Yandex Cloud `ru-central1` — проверить,
|
||||
включено ли шифрование диска/объектного хранилища на уровне YC-консоли.
|
||||
Это вне кода, но обязательно для 152-ФЗ.
|
||||
|
||||
### Т5. CSV-экспорт лидов и signed URL
|
||||
|
||||
- [ ] **`report_jobs`** (schema.sql:2313): `file_path` = `s3://bucket/path/file.xlsx`.
|
||||
Триггер `trg_report_jobs_export_log` (schema.sql:3096) автоматически пишет
|
||||
запись в `pd_processing_log` при INSERT. Проверить: триггер активен в prod.
|
||||
SQL: `SELECT tgname, tgenabled FROM pg_trigger WHERE tgname = 'trg_report_jobs_export_log';`
|
||||
- [ ] **Signed URL TTL**: schema.sql:3182 — «доступ через signed URL TTL 1 ч».
|
||||
Проверить в коде: `Storage::temporaryUrl(...)` с `now()->addHour()`.
|
||||
Файлы экспорта не доступны без аутентификации.
|
||||
- [ ] **`report_jobs.expires_at`**: автоудаление файла. Проверить: есть ли
|
||||
scheduled command / cleanup job, удаляющий S3-файл и обнуляющий `file_path`
|
||||
после `expires_at`.
|
||||
|
||||
### Т6. CSV-импорт исторических лидов
|
||||
|
||||
- [ ] **`import_log.file_path`** (schema.sql:1544): путь к загруженному CSV-файлу с
|
||||
ПДн. Проверить: файл хранится во временном/приватном location, не в
|
||||
публично доступном URL; удаляется после обработки.
|
||||
- [ ] **Проверить вручную:** содержит ли исторический CSV телефоны лидов в открытом
|
||||
виде в `storage/`? Если да — нужен cleanup после импорта.
|
||||
|
||||
---
|
||||
|
||||
## Раздел 2 — Соответствие 152-ФЗ
|
||||
|
||||
### З1. Хранение ПДн на территории РФ (ст.18.1 152-ФЗ)
|
||||
|
||||
- [ ] Облако: Yandex Cloud, регион `ru-central1` (Москва) — **✅ РФ**.
|
||||
Подтверждено в CLAUDE.md §2.
|
||||
- [ ] S3-хранилище файлов экспорта (`report_jobs.file_path`): убедиться, что
|
||||
Yandex Object Storage используется (не AWS S3 / GCS). Проверить
|
||||
`app/config/filesystems.php`.
|
||||
- [ ] Self-hosted Sentry: Yandex Cloud `ru-central1` — ✅ РФ (CLAUDE.md §2).
|
||||
Проверить: Sentry не проксирует события в eu.sentry.io / sentry.io (US).
|
||||
- [ ] Unisender Go (email): **Проверить вручную** — уточнить у Unisender
|
||||
расположение серверов; письма с ПДн (email адреса) передаются провайдеру.
|
||||
|
||||
### З2. Согласия субъектов ПДн (ст.6, ст.9 152-ФЗ)
|
||||
|
||||
- [ ] **`tenant_consents`** (schema.sql:2430): таблица согласий. Проверить:
|
||||
при регистрации тенанта записывается `consent_type='pd_processing'` с
|
||||
`document_version`, `ip_address`, `user_agent`, `given_at`.
|
||||
- [ ] Проверить: согласие на обработку ПДн лидов (телефоны физлиц) — не пользователя-
|
||||
клиента, а лидов. Лиды приходят от поставщика (crm.bp-gr.ru) — проверить
|
||||
договор с поставщиком (правовое основание обработки ст.6 ч.1 п.5 или п.4).
|
||||
**Проверить вручную** — вне schema (юридический документ).
|
||||
- [ ] `consent_type` значения: `pd_processing`, `marketing`, `oferta_v1` — убедиться,
|
||||
что consent_type='pd_processing' обязателен при регистрации (нет bypass).
|
||||
|
||||
### З3. Сроки хранения и удаление (ст.21 152-ФЗ)
|
||||
|
||||
- [ ] **Soft-delete в `deals`** (schema.sql:1648 `deleted_at`): после soft-delete
|
||||
данные остаются. Проверить: есть ли политика retention (hard-delete или
|
||||
анонимизация `phone`/`contact_name` через N дней после `deleted_at`).
|
||||
**Проверить вручную:** scheduled command для hard-delete сделок.
|
||||
- [ ] **`users.deleted_at`** (schema.sql:751): комментарий «soft delete + анонимизация».
|
||||
Проверить в коде: при soft-delete пользователя анонимизируются ли
|
||||
`email`/`first_name`/`last_name`/`phone`? Grep: `UserObserver` / `UserService`
|
||||
метод delete/anonymize.
|
||||
- [ ] **Право на удаление** (ст.21): обращение типа `request_type='deletion'` в
|
||||
`pd_subject_requests`. Проверить: есть ли процедура исполнения (скрипт/ручной
|
||||
процесс) удаления ПДн конкретного субъекта по `subject_phone`/`subject_email`
|
||||
из `deals`, `supplier_leads`, `activity_log`.
|
||||
|
||||
### З4. Журнал обработки ПДн (ст.18.1 152-ФЗ)
|
||||
|
||||
- [ ] **`pd_processing_log`** (schema.sql:2449): таблица журнала. RLS включён
|
||||
(schema.sql:2806), политика `tenant_isolation` (schema.sql:2846).
|
||||
Проверить: `subject_type`, `action`, `purpose` заполняются при
|
||||
ключевых операциях (просмотр сделки, экспорт, удаление).
|
||||
- [ ] **Триггер экспорта** `trg_report_jobs_export_log` (schema.sql:3096): AFTER
|
||||
INSERT на `report_jobs` → INSERT `pd_processing_log` с `action='exported'`.
|
||||
Закрывает требование ст.18 (учёт трансграничной передачи / выгрузки).
|
||||
- [ ] **Append-only hash chain** (schema.sql:63): `log_hash BYTEA` + триггеры
|
||||
`BEFORE UPDATE/DELETE` с `RAISE EXCEPTION`. Проверить: цепочка целостна.
|
||||
SQL: `SELECT id, log_hash IS NULL AS broken FROM pd_processing_log ORDER BY id DESC LIMIT 10;`
|
||||
|
||||
### З5. Обращения субъектов ПДн (ст.14 152-ФЗ)
|
||||
|
||||
- [ ] **`pd_subject_requests`** (schema.sql:2491): таблица обращений. Поля:
|
||||
`subject_email`, `subject_phone`, `subject_full_name`, `request_type`
|
||||
(`access`/`rectification`/`deletion`/`objection`), `deadline_at` (30 дней),
|
||||
`processing_restricted`.
|
||||
- [ ] **Триггер дедлайна** `trg_pd_subject_requests_deadline` (schema.sql:3165):
|
||||
функция `set_pd_subject_request_deadline()` заполняет `deadline_at =
|
||||
received_at + INTERVAL '30 days'` при INSERT/UPDATE.
|
||||
Проверить: `SELECT COUNT(*) FROM pd_subject_requests WHERE deadline_at IS NULL;`
|
||||
— должно быть 0.
|
||||
- [ ] **`processing_restricted`** (schema.sql:2514, ст.21 ч.5): при `TRUE`
|
||||
`ProcessingRestrictedException` блокирует операции с ПДн субъекта.
|
||||
Проверить в коде: `ProcessingRestrictionGuard` вызывается в сервисах
|
||||
перед mutable-операциями с `deals`/`users`.
|
||||
- [ ] Индекс (schema.sql:2519): `idx_pd_requests_restricted` — эффективный поиск
|
||||
активных ограничений. Проверить: он используется в `ProcessingRestrictionGuard`.
|
||||
|
||||
### З6. Уведомление РКН и реестр обработки (ст.22 152-ФЗ)
|
||||
|
||||
- [ ] **Проверить вручную:** подана ли заявка оператора в реестр Роскомнадзора
|
||||
на сайте pd.rkn.gov.ru? Это организационная мера, вне кода.
|
||||
- [ ] **Проверить вручную:** составлен ли внутренний реестр обработки ПДн
|
||||
(перечень категорий субъектов, целей, сроков, мер защиты)?
|
||||
Требование ст.22.1 ФЗ-152.
|
||||
- [ ] **`incidents_log`** (schema.sql:2535): при утечке ПДн — поле
|
||||
`related_pd_subject_request_ids BIGINT[]`. Проверить: есть ли внутренняя
|
||||
процедура уведомления РКН в течение 24 ч (ст.21.1, с 01.03.2023)?
|
||||
|
||||
### З7. Передача ПДн третьим лицам
|
||||
|
||||
- [ ] **Поставщик crm.bp-gr.ru**: получает запросы с телефонами лидов обратно
|
||||
при синхронизации статусов (`supplier_sync_log`). Проверить наличие договора
|
||||
на обработку ПДн по поручению (ст.6 ч.3 152-ФЗ).
|
||||
**Проверить вручную** — юридический документ.
|
||||
- [ ] **Unisender Go** (email-рассылки с именами пользователей):
|
||||
**Проверить вручную** — договор поручения на обработку ПДн.
|
||||
- [ ] **JivoSite** (helpdesk): передаются ли туда email/ФИО клиентов?
|
||||
**Проверить вручную**.
|
||||
@@ -0,0 +1,68 @@
|
||||
---
|
||||
name: security-go-live
|
||||
description: Единый go-live security-gate Лидерры перед публикацией в интернете — один воспроизводимый прогон всех проверок безопасности и вердикт «можно/нельзя в прод». Оркеструет ZAP (#68), Nuclei (#69), Ward (#70), pdn-152fz-audit (#71), threat-model (#72) + Semgrep #25 / Trivy #26 / gitleaks #8 / Trail of Bits #39. Используй при «прогон безопасности перед релизом», «можно ли выкатывать», «go-live security check», «финальная проверка безопасности». НЕ для полного 14-фазного аудита портала (audit-portal), отдельной проверки ПДн (pdn-152fz-audit #71) или угроз (threat-model #72).
|
||||
---
|
||||
|
||||
# Security Go-Live — единый gate безопасности перед публикацией
|
||||
|
||||
Проектный скил раздела A8 карты «Информационная безопасность». Запускает
|
||||
**один воспроизводимый прогон всех security-проверок** и выдаёт вердикт
|
||||
**GO / NO-GO** перед тем, как портал Лидерры становится доступным из интернета.
|
||||
|
||||
## Когда использовать
|
||||
|
||||
- «Прогони все проверки безопасности перед релизом»
|
||||
- «Можно ли выкатывать портал в прод по безопасности?»
|
||||
- «Go-live security check» / «финальная проверка безопасности»
|
||||
- «Готов ли портал к публикации со стороны ИБ?»
|
||||
|
||||
## Что это и чем НЕ является
|
||||
|
||||
**Это:** операционный gate — воспроизводимый чек-лист, который прогоняется
|
||||
каждый раз перед go-live и выдаёт конкретный вердикт с перечнем блокеров.
|
||||
|
||||
**Это НЕ:**
|
||||
|
||||
- ≠ `audit-portal` — тот 14-фазный сквозной аудит качества всего портала
|
||||
(статанализ, тесты, схема БД, UI-smoke, a11y, coverage, bundle и пр.);
|
||||
security-go-live — security-only срез, занимает часть дня, не несколько дней.
|
||||
- ≠ `pdn-152fz-audit` #71 — тот глубокий аудит персональных данных и 152-ФЗ;
|
||||
security-go-live вызывает его как один шаг, не заменяет.
|
||||
- ≠ `threat-model` #72 — тот строит модель угроз как документ (STRIDE, карта
|
||||
точек входа); security-go-live проверяет, что выявленные угрозы ЗАКРЫТЫ.
|
||||
|
||||
## Порядок прогона
|
||||
|
||||
Полная процедура — `references/gate.md`. Кратко:
|
||||
|
||||
1. **Статика** — gitleaks, Semgrep, Ward (config/env/deps/code), Trail of Bits.
|
||||
2. **ПДн / 152-ФЗ** — вызвать `pdn-152fz-audit` #71.
|
||||
3. **Угрозы** — вызвать `threat-model` #72, убедиться что топ-угрозы закрыты.
|
||||
4. **Динамика (локальная цель по умолчанию)** — Nuclei (`bin/nuclei.exe`),
|
||||
затем ZAP (spider + active scan). Боевой сервер — только по явной команде.
|
||||
5. **Вердикт** — GO / NO-GO с явным списком блокеров.
|
||||
|
||||
## Выход
|
||||
|
||||
```
|
||||
=== SECURITY GO-LIVE REPORT ===
|
||||
Дата: YYYY-MM-DD
|
||||
Версия схемы: <schema-version>
|
||||
Commit: <HEAD>
|
||||
|
||||
[ШАГИ 1-4 — результаты по каждому инструменту]
|
||||
|
||||
=== ВЕРДИКТ: GO ✅ / NO-GO ❌ ===
|
||||
Блокеры (critical/high): <список или "нет">
|
||||
Предупреждения (medium): <список или "нет">
|
||||
=== END ===
|
||||
```
|
||||
|
||||
## Связано
|
||||
|
||||
- `references/gate.md` — подробная процедура прогона + формат вердикта.
|
||||
- `pdn-152fz-audit` #71, `threat-model` #72 — вызываются как подшаги.
|
||||
- ZAP #68 (OWASP, DAST), Nuclei #69 (CLI `bin/nuclei.exe`), Ward #70 (Go CLI).
|
||||
- gitleaks #8, Semgrep #25, Trivy #26, Trail of Bits #39 — статика.
|
||||
- ADR-013 (infosec-tooling A8), `docs/security/nuclei-setup.md`,
|
||||
`docs/security/infosec-vet.md`.
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"skill": "security-go-live",
|
||||
"cases": [
|
||||
{"prompt": "прогони все проверки безопасности перед релизом", "should_trigger": true},
|
||||
{"prompt": "можно ли выкатывать портал в прод по безопасности", "should_trigger": true},
|
||||
{"prompt": "проведи полный аудит портала", "should_trigger": false, "expected": "audit-portal"},
|
||||
{"prompt": "проверь только персональные данные", "should_trigger": false, "expected": "pdn-152fz-audit"},
|
||||
{"prompt": "смоделируй угрозы", "should_trigger": false, "expected": "threat-model"}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,241 @@
|
||||
# Security Go-Live Gate — процедура прогона и формат вердикта
|
||||
|
||||
Подробная пошаговая процедура для скила `security-go-live` (#73).
|
||||
Цель — один воспроизводимый прогон перед каждым выходом портала в интернет.
|
||||
|
||||
---
|
||||
|
||||
## Гарды
|
||||
|
||||
**IS8 — цель по умолчанию локальная.** Все динамические проверки (Nuclei, ZAP)
|
||||
направляются на локальную или тестовую копию портала (`127.0.0.1`). Боевой
|
||||
(`crm.bp-gr.ru` или любой публичный IP) — только по явной команде заказчика:
|
||||
«сканируй прод» / «сканируй боевой».
|
||||
|
||||
**IS7 — граница с `audit-portal`.** `security-go-live` — security-only gate:
|
||||
выдаёт GO/NO-GO по безопасности. Он не заменяет 14-фазный `audit-portal`
|
||||
(тесты, схема, UI-smoke, a11y, coverage, bundle и пр.). Перед первым
|
||||
production-деплоем рекомендуется прогнать `audit-portal` **и** `security-go-live`
|
||||
как два отдельных прогона; при плановых go-live (хотфикс/фича) — достаточно
|
||||
`security-go-live`.
|
||||
|
||||
---
|
||||
|
||||
## Шаг 1 — Статика (static analysis)
|
||||
|
||||
Запустить последовательно. Каждый инструмент фиксирует результат в разделе
|
||||
отчёта.
|
||||
|
||||
### 1.1 gitleaks — поиск секретов в истории
|
||||
|
||||
```powershell
|
||||
# Полная история
|
||||
.\bin\gitleaks.exe detect --source . --log-opts "--all"
|
||||
# Только staged/unstaged (перед коммитом)
|
||||
.\bin\gitleaks.exe protect --staged
|
||||
```
|
||||
|
||||
Ожидаемо: **0 утечек**. Любой leak = NO-GO (critical).
|
||||
|
||||
### 1.2 Semgrep — статический анализ кода
|
||||
|
||||
```powershell
|
||||
npm run sast
|
||||
```
|
||||
|
||||
Ожидаемо: **0 critical/high**. Medium — предупреждение (не блокер).
|
||||
|
||||
### 1.3 Ward — Laravel config / env / deps / code
|
||||
|
||||
Ward (#70) — Go-бинарь, замена заброшенного Enlightn. Сканирует:
|
||||
`.env` (8 проверок), `config/*.php` (13 проверок), зависимости Composer
|
||||
(через OSV.dev), код (секреты, injection, XSS, debug-артефакты, crypto,
|
||||
CORS/CSRF/mass-assignment, auth).
|
||||
|
||||
```powershell
|
||||
# Если Ward установлен (pending — нет тегов-релизов, pin по commit SHA)
|
||||
.\bin\ward.exe scan --path app/
|
||||
```
|
||||
|
||||
Если Ward **не установлен** (pending `docs/security/ward-setup.md`) — отметить
|
||||
в отчёте как `PENDING` и продолжить. Ward — не блокер установки gate,
|
||||
но должен быть установлен до первого реального go-live.
|
||||
|
||||
Ожидаемо: **0 critical**. High — разобрать вручную. Ошибки конфигурации
|
||||
(APP_DEBUG=true, слабые ключи, открытые CORS) = NO-GO если critical.
|
||||
|
||||
### 1.4 Trail of Bits — глубокий on-demand аудит (#39)
|
||||
|
||||
Вызывается вручную перед первым публичным релизом или при значительных
|
||||
изменениях security-периметра. Не требуется при каждом хотфиксе.
|
||||
|
||||
```
|
||||
/differential-review:diff-review # если ревьюим конкретный diff
|
||||
/audit-context-building:audit-context # для supply-chain аудита
|
||||
```
|
||||
|
||||
Результаты фиксируются в `docs/security/trail-of-bits-YYYY-MM-DD.md`.
|
||||
|
||||
---
|
||||
|
||||
## Шаг 2 — ПДн / 152-ФЗ
|
||||
|
||||
Вызвать скил `pdn-152fz-audit` (#71).
|
||||
|
||||
```
|
||||
/pdn-152fz-audit
|
||||
```
|
||||
|
||||
Прогнать оба режима:
|
||||
|
||||
- **Режим 1 (технический):** RLS на таблицах ПДн, маскирование pg_anonymizer,
|
||||
отсутствие phone/email в логах, pg_anonymizer в дампах.
|
||||
- **Режим 2 (соответствие 152-ФЗ):** хранение в РФ, согласия, права субъекта
|
||||
(`pd_subject_requests`), журнал обработки (`pd_processing_log`), уведомление РКН.
|
||||
|
||||
Итог: список нарушений (если есть). Нарушения Режима 1 уровня critical (ПДн
|
||||
в открытых логах/Sentry) = NO-GO.
|
||||
|
||||
---
|
||||
|
||||
## Шаг 3 — Угрозы (threat model)
|
||||
|
||||
Вызвать скил `threat-model` (#72) или открыть последний файл
|
||||
`docs/security/threat-model-YYYY-MM-DD.md`.
|
||||
|
||||
Цель: убедиться, что **топ-приоритетные угрозы из STRIDE** закрыты контрмерами
|
||||
(rate-limit на login, HMAC на webhook, Sanctum token-auth, CSRF, RLS).
|
||||
|
||||
Если актуальная модель угроз отсутствует (нет файла за последние 30 дней) —
|
||||
запустить `threat-model` перед динамикой.
|
||||
|
||||
---
|
||||
|
||||
## Шаг 4 — Динамика (dynamic analysis, локальная цель)
|
||||
|
||||
> **IS8:** по умолчанию цель — локальная копия. Убедиться, что приложение
|
||||
> запущено: `php artisan serve` → `http://127.0.0.1:8000`.
|
||||
|
||||
### 4.1 Nuclei — широкое сканирование (#69)
|
||||
|
||||
Nuclei установлен как CLI-бинарь `bin/nuclei.exe` (MIT, projectdiscovery,
|
||||
v3.8.0). **Не MCP-сервер.**
|
||||
|
||||
**Квирки native-Windows (обязательно соблюдать):**
|
||||
|
||||
1. **Цель — `127.0.0.1`, НЕ `localhost`.** Резолвер Nuclei не разрешает
|
||||
`localhost` на этой машине — цель будет пропущена (квирк зафиксирован в
|
||||
`docs/security/nuclei-setup.md`).
|
||||
2. **Низкий rate-limit для dev-сервера.** `php artisan serve` однопоточный;
|
||||
без ограничений Nuclei перегружает его ложными connection-ошибками.
|
||||
Всегда использовать `-rate-limit 20 -c 5`.
|
||||
|
||||
```powershell
|
||||
# Стандартный прогон (medium+)
|
||||
bin\nuclei.exe -u "http://127.0.0.1:8000" `
|
||||
-rate-limit 20 -c 5 -timeout 5 -duc `
|
||||
-severity medium,high,critical
|
||||
|
||||
# Только технологический стек (быстрый smoke)
|
||||
bin\nuclei.exe -u "http://127.0.0.1:8000" -tags tech `
|
||||
-rate-limit 20 -c 5 -timeout 5 -duc
|
||||
```
|
||||
|
||||
Если `bin/nuclei.exe` отсутствует — отметить `PENDING` и продолжить.
|
||||
Детали установки: `docs/security/nuclei-setup.md`.
|
||||
|
||||
Ожидаемо: **0 critical/high**. Medium — разобрать вручную.
|
||||
|
||||
### 4.2 ZAP — глубокое DAST (#68)
|
||||
|
||||
ZAP (#68) — официальный MCP add-on (`zaproxy/zap-extensions`, Apache-2.0),
|
||||
alpha v0.1.0. Требует Java 17+ и запущенного ZAP-демона.
|
||||
|
||||
Если ZAP **не установлен** (pending Java) — отметить `PENDING` и продолжить.
|
||||
Детали: `docs/security/zap-setup.md` (когда будет создан).
|
||||
|
||||
```
|
||||
# Через ZAP MCP (когда ZAP установлен)
|
||||
# 1. Запустить ZAP-демон: zaproxy -daemon -port 8080 -config api.key=<key>
|
||||
# 2. Spider
|
||||
ZapStartSpiderTool(url="http://127.0.0.1:8000", contextId=...)
|
||||
# 3. Active scan
|
||||
ZapStartActiveScanTool(url="http://127.0.0.1:8000", contextId=...)
|
||||
# 4. Отчёт
|
||||
ZapGenerateReportTool(...)
|
||||
```
|
||||
|
||||
Ожидаемо: **0 critical/high**. Medium — разобрать вручную.
|
||||
Critical/high из ZAP active scan = NO-GO.
|
||||
|
||||
---
|
||||
|
||||
## Шаг 5 — Сбор находок и вердикт
|
||||
|
||||
### Severity → статус
|
||||
|
||||
| Severity | Источник | Статус gate |
|
||||
|---|---|---|
|
||||
| critical | любой инструмент | **NO-GO** (блокер) |
|
||||
| high | любой инструмент | **NO-GO** (блокер) |
|
||||
| medium | любой инструмент | Предупреждение (не блокирует go-live, фиксируется) |
|
||||
| low / info | любой инструмент | Информационно |
|
||||
| PENDING | ZAP / Ward / Nuclei не установлены | Условный GO — инструменты должны быть установлены до публичного деплоя |
|
||||
|
||||
### Формат отчёта
|
||||
|
||||
```
|
||||
=== SECURITY GO-LIVE REPORT ===
|
||||
Дата: YYYY-MM-DD
|
||||
Версия схемы: vX.XX
|
||||
Commit: <git rev-parse HEAD>
|
||||
Цель: http://127.0.0.1:<port> (локальная копия)
|
||||
|
||||
--- ШАГ 1: СТАТИКА ---
|
||||
gitleaks: OK (0 утечек) / FAIL (<N> утечек)
|
||||
Semgrep: OK (0 critical/high) / FAIL (<список>)
|
||||
Ward: OK / FAIL (<список>) / PENDING (не установлен)
|
||||
Trail of Bits: OK / SKIP (не применимо к этому прогону)
|
||||
|
||||
--- ШАГ 2: ПДн / 152-ФЗ ---
|
||||
pdn-152fz-audit Режим 1: OK / FAIL (<список>)
|
||||
pdn-152fz-audit Режим 2: OK / ПРЕДУПРЕЖДЕНИЯ (<список>)
|
||||
|
||||
--- ШАГ 3: УГРОЗЫ ---
|
||||
threat-model: ЗАКРЫТЫ (файл docs/security/threat-model-YYYY-MM-DD.md)
|
||||
Незакрытые топ-угрозы: <список или "нет">
|
||||
|
||||
--- ШАГ 4: ДИНАМИКА ---
|
||||
Nuclei: OK (0 critical/high) / FAIL (<список>) / PENDING (не установлен)
|
||||
ZAP: OK (0 critical/high) / FAIL (<список>) / PENDING (не установлен)
|
||||
|
||||
=== ВЕРДИКТ: GO ✅ / NO-GO ❌ ===
|
||||
Блокеры (critical/high):
|
||||
- <инструмент>: <описание> — <рекомендация>
|
||||
(или "Блокеров нет")
|
||||
|
||||
Предупреждения (medium):
|
||||
- <инструмент>: <описание>
|
||||
(или "Предупреждений нет")
|
||||
|
||||
PENDING-инструменты (должны быть закрыты до публичного деплоя):
|
||||
- Ward #70: установка — docs/security/ward-setup.md
|
||||
- ZAP #68: установка — docs/security/zap-setup.md (pending Java)
|
||||
(или "Все инструменты установлены")
|
||||
=== END ===
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Типичные блокеры и действия
|
||||
|
||||
| Находка | Источник | Действие |
|
||||
|---|---|---|
|
||||
| APP\_DEBUG=true | Ward / Semgrep | Исправить `.env` перед деплоем |
|
||||
| Секрет в git-истории | gitleaks | Rotate + `git filter-repo`; НЕ деплоить |
|
||||
| ПДн в логах Laravel | pdn-152fz-audit | Убрать из LogChannel + Sentry scrubbing |
|
||||
| CSRF отключён | Ward | Проверить `VerifyCsrfToken` middleware |
|
||||
| Слабый APP\_KEY | Ward | `php artisan key:generate` |
|
||||
| Критическая CVE в зависимости | Semgrep / Ward | `composer update` или `npm update` |
|
||||
| SQL injection / XSS | ZAP / Nuclei | Исправить код, перепрогнать |
|
||||
| Незакрытая STRIDE-угроза | threat-model | Реализовать контрмеру или принять риск с заказчиком |
|
||||
@@ -0,0 +1,66 @@
|
||||
---
|
||||
name: threat-model
|
||||
description: Моделирование угроз портала Лидерра по STRIDE — карта точек входа, что меняется при выходе в интернет, приоритизация защиты. Используй при «смоделируй угрозы», «откуда могут атаковать», «что защищать в первую очередь перед публикацией», «карта точек входа», «threat model / STRIDE». НЕ для аудита ПДн/152-ФЗ (pdn-152fz-audit #71), статического security-аудита кода (D3/Semgrep/Trail of Bits), generic архитектурных паттернов (architecture-patterns), go-live прогона (security-go-live #73).
|
||||
---
|
||||
|
||||
# Threat Model — моделирование угроз портала Лидерра
|
||||
|
||||
Проектный скил раздела A8 карты «Информационная безопасность». Применяет методологию
|
||||
**STRIDE** к реальным точкам входа портала и отвечает на главный вопрос перед
|
||||
публикацией: **что именно меняется, когда в систему может зайти любой из интернета**.
|
||||
|
||||
## Когда использовать
|
||||
|
||||
- «Смоделируй угрозы» / «откуда могут атаковать» / «что защищать в первую очередь»
|
||||
- Подготовка к go-live — составление модели угроз как артефакта (отдельно от
|
||||
чек-листа запуска, который — в `security-go-live #73`)
|
||||
- Анализ конкретного эндпоинта: «насколько опасен открытый `/api/webhook/{token}`?»
|
||||
- Ответ на вопрос заказчика / регулятора «покажи модель угроз»
|
||||
|
||||
## Процедура STRIDE для Лидерры
|
||||
|
||||
Полный разбор точек входа и таблица угроз — `references/stride-portal.md`.
|
||||
|
||||
### Шаги
|
||||
|
||||
1. **Определить периметр** — что сейчас открыто наружу vs что будет открыто после
|
||||
публикации. Основа: список точек входа в `references/stride-portal.md`.
|
||||
2. **Пройти по STRIDE для каждой точки** — заполнить 6 строк (S/T/R/I/D/E).
|
||||
Опираться на таблицу в `references/stride-portal.md`; при новых эндпоинтах
|
||||
добавлять строки по тому же шаблону.
|
||||
3. **Оценить вероятность × ущерб** — приоритизировать по матрице из `references/stride-portal.md`.
|
||||
4. **Сформировать список контрмер** — что уже есть (RLS, HMAC, Sanctum, rate-limit),
|
||||
чего не хватает (rate-limit на login, WAF, 2FA enforcement, и т.д.).
|
||||
5. **Сохранить результат** в `docs/security/threat-model-YYYY-MM-DD.md`.
|
||||
|
||||
## Выход
|
||||
|
||||
Файл `docs/security/threat-model-<дата>.md` со структурой:
|
||||
|
||||
- Область действия (дата, версия схемы, commit)
|
||||
- Карта точек входа (таблица)
|
||||
- STRIDE по каждой точке
|
||||
- Дельта «был закрытый круг → стал интернет»
|
||||
- Приоритизированный список рисков с контрмерами
|
||||
|
||||
## Границы
|
||||
|
||||
- ≠ `pdn-152fz-audit` #71 — тот про *персональные данные и 152-ФЗ* (конкретные
|
||||
таблицы, согласия, права субъекта); threat-model про *вектора атак и защиту
|
||||
эндпоинтов*.
|
||||
- ≠ D3 audit-security (#39/#40 Trail of Bits / Semgrep) — те про *статический
|
||||
анализ кода на уязвимости*; threat-model про *архитектурную карту угроз*.
|
||||
- ≠ `architecture-patterns` #38 — тот generic-паттерны; threat-model — конкретный
|
||||
портал, конкретные маршруты.
|
||||
- ≠ `security-go-live` #73 — тот *прогоняет конкретный чек-лист* перед релизом
|
||||
(Nmap, заголовки, CVE, gitleaks, DAST); threat-model *строит модель угроз как
|
||||
документ* (вход для чек-листа и приоритизации работ).
|
||||
|
||||
## Связано
|
||||
|
||||
- `references/stride-portal.md` — детальная карта точек входа и STRIDE-таблица.
|
||||
- `pdn-152fz-audit` #71 — смежный аудит ПДн; часто запускается вместе с threat-model.
|
||||
- `security-go-live` #73 — операционный прогон после threat-model завершён.
|
||||
- D3 / Semgrep #25 / Trail of Bits #39 — статический анализ; дополняет threat-model
|
||||
на уровне кода.
|
||||
- ADR-013 (infosec-tooling A8).
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"skill": "threat-model",
|
||||
"cases": [
|
||||
{"prompt": "смоделируй угрозы при выходе портала в интернет", "should_trigger": true},
|
||||
{"prompt": "что защищать в первую очередь перед публикацией", "should_trigger": true},
|
||||
{"prompt": "откуда могут атаковать портал", "should_trigger": true},
|
||||
{"prompt": "составь карту точек входа", "should_trigger": true},
|
||||
{"prompt": "сделай threat model по STRIDE", "should_trigger": true},
|
||||
{"prompt": "проверь соответствие 152-ФЗ", "should_trigger": false, "expected": "pdn-152fz-audit"},
|
||||
{"prompt": "прогони все проверки безопасности перед релизом", "should_trigger": false, "expected": "security-go-live"},
|
||||
{"prompt": "просканируй код на уязвимости семгрепом", "should_trigger": false, "expected": "D3/Semgrep"}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
# STRIDE — карта угроз портала Лидерра
|
||||
|
||||
Основан на реальных маршрутах `app/routes/web.php` (v8.26, 21.05.2026).
|
||||
Стек: Laravel 13 + Vue 3 + PostgreSQL 16 RLS + Redis, Yandex Cloud `ru-central1`.
|
||||
|
||||
---
|
||||
|
||||
## Карта точек входа
|
||||
|
||||
| # | Точка входа | Маршрут(ы) | Аутентификация |
|
||||
|---|---|---|---|
|
||||
| E1 | Вход / регистрация | `POST /api/auth/login`, `POST /api/auth/register` | Публичный |
|
||||
| E2 | 2FA и коды восстановления | `POST /api/auth/2fa/verify`, `POST /api/auth/2fa/recovery-use` | Публичный (pending-session) |
|
||||
| E3 | Сброс пароля | `POST /api/auth/forgot`, `POST /api/auth/reset-password` | Публичный |
|
||||
| E4 | Входящий webhook поставщика | `POST /api/webhook/supplier/{secret}` | URL-secret + IP-allowlist |
|
||||
| E5 | Входящий webhook тенанта | `POST /api/webhook/{token}` | URL-token + (prod: HMAC X-Webhook-Signature + rate-limit) |
|
||||
| E6 | API сделок | `GET/POST/PATCH/DELETE /api/deals`, `/api/deals/export`, `/api/deals/transition`, `/api/deals/restore` | Sanctum SPA + tenant |
|
||||
| E7 | API проектов | `GET/POST/PATCH/DELETE /api/projects/{id}`, `/api/projects/bulk`, `/api/projects/{id}/sync` | Sanctum SPA + tenant |
|
||||
| E8 | API импорта CSV | `POST /api/imports`, `GET /api/imports/{importLog}`, `/api/imports/unknown-statuses` | Sanctum SPA + tenant |
|
||||
| E9 | Lookup-эндпоинты | `GET /api/managers`, `GET /api/lead-statuses` | **Без auth** (открытые) |
|
||||
| E10 | Биллинг тенанта | `POST /api/billing/topup`, `GET /api/billing/wallet`, `/transactions`, `/invoices` | Sanctum SPA + tenant |
|
||||
| E11 | Charges ledger | `GET /api/billing/charges`, `POST /api/billing/charges/export` | Sanctum SPA + tenant |
|
||||
| E12 | API-ключи тенанта | `GET /api/api-keys`, `POST /api/api-keys/regenerate` | Sanctum SPA + tenant |
|
||||
| E13 | Webhook-настройки тенанта | `GET/PUT /api/tenants/me/webhook-settings`, `POST /api/webhooks/test` | Sanctum SPA + tenant |
|
||||
| E14 | Напоминания | `GET/POST/PATCH/DELETE /api/reminders/{id}` | Sanctum SPA + tenant |
|
||||
| E15 | Уведомления | `GET/PATCH/POST/DELETE /api/notifications/{id}` | Sanctum SPA + tenant |
|
||||
| E16 | Отчёты | `GET/POST/DELETE /api/reports/jobs/{id}`, `POST /{id}/retry`, `POST /{id}/cancel` | Sanctum SPA + tenant |
|
||||
| E17 | Скачивание отчёта | `GET /api/reports/jobs/{id}/file` | Signed URL (без Sanctum) |
|
||||
| E18 | Дашборд | `GET /api/dashboard/summary` | **Без auth** (MVP-заглушка) |
|
||||
| E19 | Профиль / уведомления-настройки | `GET/PATCH /api/auth/me`, `PATCH /api/auth/me/notification-preferences` | Sanctum SPA |
|
||||
| E20 | SaaS-admin: тенанты, биллинг, инциденты, система | `GET/PATCH /api/admin/**` | `saas-admin` middleware |
|
||||
| E21 | SaaS-admin: импersonation | `POST /api/admin/impersonation/init`, `/verify`, `/end` | `saas-admin` middleware |
|
||||
| E22 | SaaS-admin: supplier-integration | `GET/POST /api/admin/supplier-integration/**` | `saas-admin` middleware |
|
||||
| E23 | 2FA setup (авторизованный) | `POST /api/2fa/init`, `/confirm`, `/disable`, `/regenerate-recovery-codes` | Sanctum SPA |
|
||||
| E24 | SPA-оболочка | `GET /`, `/login`, `/register`, `/deals`, … (20+ маршрутов) | Без auth (Vue shell) |
|
||||
|
||||
---
|
||||
|
||||
## Дельта «закрытый круг → интернет»
|
||||
|
||||
До публикации портал доступен только команде (VPN или фиксированные IP).
|
||||
После публикации **любой актор из интернета** может обратиться к каждому публичному
|
||||
эндпоинту. Критические изменения:
|
||||
|
||||
| Изменение | Затронутые точки | Почему важно |
|
||||
|---|---|---|
|
||||
| Брутфорс и credential stuffing | E1 (login) | Нет rate-limit на `/api/auth/login` (на момент анализа) |
|
||||
| Энумерация пользователей | E1, E3 | Разные ответы на «существующий / несуществующий email» создают oracle |
|
||||
| Replay и forgery webhook | E4, E5 | Secret в URL виден в логах прокси/nginx; HMAC на E5 — «prod» (не в dev) |
|
||||
| Открытые lookup-эндпоинты | E9 | `GET /api/managers`, `GET /api/lead-statuses` без auth — раскрывают ФИО менеджеров |
|
||||
| Открытый дашборд | E18 | `GET /api/dashboard/summary` без auth — раскрывает KPI текущего тенанта |
|
||||
| DoS на artisan-сервере | Все | `php artisan serve` не держит нагрузку; нужен nginx/Octane |
|
||||
| SSRF через webhook-test | E13 | `POST /api/webhooks/test` отправляет запрос на URL из тела — риск SSRF во внутреннюю сеть YC |
|
||||
| Impersonation без prod-auth | E21 | `saas-admin` middleware в dev-режиме пропускает без проверки (`SAAS_ADMIN_TEST_BYPASS`) |
|
||||
| Signed URL без срока инвалидации | E17 | Отчёт с ПДн доступен 24 ч по ссылке без повторной аутентификации |
|
||||
|
||||
---
|
||||
|
||||
## STRIDE по точкам входа
|
||||
|
||||
### E1 — Вход / Регистрация (`POST /api/auth/login`, `POST /api/auth/register`)
|
||||
|
||||
| Угроза | Описание | Текущий контроль | Пробел |
|
||||
|---|---|---|---|
|
||||
| **S** Spoofing | Брутфорс пароля, credential stuffing | Bcrypt-хеш пароля | Нет rate-limit на login |
|
||||
| **T** Tampering | Подмена `tenant_id` в теле запроса | `tenant_id` берётся из `auth()->user()`, не из тела | — |
|
||||
| **R** Repudiation | Отрицание входа | `auth_log` пишет login/logout | Нет IP + User-Agent в каждой записи |
|
||||
| **I** Info disclosure | Энумерация email через разные ответы | Unified-ответ на forgot (E3) | Login может раскрывать «нет такого пользователя» |
|
||||
| **D** DoS | Флуд регистраций, засорение БД | — | Нет captcha / email-верификации на register |
|
||||
| **E** Elevation | Регистрация с `is_admin=true` в теле | Mass-assignment guard (fillable) | Проверить `$fillable` в `User` — нет ли `role` |
|
||||
|
||||
### E2 — 2FA (`POST /api/auth/2fa/verify`, `POST /api/auth/2fa/recovery-use`)
|
||||
|
||||
| Угроза | Описание | Текущий контроль | Пробел |
|
||||
|---|---|---|---|
|
||||
| **S** Spoofing | Брутфорс 6-значного TOTP | TOTP 30-сек окно | Нет rate-limit на `/2fa/verify` |
|
||||
| **T** Tampering | Подмена `pending_user_id` в session | Серверная session | Проверить изоляцию session между тенантами |
|
||||
| **R** Repudiation | Использование кода восстановления | `auth_log` | Фиксируется ли `recovery_used` событие? |
|
||||
| **I** Info disclosure | Тайминг-атака на сравнение TOTP | TOTP библиотека (constant-time?) | Проверить реализацию `verifyTwoFactor` |
|
||||
| **D** DoS | Флуд на `/2fa/verify` истощает session-store | — | Нет rate-limit |
|
||||
| **E** Elevation | Обход 2FA через `recovery-use` | Коды — одноразовые, хранятся hashed | Если коды в открытом виде — критично |
|
||||
|
||||
### E3 — Сброс пароля (`POST /api/auth/forgot`, `POST /api/auth/reset-password`)
|
||||
|
||||
| Угроза | Описание | Текущий контроль | Пробел |
|
||||
|---|---|---|---|
|
||||
| **S** Spoofing | Захват аккаунта через сброс пароля чужого email | Токен по email | Нет rate-limit на `/forgot` |
|
||||
| **T** Tampering | Подмена токена сброса | Cryptographic token (Laravel default) | Проверить срок жизни токена (1 ч?) |
|
||||
| **R** Repudiation | — | — | — |
|
||||
| **I** Info disclosure | Энумерация email через тайминг ответа | Unified-ответ задокументирован в роутах | Проверить фактическую реализацию ответа |
|
||||
| **D** DoS | Флуд `/forgot` → очередь email | — | Нет rate-limit → перегрузка Unisender Go |
|
||||
| **E** Elevation | — | — | — |
|
||||
|
||||
### E4 — Webhook поставщика (`POST /api/webhook/supplier/{secret}`)
|
||||
|
||||
| Угроза | Описание | Текущий контроль | Пробел |
|
||||
|---|---|---|---|
|
||||
| **S** Spoofing | Подделка запроса от crm.bp-gr.ru | URL-secret + IP allowlist (`system_settings.supplier_ip_allowlist`) | Secret виден в логах nginx/прокси |
|
||||
| **T** Tampering | Подмена payload (телефон, стоимость лида) | — | Нет HMAC на тело; только secret в URL |
|
||||
| **R** Repudiation | Отрицание доставки лида | `supplier_leads.raw_payload` | Нет timestamp-подписи для доказательства |
|
||||
| **I** Info disclosure | Secret в URL → в access-логах сервера | IP allowlist сужает круг | Ротация secret при компрометации? |
|
||||
| **D** DoS | Флуд поддельных лидов → списание баланса | IP allowlist | Если allowlist обходится (SSRF) |
|
||||
| **E** Elevation | Подмена `tenant_id` в payload | Берётся из `system_settings` глобально | Архитектурно корректно; проверить lookup |
|
||||
|
||||
### E5 — Webhook тенанта (`POST /api/webhook/{token}`)
|
||||
|
||||
| Угроза | Описание | Текущий контроль | Пробел |
|
||||
|---|---|---|---|
|
||||
| **S** Spoofing | Запрос от неавторизованного источника | URL-token из `tenants.webhook_token`; HMAC X-Webhook-Signature (prod) | HMAC только в prod; dev уязвим |
|
||||
| **T** Tampering | Изменение payload в transit | HMAC-валидация (prod) | В dev отключена — нельзя тестировать на prod-данных |
|
||||
| **R** Repudiation | — | `supplier_leads.raw_payload` | — |
|
||||
| **I** Info disclosure | Token в URL виден в логах | Per-token rate-limit | Нет ротации token при смене API-ключа |
|
||||
| **D** DoS | Replay flood | Per-token rate-limit (prod) | Нет в dev |
|
||||
| **E** Elevation | Лид с завышенной ценой | Стоимость берётся из `PricingTierResolver`, не из payload | Архитектурно защищено |
|
||||
|
||||
### E9 — Открытые lookup-эндпоинты (`GET /api/managers`, `GET /api/lead-statuses`)
|
||||
|
||||
| Угроза | Описание | Текущий контроль | Пробел |
|
||||
|---|---|---|---|
|
||||
| **S** Spoofing | — | — | — |
|
||||
| **T** Tampering | — | — | — |
|
||||
| **R** Repudiation | — | — | — |
|
||||
| **I** Info disclosure | ФИО менеджеров без аутентификации | — | **Нет auth** — любой из интернета получает список менеджеров |
|
||||
| **D** DoS | Флуд запросами | — | Нет rate-limit |
|
||||
| **E** Elevation | — | — | — |
|
||||
|
||||
### E18 — Дашборд без auth (`GET /api/dashboard/summary`)
|
||||
|
||||
| Угроза | Описание | Текущий контроль | Пробел |
|
||||
|---|---|---|---|
|
||||
| **I** Info disclosure | KPI, баланс, активность тенанта без аутентификации | — | **MVP-заглушка**: auth не включён; в prod обязателен |
|
||||
| **D** DoS | Тяжёлый агрегационный запрос без auth | — | Доступен без токена |
|
||||
|
||||
### E20 — SaaS-admin (`GET/PATCH /api/admin/**`)
|
||||
|
||||
| Угроза | Описание | Текущий контроль | Пробел |
|
||||
|---|---|---|---|
|
||||
| **S** Spoofing | Доступ к admin-панели без Yandex 360 SSO | `saas-admin` middleware (fail-closed 503 в prod) | SSO не реализован до Б-1; `SAAS_ADMIN_TEST_BYPASS` в prod = полный доступ |
|
||||
| **T** Tampering | Изменение тарифа, статуса тенанта без аудита | `saas_admin_audit_log` | — |
|
||||
| **R** Repudiation | Отрицание действий admin | `saas_admin_audit_log` | Нет подписи/2FA для деструктивных операций |
|
||||
| **I** Info disclosure | Данные всех тенантов | `saas-admin` middleware | SAAS_ADMIN_TEST_BYPASS=true в production = полный дамп |
|
||||
| **D** DoS | Bulk-delete тенантов | — | Нет подтверждения для деструктивных bulk-операций |
|
||||
| **E** Elevation | Impersonation любого тенанта | `saas-admin` middleware | Та же уязвимость через bypass |
|
||||
|
||||
### E21 — Impersonation (`POST /api/admin/impersonation/init`, `/verify`, `/end`)
|
||||
|
||||
| Угроза | Описание | Текущий контроль | Пробел |
|
||||
|---|---|---|---|
|
||||
| **S** Spoofing | Имперсонация без реального admin-права | `saas-admin` middleware | Bypass в dev/test режиме |
|
||||
| **T** Tampering | Изменение `admin_user_id` в токене | Token-based flow | Проверить, что token не forgeble |
|
||||
| **R** Repudiation | Отрицание сессии имперсонации | `impersonation_tokens` логирует | Нет нотификации целевому тенанту |
|
||||
| **E** Elevation | Получение прав тенанта через impersonation | Scope ограничен tenant-контекстом | Если RLS bypass во время импersонации |
|
||||
|
||||
### E13 — SSRF через webhook-test (`POST /api/webhooks/test`)
|
||||
|
||||
| Угроза | Описание | Текущий контроль | Пробел |
|
||||
|---|---|---|---|
|
||||
| **T** Tampering | Отправка запроса на внутренний адрес YC | — | **Нет фильтрации URL** — SSRF во внутреннюю сеть Yandex Cloud (metadata service 169.254.169.254) |
|
||||
| **I** Info disclosure | YC instance metadata (IAM-токен, настройки сети) | — | Критично: SSRF → metadata API → IAM credentials |
|
||||
|
||||
---
|
||||
|
||||
## Приоритизация рисков
|
||||
|
||||
Матрица: **Вероятность** (В — высокая / С — средняя / Н — низкая) ×
|
||||
**Ущерб** (К — критический / В — высокий / С — средний / Н — низкий).
|
||||
|
||||
| Приоритет | Риск | Точка | Вероятность | Ущерб | Контрмера |
|
||||
|---|---|---|---|---|---|
|
||||
| 🔴 P0 | SAAS_ADMIN_TEST_BYPASS=true в prod | E20, E21 | В | К | Убедиться, что флаг false в `.env.production`; fail-closed middleware |
|
||||
| 🔴 P0 | SSRF через `/api/webhooks/test` | E13 | С | К | Валидировать URL: запрещать RFC1918 + link-local + metadata-IP; использовать DNS-rebind защиту |
|
||||
| 🔴 P0 | `GET /api/dashboard/summary` без auth | E18 | В | В | Добавить `auth:sanctum + tenant` middleware до prod |
|
||||
| 🔴 P0 | `GET /api/managers`, `GET /api/lead-statuses` без auth | E9 | В | С | Добавить `auth:sanctum + tenant` |
|
||||
| 🟠 P1 | Нет rate-limit на login / forgot / 2fa/verify | E1, E2, E3 | В | В | Laravel Throttle middleware (e.g. `throttle:5,1`) |
|
||||
| 🟠 P1 | URL-secret поставщика виден в access-логах | E4 | С | В | Перевести на HMAC-заголовок; ротировать secret; закрыть логи |
|
||||
| 🟠 P1 | Флуд поддельных лидов → списание баланса | E4, E5 | С | В | IP allowlist жёсткий; HMAC на тело (E4); idempotency-key |
|
||||
| 🟡 P2 | Энумерация email на login (не только forgot) | E1 | В | С | Unified-ответ на login тоже |
|
||||
| 🟡 P2 | Флуд регистраций без email-верификации | E1 | С | С | Email verification или captcha |
|
||||
| 🟡 P2 | Signed URL отчёта 24 ч без аутентификации | E17 | Н | С | Сократить TTL; добавить revocation при logout |
|
||||
| 🟡 P2 | Нет нотификации тенанту при impersonation | E21 | Н | С | Email/in-app уведомление при входе admin |
|
||||
| 🟢 P3 | Тайминг-атака на TOTP | E2 | Н | С | Проверить constant-time compare в TwoFactorController |
|
||||
| 🟢 P3 | Тайминг-атака на email в forgot | E3 | Н | Н | Unified-ответ + jitter sleep |
|
||||
|
||||
---
|
||||
|
||||
## Что уже защищает портал (baseline)
|
||||
|
||||
- **RLS PostgreSQL** — 39 политик; кросс-tenant утечка через SQL закрыта.
|
||||
- **Sanctum SPA auth** — все бизнес-эндпоинты под `auth:sanctum + tenant`.
|
||||
- **Per-token rate-limit** — на входящих webhook'ах тенанта (E5).
|
||||
- **IP allowlist** — на webhook поставщика (E4).
|
||||
- **HMAC X-Webhook-Signature** — на E5 в prod (не в dev).
|
||||
- **`auth_log`** — фиксирует login/logout события.
|
||||
- **`saas_admin_audit_log`** — фиксирует admin-действия.
|
||||
- **Bcrypt** — хеш пароля; коды восстановления 2FA — hashed.
|
||||
- **`saas-admin` middleware** — fail-closed 503 в prod (если `SAAS_ADMIN_TEST_BYPASS=false`).
|
||||
- **Signed URL** — для скачивания отчётов (E17).
|
||||
- **gitleaks** — pre-commit/pre-push; секреты не должны попасть в репозиторий.
|
||||
@@ -0,0 +1,6 @@
|
||||
# gitleaks false-positive allowlist (fingerprints).
|
||||
# Format: one fingerprint per line. `gitleaks detect --report-format json` outputs them.
|
||||
|
||||
# Nuclei docs `-u http://...` — nuclei's -u flag is "target URL", not curl basic-auth.
|
||||
# Rule `curl-auth-user` matches the pattern but it's not authentication.
|
||||
f696ca50266eb1c2974b5fc89f6fa585edaf4b6b:docs/security/nuclei-setup.md:curl-auth-user:27
|
||||
@@ -10,6 +10,9 @@ use App\Models\Project;
|
||||
use App\Models\SupplierManualSyncQueue;
|
||||
use App\Models\SupplierProject;
|
||||
use App\Services\Supplier\Channel\SupplierProjectChannel;
|
||||
use App\Services\Supplier\SupplierExportMode;
|
||||
use App\Services\Supplier\SupplierPortalClient;
|
||||
use App\Support\RussianRegions;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -142,4 +145,114 @@ final class AdminSupplierIntegrationController extends Controller
|
||||
|
||||
return response()->json(['resolved' => true, 'external_id' => $found]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Глобальный режим экспорта проектов поставщику (Plan 4 Task 1).
|
||||
* Spec: docs/superpowers/specs/2026-05-20-project-migration-redesign-design.md §4.1.
|
||||
*/
|
||||
public function getExportMode(): JsonResponse
|
||||
{
|
||||
return response()->json(['mode' => SupplierExportMode::current()]);
|
||||
}
|
||||
|
||||
public function setExportMode(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'mode' => ['required', 'in:online,batch'],
|
||||
]);
|
||||
|
||||
DB::table('system_settings')->updateOrInsert(
|
||||
['key' => 'supplier_export_mode'],
|
||||
['value' => $data['mode'], 'type' => 'string', 'updated_at' => now()],
|
||||
);
|
||||
|
||||
return response()->json(['mode' => $data['mode']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Plan 4 Task 2: список supplier_projects + кто заказывал (через pivot →
|
||||
* projects → tenants) + дата последней поставки лида.
|
||||
*/
|
||||
public function projectsIndex(): JsonResponse
|
||||
{
|
||||
$rows = DB::table('supplier_projects as sp')
|
||||
->select([
|
||||
'sp.id',
|
||||
'sp.platform',
|
||||
'sp.signal_type',
|
||||
'sp.unique_key',
|
||||
'sp.subject_code',
|
||||
'sp.supplier_external_id',
|
||||
'sp.current_limit',
|
||||
'sp.inactive_since',
|
||||
])
|
||||
->orderBy('sp.unique_key')
|
||||
->orderBy('sp.subject_code')
|
||||
->orderBy('sp.platform')
|
||||
->get();
|
||||
|
||||
$projects = $rows->map(function ($sp): array {
|
||||
$orderers = DB::table('project_supplier_links as psl')
|
||||
->join('projects as p', 'p.id', '=', 'psl.project_id')
|
||||
->join('tenants as t', 't.id', '=', 'p.tenant_id')
|
||||
->where('psl.supplier_project_id', $sp->id)
|
||||
->distinct()
|
||||
->pluck('t.organization_name')
|
||||
->all();
|
||||
|
||||
$lastDelivery = DB::table('supplier_leads')
|
||||
->where('supplier_project_id', $sp->id)
|
||||
->max('received_at');
|
||||
|
||||
$subjectCode = $sp->subject_code !== null ? (int) $sp->subject_code : null;
|
||||
|
||||
return [
|
||||
'id' => (int) $sp->id,
|
||||
'platform' => $sp->platform,
|
||||
'signal_type' => $sp->signal_type,
|
||||
'unique_key' => $sp->unique_key,
|
||||
'subject_code' => $subjectCode,
|
||||
'subject_name' => $subjectCode !== null
|
||||
? (RussianRegions::CODE_TO_NAME[$subjectCode] ?? null)
|
||||
: 'РФ',
|
||||
'current_limit' => (int) $sp->current_limit,
|
||||
'supplier_external_id' => $sp->supplier_external_id,
|
||||
'inactive_since' => $sp->inactive_since,
|
||||
'orderers' => $orderers,
|
||||
'last_delivery_at' => $lastDelivery,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json(['projects' => $projects->all()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Plan 4 Task 2: bulk-delete выбранных supplier_projects.
|
||||
* Сначала на портале (deleteProject), затем локально (pivot снимается CASCADE).
|
||||
* Сбой по строке — не прерывает batch, копится в failures[].
|
||||
*/
|
||||
public function projectsDestroy(Request $request, SupplierPortalClient $client): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'ids' => ['required', 'array', 'min:1'],
|
||||
'ids.*' => ['integer'],
|
||||
]);
|
||||
|
||||
$deleted = 0;
|
||||
$failures = [];
|
||||
|
||||
foreach (SupplierProject::whereIn('id', $data['ids'])->get() as $sp) {
|
||||
try {
|
||||
if ($sp->supplier_external_id !== null) {
|
||||
$client->deleteProject((int) $sp->supplier_external_id);
|
||||
}
|
||||
$sp->delete();
|
||||
$deleted++;
|
||||
} catch (\Throwable $e) {
|
||||
$failures[] = ['id' => $sp->id, 'error' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json(['deleted' => $deleted, 'failures' => $failures]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,10 +28,9 @@ class DashboardController extends Controller
|
||||
|
||||
public function summary(Request $request): JsonResponse
|
||||
{
|
||||
$tenantId = (int) $request->query('tenant_id', '0');
|
||||
if ($tenantId < 1) {
|
||||
return response()->json(['message' => 'Параметр tenant_id обязателен.'], 422);
|
||||
}
|
||||
// Go-live (audit J3): tenant_id из authed-user (auth:sanctum + tenant
|
||||
// middleware), НЕ из параметра запроса — закрывает кросс-tenant утечку KPI.
|
||||
$tenantId = (int) $request->user()->tenant_id;
|
||||
|
||||
$tenant = Tenant::find($tenantId);
|
||||
if ($tenant === null) {
|
||||
@@ -74,7 +73,6 @@ class DashboardController extends Controller
|
||||
// --- active projects ---
|
||||
$activeProjects = DB::table('projects')
|
||||
->where('tenant_id', $tenantId)
|
||||
->whereNull('archived_at')
|
||||
->where('is_active', true)
|
||||
->count();
|
||||
$maxProjects = (int) (($tenant->limits['max_projects'] ?? 0));
|
||||
|
||||
@@ -5,7 +5,6 @@ declare(strict_types=1);
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -23,20 +22,18 @@ class ManagerController extends Controller
|
||||
/** GET /api/managers?tenant_id={id} */
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$tenantId = (int) $request->query('tenant_id', '0');
|
||||
if ($tenantId < 1) {
|
||||
return response()->json(['message' => 'Параметр tenant_id обязателен.'], 422);
|
||||
}
|
||||
|
||||
$tenant = Tenant::find($tenantId);
|
||||
if ($tenant === null) {
|
||||
return response()->json(['message' => 'Тенант не найден.'], 404);
|
||||
}
|
||||
// Go-live: tenant_id из authed-user (auth:sanctum + tenant middleware),
|
||||
// НЕ из параметра запроса — закрывает кросс-tenant утечку списка пользователей.
|
||||
$tenantId = (int) $request->user()->tenant_id;
|
||||
|
||||
$users = DB::transaction(function () use ($tenantId) {
|
||||
DB::statement('SET LOCAL app.current_tenant_id = '.$tenantId);
|
||||
|
||||
// Явный where(tenant_id) — defense-in-depth поверх RLS: роли с
|
||||
// BYPASSRLS (crm_supplier_worker / dev-superuser) RLS не применяют,
|
||||
// поэтому tenant-scope нельзя оставлять только на SET LOCAL.
|
||||
return User::query()
|
||||
->where('tenant_id', $tenantId)
|
||||
->whereNull('deleted_at')
|
||||
->where('is_active', true)
|
||||
->orderBy('first_name')
|
||||
|
||||
@@ -52,16 +52,12 @@ class ProjectController extends Controller
|
||||
|
||||
// Фильтр по статусу жизненного цикла
|
||||
$status = $request->query('status');
|
||||
if ($status === 'archived') {
|
||||
$query->archived();
|
||||
} elseif ($status === 'active') {
|
||||
$query->active()->where('is_active', true);
|
||||
if ($status === 'active') {
|
||||
$query->where('is_active', true);
|
||||
} elseif ($status === 'paused') {
|
||||
$query->active()->where('is_active', false);
|
||||
} else {
|
||||
// По умолчанию: все не архивированные (active + paused)
|
||||
$query->active();
|
||||
$query->where('is_active', false);
|
||||
}
|
||||
// default → no extra filter
|
||||
|
||||
// Поиск по name и signal_identifier
|
||||
if ($search = $request->query('search')) {
|
||||
@@ -111,11 +107,11 @@ class ProjectController extends Controller
|
||||
return response()->json(['data' => new ProjectResource($project)]);
|
||||
}
|
||||
|
||||
/** DELETE /api/projects/{id} — soft-archive (sets archived_at, is_active=false) */
|
||||
/** DELETE /api/projects/{id} — hard delete (guard по сделкам: 422 если есть сделки) */
|
||||
public function destroy(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$project = Project::where('tenant_id', $request->user()->tenant_id)->findOrFail($id);
|
||||
$this->projects->archive($project);
|
||||
$this->projects->delete($project);
|
||||
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
@@ -139,7 +135,7 @@ class ProjectController extends Controller
|
||||
return response()->json(['data' => new ProjectResource($project->fresh())]);
|
||||
}
|
||||
|
||||
/** POST /api/projects/bulk — batch pause/resume/archive/update_regions/update_days/update_limit */
|
||||
/** POST /api/projects/bulk — batch pause/resume/delete/update_regions/update_days/update_limit */
|
||||
public function bulk(BulkProjectActionRequest $request): JsonResponse
|
||||
{
|
||||
$tenantId = $request->user()->tenant_id;
|
||||
|
||||
@@ -6,11 +6,13 @@ namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\OutboundWebhookSubscription;
|
||||
use App\Support\WebhookUrlGuard;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
@@ -53,6 +55,16 @@ class WebhookSettingsController extends Controller
|
||||
'target_url' => ['required', 'string', 'url', 'max:2048', 'starts_with:https://'],
|
||||
]);
|
||||
|
||||
// SSRF-гард на сохранении: не даём записать URL во внутреннюю/служебную
|
||||
// сеть — тогда любой будущий потребитель (test() + будущая outbound-доставка
|
||||
// событий) читает из БД только безопасные адреса. NB: будущая доставка
|
||||
// обязана ВДОБАВОК звать WebhookUrlGuard перед отправкой (защита от
|
||||
// DNS-rebinding: хост сохранён публичным, позже переразрешается в приватный).
|
||||
$blockReason = WebhookUrlGuard::blockReason($validated['target_url']);
|
||||
if ($blockReason !== null) {
|
||||
throw ValidationException::withMessages(['target_url' => [$blockReason]]);
|
||||
}
|
||||
|
||||
$sub = $this->currentSubscription($request);
|
||||
$plainSecret = null;
|
||||
|
||||
@@ -95,14 +107,25 @@ class WebhookSettingsController extends Controller
|
||||
], Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
// SSRF-гард: target_url задаёт админ тенанта; блокируем адреса во
|
||||
// внутренней/зарезервированной сети (cloud-metadata 169.254.169.254,
|
||||
// loopback, RFC1918), которые https://-валидация на сохранении не ловит.
|
||||
$blockReason = WebhookUrlGuard::blockReason($sub->target_url);
|
||||
if ($blockReason !== null) {
|
||||
return response()->json([
|
||||
'ok' => false,
|
||||
'status' => null,
|
||||
'message' => $blockReason,
|
||||
], Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
$testPayload = [
|
||||
'event' => 'webhook.test',
|
||||
'sent_at' => now()->toIso8601String(),
|
||||
'message' => 'Тестовая доставка webhook от Лидерра.',
|
||||
];
|
||||
|
||||
// MVP: unsigned connectivity-проверка. SSRF-харднинг (блок приватных
|
||||
// IP) — пост-MVP security-review; URL уже ограничен https:// валидацией.
|
||||
// Unsigned connectivity-проверка (HMAC-подписанная доставка — отдельный эпик).
|
||||
try {
|
||||
$response = Http::timeout(10)
|
||||
->withHeaders(['X-Webhook-Event' => 'webhook.test'])
|
||||
|
||||
@@ -20,7 +20,7 @@ class BulkProjectActionRequest extends FormRequest
|
||||
|
||||
$rules = [
|
||||
'action' => ['required', Rule::in([
|
||||
'pause', 'resume', 'archive',
|
||||
'pause', 'resume', 'delete',
|
||||
'update_regions', 'update_days', 'update_limit',
|
||||
])],
|
||||
'ids' => ['nullable', 'array', 'max:500'],
|
||||
@@ -28,7 +28,7 @@ class BulkProjectActionRequest extends FormRequest
|
||||
'scope' => ['nullable', 'array'],
|
||||
'scope.filter' => ['nullable', 'array'],
|
||||
'scope.filter.signal_type' => ['nullable', 'string', Rule::in(['site', 'call', 'sms'])],
|
||||
'scope.filter.status' => ['nullable', 'string', Rule::in(['active', 'paused', 'archived'])],
|
||||
'scope.filter.status' => ['nullable', 'string', Rule::in(['active', 'paused'])],
|
||||
'scope.filter.search' => ['nullable', 'string', 'max:255'],
|
||||
];
|
||||
|
||||
|
||||
@@ -13,9 +13,6 @@ class ProjectResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
/** @var Project $project */
|
||||
$project = $this->resource;
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
@@ -28,7 +25,6 @@ class ProjectResource extends JsonResource
|
||||
'delivered_today' => $this->delivered_today,
|
||||
'delivered_in_month' => $this->delivered_in_month,
|
||||
'is_active' => $this->is_active,
|
||||
'archived_at' => $project->archived_at?->toIso8601String(),
|
||||
'region_mask' => $this->region_mask,
|
||||
'region_mode' => $this->region_mode,
|
||||
'regions' => $this->regions,
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Jobs\Supplier;
|
||||
|
||||
use App\Models\SupplierProject;
|
||||
use App\Services\Supplier\SupplierPortalClient;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Удаление/пере-синк доноров у поставщика после удаления Лидерра-проекта.
|
||||
*
|
||||
* Для каждого supplier_project S (донора), к которому был привязан удалённый проект:
|
||||
* - остались другие потребители (project_supplier_links) → донор нужен другим клиентам:
|
||||
* НЕ удаляем у поставщика, пере-синкаем агрегат (SyncSupplierProjectsJob).
|
||||
* - потребителей не осталось → удаляем у поставщика (deleteProject) + локальную запись S.
|
||||
*
|
||||
* Spec: docs/superpowers/specs/2026-05-21-project-delete-dedup-errors-design.md §Решение 2.
|
||||
*/
|
||||
class DeleteSupplierProjectJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public int $tries = 3;
|
||||
|
||||
public int $backoff = 60;
|
||||
|
||||
public const string DB_CONNECTION = 'pgsql_supplier';
|
||||
|
||||
/** @param array<int,int> $supplierProjectIds */
|
||||
public function __construct(public array $supplierProjectIds) {}
|
||||
|
||||
public function handle(SupplierPortalClient $client): void
|
||||
{
|
||||
$needsResync = false;
|
||||
|
||||
foreach ($this->supplierProjectIds as $id) {
|
||||
$sp = SupplierProject::on(self::DB_CONNECTION)->find($id);
|
||||
if ($sp === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$remaining = DB::connection(self::DB_CONNECTION)
|
||||
->table('project_supplier_links')
|
||||
->where('supplier_project_id', $id)
|
||||
->count();
|
||||
|
||||
if ($remaining > 0) {
|
||||
$needsResync = true;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($sp->supplier_external_id !== null && $sp->supplier_external_id !== '') {
|
||||
try {
|
||||
$client->deleteProject((int) $sp->supplier_external_id);
|
||||
} catch (Throwable $e) {
|
||||
Log::warning('supplier.delete_donor_failed', [
|
||||
'supplier_project_id' => $id, 'error' => $e->getMessage(),
|
||||
]);
|
||||
throw $e; // retry the job
|
||||
}
|
||||
}
|
||||
|
||||
$sp->delete();
|
||||
}
|
||||
|
||||
if ($needsResync) {
|
||||
SyncSupplierProjectsJob::dispatch();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,21 +36,26 @@ use Throwable;
|
||||
* Daily 18:00 МСК cron-job: синхронизирует supplier_projects с поставщиком crm.bp-gr.ru
|
||||
* (расписание перенесено 20:30 → 18:00, см. routes/console.php).
|
||||
*
|
||||
* Алгоритм (Plan 3 Task 5 — per-subject grouping):
|
||||
* 1. Загрузить активные Лидерра-projects (is_active=true, archived_at IS NULL).
|
||||
* 2. Развернуть каждый в группы (signal_type, identifier, subject_code):
|
||||
* - subjects = project.regions (1..89); пусто → одна группа subject_code=null («Вся РФ»).
|
||||
* - identifier = buildUniqueKey() (site/call → signal_identifier; sms B2 → sender+keyword; B3 → sender).
|
||||
* Алгоритм (план 3 Task 5 → переработан: one-group-per-identifier):
|
||||
* 1. Загрузить активные Лидерра-projects (is_active=true).
|
||||
* 2. Сгруппировать по (signal_type, identifier) — БЕЗ subject_code:
|
||||
* - identifier = buildUniqueKeyAgnostic() (site/call → signal_identifier; sms+keyword → sender+keyword; sms → sender).
|
||||
* - platforms = resolvePlatforms() (site/call → B1+B2+B3; sms+keyword → B2+B3; sms → B3).
|
||||
* - merged_regions = union(project.regions) по всем проектам группы.
|
||||
* Если хотя бы один проект имеет regions=[] («Вся РФ»), merged_regions=[].
|
||||
* 3. Для каждой группы:
|
||||
* - eligible-today проекты группы (workday-маска на завтра).
|
||||
* - order = computeOrder($eligibleLimits); workdays = union; tag / regions из subject.
|
||||
* - Найти существующие supplier_projects (unique_key, subject_code):
|
||||
* - Нет → saveProjectMultiFlag → 3 id → upsert supplier_projects.
|
||||
* - Есть → updateProject каждого (R6: один лимит).
|
||||
* - Pivot: для каждого Лидерра-проекта × каждого supplier_project → INSERT ... ON CONFLICT DO NOTHING.
|
||||
* - order = computeOrder($eligibleLimits); workdays = union.
|
||||
* - tag = name региона если один, иначе «РФ».
|
||||
* - Найти существующие supplier_projects (unique_key, signal_type, platform) — без subject_code-фильтра:
|
||||
* - Нет → saveProjectMultiFlag → [platform → id] → upsert supplier_projects (subject_code=null).
|
||||
* - Есть → partial-set recovery + updateProject каждого с актуальными regions/limit.
|
||||
* - Pivot: project × supplier_project → INSERT ... ON CONFLICT DO NOTHING (subject_code=null).
|
||||
* 4. Failure-handling (Auth/Transient/Client/Window/TierEscalated), time-budget cutoff — сохранены.
|
||||
*
|
||||
* Портальное ограничение: один identifier = одна группа B1/B2/B3 (status=Doubles на дублирование).
|
||||
* Поэтому все регионы проекта передаются одним списком — portal фильтрует оба одновременно.
|
||||
*
|
||||
* NOTE про connection: Eloquent::on('pgsql_supplier') для cross-tenant видимости.
|
||||
*
|
||||
* Spec:
|
||||
@@ -81,13 +86,13 @@ class SyncSupplierProjectsJob implements ShouldQueue
|
||||
/** @var Collection<int, Project> $projects */
|
||||
$projects = Project::on(self::DB_CONNECTION)
|
||||
->where('is_active', true)
|
||||
->whereNull('archived_at')
|
||||
->orderBy('id')
|
||||
->get();
|
||||
|
||||
// 2. Expand into groups (signal_type, identifier, subject_code)
|
||||
// group key => [ 'signal_type', 'identifier', 'subject_code', 'platforms', 'projects' => [...] ]
|
||||
/** @var array<string, array{signal_type: string, identifier: string, subject_code: int|null, platforms: list<string>, projects: list<Project>}> $groups */
|
||||
// 2. Group by (signal_type, identifier) — no subject_code split.
|
||||
// Portal constraint: one identifier = one B1/B2/B3 group (status=Doubles on duplicate name).
|
||||
// group key => [ 'signal_type', 'identifier', 'merged_regions', 'platforms', 'projects' => [...] ]
|
||||
/** @var array<string, array{signal_type: string, identifier: string, merged_regions: list<int>, has_all_russia: bool, platforms: list<string>, projects: list<Project>}> $groups */
|
||||
$groups = [];
|
||||
|
||||
foreach ($projects as $project) {
|
||||
@@ -95,25 +100,33 @@ class SyncSupplierProjectsJob implements ShouldQueue
|
||||
if ($platforms === []) {
|
||||
continue;
|
||||
}
|
||||
// For sms, identifier depends on whether B2 is in platforms (keyword-aware)
|
||||
// We use the B2 key as identifier when B2 is present (sms+keyword), else B3 key (sender only)
|
||||
$identifier = SupplierProjectGrouping::buildUniqueKeyAgnostic($project);
|
||||
|
||||
$subjects = SupplierProjectGrouping::subjectsOf($project);
|
||||
|
||||
foreach ($subjects as $subjectCode) {
|
||||
$key = $project->signal_type.'|'.$identifier.'|'.($subjectCode ?? 'null');
|
||||
if (! isset($groups[$key])) {
|
||||
$groups[$key] = [
|
||||
'signal_type' => (string) $project->signal_type,
|
||||
'identifier' => $identifier,
|
||||
'subject_code' => $subjectCode,
|
||||
'platforms' => $platforms,
|
||||
'projects' => [],
|
||||
];
|
||||
}
|
||||
$groups[$key]['projects'][] = $project;
|
||||
$key = $project->signal_type.'|'.$identifier;
|
||||
if (! isset($groups[$key])) {
|
||||
$groups[$key] = [
|
||||
'signal_type' => (string) $project->signal_type,
|
||||
'identifier' => $identifier,
|
||||
'merged_regions' => [],
|
||||
'has_all_russia' => false,
|
||||
'platforms' => $platforms,
|
||||
'projects' => [],
|
||||
];
|
||||
}
|
||||
// Merge regions — union across all projects in this group.
|
||||
// If any project has empty regions ("Вся РФ"), the whole group becomes "Вся РФ".
|
||||
if (! $groups[$key]['has_all_russia']) {
|
||||
$projectRegions = array_map('intval', (array) ($project->regions ?? []));
|
||||
if ($projectRegions === []) {
|
||||
$groups[$key]['has_all_russia'] = true;
|
||||
$groups[$key]['merged_regions'] = [];
|
||||
} else {
|
||||
$groups[$key]['merged_regions'] = array_values(array_unique(
|
||||
array_merge($groups[$key]['merged_regions'], $projectRegions)
|
||||
));
|
||||
}
|
||||
}
|
||||
$groups[$key]['projects'][] = $project;
|
||||
}
|
||||
|
||||
// 3. Sync each group
|
||||
@@ -168,13 +181,12 @@ class SyncSupplierProjectsJob implements ShouldQueue
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{signal_type: string, identifier: string, subject_code: int|null, platforms: list<string>, projects: list<Project>} $group
|
||||
* @param array{signal_type: string, identifier: string, merged_regions: list<int>, has_all_russia: bool, platforms: list<string>, projects: list<Project>} $group
|
||||
*/
|
||||
private function syncGroup(array $group): void
|
||||
{
|
||||
$signalType = $group['signal_type'];
|
||||
$identifier = $group['identifier'];
|
||||
$subjectCode = $group['subject_code'];
|
||||
$platforms = $group['platforms'];
|
||||
|
||||
/** @var list<Project> $groupProjects */
|
||||
@@ -198,6 +210,10 @@ class SyncSupplierProjectsJob implements ShouldQueue
|
||||
$eligibleLimits = array_map(fn (Project $p) => (int) $p->daily_limit_target, $eligible);
|
||||
$order = SupplierQuotaAllocator::computeOrder($eligibleLimits);
|
||||
|
||||
// Split the group order across platforms so Σ per-platform == order. The portal does
|
||||
// NOT divide (verified live 2026-05-21) — the full order on each B = order ×N overspend.
|
||||
$shares = SupplierQuotaAllocator::distributeForPlatform($order, $platforms);
|
||||
|
||||
$workdaysUnion = [];
|
||||
foreach ($eligible as $p) {
|
||||
foreach ($this->bitmaskToList((int) $p->delivery_days_mask, 7) as $d) {
|
||||
@@ -207,41 +223,41 @@ class SyncSupplierProjectsJob implements ShouldQueue
|
||||
sort($workdaysUnion);
|
||||
$workdays = $workdaysUnion;
|
||||
|
||||
// Tag and regions from subject
|
||||
$tag = $subjectCode !== null ? (RussianRegions::CODE_TO_NAME[$subjectCode] ?? (string) $subjectCode) : 'РФ';
|
||||
$regions = $subjectCode !== null ? [$subjectCode] : [];
|
||||
// Portal constraint: one identifier = one B1/B2/B3 group — pass all regions as a single list.
|
||||
$allRegions = $group['merged_regions'];
|
||||
sort($allRegions);
|
||||
// count=0 → all-Russia; count=1 → named region; count>1 → merged → 'РФ'
|
||||
$tag = count($allRegions) === 1
|
||||
? (RussianRegions::CODE_TO_NAME[$allRegions[0]] ?? (string) $allRegions[0])
|
||||
: 'РФ';
|
||||
|
||||
// Find existing supplier_projects for this group
|
||||
// Find existing supplier_projects for this group (no subject_code filter)
|
||||
$existingSps = SupplierProject::on(self::DB_CONNECTION)
|
||||
->where('unique_key', $identifier)
|
||||
->where('signal_type', $signalType)
|
||||
->when(
|
||||
$subjectCode !== null,
|
||||
fn ($q) => $q->where('subject_code', $subjectCode),
|
||||
fn ($q) => $q->whereNull('subject_code'),
|
||||
)
|
||||
->whereIn('platform', $platforms)
|
||||
->get();
|
||||
|
||||
if ($existingSps->isEmpty()) {
|
||||
// Create path: saveProjectMultiFlag → [platform => external_id]
|
||||
$dto = new SupplierProjectDto(
|
||||
platform: $platforms[0],
|
||||
signalType: $signalType,
|
||||
uniqueKey: $identifier,
|
||||
limit: $order,
|
||||
workdays: $workdays,
|
||||
regions: $regions,
|
||||
regionsReverse: false,
|
||||
status: 'active',
|
||||
tag: $tag,
|
||||
platforms: $platforms,
|
||||
);
|
||||
|
||||
$idMap = $this->client->saveProjectMultiFlag($dto);
|
||||
|
||||
// Upsert supplier_projects rows (one per platform)
|
||||
// Create path: one save PER platform with that platform's divided share
|
||||
// (single-flag save → exactly one rt-project, reliable id via listProjects match).
|
||||
// Throws propagate to handle() catch (failover-counter); rows persisted for earlier
|
||||
// platforms before a throw are recovered next run via the missing-set recovery below.
|
||||
foreach ($platforms as $platform) {
|
||||
$dto = new SupplierProjectDto(
|
||||
platform: $platform,
|
||||
signalType: $signalType,
|
||||
uniqueKey: $identifier,
|
||||
limit: $shares[$platform] ?? 0,
|
||||
workdays: $workdays,
|
||||
regions: $allRegions,
|
||||
regionsReverse: false,
|
||||
status: 'active',
|
||||
tag: $tag,
|
||||
platforms: [$platform],
|
||||
);
|
||||
|
||||
$idMap = $this->client->saveProjectMultiFlag($dto);
|
||||
$externalId = $idMap[$platform] ?? null;
|
||||
if ($externalId === null) {
|
||||
continue;
|
||||
@@ -251,11 +267,11 @@ class SyncSupplierProjectsJob implements ShouldQueue
|
||||
'platform' => $platform,
|
||||
'signal_type' => $signalType,
|
||||
'unique_key' => $identifier,
|
||||
'subject_code' => $subjectCode,
|
||||
'subject_code' => null,
|
||||
'supplier_external_id' => (string) $externalId,
|
||||
'current_limit' => $order,
|
||||
'current_limit' => $shares[$platform] ?? 0,
|
||||
'current_workdays' => $workdays,
|
||||
'current_regions' => $regions,
|
||||
'current_regions' => $allRegions,
|
||||
'sync_status' => 'ok',
|
||||
'last_synced_at' => now(),
|
||||
]);
|
||||
@@ -270,6 +286,50 @@ class SyncSupplierProjectsJob implements ShouldQueue
|
||||
$existingSps->push($sp);
|
||||
}
|
||||
} else {
|
||||
// External-deletion recovery: донор мог быть удалён на портале → external_id
|
||||
// в нашей БД мёртв, updateProject его молча no-op'ит. Сверяемся со списком живых
|
||||
// проектов портала и пересоздаём недостающих in-place (НЕ удаляя записи — на них
|
||||
// могут висеть лиды/списания). Throws пропагируют в outer handle() catch
|
||||
// (SupplierAuth/Transient/Client) — failover-counter semantics сохраняется.
|
||||
$livePortalIds = collect($this->client->listProjects())
|
||||
->map(fn ($p) => (string) ($p['id'] ?? ''))
|
||||
->filter()
|
||||
->all();
|
||||
|
||||
$deadSps = $existingSps->filter(
|
||||
fn (SupplierProject $sp) => $sp->supplier_external_id !== null
|
||||
&& ! in_array((string) $sp->supplier_external_id, $livePortalIds, true)
|
||||
);
|
||||
|
||||
if ($deadSps->isNotEmpty()) {
|
||||
foreach ($deadSps as $sp) {
|
||||
$recreateDto = new SupplierProjectDto(
|
||||
platform: $sp->platform,
|
||||
signalType: $signalType,
|
||||
uniqueKey: $identifier,
|
||||
limit: $shares[$sp->platform] ?? 0,
|
||||
workdays: $workdays,
|
||||
regions: $allRegions,
|
||||
regionsReverse: false,
|
||||
status: 'active',
|
||||
tag: $tag,
|
||||
platforms: [$sp->platform],
|
||||
);
|
||||
|
||||
$recreatedIdMap = $this->client->saveProjectMultiFlag($recreateDto);
|
||||
$newId = $recreatedIdMap[$sp->platform] ?? null;
|
||||
if ($newId !== null) {
|
||||
$sp->forceFill(['supplier_external_id' => (string) $newId])->save();
|
||||
SupplierSyncLog::on(self::DB_CONNECTION)->create([
|
||||
'supplier_project_id' => $sp->id,
|
||||
'action' => 'create',
|
||||
'http_status' => 200,
|
||||
'created_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fix #3 (review-followup): partial-set recovery — если предыдущий run создал
|
||||
// не все platforms (e.g. B1+B2 OK, B3 escalated), re-attempt missing via multi-flag
|
||||
// save с platforms=$missingPlatforms. Throws пропагируют в outer handle() catch
|
||||
@@ -278,22 +338,21 @@ class SyncSupplierProjectsJob implements ShouldQueue
|
||||
$missingPlatforms = array_values(array_diff($platforms, $existingPlatforms));
|
||||
|
||||
if ($missingPlatforms !== []) {
|
||||
$missingDto = new SupplierProjectDto(
|
||||
platform: $missingPlatforms[0],
|
||||
signalType: $signalType,
|
||||
uniqueKey: $identifier,
|
||||
limit: $order,
|
||||
workdays: $workdays,
|
||||
regions: $regions,
|
||||
regionsReverse: false,
|
||||
status: 'active',
|
||||
tag: $tag,
|
||||
platforms: $missingPlatforms,
|
||||
);
|
||||
|
||||
$missingIdMap = $this->client->saveProjectMultiFlag($missingDto);
|
||||
|
||||
foreach ($missingPlatforms as $platform) {
|
||||
$missingDto = new SupplierProjectDto(
|
||||
platform: $platform,
|
||||
signalType: $signalType,
|
||||
uniqueKey: $identifier,
|
||||
limit: $shares[$platform] ?? 0,
|
||||
workdays: $workdays,
|
||||
regions: $allRegions,
|
||||
regionsReverse: false,
|
||||
status: 'active',
|
||||
tag: $tag,
|
||||
platforms: [$platform],
|
||||
);
|
||||
|
||||
$missingIdMap = $this->client->saveProjectMultiFlag($missingDto);
|
||||
$externalId = $missingIdMap[$platform] ?? null;
|
||||
if ($externalId === null) {
|
||||
continue;
|
||||
@@ -302,11 +361,11 @@ class SyncSupplierProjectsJob implements ShouldQueue
|
||||
'platform' => $platform,
|
||||
'signal_type' => $signalType,
|
||||
'unique_key' => $identifier,
|
||||
'subject_code' => $subjectCode,
|
||||
'subject_code' => null,
|
||||
'supplier_external_id' => (string) $externalId,
|
||||
'current_limit' => $order,
|
||||
'current_limit' => $shares[$platform] ?? 0,
|
||||
'current_workdays' => $workdays,
|
||||
'current_regions' => $regions,
|
||||
'current_regions' => $allRegions,
|
||||
'sync_status' => 'ok',
|
||||
'last_synced_at' => now(),
|
||||
]);
|
||||
@@ -320,9 +379,9 @@ class SyncSupplierProjectsJob implements ShouldQueue
|
||||
}
|
||||
}
|
||||
|
||||
// Fix #2 (review-followup): per-platform DTO в update-loop, чтобы portal получал
|
||||
// правильные srcrt/srcbl/srcmt для конкретной редактируемой строки (не first()
|
||||
// из mixed-platform existing set). R6 one shared limit/regions сохраняется.
|
||||
// per-platform DTO в update-loop: portal получает правильные srcrt/srcbl/srcmt для
|
||||
// конкретной строки + её долю лимита ($shares), чтобы Σ по площадкам == order
|
||||
// (а не order на каждой). Regions/workdays общие для группы.
|
||||
foreach ($existingSps as $sp) {
|
||||
if ($sp->supplier_external_id === null) {
|
||||
continue;
|
||||
@@ -331,9 +390,9 @@ class SyncSupplierProjectsJob implements ShouldQueue
|
||||
platform: $sp->platform,
|
||||
signalType: $signalType,
|
||||
uniqueKey: $identifier,
|
||||
limit: $order,
|
||||
limit: $shares[$sp->platform] ?? 0,
|
||||
workdays: $workdays,
|
||||
regions: $regions,
|
||||
regions: $allRegions,
|
||||
regionsReverse: false,
|
||||
status: 'active',
|
||||
tag: $tag,
|
||||
@@ -341,9 +400,9 @@ class SyncSupplierProjectsJob implements ShouldQueue
|
||||
);
|
||||
$this->channel->updateProject((int) $sp->supplier_external_id, $perPlatformDto);
|
||||
$sp->forceFill([
|
||||
'current_limit' => $order,
|
||||
'current_limit' => $shares[$sp->platform] ?? 0,
|
||||
'current_workdays' => $workdays,
|
||||
'current_regions' => $regions,
|
||||
'current_regions' => $allRegions,
|
||||
'sync_status' => 'ok',
|
||||
'last_synced_at' => now(),
|
||||
])->save();
|
||||
@@ -364,8 +423,7 @@ class SyncSupplierProjectsJob implements ShouldQueue
|
||||
'project_id' => $lp->id,
|
||||
'supplier_project_id' => $sp->id,
|
||||
'platform' => $sp->platform,
|
||||
// @phpstan-ignore-next-line property.notFound — subject_code is in $fillable/casts, IDE stubs lag
|
||||
'subject_code' => $sp->subject_code,
|
||||
'subject_code' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -375,7 +433,7 @@ class SyncSupplierProjectsJob implements ShouldQueue
|
||||
* Log failure for a group (before any supplier_project is created/updated we don't have sp id,
|
||||
* so we look up existing or skip — best-effort audit).
|
||||
*
|
||||
* @param array{signal_type: string, identifier: string, subject_code: int|null, platforms: list<string>, projects: list<Project>} $group
|
||||
* @param array{signal_type: string, identifier: string, merged_regions: list<int>, has_all_russia: bool, platforms: list<string>, projects: list<Project>} $group
|
||||
*/
|
||||
private function logGroupFailure(array $group, Throwable $e): void
|
||||
{
|
||||
@@ -384,15 +442,10 @@ class SyncSupplierProjectsJob implements ShouldQueue
|
||||
$httpStatus = $e->httpStatus;
|
||||
}
|
||||
|
||||
// Find any existing sp row for the group to link log entry
|
||||
// Find any existing sp row for the group to link log entry (no subject_code filter)
|
||||
$sp = SupplierProject::on(self::DB_CONNECTION)
|
||||
->where('unique_key', $group['identifier'])
|
||||
->where('signal_type', $group['signal_type'])
|
||||
->when(
|
||||
$group['subject_code'] !== null,
|
||||
fn ($q) => $q->where('subject_code', $group['subject_code']),
|
||||
fn ($q) => $q->whereNull('subject_code'),
|
||||
)
|
||||
->first();
|
||||
|
||||
if ($sp !== null) {
|
||||
|
||||
@@ -14,6 +14,7 @@ use App\Services\Supplier\Dto\SupplierProjectDto;
|
||||
use App\Services\Supplier\SupplierExportMode;
|
||||
use App\Services\Supplier\SupplierPortalClient;
|
||||
use App\Services\Supplier\SupplierProjectGrouping;
|
||||
use App\Services\Supplier\SupplierQuotaAllocator;
|
||||
use App\Support\RussianRegions;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
@@ -60,11 +61,23 @@ class SyncSupplierProjectJob implements ShouldQueue
|
||||
/** @var array<int, int> */
|
||||
public array $backoff = [15, 60, 300];
|
||||
|
||||
/**
|
||||
* BYPASSRLS-роль crm_supplier_worker для всех DB-операций (как у всех supplier-flow
|
||||
* джобов: SyncSupplierProjectsJob/DeleteSupplierProjectJob/CsvReconcileJob/…).
|
||||
*
|
||||
* Джоб запускается из очереди, где SetTenantContext-прослойка не отрабатывает и
|
||||
* app.current_tenant_id GUC не установлен. Под обычной ролью crm_app_user первый же
|
||||
* SELECT по projects падает 42704 (unrecognized configuration parameter
|
||||
* "app.current_tenant_id"). На dev не всплывало — там DB_USERNAME=postgres (superuser,
|
||||
* RLS обходится). Plan 3 Task 3 learning.
|
||||
*/
|
||||
public const DB_CONNECTION = 'pgsql_supplier';
|
||||
|
||||
public function __construct(public int $projectId) {}
|
||||
|
||||
public function handle(SupplierProjectChannel $channel): void
|
||||
{
|
||||
$project = Project::find($this->projectId);
|
||||
$project = Project::on(self::DB_CONNECTION)->find($this->projectId);
|
||||
|
||||
if ($project === null) {
|
||||
Log::warning("SyncSupplierProjectJob: project {$this->projectId} not found — skipping");
|
||||
@@ -92,179 +105,158 @@ class SyncSupplierProjectJob implements ShouldQueue
|
||||
return;
|
||||
}
|
||||
|
||||
$subjects = SupplierProjectGrouping::subjectsOf($project);
|
||||
$identifier = SupplierProjectGrouping::buildUniqueKey($project, $platforms[0]);
|
||||
|
||||
foreach ($subjects as $subject) {
|
||||
// Use first platform for key (site/call → identifier; sms → B2/B3 key)
|
||||
$identifier = SupplierProjectGrouping::buildUniqueKey($project, $platforms[0]);
|
||||
// Portal constraint: one identifier = one B1/B2/B3 group (status=Doubles on duplicate name).
|
||||
// Pass all project regions as a single group — no per-subject split.
|
||||
$allRegions = array_map('intval', (array) ($project->regions ?? []));
|
||||
// count=0 → all-Russia; count=1 → named region; count>1 → merged → 'РФ'
|
||||
$tag = count($allRegions) === 1
|
||||
? (RussianRegions::CODE_TO_NAME[$allRegions[0]] ?? (string) $allRegions[0])
|
||||
: 'РФ';
|
||||
|
||||
$tag = $subject !== null
|
||||
? (RussianRegions::CODE_TO_NAME[$subject] ?? (string) $subject)
|
||||
: 'РФ';
|
||||
$regions = $subject !== null ? [$subject] : [];
|
||||
$workdays = $this->workdaysFromMask((int) $project->delivery_days_mask);
|
||||
|
||||
// Idempotency: existing supplier_projects for this (identifier, subject)?
|
||||
$existingSps = SupplierProject::query()
|
||||
->where('unique_key', $identifier)
|
||||
->where('signal_type', (string) $project->signal_type)
|
||||
->when(
|
||||
$subject !== null,
|
||||
fn ($q) => $q->where('subject_code', $subject),
|
||||
fn ($q) => $q->whereNull('subject_code'),
|
||||
)
|
||||
->whereIn('platform', $platforms)
|
||||
->get();
|
||||
// Split the limit across the platforms so Σ per-platform limits == project limit.
|
||||
// The portal does NOT divide (verified live 2026-05-21) — replicating the full limit
|
||||
// to B1/B2/B3 = order ×N (overspend). See SupplierQuotaAllocator::distributeForPlatform.
|
||||
$shares = SupplierQuotaAllocator::distributeForPlatform((int) $project->daily_limit_target, $platforms);
|
||||
|
||||
if ($existingSps->isEmpty()) {
|
||||
// Create path: saveProjectMultiFlag → [platform => external_id]
|
||||
$dto = new SupplierProjectDto(
|
||||
platform: $platforms[0],
|
||||
signalType: (string) $project->signal_type,
|
||||
uniqueKey: $identifier,
|
||||
limit: (int) $project->daily_limit_target,
|
||||
workdays: [1, 2, 3, 4, 5, 6, 7],
|
||||
regions: $regions,
|
||||
regionsReverse: false,
|
||||
status: 'active',
|
||||
tag: $tag,
|
||||
platforms: $platforms,
|
||||
);
|
||||
// Idempotency: find existing by identifier regardless of subject_code (any previous run).
|
||||
$existingSps = SupplierProject::on(self::DB_CONNECTION)
|
||||
->where('unique_key', $identifier)
|
||||
->where('signal_type', (string) $project->signal_type)
|
||||
->whereIn('platform', $platforms)
|
||||
->get();
|
||||
|
||||
try {
|
||||
$idMap = $client->saveProjectMultiFlag($dto);
|
||||
} catch (TierEscalatedException $e) {
|
||||
Log::info("SyncSupplierProjectJob: project {$project->id} subject={$subject} escalated to manual queue #{$e->queueRowId}");
|
||||
|
||||
continue;
|
||||
} catch (WindowDeferredException) {
|
||||
Log::info("SyncSupplierProjectJob: project {$project->id} subject={$subject} deferred by portal window");
|
||||
|
||||
continue;
|
||||
} catch (\Throwable $e) {
|
||||
// Online multi-flag save bypasses FailoverProjectChannel (tier-1 only by design,
|
||||
// см. class docblock). При transient/auth/client/network fail — log+skip; следующий
|
||||
// tries-retry (15s, 60s, 300s) или ночной SyncSupplierProjectsJob подберёт.
|
||||
Log::warning("SyncSupplierProjectJob: online multi-flag save failed for project {$project->id} subject={$subject} (".get_class($e).'): '.$e->getMessage());
|
||||
if ($existingSps->isEmpty()) {
|
||||
// Create path: one save PER platform with that platform's divided share
|
||||
// (single-flag save → exactly one rt-project, reliable id via listProjects match).
|
||||
$idMap = $this->createPerPlatform($client, $project, $identifier, $tag, $workdays, $allRegions, $shares, $platforms);
|
||||
|
||||
foreach ($platforms as $platform) {
|
||||
$externalId = $idMap[$platform] ?? null;
|
||||
if ($externalId === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($platforms as $platform) {
|
||||
$externalId = $idMap[$platform] ?? null;
|
||||
$sp = SupplierProject::on(self::DB_CONNECTION)->create([
|
||||
'platform' => $platform,
|
||||
'signal_type' => (string) $project->signal_type,
|
||||
'unique_key' => $identifier,
|
||||
'subject_code' => null,
|
||||
'supplier_external_id' => (string) $externalId,
|
||||
'current_limit' => $shares[$platform] ?? 0,
|
||||
'current_workdays' => $workdays,
|
||||
'current_regions' => $allRegions,
|
||||
'sync_status' => 'ok',
|
||||
'last_synced_at' => now(),
|
||||
]);
|
||||
|
||||
$existingSps->push($sp);
|
||||
}
|
||||
} else {
|
||||
// External-deletion recovery: донор мог быть удалён на портале (вручную или
|
||||
// прошлым hard-delete). Тогда external_id в нашей БД мёртв, а updateProject
|
||||
// такого id портал молча принимает (no-op) — донор не пересоздаётся. Поэтому
|
||||
// сверяемся со списком живых проектов портала и пересоздаём недостающих
|
||||
// in-place (НЕ удаляя записи — на supplier_project могут висеть лиды/списания).
|
||||
$livePortalIds = collect($client->listProjects())
|
||||
->map(fn ($p) => (string) ($p['id'] ?? ''))
|
||||
->filter()
|
||||
->all();
|
||||
|
||||
$deadSps = $existingSps->filter(
|
||||
fn (SupplierProject $sp) => $sp->supplier_external_id !== null
|
||||
&& ! in_array((string) $sp->supplier_external_id, $livePortalIds, true)
|
||||
);
|
||||
|
||||
if ($deadSps->isNotEmpty()) {
|
||||
$deadPlatforms = array_values($deadSps->pluck('platform')->all());
|
||||
$recreatedIdMap = $this->createPerPlatform($client, $project, $identifier, $tag, $workdays, $allRegions, $shares, $deadPlatforms);
|
||||
|
||||
foreach ($deadSps as $sp) {
|
||||
$newId = $recreatedIdMap[$sp->platform] ?? null;
|
||||
if ($newId !== null) {
|
||||
$sp->forceFill(['supplier_external_id' => (string) $newId])->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Partial-set recovery: если предыдущий run создал не все platforms.
|
||||
$existingPlatforms = $existingSps->pluck('platform')->all();
|
||||
$missingPlatforms = array_values(array_diff($platforms, $existingPlatforms));
|
||||
|
||||
if ($missingPlatforms !== []) {
|
||||
$missingIdMap = $this->createPerPlatform($client, $project, $identifier, $tag, $workdays, $allRegions, $shares, $missingPlatforms);
|
||||
|
||||
foreach ($missingPlatforms as $platform) {
|
||||
$externalId = $missingIdMap[$platform] ?? null;
|
||||
if ($externalId === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$sp = SupplierProject::create([
|
||||
$sp = SupplierProject::on(self::DB_CONNECTION)->create([
|
||||
'platform' => $platform,
|
||||
'signal_type' => (string) $project->signal_type,
|
||||
'unique_key' => $identifier,
|
||||
'subject_code' => $subject,
|
||||
'subject_code' => null,
|
||||
'supplier_external_id' => (string) $externalId,
|
||||
'current_limit' => (int) $project->daily_limit_target,
|
||||
'current_workdays' => [1, 2, 3, 4, 5, 6, 7],
|
||||
'current_regions' => $regions,
|
||||
'current_limit' => $shares[$platform] ?? 0,
|
||||
'current_workdays' => $workdays,
|
||||
'current_regions' => $allRegions,
|
||||
'sync_status' => 'ok',
|
||||
'last_synced_at' => now(),
|
||||
]);
|
||||
|
||||
$existingSps->push($sp);
|
||||
}
|
||||
} else {
|
||||
// Fix #3 (review-followup): partial-set recovery — если предыдущий run создал
|
||||
// не все platforms (e.g. B1+B2 OK, B3 escalated), re-attempt missing via
|
||||
// multi-flag save с platforms=$missingPlatforms (srcrt/srcbl/srcmt только missing).
|
||||
$existingPlatforms = $existingSps->pluck('platform')->all();
|
||||
$missingPlatforms = array_values(array_diff($platforms, $existingPlatforms));
|
||||
|
||||
if ($missingPlatforms !== []) {
|
||||
$missingDto = new SupplierProjectDto(
|
||||
platform: $missingPlatforms[0],
|
||||
signalType: (string) $project->signal_type,
|
||||
uniqueKey: $identifier,
|
||||
limit: (int) $project->daily_limit_target,
|
||||
workdays: [1, 2, 3, 4, 5, 6, 7],
|
||||
regions: $regions,
|
||||
regionsReverse: false,
|
||||
status: 'active',
|
||||
tag: $tag,
|
||||
platforms: $missingPlatforms,
|
||||
);
|
||||
|
||||
try {
|
||||
$missingIdMap = $client->saveProjectMultiFlag($missingDto);
|
||||
} catch (TierEscalatedException $e) {
|
||||
Log::info("SyncSupplierProjectJob: project {$project->id} subject={$subject} missing-platform re-attempt escalated #{$e->queueRowId}");
|
||||
$missingIdMap = [];
|
||||
} catch (WindowDeferredException) {
|
||||
Log::info("SyncSupplierProjectJob: project {$project->id} subject={$subject} missing-platform deferred by portal window");
|
||||
$missingIdMap = [];
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning("SyncSupplierProjectJob: missing-platform multi-flag failed for project {$project->id} subject={$subject}: ".$e->getMessage());
|
||||
$missingIdMap = [];
|
||||
}
|
||||
|
||||
foreach ($missingPlatforms as $platform) {
|
||||
$externalId = $missingIdMap[$platform] ?? null;
|
||||
if ($externalId === null) {
|
||||
continue;
|
||||
}
|
||||
$sp = SupplierProject::create([
|
||||
'platform' => $platform,
|
||||
'signal_type' => (string) $project->signal_type,
|
||||
'unique_key' => $identifier,
|
||||
'subject_code' => $subject,
|
||||
'supplier_external_id' => (string) $externalId,
|
||||
'current_limit' => (int) $project->daily_limit_target,
|
||||
'current_workdays' => [1, 2, 3, 4, 5, 6, 7],
|
||||
'current_regions' => $regions,
|
||||
'sync_status' => 'ok',
|
||||
'last_synced_at' => now(),
|
||||
]);
|
||||
$existingSps->push($sp);
|
||||
}
|
||||
}
|
||||
|
||||
// Fix #2 (review-followup): per-platform DTO в update-loop, чтобы portal
|
||||
// получал корректные srcrt/srcbl/srcmt флаги для конкретной редактируемой строки
|
||||
// (не первой из mixed-platform existing set). R6 one shared limit/regions сохраняется.
|
||||
foreach ($existingSps as $sp) {
|
||||
if ($sp->supplier_external_id === null) {
|
||||
continue;
|
||||
}
|
||||
$perPlatformDto = new SupplierProjectDto(
|
||||
platform: $sp->platform,
|
||||
signalType: (string) $project->signal_type,
|
||||
uniqueKey: $identifier,
|
||||
limit: (int) $project->daily_limit_target,
|
||||
workdays: [1, 2, 3, 4, 5, 6, 7],
|
||||
regions: $regions,
|
||||
regionsReverse: false,
|
||||
status: 'active',
|
||||
tag: $tag,
|
||||
platforms: [$sp->platform],
|
||||
);
|
||||
$channel->updateProject((int) $sp->supplier_external_id, $perPlatformDto);
|
||||
$sp->forceFill([
|
||||
'current_limit' => (int) $project->daily_limit_target,
|
||||
'current_regions' => $regions,
|
||||
'sync_status' => 'ok',
|
||||
'last_synced_at' => now(),
|
||||
])->save();
|
||||
}
|
||||
}
|
||||
|
||||
// Pivot: project × each supplier_project → ON CONFLICT DO NOTHING
|
||||
// Update existing supplier projects with current regions/limit.
|
||||
foreach ($existingSps as $sp) {
|
||||
DB::table('project_supplier_links')->insertOrIgnore([
|
||||
'project_id' => $project->id,
|
||||
'supplier_project_id' => $sp->id,
|
||||
'platform' => $sp->platform,
|
||||
// @phpstan-ignore-next-line property.notFound — subject_code in fillable/casts, IDE stubs lag
|
||||
'subject_code' => $sp->subject_code,
|
||||
]);
|
||||
if ($sp->supplier_external_id === null) {
|
||||
continue;
|
||||
}
|
||||
$perPlatformDto = new SupplierProjectDto(
|
||||
platform: $sp->platform,
|
||||
signalType: (string) $project->signal_type,
|
||||
uniqueKey: $identifier,
|
||||
limit: $shares[$sp->platform] ?? 0,
|
||||
workdays: $workdays,
|
||||
regions: $allRegions,
|
||||
regionsReverse: false,
|
||||
status: 'active',
|
||||
tag: $tag,
|
||||
platforms: [$sp->platform],
|
||||
);
|
||||
$channel->updateProject((int) $sp->supplier_external_id, $perPlatformDto);
|
||||
$sp->forceFill([
|
||||
'current_limit' => $shares[$sp->platform] ?? 0,
|
||||
'current_workdays' => $workdays,
|
||||
'current_regions' => $allRegions,
|
||||
'sync_status' => 'ok',
|
||||
'last_synced_at' => now(),
|
||||
])->save();
|
||||
}
|
||||
}
|
||||
|
||||
// Pivot: project × each supplier_project → ON CONFLICT DO NOTHING
|
||||
foreach ($existingSps as $sp) {
|
||||
DB::connection(self::DB_CONNECTION)->table('project_supplier_links')->insertOrIgnore([
|
||||
'project_id' => $project->id,
|
||||
'supplier_project_id' => $sp->id,
|
||||
'platform' => $sp->platform,
|
||||
'subject_code' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
// Mirror the link into the legacy FK columns (supplier_b{1,2,3}_project_id) so the
|
||||
// UI sync-status (ProjectResource → aggregateSyncStatus, which reads supplierB1/B2/B3)
|
||||
// reflects the synced stack in online mode too — online primarily uses the pivot.
|
||||
foreach ($existingSps as $sp) {
|
||||
$column = 'supplier_'.strtolower((string) $sp->platform).'_project_id';
|
||||
$project->{$column} = $sp->id;
|
||||
}
|
||||
$project->save();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
@@ -274,13 +266,14 @@ class SyncSupplierProjectJob implements ShouldQueue
|
||||
private function handleBatch(Project $project, SupplierProjectChannel $channel): void
|
||||
{
|
||||
$platforms = SupplierProjectGrouping::resolvePlatforms($project);
|
||||
$workdays = $this->workdaysFromMask((int) $project->delivery_days_mask);
|
||||
|
||||
foreach ($platforms as $platform) {
|
||||
$uniqueKey = SupplierProjectGrouping::buildUniqueKey($project, $platform);
|
||||
$column = 'supplier_'.strtolower($platform).'_project_id';
|
||||
|
||||
// Idempotency: local supplier_projects-запись уже есть?
|
||||
$existing = SupplierProject::query()
|
||||
$existing = SupplierProject::on(self::DB_CONNECTION)
|
||||
->where('platform', $platform)
|
||||
->where('signal_type', $project->signal_type)
|
||||
->where('unique_key', $uniqueKey)
|
||||
@@ -297,7 +290,7 @@ class SyncSupplierProjectJob implements ShouldQueue
|
||||
signalType: (string) $project->signal_type,
|
||||
uniqueKey: $uniqueKey,
|
||||
limit: 0,
|
||||
workdays: [1, 2, 3, 4, 5, 6, 7],
|
||||
workdays: $workdays,
|
||||
regions: [],
|
||||
regionsReverse: false,
|
||||
status: 'active',
|
||||
@@ -317,13 +310,13 @@ class SyncSupplierProjectJob implements ShouldQueue
|
||||
continue;
|
||||
}
|
||||
|
||||
$sp = SupplierProject::query()->create([
|
||||
$sp = SupplierProject::on(self::DB_CONNECTION)->create([
|
||||
'platform' => $platform,
|
||||
'signal_type' => $project->signal_type,
|
||||
'unique_key' => $uniqueKey,
|
||||
'supplier_external_id' => (string) $externalId,
|
||||
'current_limit' => 0,
|
||||
'current_workdays' => [1, 2, 3, 4, 5, 6, 7],
|
||||
'current_workdays' => $workdays,
|
||||
'current_regions' => null,
|
||||
'sync_status' => 'ok',
|
||||
]);
|
||||
@@ -333,4 +326,86 @@ class SyncSupplierProjectJob implements ShouldQueue
|
||||
|
||||
$project->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Создаёт проекты на портале ПО ОДНОМУ на платформу с её долей лимита ($shares).
|
||||
*
|
||||
* Один single-flag save = ровно один rt-проект → надёжный id через listProjects-матч.
|
||||
* Так per-platform лимит = доля (Σ == заказу), а не полный лимит на каждой площадке.
|
||||
* Per-platform tolerance: tier-escalation / window-defer / прочая ошибка одной площадки
|
||||
* не валит остальные — пропускаем, следующий run (или ночной батч) подберёт недостающее.
|
||||
*
|
||||
* @param array<string, int> $shares [platform => лимит площадки]
|
||||
* @param list<string> $platformsToCreate
|
||||
* @return array<string, int> [platform => external_id] для успешно созданных
|
||||
*/
|
||||
private function createPerPlatform(
|
||||
SupplierPortalClient $client,
|
||||
Project $project,
|
||||
string $identifier,
|
||||
string $tag,
|
||||
array $workdays,
|
||||
array $allRegions,
|
||||
array $shares,
|
||||
array $platformsToCreate,
|
||||
): array {
|
||||
$idMap = [];
|
||||
|
||||
foreach ($platformsToCreate as $platform) {
|
||||
$dto = new SupplierProjectDto(
|
||||
platform: $platform,
|
||||
signalType: (string) $project->signal_type,
|
||||
uniqueKey: $identifier,
|
||||
limit: $shares[$platform] ?? 0,
|
||||
workdays: $workdays,
|
||||
regions: $allRegions,
|
||||
regionsReverse: false,
|
||||
status: 'active',
|
||||
tag: $tag,
|
||||
platforms: [$platform],
|
||||
);
|
||||
|
||||
try {
|
||||
$result = $client->saveProjectMultiFlag($dto);
|
||||
} catch (TierEscalatedException $e) {
|
||||
Log::info("SyncSupplierProjectJob: project {$project->id} {$platform} escalated to manual queue #{$e->queueRowId}");
|
||||
|
||||
continue;
|
||||
} catch (WindowDeferredException) {
|
||||
Log::info("SyncSupplierProjectJob: project {$project->id} {$platform} deferred by portal window");
|
||||
|
||||
continue;
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning("SyncSupplierProjectJob: online per-platform save failed for project {$project->id} {$platform} (".get_class($e).'): '.$e->getMessage());
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($result[$platform])) {
|
||||
$idMap[$platform] = $result[$platform];
|
||||
}
|
||||
}
|
||||
|
||||
return $idMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bitmask → ISO weekday list. bit 0 = Mon (ISO 1) … bit 6 = Sun (ISO 7).
|
||||
*
|
||||
* Mirror of SyncSupplierProjectsJob::bitmaskToList(). Kept inline (not
|
||||
* extracted to a shared helper) to keep this fix surgical.
|
||||
*
|
||||
* @return list<int>
|
||||
*/
|
||||
private function workdaysFromMask(int $mask): array
|
||||
{
|
||||
$out = [];
|
||||
for ($i = 0; $i < 7; $i++) {
|
||||
if (($mask & (1 << $i)) !== 0) {
|
||||
$out[] = $i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,6 @@ class Project extends Model
|
||||
'tag',
|
||||
'type',
|
||||
'is_active',
|
||||
// Plan 5 Task 1 (schema v8.20): soft archive flow — lifecycle-state рядом с is_active.
|
||||
'archived_at',
|
||||
'daily_limit_target',
|
||||
'effective_daily_limit_today',
|
||||
'effective_limit_calculated_at',
|
||||
@@ -87,8 +85,6 @@ class Project extends Model
|
||||
'sms_senders' => 'array',
|
||||
'delivered_in_month' => 'integer',
|
||||
'delivered_today' => 'integer',
|
||||
// Plan 5 Task 1 (schema v8.20): soft archive.
|
||||
'archived_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -151,33 +147,6 @@ class Project extends Model
|
||||
return $query->where('signal_type', $signalType)->where('signal_identifier', $identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Не архивированные проекты (archived_at IS NULL).
|
||||
*
|
||||
* Внимание: scope не фильтрует is_active. Приостановленные (is_active=false)
|
||||
* проекты сюда попадают — это разные lifecycle-состояния. Если нужны только
|
||||
* «работающие» (не архив И не на паузе) — комбинируйте:
|
||||
* ->active()->where('is_active', true).
|
||||
*
|
||||
* @param Builder<Project> $query
|
||||
* @return Builder<Project>
|
||||
*/
|
||||
public function scopeActive(Builder $query): Builder
|
||||
{
|
||||
return $query->whereNull('archived_at');
|
||||
}
|
||||
|
||||
/**
|
||||
* Архивированные проекты (archived_at IS NOT NULL).
|
||||
*
|
||||
* @param Builder<Project> $query
|
||||
* @return Builder<Project>
|
||||
*/
|
||||
public function scopeArchived(Builder $query): Builder
|
||||
{
|
||||
return $query->whereNotNull('archived_at');
|
||||
}
|
||||
|
||||
/**
|
||||
* Все связанные SupplierProject из eager-loaded BelongsTo отношений.
|
||||
*
|
||||
|
||||
@@ -4,10 +4,12 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Project;
|
||||
|
||||
use App\Jobs\Supplier\DeleteSupplierProjectJob;
|
||||
use App\Jobs\SyncSupplierProjectJob;
|
||||
use App\Models\Project;
|
||||
use App\Models\Tenant;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ProjectService
|
||||
{
|
||||
@@ -19,7 +21,6 @@ class ProjectService
|
||||
$data['tenant_id'], $data['signal_type'],
|
||||
$data['delivered_today'], $data['delivered_in_month'],
|
||||
$data['supplier_b1_project_id'], $data['supplier_b2_project_id'], $data['supplier_b3_project_id'],
|
||||
$data['archived_at'],
|
||||
);
|
||||
|
||||
if (isset($data['daily_limit_target']) && $data['daily_limit_target'] < $project->delivered_today) {
|
||||
@@ -32,10 +33,26 @@ class ProjectService
|
||||
], 422));
|
||||
}
|
||||
|
||||
// Resync на смену любого источник-несущего поля — поставщику нужно знать актуальный домен/телефон/sms.
|
||||
// Resync на смену источник-несущих полей, регионов, лимита и дней недели —
|
||||
// поставщик должен видеть актуальные параметры сразу, не дожидаясь ночного батча.
|
||||
$needsResync = array_key_exists('sms_senders', $data)
|
||||
|| array_key_exists('sms_keyword', $data)
|
||||
|| array_key_exists('signal_identifier', $data);
|
||||
|| array_key_exists('signal_identifier', $data)
|
||||
|| array_key_exists('regions', $data)
|
||||
|| array_key_exists('daily_limit_target', $data)
|
||||
|| array_key_exists('delivery_days_mask', $data);
|
||||
|
||||
if (array_key_exists('signal_identifier', $data) || array_key_exists('sms_senders', $data) || array_key_exists('sms_keyword', $data)) {
|
||||
$this->assertSourceUnique($project->tenant_id, array_merge([
|
||||
'signal_type' => $project->signal_type,
|
||||
'signal_identifier' => $project->signal_identifier,
|
||||
'sms_senders' => $project->sms_senders,
|
||||
'sms_keyword' => $project->sms_keyword,
|
||||
], $data), exceptId: $project->id);
|
||||
}
|
||||
if (array_key_exists('name', $data)) {
|
||||
$this->assertNameUnique($project->tenant_id, (string) $data['name'], exceptId: $project->id);
|
||||
}
|
||||
|
||||
$project->update($data);
|
||||
|
||||
@@ -46,17 +63,26 @@ class ProjectService
|
||||
return $project->fresh();
|
||||
}
|
||||
|
||||
public function archive(Project $project): void
|
||||
public function delete(Project $project): void
|
||||
{
|
||||
if ($project->archived_at !== null) {
|
||||
$hasDeals = DB::table('deals')->where('project_id', $project->id)->exists();
|
||||
if ($hasDeals) {
|
||||
throw new HttpResponseException(response()->json([
|
||||
'message' => 'Project уже архивирован.',
|
||||
], 409));
|
||||
'errors' => ['project' => ['Нельзя удалить проект: по нему есть сделки. Поставьте приём на паузу, чтобы скрыть проект из работы.']],
|
||||
], 422));
|
||||
}
|
||||
|
||||
// Капчим доноров ДО удаления — pivot уйдёт каскадом.
|
||||
$supplierProjectIds = DB::table('project_supplier_links')
|
||||
->where('project_id', $project->id)
|
||||
->pluck('supplier_project_id')
|
||||
->all();
|
||||
|
||||
$project->delete(); // hard delete (Project без SoftDeletes); cascade чистит pivot + служебные.
|
||||
|
||||
if ($supplierProjectIds !== []) {
|
||||
DeleteSupplierProjectJob::dispatch(array_map('intval', $supplierProjectIds));
|
||||
}
|
||||
$project->update([
|
||||
'is_active' => false,
|
||||
'archived_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function triggerSync(Project $project): void
|
||||
@@ -79,9 +105,8 @@ class ProjectService
|
||||
}
|
||||
if (! empty($filter['status'])) {
|
||||
match ($filter['status']) {
|
||||
'active' => $query->where('is_active', true)->whereNull('archived_at'),
|
||||
'paused' => $query->where('is_active', false)->whereNull('archived_at'),
|
||||
'archived' => $query->whereNotNull('archived_at'),
|
||||
'active' => $query->where('is_active', true),
|
||||
'paused' => $query->where('is_active', false),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
@@ -104,7 +129,7 @@ class ProjectService
|
||||
return match ($action) {
|
||||
'pause' => $this->bulkSimpleUpdate($query, ['is_active' => false]),
|
||||
'resume' => $this->bulkSimpleUpdate($query, ['is_active' => true]),
|
||||
'archive' => $this->bulkSimpleUpdate($query, ['is_active' => false, 'archived_at' => now()]),
|
||||
'delete' => $this->bulkDelete($query),
|
||||
'update_regions' => $this->bulkUpdateRegions($query, $payload),
|
||||
'update_days' => $this->bulkUpdateDays($query, $payload),
|
||||
'update_limit' => $this->bulkUpdateLimit($query, $payload),
|
||||
@@ -118,6 +143,29 @@ class ProjectService
|
||||
return ['updated' => $updated, 'skipped' => [], 'warnings' => []];
|
||||
}
|
||||
|
||||
private function bulkDelete($query): array
|
||||
{
|
||||
$projects = (clone $query)->get(['id']);
|
||||
$deleted = 0;
|
||||
$skipped = [];
|
||||
|
||||
foreach ($projects as $p) {
|
||||
$model = Project::find($p->id);
|
||||
if ($model === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->delete($model);
|
||||
$deleted++;
|
||||
} catch (HttpResponseException) {
|
||||
$skipped[] = ['id' => $p->id, 'reason' => 'has_deals'];
|
||||
}
|
||||
}
|
||||
|
||||
return ['updated' => $deleted, 'skipped' => $skipped, 'warnings' => []];
|
||||
}
|
||||
|
||||
/**
|
||||
* Plan 6.5: субъект-уровневый bulk-edit `regions` INT[].
|
||||
*
|
||||
@@ -209,10 +257,60 @@ class ProjectService
|
||||
return ['updated' => $updated, 'skipped' => $skipped, 'warnings' => []];
|
||||
}
|
||||
|
||||
private function assertNameUnique(int $tenantId, string $name, ?int $exceptId = null): void
|
||||
{
|
||||
$q = Project::where('tenant_id', $tenantId)->where('name', $name);
|
||||
if ($exceptId !== null) {
|
||||
$q->where('id', '!=', $exceptId);
|
||||
}
|
||||
if ($q->exists()) {
|
||||
throw new HttpResponseException(response()->json([
|
||||
'errors' => ['name' => ['Проект с таким названием у вас уже есть. Выберите другое название.']],
|
||||
], 422));
|
||||
}
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $data */
|
||||
private function assertSourceUnique(int $tenantId, array $data, ?int $exceptId = null): void
|
||||
{
|
||||
$signalType = $data['signal_type'] ?? null;
|
||||
$q = Project::where('tenant_id', $tenantId)->where('signal_type', $signalType);
|
||||
if ($exceptId !== null) {
|
||||
$q->where('id', '!=', $exceptId);
|
||||
}
|
||||
|
||||
if (in_array($signalType, ['call', 'site'], true)) {
|
||||
$identifier = (string) ($data['signal_identifier'] ?? '');
|
||||
if ($identifier === '') {
|
||||
return;
|
||||
}
|
||||
$q->where('signal_identifier', $identifier);
|
||||
} elseif ($signalType === 'sms') {
|
||||
$senders = (array) ($data['sms_senders'] ?? []);
|
||||
$norm = collect($senders)->map(fn ($s) => mb_strtolower(trim((string) $s)))->sort()->values()->all();
|
||||
if ($norm === []) {
|
||||
return;
|
||||
}
|
||||
$keyword = $data['sms_keyword'] ?? null;
|
||||
$q->where('sms_keyword', $keyword)
|
||||
->whereJsonContains('sms_senders', $norm)
|
||||
->whereRaw('jsonb_array_length(sms_senders::jsonb) = ?', [count($norm)]);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
$existing = $q->first();
|
||||
if ($existing !== null) {
|
||||
throw new HttpResponseException(response()->json([
|
||||
'errors' => ['signal_identifier' => ["У вас уже есть проект с этим источником: «{$existing->name}»."]],
|
||||
], 422));
|
||||
}
|
||||
}
|
||||
|
||||
public function create(Tenant $tenant, array $data): Project
|
||||
{
|
||||
$limit = (int) ($tenant->limits['max_projects'] ?? 10);
|
||||
$current = Project::where('tenant_id', $tenant->id)->active()->count();
|
||||
$current = Project::where('tenant_id', $tenant->id)->count();
|
||||
if ($current >= $limit) {
|
||||
throw new HttpResponseException(response()->json([
|
||||
'message' => "Достигнут лимит проектов ({$limit}). Смените тариф.",
|
||||
@@ -226,6 +324,10 @@ class ProjectService
|
||||
// PhonePrefixService / LeadRouter, удаляются в Plan 6.5 после переключения читателей.
|
||||
$data['region_mask'] = 255;
|
||||
$data['region_mode'] = 'include';
|
||||
|
||||
$this->assertNameUnique($tenant->id, (string) $data['name']);
|
||||
$this->assertSourceUnique($tenant->id, $data);
|
||||
|
||||
$project = Project::create($data);
|
||||
|
||||
SyncSupplierProjectJob::dispatch($project->id);
|
||||
|
||||
@@ -9,6 +9,7 @@ use App\Exceptions\Supplier\SupplierClientException;
|
||||
use App\Exceptions\Supplier\SupplierTransientException;
|
||||
use App\Jobs\Supplier\RefreshSupplierSessionJob;
|
||||
use App\Services\Supplier\Dto\SupplierProjectDto;
|
||||
use App\Support\SupplierRegions;
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Http\Client\Factory as HttpFactory;
|
||||
@@ -112,7 +113,11 @@ class SupplierPortalClient
|
||||
$srcToPlatform = ['rt' => 'B1', 'bl' => 'B2', 'mt' => 'B3'];
|
||||
$out = [];
|
||||
foreach ($this->listProjects() as $p) {
|
||||
if (($p['name'] ?? null) !== $dto->uniqueKey || ($p['tag'] ?? null) !== $dto->tag) {
|
||||
// Real portal returns name='B1_<identifier>' and identifier in 'content'.
|
||||
// Test mocks omit 'content' and put identifier directly in 'name' — fall back to 'name'
|
||||
// when 'content' is absent so both shapes work.
|
||||
$identifier = $p['content'] ?? $p['name'] ?? null;
|
||||
if ($identifier !== $dto->uniqueKey || ($p['tag'] ?? null) !== $dto->tag) {
|
||||
continue;
|
||||
}
|
||||
$platform = $srcToPlatform[$p['src'] ?? ''] ?? null;
|
||||
@@ -473,7 +478,10 @@ class SupplierPortalClient
|
||||
'srcseg' => false,
|
||||
'limit' => $dto->limit,
|
||||
'workdays' => $workdays,
|
||||
'regions' => $dto->regions,
|
||||
// DTO несёт Лидерра-коды (конституционный порядок); поставщик ждёт
|
||||
// свои коды (ГИБДД). Без перевода уходил чужой регион (Красноярский 29
|
||||
// → Архангельск 29). См. App\Support\SupplierRegions.
|
||||
'regions' => SupplierRegions::mapToSupplier($dto->regions),
|
||||
'regions_reverse' => $dto->regionsReverse,
|
||||
'status' => $dto->status === 'active',
|
||||
'show' => true,
|
||||
|
||||
@@ -11,14 +11,19 @@ use Illuminate\Support\Collection;
|
||||
/**
|
||||
* Pure function: формула заказа у поставщика на (источник × субъект).
|
||||
*
|
||||
* Эпик миграции проектов (Plan 3): platform-split B1/B2/B3 удалён — портал
|
||||
* делит лимит сам (R6). Один лимит на группу eligible-клиентов:
|
||||
* Заказ группы eligible-клиентов:
|
||||
*
|
||||
* order = max(наибольший_лимит, ceil(Σ_лимитов / 3))
|
||||
*
|
||||
* ceil(Σ/3) — ёмкость шаринга (лид продаётся ≤3 раз).
|
||||
* ceil(Σ/3) — ёмкость шаринга (лид продаётся ≤3 раз клиентам Лидерры).
|
||||
* наиб — крупнейший клиент должен иметь шанс добрать.
|
||||
*
|
||||
* Этот `order` затем ДЕЛИТСЯ между площадками B1/B2/B3 через distributeForPlatform()
|
||||
* так, чтобы Σ per-platform лимитов == order. Портал НЕ делит сам: проверено вживую
|
||||
* 2026-05-21 (listProjects) — каждый B-проект честно набирает до своего лимита
|
||||
* независимо, поэтому одинаковый лимит на 3 площадках = заказ ×3 (переплата).
|
||||
* Plan 3 R6 («портал делит, verified 15→5») оказался ложным — split восстановлен.
|
||||
*
|
||||
* `allocate()` оставлен с прежней сигнатурой для временной совместимости
|
||||
* c SyncSupplierProjectsJob — внутри использует computeOrder, возвращает
|
||||
* DTO с одинаковым limit на любую platform/signalType.
|
||||
@@ -76,7 +81,7 @@ final class SupplierQuotaAllocator
|
||||
* ceil(Σ/3) — ёмкость шаринга (лид продаётся ≤3 раз).
|
||||
* наиб — крупнейший клиент должен иметь шанс добрать.
|
||||
*
|
||||
* Один лимит на группу; портал делит на B1/B2/B3 сам (R6 — наш split убран).
|
||||
* Возвращает заказ ГРУППЫ; деление между B1/B2/B3 — distributeForPlatform().
|
||||
*
|
||||
* @param array<int, int> $dailyLimits лимиты eligible-сегодня клиентов группы
|
||||
*/
|
||||
@@ -92,6 +97,40 @@ final class SupplierQuotaAllocator
|
||||
return max($max, (int) ceil($sum / 3));
|
||||
}
|
||||
|
||||
/**
|
||||
* Делит групповой заказ между площадками так, чтобы СУММА per-platform лимитов == order.
|
||||
*
|
||||
* Largest-remainder: каждой площадке floor(order/N), затем по +1 первым (order mod N)
|
||||
* площадкам в порядке списка. Сумма всегда точно равна order — ни переплаты, ни недобора.
|
||||
*
|
||||
* Восстанавливает поведение, удалённое в Plan 3 R6 (ошибочное допущение «портал делит сам»).
|
||||
* Портал НЕ делит — каждый B-проект набирает до своего лимита независимо; одинаковый
|
||||
* лимит на N площадках = заказ ×N (переплата). Verified live 2026-05-21.
|
||||
*
|
||||
* @param list<string> $platforms площадки в каноническом порядке (B1<B2<B3)
|
||||
* @return array<string, int> [platform => лимит этой площадки]
|
||||
*/
|
||||
public static function distributeForPlatform(int $order, array $platforms): array
|
||||
{
|
||||
$count = count($platforms);
|
||||
if ($count === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$order = max(0, $order);
|
||||
$base = intdiv($order, $count);
|
||||
$remainder = $order % $count;
|
||||
|
||||
$shares = [];
|
||||
$i = 0;
|
||||
foreach ($platforms as $platform) {
|
||||
$shares[$platform] = $base + ($i < $remainder ? 1 : 0);
|
||||
$i++;
|
||||
}
|
||||
|
||||
return $shares;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, mixed> $arrays
|
||||
* @return array<int, int>
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Перевод кодов регионов: Лидерра → поставщик crm.bp-gr.ru.
|
||||
*
|
||||
* Лидерра нумерует субъекты РФ по конституционному порядку (ст. 65), 1..89 —
|
||||
* см. {@see RussianRegions}: Красноярский край = 29, Архангельская обл. = 35.
|
||||
* Поставщик нумерует по автомобильным кодам (ГИБДД): Красноярский = 24,
|
||||
* Архангельская = 29. Без перевода Sync отправлял Лидерра-код «как есть»
|
||||
* (`regions => [29]` для Красноярского), а поставщик понимал его как СВОЙ № 29 =
|
||||
* Архангельск → у поставщика выбирался ЧУЖОЙ регион. На dev не всплывало —
|
||||
* проверяли на «вся РФ» (пустой regions).
|
||||
*
|
||||
* Карта построена сверкой имён {@see RussianRegions::CODE_TO_NAME} ↔ live-дерево
|
||||
* регионов формы «Добавить проект» поставщика (recon 2026-05-21: node-key="id",
|
||||
* 79 субъектов-листьев). Все 79 кодов поставщика покрыты (биекция на 79).
|
||||
*
|
||||
* 10 субъектов Лидерры поставщик НЕ предлагает (нет в дереве) — их коды
|
||||
* отбрасываются при переводе (с warning'ом): Московская обл. (56),
|
||||
* Ленинградская обл. (53), Крым (13), Севастополь (84), ДНР (6), ЛНР (14),
|
||||
* Запорожская (43), Херсонская (79), Ненецкий АО (86), Ямало-Ненецкий АО (89).
|
||||
* Если у проекта это был ЕДИНСТВЕННЫЙ регион — у поставщика проект окажется без
|
||||
* георфильтра (вся РФ). Это ограничение покрытия поставщика, не баг перевода.
|
||||
*/
|
||||
final class SupplierRegions
|
||||
{
|
||||
/**
|
||||
* Лидерра-код (конституционный 1..89) => код поставщика (ГИБДД).
|
||||
*
|
||||
* @var array<int, int>
|
||||
*/
|
||||
public const LIDERRA_TO_SUPPLIER = [
|
||||
// Республики
|
||||
1 => 1, // Республика Адыгея
|
||||
2 => 4, // Республика Алтай
|
||||
3 => 2, // Республика Башкортостан
|
||||
4 => 3, // Республика Бурятия
|
||||
5 => 5, // Республика Дагестан
|
||||
7 => 6, // Республика Ингушетия
|
||||
8 => 7, // Кабардино-Балкарская Республика
|
||||
9 => 8, // Республика Калмыкия
|
||||
10 => 9, // Карачаево-Черкесская Республика
|
||||
11 => 10, // Республика Карелия
|
||||
12 => 11, // Республика Коми
|
||||
15 => 12, // Республика Марий Эл
|
||||
16 => 13, // Республика Мордовия
|
||||
17 => 14, // Республика Саха (Якутия)
|
||||
18 => 15, // Республика Северная Осетия — Алания
|
||||
19 => 16, // Республика Татарстан
|
||||
20 => 17, // Республика Тыва
|
||||
21 => 18, // Удмуртская Республика
|
||||
22 => 19, // Республика Хакасия
|
||||
23 => 20, // Чеченская Республика
|
||||
24 => 21, // Чувашская Республика
|
||||
// Края
|
||||
25 => 22, // Алтайский край
|
||||
26 => 75, // Забайкальский край
|
||||
27 => 41, // Камчатский край
|
||||
28 => 23, // Краснодарский край
|
||||
29 => 24, // Красноярский край
|
||||
30 => 59, // Пермский край
|
||||
31 => 25, // Приморский край
|
||||
32 => 26, // Ставропольский край
|
||||
33 => 27, // Хабаровский край
|
||||
// Области
|
||||
34 => 28, // Амурская область
|
||||
35 => 29, // Архангельская область
|
||||
36 => 30, // Астраханская область
|
||||
37 => 31, // Белгородская область
|
||||
38 => 32, // Брянская область
|
||||
39 => 33, // Владимирская область
|
||||
40 => 34, // Волгоградская область
|
||||
41 => 35, // Вологодская область
|
||||
42 => 36, // Воронежская область
|
||||
44 => 37, // Ивановская область
|
||||
45 => 38, // Иркутская область
|
||||
46 => 39, // Калининградская область
|
||||
47 => 40, // Калужская область
|
||||
48 => 42, // Кемеровская область
|
||||
49 => 43, // Кировская область
|
||||
50 => 44, // Костромская область
|
||||
51 => 45, // Курганская область
|
||||
52 => 46, // Курская область
|
||||
54 => 48, // Липецкая область
|
||||
55 => 49, // Магаданская область
|
||||
57 => 51, // Мурманская область
|
||||
58 => 52, // Нижегородская область
|
||||
59 => 53, // Новгородская область
|
||||
60 => 54, // Новосибирская область
|
||||
61 => 55, // Омская область
|
||||
62 => 56, // Оренбургская область
|
||||
63 => 57, // Орловская область
|
||||
64 => 58, // Пензенская область
|
||||
65 => 60, // Псковская область
|
||||
66 => 61, // Ростовская область
|
||||
67 => 62, // Рязанская область
|
||||
68 => 63, // Самарская область
|
||||
69 => 64, // Саратовская область
|
||||
70 => 65, // Сахалинская область
|
||||
71 => 66, // Свердловская область
|
||||
72 => 67, // Смоленская область
|
||||
73 => 68, // Тамбовская область
|
||||
74 => 69, // Тверская область
|
||||
75 => 70, // Томская область
|
||||
76 => 71, // Тульская область
|
||||
77 => 72, // Тюменская область
|
||||
78 => 73, // Ульяновская область
|
||||
80 => 74, // Челябинская область
|
||||
81 => 76, // Ярославская область
|
||||
// Города федерального значения
|
||||
82 => 77, // Москва
|
||||
83 => 78, // Санкт-Петербург
|
||||
// Автономная область / округа
|
||||
85 => 79, // Еврейская автономная область
|
||||
87 => 86, // Ханты-Мансийский автономный округ — Югра
|
||||
88 => 87, // Чукотский автономный округ
|
||||
];
|
||||
|
||||
/**
|
||||
* Переводит Лидерра-коды регионов в коды поставщика. Неизвестные (нет у
|
||||
* поставщика) отбрасываются с warning'ом; sentinel 0 («Вся РФ») игнорируется.
|
||||
* Результат — уникальные коды поставщика по возрастанию.
|
||||
*
|
||||
* @param list<int>|array<int|string, int|string> $liderraCodes
|
||||
* @return list<int>
|
||||
*/
|
||||
public static function mapToSupplier(array $liderraCodes): array
|
||||
{
|
||||
$out = [];
|
||||
$dropped = [];
|
||||
|
||||
foreach ($liderraCodes as $code) {
|
||||
$code = (int) $code;
|
||||
if ($code === 0) {
|
||||
continue; // sentinel «Вся РФ»
|
||||
}
|
||||
if (isset(self::LIDERRA_TO_SUPPLIER[$code])) {
|
||||
$out[self::LIDERRA_TO_SUPPLIER[$code]] = true;
|
||||
} else {
|
||||
$dropped[] = $code;
|
||||
}
|
||||
}
|
||||
|
||||
if ($dropped !== []) {
|
||||
Log::warning('supplier.regions.unmapped', [
|
||||
'liderra_codes' => $dropped,
|
||||
'note' => 'supplier does not offer these subjects — geo-filter dropped for them',
|
||||
]);
|
||||
}
|
||||
|
||||
$codes = array_keys($out);
|
||||
sort($codes);
|
||||
|
||||
return $codes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
/**
|
||||
* SSRF-гард для исходящих webhook-URL.
|
||||
*
|
||||
* Webhook target_url задаёт авторизованный админ тенанта. Без проверки он может
|
||||
* указать внутренний адрес (`https://169.254.169.254/` cloud-metadata,
|
||||
* `https://127.0.0.1/`, `https://10.0.0.0/8`) и через кнопку «тест» получить
|
||||
* ответ внутренней службы (SSRF + info-leak). starts_with:https:// этого не ловит.
|
||||
*
|
||||
* Политика: блокируем, только если хост РАЗРЕШАЕТСЯ в приватный/зарезервированный
|
||||
* IP. Неразрешимый хост (NXDOMAIN) — не SSRF-вектор, пропускаем (реальный запрос
|
||||
* упадёт сам). Проверяются все A/AAAA-записи (защита от hostname→private).
|
||||
*/
|
||||
final class WebhookUrlGuard
|
||||
{
|
||||
/**
|
||||
* @return string|null Причина блокировки (человекочитаемая) или null, если адрес безопасен.
|
||||
*/
|
||||
public static function blockReason(string $url): ?string
|
||||
{
|
||||
$host = parse_url($url, PHP_URL_HOST);
|
||||
if (! is_string($host) || $host === '') {
|
||||
return 'Некорректный URL webhook.';
|
||||
}
|
||||
$host = trim($host, '[]'); // снять скобки IPv6-литерала
|
||||
|
||||
foreach (self::resolve($host) as $ip) {
|
||||
if (! self::isPublicIp($ip)) {
|
||||
return 'URL webhook ведёт во внутреннюю/зарезервированную сеть — запрещено.';
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @return list<string> Все IP, в которые разрешается хост (пусто, если не разрешается). */
|
||||
private static function resolve(string $host): array
|
||||
{
|
||||
if (filter_var($host, FILTER_VALIDATE_IP) !== false) {
|
||||
return [$host]; // IP-литерал — без DNS
|
||||
}
|
||||
|
||||
$ips = [];
|
||||
$v4 = gethostbynamel($host);
|
||||
if (is_array($v4)) {
|
||||
$ips = array_merge($ips, $v4);
|
||||
}
|
||||
$aaaa = @dns_get_record($host, DNS_AAAA);
|
||||
if (is_array($aaaa)) {
|
||||
foreach ($aaaa as $rec) {
|
||||
if (isset($rec['ipv6']) && is_string($rec['ipv6'])) {
|
||||
$ips[] = $rec['ipv6'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array_values(array_unique($ips));
|
||||
}
|
||||
|
||||
private static function isPublicIp(string $ip): bool
|
||||
{
|
||||
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
|
||||
return filter_var(
|
||||
$ip,
|
||||
FILTER_VALIDATE_IP,
|
||||
FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
|
||||
) !== false;
|
||||
}
|
||||
|
||||
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) {
|
||||
$lower = strtolower($ip);
|
||||
// loopback / unspecified
|
||||
if ($lower === '::1' || $lower === '::') {
|
||||
return false;
|
||||
}
|
||||
// link-local fe80::/10
|
||||
if (preg_match('/^fe[89ab]/', $lower) === 1) {
|
||||
return false;
|
||||
}
|
||||
// unique-local fc00::/7
|
||||
if ($lower[0] === 'f' && in_array($lower[1], ['c', 'd'], true)) {
|
||||
return false;
|
||||
}
|
||||
// IPv4-mapped ::ffff:a.b.c.d — проверить встроенный IPv4
|
||||
if (str_contains($lower, '::ffff:')) {
|
||||
$v4 = substr($lower, (int) strrpos($lower, ':') + 1);
|
||||
if (filter_var($v4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
|
||||
return self::isPublicIp($v4);
|
||||
}
|
||||
}
|
||||
|
||||
return filter_var(
|
||||
$ip,
|
||||
FILTER_VALIDATE_IP,
|
||||
FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
|
||||
) !== false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+17
-1
@@ -2,9 +2,12 @@
|
||||
|
||||
use App\Http\Middleware\EnsureSaasAdmin;
|
||||
use App\Http\Middleware\SetTenantContext;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Foundation\Configuration\Exceptions;
|
||||
use Illuminate\Foundation\Configuration\Middleware;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
return Application::configure(basePath: dirname(__DIR__))
|
||||
->withRouting(
|
||||
@@ -30,5 +33,18 @@ return Application::configure(basePath: dirname(__DIR__))
|
||||
]);
|
||||
})
|
||||
->withExceptions(function (Exceptions $exceptions): void {
|
||||
//
|
||||
$exceptions->render(function (QueryException $e, Request $request) {
|
||||
Log::error('db.query_exception', [
|
||||
'message' => $e->getMessage(),
|
||||
'sql' => $e->getSql(),
|
||||
'path' => $request->path(),
|
||||
]);
|
||||
if ($request->expectsJson()) {
|
||||
return response()->json([
|
||||
'message' => 'Не удалось сохранить. Проверьте данные или попробуйте ещё раз.',
|
||||
], 422);
|
||||
}
|
||||
|
||||
return null; // default render for non-JSON
|
||||
});
|
||||
})->create();
|
||||
|
||||
+8
-1
@@ -18,6 +18,7 @@
|
||||
"require-dev": {
|
||||
"barryvdh/laravel-ide-helper": "*",
|
||||
"deptrac/deptrac": "^4.6",
|
||||
"driftingly/rector-laravel": "^2.3",
|
||||
"fakerphp/faker": "^1.23",
|
||||
"infection/infection": "^0.32.7",
|
||||
"larastan/larastan": "*",
|
||||
@@ -27,8 +28,10 @@
|
||||
"laravel/pint": "^1.29",
|
||||
"mockery/mockery": "^1.6",
|
||||
"nunomaduro/collision": "^8.6",
|
||||
"nunomaduro/phpinsights": "*",
|
||||
"pestphp/pest": "^4.7",
|
||||
"pestphp/pest-plugin-laravel": "^4.1",
|
||||
"rector/rector": "^2.4",
|
||||
"roave/security-advisories": "dev-latest"
|
||||
},
|
||||
"autoload": {
|
||||
@@ -64,6 +67,9 @@
|
||||
"pint:test": "@php vendor/bin/pint --test",
|
||||
"test:parallel": "@php vendor/bin/pest --parallel --recreate-databases",
|
||||
"stan": "@php vendor/bin/phpstan analyse --memory-limit=512M",
|
||||
"rector": "@php vendor/bin/rector process --dry-run",
|
||||
"rector:fix": "@php vendor/bin/rector process",
|
||||
"insights": "@php artisan insights --no-interaction",
|
||||
"mutation": "@php vendor/bin/infection --threads=2 --min-msi=50",
|
||||
"audit-offline": "@composer audit --locked",
|
||||
"demo:seed": "@php artisan db:seed --class=DemoSeeder --force",
|
||||
@@ -102,7 +108,8 @@
|
||||
"allow-plugins": {
|
||||
"pestphp/pest-plugin": true,
|
||||
"php-http/discovery": true,
|
||||
"infection/extension-installer": true
|
||||
"infection/extension-installer": true,
|
||||
"dealerdirect/phpcodesniffer-composer-installer": true
|
||||
}
|
||||
},
|
||||
"minimum-stability": "stable",
|
||||
|
||||
Generated
+2162
-1
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use NunoMaduro\PhpInsights\Domain\Insights\ForbiddenDefineFunctions;
|
||||
use NunoMaduro\PhpInsights\Domain\Insights\ForbiddenFinalClasses;
|
||||
use NunoMaduro\PhpInsights\Domain\Insights\ForbiddenNormalClasses;
|
||||
use NunoMaduro\PhpInsights\Domain\Insights\ForbiddenPrivateMethods;
|
||||
use NunoMaduro\PhpInsights\Domain\Insights\ForbiddenTraits;
|
||||
use NunoMaduro\PhpInsights\Domain\Insights\SyntaxCheck;
|
||||
use NunoMaduro\PhpInsights\Domain\Metrics\Architecture\Classes;
|
||||
use SlevomatCodingStandard\Sniffs\Commenting\UselessFunctionDocCommentSniff;
|
||||
use SlevomatCodingStandard\Sniffs\Namespaces\AlphabeticallySortedUsesSniff;
|
||||
use SlevomatCodingStandard\Sniffs\TypeHints\DeclareStrictTypesSniff;
|
||||
use SlevomatCodingStandard\Sniffs\TypeHints\DisallowMixedTypeHintSniff;
|
||||
use SlevomatCodingStandard\Sniffs\TypeHints\ParameterTypeHintSniff;
|
||||
use SlevomatCodingStandard\Sniffs\TypeHints\PropertyTypeHintSniff;
|
||||
use SlevomatCodingStandard\Sniffs\TypeHints\ReturnTypeHintSniff;
|
||||
|
||||
return [
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Default Preset
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This option controls the default preset that will be used by PHP Insights
|
||||
| to make your code reliable, simple, and clean. However, you can always
|
||||
| adjust the `Metrics` and `Insights` below in this configuration file.
|
||||
|
|
||||
| Supported: "default", "laravel", "symfony", "magento2", "drupal", "wordpress"
|
||||
|
|
||||
*/
|
||||
|
||||
'preset' => 'laravel',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| IDE
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This options allow to add hyperlinks in your terminal to quickly open
|
||||
| files in your favorite IDE while browsing your PhpInsights report.
|
||||
|
|
||||
| Supported: "textmate", "macvim", "emacs", "sublime", "phpstorm",
|
||||
| "atom", "vscode".
|
||||
|
|
||||
| If you have another IDE that is not in this list but which provide an
|
||||
| url-handler, you could fill this config with a pattern like this:
|
||||
|
|
||||
| myide://open?url=file://%f&line=%l
|
||||
|
|
||||
*/
|
||||
|
||||
'ide' => null,
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Configuration
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Here you may adjust all the various `Insights` that will be used by PHP
|
||||
| Insights. You can either add, remove or configure `Insights`. Keep in
|
||||
| mind, that all added `Insights` must belong to a specific `Metric`.
|
||||
|
|
||||
*/
|
||||
|
||||
'exclude' => [
|
||||
// 'path/to/directory-or-file'
|
||||
],
|
||||
|
||||
'add' => [
|
||||
Classes::class => [
|
||||
ForbiddenFinalClasses::class,
|
||||
],
|
||||
],
|
||||
|
||||
'remove' => [
|
||||
// SyntaxCheck спавнит дочерний `php -l` процесс — на native-Windows возвращает
|
||||
// не-JSON и крашит PHP Insights (A1 backend-tooling, 20.05.2026). Избыточен:
|
||||
// синтаксис ловят Pint / Larastan / сам PHP. Стиль — владелец Pint (BT4, ADR-013).
|
||||
SyntaxCheck::class,
|
||||
AlphabeticallySortedUsesSniff::class,
|
||||
DeclareStrictTypesSniff::class,
|
||||
DisallowMixedTypeHintSniff::class,
|
||||
ForbiddenDefineFunctions::class,
|
||||
ForbiddenNormalClasses::class,
|
||||
ForbiddenTraits::class,
|
||||
ParameterTypeHintSniff::class,
|
||||
PropertyTypeHintSniff::class,
|
||||
ReturnTypeHintSniff::class,
|
||||
UselessFunctionDocCommentSniff::class,
|
||||
],
|
||||
|
||||
'config' => [
|
||||
ForbiddenPrivateMethods::class => [
|
||||
'title' => 'The usage of private methods is not idiomatic in Laravel.',
|
||||
],
|
||||
],
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Requirements
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Here you may define a level you want to reach per `Insights` category.
|
||||
| When a score is lower than the minimum level defined, then an error
|
||||
| code will be returned. This is optional and individually defined.
|
||||
|
|
||||
*/
|
||||
|
||||
'requirements' => [
|
||||
// Anti-regression floors из baseline 20.05.2026 (Code 80 / Complexity 81 /
|
||||
// Architecture 75). Чуть ниже текущих — гейт ловит деградацию, не текущий долг.
|
||||
// Style НЕ гейтим — владелец стиля Pint (BT4, ADR-013). Security-check off —
|
||||
// дублирует roave/security-advisories + composer audit.
|
||||
'min-quality' => 78,
|
||||
'min-complexity' => 79,
|
||||
'min-architecture' => 73,
|
||||
'disable-security-check' => true,
|
||||
],
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Threads
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Here you may adjust how many threads (core) PHPInsights can use to perform
|
||||
| the analysis. This is optional, don't provide it and the tool will guess
|
||||
| the max core number available. It accepts null value or integer > 0.
|
||||
|
|
||||
*/
|
||||
|
||||
'threads' => null,
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Timeout
|
||||
|--------------------------------------------------------------------------
|
||||
| Here you may adjust the timeout (in seconds) for PHPInsights to run before
|
||||
| a ProcessTimedOutException is thrown.
|
||||
| This accepts an int > 0. Default is 60 seconds, which is the default value
|
||||
| of Symfony's setTimeout function.
|
||||
|
|
||||
*/
|
||||
|
||||
'timeout' => 60,
|
||||
];
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
DB::statement('ALTER TABLE projects DROP COLUMN IF EXISTS archived_at');
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
DB::statement('ALTER TABLE projects ADD COLUMN archived_at TIMESTAMPTZ NULL');
|
||||
}
|
||||
};
|
||||
+152
-2
@@ -258,6 +258,90 @@ parameters:
|
||||
count: 1
|
||||
path: app/Services/Supplier/SupplierProjectGrouping.php
|
||||
|
||||
-
|
||||
message: '#^Class NunoMaduro\\PhpInsights\\Domain\\Insights\\ForbiddenDefineFunctions not found\.$#'
|
||||
identifier: class.notFound
|
||||
count: 1
|
||||
path: config/insights.php
|
||||
|
||||
-
|
||||
message: '#^Class NunoMaduro\\PhpInsights\\Domain\\Insights\\ForbiddenFinalClasses not found\.$#'
|
||||
identifier: class.notFound
|
||||
count: 1
|
||||
path: config/insights.php
|
||||
|
||||
-
|
||||
message: '#^Class NunoMaduro\\PhpInsights\\Domain\\Insights\\ForbiddenNormalClasses not found\.$#'
|
||||
identifier: class.notFound
|
||||
count: 1
|
||||
path: config/insights.php
|
||||
|
||||
-
|
||||
message: '#^Class NunoMaduro\\PhpInsights\\Domain\\Insights\\ForbiddenPrivateMethods not found\.$#'
|
||||
identifier: class.notFound
|
||||
count: 1
|
||||
path: config/insights.php
|
||||
|
||||
-
|
||||
message: '#^Class NunoMaduro\\PhpInsights\\Domain\\Insights\\ForbiddenTraits not found\.$#'
|
||||
identifier: class.notFound
|
||||
count: 1
|
||||
path: config/insights.php
|
||||
|
||||
-
|
||||
message: '#^Class NunoMaduro\\PhpInsights\\Domain\\Insights\\SyntaxCheck not found\.$#'
|
||||
identifier: class.notFound
|
||||
count: 1
|
||||
path: config/insights.php
|
||||
|
||||
-
|
||||
message: '#^Class NunoMaduro\\PhpInsights\\Domain\\Metrics\\Architecture\\Classes not found\.$#'
|
||||
identifier: class.notFound
|
||||
count: 1
|
||||
path: config/insights.php
|
||||
|
||||
-
|
||||
message: '#^Class SlevomatCodingStandard\\Sniffs\\Commenting\\UselessFunctionDocCommentSniff not found\.$#'
|
||||
identifier: class.notFound
|
||||
count: 1
|
||||
path: config/insights.php
|
||||
|
||||
-
|
||||
message: '#^Class SlevomatCodingStandard\\Sniffs\\Namespaces\\AlphabeticallySortedUsesSniff not found\.$#'
|
||||
identifier: class.notFound
|
||||
count: 1
|
||||
path: config/insights.php
|
||||
|
||||
-
|
||||
message: '#^Class SlevomatCodingStandard\\Sniffs\\TypeHints\\DeclareStrictTypesSniff not found\.$#'
|
||||
identifier: class.notFound
|
||||
count: 1
|
||||
path: config/insights.php
|
||||
|
||||
-
|
||||
message: '#^Class SlevomatCodingStandard\\Sniffs\\TypeHints\\DisallowMixedTypeHintSniff not found\.$#'
|
||||
identifier: class.notFound
|
||||
count: 1
|
||||
path: config/insights.php
|
||||
|
||||
-
|
||||
message: '#^Class SlevomatCodingStandard\\Sniffs\\TypeHints\\ParameterTypeHintSniff not found\.$#'
|
||||
identifier: class.notFound
|
||||
count: 1
|
||||
path: config/insights.php
|
||||
|
||||
-
|
||||
message: '#^Class SlevomatCodingStandard\\Sniffs\\TypeHints\\PropertyTypeHintSniff not found\.$#'
|
||||
identifier: class.notFound
|
||||
count: 1
|
||||
path: config/insights.php
|
||||
|
||||
-
|
||||
message: '#^Class SlevomatCodingStandard\\Sniffs\\TypeHints\\ReturnTypeHintSniff not found\.$#'
|
||||
identifier: class.notFound
|
||||
count: 1
|
||||
path: config/insights.php
|
||||
|
||||
-
|
||||
message: '#^Return type \(array\<string, mixed\>\) of method Database\\Factories\\BalanceTransactionFactory\:\:definition\(\) should be compatible with return type \(array\<model property of App\\Models\\BalanceTransaction, mixed\>\) of method Illuminate\\Database\\Eloquent\\Factories\\Factory\<App\\Models\\BalanceTransaction\>\:\:definition\(\)$#'
|
||||
identifier: method.childReturnType
|
||||
@@ -348,6 +432,24 @@ parameters:
|
||||
count: 3
|
||||
path: tests/Feature/Admin/AdminSuppliersControllerTest.php
|
||||
|
||||
-
|
||||
message: '#^Call to an undefined method Pest\\PendingCalls\\TestCall\:\:actingAs\(\)\.$#'
|
||||
identifier: method.notFound
|
||||
count: 3
|
||||
path: tests/Feature/Admin/SupplierExportModeEndpointTest.php
|
||||
|
||||
-
|
||||
message: '#^Call to an undefined method Pest\\PendingCalls\\TestCall\:\:getJson\(\)\.$#'
|
||||
identifier: method.notFound
|
||||
count: 1
|
||||
path: tests/Feature/Admin/SupplierExportModeEndpointTest.php
|
||||
|
||||
-
|
||||
message: '#^Call to an undefined method Pest\\PendingCalls\\TestCall\:\:postJson\(\)\.$#'
|
||||
identifier: method.notFound
|
||||
count: 2
|
||||
path: tests/Feature/Admin/SupplierExportModeEndpointTest.php
|
||||
|
||||
-
|
||||
message: '#^Call to an undefined method Pest\\PendingCalls\\TestCall\:\:actingAs\(\)\.$#'
|
||||
identifier: method.notFound
|
||||
@@ -366,6 +468,24 @@ parameters:
|
||||
count: 2
|
||||
path: tests/Feature/Admin/SupplierManualQueueTest.php
|
||||
|
||||
-
|
||||
message: '#^Call to an undefined method Pest\\PendingCalls\\TestCall\:\:actingAs\(\)\.$#'
|
||||
identifier: method.notFound
|
||||
count: 5
|
||||
path: tests/Feature/Admin/SupplierProjectsAdminTest.php
|
||||
|
||||
-
|
||||
message: '#^Call to an undefined method Pest\\PendingCalls\\TestCall\:\:getJson\(\)\.$#'
|
||||
identifier: method.notFound
|
||||
count: 2
|
||||
path: tests/Feature/Admin/SupplierProjectsAdminTest.php
|
||||
|
||||
-
|
||||
message: '#^Call to an undefined method Pest\\PendingCalls\\TestCall\:\:postJson\(\)\.$#'
|
||||
identifier: method.notFound
|
||||
count: 4
|
||||
path: tests/Feature/Admin/SupplierProjectsAdminTest.php
|
||||
|
||||
-
|
||||
message: '#^Call to an undefined method Pest\\PendingCalls\\TestCall\:\:getJson\(\)\.$#'
|
||||
identifier: method.notFound
|
||||
@@ -1533,9 +1653,15 @@ parameters:
|
||||
-
|
||||
message: '#^Call to an undefined method Pest\\PendingCalls\\TestCall\:\:actingAs\(\)\.$#'
|
||||
identifier: method.notFound
|
||||
count: 12
|
||||
count: 14
|
||||
path: tests/Feature/Plan5/Projects/ProjectsUpdateTest.php
|
||||
|
||||
-
|
||||
message: '#^Call to an undefined method Pest\\PendingCalls\\TestCall\:\:getJson\(\)\.$#'
|
||||
identifier: method.notFound
|
||||
count: 1
|
||||
path: tests/Feature/Project/QueryExceptionRenderTest.php
|
||||
|
||||
-
|
||||
message: '#^Access to an undefined property Pest\\PendingCalls\\TestCall\:\:\$tenant\.$#'
|
||||
identifier: property.notFound
|
||||
@@ -1812,6 +1938,12 @@ parameters:
|
||||
count: 2
|
||||
path: tests/Feature/Supplier/FailoverProjectChannelLiveSmokeTest.php
|
||||
|
||||
-
|
||||
message: '#^Call to an undefined method Mockery\\ExpectationInterface\|Mockery\\HigherOrderMessage\:\:once\(\)\.$#'
|
||||
identifier: method.notFound
|
||||
count: 1
|
||||
path: tests/Feature/Supplier/DeleteSupplierProjectJobTest.php
|
||||
|
||||
-
|
||||
message: '#^Call to an undefined method Pest\\PendingCalls\\TestCall\:\:artisan\(\)\.$#'
|
||||
identifier: method.notFound
|
||||
@@ -1851,7 +1983,7 @@ parameters:
|
||||
-
|
||||
message: '#^Call to an undefined method Pest\\PendingCalls\\TestCall\:\:mock\(\)\.$#'
|
||||
identifier: method.notFound
|
||||
count: 1
|
||||
count: 2
|
||||
path: tests/Feature/Supplier/SyncSupplierProjectJobTest.php
|
||||
|
||||
-
|
||||
@@ -1979,3 +2111,21 @@ parameters:
|
||||
identifier: argument.type
|
||||
count: 1
|
||||
path: tests/Unit/Supplier/SupplierQuotaAllocatorTest.php
|
||||
|
||||
-
|
||||
message: '#^Access to an undefined property Pest\\PendingCalls\\TestCall\:\:\$tenant\.$#'
|
||||
identifier: property.notFound
|
||||
count: 6
|
||||
path: tests/Feature/Project/ProjectCreateDedupTest.php
|
||||
|
||||
-
|
||||
message: '#^Call to an undefined method Pest\\PendingCalls\\TestCall\:\:fail\(\)\.$#'
|
||||
identifier: method.notFound
|
||||
count: 1
|
||||
path: tests/Feature/Project/ProjectCreateDedupTest.php
|
||||
|
||||
-
|
||||
message: '#^Call to an undefined method Symfony\\Component\\HttpFoundation\\Response\:\:getData\(\)\.$#'
|
||||
identifier: method.notFound
|
||||
count: 1
|
||||
path: tests/Feature/Project/ProjectCreateDedupTest.php
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
|
||||
// Консервативный старт (A1 backend-tooling #64): мёртвый код + качество кода.
|
||||
// БЕЗ type-declaration наборов и БЕЗ LaravelSetProvider (version-upgrade) на первом
|
||||
// заходе — их прогоняем вручную при апгрейде Laravel, не как per-commit гейт.
|
||||
return RectorConfig::configure()
|
||||
->withPaths([
|
||||
__DIR__.'/app',
|
||||
__DIR__.'/database',
|
||||
__DIR__.'/routes',
|
||||
])
|
||||
->withPreparedSets(
|
||||
deadCode: true,
|
||||
codeQuality: true,
|
||||
);
|
||||
@@ -260,10 +260,12 @@ export interface ApiProject {
|
||||
}
|
||||
|
||||
export async function listProjects(tenantId: number): Promise<ApiProject[]> {
|
||||
const { data } = await apiClient.get<{ projects: ApiProject[] }>('/api/projects', {
|
||||
// ProjectController::index() отдаёт { data: ProjectResource::collection(...) }.
|
||||
// `?? []` — защита от undefined.map в DealsView при нештатном ответе.
|
||||
const { data } = await apiClient.get<{ data: ApiProject[] }>('/api/projects', {
|
||||
params: { tenant_id: tenantId },
|
||||
});
|
||||
return data.projects;
|
||||
return data.data ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,13 +6,30 @@
|
||||
*
|
||||
* Sprint 4 Phase B/3 — split DashboardView (audit O-refactor-04 закрытие).
|
||||
*/
|
||||
import { computed } from 'vue';
|
||||
import { useAuthStore } from '../../stores/auth';
|
||||
|
||||
const range = defineModel<'today' | '7d' | '30d' | 'custom'>({ required: true });
|
||||
|
||||
const auth = useAuthStore();
|
||||
|
||||
/** Имя залогиненного пользователя (было захардкожено «Иван»). */
|
||||
const firstName = computed(() => auth.user?.first_name?.trim() || 'коллега');
|
||||
|
||||
/** Приветствие по времени суток (МСК машины пользователя). */
|
||||
const greeting = computed(() => {
|
||||
const h = new Date().getHours();
|
||||
if (h < 6) return 'Доброй ночи';
|
||||
if (h < 12) return 'Доброе утро';
|
||||
if (h < 18) return 'Добрый день';
|
||||
return 'Добрый вечер';
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<header class="page-head">
|
||||
<div>
|
||||
<h1 class="text-h4 mb-2 page-greet">Доброе утро, <em class="text-primary">Иван</em></h1>
|
||||
<h1 class="text-h4 mb-2 page-greet">{{ greeting }}, <em class="text-primary">{{ firstName }}</em></h1>
|
||||
<div class="page-meta text-body-2 text-medium-emphasis">
|
||||
<span><span class="num text-primary">+3</span> новых лида с утра</span>
|
||||
<span class="sep">·</span>
|
||||
|
||||
@@ -9,6 +9,7 @@ import { useRouter } from 'vue-router';
|
||||
import { useAuthStore } from '../../stores/auth';
|
||||
import { useNotificationsStore } from '../../stores/notifications';
|
||||
import { useCommandPalette } from '../../composables/useCommandPalette';
|
||||
import { repositionMenuAfterOpen } from '../../utils/menuRepositionFix';
|
||||
|
||||
defineProps<{
|
||||
pageTitle: string;
|
||||
@@ -111,7 +112,7 @@ async function handleLogout(): Promise<void> {
|
||||
</template>
|
||||
</v-btn>
|
||||
|
||||
<v-menu offset="8" :close-on-content-click="false" location="bottom end">
|
||||
<v-menu offset="8" :close-on-content-click="false" location="bottom end" @update:model-value="repositionMenuAfterOpen">
|
||||
<template #activator="{ props: bellProps }">
|
||||
<v-btn
|
||||
v-bind="bellProps"
|
||||
@@ -173,7 +174,7 @@ async function handleLogout(): Promise<void> {
|
||||
</v-card>
|
||||
</v-menu>
|
||||
|
||||
<v-menu offset="8">
|
||||
<v-menu offset="8" @update:model-value="repositionMenuAfterOpen">
|
||||
<template #activator="{ props }">
|
||||
<v-btn v-bind="props" variant="text" size="small" class="user-chip ml-2" aria-label="Меню пользователя">
|
||||
<v-avatar size="28" color="primary" class="mr-2">
|
||||
|
||||
@@ -29,11 +29,11 @@
|
||||
|
||||
<v-btn
|
||||
color="error"
|
||||
prepend-icon="mdi-archive"
|
||||
data-testid="bulk-archive"
|
||||
@click="confirmAndRun('archive')"
|
||||
prepend-icon="mdi-delete"
|
||||
data-testid="bulk-delete"
|
||||
@click="confirmAndRun('delete')"
|
||||
>
|
||||
Архивировать
|
||||
Удалить
|
||||
</v-btn>
|
||||
|
||||
<v-spacer />
|
||||
@@ -92,11 +92,10 @@ const skipToastText = ref('');
|
||||
const messages: Record<string, string> = {
|
||||
pause: 'Приостановить выбранные проекты?',
|
||||
resume: 'Возобновить выбранные проекты?',
|
||||
archive:
|
||||
'Архивировать выбранные проекты?\nДействие необратимо в Plan 5 (восстановление потребует ручного запроса).',
|
||||
delete: 'Удалить выбранные проекты? Действие необратимо. Проекты со сделками будут пропущены.',
|
||||
};
|
||||
|
||||
async function confirmAndRun(action: 'pause' | 'resume' | 'archive') {
|
||||
async function confirmAndRun(action: 'pause' | 'resume' | 'delete') {
|
||||
if (!window.confirm(messages[action])) return;
|
||||
await runBulk({ action });
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ const base = {
|
||||
daily_limit_target: 50,
|
||||
delivered_today: 32,
|
||||
is_active: true,
|
||||
archived_at: null,
|
||||
sync_status: 'ok' as const,
|
||||
};
|
||||
|
||||
|
||||
@@ -48,9 +48,9 @@
|
||||
<template #prepend><v-icon>mdi-refresh</v-icon></template>
|
||||
<v-list-item-title>Синхронизировать</v-list-item-title>
|
||||
</v-list-item>
|
||||
<v-list-item @click="$emit('archive', project)">
|
||||
<template #prepend><v-icon>mdi-archive</v-icon></template>
|
||||
<v-list-item-title>Архивировать</v-list-item-title>
|
||||
<v-list-item @click="$emit('delete', project)">
|
||||
<template #prepend><v-icon>mdi-delete</v-icon></template>
|
||||
<v-list-item-title>Удалить</v-list-item-title>
|
||||
</v-list-item>
|
||||
</v-list>
|
||||
</v-menu>
|
||||
@@ -97,7 +97,7 @@ defineEmits<{
|
||||
edit: [project: Project];
|
||||
'toggle-active': [project: Project];
|
||||
'sync-now': [project: Project];
|
||||
archive: [project: Project];
|
||||
delete: [project: Project];
|
||||
}>();
|
||||
|
||||
const typeLabel = computed(() => ({ site: 'Сайт', call: 'Звонок', sms: 'СМС' })[props.project.signal_type]);
|
||||
|
||||
@@ -63,10 +63,10 @@ async function onPause(): Promise<void> {
|
||||
async function onDelete(): Promise<void> {
|
||||
if (!props.project) return;
|
||||
const ok = window.confirm(
|
||||
'Архивировать проект? Действие необратимо в Plan 5 (восстановление потребует ручного запроса).',
|
||||
'Удалить проект? Действие необратимо. Если по проекту есть сделки — удаление будет заблокировано.',
|
||||
);
|
||||
if (!ok) return;
|
||||
await store.archive(props.project.id);
|
||||
await store.del(props.project.id);
|
||||
emit('close');
|
||||
}
|
||||
|
||||
|
||||
@@ -25,14 +25,15 @@ interface NavItem {
|
||||
}
|
||||
|
||||
const navItems: NavItem[] = [
|
||||
{ title: 'Тенанты', icon: 'mdi-account-group-outline', to: '/admin/tenants', count: 142 },
|
||||
{ title: 'Тенанты', icon: 'mdi-account-group-outline', to: '/admin/tenants' },
|
||||
{ title: 'Биллинг', icon: 'mdi-credit-card-outline', to: '/admin/billing' },
|
||||
{ title: 'Тарифная сетка', icon: 'mdi-tag-arrow-right', to: '/admin/pricing-tiers' },
|
||||
{ title: 'Цены поставщиков', icon: 'mdi-currency-rub', to: '/admin/supplier-prices' },
|
||||
{ title: 'Инциденты', icon: 'mdi-alert-outline', to: '/admin/incidents', count: 3 },
|
||||
{ title: 'Инциденты', icon: 'mdi-alert-outline', to: '/admin/incidents' },
|
||||
{ title: 'Impersonation', icon: 'mdi-account-switch', to: '/admin/impersonation' },
|
||||
{ title: 'Система', icon: 'mdi-cog-outline', to: '/admin/system' },
|
||||
{ title: 'Интеграция с поставщиком', icon: 'mdi-swap-horizontal', to: '/admin/supplier-integration' },
|
||||
{ title: 'Проекты у поставщика', icon: 'mdi-format-list-checks', to: '/admin/supplier-projects' },
|
||||
];
|
||||
|
||||
const route = useRoute();
|
||||
|
||||
@@ -41,7 +41,13 @@ const navItems = computed(() => [
|
||||
]);
|
||||
|
||||
const currentPageTitle = computed(() => {
|
||||
return navItems.value.find((i) => i.to === route.path)?.title ?? 'Страница';
|
||||
// Сначала короткий title из sidebar-nav (Дашборд/Сделки/…), затем — route.meta.title
|
||||
// для страниц вне sidebar (Напоминания, Импорт данных), и только потом fallback.
|
||||
return (
|
||||
navItems.value.find((i) => i.to === route.path)?.title ??
|
||||
(route.meta.title as string | undefined) ??
|
||||
'Страница'
|
||||
);
|
||||
});
|
||||
|
||||
async function loadNotifications(): Promise<void> {
|
||||
|
||||
@@ -168,6 +168,7 @@ const lucideMap: Record<string, Component> = {
|
||||
'mdi-content-save-outline': Save,
|
||||
'mdi-credit-card-outline': CreditCard,
|
||||
'mdi-currency-rub': RussianRuble,
|
||||
'mdi-delete': Trash2,
|
||||
'mdi-delete-outline': Trash2,
|
||||
'mdi-dots-vertical': MoreVertical,
|
||||
'mdi-download': Download,
|
||||
|
||||
@@ -283,6 +283,18 @@ const routes: RouteRecordRaw[] = [
|
||||
devLabel: 'Admin Supplier Integration',
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/admin/supplier-projects',
|
||||
name: 'admin-supplier-projects',
|
||||
component: () => import('../views/admin/AdminSupplierProjectsView.vue'),
|
||||
meta: {
|
||||
layout: 'admin',
|
||||
title: 'Проекты у поставщика',
|
||||
requiresAuth: true,
|
||||
devIndex: 31,
|
||||
devLabel: 'Admin Supplier Projects',
|
||||
},
|
||||
},
|
||||
// Error pages: 403/500 явные + catch-all 404 (всегда последний).
|
||||
{
|
||||
path: '/403',
|
||||
|
||||
@@ -13,7 +13,6 @@ export interface Project {
|
||||
delivered_today: number;
|
||||
delivered_in_month?: number;
|
||||
is_active: boolean;
|
||||
archived_at: string | null;
|
||||
region_mask?: number;
|
||||
region_mode?: string;
|
||||
regions?: number[]; // Plan 6 — subject codes 1..89; пустой массив = вся РФ
|
||||
@@ -65,7 +64,7 @@ export const useProjectsStore = defineStore('projects', () => {
|
||||
return data.data;
|
||||
}
|
||||
|
||||
async function archive(id: number) {
|
||||
async function del(id: number) {
|
||||
await axios.delete(`/api/projects/${id}`);
|
||||
await fetch();
|
||||
}
|
||||
@@ -94,7 +93,7 @@ export const useProjectsStore = defineStore('projects', () => {
|
||||
selectedIds.value.clear();
|
||||
}
|
||||
|
||||
async function bulkAction(action: 'pause' | 'resume' | 'archive') {
|
||||
async function bulkAction(action: 'pause' | 'resume' | 'delete') {
|
||||
const ids = Array.from(selectedIds.value);
|
||||
if (!ids.length) return;
|
||||
await axios.post('/api/projects/bulk', { action, ids });
|
||||
@@ -103,7 +102,7 @@ export const useProjectsStore = defineStore('projects', () => {
|
||||
}
|
||||
|
||||
interface BulkPayload {
|
||||
action: 'pause' | 'resume' | 'archive' | 'update_regions' | 'update_days' | 'update_limit';
|
||||
action: 'pause' | 'resume' | 'delete' | 'update_regions' | 'update_days' | 'update_limit';
|
||||
add?: number;
|
||||
remove?: number;
|
||||
// Plan 6.5 — update_regions оперирует кодами субъектов (1..89), не bitmask ФО.
|
||||
@@ -200,7 +199,7 @@ export const useProjectsStore = defineStore('projects', () => {
|
||||
fetch,
|
||||
create,
|
||||
update,
|
||||
archive,
|
||||
del,
|
||||
syncNow,
|
||||
toggleActive,
|
||||
toggleSelect,
|
||||
|
||||
@@ -5,6 +5,31 @@
|
||||
<v-btn color="primary" prepend-icon="mdi-plus" @click="openCreate">+ Создать проект</v-btn>
|
||||
</div>
|
||||
|
||||
<v-alert
|
||||
v-if="showCutoffBanner"
|
||||
data-testid="cutoff-banner"
|
||||
type="info"
|
||||
variant="tonal"
|
||||
border="start"
|
||||
class="mb-4"
|
||||
>
|
||||
<div class="d-flex justify-space-between align-start gap-2">
|
||||
<span>
|
||||
Важно: изменения по проектам (добавление, удаление, лимиты, рабочие дни, регионы)
|
||||
вносите <strong>до 18:00 МСК</strong>. Изменения после 18:00 применяются при следующей
|
||||
синхронизации — на следующий день.
|
||||
</span>
|
||||
<v-btn
|
||||
data-testid="cutoff-banner-close"
|
||||
icon="mdi-close"
|
||||
size="x-small"
|
||||
variant="text"
|
||||
aria-label="Скрыть уведомление"
|
||||
@click="dismissCutoffBanner"
|
||||
/>
|
||||
</div>
|
||||
</v-alert>
|
||||
|
||||
<div class="d-flex gap-3 mb-4">
|
||||
<v-select
|
||||
v-model="store.filters.signal_type"
|
||||
@@ -71,7 +96,7 @@
|
||||
@edit="openEdit"
|
||||
@toggle-active="store.toggleActive"
|
||||
@sync-now="(p: Project) => store.syncNow(p.id)"
|
||||
@archive="(p: Project) => store.archive(p.id)"
|
||||
@delete="(p: Project) => store.del(p.id)"
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -101,6 +126,15 @@ const createOpen = ref(false);
|
||||
const editOpen = ref(false);
|
||||
const editing = ref<Project | null>(null);
|
||||
|
||||
// Информационный баннер о сроке внесения изменений (синхронизация с поставщиком в 18:00 МСК).
|
||||
// Закрытие запоминается, чтобы не показывать повторно.
|
||||
const CUTOFF_BANNER_KEY = 'projects.cutoffBannerDismissed';
|
||||
const showCutoffBanner = ref(localStorage.getItem(CUTOFF_BANNER_KEY) !== '1');
|
||||
function dismissCutoffBanner(): void {
|
||||
showCutoffBanner.value = false;
|
||||
localStorage.setItem(CUTOFF_BANNER_KEY, '1');
|
||||
}
|
||||
|
||||
const singleSelectedProject = computed<Project | null>(() => {
|
||||
if (store.selectedIds.size !== 1) return null;
|
||||
const [id] = store.selectedIds;
|
||||
@@ -123,7 +157,6 @@ const typeFilters = [
|
||||
const statusFilters = [
|
||||
{ title: 'Активные', value: 'active' },
|
||||
{ title: 'На паузе', value: 'paused' },
|
||||
{ title: 'Архивные', value: 'archived' },
|
||||
];
|
||||
|
||||
let searchTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
@@ -27,6 +27,38 @@ const loading = ref(false);
|
||||
const reconciling = ref(false);
|
||||
const error = ref<string | null>(null);
|
||||
|
||||
// --- Plan 4 Task 1: глобальный режим экспорта проектов (online|batch) ---
|
||||
|
||||
type ExportMode = 'online' | 'batch';
|
||||
const exportMode = ref<ExportMode>('batch');
|
||||
const exportModeError = ref<string | null>(null);
|
||||
const exportModeSaving = ref(false);
|
||||
|
||||
async function loadExportMode(): Promise<void> {
|
||||
try {
|
||||
const { data } = await axios.get('/api/admin/supplier-integration/export-mode');
|
||||
if (data?.mode === 'online' || data?.mode === 'batch') {
|
||||
exportMode.value = data.mode;
|
||||
}
|
||||
} catch {
|
||||
exportModeError.value = 'Не удалось загрузить режим экспорта.';
|
||||
}
|
||||
}
|
||||
|
||||
async function setExportMode(mode: ExportMode): Promise<void> {
|
||||
if (exportMode.value === mode) return;
|
||||
exportModeSaving.value = true;
|
||||
exportModeError.value = null;
|
||||
try {
|
||||
const { data } = await axios.post('/api/admin/supplier-integration/export-mode', { mode });
|
||||
exportMode.value = data?.mode === 'online' ? 'online' : 'batch';
|
||||
} catch {
|
||||
exportModeError.value = 'Не удалось сохранить режим экспорта.';
|
||||
} finally {
|
||||
exportModeSaving.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function load(): Promise<void> {
|
||||
loading.value = true;
|
||||
error.value = null;
|
||||
@@ -106,6 +138,7 @@ function formatDate(s: string): string {
|
||||
onMounted(() => {
|
||||
void load();
|
||||
void loadManualQueue();
|
||||
void loadExportMode();
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -113,6 +146,43 @@ onMounted(() => {
|
||||
<div class="pa-6">
|
||||
<h1 class="text-h5 mb-4">Интеграция с поставщиком</h1>
|
||||
|
||||
<v-card class="mb-4">
|
||||
<v-card-title>Режим экспорта проектов</v-card-title>
|
||||
<v-card-text>
|
||||
<v-alert v-if="exportModeError" type="error" density="compact" class="mb-3">
|
||||
{{ exportModeError }}
|
||||
</v-alert>
|
||||
<div data-testid="export-mode-toggle">
|
||||
<v-btn-toggle
|
||||
:model-value="exportMode"
|
||||
mandatory
|
||||
color="primary"
|
||||
density="comfortable"
|
||||
:disabled="exportModeSaving"
|
||||
>
|
||||
<v-btn
|
||||
data-testid="export-mode-online"
|
||||
value="online"
|
||||
@click="setExportMode('online')"
|
||||
>
|
||||
Онлайн
|
||||
</v-btn>
|
||||
<v-btn
|
||||
data-testid="export-mode-batch"
|
||||
value="batch"
|
||||
@click="setExportMode('batch')"
|
||||
>
|
||||
Пакетный
|
||||
</v-btn>
|
||||
</v-btn-toggle>
|
||||
</div>
|
||||
<p class="text-caption text-medium-emphasis mt-3 mb-0">
|
||||
Онлайн — изменения проекта переносятся к поставщику сразу.
|
||||
Пакетный — ночной синк в 18:00 (SyncSupplierProjectsJob).
|
||||
</p>
|
||||
</v-card-text>
|
||||
</v-card>
|
||||
|
||||
<v-card class="mb-4">
|
||||
<v-card-title>Здоровье резервного канала</v-card-title>
|
||||
<v-card-text>
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
<template>
|
||||
<div class="admin-supplier-projects-view pa-6">
|
||||
<h1 class="text-h5 mb-4">Проекты у поставщика</h1>
|
||||
<p class="text-body-2 text-medium-emphasis mb-4">
|
||||
Все проекты, заведённые у поставщика crm.bp-gr.ru. Удаление снимает проект
|
||||
на портале и локальные привязки тенантов (каскадом).
|
||||
</p>
|
||||
|
||||
<v-alert
|
||||
v-if="fetchError"
|
||||
type="warning"
|
||||
variant="tonal"
|
||||
density="compact"
|
||||
class="mb-4"
|
||||
data-testid="projects-fetch-error"
|
||||
closable
|
||||
@click:close="fetchError = null"
|
||||
>
|
||||
{{ fetchError }}
|
||||
</v-alert>
|
||||
|
||||
<div class="d-flex align-center mb-3">
|
||||
<v-btn
|
||||
color="error"
|
||||
variant="flat"
|
||||
prepend-icon="mdi-delete-outline"
|
||||
data-testid="bulk-delete-btn"
|
||||
:disabled="selected.length === 0"
|
||||
:loading="deleting"
|
||||
@click="confirmOpen = true"
|
||||
>
|
||||
Удалить выбранные ({{ selected.length }})
|
||||
</v-btn>
|
||||
<v-spacer />
|
||||
<v-btn variant="text" prepend-icon="mdi-refresh" :loading="loading" @click="load">
|
||||
Обновить
|
||||
</v-btn>
|
||||
</div>
|
||||
|
||||
<v-card elevation="1">
|
||||
<v-data-table
|
||||
:headers="headers"
|
||||
:items="projects"
|
||||
:loading="loading"
|
||||
density="comfortable"
|
||||
item-value="id"
|
||||
>
|
||||
<template #[`item.select`]="{ item }">
|
||||
<v-checkbox
|
||||
:model-value="selected.includes(item.id)"
|
||||
:data-testid="`row-checkbox-${item.id}`"
|
||||
hide-details
|
||||
density="compact"
|
||||
@update:model-value="(v: boolean | null) => toggleRow(item.id, v)"
|
||||
/>
|
||||
</template>
|
||||
<template #[`item.orderers`]="{ item }">
|
||||
<span v-if="item.orderers.length">{{ item.orderers.join(', ') }}</span>
|
||||
<span v-else class="text-medium-emphasis">—</span>
|
||||
</template>
|
||||
<template #[`item.last_delivery_at`]="{ item }">
|
||||
{{ item.last_delivery_at ? formatDate(item.last_delivery_at) : '—' }}
|
||||
</template>
|
||||
</v-data-table>
|
||||
</v-card>
|
||||
|
||||
<v-dialog v-model="confirmOpen" max-width="480">
|
||||
<v-card>
|
||||
<v-card-title>Удалить выбранные проекты?</v-card-title>
|
||||
<v-card-text>
|
||||
Будет удалено проектов: <strong>{{ selected.length }}</strong>.
|
||||
Действие снимает проекты у поставщика и локальные привязки.
|
||||
Отменить нельзя.
|
||||
</v-card-text>
|
||||
<v-card-actions>
|
||||
<v-spacer />
|
||||
<v-btn variant="text" @click="confirmOpen = false">Отмена</v-btn>
|
||||
<v-btn
|
||||
color="error"
|
||||
variant="flat"
|
||||
data-testid="confirm-delete-btn"
|
||||
:loading="deleting"
|
||||
@click="performDelete"
|
||||
>
|
||||
Удалить
|
||||
</v-btn>
|
||||
</v-card-actions>
|
||||
</v-card>
|
||||
</v-dialog>
|
||||
|
||||
<v-snackbar
|
||||
v-model="snackbarOpen"
|
||||
:timeout="4000"
|
||||
:color="snackbarColor"
|
||||
location="bottom right"
|
||||
data-testid="projects-snackbar"
|
||||
>
|
||||
{{ snackbarText }}
|
||||
</v-snackbar>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted } from 'vue';
|
||||
import axios from 'axios';
|
||||
|
||||
/**
|
||||
* SaaS-admin → «Проекты у поставщика» (Plan 4 Task 3).
|
||||
*
|
||||
* Backend: AdminSupplierIntegrationController::projectsIndex / projectsDestroy.
|
||||
* Список supplier_projects + кто заказывал (orderers) + дата последней поставки;
|
||||
* bulk-delete выбранных (портал + локально каскадом).
|
||||
*/
|
||||
|
||||
interface SupplierProjectRow {
|
||||
id: number;
|
||||
platform: string;
|
||||
signal_type: string;
|
||||
unique_key: string;
|
||||
subject_code: number | null;
|
||||
subject_name: string | null;
|
||||
current_limit: number;
|
||||
supplier_external_id: string | null;
|
||||
orderers: string[];
|
||||
last_delivery_at: string | null;
|
||||
}
|
||||
|
||||
const projects = ref<SupplierProjectRow[]>([]);
|
||||
const selected = ref<number[]>([]);
|
||||
const loading = ref(false);
|
||||
const deleting = ref(false);
|
||||
const fetchError = ref<string | null>(null);
|
||||
const confirmOpen = ref(false);
|
||||
const snackbarOpen = ref(false);
|
||||
const snackbarText = ref('');
|
||||
const snackbarColor = ref<'success' | 'warning' | 'error'>('success');
|
||||
|
||||
const headers = [
|
||||
{ title: '', key: 'select', sortable: false, width: 56 },
|
||||
{ title: 'Источник', key: 'unique_key', sortable: true },
|
||||
{ title: 'Платформа', key: 'platform', sortable: true, width: 110 },
|
||||
{ title: 'Регион', key: 'subject_name', sortable: true },
|
||||
{ title: 'Лимит', key: 'current_limit', sortable: true, width: 90 },
|
||||
{ title: 'Кто заказывал', key: 'orderers', sortable: false },
|
||||
{ title: 'Последняя поставка', key: 'last_delivery_at', sortable: true, width: 180 },
|
||||
];
|
||||
|
||||
function toggleRow(id: number, value: boolean | null): void {
|
||||
if (value) {
|
||||
if (!selected.value.includes(id)) selected.value.push(id);
|
||||
} else {
|
||||
selected.value = selected.value.filter((x) => x !== id);
|
||||
}
|
||||
}
|
||||
|
||||
function formatDate(s: string): string {
|
||||
return new Date(s).toLocaleString('ru-RU');
|
||||
}
|
||||
|
||||
async function load(): Promise<void> {
|
||||
loading.value = true;
|
||||
fetchError.value = null;
|
||||
try {
|
||||
const { data } = await axios.get('/api/admin/supplier-integration/projects');
|
||||
projects.value = Array.isArray(data?.projects) ? data.projects : [];
|
||||
// Снять выбор с уже удалённых строк.
|
||||
const ids = new Set(projects.value.map((p) => p.id));
|
||||
selected.value = selected.value.filter((id) => ids.has(id));
|
||||
} catch {
|
||||
fetchError.value = 'Не удалось загрузить список проектов.';
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function performDelete(): Promise<void> {
|
||||
if (selected.value.length === 0) {
|
||||
confirmOpen.value = false;
|
||||
return;
|
||||
}
|
||||
deleting.value = true;
|
||||
try {
|
||||
const { data } = await axios.post('/api/admin/supplier-integration/projects/delete', {
|
||||
ids: selected.value,
|
||||
});
|
||||
const deleted = Number(data?.deleted ?? 0);
|
||||
const failures = Array.isArray(data?.failures) ? data.failures : [];
|
||||
if (failures.length > 0) {
|
||||
snackbarColor.value = 'warning';
|
||||
snackbarText.value = `Удалено: ${deleted}. Не удалось: ${failures.length}.`;
|
||||
} else {
|
||||
snackbarColor.value = 'success';
|
||||
snackbarText.value = `Удалено проектов: ${deleted}.`;
|
||||
}
|
||||
snackbarOpen.value = true;
|
||||
confirmOpen.value = false;
|
||||
selected.value = [];
|
||||
await load();
|
||||
} catch {
|
||||
snackbarColor.value = 'error';
|
||||
snackbarText.value = 'Ошибка при удалении проектов.';
|
||||
snackbarOpen.value = true;
|
||||
} finally {
|
||||
deleting.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(load);
|
||||
|
||||
defineExpose({ load, performDelete, toggleRow, projects, selected, confirmOpen });
|
||||
</script>
|
||||
@@ -25,7 +25,6 @@ const sampleProject = {
|
||||
daily_limit_target: 50,
|
||||
delivered_today: 12,
|
||||
is_active: true,
|
||||
archived_at: null,
|
||||
sync_status: 'ok' as const,
|
||||
region_mask: 0,
|
||||
region_mode: 'include' as const,
|
||||
|
||||
@@ -86,17 +86,20 @@
|
||||
/>
|
||||
|
||||
<v-autocomplete
|
||||
v-model="form.regions"
|
||||
:model-value="form.regions"
|
||||
:items="selectableRegions"
|
||||
item-title="name"
|
||||
item-value="code"
|
||||
label="Регионы (пусто = вся РФ)"
|
||||
label="Регионы"
|
||||
:disabled="vsyaRfConfirmed"
|
||||
multiple
|
||||
chips
|
||||
clearable
|
||||
density="comfortable"
|
||||
class="ld-input-quiet"
|
||||
data-testid="regions-autocomplete"
|
||||
:error-messages="errors.regions"
|
||||
@update:model-value="onRegionsChange"
|
||||
@update:menu="repositionMenuAfterOpen"
|
||||
>
|
||||
<template #item="{ props: itemProps, item }">
|
||||
@@ -108,6 +111,51 @@
|
||||
</template>
|
||||
</v-autocomplete>
|
||||
|
||||
<v-checkbox
|
||||
:model-value="vsyaRf"
|
||||
label="Вся РФ (все регионы)"
|
||||
density="comfortable"
|
||||
hide-details
|
||||
data-testid="vsya-rf-checkbox"
|
||||
@update:model-value="(v: boolean | null) => (v ? chooseVsyaRf() : cancelVsyaRf())"
|
||||
/>
|
||||
|
||||
<v-alert
|
||||
v-if="vsyaRf && !vsyaRfConfirmed"
|
||||
type="warning"
|
||||
variant="tonal"
|
||||
density="compact"
|
||||
class="mt-2"
|
||||
data-testid="vsya-rf-warning"
|
||||
>
|
||||
Вы выбрали всю Россию — проект будет получать лиды по всем регионам
|
||||
(всем субъектам РФ). Подтвердите, что это намеренно.
|
||||
<div class="mt-2">
|
||||
<v-btn
|
||||
size="small"
|
||||
color="warning"
|
||||
variant="flat"
|
||||
data-testid="confirm-vsya-rf"
|
||||
@click="confirmVsyaRf"
|
||||
>
|
||||
Подтверждаю «Вся РФ»
|
||||
</v-btn>
|
||||
<v-btn size="small" variant="text" class="ml-2" @click="cancelVsyaRf">
|
||||
Отмена
|
||||
</v-btn>
|
||||
</div>
|
||||
</v-alert>
|
||||
|
||||
<v-chip
|
||||
v-else-if="vsyaRfConfirmed"
|
||||
color="success"
|
||||
size="small"
|
||||
class="mt-2"
|
||||
data-testid="vsya-rf-confirmed"
|
||||
>
|
||||
Вся РФ — подтверждено
|
||||
</v-chip>
|
||||
|
||||
<v-alert
|
||||
v-if="generalError"
|
||||
type="error"
|
||||
@@ -176,6 +224,38 @@ const errors = reactive<Record<string, string[]>>({});
|
||||
const saving = ref(false);
|
||||
const generalError = ref<string | null>(null);
|
||||
|
||||
// Plan 4 Task 4: обязательный выбор региона + явная «Вся РФ» с подтверждением.
|
||||
// vsyaRf — чекбокс выбран; vsyaRfConfirmed — подтверждён через предупреждение.
|
||||
// На бэке regions=[] (Вся РФ) и «забыл» неотличимы → гейт намеренно UI-only.
|
||||
const vsyaRf = ref(false);
|
||||
const vsyaRfConfirmed = ref(false);
|
||||
|
||||
function chooseVsyaRf(): void {
|
||||
vsyaRf.value = true;
|
||||
vsyaRfConfirmed.value = false;
|
||||
}
|
||||
|
||||
function confirmVsyaRf(): void {
|
||||
vsyaRfConfirmed.value = true;
|
||||
form.regions = []; // Вся РФ → пустой массив субъектов
|
||||
delete errors.regions;
|
||||
}
|
||||
|
||||
function cancelVsyaRf(): void {
|
||||
vsyaRf.value = false;
|
||||
vsyaRfConfirmed.value = false;
|
||||
}
|
||||
|
||||
function onRegionsChange(codes: number[]): void {
|
||||
form.regions = Array.isArray(codes) ? codes : [];
|
||||
if (form.regions.length > 0) {
|
||||
// Взаимоисключение: выбор конкретных субъектов снимает «Вся РФ».
|
||||
vsyaRf.value = false;
|
||||
vsyaRfConfirmed.value = false;
|
||||
delete errors.regions;
|
||||
}
|
||||
}
|
||||
|
||||
const dayLabels = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'];
|
||||
const selectedDays = ref<number[]>([0, 1, 2, 3, 4, 5, 6]);
|
||||
watch(selectedDays, (days) => {
|
||||
@@ -191,12 +271,18 @@ watch(
|
||||
() => props.modelValue,
|
||||
(open) => {
|
||||
if (open) generalError.value = null;
|
||||
if (open) {
|
||||
delete errors.regions;
|
||||
}
|
||||
if (open && props.mode === 'edit' && props.project) {
|
||||
Object.assign(form, props.project);
|
||||
form.regions = Array.isArray(props.project.regions) ? [...props.project.regions] : [];
|
||||
const days: number[] = [];
|
||||
for (let i = 0; i < 7; i++) if (form.delivery_days_mask & (1 << i)) days.push(i);
|
||||
selectedDays.value = days;
|
||||
// Существующий проект с пустыми регионами = «Вся РФ» (предзаполняем подтверждённым).
|
||||
vsyaRf.value = form.regions.length === 0;
|
||||
vsyaRfConfirmed.value = form.regions.length === 0;
|
||||
} else if (open) {
|
||||
Object.assign(form, {
|
||||
name: '',
|
||||
@@ -209,14 +295,24 @@ watch(
|
||||
delivery_days_mask: 127,
|
||||
});
|
||||
selectedDays.value = [0, 1, 2, 3, 4, 5, 6];
|
||||
vsyaRf.value = false;
|
||||
vsyaRfConfirmed.value = false;
|
||||
}
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
async function submit() {
|
||||
saving.value = true;
|
||||
generalError.value = null;
|
||||
Object.keys(errors).forEach((k) => delete errors[k]);
|
||||
|
||||
// Гейт обязательного региона: нужны либо субъекты, либо подтверждённая «Вся РФ».
|
||||
if (form.regions.length === 0 && !vsyaRfConfirmed.value) {
|
||||
errors.regions = ['Выберите регион или подтвердите «Вся РФ»'];
|
||||
return;
|
||||
}
|
||||
|
||||
saving.value = true;
|
||||
try {
|
||||
await ensureCsrfCookie();
|
||||
if (props.mode === 'edit' && props.project) {
|
||||
@@ -241,6 +337,8 @@ async function submit() {
|
||||
function close() {
|
||||
emit('update:modelValue', false);
|
||||
}
|
||||
|
||||
defineExpose({ chooseVsyaRf, confirmVsyaRf, cancelVsyaRf, onRegionsChange, vsyaRf, vsyaRfConfirmed, form, submit });
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
+23
-5
@@ -154,6 +154,14 @@ Route::middleware('saas-admin')->group(function () {
|
||||
Route::get('/api/admin/supplier-integration/manual-queue', 'App\Http\Controllers\Api\AdminSupplierIntegrationController@manualQueueIndex');
|
||||
Route::post('/api/admin/supplier-integration/manual-queue/{id}/resolve', 'App\Http\Controllers\Api\AdminSupplierIntegrationController@manualQueueResolve')
|
||||
->where('id', '[0-9]+');
|
||||
|
||||
// Plan 4 Task 1: глобальный тумблер режима экспорта проектов (online|batch).
|
||||
Route::get('/api/admin/supplier-integration/export-mode', 'App\Http\Controllers\Api\AdminSupplierIntegrationController@getExportMode');
|
||||
Route::post('/api/admin/supplier-integration/export-mode', 'App\Http\Controllers\Api\AdminSupplierIntegrationController@setExportMode');
|
||||
|
||||
// Plan 4 Task 2: экран «Проекты у поставщика» — список + bulk-delete.
|
||||
Route::get('/api/admin/supplier-integration/projects', 'App\Http\Controllers\Api\AdminSupplierIntegrationController@projectsIndex');
|
||||
Route::post('/api/admin/supplier-integration/projects/delete', 'App\Http\Controllers\Api\AdminSupplierIntegrationController@projectsDestroy');
|
||||
});
|
||||
|
||||
// Plan 4 Task 11: tenant charges ledger (read-only + CSV export).
|
||||
@@ -186,9 +194,12 @@ Route::middleware(['auth:sanctum', 'tenant'])->group(function () {
|
||||
Route::post('/api/webhooks/test', 'App\Http\Controllers\Api\WebhookSettingsController@test');
|
||||
});
|
||||
|
||||
// Дашборд — агрегат KPI/баланса/активности/воронки (audit J3). На MVP без
|
||||
// auth-middleware (tenant_id параметром); production: middleware('auth:sanctum','tenant').
|
||||
Route::get('/api/dashboard/summary', 'App\Http\Controllers\Api\DashboardController@summary');
|
||||
// Дашборд — агрегат KPI/баланса/активности/воронки (audit J3). Go-live: auth:sanctum
|
||||
// + tenant; tenant_id из auth()->user()->tenant_id (SetTenantContext), НЕ из параметра
|
||||
// запроса — закрывает кросс-tenant утечку KPI (как DealController J1).
|
||||
Route::middleware(['auth:sanctum', 'tenant'])->group(function () {
|
||||
Route::get('/api/dashboard/summary', 'App\Http\Controllers\Api\DashboardController@summary');
|
||||
});
|
||||
|
||||
// Сделки — single-resource CRUD + bulk + export. J1 (Sprint 3F, audit):
|
||||
// auth:sanctum + tenant. tenant_id берётся из auth()->user()->tenant_id
|
||||
@@ -220,8 +231,15 @@ Route::middleware(['auth:sanctum', 'tenant'])->group(function () {
|
||||
});
|
||||
|
||||
// Lookup endpoints — заполняют v-select'ы (NewDealDialog, smart-filters).
|
||||
Route::get('/api/managers', 'App\Http\Controllers\Api\ManagerController@index');
|
||||
Route::get('/api/lead-statuses', 'App\Http\Controllers\Api\LeadStatusController@index');
|
||||
// Go-live: auth:sanctum. /api/managers — tenant-scoped (tenant_id из authed-user, НЕ из
|
||||
// параметра — закрывает кросс-tenant утечку списка пользователей); /api/lead-statuses —
|
||||
// глобальная таблица (без tenant_id), нужен только auth:sanctum.
|
||||
Route::middleware(['auth:sanctum', 'tenant'])->group(function () {
|
||||
Route::get('/api/managers', 'App\Http\Controllers\Api\ManagerController@index');
|
||||
});
|
||||
Route::middleware('auth:sanctum')->group(function () {
|
||||
Route::get('/api/lead-statuses', 'App\Http\Controllers\Api\LeadStatusController@index');
|
||||
});
|
||||
|
||||
// Plan 5 Task 2: Projects CRUD — расширенный API с auth:sanctum + RLS.
|
||||
// Заменяет старый GET /api/projects?tenant_id={id} (без auth, MVP-версия).
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* ВРЕМЕННЫЙ demo-скрипт — разбивает 5 тестовых пользователей на 5 отдельных тенантов.
|
||||
* Каждый логин = своя компания, данные изолированы.
|
||||
* Идемпотентный: повторный запуск не дублирует тенанты.
|
||||
* Запуск: php artisan tinker storage/_demo_split_tenants.php
|
||||
*/
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// 1. Проверяем исходное состояние
|
||||
// -------------------------------------------------------------------
|
||||
$totalBefore = User::count();
|
||||
$tenantsBefore = Tenant::count();
|
||||
echo "=== ДО: {$totalBefore} пользователей, {$tenantsBefore} тенант(ов) ===\n\n";
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// 2. Описание каждого пользователя и его будущего тенанта
|
||||
// -------------------------------------------------------------------
|
||||
$accounts = [
|
||||
[
|
||||
'email' => 'admin@demo.local',
|
||||
'tenant_subdomain' => 'demo', // оставляем существующий тенант
|
||||
'org_name' => null, // null = взять из существующего
|
||||
'create_new_tenant' => false,
|
||||
],
|
||||
[
|
||||
'email' => 'manager1@demo.local',
|
||||
'tenant_subdomain' => 'ivan-demo',
|
||||
'org_name' => 'Компания Ивана',
|
||||
'create_new_tenant' => true,
|
||||
],
|
||||
[
|
||||
'email' => 'manager2@demo.local',
|
||||
'tenant_subdomain' => 'anna-demo',
|
||||
'org_name' => 'Компания Анны',
|
||||
'create_new_tenant' => true,
|
||||
],
|
||||
[
|
||||
'email' => 'manager3@demo.local',
|
||||
'tenant_subdomain' => 'petr-demo',
|
||||
'org_name' => 'Компания Петра',
|
||||
'create_new_tenant' => true,
|
||||
],
|
||||
[
|
||||
'email' => 'manager4@demo.local',
|
||||
'tenant_subdomain' => 'mariya-demo',
|
||||
'org_name' => 'Компания Марии',
|
||||
'create_new_tenant' => true,
|
||||
],
|
||||
];
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// 3. Создаём тенанты и переназначаем пользователей
|
||||
// -------------------------------------------------------------------
|
||||
foreach ($accounts as $a) {
|
||||
$user = User::query()->where('email', $a['email'])->firstOrFail();
|
||||
|
||||
if (! $a['create_new_tenant']) {
|
||||
// Demo Admin остаётся в tenant "demo"
|
||||
$tenant = Tenant::query()->where('subdomain', $a['tenant_subdomain'])->firstOrFail();
|
||||
echo "SKIP {$user->email} → тенант «{$tenant->organization_name}» (id={$tenant->id}) — без изменений\n";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Создаём новый тенант, если ещё не существует
|
||||
$tenant = Tenant::query()->firstOrCreate(
|
||||
['subdomain' => $a['tenant_subdomain']],
|
||||
[
|
||||
'organization_name' => $a['org_name'],
|
||||
'contact_email' => $user->email,
|
||||
'webhook_token' => Str::random(64),
|
||||
'timezone' => 'Europe/Moscow',
|
||||
'locale' => 'ru',
|
||||
'is_trial' => true,
|
||||
'api_key_limit' => 5,
|
||||
]
|
||||
);
|
||||
|
||||
// Переназначаем пользователя в новый тенант
|
||||
$user->tenant_id = $tenant->id;
|
||||
$user->save();
|
||||
|
||||
echo "OK {$user->email} → новый тенант «{$tenant->organization_name}» (id={$tenant->id}, subdomain={$tenant->subdomain})\n";
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// 4. Итоговый отчёт
|
||||
// -------------------------------------------------------------------
|
||||
echo "\n=== ИТОГО: изоляция тенантов ===\n";
|
||||
$tenants = Tenant::query()
|
||||
->whereIn('subdomain', ['demo', 'ivan-demo', 'anna-demo', 'petr-demo', 'mariya-demo'])
|
||||
->orderBy('id')
|
||||
->get();
|
||||
|
||||
foreach ($tenants as $t) {
|
||||
$users = User::query()->where('tenant_id', $t->id)->pluck('email')->implode(', ');
|
||||
$projects = Project::query()->where('tenant_id', $t->id)->count();
|
||||
echo sprintf(
|
||||
" Тенант %-12s (id=%-2d) — пользователи: %-40s | проектов: %d\n",
|
||||
$t->subdomain,
|
||||
$t->id,
|
||||
$users ?: '(нет)',
|
||||
$projects
|
||||
);
|
||||
}
|
||||
|
||||
echo "\nГотово. Каждый логин теперь в отдельной компании.\n";
|
||||
echo "Пароль для всех: password\n";
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
uses(DatabaseTransactions::class);
|
||||
|
||||
// EnsureSaasAdmin — стаб в testing (см. SupplierManualQueueTest::16); actingAs
|
||||
// нужен только для прохода middleware-стека auth+admin.
|
||||
|
||||
it('GET export-mode returns current value', function (): void {
|
||||
$this->actingAs(User::factory()->create());
|
||||
DB::table('system_settings')->updateOrInsert(
|
||||
['key' => 'supplier_export_mode'],
|
||||
['value' => 'batch', 'type' => 'string', 'updated_at' => now()],
|
||||
);
|
||||
|
||||
$this->getJson('/api/admin/supplier-integration/export-mode')
|
||||
->assertOk()
|
||||
->assertJson(['mode' => 'batch']);
|
||||
});
|
||||
|
||||
it('POST export-mode switches value', function (): void {
|
||||
$this->actingAs(User::factory()->create());
|
||||
DB::table('system_settings')->updateOrInsert(
|
||||
['key' => 'supplier_export_mode'],
|
||||
['value' => 'batch', 'type' => 'string', 'updated_at' => now()],
|
||||
);
|
||||
|
||||
$this->postJson('/api/admin/supplier-integration/export-mode', ['mode' => 'online'])
|
||||
->assertOk()
|
||||
->assertJson(['mode' => 'online']);
|
||||
|
||||
expect(DB::table('system_settings')->where('key', 'supplier_export_mode')->value('value'))
|
||||
->toBe('online');
|
||||
});
|
||||
|
||||
it('POST export-mode rejects invalid value', function (): void {
|
||||
$this->actingAs(User::factory()->create());
|
||||
|
||||
$this->postJson('/api/admin/supplier-integration/export-mode', ['mode' => 'turbo'])
|
||||
->assertStatus(422);
|
||||
});
|
||||
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\SupplierProject;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use App\Services\Supplier\SupplierPortalClient;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
uses(DatabaseTransactions::class);
|
||||
|
||||
// EnsureSaasAdmin — стаб в testing (см. SupplierManualQueueTest::16): обычный
|
||||
// User::factory + actingAs без guard'а.
|
||||
|
||||
it('GET /admin/supplier-integration/projects returns rows with orderers + last delivery', function (): void {
|
||||
$this->actingAs(User::factory()->create());
|
||||
$tenant = Tenant::factory()->create(['organization_name' => 'ООО Ромашка']);
|
||||
|
||||
$sp = SupplierProject::query()->create([
|
||||
'platform' => 'B1',
|
||||
'signal_type' => 'site',
|
||||
'unique_key' => 'okna.ru',
|
||||
'subject_code' => 82, // Москва (по конституционному порядку, ст. 65)
|
||||
'current_limit' => 5,
|
||||
'current_workdays' => [1, 2, 3, 4, 5, 6, 7],
|
||||
'current_regions' => null,
|
||||
'sync_status' => 'ok',
|
||||
'supplier_external_id' => '777',
|
||||
]);
|
||||
|
||||
$project = Project::factory()->for($tenant)->create();
|
||||
DB::table('project_supplier_links')->insert([
|
||||
'project_id' => $project->id,
|
||||
'supplier_project_id' => $sp->id,
|
||||
'platform' => 'B1',
|
||||
'subject_code' => 82,
|
||||
]);
|
||||
|
||||
DB::table('supplier_leads')->insert([
|
||||
'supplier_project_id' => $sp->id,
|
||||
'platform' => 'B1',
|
||||
'raw_payload' => json_encode([]),
|
||||
'phone' => '+79991234567',
|
||||
'received_at' => '2026-05-19 10:00:00',
|
||||
]);
|
||||
|
||||
$resp = $this->getJson('/api/admin/supplier-integration/projects')
|
||||
->assertOk()
|
||||
->json();
|
||||
|
||||
$row = collect($resp['projects'])->firstWhere('id', $sp->id);
|
||||
expect($row)->not->toBeNull()
|
||||
->and($row['unique_key'])->toBe('okna.ru')
|
||||
->and($row['subject_code'])->toBe(82)
|
||||
->and($row['subject_name'])->toBe('Москва')
|
||||
->and($row['platform'])->toBe('B1')
|
||||
->and($row['current_limit'])->toBe(5)
|
||||
->and($row['orderers'])->toContain('ООО Ромашка')
|
||||
->and($row['last_delivery_at'])->not->toBeNull();
|
||||
});
|
||||
|
||||
it('GET /projects returns subject_name «РФ» for NULL subject_code', function (): void {
|
||||
$this->actingAs(User::factory()->create());
|
||||
|
||||
$sp = SupplierProject::query()->create([
|
||||
'platform' => 'B2',
|
||||
'signal_type' => 'site',
|
||||
'unique_key' => 'all-russia.example',
|
||||
'subject_code' => null,
|
||||
'current_limit' => 0,
|
||||
'current_workdays' => [1, 2, 3, 4, 5, 6, 7],
|
||||
'current_regions' => null,
|
||||
'sync_status' => 'ok',
|
||||
'supplier_external_id' => '888',
|
||||
]);
|
||||
|
||||
$resp = $this->getJson('/api/admin/supplier-integration/projects')->assertOk()->json();
|
||||
$row = collect($resp['projects'])->firstWhere('id', $sp->id);
|
||||
expect($row['subject_code'])->toBeNull()
|
||||
->and($row['subject_name'])->toBe('РФ');
|
||||
});
|
||||
|
||||
it('POST /projects/delete deletes on portal + locally (pivot cascades)', function (): void {
|
||||
$this->actingAs(User::factory()->create());
|
||||
|
||||
// Мокаем portal-клиент, чтобы не лезть в Redis-сессию (SupplierPortalClient::loadSession()).
|
||||
$deletedExternalIds = [];
|
||||
$clientMock = new class($deletedExternalIds) extends SupplierPortalClient
|
||||
{
|
||||
/** @var array<int, int> */
|
||||
public array $calls;
|
||||
|
||||
public function __construct(array &$calls)
|
||||
{
|
||||
$this->calls = &$calls;
|
||||
}
|
||||
|
||||
public function deleteProject(int $externalId): void
|
||||
{
|
||||
$this->calls[] = $externalId;
|
||||
}
|
||||
};
|
||||
app()->instance(SupplierPortalClient::class, $clientMock);
|
||||
|
||||
$tenant = Tenant::factory()->create();
|
||||
$sp = SupplierProject::query()->create([
|
||||
'platform' => 'B1',
|
||||
'signal_type' => 'site',
|
||||
'unique_key' => 'delete-me.ru',
|
||||
'subject_code' => 77,
|
||||
'current_limit' => 0,
|
||||
'current_workdays' => [1, 2, 3, 4, 5, 6, 7],
|
||||
'current_regions' => null,
|
||||
'sync_status' => 'ok',
|
||||
'supplier_external_id' => '999',
|
||||
]);
|
||||
$project = Project::factory()->for($tenant)->create();
|
||||
DB::table('project_supplier_links')->insert([
|
||||
'project_id' => $project->id,
|
||||
'supplier_project_id' => $sp->id,
|
||||
'platform' => 'B1',
|
||||
'subject_code' => 77,
|
||||
]);
|
||||
|
||||
$this->postJson('/api/admin/supplier-integration/projects/delete', ['ids' => [$sp->id]])
|
||||
->assertOk()
|
||||
->assertJson(['deleted' => 1, 'failures' => []]);
|
||||
|
||||
expect(SupplierProject::find($sp->id))->toBeNull();
|
||||
expect($clientMock->calls)->toBe([999]);
|
||||
expect(DB::table('project_supplier_links')->where('supplier_project_id', $sp->id)->count())->toBe(0);
|
||||
});
|
||||
|
||||
it('POST /projects/delete validates ids array', function (): void {
|
||||
$this->actingAs(User::factory()->create());
|
||||
|
||||
$this->postJson('/api/admin/supplier-integration/projects/delete', ['ids' => []])
|
||||
->assertStatus(422);
|
||||
|
||||
$this->postJson('/api/admin/supplier-integration/projects/delete', [])
|
||||
->assertStatus(422);
|
||||
});
|
||||
|
||||
it('POST /projects/delete collects failures without aborting batch', function (): void {
|
||||
$this->actingAs(User::factory()->create());
|
||||
|
||||
$clientMock = new class extends SupplierPortalClient
|
||||
{
|
||||
public int $callsCount = 0;
|
||||
|
||||
public function __construct() {}
|
||||
|
||||
public function deleteProject(int $externalId): void
|
||||
{
|
||||
$this->callsCount++;
|
||||
if ($externalId === 555) {
|
||||
throw new RuntimeException('portal said no');
|
||||
}
|
||||
}
|
||||
};
|
||||
app()->instance(SupplierPortalClient::class, $clientMock);
|
||||
|
||||
$spOk = SupplierProject::query()->create([
|
||||
'platform' => 'B1', 'signal_type' => 'site', 'unique_key' => 'ok.ru',
|
||||
'subject_code' => 77, 'current_limit' => 0,
|
||||
'current_workdays' => [1, 2, 3, 4, 5, 6, 7], 'current_regions' => null,
|
||||
'sync_status' => 'ok', 'supplier_external_id' => '111',
|
||||
]);
|
||||
$spBad = SupplierProject::query()->create([
|
||||
'platform' => 'B1', 'signal_type' => 'site', 'unique_key' => 'bad.ru',
|
||||
'subject_code' => 77, 'current_limit' => 0,
|
||||
'current_workdays' => [1, 2, 3, 4, 5, 6, 7], 'current_regions' => null,
|
||||
'sync_status' => 'ok', 'supplier_external_id' => '555',
|
||||
]);
|
||||
|
||||
$resp = $this->postJson('/api/admin/supplier-integration/projects/delete', [
|
||||
'ids' => [$spOk->id, $spBad->id],
|
||||
])->assertOk()->json();
|
||||
|
||||
expect($resp['deleted'])->toBe(1)
|
||||
->and(count($resp['failures']))->toBe(1)
|
||||
->and($resp['failures'][0]['id'])->toBe($spBad->id)
|
||||
->and($resp['failures'][0]['error'])->toContain('portal said no');
|
||||
|
||||
expect(SupplierProject::find($spOk->id))->toBeNull();
|
||||
expect(SupplierProject::find($spBad->id))->not->toBeNull(); // bad — не удалён локально
|
||||
});
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
use App\Models\Deal;
|
||||
use App\Models\Project;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
@@ -36,12 +37,14 @@ function makeDashboardDeal(
|
||||
]);
|
||||
}
|
||||
|
||||
it('422 без tenant_id', function () {
|
||||
$this->getJson('/api/dashboard/summary')->assertStatus(422);
|
||||
});
|
||||
/** Авторизоваться как пользователь данного тенанта (auth:sanctum + tenant). */
|
||||
function actingForTenant(Tenant $tenant): void
|
||||
{
|
||||
test()->actingAs(User::factory()->for($tenant)->create());
|
||||
}
|
||||
|
||||
it('404 для несуществующего тенанта', function () {
|
||||
$this->getJson('/api/dashboard/summary?tenant_id=999999')->assertStatus(404);
|
||||
it('401 без авторизации', function () {
|
||||
$this->getJson('/api/dashboard/summary')->assertStatus(401);
|
||||
});
|
||||
|
||||
it('возвращает структуру summary с range по умолчанию 7d', function () {
|
||||
@@ -50,7 +53,8 @@ it('возвращает структуру summary с range по умолчан
|
||||
'balance_rub' => '14250.00',
|
||||
'balance_leads' => 285,
|
||||
]);
|
||||
$this->getJson("/api/dashboard/summary?tenant_id={$tenant->id}")
|
||||
actingForTenant($tenant);
|
||||
$this->getJson('/api/dashboard/summary')
|
||||
->assertOk()
|
||||
->assertJsonPath('range', '7d')
|
||||
->assertJsonPath('balance.amount_rub', '14250.00')
|
||||
@@ -67,6 +71,7 @@ it('возвращает структуру summary с range по умолчан
|
||||
|
||||
it('leads_received считает только сделки окна, без deleted и is_test', function () {
|
||||
$tenant = Tenant::factory()->create();
|
||||
actingForTenant($tenant);
|
||||
$project = Project::factory()->create(['tenant_id' => $tenant->id]);
|
||||
// 3 живые сделки в окне 7d + 1 deleted + 1 is_test + 1 вне окна (8 дней назад)
|
||||
makeDashboardDeal($tenant, $project, 'new', now()->subDays(1));
|
||||
@@ -76,31 +81,32 @@ it('leads_received считает только сделки окна, без del
|
||||
makeDashboardDeal($tenant, $project, 'new', now()->subDays(1), isTest: true);
|
||||
makeDashboardDeal($tenant, $project, 'new', now()->subDays(8));
|
||||
|
||||
$this->getJson("/api/dashboard/summary?tenant_id={$tenant->id}&range=7d")
|
||||
$this->getJson('/api/dashboard/summary?range=7d')
|
||||
->assertOk()
|
||||
->assertJsonPath('leads_received.value', 3);
|
||||
});
|
||||
|
||||
it('conversion = доля статуса won в окне', function () {
|
||||
$tenant = Tenant::factory()->create();
|
||||
actingForTenant($tenant);
|
||||
$project = Project::factory()->create(['tenant_id' => $tenant->id]);
|
||||
makeDashboardDeal($tenant, $project, 'won', now()->subDays(1));
|
||||
makeDashboardDeal($tenant, $project, 'new', now()->subDays(1));
|
||||
makeDashboardDeal($tenant, $project, 'new', now()->subDays(1));
|
||||
makeDashboardDeal($tenant, $project, 'new', now()->subDays(1));
|
||||
// 1 won из 4 → 25.0%; PHP json_encode кодирует 25.0 как 25 (без дроби)
|
||||
$this->getJson("/api/dashboard/summary?tenant_id={$tenant->id}")
|
||||
$this->getJson('/api/dashboard/summary')
|
||||
->assertOk()
|
||||
->assertJsonPath('conversion.value', 25);
|
||||
});
|
||||
|
||||
it('active_projects считает archived_at IS NULL AND is_active=true + limit из limits', function () {
|
||||
it('active_projects считает is_active=true + limit из limits', function () {
|
||||
$tenant = Tenant::factory()->create(['limits' => ['max_projects' => 10]]);
|
||||
Project::factory()->create(['tenant_id' => $tenant->id, 'archived_at' => null, 'is_active' => true]);
|
||||
Project::factory()->create(['tenant_id' => $tenant->id, 'archived_at' => null, 'is_active' => true]);
|
||||
Project::factory()->create(['tenant_id' => $tenant->id, 'archived_at' => now(), 'is_active' => true]);
|
||||
Project::factory()->create(['tenant_id' => $tenant->id, 'archived_at' => null, 'is_active' => false]);
|
||||
$this->getJson("/api/dashboard/summary?tenant_id={$tenant->id}")
|
||||
actingForTenant($tenant);
|
||||
Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
|
||||
Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
|
||||
Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => false]);
|
||||
$this->getJson('/api/dashboard/summary')
|
||||
->assertOk()
|
||||
->assertJsonPath('active_projects.active', 2)
|
||||
->assertJsonPath('active_projects.limit', 10);
|
||||
@@ -108,11 +114,12 @@ it('active_projects считает archived_at IS NULL AND is_active=true + limi
|
||||
|
||||
it('funnel группирует живые сделки по статусу', function () {
|
||||
$tenant = Tenant::factory()->create();
|
||||
actingForTenant($tenant);
|
||||
$project = Project::factory()->create(['tenant_id' => $tenant->id]);
|
||||
makeDashboardDeal($tenant, $project, 'new', now()->subDays(1));
|
||||
makeDashboardDeal($tenant, $project, 'new', now()->subDays(1));
|
||||
makeDashboardDeal($tenant, $project, 'won', now()->subDays(1));
|
||||
$this->getJson("/api/dashboard/summary?tenant_id={$tenant->id}")
|
||||
$this->getJson('/api/dashboard/summary')
|
||||
->assertOk()
|
||||
->assertJsonPath('funnel.new', 2)
|
||||
->assertJsonPath('funnel.won', 1);
|
||||
@@ -120,7 +127,8 @@ it('funnel группирует живые сделки по статусу', fu
|
||||
|
||||
it('activity возвращает 7 точек и 7 меток', function () {
|
||||
$tenant = Tenant::factory()->create();
|
||||
$this->getJson("/api/dashboard/summary?tenant_id={$tenant->id}")
|
||||
actingForTenant($tenant);
|
||||
$this->getJson('/api/dashboard/summary')
|
||||
->assertOk()
|
||||
->assertJsonCount(7, 'activity.points')
|
||||
->assertJsonCount(7, 'activity.labels');
|
||||
@@ -130,11 +138,12 @@ it('runway_days использует фикс. 7д-окно независимо
|
||||
// balance_leads = 70; 7 сделок за последние 7 дней → avgDaily=1 → runway=70.
|
||||
// Баг: range=today → $curLeads=1 → avgDaily=1/7≈0.143 → runway≈490 (неверно).
|
||||
$tenant = Tenant::factory()->create(['balance_leads' => 70]);
|
||||
actingForTenant($tenant);
|
||||
$project = Project::factory()->create(['tenant_id' => $tenant->id]);
|
||||
for ($i = 0; $i <= 6; $i++) {
|
||||
makeDashboardDeal($tenant, $project, 'new', now()->subDays($i));
|
||||
}
|
||||
$this->getJson("/api/dashboard/summary?tenant_id={$tenant->id}&range=today")
|
||||
$this->getJson('/api/dashboard/summary?range=today')
|
||||
->assertOk()
|
||||
->assertJsonPath('balance.runway_days', 70);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
uses(DatabaseTransactions::class);
|
||||
|
||||
/**
|
||||
* Go-live security: lookup/дашборд эндпоинты до этого были открыты (без
|
||||
* auth-middleware, tenant_id параметром) — любой неавторизованный мог получить
|
||||
* KPI/список пользователей произвольного тенанта по ?tenant_id={чужой}.
|
||||
*
|
||||
* Закрытие: auth:sanctum + tenant, tenant_id из authed-user (как DealController J1).
|
||||
*/
|
||||
|
||||
// --- 401 без авторизации ---
|
||||
|
||||
test('GET /api/dashboard/summary без авторизации возвращает 401', function () {
|
||||
$this->getJson('/api/dashboard/summary')->assertStatus(401);
|
||||
});
|
||||
|
||||
test('GET /api/managers без авторизации возвращает 401', function () {
|
||||
$this->getJson('/api/managers')->assertStatus(401);
|
||||
});
|
||||
|
||||
test('GET /api/lead-statuses без авторизации возвращает 401', function () {
|
||||
$this->getJson('/api/lead-statuses')->assertStatus(401);
|
||||
});
|
||||
|
||||
// --- cross-tenant: tenant_id из user, параметр чужого тенанта игнорируется ---
|
||||
|
||||
test('dashboard/summary берёт tenant из authed-user, игнорирует ?tenant_id чужого', function () {
|
||||
$mine = Tenant::factory()->create(['balance_rub' => '111.00', 'balance_leads' => 11]);
|
||||
$other = Tenant::factory()->create(['balance_rub' => '999.00', 'balance_leads' => 99]);
|
||||
$this->actingAs(User::factory()->for($mine)->create());
|
||||
|
||||
$this->getJson("/api/dashboard/summary?tenant_id={$other->id}")
|
||||
->assertOk()
|
||||
->assertJsonPath('balance.amount_rub', '111.00');
|
||||
});
|
||||
|
||||
test('managers берёт tenant из authed-user, не отдаёт пользователей чужого тенанта', function () {
|
||||
$mine = Tenant::factory()->create();
|
||||
$other = Tenant::factory()->create();
|
||||
$me = User::factory()->for($mine)->create(['first_name' => 'Свой', 'last_name' => 'Менеджер', 'is_active' => true]);
|
||||
User::factory()->for($other)->create(['first_name' => 'Чужой', 'last_name' => 'Менеджер', 'is_active' => true]);
|
||||
$this->actingAs($me);
|
||||
|
||||
$names = $this->getJson("/api/managers?tenant_id={$other->id}")
|
||||
->assertOk()
|
||||
->json('managers.*.name');
|
||||
|
||||
expect($names)->toContain('Свой М.');
|
||||
expect($names)->not->toContain('Чужой М.');
|
||||
});
|
||||
@@ -36,7 +36,7 @@ it('end-to-end: 1 webhook → 3 deal copies for 3 active tenants', function ():
|
||||
for ($i = 0; $i < 3; $i++) {
|
||||
$t = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
$tenants->push($t);
|
||||
$projects->push(Project::factory()->create([
|
||||
$project = Project::factory()->create([
|
||||
'tenant_id' => $t->id,
|
||||
'supplier_b1_project_id' => $supplier->id,
|
||||
'signal_type' => 'site',
|
||||
@@ -49,18 +49,24 @@ it('end-to-end: 1 webhook → 3 deal copies for 3 active tenants', function ():
|
||||
'region_mode' => 'include',
|
||||
'daily_limit_target' => 10,
|
||||
'effective_daily_limit_today' => null,
|
||||
]));
|
||||
]);
|
||||
$projects->push($project);
|
||||
// v8.26 (Plan 1-2): LeadRouter eligibility — через pivot project_supplier_links,
|
||||
// не legacy supplier_b1_project_id. Без pivot-связи проект не eligible → 0 сделок.
|
||||
linkProjectToSupplier($project, $supplier);
|
||||
}
|
||||
|
||||
// 4-й tenant — paused
|
||||
// 4-й tenant — paused (is_active=false). Связь в pivot есть, чтобы проверялся
|
||||
// именно фильтр is_active, а не отсутствие связи.
|
||||
$pausedTenant = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
Project::factory()->create([
|
||||
$pausedProject = Project::factory()->create([
|
||||
'tenant_id' => $pausedTenant->id,
|
||||
'supplier_b1_project_id' => $supplier->id,
|
||||
'signal_type' => 'site',
|
||||
'signal_identifier' => 'vashinvestor.ru',
|
||||
'is_active' => false,
|
||||
]);
|
||||
linkProjectToSupplier($pausedProject, $supplier);
|
||||
|
||||
$vid = 432176649;
|
||||
$response = $this->postJson('/api/webhook/supplier/test-secret-32chars-aaaaaaaaaaaaaa', [
|
||||
|
||||
@@ -27,19 +27,23 @@ test('supplier_projects table exists with required columns', function () {
|
||||
}
|
||||
});
|
||||
|
||||
test('supplier_projects has unique constraint on (platform, unique_key)', function () {
|
||||
test('supplier_projects has unique constraint on (platform, unique_key, subject_code)', function () {
|
||||
// v8.26 (project-migration-redesign Plan 1): per-субъект экспорт — composite unique
|
||||
// расширен до (platform, unique_key, subject_code) NULLS NOT DISTINCT. Старый
|
||||
// 2-колоночный индекс supplier_projects_platform_unique_key_unique заменён.
|
||||
$idx = DB::selectOne(
|
||||
"SELECT indexdef
|
||||
FROM pg_indexes
|
||||
WHERE tablename = 'supplier_projects'
|
||||
AND indexname = 'supplier_projects_platform_unique_key_unique'"
|
||||
AND indexname = 'supplier_projects_platform_key_subject_unique'"
|
||||
);
|
||||
|
||||
expect($idx)->not->toBeNull();
|
||||
expect($idx->indexdef)
|
||||
->toContain('UNIQUE')
|
||||
->toContain('platform')
|
||||
->toContain('unique_key');
|
||||
->toContain('unique_key')
|
||||
->toContain('subject_code');
|
||||
});
|
||||
|
||||
test('supplier_projects platform check constraint allows only B1, B2, B3', function () {
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
@@ -9,11 +11,23 @@ use Illuminate\Support\Facades\DB;
|
||||
* Тесты GET /api/lead-statuses — глобальный lookup статусов воронки.
|
||||
*
|
||||
* Таблица lead_statuses не tenant-aware, seeded в schema.sql (5 системных
|
||||
* статусов воронки: new/viewed/in_progress/won/lost).
|
||||
* статусов воронки: new/viewed/in_progress/won/lost). Go-live: эндпоинт за
|
||||
* auth:sanctum (глобальная таблица — tenant-middleware не нужен).
|
||||
*/
|
||||
uses(DatabaseTransactions::class);
|
||||
|
||||
/** Авторизоваться любым пользователем (lead-statuses требует только auth:sanctum). */
|
||||
function authLeadStatuses(): void
|
||||
{
|
||||
test()->actingAs(User::factory()->for(Tenant::factory())->create());
|
||||
}
|
||||
|
||||
test('GET /api/lead-statuses без авторизации возвращает 401', function () {
|
||||
$this->getJson('/api/lead-statuses')->assertStatus(401);
|
||||
});
|
||||
|
||||
test('GET /api/lead-statuses возвращает 200 и не пустой список', function () {
|
||||
authLeadStatuses();
|
||||
$r = $this->getJson('/api/lead-statuses');
|
||||
|
||||
$r->assertStatus(200);
|
||||
@@ -22,6 +36,7 @@ test('GET /api/lead-statuses возвращает 200 и не пустой сп
|
||||
});
|
||||
|
||||
test('GET /api/lead-statuses возвращает все 5 системных статусов из seed', function () {
|
||||
authLeadStatuses();
|
||||
$r = $this->getJson('/api/lead-statuses');
|
||||
|
||||
$slugs = collect($r->json('lead_statuses'))->pluck('slug')->all();
|
||||
@@ -32,6 +47,7 @@ test('GET /api/lead-statuses возвращает все 5 системных с
|
||||
});
|
||||
|
||||
test('GET /api/lead-statuses возвращает поля slug, name_ru, color_hex, sort_order, is_system', function () {
|
||||
authLeadStatuses();
|
||||
$r = $this->getJson('/api/lead-statuses');
|
||||
|
||||
$first = $r->json('lead_statuses.0');
|
||||
@@ -42,6 +58,7 @@ test('GET /api/lead-statuses возвращает поля slug, name_ru, color_
|
||||
});
|
||||
|
||||
test('GET /api/lead-statuses сортирует по sort_order', function () {
|
||||
authLeadStatuses();
|
||||
$r = $this->getJson('/api/lead-statuses');
|
||||
|
||||
$sortOrders = collect($r->json('lead_statuses'))->pluck('sort_order')->all();
|
||||
@@ -51,6 +68,7 @@ test('GET /api/lead-statuses сортирует по sort_order', function () {
|
||||
});
|
||||
|
||||
test('GET /api/lead-statuses включает кастомный slug, добавленный после seed', function () {
|
||||
authLeadStatuses();
|
||||
DB::table('lead_statuses')->insert([
|
||||
'slug' => 'custom_test_'.bin2hex(random_bytes(3)),
|
||||
'name_ru' => 'Кастомный тест',
|
||||
|
||||
@@ -15,7 +15,8 @@ beforeEach(function () {
|
||||
|
||||
test('GET /api/managers возвращает active users тенанта', function () {
|
||||
DB::statement('SET app.current_tenant_id = '.$this->tenant->id);
|
||||
User::factory()->for($this->tenant)->create([
|
||||
// actingAs одного из активных пользователей тенанта — он сам входит в список.
|
||||
$ivan = User::factory()->for($this->tenant)->create([
|
||||
'first_name' => 'Иван', 'last_name' => 'Петров', 'is_active' => true,
|
||||
]);
|
||||
User::factory()->for($this->tenant)->create([
|
||||
@@ -25,7 +26,8 @@ test('GET /api/managers возвращает active users тенанта', funct
|
||||
'first_name' => 'Удалённый', 'is_active' => false,
|
||||
]);
|
||||
|
||||
$r = $this->getJson('/api/managers?tenant_id='.$this->tenant->id);
|
||||
$this->actingAs($ivan);
|
||||
$r = $this->getJson('/api/managers');
|
||||
$r->assertStatus(200);
|
||||
$managers = $r->json('managers');
|
||||
expect($managers)->toHaveCount(2);
|
||||
@@ -35,28 +37,23 @@ test('GET /api/managers возвращает active users тенанта', funct
|
||||
|
||||
test('GET /api/managers возвращает initials с fallback на email', function () {
|
||||
DB::statement('SET app.current_tenant_id = '.$this->tenant->id);
|
||||
User::factory()->for($this->tenant)->create([
|
||||
$admin = User::factory()->for($this->tenant)->create([
|
||||
'email' => 'admin@example.ru',
|
||||
'first_name' => null,
|
||||
'last_name' => null,
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$r = $this->getJson('/api/managers?tenant_id='.$this->tenant->id);
|
||||
$this->actingAs($admin);
|
||||
$r = $this->getJson('/api/managers');
|
||||
$r->assertStatus(200);
|
||||
$manager = $r->json('managers.0');
|
||||
expect($manager['name'])->toBe('admin@example.ru');
|
||||
expect($manager['initials'])->toBe('AD');
|
||||
});
|
||||
|
||||
test('GET /api/managers 422 без tenant_id', function () {
|
||||
$r = $this->getJson('/api/managers');
|
||||
$r->assertStatus(422);
|
||||
});
|
||||
|
||||
test('GET /api/managers 404 unknown tenant', function () {
|
||||
$r = $this->getJson('/api/managers?tenant_id=999999');
|
||||
$r->assertStatus(404);
|
||||
test('GET /api/managers без авторизации возвращает 401', function () {
|
||||
$this->getJson('/api/managers')->assertStatus(401);
|
||||
});
|
||||
|
||||
test('POST /api/deals 422 если manager_id не принадлежит tenant\'у', function () {
|
||||
|
||||
@@ -59,26 +59,28 @@ it('supplier_csv_reconcile_log table exists with required columns and status CHE
|
||||
]))->toThrow(QueryException::class);
|
||||
});
|
||||
|
||||
it('schema.sql v8.25 has correct metrics — 64 base tables, 121 indexes, 40 RLS policies', function () {
|
||||
it('schema.sql v8.26 has correct metrics — 65 base tables, 123 indexes, 40 RLS policies', function () {
|
||||
// Замена destructive `migrate:fresh` (cross-test coupling: после DROP CASCADE остальные
|
||||
// Feature-тесты в той же сессии видели пустую БД). Static parse `db/schema.sql` —
|
||||
// источник истины метрик из spec §2.4 / db/CHANGELOG_schema.md v8.25.
|
||||
// источник истины метрик из spec §2.4 / db/CHANGELOG_schema.md v8.26.
|
||||
// v8.21 (Sprint 4): +1 таблица import_unknown_statuses, +1 индекс, +1 RLS-политика.
|
||||
// v8.22 (Plan 6/C9): +1 GIN-индекс idx_projects_regions.
|
||||
// v8.25 (supplier-failover): +1 таблица supplier_manual_sync_queue, +2 индекса.
|
||||
// v8.26 (project-migration-redesign Plans 1-3): +1 таблица project_supplier_links (M:N pivot)
|
||||
// + 2 индекса (supplier_projects_platform_key_subject_unique, idx_psl_*).
|
||||
$schemaPath = dirname(base_path()).DIRECTORY_SEPARATOR.'db'.DIRECTORY_SEPARATOR.'schema.sql';
|
||||
expect(is_file($schemaPath) && is_readable($schemaPath))->toBeTrue();
|
||||
$schema = file_get_contents($schemaPath);
|
||||
expect($schema)->not->toBeFalse();
|
||||
|
||||
// 64 base tables = все CREATE TABLE минус 12 партиций (PARTITION OF).
|
||||
// 65 base tables = все CREATE TABLE минус 12 партиций (PARTITION OF).
|
||||
$createTables = preg_match_all('/^CREATE TABLE\b/m', $schema);
|
||||
$partitionOf = preg_match_all('/CREATE TABLE\s+\w+\s+PARTITION OF\b/m', $schema);
|
||||
$baseTables = $createTables - $partitionOf;
|
||||
expect($baseTables)->toBe(64);
|
||||
expect($baseTables)->toBe(65);
|
||||
|
||||
$createIndexes = preg_match_all('/^CREATE\s+(?:UNIQUE\s+)?INDEX\b/m', $schema);
|
||||
expect($createIndexes)->toBe(121); // v8.25: +2 idx_smsq_status_created, idx_smsq_project
|
||||
expect($createIndexes)->toBe(123); // v8.26: +2 supplier_projects_platform_key_subject_unique, idx_psl_*
|
||||
|
||||
$createPolicies = preg_match_all('/^CREATE\s+POLICY\b/m', $schema);
|
||||
expect($createPolicies)->toBe(40);
|
||||
|
||||
@@ -8,10 +8,13 @@ use App\Models\SupplierProject;
|
||||
use App\Models\Tenant;
|
||||
use App\Services\Supplier\Channel\SupplierProjectChannel;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Tests\Concerns\SharesSupplierPdo;
|
||||
|
||||
// TestCase auto-bound via tests/Pest.php (->in('Feature')).
|
||||
// DatabaseTransactions — per-test isolation.
|
||||
uses(DatabaseTransactions::class);
|
||||
// SharesSupplierPdo — SyncSupplierProjectJob теперь пишет через pgsql_supplier (BYPASSRLS);
|
||||
// без шаринга PDO записи джоба не видны default-connection ассертам под DatabaseTransactions.
|
||||
uses(DatabaseTransactions::class, SharesSupplierPdo::class);
|
||||
|
||||
/**
|
||||
* Хелпер: разрешает SupplierProjectChannel из контейнера и вызывает Job.handle().
|
||||
|
||||
@@ -6,28 +6,34 @@ use App\Jobs\SyncSupplierProjectJob;
|
||||
use App\Models\Project;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
|
||||
beforeEach(fn () => Queue::fake());
|
||||
|
||||
it('destroy archives project (sets archived_at, is_active=false)', function () {
|
||||
it('destroy hard-deletes a project with no deals', function () {
|
||||
$tenant = Tenant::factory()->create();
|
||||
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
||||
$project = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
|
||||
|
||||
$this->actingAs($user)->deleteJson("/api/projects/{$project->id}")->assertNoContent();
|
||||
|
||||
$project->refresh();
|
||||
expect($project->is_active)->toBeFalse();
|
||||
expect($project->archived_at)->not->toBeNull();
|
||||
expect(Project::find($project->id))->toBeNull();
|
||||
});
|
||||
|
||||
it('destroy returns 409 if already archived', function () {
|
||||
it('destroy returns 422 if project has deals', function () {
|
||||
$tenant = Tenant::factory()->create();
|
||||
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
||||
$project = Project::factory()->create(['tenant_id' => $tenant->id, 'archived_at' => now()]);
|
||||
$project = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
|
||||
DB::table('deals')->insert([
|
||||
'tenant_id' => $tenant->id, 'project_id' => $project->id,
|
||||
'phone' => '79990001100', 'status' => 'new',
|
||||
'received_at' => now(), 'created_at' => now(),
|
||||
]);
|
||||
|
||||
$this->actingAs($user)->deleteJson("/api/projects/{$project->id}")->assertStatus(409);
|
||||
$this->actingAs($user)->deleteJson("/api/projects/{$project->id}")->assertStatus(422);
|
||||
|
||||
expect(Project::find($project->id))->not->toBeNull();
|
||||
});
|
||||
|
||||
it('sync re-dispatches SyncSupplierProjectJob', function () {
|
||||
@@ -81,16 +87,16 @@ it('bulk filters out cross-tenant ids silently', function () {
|
||||
expect($pB->fresh()->is_active)->toBeTrue();
|
||||
});
|
||||
|
||||
it('bulk archive sets archived_at on multiple', function () {
|
||||
it('bulk delete removes project with no deals', function () {
|
||||
$tenant = Tenant::factory()->create();
|
||||
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
||||
$p1 = Project::factory()->create(['tenant_id' => $tenant->id]);
|
||||
|
||||
$this->actingAs($user)->postJson('/api/projects/bulk', [
|
||||
'action' => 'archive', 'ids' => [$p1->id],
|
||||
])->assertOk();
|
||||
'action' => 'delete', 'ids' => [$p1->id],
|
||||
])->assertOk()->assertJsonPath('updated', 1);
|
||||
|
||||
expect($p1->fresh()->archived_at)->not->toBeNull();
|
||||
expect(Project::find($p1->id))->toBeNull();
|
||||
});
|
||||
|
||||
it('bulk rejects > 500 ids', function () {
|
||||
|
||||
@@ -16,7 +16,7 @@ it('returns paginated list of active projects for current tenant', function () {
|
||||
$response->assertOk();
|
||||
$response->assertJsonStructure([
|
||||
'data' => [['id', 'name', 'signal_type', 'signal_identifier', 'daily_limit_target',
|
||||
'delivered_today', 'is_active', 'archived_at', 'sync_status']],
|
||||
'delivered_today', 'is_active', 'sync_status']],
|
||||
'meta' => ['current_page', 'per_page', 'total'],
|
||||
]);
|
||||
expect($response->json('meta.total'))->toBe(3);
|
||||
@@ -45,23 +45,24 @@ it('isolates projects per tenant (RLS)', function () {
|
||||
expect($response->json('meta.total'))->toBe(2);
|
||||
});
|
||||
|
||||
it('excludes archived projects by default', function () {
|
||||
it('returns all projects by default (archive feature removed in v8.27)', function () {
|
||||
$tenant = Tenant::factory()->create();
|
||||
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
||||
Project::factory()->create(['tenant_id' => $tenant->id]);
|
||||
Project::factory()->create(['tenant_id' => $tenant->id, 'archived_at' => now()]);
|
||||
Project::factory()->create(['tenant_id' => $tenant->id]);
|
||||
|
||||
$response = $this->actingAs($user)->getJson('/api/projects');
|
||||
|
||||
expect($response->json('meta.total'))->toBe(1);
|
||||
expect($response->json('meta.total'))->toBe(2);
|
||||
});
|
||||
|
||||
it('returns archived when status=archived requested', function () {
|
||||
it('status=active returns only is_active=true projects', function () {
|
||||
$tenant = Tenant::factory()->create();
|
||||
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
||||
Project::factory()->create(['tenant_id' => $tenant->id, 'archived_at' => now()]);
|
||||
Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
|
||||
Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => false]);
|
||||
|
||||
$response = $this->actingAs($user)->getJson('/api/projects?status=archived');
|
||||
$response = $this->actingAs($user)->getJson('/api/projects?status=active');
|
||||
|
||||
expect($response->json('meta.total'))->toBe(1);
|
||||
});
|
||||
@@ -140,19 +141,18 @@ it('search is case-insensitive for Cyrillic substrings', function () {
|
||||
expect($partial->json('meta.total'))->toBe(1);
|
||||
});
|
||||
|
||||
it('show returns 200 for archived project (read access preserved)', function () {
|
||||
it('show returns 200 for any project by id', function () {
|
||||
$tenant = Tenant::factory()->create();
|
||||
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
||||
$project = Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'archived_at' => now(),
|
||||
'signal_type' => 'site',
|
||||
'signal_identifier' => 'archived.ru',
|
||||
'signal_identifier' => 'myproject.ru',
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->getJson("/api/projects/{$project->id}");
|
||||
|
||||
$response->assertOk();
|
||||
expect($response->json('data.id'))->toBe($project->id);
|
||||
expect($response->json('data.archived_at'))->not->toBeNull();
|
||||
expect($response->json('data'))->not->toHaveKey('archived_at');
|
||||
});
|
||||
|
||||
@@ -10,7 +10,7 @@ use Illuminate\Support\Facades\Queue;
|
||||
|
||||
beforeEach(fn () => Queue::fake());
|
||||
|
||||
it('updates name+daily_limit without resync', function () {
|
||||
it('updates name without resync (name is local-only)', function () {
|
||||
$tenant = Tenant::factory()->create();
|
||||
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
||||
$project = Project::factory()->create([
|
||||
@@ -19,14 +19,45 @@ it('updates name+daily_limit without resync', function () {
|
||||
]);
|
||||
|
||||
$this->actingAs($user)->patchJson("/api/projects/{$project->id}", [
|
||||
'name' => 'New name', 'daily_limit_target' => 50,
|
||||
'name' => 'New name',
|
||||
])->assertOk();
|
||||
|
||||
expect($project->fresh()->name)->toBe('New name');
|
||||
expect($project->fresh()->daily_limit_target)->toBe(50);
|
||||
Queue::assertNotPushed(SyncSupplierProjectJob::class);
|
||||
});
|
||||
|
||||
it('changing daily_limit_target triggers resync (poster must see new limit immediately)', function () {
|
||||
$tenant = Tenant::factory()->create();
|
||||
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
||||
$project = Project::factory()->create([
|
||||
'tenant_id' => $tenant->id, 'signal_type' => 'site',
|
||||
'signal_identifier' => 'a.ru', 'daily_limit_target' => 10,
|
||||
]);
|
||||
|
||||
$this->actingAs($user)->patchJson("/api/projects/{$project->id}", [
|
||||
'daily_limit_target' => 50,
|
||||
])->assertOk();
|
||||
|
||||
expect($project->fresh()->daily_limit_target)->toBe(50);
|
||||
Queue::assertPushed(SyncSupplierProjectJob::class);
|
||||
});
|
||||
|
||||
it('changing delivery_days_mask triggers resync (poster must see new days immediately)', function () {
|
||||
$tenant = Tenant::factory()->create();
|
||||
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
||||
$project = Project::factory()->create([
|
||||
'tenant_id' => $tenant->id, 'signal_type' => 'site',
|
||||
'signal_identifier' => 'a.ru', 'delivery_days_mask' => 31,
|
||||
]);
|
||||
|
||||
$this->actingAs($user)->patchJson("/api/projects/{$project->id}", [
|
||||
'delivery_days_mask' => 63, // +Сб
|
||||
])->assertOk();
|
||||
|
||||
expect($project->fresh()->delivery_days_mask)->toBe(63);
|
||||
Queue::assertPushed(SyncSupplierProjectJob::class);
|
||||
});
|
||||
|
||||
it('changing sms_senders triggers resync', function () {
|
||||
$tenant = Tenant::factory()->create();
|
||||
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\Project;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
@@ -11,15 +10,6 @@ use Illuminate\Support\Facades\Schema;
|
||||
// DatabaseTransactions — изоляция; also ensures DB connection is bootstrapped.
|
||||
uses(DatabaseTransactions::class);
|
||||
|
||||
it('projects table has archived_at column nullable timestamp', function () {
|
||||
expect(Schema::hasColumn('projects', 'archived_at'))->toBeTrue();
|
||||
$type = Schema::getColumnType('projects', 'archived_at');
|
||||
// PostgreSQL TIMESTAMPTZ → Doctrine/Laravel reports 'timestamptz' (not 'timestamp').
|
||||
expect($type)->toBe('timestamptz');
|
||||
});
|
||||
|
||||
it('Project model has archived_at in fillable and casts it to datetime', function () {
|
||||
$project = new Project;
|
||||
expect(in_array('archived_at', $project->getFillable(), true))->toBeTrue();
|
||||
expect($project->getCasts()['archived_at'] ?? null)->toBe('datetime');
|
||||
it('projects table does NOT have archived_at column (feature removed in v8.27)', function () {
|
||||
expect(Schema::hasColumn('projects', 'archived_at'))->toBeFalse();
|
||||
});
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\Tenant;
|
||||
use App\Services\Project\ProjectService;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
|
||||
beforeEach(function () {
|
||||
Queue::fake();
|
||||
$this->tenant = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
});
|
||||
|
||||
function makeCall(array $over = []): array
|
||||
{
|
||||
return array_merge([
|
||||
'name' => 'Проект A', 'signal_type' => 'call', 'signal_identifier' => '79991110000',
|
||||
'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31,
|
||||
], $over);
|
||||
}
|
||||
|
||||
it('blocks duplicate source within tenant with human message', function () {
|
||||
app(ProjectService::class)->create($this->tenant, makeCall());
|
||||
expect(fn () => app(ProjectService::class)
|
||||
->create($this->tenant, makeCall(['name' => 'Проект B'])))
|
||||
->toThrow(HttpResponseException::class);
|
||||
});
|
||||
|
||||
it('allows same source for a different tenant (sharing)', function () {
|
||||
$other = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
app(ProjectService::class)->create($this->tenant, makeCall());
|
||||
$p = app(ProjectService::class)->create($other, makeCall(['name' => 'Проект B']));
|
||||
expect($p)->toBeInstanceOf(Project::class);
|
||||
});
|
||||
|
||||
it('blocks duplicate name within tenant with human message (not SQL)', function () {
|
||||
app(ProjectService::class)->create($this->tenant, makeCall());
|
||||
try {
|
||||
app(ProjectService::class)
|
||||
->create($this->tenant, makeCall(['name' => 'Проект A', 'signal_identifier' => '79992220000']));
|
||||
$this->fail('expected HttpResponseException');
|
||||
} catch (HttpResponseException $e) {
|
||||
$body = $e->getResponse()->getData(true);
|
||||
expect($body['errors']['name'][0] ?? '')->not->toContain('SQLSTATE');
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\Tenant;
|
||||
use App\Services\Project\ProjectService;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
|
||||
beforeEach(fn () => Queue::fake());
|
||||
|
||||
it('hard-deletes an empty project', function () {
|
||||
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
$project = app(ProjectService::class)->create($tenant, [
|
||||
'name' => 'Empty', 'signal_type' => 'call', 'signal_identifier' => '79991110000',
|
||||
'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31,
|
||||
]);
|
||||
|
||||
app(ProjectService::class)->delete($project);
|
||||
|
||||
expect(Project::find($project->id))->toBeNull();
|
||||
});
|
||||
|
||||
it('blocks delete when project has deals', function () {
|
||||
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
$project = app(ProjectService::class)->create($tenant, [
|
||||
'name' => 'WithDeals', 'signal_type' => 'call', 'signal_identifier' => '79991110000',
|
||||
'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31,
|
||||
]);
|
||||
DB::table('deals')->insert([
|
||||
'tenant_id' => $tenant->id, 'project_id' => $project->id, 'phone' => '79990001122',
|
||||
'status' => 'new', 'received_at' => now(), 'created_at' => now(),
|
||||
]);
|
||||
|
||||
expect(fn () => app(ProjectService::class)->delete($project))
|
||||
->toThrow(HttpResponseException::class);
|
||||
expect(Project::find($project->id))->not->toBeNull();
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\Tenant;
|
||||
use App\Services\Project\ProjectService;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
|
||||
beforeEach(fn () => Queue::fake());
|
||||
|
||||
it('blocks update that collides source with another project of same tenant', function () {
|
||||
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
$svc = app(ProjectService::class);
|
||||
$a = $svc->create($tenant, ['name' => 'A', 'signal_type' => 'call', 'signal_identifier' => '79991110000', 'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31]);
|
||||
$b = $svc->create($tenant, ['name' => 'B', 'signal_type' => 'call', 'signal_identifier' => '79992220000', 'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31]);
|
||||
|
||||
expect(fn () => $svc->update($b, ['signal_identifier' => '79991110000']))
|
||||
->toThrow(HttpResponseException::class);
|
||||
});
|
||||
|
||||
it('allows update keeping same source on the same project', function () {
|
||||
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
$svc = app(ProjectService::class);
|
||||
$a = $svc->create($tenant, ['name' => 'A', 'signal_type' => 'call', 'signal_identifier' => '79991110000', 'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31]);
|
||||
$updated = $svc->update($a, ['signal_identifier' => '79991110000', 'daily_limit_target' => 7]);
|
||||
expect($updated->daily_limit_target)->toBe(7);
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
it('renders QueryException as human JSON message, not SQLSTATE', function () {
|
||||
Route::get('/_test/boom-query', function () {
|
||||
throw new QueryException('pgsql', 'SELECT 1', [], new Exception('SQLSTATE[23505] duplicate key'));
|
||||
});
|
||||
|
||||
$res = $this->getJson('/_test/boom-query');
|
||||
$res->assertStatus(422);
|
||||
expect($res->json('message'))->not->toContain('SQLSTATE');
|
||||
expect($res->json('message'))->toContain('Не удалось');
|
||||
});
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Jobs\Supplier\DeleteSupplierProjectJob;
|
||||
use App\Jobs\Supplier\SyncSupplierProjectsJob;
|
||||
use App\Models\Project;
|
||||
use App\Models\SupplierProject;
|
||||
use App\Models\Tenant;
|
||||
use App\Services\Supplier\SupplierPortalClient;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Tests\Concerns\SharesSupplierPdo;
|
||||
|
||||
uses(DatabaseTransactions::class, SharesSupplierPdo::class);
|
||||
|
||||
it('deletes donor at supplier when no consumers remain', function (): void {
|
||||
$sp = SupplierProject::query()->create([
|
||||
'platform' => 'B1', 'signal_type' => 'call', 'unique_key' => '79991110000',
|
||||
'supplier_external_id' => '555', 'current_limit' => 1,
|
||||
]);
|
||||
|
||||
$mock = Mockery::mock(SupplierPortalClient::class);
|
||||
$mock->shouldReceive('deleteProject')->once()->with(555);
|
||||
app()->instance(SupplierPortalClient::class, $mock);
|
||||
|
||||
(new DeleteSupplierProjectJob([$sp->id]))->handle(app(SupplierPortalClient::class));
|
||||
|
||||
expect(SupplierProject::find($sp->id))->toBeNull();
|
||||
});
|
||||
|
||||
it('does NOT delete donor at supplier when other consumers remain; re-syncs', function (): void {
|
||||
Bus::fake([SyncSupplierProjectsJob::class]);
|
||||
|
||||
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
$sp = SupplierProject::query()->create([
|
||||
'platform' => 'B1', 'signal_type' => 'call', 'unique_key' => '79991110001',
|
||||
'supplier_external_id' => '556', 'current_limit' => 1,
|
||||
]);
|
||||
$other = Project::factory()->create(['tenant_id' => $tenant->id]);
|
||||
|
||||
DB::table('project_supplier_links')->insert([
|
||||
'project_id' => $other->id,
|
||||
'supplier_project_id' => $sp->id,
|
||||
'platform' => 'B1',
|
||||
'subject_code' => null,
|
||||
]);
|
||||
|
||||
$mock = Mockery::mock(SupplierPortalClient::class);
|
||||
$mock->shouldNotReceive('deleteProject');
|
||||
app()->instance(SupplierPortalClient::class, $mock);
|
||||
|
||||
(new DeleteSupplierProjectJob([$sp->id]))->handle(app(SupplierPortalClient::class));
|
||||
|
||||
expect(SupplierProject::find($sp->id))->not->toBeNull();
|
||||
Bus::assertDispatched(SyncSupplierProjectsJob::class);
|
||||
});
|
||||
@@ -21,6 +21,7 @@ declare(strict_types=1);
|
||||
*/
|
||||
|
||||
use App\Jobs\RouteSupplierLeadJob;
|
||||
use App\Jobs\SyncSupplierProjectJob;
|
||||
use App\Models\Project;
|
||||
use App\Models\SupplierProject;
|
||||
use App\Models\Tenant;
|
||||
@@ -46,6 +47,14 @@ test('RouteSupplierLeadJob declares DB_CONNECTION = pgsql_supplier (Plan 3 Task
|
||||
expect(RouteSupplierLeadJob::DB_CONNECTION)->toBe('pgsql_supplier');
|
||||
});
|
||||
|
||||
test('SyncSupplierProjectJob declares DB_CONNECTION = pgsql_supplier (queue worker has no tenant GUC)', function (): void {
|
||||
// Дублирует RouteSupplierLeadJob: создание/правка проекта тоже запускается из очереди,
|
||||
// где SetTenantContext-прослойка не отработала. Под обычной ролью crm_app_user
|
||||
// SELECT по projects падает 42704 (unrecognized configuration parameter
|
||||
// "app.current_tenant_id"). Все DB-операции джоба обязаны идти через pgsql_supplier (BYPASSRLS).
|
||||
expect(SyncSupplierProjectJob::DB_CONNECTION)->toBe('pgsql_supplier');
|
||||
});
|
||||
|
||||
test('failed_webhook_jobs INSERT с tenant_id=NULL проходит под pgsql_supplier (BLOCKER #6)', function (): void {
|
||||
// Под обычной ролью policy tenant_isolation USING (tenant_id = current_setting('app.current_tenant_id')::bigint)
|
||||
// отвергает NULL (NULL :: bigint = NULL, NULL = '0'::bigint → NULL → false).
|
||||
|
||||
@@ -9,11 +9,12 @@ use Illuminate\Support\Facades\Http;
|
||||
it('multi-flag save returns external_id per platform via listProjects', function (): void {
|
||||
Http::fake([
|
||||
'*/admin/visit/rt-project-save' => Http::response(['status' => 'OK', 'id' => '300'], 200),
|
||||
// Real portal returns name='B1_<identifier>' with the identifier in 'content'.
|
||||
'*/admin/visit/rt-projects-load*' => Http::response(['projects' => [
|
||||
['id' => '100', 'name' => 'okna.ru', 'tag' => 'Москва', 'src' => 'rt'],
|
||||
['id' => '200', 'name' => 'okna.ru', 'tag' => 'Москва', 'src' => 'bl'],
|
||||
['id' => '300', 'name' => 'okna.ru', 'tag' => 'Москва', 'src' => 'mt'],
|
||||
['id' => '999', 'name' => 'other.ru', 'tag' => 'Москва', 'src' => 'rt'],
|
||||
['id' => '100', 'name' => 'B1_okna.ru', 'content' => 'okna.ru', 'tag' => 'Москва', 'src' => 'rt'],
|
||||
['id' => '200', 'name' => 'B2_okna.ru', 'content' => 'okna.ru', 'tag' => 'Москва', 'src' => 'bl'],
|
||||
['id' => '300', 'name' => 'B3_okna.ru', 'content' => 'okna.ru', 'tag' => 'Москва', 'src' => 'mt'],
|
||||
['id' => '999', 'name' => 'B1_other.ru', 'content' => 'other.ru', 'tag' => 'Москва', 'src' => 'rt'],
|
||||
]], 200),
|
||||
]);
|
||||
|
||||
|
||||
@@ -99,7 +99,9 @@ it('saveProject maps signalType call → type:"calls" and B2 → srcbl=true (sin
|
||||
&& $request['srcrt'] === false
|
||||
&& $request['srcbl'] === true
|
||||
&& $request['srcmt'] === false
|
||||
&& $request['regions'] === [77]
|
||||
// Лидерра-код 77 (Тюменская обл., конституционный порядок) переводится
|
||||
// в код поставщика 72 (ГИБДД). См. App\Support\SupplierRegions.
|
||||
&& $request['regions'] === [72]
|
||||
&& $request['regions_reverse'] === true
|
||||
&& $request['status'] === false;
|
||||
});
|
||||
|
||||
@@ -38,10 +38,10 @@ afterEach(function (): void {
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Online mode: per-subject supplier_projects + pivot
|
||||
// Online mode: single-group supplier_projects + pivot
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
it('online mode creates per-subject supplier_projects with full params + pivot', function (): void {
|
||||
it('online mode creates single-group supplier_projects with full regions + pivot', function (): void {
|
||||
DB::table('system_settings')->where('key', 'supplier_export_mode')->update(['value' => 'online']);
|
||||
|
||||
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
@@ -73,13 +73,170 @@ it('online mode creates per-subject supplier_projects with full params + pivot',
|
||||
|
||||
(new SyncSupplierProjectJob($project->id))->handle(app(SupplierProjectChannel::class));
|
||||
|
||||
// 3 supplier_projects: subject_code=82, platforms B1/B2/B3
|
||||
expect(SupplierProject::where('unique_key', 'okna.ru')->where('subject_code', 82)->count())->toBe(3);
|
||||
// 3 supplier_projects: subject_code=null (single group), platforms B1/B2/B3
|
||||
expect(SupplierProject::where('unique_key', 'okna.ru')->whereNull('subject_code')->count())->toBe(3);
|
||||
|
||||
// pivot: 3 links for this project
|
||||
expect(DB::table('project_supplier_links')->where('project_id', $project->id)->count())->toBe(3);
|
||||
});
|
||||
|
||||
it('online create DIVIDES the limit across B1/B2/B3 so supplier total == project limit (not ×3)', function (): void {
|
||||
// Money-loss regression (owner-reported 2026-05-21, verified live): the limit was
|
||||
// replicated full to all 3 platforms (18 → 18/18/18 = supplier could deliver up to 54).
|
||||
// The portal does NOT divide — each B-project honours its own limit independently.
|
||||
// Fix: split the limit so Σ per-platform == project limit (18 → 6/6/6).
|
||||
DB::table('system_settings')->where('key', 'supplier_export_mode')->update(['value' => 'online']);
|
||||
|
||||
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
$project = Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'signal_type' => 'call',
|
||||
'signal_identifier' => '79991110000',
|
||||
'is_active' => true,
|
||||
'daily_limit_target' => 18,
|
||||
'regions' => [],
|
||||
'delivery_days_mask' => 127,
|
||||
]);
|
||||
|
||||
$capturedLimits = [];
|
||||
Http::fake([
|
||||
'crm.bp-gr.ru/admin/visit/rt-project-save' => function ($request) use (&$capturedLimits) {
|
||||
$body = $request->data();
|
||||
$capturedLimits[] = $body['limit'] ?? null;
|
||||
|
||||
return Http::response(['status' => 'OK', 'message' => '', 'id' => '3000'], 200);
|
||||
},
|
||||
'crm.bp-gr.ru/admin/visit/rt-projects-load*' => Http::response(['projects' => [
|
||||
['id' => '3001', 'src' => 'rt', 'name' => '79991110000', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79991110000'],
|
||||
['id' => '3002', 'src' => 'bl', 'name' => '79991110000', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79991110000'],
|
||||
['id' => '3003', 'src' => 'mt', 'name' => '79991110000', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79991110000'],
|
||||
]], 200),
|
||||
]);
|
||||
|
||||
(new SyncSupplierProjectJob($project->id))->handle(app(SupplierProjectChannel::class));
|
||||
|
||||
$sps = SupplierProject::where('unique_key', '79991110000')->get();
|
||||
expect($sps)->toHaveCount(3);
|
||||
// Σ per-platform limits == the project limit — the loss-prevention invariant.
|
||||
expect($sps->sum('current_limit'))->toBe(18);
|
||||
foreach ($sps as $sp) {
|
||||
expect($sp->current_limit)->toBe(6); // 18 / 3 platforms
|
||||
}
|
||||
// Every limit pushed to the portal is the divided share, never the full 18.
|
||||
$sent = array_values(array_filter($capturedLimits, fn ($l) => $l !== null));
|
||||
expect($sent)->not->toBeEmpty();
|
||||
foreach ($sent as $l) {
|
||||
expect((int) $l)->toBe(6);
|
||||
}
|
||||
});
|
||||
|
||||
it('online mode passes real workdays from delivery_days_mask (not hardcoded [1..7])', function (): void {
|
||||
// Regression: до фикса хардкодилось [1,2,3,4,5,6,7] независимо от delivery_days_mask.
|
||||
// delivery_days_mask=31 = 0b0011111 = Пн-Пт (ISO дни 1-5). Workdays поставщика должны быть [1,2,3,4,5].
|
||||
DB::table('system_settings')->where('key', 'supplier_export_mode')->update(['value' => 'online']);
|
||||
|
||||
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
$project = Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'signal_type' => 'call',
|
||||
'signal_identifier' => '79135191264',
|
||||
'is_active' => true,
|
||||
'daily_limit_target' => 15,
|
||||
'regions' => [],
|
||||
'delivery_days_mask' => 31, // Пн-Пт
|
||||
]);
|
||||
|
||||
$capturedWorkdays = null;
|
||||
Http::fake([
|
||||
'crm.bp-gr.ru/admin/visit/rt-project-save' => function ($request) use (&$capturedWorkdays) {
|
||||
$body = $request->data();
|
||||
if (isset($body['workdays'])) {
|
||||
$capturedWorkdays = $body['workdays'];
|
||||
}
|
||||
|
||||
return Http::response(['status' => 'OK', 'message' => '', 'result' => null, 'id' => '2001'], 200);
|
||||
},
|
||||
'crm.bp-gr.ru/admin/visit/rt-projects-load*' => Http::response(
|
||||
['projects' => [
|
||||
['id' => '2001', 'src' => 'rt', 'name' => '79135191264', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79135191264'],
|
||||
['id' => '2002', 'src' => 'bl', 'name' => '79135191264', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79135191264'],
|
||||
['id' => '2003', 'src' => 'mt', 'name' => '79135191264', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79135191264'],
|
||||
]],
|
||||
200,
|
||||
),
|
||||
]);
|
||||
|
||||
(new SyncSupplierProjectJob($project->id))->handle(app(SupplierProjectChannel::class));
|
||||
|
||||
// 1) supplier_projects записаны с реальными буднями, не all-7.
|
||||
$sps = SupplierProject::where('unique_key', '79135191264')->get();
|
||||
expect($sps)->toHaveCount(3);
|
||||
foreach ($sps as $sp) {
|
||||
expect($sp->current_workdays)->toBe([1, 2, 3, 4, 5]);
|
||||
}
|
||||
|
||||
// 2) HTTP payload к порталу содержал ["1","2","3","4","5"], не ["1".."7"].
|
||||
expect($capturedWorkdays)->toBe(['1', '2', '3', '4', '5']);
|
||||
});
|
||||
|
||||
it('online mode update-path: existing supplier_projects.current_workdays is refreshed (not just regions/limit)', function (): void {
|
||||
// Regression: forceFill ранее не включал current_workdays — после первого create со
|
||||
// старым хардкод-[1..7] последующий ресинк не подтягивал реальные дни.
|
||||
DB::table('system_settings')->where('key', 'supplier_export_mode')->update(['value' => 'online']);
|
||||
|
||||
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
$project = Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'signal_type' => 'call',
|
||||
'signal_identifier' => '79991234567',
|
||||
'is_active' => true,
|
||||
'daily_limit_target' => 9,
|
||||
'regions' => [],
|
||||
'delivery_days_mask' => 31, // Пн-Пт
|
||||
]);
|
||||
|
||||
// Pre-seed existing supplier_projects со старыми (хардкод-)workdays.
|
||||
foreach (['B1', 'B2', 'B3'] as $platform) {
|
||||
SupplierProject::create([
|
||||
'platform' => $platform,
|
||||
'signal_type' => 'call',
|
||||
'unique_key' => '79991234567',
|
||||
'subject_code' => null,
|
||||
'supplier_external_id' => '99'.$platform,
|
||||
'current_limit' => 6,
|
||||
'current_workdays' => [1, 2, 3, 4, 5, 6, 7],
|
||||
'current_regions' => [],
|
||||
'sync_status' => 'ok',
|
||||
'last_synced_at' => now()->subDay(),
|
||||
]);
|
||||
}
|
||||
|
||||
// listProjects (dead-donor liveness check) must see the seeded donors as alive,
|
||||
// so the update path runs without recreating (and without hitting the real portal).
|
||||
Http::fake([
|
||||
'crm.bp-gr.ru/admin/visit/rt-projects-load*' => Http::response(['projects' => [
|
||||
['id' => '99B1', 'src' => 'rt', 'name' => '79991234567', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79991234567'],
|
||||
['id' => '99B2', 'src' => 'bl', 'name' => '79991234567', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79991234567'],
|
||||
['id' => '99B3', 'src' => 'mt', 'name' => '79991234567', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79991234567'],
|
||||
]], 200),
|
||||
]);
|
||||
|
||||
$this->mock(SupplierProjectChannel::class, function ($mock): void {
|
||||
$mock->shouldReceive('updateProject')->times(3)->andReturn(true);
|
||||
});
|
||||
|
||||
(new SyncSupplierProjectJob($project->id))->handle(app(SupplierProjectChannel::class));
|
||||
|
||||
$sps = SupplierProject::where('unique_key', '79991234567')->get();
|
||||
expect($sps)->toHaveCount(3);
|
||||
// 9 split across B1/B2/B3 = 3/3/3 (Σ == 9 = project limit, not 9 on each = 27).
|
||||
expect($sps->sum('current_limit'))->toBe(9);
|
||||
foreach ($sps as $sp) {
|
||||
expect($sp->current_workdays)->toBe([1, 2, 3, 4, 5]);
|
||||
expect($sp->current_limit)->toBe(3);
|
||||
}
|
||||
});
|
||||
|
||||
it('online mode all-RF (no regions): 1 group subject_code=null, 3 supplier_projects + 3 pivot links', function (): void {
|
||||
DB::table('system_settings')->where('key', 'supplier_export_mode')->update(['value' => 'online']);
|
||||
|
||||
@@ -118,6 +275,103 @@ it('online mode all-RF (no regions): 1 group subject_code=null, 3 supplier_proje
|
||||
expect(DB::table('project_supplier_links')->where('project_id', $project->id)->count())->toBe(3);
|
||||
});
|
||||
|
||||
it('online mode re-creates donor on portal when its external_id no longer exists there', function (): void {
|
||||
// Regression: если донора удалили на портале, в нашей БД остаются supplier_projects
|
||||
// с мёртвыми external_id. Раньше джоб шёл по update-ветке → updateProject мёртвого id
|
||||
// портал молча принимает (no-op) → донор не пересоздаётся. Фикс: проверять, жив ли
|
||||
// external_id на портале (listProjects), и пересоздавать недостающих in-place
|
||||
// (НЕ удаляя записи — на них могут висеть лиды/списания).
|
||||
DB::table('system_settings')->where('key', 'supplier_export_mode')->update(['value' => 'online']);
|
||||
|
||||
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
$project = Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'signal_type' => 'call',
|
||||
'signal_identifier' => '79990001122',
|
||||
'is_active' => true,
|
||||
'daily_limit_target' => 10,
|
||||
'regions' => [],
|
||||
'delivery_days_mask' => 31,
|
||||
]);
|
||||
|
||||
// Pre-seed supplier_projects, чьи external_id указывают на удалённых с портала доноров.
|
||||
foreach (['B1', 'B2', 'B3'] as $platform) {
|
||||
SupplierProject::create([
|
||||
'platform' => $platform,
|
||||
'signal_type' => 'call',
|
||||
'unique_key' => '79990001122',
|
||||
'subject_code' => null,
|
||||
'supplier_external_id' => 'DEAD'.$platform,
|
||||
'current_limit' => 10,
|
||||
'current_workdays' => [1, 2, 3, 4, 5],
|
||||
'current_regions' => [],
|
||||
'sync_status' => 'ok',
|
||||
'last_synced_at' => now()->subDay(),
|
||||
]);
|
||||
}
|
||||
|
||||
$loadCalls = 0;
|
||||
Http::fake([
|
||||
'crm.bp-gr.ru/admin/visit/rt-project-save' => Http::response(['status' => 'OK', 'message' => '', 'id' => '7003'], 200),
|
||||
'crm.bp-gr.ru/admin/visit/rt-projects-load*' => function () use (&$loadCalls) {
|
||||
$loadCalls++;
|
||||
// Первый load = проверка существования → донор удалён (пусто).
|
||||
if ($loadCalls === 1) {
|
||||
return Http::response(['projects' => []], 200);
|
||||
}
|
||||
|
||||
// Последующие load (внутри saveProjectMultiFlag) = свежесозданные доноры.
|
||||
return Http::response(['projects' => [
|
||||
['id' => '7001', 'src' => 'rt', 'name' => '79990001122', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79990001122'],
|
||||
['id' => '7002', 'src' => 'bl', 'name' => '79990001122', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79990001122'],
|
||||
['id' => '7003', 'src' => 'mt', 'name' => '79990001122', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79990001122'],
|
||||
]], 200);
|
||||
},
|
||||
]);
|
||||
|
||||
(new SyncSupplierProjectJob($project->id))->handle(app(SupplierProjectChannel::class));
|
||||
|
||||
// external_id переписаны на свежесозданных доноров (не DEAD*), записи не удалены.
|
||||
$sps = SupplierProject::where('unique_key', '79990001122')->orderBy('platform')->get();
|
||||
expect($sps)->toHaveCount(3);
|
||||
expect($sps->pluck('supplier_external_id')->all())->toBe(['7001', '7002', '7003']);
|
||||
});
|
||||
|
||||
it('online mode also populates legacy supplier_b{1,2,3}_project_id so UI sync-status is not stuck pending', function (): void {
|
||||
// Regression: online mode writes the link to the pivot, but ProjectResource/aggregateSyncStatus
|
||||
// read the legacy FK columns (supplierB1/B2/B3). They stayed NULL in online → "Sync pending"
|
||||
// forever even though the stack is synced. Online must populate them too.
|
||||
DB::table('system_settings')->where('key', 'supplier_export_mode')->update(['value' => 'online']);
|
||||
|
||||
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
$project = Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'signal_type' => 'site',
|
||||
'signal_identifier' => 'uisync.example.com',
|
||||
'is_active' => true,
|
||||
'daily_limit_target' => 5,
|
||||
'regions' => [],
|
||||
'delivery_days_mask' => 127,
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'crm.bp-gr.ru/admin/visit/rt-project-save' => Http::response(['status' => 'OK', 'message' => '', 'id' => '9003'], 200),
|
||||
'crm.bp-gr.ru/admin/visit/rt-projects-load*' => Http::response(['projects' => [
|
||||
['id' => '9001', 'src' => 'rt', 'name' => 'uisync.example.com', 'tag' => 'РФ', 'type' => 'hosts', 'content' => 'uisync.example.com'],
|
||||
['id' => '9002', 'src' => 'bl', 'name' => 'uisync.example.com', 'tag' => 'РФ', 'type' => 'hosts', 'content' => 'uisync.example.com'],
|
||||
['id' => '9003', 'src' => 'mt', 'name' => 'uisync.example.com', 'tag' => 'РФ', 'type' => 'hosts', 'content' => 'uisync.example.com'],
|
||||
]], 200),
|
||||
]);
|
||||
|
||||
(new SyncSupplierProjectJob($project->id))->handle(app(SupplierProjectChannel::class));
|
||||
|
||||
$project->refresh();
|
||||
expect($project->supplier_b1_project_id)->not->toBeNull();
|
||||
expect($project->supplier_b2_project_id)->not->toBeNull();
|
||||
expect($project->supplier_b3_project_id)->not->toBeNull();
|
||||
expect($project->aggregateSyncStatus())->toBe('ok');
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Batch mode: keeps каркас (limit 0, no per-subject save, no pivot)
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -155,3 +409,53 @@ it('batch mode keeps каркас (limit=0, sets supplier_b{1,2,3}_project_id, n
|
||||
// Batch: no pivot rows (nightly job fills them)
|
||||
expect(DB::table('project_supplier_links')->where('project_id', $project->id)->count())->toBe(0);
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Connection: must use pgsql_supplier (BYPASSRLS) — queue worker has no tenant GUC
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
it('runs every projects query on the pgsql_supplier (BYPASSRLS) connection', function (): void {
|
||||
// Regression: job ran on the default RLS-enforced connection. On a real queue worker
|
||||
// (role crm_app_user, no SetTenantContext middleware → no app.current_tenant_id GUC)
|
||||
// the very first Project::find() dies with SQLSTATE 42704 before any supplier contact,
|
||||
// so the supplier project is never created and the UI sticks on "Sync pending".
|
||||
// Every sibling supplier job (SyncSupplierProjectsJob/DeleteSupplierProjectJob/…) uses
|
||||
// pgsql_supplier; this one must too. On dev (postgres superuser) RLS is bypassed, so we
|
||||
// assert the *connection* the queries run on rather than RLS enforcement.
|
||||
DB::table('system_settings')->where('key', 'supplier_export_mode')->update(['value' => 'online']);
|
||||
|
||||
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
|
||||
$project = Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'signal_type' => 'site',
|
||||
'signal_identifier' => 'conn-test.example.com',
|
||||
'is_active' => true,
|
||||
'daily_limit_target' => 10,
|
||||
'regions' => [],
|
||||
'delivery_days_mask' => 127,
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'crm.bp-gr.ru/admin/visit/rt-project-save' => Http::response(['status' => 'OK', 'message' => '', 'id' => '8003'], 200),
|
||||
'crm.bp-gr.ru/admin/visit/rt-projects-load*' => Http::response(['projects' => [
|
||||
['id' => '8001', 'src' => 'rt', 'name' => 'conn-test.example.com', 'tag' => 'РФ', 'type' => 'hosts', 'content' => 'conn-test.example.com'],
|
||||
['id' => '8002', 'src' => 'bl', 'name' => 'conn-test.example.com', 'tag' => 'РФ', 'type' => 'hosts', 'content' => 'conn-test.example.com'],
|
||||
['id' => '8003', 'src' => 'mt', 'name' => 'conn-test.example.com', 'tag' => 'РФ', 'type' => 'hosts', 'content' => 'conn-test.example.com'],
|
||||
]], 200),
|
||||
]);
|
||||
|
||||
// Listen only during the job run (factory queries above are already done).
|
||||
$projectConnections = [];
|
||||
DB::listen(function ($query) use (&$projectConnections): void {
|
||||
// '"projects"' (quoted table) does NOT match '"supplier_projects"' or
|
||||
// '"project_supplier_links"', so this captures only the projects table.
|
||||
if (str_contains($query->sql, '"projects"')) {
|
||||
$projectConnections[] = $query->connectionName;
|
||||
}
|
||||
});
|
||||
|
||||
(new SyncSupplierProjectJob($project->id))->handle(app(SupplierProjectChannel::class));
|
||||
|
||||
expect($projectConnections)->not->toBeEmpty();
|
||||
expect(array_values(array_unique($projectConnections)))->toBe(['pgsql_supplier']);
|
||||
});
|
||||
|
||||
@@ -42,79 +42,66 @@ afterEach(function (): void {
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Per-subject grouping
|
||||
// Multi-region grouping (merged into single group)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Project regions=[82,83] site → 2 groups (Москва, СПб) →
|
||||
* 2 multi-flag saves → 6 supplier_projects (2 subjects × 3 platforms B1/B2/B3)
|
||||
* with correct subject_code/tag; pivot — 6 links for the project.
|
||||
* Project regions=[82,83] site → 1 group (merged regions) → tag='РФ' →
|
||||
* 1 multi-flag save → 3 supplier_projects (platforms B1/B2/B3)
|
||||
* subject_code=null, current_regions=[82,83]; pivot — 3 links for the project.
|
||||
*/
|
||||
test('per-subject: regions=[82,83] site → 6 supplier_projects + 6 pivot links', function (): void {
|
||||
test('single-group: regions=[82,83] site → merged regions tag=РФ → 3 supplier_projects + 3 pivot links', function (): void {
|
||||
$tenant = Tenant::factory()->create();
|
||||
|
||||
/** @var Project $project */
|
||||
$project = Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'is_active' => true,
|
||||
'archived_at' => null,
|
||||
'signal_type' => 'site',
|
||||
'signal_identifier' => 'persubject.example.com',
|
||||
'daily_limit_target' => 9,
|
||||
'delivery_days_mask' => 127, // all days
|
||||
'delivery_days_mask' => 127,
|
||||
'regions' => [82, 83],
|
||||
]);
|
||||
|
||||
// saveProjectMultiFlag calls rt-project-save once per subject, then listProjects to get ids
|
||||
// One save (merged regions=[82,83] → tag='РФ') + one listProjects
|
||||
Http::fake([
|
||||
// first save (subject 82 = Москва)
|
||||
'crm.bp-gr.ru/admin/visit/rt-project-save' => Http::sequence()
|
||||
->push(['status' => 'OK', 'message' => '', 'result' => null, 'id' => '1001'], 200)
|
||||
// second save (subject 83 = Санкт-Петербург)
|
||||
->push(['status' => 'OK', 'message' => '', 'result' => null, 'id' => '2001'], 200),
|
||||
// listProjects called after each save — return 3 rows per group
|
||||
'crm.bp-gr.ru/admin/visit/rt-projects-load*' => Http::sequence()
|
||||
// After first save (Москва tag)
|
||||
->push(['projects' => [
|
||||
['id' => '1001', 'src' => 'rt', 'name' => 'persubject.example.com', 'tag' => 'Москва', 'type' => 'hosts', 'content' => 'persubject.example.com'],
|
||||
['id' => '1002', 'src' => 'bl', 'name' => 'persubject.example.com', 'tag' => 'Москва', 'type' => 'hosts', 'content' => 'persubject.example.com'],
|
||||
['id' => '1003', 'src' => 'mt', 'name' => 'persubject.example.com', 'tag' => 'Москва', 'type' => 'hosts', 'content' => 'persubject.example.com'],
|
||||
]], 200)
|
||||
// After second save (СПб tag)
|
||||
->push(['projects' => [
|
||||
['id' => '1001', 'src' => 'rt', 'name' => 'persubject.example.com', 'tag' => 'Москва', 'type' => 'hosts', 'content' => 'persubject.example.com'],
|
||||
['id' => '1002', 'src' => 'bl', 'name' => 'persubject.example.com', 'tag' => 'Москва', 'type' => 'hosts', 'content' => 'persubject.example.com'],
|
||||
['id' => '1003', 'src' => 'mt', 'name' => 'persubject.example.com', 'tag' => 'Москва', 'type' => 'hosts', 'content' => 'persubject.example.com'],
|
||||
['id' => '2001', 'src' => 'rt', 'name' => 'persubject.example.com', 'tag' => 'Санкт-Петербург', 'type' => 'hosts', 'content' => 'persubject.example.com'],
|
||||
['id' => '2002', 'src' => 'bl', 'name' => 'persubject.example.com', 'tag' => 'Санкт-Петербург', 'type' => 'hosts', 'content' => 'persubject.example.com'],
|
||||
['id' => '2003', 'src' => 'mt', 'name' => 'persubject.example.com', 'tag' => 'Санкт-Петербург', 'type' => 'hosts', 'content' => 'persubject.example.com'],
|
||||
]], 200),
|
||||
'crm.bp-gr.ru/admin/visit/rt-project-save' => Http::response(
|
||||
['status' => 'OK', 'message' => '', 'result' => null, 'id' => '1001'],
|
||||
200,
|
||||
),
|
||||
'crm.bp-gr.ru/admin/visit/rt-projects-load*' => Http::response(
|
||||
['projects' => [
|
||||
['id' => '1001', 'src' => 'rt', 'name' => 'persubject.example.com', 'tag' => 'РФ', 'type' => 'hosts', 'content' => 'persubject.example.com'],
|
||||
['id' => '1002', 'src' => 'bl', 'name' => 'persubject.example.com', 'tag' => 'РФ', 'type' => 'hosts', 'content' => 'persubject.example.com'],
|
||||
['id' => '1003', 'src' => 'mt', 'name' => 'persubject.example.com', 'tag' => 'РФ', 'type' => 'hosts', 'content' => 'persubject.example.com'],
|
||||
]],
|
||||
200,
|
||||
),
|
||||
]);
|
||||
|
||||
(new SyncSupplierProjectsJob)->handle(app(AjaxProjectChannel::class));
|
||||
|
||||
// 6 supplier_projects created: 2 subjects × 3 platforms
|
||||
// 3 supplier_projects (not 6): all regions merged into one group
|
||||
$sps = SupplierProject::on('pgsql_supplier')
|
||||
->where('unique_key', 'persubject.example.com')
|
||||
->where('signal_type', 'site')
|
||||
->get();
|
||||
|
||||
expect($sps)->toHaveCount(6);
|
||||
expect($sps)->toHaveCount(3);
|
||||
expect($sps->pluck('platform')->sort()->values()->all())->toBe(['B1', 'B2', 'B3']);
|
||||
|
||||
// subject_code 82 → 3 rows (B1/B2/B3)
|
||||
$m = $sps->where('subject_code', 82);
|
||||
expect($m)->toHaveCount(3);
|
||||
expect($m->pluck('platform')->sort()->values()->all())->toBe(['B1', 'B2', 'B3']);
|
||||
// subject_code=null (no per-subject split)
|
||||
expect($sps->pluck('subject_code')->unique()->all())->toContain(null);
|
||||
|
||||
// subject_code 83 → 3 rows
|
||||
$spb = $sps->where('subject_code', 83);
|
||||
expect($spb)->toHaveCount(3);
|
||||
// regions merged: [82, 83] — sorted ascending, stored on each SP
|
||||
expect($sps->firstWhere('platform', 'B1')->current_regions)->toBe([82, 83]);
|
||||
|
||||
// pivot: 6 links for this project
|
||||
// pivot: 3 links (not 6)
|
||||
$pivotCount = DB::table('project_supplier_links')
|
||||
->where('project_id', $project->id)
|
||||
->count();
|
||||
expect($pivotCount)->toBe(6);
|
||||
expect($pivotCount)->toBe(3);
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -128,7 +115,6 @@ test('all-RF pool: regions=[] → 1 group subject_code=null tag=РФ → 3 suppl
|
||||
$project = Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'is_active' => true,
|
||||
'archived_at' => null,
|
||||
'signal_type' => 'site',
|
||||
'signal_identifier' => 'rf-pool.example.com',
|
||||
'daily_limit_target' => 6,
|
||||
@@ -173,13 +159,12 @@ test('all-RF pool: regions=[] → 1 group subject_code=null tag=РФ → 3 suppl
|
||||
// Order: 2 projects on one (source × subject) → computeOrder
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
test('order: 2 projects same source×subject → computeOrder(limits=[10,20]) → limit=20', function (): void {
|
||||
test('order: 2 projects same source×subject → computeOrder([10,20])=20 split across B1/B2/B3 = 7/7/6', function (): void {
|
||||
$tenant = Tenant::factory()->create();
|
||||
|
||||
Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'is_active' => true,
|
||||
'archived_at' => null,
|
||||
'signal_type' => 'site',
|
||||
'signal_identifier' => 'order-test.example.com',
|
||||
'daily_limit_target' => 10,
|
||||
@@ -190,7 +175,6 @@ test('order: 2 projects same source×subject → computeOrder(limits=[10,20])
|
||||
Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'is_active' => true,
|
||||
'archived_at' => null,
|
||||
'signal_type' => 'site',
|
||||
'signal_identifier' => 'order-test.example.com',
|
||||
'daily_limit_target' => 20,
|
||||
@@ -216,17 +200,49 @@ test('order: 2 projects same source×subject → computeOrder(limits=[10,20])
|
||||
|
||||
(new SyncSupplierProjectsJob)->handle(app(AjaxProjectChannel::class));
|
||||
|
||||
// computeOrder([10, 20]) = max(20, ceil(30/3)=10) = 20
|
||||
$sp = SupplierProject::on('pgsql_supplier')
|
||||
// computeOrder([10, 20]) = max(20, ceil(30/3)=10) = 20 (the GROUP order), then split
|
||||
// across B1/B2/B3 = 7/7/6 (Σ == 20 — NOT 20 on each = 60, which would be the ×3 overspend).
|
||||
$sps = SupplierProject::on('pgsql_supplier')
|
||||
->where('unique_key', 'order-test.example.com')
|
||||
->where('platform', 'B1')
|
||||
->first();
|
||||
->get();
|
||||
|
||||
expect($sp)->not->toBeNull();
|
||||
expect($sp->current_limit)->toBe(20);
|
||||
// Single group → exactly 3 supplier_projects (not 6 as would happen if grouped separately)
|
||||
expect($sps)->toHaveCount(3);
|
||||
expect($sps->sum('current_limit'))->toBe(20);
|
||||
expect($sps->firstWhere('platform', 'B1')->current_limit)->toBe(7);
|
||||
});
|
||||
|
||||
// Only one save call (single group) — not 2
|
||||
Http::assertSentCount(2); // 1 save + 1 listProjects
|
||||
test('limit is DIVIDED across B1/B2/B3 so supplier total == project limit (owner-reported ×3 bug)', function (): void {
|
||||
// The owner reported (and we verified live 2026-05-21): call limit 18 → 18/18/18 on the
|
||||
// portal = supplier could deliver up to 54. The portal does NOT divide. Fix splits 18 → 6/6/6.
|
||||
$tenant = Tenant::factory()->create();
|
||||
Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'is_active' => true,
|
||||
'signal_type' => 'call',
|
||||
'signal_identifier' => '79135161263',
|
||||
'daily_limit_target' => 18,
|
||||
'delivery_days_mask' => 127,
|
||||
'regions' => [],
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'crm.bp-gr.ru/admin/visit/rt-project-save' => Http::response(['status' => 'OK', 'message' => '', 'id' => '4000'], 200),
|
||||
'crm.bp-gr.ru/admin/visit/rt-projects-load*' => Http::response(['projects' => [
|
||||
['id' => '4001', 'src' => 'rt', 'name' => '79135161263', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79135161263'],
|
||||
['id' => '4002', 'src' => 'bl', 'name' => '79135161263', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79135161263'],
|
||||
['id' => '4003', 'src' => 'mt', 'name' => '79135161263', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79135161263'],
|
||||
]], 200),
|
||||
]);
|
||||
|
||||
(new SyncSupplierProjectsJob)->handle(app(AjaxProjectChannel::class));
|
||||
|
||||
// Assert only THIS group's rows (the nightly job syncs every active project in the DB).
|
||||
$sps = SupplierProject::on('pgsql_supplier')->where('unique_key', '79135161263')->get();
|
||||
expect($sps)->toHaveCount(3);
|
||||
expect($sps->sum('current_limit'))->toBe(18); // Σ == project limit (not 54)
|
||||
expect($sps->sortBy('platform')->pluck('current_limit', 'platform')->all())
|
||||
->toBe(['B1' => 6, 'B2' => 6, 'B3' => 6]); // 18 / 3
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -239,7 +255,6 @@ test('sms+keyword → platforms B2+B3 (2 supplier_projects per subject)', functi
|
||||
Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'is_active' => true,
|
||||
'archived_at' => null,
|
||||
'signal_type' => 'sms',
|
||||
'signal_identifier' => null,
|
||||
'sms_senders' => ['79001234567'],
|
||||
@@ -281,7 +296,6 @@ test('sms without keyword → platform B3 only (1 supplier_project)', function (
|
||||
Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'is_active' => true,
|
||||
'archived_at' => null,
|
||||
'signal_type' => 'sms',
|
||||
'signal_identifier' => null,
|
||||
'sms_senders' => ['79009876543'],
|
||||
@@ -324,7 +338,6 @@ test('idempotent: repeat run with no changes → updateProject not duplicate', f
|
||||
Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'is_active' => true,
|
||||
'archived_at' => null,
|
||||
'signal_type' => 'site',
|
||||
'signal_identifier' => 'idempotent.example.com',
|
||||
'daily_limit_target' => 9,
|
||||
@@ -385,7 +398,6 @@ test('respects time budget by stopping at 20:55 МСК', function (): void {
|
||||
Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'is_active' => true,
|
||||
'archived_at' => null,
|
||||
'signal_type' => 'site',
|
||||
'signal_identifier' => 'time-budget.example.com',
|
||||
'daily_limit_target' => 9,
|
||||
@@ -407,7 +419,6 @@ test('sticky auth error throws and sends critical alert email', function (): voi
|
||||
Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'is_active' => true,
|
||||
'archived_at' => null,
|
||||
'signal_type' => 'site',
|
||||
'signal_identifier' => 'auth-fail.example.com',
|
||||
'daily_limit_target' => 9,
|
||||
@@ -435,7 +446,6 @@ test('aborts after 50 consecutive transient failures and sends alert', function
|
||||
Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'is_active' => true,
|
||||
'archived_at' => null,
|
||||
'signal_type' => 'site',
|
||||
'signal_identifier' => "host{$i}.abort.com",
|
||||
'daily_limit_target' => 9,
|
||||
@@ -459,7 +469,6 @@ test('writes supplier_sync_log row for each successful action', function (): voi
|
||||
Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'is_active' => true,
|
||||
'archived_at' => null,
|
||||
'signal_type' => 'site',
|
||||
'signal_identifier' => 'audit-log.example.com',
|
||||
'daily_limit_target' => 9,
|
||||
@@ -501,3 +510,57 @@ test('writes supplier_sync_log row for each successful action', function (): voi
|
||||
->and($log->http_status)->toBe(200)
|
||||
->and($log->error_message)->toBeNull();
|
||||
});
|
||||
|
||||
test('nightly: re-creates donor on portal when its external_id no longer exists there', function (): void {
|
||||
// Regression mirror of SyncSupplierProjectJobTest: donor deleted on portal → stale
|
||||
// external_id in our DB → updateProject is a silent no-op → donor never re-created.
|
||||
// Nightly reconciler must detect missing donors (listProjects) and re-create in-place.
|
||||
$tenant = Tenant::factory()->create();
|
||||
Project::factory()->create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'is_active' => true,
|
||||
'signal_type' => 'call',
|
||||
'signal_identifier' => '79993334455',
|
||||
'daily_limit_target' => 10,
|
||||
'delivery_days_mask' => 127,
|
||||
'regions' => [],
|
||||
]);
|
||||
|
||||
foreach (['B1', 'B2', 'B3'] as $platform) {
|
||||
SupplierProject::on('pgsql_supplier')->forceCreate([
|
||||
'platform' => $platform,
|
||||
'signal_type' => 'call',
|
||||
'unique_key' => '79993334455',
|
||||
'subject_code' => null,
|
||||
'supplier_external_id' => 'GONE'.$platform,
|
||||
'current_limit' => 10,
|
||||
'current_workdays' => [1, 2, 3, 4, 5, 6, 7],
|
||||
'current_regions' => [],
|
||||
'sync_status' => 'ok',
|
||||
'last_synced_at' => now()->subDay(),
|
||||
]);
|
||||
}
|
||||
|
||||
$loadCalls = 0;
|
||||
Http::fake([
|
||||
'crm.bp-gr.ru/admin/visit/rt-project-save' => Http::response(['status' => 'OK', 'message' => '', 'id' => '8003'], 200),
|
||||
'crm.bp-gr.ru/admin/visit/rt-projects-load*' => function () use (&$loadCalls) {
|
||||
$loadCalls++;
|
||||
if ($loadCalls === 1) {
|
||||
return Http::response(['projects' => []], 200);
|
||||
}
|
||||
|
||||
return Http::response(['projects' => [
|
||||
['id' => '8001', 'src' => 'rt', 'name' => '79993334455', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79993334455'],
|
||||
['id' => '8002', 'src' => 'bl', 'name' => '79993334455', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79993334455'],
|
||||
['id' => '8003', 'src' => 'mt', 'name' => '79993334455', 'tag' => 'РФ', 'type' => 'calls', 'content' => '79993334455'],
|
||||
]], 200);
|
||||
},
|
||||
]);
|
||||
|
||||
(new SyncSupplierProjectsJob)->handle(app(AjaxProjectChannel::class));
|
||||
|
||||
$sps = SupplierProject::on('pgsql_supplier')->where('unique_key', '79993334455')->orderBy('platform')->get();
|
||||
expect($sps)->toHaveCount(3);
|
||||
expect($sps->pluck('supplier_external_id')->all())->toBe(['8001', '8002', '8003']);
|
||||
});
|
||||
|
||||
@@ -27,13 +27,13 @@ test('GET webhook-settings возвращает подписку тенанта'
|
||||
OutboundWebhookSubscription::factory()->create([
|
||||
'tenant_id' => $this->tenant->id,
|
||||
'user_id' => $this->user->id,
|
||||
'target_url' => 'https://crm.example.ru/hook',
|
||||
'target_url' => 'https://93.184.216.34/hook',
|
||||
]);
|
||||
|
||||
$response = $this->getJson('/api/tenants/me/webhook-settings');
|
||||
|
||||
$response->assertOk();
|
||||
expect($response->json('data.target_url'))->toBe('https://crm.example.ru/hook');
|
||||
expect($response->json('data.target_url'))->toBe('https://93.184.216.34/hook');
|
||||
expect($response->json('data'))->toHaveKeys(['target_url', 'secret_prefix', 'events', 'is_active']);
|
||||
expect($response->json('data'))->not->toHaveKey('secret_hash');
|
||||
});
|
||||
@@ -55,11 +55,11 @@ test('GET webhook-settings изолирован по тенанту', function (
|
||||
|
||||
test('PUT webhook-settings создаёт подписку и возвращает secret один раз', function () {
|
||||
$response = $this->putJson('/api/tenants/me/webhook-settings', [
|
||||
'target_url' => 'https://crm.example.ru/hook',
|
||||
'target_url' => 'https://93.184.216.34/hook',
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
expect($response->json('data.target_url'))->toBe('https://crm.example.ru/hook');
|
||||
expect($response->json('data.target_url'))->toBe('https://93.184.216.34/hook');
|
||||
expect($response->json('data.secret'))->toStartWith('whsec_');
|
||||
expect($response->json('data.events'))->toBeArray()->not->toBeEmpty();
|
||||
|
||||
@@ -72,15 +72,15 @@ test('PUT webhook-settings обновляет URL существующей по
|
||||
OutboundWebhookSubscription::factory()->create([
|
||||
'tenant_id' => $this->tenant->id,
|
||||
'user_id' => $this->user->id,
|
||||
'target_url' => 'https://old.example.ru/hook',
|
||||
'target_url' => 'https://8.8.8.8/hook',
|
||||
]);
|
||||
|
||||
$response = $this->putJson('/api/tenants/me/webhook-settings', [
|
||||
'target_url' => 'https://new.example.ru/hook',
|
||||
'target_url' => 'https://1.1.1.1/hook',
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
expect($response->json('data.target_url'))->toBe('https://new.example.ru/hook');
|
||||
expect($response->json('data.target_url'))->toBe('https://1.1.1.1/hook');
|
||||
expect($response->json('data'))->not->toHaveKey('secret');
|
||||
expect(OutboundWebhookSubscription::query()->where('tenant_id', $this->tenant->id)->count())->toBe(1);
|
||||
});
|
||||
@@ -91,12 +91,20 @@ test('PUT webhook-settings: 422 при не-https URL', function () {
|
||||
])->assertStatus(422)->assertJsonValidationErrorFor('target_url');
|
||||
});
|
||||
|
||||
test('PUT webhook-settings: 422 для приватного/служебного IP в target_url (SSRF), не сохраняет', function () {
|
||||
$this->putJson('/api/tenants/me/webhook-settings', [
|
||||
'target_url' => 'https://169.254.169.254/hook',
|
||||
])->assertStatus(422)->assertJsonValidationErrorFor('target_url');
|
||||
|
||||
expect(OutboundWebhookSubscription::query()->where('tenant_id', $this->tenant->id)->count())->toBe(0);
|
||||
});
|
||||
|
||||
test('POST webhooks/test отправляет запрос и возвращает результат', function () {
|
||||
Http::fake(['*' => Http::response(['ok' => true], 200)]);
|
||||
OutboundWebhookSubscription::factory()->create([
|
||||
'tenant_id' => $this->tenant->id,
|
||||
'user_id' => $this->user->id,
|
||||
'target_url' => 'https://crm.example.ru/hook',
|
||||
'target_url' => 'https://93.184.216.34/hook',
|
||||
]);
|
||||
|
||||
$response = $this->postJson('/api/webhooks/test');
|
||||
@@ -104,7 +112,7 @@ test('POST webhooks/test отправляет запрос и возвращае
|
||||
$response->assertOk();
|
||||
expect($response->json('ok'))->toBeTrue();
|
||||
expect($response->json('status'))->toBe(200);
|
||||
Http::assertSent(fn ($req) => $req->url() === 'https://crm.example.ru/hook');
|
||||
Http::assertSent(fn ($req) => $req->url() === 'https://93.184.216.34/hook');
|
||||
});
|
||||
|
||||
test('POST webhooks/test возвращает ok=false при ошибке endpoint', function () {
|
||||
@@ -112,7 +120,7 @@ test('POST webhooks/test возвращает ok=false при ошибке endpo
|
||||
OutboundWebhookSubscription::factory()->create([
|
||||
'tenant_id' => $this->tenant->id,
|
||||
'user_id' => $this->user->id,
|
||||
'target_url' => 'https://crm.example.ru/hook',
|
||||
'target_url' => 'https://93.184.216.34/hook',
|
||||
]);
|
||||
|
||||
$response = $this->postJson('/api/webhooks/test');
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\OutboundWebhookSubscription;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use App\Support\WebhookUrlGuard;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
uses(DatabaseTransactions::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$this->tenant = Tenant::factory()->create();
|
||||
$this->user = User::factory()->for($this->tenant)->create();
|
||||
$this->actingAs($this->user);
|
||||
});
|
||||
|
||||
// --- unit: WebhookUrlGuard (IP-литералы, без DNS) ---
|
||||
|
||||
test('WebhookUrlGuard блокирует приватные/зарезервированные/loopback IP', function (string $url) {
|
||||
expect(WebhookUrlGuard::blockReason($url))->not->toBeNull();
|
||||
})->with([
|
||||
'https://127.0.0.1/hook', // loopback
|
||||
'https://10.0.0.1/hook', // private A
|
||||
'https://172.16.0.1/hook', // private B
|
||||
'https://192.168.1.1/hook', // private C
|
||||
'https://169.254.169.254/hook', // link-local / cloud metadata
|
||||
'https://[::1]/hook', // IPv6 loopback
|
||||
]);
|
||||
|
||||
test('WebhookUrlGuard пропускает публичный IP', function () {
|
||||
expect(WebhookUrlGuard::blockReason('https://93.184.216.34/hook'))->toBeNull();
|
||||
});
|
||||
|
||||
test('WebhookUrlGuard отклоняет битый URL', function () {
|
||||
expect(WebhookUrlGuard::blockReason('not-a-url'))->not->toBeNull();
|
||||
});
|
||||
|
||||
// --- endpoint: webhooks/test не должен бить во внутреннюю сеть ---
|
||||
|
||||
test('POST webhooks/test блокирует приватный IP target_url (SSRF) и не шлёт запрос', function () {
|
||||
Http::fake();
|
||||
OutboundWebhookSubscription::factory()->create([
|
||||
'tenant_id' => $this->tenant->id,
|
||||
'user_id' => $this->user->id,
|
||||
'target_url' => 'https://169.254.169.254/hook',
|
||||
]);
|
||||
|
||||
$this->postJson('/api/webhooks/test')->assertStatus(422);
|
||||
Http::assertNothingSent();
|
||||
});
|
||||
|
||||
test('POST webhooks/test пропускает публичный target_url', function () {
|
||||
Http::fake(['*' => Http::response(['ok' => true], 200)]);
|
||||
OutboundWebhookSubscription::factory()->create([
|
||||
'tenant_id' => $this->tenant->id,
|
||||
'user_id' => $this->user->id,
|
||||
'target_url' => 'https://93.184.216.34/hook',
|
||||
]);
|
||||
|
||||
$this->postJson('/api/webhooks/test')
|
||||
->assertOk()
|
||||
->assertJsonPath('ok', true);
|
||||
Http::assertSentCount(1);
|
||||
});
|
||||
@@ -9,9 +9,10 @@ import { useAuthStore } from '../../resources/js/stores/auth';
|
||||
import type { AuthUser } from '../../resources/js/api/auth';
|
||||
|
||||
// AdminLayout содержит:
|
||||
// - sidebar #012019 с brand-block «Лидерра.» + ADMIN метка + 7 nav-items
|
||||
// (Тенанты 142 / Биллинг / Тарифная сетка / Цены поставщиков / Инциденты 3 /
|
||||
// Impersonation / Система);
|
||||
// - sidebar #012019 с brand-block «Лидерра.» + ADMIN метка + 9 nav-items
|
||||
// (Тенанты / Биллинг / Тарифная сетка / Цены поставщиков / Инциденты /
|
||||
// Impersonation / Система / Интеграция с поставщиком / Проекты у поставщика),
|
||||
// без mock count-badge;
|
||||
// - topbar с breadcrumb («Админка › <currentPageTitle>») + user-menu;
|
||||
// - <v-main> RouterView; DevIndexBadge.
|
||||
|
||||
@@ -84,12 +85,12 @@ describe('AdminLayout.vue', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('показывает count-badge для Тенантов (142) и Инцидентов (3) и не для остальных', async () => {
|
||||
it('не рендерит захардкоженные mock count-badge (live-счётчики — отдельная фича)', async () => {
|
||||
// Ранее в nav были mock-счётчики Тенанты=142 / Инциденты=3, расходящиеся с реальными
|
||||
// данными (5 тенантов / 0 открытых инцидентов). Удалены — неверный бейдж хуже отсутствия.
|
||||
const { wrapper } = await mountAdminLayout();
|
||||
const counts = wrapper.findAll('.nav-count').map((n) => n.text());
|
||||
expect(counts).toContain('142');
|
||||
expect(counts).toContain('3');
|
||||
expect(counts).toHaveLength(2);
|
||||
const counts = wrapper.findAll('.nav-count');
|
||||
expect(counts).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('breadcrumb на /admin/tenants показывает «Тенанты»', async () => {
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { mount } from '@vue/test-utils';
|
||||
import { createVuetify } from 'vuetify';
|
||||
import * as components from 'vuetify/components';
|
||||
import * as directives from 'vuetify/directives';
|
||||
import axios from 'axios';
|
||||
import AdminSupplierIntegrationView from '../../resources/js/views/admin/AdminSupplierIntegrationView.vue';
|
||||
|
||||
vi.mock('axios');
|
||||
|
||||
const vuetify = createVuetify({ components, directives });
|
||||
|
||||
describe('AdminSupplierIntegrationView — export-mode toggle (Plan 4 Task 1)', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
(axios.get as ReturnType<typeof vi.fn>).mockImplementation((url: string) => {
|
||||
if (url.endsWith('/export-mode')) {
|
||||
return Promise.resolve({ data: { mode: 'batch' } });
|
||||
}
|
||||
if (url.endsWith('/manual-queue')) {
|
||||
return Promise.resolve({ data: { queue: [] } });
|
||||
}
|
||||
return Promise.resolve({ data: { health: null, history: [] } });
|
||||
});
|
||||
(axios.post as ReturnType<typeof vi.fn>).mockResolvedValue({ data: { mode: 'online' } });
|
||||
});
|
||||
|
||||
it('GETs current mode on mount and renders the toggle with current label', async () => {
|
||||
const wrapper = mount(AdminSupplierIntegrationView, { global: { plugins: [vuetify] } });
|
||||
await new Promise((r) => setTimeout(r, 50));
|
||||
|
||||
expect(axios.get).toHaveBeenCalledWith('/api/admin/supplier-integration/export-mode');
|
||||
const toggle = wrapper.find('[data-testid="export-mode-toggle"]');
|
||||
expect(toggle.exists()).toBe(true);
|
||||
expect(wrapper.text()).toContain('Режим экспорта проектов');
|
||||
expect(wrapper.text()).toContain('Пакетный');
|
||||
});
|
||||
|
||||
it('switching to online POSTs the new value', async () => {
|
||||
const wrapper = mount(AdminSupplierIntegrationView, { global: { plugins: [vuetify] } });
|
||||
await new Promise((r) => setTimeout(r, 50));
|
||||
|
||||
const onlineBtn = wrapper.find('[data-testid="export-mode-online"]');
|
||||
expect(onlineBtn.exists()).toBe(true);
|
||||
await onlineBtn.trigger('click');
|
||||
await new Promise((r) => setTimeout(r, 20));
|
||||
|
||||
expect(axios.post).toHaveBeenCalledWith(
|
||||
'/api/admin/supplier-integration/export-mode',
|
||||
{ mode: 'online' },
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,83 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { mount, flushPromises } from '@vue/test-utils';
|
||||
import { createVuetify } from 'vuetify';
|
||||
import * as components from 'vuetify/components';
|
||||
import * as directives from 'vuetify/directives';
|
||||
import axios from 'axios';
|
||||
import AdminSupplierProjectsView from '../../resources/js/views/admin/AdminSupplierProjectsView.vue';
|
||||
|
||||
vi.mock('axios');
|
||||
|
||||
const vuetify = createVuetify({ components, directives });
|
||||
|
||||
// VDialog телепортит контент в body → стаб рендерит слот инлайн (квирк: VDialog
|
||||
// teleport стаб для поиска confirm-кнопки внутри диалога).
|
||||
const mountView = () =>
|
||||
mount(AdminSupplierProjectsView, {
|
||||
global: {
|
||||
plugins: [vuetify],
|
||||
stubs: { VDialog: { template: '<div><slot /></div>' } },
|
||||
},
|
||||
});
|
||||
|
||||
describe('AdminSupplierProjectsView (Plan 4 Task 3)', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
(axios.get as ReturnType<typeof vi.fn>).mockResolvedValue({
|
||||
data: {
|
||||
projects: [
|
||||
{
|
||||
id: 1,
|
||||
platform: 'B1',
|
||||
signal_type: 'site',
|
||||
unique_key: 'okna.ru',
|
||||
subject_code: 82,
|
||||
subject_name: 'Москва',
|
||||
current_limit: 5,
|
||||
supplier_external_id: '777',
|
||||
orderers: ['ООО Ромашка'],
|
||||
last_delivery_at: '2026-05-19T10:00:00Z',
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
(axios.post as ReturnType<typeof vi.fn>).mockResolvedValue({
|
||||
data: { deleted: 1, failures: [] },
|
||||
});
|
||||
});
|
||||
|
||||
it('GETs list on mount and renders rows (source, region, orderers)', async () => {
|
||||
const wrapper = mountView();
|
||||
await flushPromises();
|
||||
|
||||
expect(axios.get).toHaveBeenCalledWith('/api/admin/supplier-integration/projects');
|
||||
const text = wrapper.text();
|
||||
expect(text).toContain('okna.ru');
|
||||
expect(text).toContain('Москва');
|
||||
expect(text).toContain('ООО Ромашка');
|
||||
});
|
||||
|
||||
it('bulk-deletes selected rows after confirm', async () => {
|
||||
const wrapper = mountView();
|
||||
await flushPromises();
|
||||
|
||||
await wrapper.find('[data-testid="row-checkbox-1"] input').setValue(true);
|
||||
await wrapper.find('[data-testid="bulk-delete-btn"]').trigger('click');
|
||||
await flushPromises();
|
||||
await wrapper.find('[data-testid="confirm-delete-btn"]').trigger('click');
|
||||
await flushPromises();
|
||||
|
||||
expect(axios.post).toHaveBeenCalledWith(
|
||||
'/api/admin/supplier-integration/projects/delete',
|
||||
{ ids: [1] },
|
||||
);
|
||||
});
|
||||
|
||||
it('bulk-delete button is disabled when nothing selected', async () => {
|
||||
const wrapper = mountView();
|
||||
await flushPromises();
|
||||
|
||||
const btn = wrapper.find('[data-testid="bulk-delete-btn"]');
|
||||
expect(btn.attributes('disabled')).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -59,6 +59,13 @@ const mountAppLayout = async (path = '/dashboard', user: AuthUser | null = mockU
|
||||
{ path: '/billing', component: { template: '<div>billing</div>' } },
|
||||
{ path: '/reports', component: { template: '<div>reports</div>' } },
|
||||
{ path: '/settings', component: { template: '<div>settings</div>' } },
|
||||
// Не в sidebar nav, но имеют meta.title — topbar должен брать title оттуда.
|
||||
{
|
||||
path: '/reminders',
|
||||
component: { template: '<div>reminders</div>' },
|
||||
meta: { title: 'Напоминания' },
|
||||
},
|
||||
{ path: '/import', component: { template: '<div>import</div>' }, meta: { title: 'Импорт данных' } },
|
||||
],
|
||||
});
|
||||
await router.push(path);
|
||||
@@ -110,6 +117,18 @@ describe('AppLayout.vue', () => {
|
||||
expect(wrapper.text()).toContain('Дашборд');
|
||||
});
|
||||
|
||||
it('topbar title для страницы вне sidebar nav берётся из route.meta.title (Напоминания)', async () => {
|
||||
const wrapper = await mountAppLayout('/reminders');
|
||||
// Напоминания нет в sidebar nav (см. тест выше) — title должен прийти из meta, не «Страница».
|
||||
expect(wrapper.text()).toContain('Напоминания');
|
||||
expect(wrapper.text()).not.toContain('Страница');
|
||||
});
|
||||
|
||||
it('topbar title для /import берётся из route.meta.title (Импорт данных)', async () => {
|
||||
const wrapper = await mountAppLayout('/import');
|
||||
expect(wrapper.text()).toContain('Импорт данных');
|
||||
});
|
||||
|
||||
it('user-chip показывает initials и shortName из store user', async () => {
|
||||
const wrapper = await mountAppLayout();
|
||||
const text = wrapper.text();
|
||||
|
||||
@@ -169,7 +169,7 @@ describe('BulkActionsBar — extended', () => {
|
||||
expect((wrapper.vm as unknown as { regionsOpen: boolean }).regionsOpen).toBe(true);
|
||||
});
|
||||
|
||||
it('keeps existing pause/resume/archive buttons', async () => {
|
||||
it('keeps existing pause/resume/delete buttons', async () => {
|
||||
setActivePinia(createPinia());
|
||||
vi.mocked(axios.post).mockResolvedValue({ data: { updated: 1, skipped: [], warnings: [] } });
|
||||
vi.mocked(axios.get).mockResolvedValue({ data: { data: [], meta: { total: 0 } } });
|
||||
@@ -184,6 +184,6 @@ describe('BulkActionsBar — extended', () => {
|
||||
});
|
||||
expect(wrapper.find('[data-testid="bulk-pause"]').exists()).toBe(true);
|
||||
expect(wrapper.find('[data-testid="bulk-resume"]').exists()).toBe(true);
|
||||
expect(wrapper.find('[data-testid="bulk-archive"]').exists()).toBe(true);
|
||||
expect(wrapper.find('[data-testid="bulk-delete"]').exists()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { mount } from '@vue/test-utils';
|
||||
import { createPinia, setActivePinia } from 'pinia';
|
||||
import { createVuetify } from 'vuetify';
|
||||
|
||||
import DashboardPageHead from '../../resources/js/components/dashboard/DashboardPageHead.vue';
|
||||
import { useAuthStore } from '../../resources/js/stores/auth';
|
||||
import type { AuthUser } from '../../resources/js/api/auth';
|
||||
|
||||
const mockUser: AuthUser = {
|
||||
id: 1,
|
||||
email: 'petr.sidorov@example.ru',
|
||||
first_name: 'Пётр',
|
||||
last_name: 'Сидоров',
|
||||
tenant_id: 1,
|
||||
totp_enabled: false,
|
||||
last_login_at: null,
|
||||
};
|
||||
|
||||
const mountHead = (user: AuthUser | null = mockUser) => {
|
||||
setActivePinia(createPinia());
|
||||
useAuthStore().user = user;
|
||||
return mount(DashboardPageHead, {
|
||||
props: { modelValue: 'today' },
|
||||
global: { plugins: [createVuetify()] },
|
||||
});
|
||||
};
|
||||
|
||||
describe('DashboardPageHead.vue', () => {
|
||||
it('приветствие использует имя залогиненного пользователя, не захардкоженное «Иван»', () => {
|
||||
const wrapper = mountHead();
|
||||
const greet = wrapper.find('.page-greet').text();
|
||||
expect(greet).toContain('Пётр');
|
||||
expect(greet).not.toContain('Иван');
|
||||
});
|
||||
|
||||
it('при отсутствии user приветствие рендерится без падения', () => {
|
||||
const wrapper = mountHead(null);
|
||||
expect(wrapper.find('.page-greet').exists()).toBe(true);
|
||||
expect(wrapper.find('.page-greet').text().length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
@@ -8,9 +8,9 @@ import type { LeadStatus } from '../../resources/js/composables/leadStatuses';
|
||||
const vuetify = createVuetify();
|
||||
|
||||
const statuses: LeadStatus[] = [
|
||||
{ slug: 'new', nameRu: 'Новая сделка', colorHex: '#5b2db2', order: 1 } as LeadStatus,
|
||||
{ slug: 'viewed', nameRu: 'Просмотрено', colorHex: '#5a2db2', order: 2 } as LeadStatus,
|
||||
{ slug: 'won', nameRu: 'Куплено', colorHex: '#00A36C', order: 3 } as LeadStatus,
|
||||
{ slug: 'new', nameRu: 'Новая сделка', isSystem: true, sortOrder: 1, colorHex: '#5b2db2' },
|
||||
{ slug: 'viewed', nameRu: 'Просмотрено', isSystem: true, sortOrder: 2, colorHex: '#5a2db2' },
|
||||
{ slug: 'won', nameRu: 'Куплено', isSystem: true, sortOrder: 3, colorHex: '#00A36C' },
|
||||
];
|
||||
|
||||
function makeDeal(over: Partial<MockDeal> = {}): MockDeal {
|
||||
|
||||
@@ -23,7 +23,6 @@ const sampleProject = {
|
||||
daily_limit_target: 10,
|
||||
delivered_today: 0,
|
||||
is_active: true,
|
||||
archived_at: null,
|
||||
sync_status: 'ok' as const,
|
||||
region_mask: 0,
|
||||
region_mode: 'include',
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { mount, flushPromises } from '@vue/test-utils';
|
||||
import { createPinia, setActivePinia } from 'pinia';
|
||||
import { createVuetify } from 'vuetify';
|
||||
|
||||
vi.mock('axios');
|
||||
vi.mock('../../resources/js/api/client', () => ({
|
||||
apiClient: {
|
||||
post: vi.fn().mockResolvedValue({ data: {} }),
|
||||
patch: vi.fn().mockResolvedValue({ data: {} }),
|
||||
},
|
||||
ensureCsrfCookie: vi.fn().mockResolvedValue(undefined),
|
||||
extractErrorMessage: vi.fn(() => 'Произошла ошибка.'),
|
||||
}));
|
||||
|
||||
import { apiClient } from '../../resources/js/api/client';
|
||||
import NewProjectDialog from '../../resources/js/views/projects/NewProjectDialog.vue';
|
||||
|
||||
// VDialog teleport-стаб (как в NewProjectDialog.spec.ts): рендерит слот инлайн.
|
||||
const factory = () =>
|
||||
mount(NewProjectDialog, {
|
||||
props: { modelValue: true, mode: 'create' as const },
|
||||
global: {
|
||||
plugins: [createVuetify()],
|
||||
stubs: {
|
||||
VDialog: {
|
||||
template: '<div class="dialog-stub" v-if="modelValue"><slot /></div>',
|
||||
props: ['modelValue'],
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia());
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('NewProjectDialog — required region gate + «Вся РФ» (Plan 4 Task 4)', () => {
|
||||
it('blocks submit when no region chosen and shows error', async () => {
|
||||
const w = factory();
|
||||
await flushPromises();
|
||||
|
||||
await w.find('[data-testid="submit-btn"]').trigger('click');
|
||||
await flushPromises();
|
||||
|
||||
expect(apiClient.post).not.toHaveBeenCalled();
|
||||
expect(w.text()).toContain('Выберите регион');
|
||||
});
|
||||
|
||||
it('«Вся РФ» shows warning, requires confirm, then submits regions=[]', async () => {
|
||||
const w = factory();
|
||||
await flushPromises();
|
||||
|
||||
(w.vm as unknown as { chooseVsyaRf: () => void }).chooseVsyaRf();
|
||||
await w.vm.$nextTick();
|
||||
expect(w.text()).toContain('всю Россию');
|
||||
|
||||
await w.find('[data-testid="confirm-vsya-rf"]').trigger('click');
|
||||
await w.vm.$nextTick();
|
||||
|
||||
await w.find('[data-testid="submit-btn"]').trigger('click');
|
||||
await flushPromises();
|
||||
|
||||
expect(apiClient.post).toHaveBeenCalledTimes(1);
|
||||
const payload = (apiClient.post as unknown as { mock: { calls: unknown[][] } }).mock.calls[0][1] as {
|
||||
regions: number[];
|
||||
};
|
||||
expect(payload.regions).toEqual([]);
|
||||
});
|
||||
|
||||
it('picking subjects after «Вся РФ» clears the confirmation (mutual exclusion)', async () => {
|
||||
const w = factory();
|
||||
await flushPromises();
|
||||
|
||||
const vm = w.vm as unknown as {
|
||||
chooseVsyaRf: () => void;
|
||||
confirmVsyaRf: () => void;
|
||||
onRegionsChange: (codes: number[]) => void;
|
||||
vsyaRfConfirmed: boolean;
|
||||
};
|
||||
vm.chooseVsyaRf();
|
||||
vm.confirmVsyaRf();
|
||||
await w.vm.$nextTick();
|
||||
expect(vm.vsyaRfConfirmed).toBe(true);
|
||||
|
||||
vm.onRegionsChange([77]);
|
||||
await w.vm.$nextTick();
|
||||
expect(vm.vsyaRfConfirmed).toBe(false);
|
||||
|
||||
await w.find('[data-testid="submit-btn"]').trigger('click');
|
||||
await flushPromises();
|
||||
|
||||
const payload = (apiClient.post as unknown as { mock: { calls: unknown[][] } }).mock.calls[0][1] as {
|
||||
regions: number[];
|
||||
};
|
||||
expect(payload.regions).toEqual([77]);
|
||||
});
|
||||
});
|
||||
@@ -12,7 +12,6 @@ const baseProject = {
|
||||
daily_limit_target: 50,
|
||||
delivered_today: 32,
|
||||
is_active: true,
|
||||
archived_at: null,
|
||||
sync_status: 'ok' as const,
|
||||
};
|
||||
|
||||
|
||||
@@ -11,7 +11,6 @@ const sample = {
|
||||
is_active: true,
|
||||
daily_limit_target: 50,
|
||||
delivered_today: 12,
|
||||
archived_at: null,
|
||||
sync_status: 'ok' as const,
|
||||
};
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ const sampleProject: Project = {
|
||||
daily_limit_target: 30,
|
||||
delivered_today: 0,
|
||||
is_active: true,
|
||||
archived_at: null,
|
||||
region_mask: 0,
|
||||
region_mode: 'include',
|
||||
regions: [],
|
||||
@@ -152,10 +151,10 @@ describe('ProjectDetailsDrawer', () => {
|
||||
expect(wrapper.get('[data-testid="pdd-pause"]').text()).toContain('Приостановить');
|
||||
});
|
||||
|
||||
it('Delete: confirm=true → archive + close emit', async () => {
|
||||
it('Delete: confirm=true → del + close emit', async () => {
|
||||
const wrapper = mount(ProjectDetailsDrawer, { props: { project: sampleProject } });
|
||||
const store = useProjectsStore();
|
||||
const spy = vi.spyOn(store, 'archive').mockResolvedValueOnce(undefined);
|
||||
const spy = vi.spyOn(store, 'del').mockResolvedValueOnce(undefined);
|
||||
vi.stubGlobal('confirm', () => true);
|
||||
|
||||
await wrapper.get('[data-testid="pdd-delete"]').trigger('click');
|
||||
@@ -166,10 +165,10 @@ describe('ProjectDetailsDrawer', () => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it('Delete: confirm=false → no archive, no close', async () => {
|
||||
it('Delete: confirm=false → no del, no close', async () => {
|
||||
const wrapper = mount(ProjectDetailsDrawer, { props: { project: sampleProject } });
|
||||
const store = useProjectsStore();
|
||||
const spy = vi.spyOn(store, 'archive').mockResolvedValueOnce(undefined);
|
||||
const spy = vi.spyOn(store, 'del').mockResolvedValueOnce(undefined);
|
||||
vi.stubGlobal('confirm', () => false);
|
||||
|
||||
await wrapper.get('[data-testid="pdd-delete"]').trigger('click');
|
||||
|
||||
@@ -52,7 +52,6 @@ describe('ProjectsView', () => {
|
||||
daily_limit_target: 10,
|
||||
delivered_today: 0,
|
||||
is_active: true,
|
||||
archived_at: null,
|
||||
sync_status: 'ok',
|
||||
},
|
||||
],
|
||||
@@ -82,7 +81,6 @@ describe('ProjectsView', () => {
|
||||
daily_limit_target: 10,
|
||||
delivered_today: 0,
|
||||
is_active: true,
|
||||
archived_at: null,
|
||||
sync_status: 'ok',
|
||||
},
|
||||
{
|
||||
@@ -93,7 +91,6 @@ describe('ProjectsView', () => {
|
||||
daily_limit_target: 10,
|
||||
delivered_today: 0,
|
||||
is_active: true,
|
||||
archived_at: null,
|
||||
sync_status: 'ok',
|
||||
},
|
||||
],
|
||||
@@ -127,7 +124,6 @@ describe('ProjectsView × ProjectDetailsDrawer integration', () => {
|
||||
daily_limit_target: 10,
|
||||
delivered_today: 0,
|
||||
is_active: true,
|
||||
archived_at: null,
|
||||
sync_status: 'ok' as const,
|
||||
};
|
||||
const projectB = {
|
||||
@@ -138,7 +134,6 @@ describe('ProjectsView × ProjectDetailsDrawer integration', () => {
|
||||
daily_limit_target: 10,
|
||||
delivered_today: 0,
|
||||
is_active: true,
|
||||
archived_at: null,
|
||||
sync_status: 'ok' as const,
|
||||
};
|
||||
|
||||
@@ -244,3 +239,36 @@ describe('ProjectsView × ProjectDetailsDrawer integration', () => {
|
||||
expect(wrapper.find('.projects-view').classes()).not.toContain('has-drawer');
|
||||
});
|
||||
});
|
||||
|
||||
describe('ProjectsView 18:00 cutoff banner', () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear();
|
||||
(axios.get as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({
|
||||
data: { data: [], meta: { total: 0, current_page: 1, per_page: 20 } },
|
||||
});
|
||||
});
|
||||
|
||||
it('shows the cutoff banner with the 18:00 deadline by default', async () => {
|
||||
const wrapper = factory();
|
||||
await flushPromises();
|
||||
const banner = wrapper.find('[data-testid="cutoff-banner"]');
|
||||
expect(banner.exists()).toBe(true);
|
||||
expect(banner.text()).toContain('18:00');
|
||||
});
|
||||
|
||||
it('hides the banner after the close button and remembers it in localStorage', async () => {
|
||||
const wrapper = factory();
|
||||
await flushPromises();
|
||||
await wrapper.find('[data-testid="cutoff-banner-close"]').trigger('click');
|
||||
await wrapper.vm.$nextTick();
|
||||
expect(wrapper.find('[data-testid="cutoff-banner"]').exists()).toBe(false);
|
||||
expect(localStorage.getItem('projects.cutoffBannerDismissed')).toBe('1');
|
||||
});
|
||||
|
||||
it('stays hidden on next mount when previously dismissed', async () => {
|
||||
localStorage.setItem('projects.cutoffBannerDismissed', '1');
|
||||
const wrapper = factory();
|
||||
await flushPromises();
|
||||
expect(wrapper.find('[data-testid="cutoff-banner"]').exists()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,7 +20,6 @@ const mountView = async () => {
|
||||
daily_limit_target: 100,
|
||||
delivered_today: 0,
|
||||
is_active: true,
|
||||
archived_at: null,
|
||||
region_mask: 1,
|
||||
region_mode: 'include',
|
||||
delivery_days_mask: 31,
|
||||
@@ -34,7 +33,6 @@ const mountView = async () => {
|
||||
daily_limit_target: 100,
|
||||
delivered_today: 0,
|
||||
is_active: true,
|
||||
archived_at: null,
|
||||
region_mask: 1,
|
||||
region_mode: 'include',
|
||||
delivery_days_mask: 31,
|
||||
|
||||
@@ -168,12 +168,21 @@ describe('api/deals', () => {
|
||||
expect(r).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('listProjects() GET /api/projects + unwraps data.projects', async () => {
|
||||
it('listProjects() GET /api/projects + unwraps { data: [...] } (JsonResource collection)', async () => {
|
||||
// ProjectController::index() отдаёт response()->json(['data' => ProjectResource::collection(...)]).
|
||||
vi.mocked(apiClient.get).mockResolvedValue({
|
||||
data: { projects: [{ id: 1, name: 'P', tag: 'site', type: 'webhook' }] },
|
||||
data: { data: [{ id: 1, name: 'B1_Окна СПб' }, { id: 2, name: 'B2_Двери' }] },
|
||||
});
|
||||
const r = await listProjects(1);
|
||||
expect(apiClient.get).toHaveBeenCalledWith('/api/projects', { params: { tenant_id: 1 } });
|
||||
expect(r[0].name).toBe('P');
|
||||
expect(Array.isArray(r)).toBe(true);
|
||||
expect(r).toHaveLength(2);
|
||||
expect(r[0].name).toBe('B1_Окна СПб');
|
||||
});
|
||||
|
||||
it('listProjects() возвращает [] при ответе без массива (защита от undefined.map)', async () => {
|
||||
vi.mocked(apiClient.get).mockResolvedValue({ data: {} });
|
||||
const r = await listProjects(1);
|
||||
expect(r).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -66,6 +66,14 @@ describe('projectsStore (no polling)', () => {
|
||||
expect(store.selectedIds.has(1)).toBe(false);
|
||||
});
|
||||
|
||||
it('del() calls DELETE /api/projects/{id}', async () => {
|
||||
const store = useProjectsStore();
|
||||
vi.spyOn(axios, 'delete').mockResolvedValue({ data: null });
|
||||
vi.spyOn(axios, 'get').mockResolvedValue({ data: { data: [], meta: { total: 0 } } });
|
||||
await store.del(7);
|
||||
expect(axios.delete).toHaveBeenCalledWith('/api/projects/7');
|
||||
});
|
||||
|
||||
it('bulkAction sends array of ids and clears selection', async () => {
|
||||
(axios.post as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({ data: { updated: 2 } });
|
||||
(axios.get as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({
|
||||
@@ -104,7 +112,6 @@ describe('projectsStore (polling)', () => {
|
||||
daily_limit_target: 10,
|
||||
delivered_today: 0,
|
||||
is_active: true,
|
||||
archived_at: null,
|
||||
region_mask: 0,
|
||||
region_mode: 'include',
|
||||
delivery_days_mask: 127,
|
||||
@@ -133,7 +140,6 @@ describe('projectsStore (polling)', () => {
|
||||
daily_limit_target: 10,
|
||||
delivered_today: 0,
|
||||
is_active: true,
|
||||
archived_at: null,
|
||||
region_mask: 0,
|
||||
region_mode: 'include',
|
||||
delivery_days_mask: 127,
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user