40 Commits
Author SHA1 Message Date
rust dda314cddc Merge pull request 'Аудит: читаемые имена и «было → стало» вместо сырых данных' (#28) from t25_audit_readable into main
ci / build-test (push) Successful in 2m48s
Reviewed-on: #28
2026-09-14 23:36:10 +03:00
stepan 16f4270995 Скрывать в аудите изменения без изменения значения
ci / build-test (pull_request) Successful in 2m59s
2026-09-13 21:45:26 +03:00
stepan ac9d92d196 Подписывать настройки и колонки в аудите по-русски
ci / build-test (pull_request) Successful in 2m59s
2026-09-13 21:39:03 +03:00
stepan fb964f9b4f Показывать в аудите имена карточек и колонок вместо идентификаторов 2026-09-13 21:39:03 +03:00
stepan f7459069d1 Нормализовать список изменённых настроек в аудите 2026-09-13 21:39:02 +03:00
stepan 37b7524665 Исправить пробел в строке изменения аудита
ci / build-test (pull_request) Successful in 2m59s
2026-09-13 21:21:02 +03:00
stepan d0e202fbc6 Показывать в аудите значения изменённых настроек
settings_updated пишет по каждому ключу «поле: было → стало» (скаляры и списки; секреты и снимки состояния — пометкой «изменено»), длинные значения обрезаются.
2026-09-13 21:21:02 +03:00
stepan 905129effa Добавить подпись поля идентификатора в аудите
ci / build-test (pull_request) Successful in 3m0s
2026-09-13 21:08:15 +03:00
stepan f31989a133 Показывать в аудите изменение «было → стало»
Перемещение карточки пишет прежнюю и новую колонку (CardMoveResultDto.From/To), изменение колонки — переименование «было → стало», разбор старого плоского формата спаривает oldBudget/budgetTokens и oldPeriod/period. Добавлены тесты разбора.
2026-09-13 21:08:15 +03:00
rust 44d2ee61f7 Merge pull request 'Операторский аудит: понятные тексты действий и изменений вместо JSON' (#27) from t25_audit_readable into main
ci / build-test (push) Successful in 3m28s
Reviewed-on: #27
2026-09-13 21:01:40 +03:00
stepan 28cf8f23ca Починить сборку сервисов в Docker после ссылок на общие контракты
ci / build-test (pull_request) Successful in 2m57s
Dockerfile ai/ml/storage/telegram копируют core/Deal.SharedKernel (нужен Deal.Grpc.Hosting), telegram — ещё core/Deal.Contracts (каталоги операций и фаз Telegram).
2026-09-13 20:54:23 +03:00
stepan b190ac75b1 Использовать каталоги операций и фаз Telegram
ci / build-test (pull_request) Canceled after 2m33s
2026-09-13 20:51:43 +03:00
stepan b7c5e13fc3 Вынести виды файлов Storage в каталог 2026-09-13 20:51:42 +03:00
stepan 04d7a0f6fb Вынести wire-ключи и операции ИИ-сервиса в каталоги 2026-09-13 20:51:42 +03:00
stepan 1fbe29a838 Вынести магические строки статусов, видов и ключей в каталоги
Литералы статусов/видов/фаз и wire-ключей заменены каталогами: значения сущностей и репозиториев — TenantStatuses/TenantLimitPeriods/InviteStatuses/ContainerKinds/ContainerSpaces/CardIds/Discovery*/PipelineQueueStatuses; контакты и виды вложений — CardContactTypes/CardFileKinds; источники и этапы отсева — PipelineRejectStages/PipelineRejectSources; ключи промптов/провайдеров — SettingsFieldKeys; wire-ключи канбана — KanbanWireKeys; режимы правил — ColumnRuleModes; операции/фазы/виды диалогов/контакты Telegram и общий BoolText — в Deal.Contracts/Deal.SharedKernel; имена метрик — DealMetrics. Значения не менялись.
2026-09-13 20:51:36 +03:00
stepan 8dde35de49 Использовать каталог AuditFields в тестах
ci / build-test (pull_request) Successful in 2m59s
2026-09-13 20:32:24 +03:00
stepan 71bbdd4dd5 Вынести булевы литералы аудита в константы 2026-09-13 20:32:24 +03:00
stepan 482b345438 Вынести коды параметров аудита и ключи JSON в константы
Магические строки кодов деталей аудита заменены каталогом AuditFields (68 мест), ключи элемента изменения и разбора JSON — константами, ключи хранилища ключей Telegram и геометрия маски — именованными константами.
2026-09-13 20:32:11 +03:00
stepan 6a88032414 Уточнить документацию аудита: логин реального пользователя
ci / build-test (pull_request) Successful in 3m12s
2026-09-13 20:23:19 +03:00
stepan 525759b232 Проверить логин владельца в ответе аудита
Подставка хранилища пользователей умеет FindOwnerLoginsByTenantIdsAsync; тест аудита проверяет userName.
2026-09-13 20:23:19 +03:00
stepan a99bb4a713 Показывать реального пользователя в аудите и лимитах
Добавлен батч-резолв логинов владельцев пространств (IAuthStore.FindOwnerLoginsByTenantIdsAsync). Ответы аудита дополнены userName (владелец пространства либо логин попытки), ответы лимитов — ownerLogin; имя пространства остаётся вторичным.
2026-09-13 20:23:12 +03:00
stepan c46bbe9332 Вывести логин пользователя в консоли оператора
В аудите и лимитах основной строкой показывается логин реального пользователя, имя пространства и id — вторично.
2026-09-13 20:22:59 +03:00
stepan ac2bf4b302 Описать контракт деталей аудита в документации
ci / build-test (pull_request) Successful in 2m58s
2026-09-13 19:18:16 +03:00
stepan 4a6fa374c5 Обновить тесты аудита под контракт изменений
HTTP-тесты аудита и лимитов проверяют changes вместо плоского detailJson; подставки реестра умеют FindNamesByIdsAsync. Добавлены тесты разбора деталей (новый формат, старый плоский, повреждённый JSON) и проверка ответа аудита с именем пользователя и изменениями.
2026-09-13 19:18:16 +03:00
stepan ad5fa7c083 Показать понятный аудит в консоли оператора
Названия событий, акторов, параметров и значений вынесены в ресурсы ru.js (operator.event/actor/field/value) с заделом под языки. Таблица аудита показывает понятное название действия, сырой ключ вторично, изменения «было → стало» и сырой JSON под спойлером. В колонке пользователя основное — имя, id мелко под ним. Термин «тенант» в UI оператора заменён на «Пользователь».
2026-09-13 19:18:06 +03:00
stepan 02a4f9c749 Добавить контракт изменений деталей аудита
Детали событий описываются списком изменений «параметр: было → стало» (AuditChangeDto, AuditDetails), а не произвольным JSON. Запись везде идёт через конструктор AuditDetails; разбор старого плоского формата сохранён. Операторские ручки аудита отдают AuditRecordViewDto с changes и именем пользователя (батч-резолв через ITenantRepository.FindNamesByIdsAsync). Детектор подозрительной активности читает логин через новый разбор.
2026-09-13 19:17:57 +03:00
rust 585397b9a3 Merge pull request 'Перевести оставшиеся «не найдено» в доменных сервисах на DealException' (#26) from t21_notfound_exceptions into main
ci / build-test (push) Successful in 2m56s
Reviewed-on: #26
2026-09-13 18:58:39 +03:00
stepan 75de70e09c Перевести «не найдено» пользователя у оператора на исключение
ci / build-test (pull_request) Successful in 3m7s
TenantAdminService.GetAsync бросает NotFoundException вместо null; эндпоинт деталей без 404-проверки.
2026-09-13 18:49:18 +03:00
stepan 891a894aed Перевести «не найдено» ML/Pipeline на NotFoundException
MlReviewService.ApplyAsync (исходное сообщение) и PipelineProcessingService.ReturnAsync (запись отсева) бросают NotFoundException; эндпоинты без 404-проверок.
2026-09-13 18:44:33 +03:00
stepan f93fb0fdd3 Перевести «не найдено» кандидатов Discovery на исключения
DiscoveryCandidatesService (get/set/status/mark_joined/mark_rejected/add при отсутствии задачи) бросает NotFoundException; skip-ветки add оставлены null. Эндпоинты без локальных 404-проверок.
2026-09-13 18:27:18 +03:00
stepan d2d6b81aa0 Перевести «не найдено» карточек на NotFoundException
CardsService (get/move/links/reminders) и CardMover бросают NotFoundException вместо null/CardResultDto(null,null)/Exists=false. CardMoveResultDto — только Error; эндпоинты без локальных 404-проверок.
2026-09-13 18:23:53 +03:00
rust a9bfecf9cc Merge pull request 'Поднять dev-стек в Docker (фронт + observability) и починить подключение Telegram' (#24) from t23_dev_stack_telegram into main
ci / build-test (push) Successful in 2m55s
Reviewed-on: #24
2026-09-13 17:58:14 +03:00
stepan 13d7994511 Обновить тест статуса Telegram под новую семантику
ci / build-test (pull_request) Successful in 2m50s
connected = phase ready; живой account приоритетнее KV tgAccount.
2026-09-13 16:27:19 +03:00
stepan a9f2b3a1ef Починить QR-вход и реактивный статус Telegram
ci / build-test (pull_request) Failing after 1m58s
tgStart ждёт старт QR до показа картинки (устранена гонка с 404). Вкладка Telegram опрашивает статус и обновляет QR по таймеру, не останавливаясь до авторизации. Статус: account берётся из живого статуса сервиса, connected = phase ready (авторизация), а не транспорт.
2026-09-13 16:22:20 +03:00
stepan a9bcd7c5f7 Поднять фронт и observability в dev-стеке
Сервис frontend: сборка SPA (Vite) и отдача через Caddy, /api → core:5080. В профиль observability добавлены Loki/Promtail/Grafana (в dev их не было). Dev-Caddyfile отдаёт SPA с Cache-Control no-cache, чтобы пересборка подхватывалась обновлением страницы.
2026-09-13 16:22:16 +03:00
rust beaf20df42 Merge pull request 'Запретить возврат null, унифицировать исключения и вынести тексты в ресурсы' (#20) from t16_null_exceptions_resources into main
ci / build-test (push) Successful in 3m30s
Reviewed-on: #20
2026-09-13 14:54:26 +03:00
rust 4526532b1a Merge branch 'main' into t16_null_exceptions_resources
ci / build-test (pull_request) Successful in 3m1s
2026-09-13 14:51:18 +03:00
rust 79793a7635 Merge pull request 'Проверить явную реализацию интерфейсов и дублирование summary' (#19) from t15_explicit_ifaces into main
ci / build-test (push) Successful in 3m23s
Reviewed-on: #19
2026-09-13 14:50:23 +03:00
stepan b8570e3197 Убрать XML-доки из реализаций интерфейсов
ci / build-test (pull_request) Successful in 2m53s
Описание живёт только в интерфейсе: удалены классовые summary и
док-блоки членов-реализаций (28 файлов). Правило уточнено в §11.
2026-09-13 14:15:23 +03:00
stepan 0d2204219a Явные реализации интерфейсов в проде
TenantContext, LocalTelegramGateway, DefaultPasswordHasher и
WTelegramSessionClient переведены на явные реализации; типы в тестах
приведены к ITenantContext.
2026-09-13 14:15:23 +03:00
178 changed files with 3278 additions and 981 deletions
+19
View File
@@ -0,0 +1,19 @@
# Dev-edge «Дейла» (compose.dev.yml, сервис frontend): SPA + /api на core.
# Отличие от prod-Caddyfile: HTTP без TLS и без плейсхолдер-домена — для локального просмотра UI.
# Статика — собранный SPA (Vite) в /srv, неизвестные пути отдают index.html (история браузера).
:80 {
# API core: /api/* уходит на core:5080 без перезаписи (контракт /api неизменен).
# SSE (/api/events), файлы и QR-SVG проходят reverse_proxy потоково.
handle /api/* {
reverse_proxy core:5080
}
handle {
# SPA/ассеты в dev не кэшируем: пересборка фронта должна подхватываться по F5.
header Cache-Control "no-cache"
root * /srv
try_files {path} /index.html
file_server
}
}
+65
View File
@@ -254,6 +254,18 @@ services:
timeout: 3s timeout: 3s
retries: 10 retries: 10
# Фронтенд (SPA) — сборка образа (Vite) и отдача через Caddy; /api → core:5080. UI — http://localhost:8080.
frontend:
build:
context: ..
dockerfile: src/frontend/Dockerfile
container_name: deal-frontend
ports:
- "8080:80"
depends_on:
core:
condition: service_healthy
# Prometheus (профиль observability, этап 12/пакет A) — сбор /metrics всех 4 процессов (:9464) # Prometheus (профиль observability, этап 12/пакет A) — сбор /metrics всех 4 процессов (:9464)
# внутри dev-сети. Подъём: docker compose -f deploy/compose.dev.yml --profile observability up -d. # внутри dev-сети. Подъём: docker compose -f deploy/compose.dev.yml --profile observability up -d.
# Конфиг — общий deploy/observability/prometheus.yml (те же имена сервисов и таргеты). UI — 9090. # Конфиг — общий deploy/observability/prometheus.yml (те же имена сервисов и таргеты). UI — 9090.
@@ -332,6 +344,56 @@ services:
- /:/host/root:ro - /:/host/root:ro
pid: host pid: host
# Loki — хранилище логов (профиль observability), UI/API — :3100.
loki:
image: grafana/loki:3.4.2
container_name: deal-loki
profiles: ["observability"]
command: -config.file=/etc/loki/loki.yml
ports:
- "3100:3100"
volumes:
- ./observability/loki.yml:/etc/loki/loki.yml:ro
- deal_loki_data:/loki
# Promtail — сбор docker-логов deal-процессов в Loki (docker.sock, профиль observability).
promtail:
image: grafana/promtail:3.4.2
container_name: deal-promtail
profiles: ["observability"]
command: -config.file=/etc/promtail/promtail.yml
volumes:
- ./observability/promtail.yml:/etc/promtail/promtail.yml:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- deal_promtail_data:/var/lib/promtail
depends_on:
loki:
condition: service_started
# Grafana — UI логов/метрик/трейсов (профиль observability), локальный вход admin/admin.
grafana:
image: grafana/grafana:11.5.2
container_name: deal-grafana
profiles: ["observability"]
environment:
GF_SECURITY_ADMIN_USER: admin
GF_SECURITY_ADMIN_PASSWORD: admin
GF_USERS_ALLOW_SIGN_UP: "false"
GF_AUTH_ANONYMOUS_ENABLED: "false"
ports:
- "3001:3000"
volumes:
- ./observability/grafana/provisioning:/etc/grafana/provisioning:ro
- ./observability/grafana/dashboards:/var/lib/grafana/dashboards:ro
- deal_grafana_data:/var/lib/grafana
depends_on:
loki:
condition: service_started
prometheus:
condition: service_started
tempo:
condition: service_started
volumes: volumes:
deal_pgdata: deal_pgdata:
deal_minio_data: deal_minio_data:
@@ -340,3 +402,6 @@ volumes:
deal_api_data: deal_api_data:
deal_prometheus_data: deal_prometheus_data:
deal_tempo_data: deal_tempo_data:
deal_loki_data:
deal_promtail_data:
deal_grafana_data:
@@ -149,8 +149,14 @@
"actorType": "tenant", "actorType": "tenant",
"actorId": "1f2e3d4c-5b6a-7980-1234-56789abcdef0", "actorId": "1f2e3d4c-5b6a-7980-1234-56789abcdef0",
"tenantId": "aabbccdd-eeff-0011-2233-445566778899", "tenantId": "aabbccdd-eeff-0011-2233-445566778899",
"userName": "owner@example.com",
"tenantName": "ООО «Ромашка»",
"ip": "203.0.113.7", "ip": "203.0.113.7",
"detailJson": "{\"cardId\":\"c_1a2b3c4d5e6f\",\"to\":\"planned\"}", "changes": [
{ "field": "cardId", "to": "c_1a2b3c4d5e6f" },
{ "field": "to", "to": "planned" }
],
"detailJson": "{\"changes\":[{\"field\":\"cardId\",\"to\":\"c_1a2b3c4d5e6f\"},{\"field\":\"to\",\"to\":\"planned\"}]}",
"at": "2026-09-10T15:22:46.123Z", "at": "2026-09-10T15:22:46.123Z",
"id": 1042 "id": 1042
} }
@@ -162,7 +168,19 @@
``` ```
- `items` — новые сверху (`at` DESC). `total` — полное число по фильтру (без `limit`/`offset`). - `items` — новые сверху (`at` DESC). `total` — полное число по фильтру (без `limit`/`offset`).
- `detailJson`**строка** JSON деталей события (без секретов), может быть `null`. - `userName` — логин реального пользователя (владелец пространства либо логин попытки); `null`, если не разрешён.
- `tenantName` — имя пространства события (join с реестром); `null`, если не разрешено.
- `changes` — человекочитаемые изменения параметров события (см. «Детали события»); `[]`, если деталей нет.
- `detailJson`**строка** сырого JSON деталей события (без секретов) для спойлера; может быть `null`.
### Детали события (`changes`)
Детали события хранятся как JSON вида `{ "changes": [ { "field": "<код>", "from": "<было>", "to": "<стало>" } ] }`,
где `field` — стабильный код параметра, `from` — предыдущее значение (`null` — параметр задан впервые),
`to` — новое. Сериализация деталей — `AuditService.ToDetailJson`, форма изменения — `AuditDetails.Set`/
`AuditDetails.Change`. Ядро отдаёт `changes` как есть; человекочитаемые названия событий, акторов, параметров
и значений — в ресурсах интерфейса (`ru.js`: `operator.event`/`operator.actor`/`operator.field`/`operator.value`).
Прежние записи плоского формата (`{ "login": "..." }`, пары `old*`/`new*`) читаются обратно совместимо.
**Коды**: `200`, `401`. **Коды**: `200`, `401`.
+2 -1
View File
@@ -271,7 +271,8 @@
- **Явная реализация интерфейсов — по умолчанию** (`Task ICardStore.GetAsync(...)`). **[изм. 2026-09-11, - **Явная реализация интерфейсов — по умолчанию** (`Task ICardStore.GetAsync(...)`). **[изм. 2026-09-11,
решение владельца]** Классы напрямую не вызываются — только через интерфейсы; исключения: DTO/модели решение владельца]** Классы напрямую не вызываются — только через интерфейсы; исключения: DTO/модели
(напр. `Card` и семейство `I*Card`), хелперы, extension-классы. Весь прод-код уже переведён на явные (напр. `Card` и семейство `I*Card`), хелперы, extension-классы. Весь прод-код уже переведён на явные
реализации (codemod `scripts/make_explicit.py`, идемпотентный). реализации (codemod `scripts/make_explicit.py` и `scripts/strip_implementation_docs.py` — идемпотентны,
`--apply` применяет правки, без флага — dry-run-отчёт).
- Один публичный тип интерфейса = один файл (как и для классов); имя файла = имя типа. - Один публичный тип интерфейса = один файл (как и для классов); имя файла = имя типа.
- **Маркерные классы не используются** — если нужен маркер, это маркерный интерфейс - **Маркерные классы не используются** — если нужен маркер, это маркерный интерфейс
(`IKanbanModule`, `ISharedKernel` и т.п.). **[изм. 2026-09-11]** (`IKanbanModule`, `ISharedKernel` и т.п.). **[изм. 2026-09-11]**
+27
View File
@@ -0,0 +1,27 @@
namespace Deal.Ai;
/// <summary>
/// Имена операций AiService для аудита
/// </summary>
public static class AiOperations
{
/// <summary>
/// Операция ИИ-фильтра входящих сообщений.
/// </summary>
public const string Filter = "filter";
/// <summary>
/// Операция полного разбора лида.
/// </summary>
public const string Classify = "classify";
/// <summary>
/// Операция генерации ключевых слов discovery-задачи.
/// </summary>
public const string GenerateKeywords = "generate_keywords";
/// <summary>
/// Операция оценки соответствия сообщения задаче поиска.
/// </summary>
public const string EvaluateFit = "evaluate_fit";
}
+4 -4
View File
@@ -150,7 +150,7 @@ public sealed class AiServiceImpl : AiService.AiServiceBase
} }
catch (LlmCallException callError) catch (LlmCallException callError)
{ {
LogAiUnavailable("filter", tenantId, config, callError); LogAiUnavailable(AiOperations.Filter, tenantId, config, callError);
throw ToUnavailable(callError); throw ToUnavailable(callError);
} }
} }
@@ -191,7 +191,7 @@ public sealed class AiServiceImpl : AiService.AiServiceBase
} }
catch (LlmCallException callError) catch (LlmCallException callError)
{ {
LogAiUnavailable("classify", tenantId, config, callError); LogAiUnavailable(AiOperations.Classify, tenantId, config, callError);
throw ToUnavailable(callError); throw ToUnavailable(callError);
} }
} }
@@ -226,7 +226,7 @@ public sealed class AiServiceImpl : AiService.AiServiceBase
} }
catch (LlmCallException callError) catch (LlmCallException callError)
{ {
LogAiUnavailable("generate_keywords", tenantId, config, callError); LogAiUnavailable(AiOperations.GenerateKeywords, tenantId, config, callError);
throw ToUnavailable(callError); throw ToUnavailable(callError);
} }
} }
@@ -267,7 +267,7 @@ public sealed class AiServiceImpl : AiService.AiServiceBase
} }
catch (LlmCallException callError) catch (LlmCallException callError)
{ {
LogAiUnavailable("evaluate_fit", tenantId, config, callError); LogAiUnavailable(AiOperations.EvaluateFit, tenantId, config, callError);
throw ToUnavailable(callError); throw ToUnavailable(callError);
} }
} }
+2
View File
@@ -12,6 +12,7 @@ WORKDIR /repo
# Restore-слой: только csproj/props (кэш слоёв Docker — restore не повторяется при правке исходников). # Restore-слой: только csproj/props (кэш слоёв Docker — restore не повторяется при правке исходников).
COPY src/contracts/Deal.Proto.csproj src/contracts/ COPY src/contracts/Deal.Proto.csproj src/contracts/
COPY src/grpc-hosting/Deal.Grpc.Hosting/Deal.Grpc.Hosting.csproj src/grpc-hosting/Deal.Grpc.Hosting/ COPY src/grpc-hosting/Deal.Grpc.Hosting/Deal.Grpc.Hosting.csproj src/grpc-hosting/Deal.Grpc.Hosting/
COPY src/core/Deal.SharedKernel/Deal.SharedKernel.csproj src/core/Deal.SharedKernel/
COPY src/ai-service/Directory.Build.props src/ai-service/ COPY src/ai-service/Directory.Build.props src/ai-service/
COPY src/ai-service/Deal.Ai/Deal.Ai.csproj src/ai-service/Deal.Ai/ COPY src/ai-service/Deal.Ai/Deal.Ai.csproj src/ai-service/Deal.Ai/
RUN dotnet restore src/ai-service/Deal.Ai/Deal.Ai.csproj RUN dotnet restore src/ai-service/Deal.Ai/Deal.Ai.csproj
@@ -19,6 +20,7 @@ RUN dotnet restore src/ai-service/Deal.Ai/Deal.Ai.csproj
# Исходники: контракты (.proto) + общая gRPC-обвязка + проект сервиса. # Исходники: контракты (.proto) + общая gRPC-обвязка + проект сервиса.
COPY src/contracts/ src/contracts/ COPY src/contracts/ src/contracts/
COPY src/grpc-hosting/ src/grpc-hosting/ COPY src/grpc-hosting/ src/grpc-hosting/
COPY src/core/Deal.SharedKernel/ src/core/Deal.SharedKernel/
COPY src/ai-service/Deal.Ai/ src/ai-service/Deal.Ai/ COPY src/ai-service/Deal.Ai/ src/ai-service/Deal.Ai/
RUN dotnet publish src/ai-service/Deal.Ai/Deal.Ai.csproj -c Release -o /app/publish RUN dotnet publish src/ai-service/Deal.Ai/Deal.Ai.csproj -c Release -o /app/publish
@@ -0,0 +1,17 @@
namespace Deal.Ai.Llm;
/// <summary>
/// Роли сообщений чата в wire-формате ИИ-провайдеров
/// </summary>
public static class LlmChatRoles
{
/// <summary>
/// Роль системного сообщения.
/// </summary>
public const string System = "system";
/// <summary>
/// Роль пользовательского сообщения.
/// </summary>
public const string User = "user";
}
+25 -35
View File
@@ -4,9 +4,6 @@ using System.Text.Json.Nodes;
namespace Deal.Ai.Llm; namespace Deal.Ai.Llm;
/// <summary>
/// HTTP-реализация <see cref="IProviderClient"/>
/// </summary>
public sealed class LlmHttpClient : IProviderClient public sealed class LlmHttpClient : IProviderClient
{ {
// Относительный путь OpenAI-совместимого эндпоинта (база уже без хвостового «/»). // Относительный путь OpenAI-совместимого эндпоинта (база уже без хвостового «/»).
@@ -58,13 +55,6 @@ public sealed class LlmHttpClient : IProviderClient
_anthropicCallTimeout = anthropicCallTimeout; _anthropicCallTimeout = anthropicCallTimeout;
} }
/// <summary>
/// Выполняет один вызов модели по выбранной схеме API.
/// </summary>
/// <param name="config">Конфиг провайдера (стиль — <c>ApiStyle</c>).</param>
/// <param name="systemPrompt">Системный промпт.</param>
/// <param name="userText">Пользовательское сообщение/контекст.</param>
/// <returns>Текст ответа и usage API-ответа (null при его отсутствии).</returns>
async Task<ProviderChatResult> IProviderClient.ChatAsync( async Task<ProviderChatResult> IProviderClient.ChatAsync(
LlmConfig config, LlmConfig config,
string systemPrompt, string systemPrompt,
@@ -110,12 +100,12 @@ public sealed class LlmHttpClient : IProviderClient
var body = new JsonObject var body = new JsonObject
{ {
["model"] = config.Model, [LlmWireKeys.Model] = config.Model,
["messages"] = new JsonArray( [LlmWireKeys.Messages] = new JsonArray(
ChatMessage("system", systemPrompt), ChatMessage(LlmChatRoles.System, systemPrompt),
ChatMessage("user", userText)), ChatMessage(LlmChatRoles.User, userText)),
["temperature"] = Temperature, [LlmWireKeys.Temperature] = Temperature,
["max_tokens"] = MaxResponseTokens, [LlmWireKeys.MaxTokens] = MaxResponseTokens,
}; };
request.Content = JsonBody(body); request.Content = JsonBody(body);
@@ -133,10 +123,10 @@ public sealed class LlmHttpClient : IProviderClient
var body = new JsonObject var body = new JsonObject
{ {
["model"] = config.Model, [LlmWireKeys.Model] = config.Model,
["max_tokens"] = MaxResponseTokens, [LlmWireKeys.MaxTokens] = MaxResponseTokens,
["system"] = systemPrompt, [LlmWireKeys.System] = systemPrompt,
["messages"] = new JsonArray(ChatMessage("user", userText)), [LlmWireKeys.Messages] = new JsonArray(ChatMessage(LlmChatRoles.User, userText)),
}; };
request.Content = JsonBody(body); request.Content = JsonBody(body);
@@ -166,7 +156,7 @@ public sealed class LlmHttpClient : IProviderClient
private static ProviderChatResult ReadOpenAiBody(string body) private static ProviderChatResult ReadOpenAiBody(string body)
{ {
JsonObject? payload = ParseObjectOrThrow(body); JsonObject? payload = ParseObjectOrThrow(body);
JsonArray? choices = payload["choices"] as JsonArray; JsonArray? choices = payload[LlmWireKeys.Choices] as JsonArray;
if (choices is null || choices.Count == 0) if (choices is null || choices.Count == 0)
{ {
// Тип сбоя без содержимого тела (замечание code-review: тело ответа наружу/в лог не уходит). // Тип сбоя без содержимого тела (замечание code-review: тело ответа наружу/в лог не уходит).
@@ -174,14 +164,14 @@ public sealed class LlmHttpClient : IProviderClient
} }
// choices[i] — объект выбора {message, finish_reason, …}; текст — в message.content. // choices[i] — объект выбора {message, finish_reason, …}; текст — в message.content.
JsonObject? message = (choices[0] as JsonObject)?["message"] as JsonObject; JsonObject? message = (choices[0] as JsonObject)?[LlmWireKeys.Message] as JsonObject;
string? content = ReadStringField(message, "content"); string? content = ReadStringField(message, LlmWireKeys.Content);
if (string.IsNullOrEmpty(content) && !string.IsNullOrEmpty(ReadStringField(message, "reasoning_content"))) if (string.IsNullOrEmpty(content) && !string.IsNullOrEmpty(ReadStringField(message, LlmWireKeys.ReasoningContent)))
{ {
throw new LlmHttpException("Модель вернула только reasoning без ответа"); throw new LlmHttpException("Модель вернула только reasoning без ответа");
} }
return new ProviderChatResult(content ?? string.Empty, ReadOpenAiUsage(payload["usage"])); return new ProviderChatResult(content ?? string.Empty, ReadOpenAiUsage(payload[LlmWireKeys.Usage]));
} }
private static ProviderChatResult ReadAnthropicBody(string body) private static ProviderChatResult ReadAnthropicBody(string body)
@@ -189,18 +179,18 @@ public sealed class LlmHttpClient : IProviderClient
JsonObject? payload = ParseObjectOrThrow(body); JsonObject? payload = ParseObjectOrThrow(body);
var text = new StringBuilder(); var text = new StringBuilder();
if (payload["content"] is JsonArray contentBlocks) if (payload[LlmWireKeys.Content] is JsonArray contentBlocks)
{ {
foreach (JsonNode? blockNode in contentBlocks) foreach (JsonNode? blockNode in contentBlocks)
{ {
if (blockNode is JsonObject block) if (blockNode is JsonObject block)
{ {
text.Append(ReadStringField(block, "text")); text.Append(ReadStringField(block, LlmWireKeys.Text));
} }
} }
} }
return new ProviderChatResult(text.ToString(), ReadAnthropicUsage(payload["usage"])); return new ProviderChatResult(text.ToString(), ReadAnthropicUsage(payload[LlmWireKeys.Usage]));
} }
// Возвращает usage OpenAI-совместимого ответа (prompt/completion/total_tokens) либо null. // Возвращает usage OpenAI-совместимого ответа (prompt/completion/total_tokens) либо null.
@@ -212,9 +202,9 @@ public sealed class LlmHttpClient : IProviderClient
return null; return null;
} }
int? promptTokens = ReadIntField(usage, "prompt_tokens"); int? promptTokens = ReadIntField(usage, LlmWireKeys.PromptTokens);
int? completionTokens = ReadIntField(usage, "completion_tokens"); int? completionTokens = ReadIntField(usage, LlmWireKeys.CompletionTokens);
int? totalTokens = ReadIntField(usage, "total_tokens"); int? totalTokens = ReadIntField(usage, LlmWireKeys.TotalTokens);
return promptTokens is null || completionTokens is null || totalTokens is null return promptTokens is null || completionTokens is null || totalTokens is null
? null ? null
: new ProviderUsage(promptTokens.Value, completionTokens.Value, totalTokens.Value); : new ProviderUsage(promptTokens.Value, completionTokens.Value, totalTokens.Value);
@@ -229,8 +219,8 @@ public sealed class LlmHttpClient : IProviderClient
return null; return null;
} }
int? inputTokens = ReadIntField(usage, "input_tokens"); int? inputTokens = ReadIntField(usage, LlmWireKeys.InputTokens);
int? outputTokens = ReadIntField(usage, "output_tokens"); int? outputTokens = ReadIntField(usage, LlmWireKeys.OutputTokens);
return inputTokens is null || outputTokens is null return inputTokens is null || outputTokens is null
? null ? null
: new ProviderUsage(inputTokens.Value, outputTokens.Value, inputTokens.Value + outputTokens.Value); : new ProviderUsage(inputTokens.Value, outputTokens.Value, inputTokens.Value + outputTokens.Value);
@@ -264,8 +254,8 @@ public sealed class LlmHttpClient : IProviderClient
private static JsonObject ChatMessage(string role, string content) private static JsonObject ChatMessage(string role, string content)
=> new() => new()
{ {
["role"] = role, [LlmWireKeys.Role] = role,
["content"] = content, [LlmWireKeys.Content] = content,
}; };
// Создаёт JSON-содержимое запроса (application/json). // Создаёт JSON-содержимое запроса (application/json).
+92
View File
@@ -0,0 +1,92 @@
namespace Deal.Ai.Llm;
/// <summary>
/// JSON-ключи wire-формата запросов и ответов ИИ-провайдеров
/// </summary>
public static class LlmWireKeys
{
/// <summary>
/// Ключ имени модели.
/// </summary>
public const string Model = "model";
/// <summary>
/// Ключ списка сообщений чата.
/// </summary>
public const string Messages = "messages";
/// <summary>
/// Ключ системного промпта (Anthropic — отдельным полем).
/// </summary>
public const string System = "system";
/// <summary>
/// Ключ температуры генерации (OpenAI-совместимый формат).
/// </summary>
public const string Temperature = "temperature";
/// <summary>
/// Ключ лимита токенов ответа.
/// </summary>
public const string MaxTokens = "max_tokens";
/// <summary>
/// Ключ списка вариантов ответа (OpenAI-совместимый формат).
/// </summary>
public const string Choices = "choices";
/// <summary>
/// Ключ выбранного варианта ответа (OpenAI-совместимый формат).
/// </summary>
public const string Message = "message";
/// <summary>
/// Ключ текстового содержимого.
/// </summary>
public const string Content = "content";
/// <summary>
/// Ключ текста рассуждений модели (reasoning).
/// </summary>
public const string ReasoningContent = "reasoning_content";
/// <summary>
/// Ключ оценки токенов вызова.
/// </summary>
public const string Usage = "usage";
/// <summary>
/// Ключ текста блока контента (Anthropic).
/// </summary>
public const string Text = "text";
/// <summary>
/// Ключ числа токенов запроса (OpenAI-совместимый формат).
/// </summary>
public const string PromptTokens = "prompt_tokens";
/// <summary>
/// Ключ числа токенов ответа (OpenAI-совместимый формат).
/// </summary>
public const string CompletionTokens = "completion_tokens";
/// <summary>
/// Ключ суммарного числа токенов (OpenAI-совместимый формат).
/// </summary>
public const string TotalTokens = "total_tokens";
/// <summary>
/// Ключ числа входных токенов (Anthropic).
/// </summary>
public const string InputTokens = "input_tokens";
/// <summary>
/// Ключ числа выходных токенов (Anthropic).
/// </summary>
public const string OutputTokens = "output_tokens";
/// <summary>
/// Ключ роли сообщения чата.
/// </summary>
public const string Role = "role";
}
@@ -127,9 +127,9 @@ public static class AiCheckEndpoint
&& entry.ValueKind == JsonValueKind.Object) && entry.ValueKind == JsonValueKind.Object)
{ {
return new AiConfigSetting( return new AiConfigSetting(
ApiKey: ReadField(entry, "apiKey") ?? defaults.ApiKey, ApiKey: ReadField(entry, SettingsFieldKeys.ApiKey) ?? defaults.ApiKey,
BaseUrl: ReadField(entry, "baseUrl") ?? defaults.BaseUrl, BaseUrl: ReadField(entry, SettingsFieldKeys.BaseUrl) ?? defaults.BaseUrl,
Model: ReadField(entry, "model") ?? defaults.Model); Model: ReadField(entry, SettingsFieldKeys.Model) ?? defaults.Model);
} }
} }
catch (JsonException) catch (JsonException)
+5 -5
View File
@@ -68,7 +68,7 @@ public static class AuthEndpoints
ActorId: result.UserId, ActorId: result.UserId,
TenantId: result.TenantId, TenantId: result.TenantId,
Ip: ClientIp(context), Ip: ClientIp(context),
DetailJson: AuditService.ToDetailJson(new { login = NormalizeLogin(body.Login) })), ct); DetailJson: AuditService.ToDetailJson([AuditDetails.Set(AuditFields.Login, NormalizeLogin(body.Login))])), ct);
return EndpointResults.Forbidden(TenantSuspendedDetail); return EndpointResults.Forbidden(TenantSuspendedDetail);
} }
@@ -84,7 +84,7 @@ public static class AuthEndpoints
ActorId: null, ActorId: null,
TenantId: null, TenantId: null,
Ip: ClientIp(context), Ip: ClientIp(context),
DetailJson: AuditService.ToDetailJson(new { login = attemptedLogin })), ct); DetailJson: AuditService.ToDetailJson([AuditDetails.Set(AuditFields.Login, attemptedLogin)])), ct);
} }
return EndpointResults.Unauthorized(InvalidCredentialsDetail); return EndpointResults.Unauthorized(InvalidCredentialsDetail);
@@ -98,7 +98,7 @@ public static class AuthEndpoints
ActorId: result.UserId, ActorId: result.UserId,
TenantId: result.TenantId, TenantId: result.TenantId,
Ip: ClientIp(context), Ip: ClientIp(context),
DetailJson: AuditService.ToDetailJson(new { login = result.Login })), ct); DetailJson: AuditService.ToDetailJson([AuditDetails.Set(AuditFields.Login, result.Login)])), ct);
SessionCookieWriter.Append(context, cookieOptions.Value, result.Token); SessionCookieWriter.Append(context, cookieOptions.Value, result.Token);
return Results.Ok(new { ok = true, login = result.Login }); return Results.Ok(new { ok = true, login = result.Login });
@@ -125,12 +125,12 @@ public static class AuthEndpoints
ActorId: logout.OperatorId, ActorId: logout.OperatorId,
TenantId: logout.TenantId, TenantId: logout.TenantId,
Ip: ClientIp(context), Ip: ClientIp(context),
DetailJson: AuditService.ToDetailJson(new { login = logout.Login })), ct); DetailJson: AuditService.ToDetailJson([AuditDetails.Set(AuditFields.Login, logout.Login)])), ct);
} }
if (user is not null) if (user is not null)
{ {
await AuditAppender.AppendTenantAsync(context, AuditEvents.TenantLogout, new { login = user.Login }, ct); await AuditAppender.AppendTenantAsync(context, AuditEvents.TenantLogout, [AuditDetails.Set(AuditFields.Login, user.Login)], ct);
} }
context.Response.Cookies.Delete(cookieName); context.Response.Cookies.Delete(cookieName);
@@ -120,7 +120,7 @@ public static class CardDetailsEndpoints
CardsService service = context.RequestServices.GetRequiredService<CardsService>(); CardsService service = context.RequestServices.GetRequiredService<CardsService>();
CardDto created = await service.CreateLocalCardAsync(ToCreateLocalDto(body), ct); CardDto created = await service.CreateLocalCardAsync(ToCreateLocalDto(body), ct);
await AuditAppender.AppendTenantAsync(context, AuditEvents.CardCreated, new { cardId = created.Id }, ct); await AuditAppender.AppendTenantAsync(context, AuditEvents.CardCreated, [AuditDetails.Set(AuditFields.CardId, created.Id)], ct);
return await ReadCardAsync(context, created.Id, ct); return await ReadCardAsync(context, created.Id, ct);
} }
@@ -202,9 +202,7 @@ public static class CardDetailsEndpoints
return EndpointResults.BadRequest(result.Error); return EndpointResults.BadRequest(result.Error);
} }
return result.Card is null return await ReadCardAsync(context, cardId, ct);
? EndpointResults.NotFound(CardNotFoundDetail)
: await ReadCardAsync(context, cardId, ct);
} }
// DELETE /api/cards/{cardId}/links/{linkId}: удалить ссылку. Ответ — карточка. // DELETE /api/cards/{cardId}/links/{linkId}: удалить ссылку. Ответ — карточка.
@@ -220,10 +218,8 @@ public static class CardDetailsEndpoints
} }
CardsService service = context.RequestServices.GetRequiredService<CardsService>(); CardsService service = context.RequestServices.GetRequiredService<CardsService>();
CardResultDto result = await service.RemoveLinkAsync(cardId, linkId, ct); await service.RemoveLinkAsync(cardId, linkId, ct);
return result.Card is null return await ReadCardAsync(context, cardId, ct);
? EndpointResults.NotFound(CardNotFoundDetail)
: await ReadCardAsync(context, cardId, ct);
} }
// POST /api/cards/{cardId}/files: загрузка вложений (multipart/form-data, поле files). // POST /api/cards/{cardId}/files: загрузка вложений (multipart/form-data, поле files).
@@ -240,10 +236,7 @@ public static class CardDetailsEndpoints
} }
CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>(); CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>();
if (await cardsService.GetCardAsync(cardId, ct) is null) await cardsService.GetCardAsync(cardId, ct);
{
return EndpointResults.NotFound(CardNotFoundDetail);
}
IFormCollection form; IFormCollection form;
try try
@@ -357,9 +350,7 @@ public static class CardDetailsEndpoints
return EndpointResults.BadRequest(result.Error); return EndpointResults.BadRequest(result.Error);
} }
return result.Card is null return await ReadCardAsync(context, cardId, ct);
? EndpointResults.NotFound(CardNotFoundDetail)
: await ReadCardAsync(context, cardId, ct);
} }
// DELETE /api/cards/{cardId}/reminder: снять напоминание. Ответ — карточка. // DELETE /api/cards/{cardId}/reminder: снять напоминание. Ответ — карточка.
@@ -374,9 +365,8 @@ public static class CardDetailsEndpoints
} }
CardsService service = context.RequestServices.GetRequiredService<CardsService>(); CardsService service = context.RequestServices.GetRequiredService<CardsService>();
return await service.ClearReminderAsync(cardId, ct) await service.ClearReminderAsync(cardId, ct);
? await ReadCardAsync(context, cardId, ct) return await ReadCardAsync(context, cardId, ct);
: EndpointResults.NotFound(CardNotFoundDetail);
} }
// POST /api/cards/{cardId}/reminder/snooze: «напомнить позже» (now + 24 ч). Ответ — карточка. // POST /api/cards/{cardId}/reminder/snooze: «напомнить позже» (now + 24 ч). Ответ — карточка.
@@ -391,9 +381,8 @@ public static class CardDetailsEndpoints
} }
CardsService service = context.RequestServices.GetRequiredService<CardsService>(); CardsService service = context.RequestServices.GetRequiredService<CardsService>();
return await service.SnoozeReminderAsync(cardId, ct) await service.SnoozeReminderAsync(cardId, ct);
? await ReadCardAsync(context, cardId, ct) return await ReadCardAsync(context, cardId, ct);
: EndpointResults.NotFound(CardNotFoundDetail);
} }
// Читает карточку через единый сервис и возвращает её как ответ (404 — карточки нет). // Читает карточку через единый сервис и возвращает её как ответ (404 — карточки нет).
@@ -412,11 +401,7 @@ public static class CardDetailsEndpoints
CancellationToken ct) CancellationToken ct)
{ {
CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>(); CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>();
CardDto? card = await cardsService.GetCardAsync(cardId, ct); CardDto card = await cardsService.GetCardAsync(cardId, ct);
if (card is null)
{
return EndpointResults.NotFound(CardNotFoundDetail);
}
SourceContent content = await cardsService.ResolveSourceAsync(card, ct); SourceContent content = await cardsService.ResolveSourceAsync(card, ct);
return Results.Ok(content); return Results.Ok(content);
@@ -428,10 +413,8 @@ public static class CardDetailsEndpoints
CancellationToken ct) CancellationToken ct)
{ {
CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>(); CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>();
CardDto? card = await cardsService.GetCardAsync(cardId, ct); CardDto card = await cardsService.GetCardAsync(cardId, ct);
return card is null return Results.Ok(card);
? EndpointResults.NotFound(CardNotFoundDetail)
: Results.Ok(card);
} }
// Имя файла для Content-Disposition без кавычек «"». // Имя файла для Content-Disposition без кавычек «"».
+27 -28
View File
@@ -105,15 +105,15 @@ public static class CardsEndpoints
CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>(); CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>();
CardCountsDto counts = await cardsService.CountsAsync(ct); CardCountsDto counts = await cardsService.CountsAsync(ct);
var wire = new Dictionary<string, object> { ["new"] = counts.New }; var wire = new Dictionary<string, object> { [KanbanWireKeys.New] = counts.New };
foreach ((string col, CardColumnCountDto column) in counts.Columns) foreach ((string col, CardColumnCountDto column) in counts.Columns)
{ {
wire[col] = column; wire[col] = column;
} }
wire["learning"] = counts.Learning; wire[KanbanWireKeys.Learning] = counts.Learning;
wire["ml"] = counts.Ml; wire[KanbanWireKeys.Ml] = counts.Ml;
wire["ai"] = counts.Ai; wire[KanbanWireKeys.Ai] = counts.Ai;
return Results.Ok(wire); return Results.Ok(wire);
} }
@@ -128,10 +128,8 @@ public static class CardsEndpoints
} }
CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>(); CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>();
CardDto? card = await cardsService.GetCardAsync(cardId, ct); CardDto card = await cardsService.GetCardAsync(cardId, ct);
return card is null return Results.Ok(card);
? EndpointResults.NotFound(CardNotFoundDetail)
: Results.Ok(card);
} }
private static async Task<IResult> MarkAllSeenAsync(HttpContext context, CancellationToken ct) private static async Task<IResult> MarkAllSeenAsync(HttpContext context, CancellationToken ct)
@@ -184,18 +182,18 @@ public static class CardsEndpoints
return EndpointResults.BadRequest(outcome.Error); return EndpointResults.BadRequest(outcome.Error);
} }
if (!outcome.Exists) await AuditAppender.AppendTenantAsync(
{ context,
return EndpointResults.NotFound(CardNotFoundDetail); AuditEvents.CardMoved,
} [
AuditDetails.Set(AuditFields.CardId, cardId),
await AuditAppender.AppendTenantAsync(context, AuditEvents.CardMoved, new { cardId, to = body.To }, ct); AuditDetails.Change(AuditFields.ContainerId, outcome.From, outcome.To),
],
ct);
CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>(); CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>();
CardDto? unified = await cardsService.GetCardAsync(cardId, ct); CardDto unified = await cardsService.GetCardAsync(cardId, ct);
return unified is null return Results.Ok(unified);
? EndpointResults.NotFound(CardNotFoundDetail)
: Results.Ok(unified);
} }
private static async Task<IResult> TrashAsync( private static async Task<IResult> TrashAsync(
@@ -211,7 +209,7 @@ public static class CardsEndpoints
CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>(); CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>();
await cardsService.TrashCardAsync(cardId, ct); await cardsService.TrashCardAsync(cardId, ct);
await AuditAppender.AppendTenantAsync(context, AuditEvents.CardTrashed, new { cardId }, ct); await AuditAppender.AppendTenantAsync(context, AuditEvents.CardTrashed, [AuditDetails.Set(AuditFields.CardId, cardId)], ct);
return Results.Ok(new { ok = true }); return Results.Ok(new { ok = true });
} }
@@ -228,7 +226,7 @@ public static class CardsEndpoints
CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>(); CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>();
string col = await cardsService.RestoreCardAsync(cardId, ct); string col = await cardsService.RestoreCardAsync(cardId, ct);
await AuditAppender.AppendTenantAsync(context, AuditEvents.CardRestored, new { cardId, col }, ct); await AuditAppender.AppendTenantAsync(context, AuditEvents.CardRestored, [AuditDetails.Set(AuditFields.CardId, cardId), AuditDetails.Set(AuditFields.Column, col)], ct);
return Results.Ok(new { ok = true, col }); return Results.Ok(new { ok = true, col });
} }
@@ -249,7 +247,7 @@ public static class CardsEndpoints
return EndpointResults.NotFound(CardNotFoundDetail); return EndpointResults.NotFound(CardNotFoundDetail);
} }
await AuditAppender.AppendTenantAsync(context, AuditEvents.CardDeleted, new { cardId }, ct); await AuditAppender.AppendTenantAsync(context, AuditEvents.CardDeleted, [AuditDetails.Set(AuditFields.CardId, cardId)], ct);
return Results.Ok(new { ok = true }); return Results.Ok(new { ok = true });
} }
@@ -293,7 +291,7 @@ public static class CardsEndpoints
return EndpointResults.NotFound(CardNotFoundDetail); return EndpointResults.NotFound(CardNotFoundDetail);
} }
await AuditAppender.AppendTenantAsync(context, AuditEvents.CardCommentAdded, new { cardId }, ct); await AuditAppender.AppendTenantAsync(context, AuditEvents.CardCommentAdded, [AuditDetails.Set(AuditFields.CardId, cardId)], ct);
return Results.Ok(new { comments = result.Comments }); return Results.Ok(new { comments = result.Comments });
} }
@@ -353,11 +351,7 @@ public static class CardsEndpoints
} }
CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>(); CardsService cardsService = context.RequestServices.GetRequiredService<CardsService>();
CardDto? card = await cardsService.GetCardAsync(cardId, ct); CardDto card = await cardsService.GetCardAsync(cardId, ct);
if (card is null)
{
return EndpointResults.NotFound(CardNotFoundDetail);
}
CardReclassifier reclassifier = context.RequestServices.GetRequiredService<CardReclassifier>(); CardReclassifier reclassifier = context.RequestServices.GetRequiredService<CardReclassifier>();
ReclassifyResultDto result = await reclassifier.ReclassifyCardAsync(card, ct); ReclassifyResultDto result = await reclassifier.ReclassifyCardAsync(card, ct);
@@ -414,7 +408,12 @@ public static class CardsEndpoints
return AuditAppender.AppendTenantAsync( return AuditAppender.AppendTenantAsync(
context, context,
AuditEvents.CardReclassified, AuditEvents.CardReclassified,
new { attempted = result.Attempted, reclassified = result.Reclassified, moved = result.Moved, trashed = result.Trashed }, [
AuditDetails.Set(AuditFields.Attempted, result.Attempted),
AuditDetails.Set(AuditFields.Reclassified, result.Reclassified),
AuditDetails.Set(AuditFields.Moved, result.Moved),
AuditDetails.Set(AuditFields.Trashed, result.Trashed),
],
ct); ct);
} }
@@ -94,7 +94,7 @@ public static class ContainersEndpoints
Note: body.Note ?? string.Empty), Note: body.Note ?? string.Empty),
ct); ct);
await AuditAppender.AppendTenantAsync(context, AuditEvents.ContainerCreated, new { id = created.Id, name = created.Name }, ct); await AuditAppender.AppendTenantAsync(context, AuditEvents.ContainerCreated, [AuditDetails.Set(AuditFields.ContainerId, created.Id), AuditDetails.Set(AuditFields.Name, created.Name)], ct);
return Results.Ok(new { id = created.Id }); return Results.Ok(new { id = created.Id });
} }
@@ -140,6 +140,7 @@ public static class ContainersEndpoints
} }
ContainersService containers = context.RequestServices.GetRequiredService<ContainersService>(); ContainersService containers = context.RequestServices.GetRequiredService<ContainersService>();
ContainerDto before = await containers.GetAsync(containerId, ct);
ContainerDto updated = await containers.PatchAsync( ContainerDto updated = await containers.PatchAsync(
containerId, containerId,
new ContainerPatchDto( new ContainerPatchDto(
@@ -153,7 +154,13 @@ public static class ContainersEndpoints
patchBody.Policy), patchBody.Policy),
ct); ct);
await AuditAppender.AppendTenantAsync(context, AuditEvents.ContainerUpdated, new { id = updated.Id }, ct); await AuditAppender.AppendTenantAsync(
context,
AuditEvents.ContainerUpdated,
string.Equals(before.Name, updated.Name, StringComparison.Ordinal)
? [AuditDetails.Set(AuditFields.ContainerId, updated.Id)]
: [AuditDetails.Change(AuditFields.Name, before.Name, updated.Name)],
ct);
return Results.Ok(new { id = updated.Id }); return Results.Ok(new { id = updated.Id });
} }
@@ -171,7 +178,7 @@ public static class ContainersEndpoints
ContainersService containers = context.RequestServices.GetRequiredService<ContainersService>(); ContainersService containers = context.RequestServices.GetRequiredService<ContainersService>();
ContainerDto accepted = await containers.AcceptSuggestedAsync(containerId, ct); ContainerDto accepted = await containers.AcceptSuggestedAsync(containerId, ct);
await AuditAppender.AppendTenantAsync(context, AuditEvents.ContainerUpdated, new { id = accepted.Id }, ct); await AuditAppender.AppendTenantAsync(context, AuditEvents.ContainerUpdated, [AuditDetails.Set(AuditFields.ContainerId, accepted.Id)], ct);
return Results.Ok(accepted); return Results.Ok(accepted);
} }
@@ -189,7 +196,7 @@ public static class ContainersEndpoints
ContainersService containers = context.RequestServices.GetRequiredService<ContainersService>(); ContainersService containers = context.RequestServices.GetRequiredService<ContainersService>();
int moved = await containers.DeleteAsync(containerId, ct); int moved = await containers.DeleteAsync(containerId, ct);
await AuditAppender.AppendTenantAsync(context, AuditEvents.ContainerDeleted, new { id = containerId }, ct); await AuditAppender.AppendTenantAsync(context, AuditEvents.ContainerDeleted, [AuditDetails.Set(AuditFields.ContainerId, containerId)], ct);
return Results.Ok(new { ok = true, movedToInbox = moved }); return Results.Ok(new { ok = true, movedToInbox = moved });
} }
@@ -288,12 +295,12 @@ public static class ContainersEndpoints
var wire = new Dictionary<string, object>(); var wire = new Dictionary<string, object>();
if (state.Collapsed is { } collapsed) if (state.Collapsed is { } collapsed)
{ {
wire["collapsed"] = collapsed; wire[KanbanWireKeys.Collapsed] = collapsed;
} }
if (state.Width is not null) if (state.Width is not null)
{ {
wire["width"] = state.Width; wire[KanbanWireKeys.Width] = state.Width;
} }
return wire; return wire;
@@ -55,9 +55,6 @@ public static class DiscoveryEndpoints
// Путь лога задачи (GET). // Путь лога задачи (GET).
private const string TaskLogPath = "/tasks/{task_id}/log"; private const string TaskLogPath = "/tasks/{task_id}/log";
// 404: кандидат не найден.
private const string CandidateNotFoundDetail = "Кандидат не найден";
private const string AlreadyJoinedDetail = "Уже вступили в этот источник"; private const string AlreadyJoinedDetail = "Уже вступили в этот источник";
private const string JoinedRejectDetail = "Уже вступили — удалите источник из каналов"; private const string JoinedRejectDetail = "Уже вступили — удалите источник из каналов";
@@ -281,11 +278,7 @@ public static class DiscoveryEndpoints
} }
DiscoveryCandidatesService candidates = context.RequestServices.GetRequiredService<DiscoveryCandidatesService>(); DiscoveryCandidatesService candidates = context.RequestServices.GetRequiredService<DiscoveryCandidatesService>();
DiscoveryCandidateDto? row = await candidates.GetAsync(dialog_id, ct); DiscoveryCandidateDto row = await candidates.GetAsync(dialog_id, ct);
if (row is null)
{
return EndpointResults.NotFound(CandidateNotFoundDetail);
}
if (row.Status == DiscoveryCandidateStatuses.Joined) if (row.Status == DiscoveryCandidateStatuses.Joined)
{ {
@@ -314,8 +307,8 @@ public static class DiscoveryEndpoints
try try
{ {
DiscoveryCandidateDto? joined = await candidates.MarkJoinedAsync(dialog_id, auto: false, ct); DiscoveryCandidateDto joined = await candidates.MarkJoinedAsync(dialog_id, auto: false, ct);
return joined is null ? EndpointResults.NotFound(CandidateNotFoundDetail) : Results.Ok(joined); return Results.Ok(joined);
} }
catch (DiscoveryValidationException exception) catch (DiscoveryValidationException exception)
{ {
@@ -334,11 +327,7 @@ public static class DiscoveryEndpoints
} }
DiscoveryCandidatesService candidates = context.RequestServices.GetRequiredService<DiscoveryCandidatesService>(); DiscoveryCandidatesService candidates = context.RequestServices.GetRequiredService<DiscoveryCandidatesService>();
DiscoveryCandidateDto? row = await candidates.GetAsync(dialog_id, ct); DiscoveryCandidateDto row = await candidates.GetAsync(dialog_id, ct);
if (row is null)
{
return EndpointResults.NotFound(CandidateNotFoundDetail);
}
if (row.Status == DiscoveryCandidateStatuses.Joined) if (row.Status == DiscoveryCandidateStatuses.Joined)
{ {
@@ -347,8 +336,8 @@ public static class DiscoveryEndpoints
try try
{ {
DiscoveryCandidateDto? rejected = await candidates.MarkRejectedAsync(dialog_id, ManualRejectReason, ct); DiscoveryCandidateDto rejected = await candidates.MarkRejectedAsync(dialog_id, ManualRejectReason, ct);
return rejected is null ? EndpointResults.NotFound(CandidateNotFoundDetail) : Results.Ok(rejected); return Results.Ok(rejected);
} }
catch (DiscoveryValidationException exception) catch (DiscoveryValidationException exception)
{ {
+2 -1
View File
@@ -69,7 +69,8 @@ public static class JoinEndpoint
TenantId: result.TenantId, TenantId: result.TenantId,
Ip: ClientIp(context), Ip: ClientIp(context),
// Код инвайта — capability-токен: в аудит пишется только SHA-256-хэш (Security review). // Код инвайта — capability-токен: в аудит пишется только SHA-256-хэш (Security review).
DetailJson: AuditService.ToDetailJson(new { email = result.Login, codeHash = SessionTokens.HashToken(body.Code?.Trim() ?? string.Empty) })), ct); DetailJson: AuditService.ToDetailJson(
[AuditDetails.Set(AuditFields.Email, result.Login), AuditDetails.Set(AuditFields.CodeHash, SessionTokens.HashToken(body.Code?.Trim() ?? string.Empty))])), ct);
return Results.Ok(new { ok = true, login = result.Login }); return Results.Ok(new { ok = true, login = result.Login });
} }
+1 -8
View File
@@ -37,8 +37,6 @@ public static class MlEndpoints
private const string EnterTextDetail = "Введите текст"; private const string EnterTextDetail = "Введите текст";
private const string MessageNotFoundDetail = "Исходное сообщение не найдено";
/// <summary> /// <summary>
/// Регистрирует группу /api/ml /// Регистрирует группу /api/ml
/// </summary> /// </summary>
@@ -141,12 +139,7 @@ public static class MlEndpoints
} }
MlReviewService review = context.RequestServices.GetRequiredService<MlReviewService>(); MlReviewService review = context.RequestServices.GetRequiredService<MlReviewService>();
MlApplyResult? result = await review.ApplyAsync(body.DialogId, body.MsgId, body.Action, ct); MlApplyResult result = await review.ApplyAsync(body.DialogId, body.MsgId, body.Action, ct);
if (result is null)
{
return EndpointResults.NotFound(MessageNotFoundDetail);
}
if (result.Error is not null) if (result.Error is not null)
{ {
return EndpointResults.BadRequest(result.Error); return EndpointResults.BadRequest(result.Error);
@@ -1,5 +1,6 @@
using Deal.Api.Extensions; using Deal.Api.Extensions;
using Deal.Api.Services; using Deal.Api.Services;
using Deal.Modules.Tenants.Application.Abstractions;
using Deal.Modules.Tenants.Application.Models; using Deal.Modules.Tenants.Application.Models;
using Deal.Modules.Tenants.Application.Services; using Deal.Modules.Tenants.Application.Services;
@@ -132,6 +133,9 @@ public static class OperatorAnalyticsEndpoints
int? offset, int? offset,
HttpContext context, HttpContext context,
AnalyticsService analyticsService, AnalyticsService analyticsService,
ITenantRepository tenantRepository,
IAuthStore authStore,
IAuditReferenceResolver referenceResolver,
CancellationToken ct) CancellationToken ct)
{ {
if (context.GetCurrentOperator() is null) if (context.GetCurrentOperator() is null)
@@ -141,7 +145,8 @@ public static class OperatorAnalyticsEndpoints
AnalyticsActivityDto activity = await analyticsService.ActivityAsync( AnalyticsActivityDto activity = await analyticsService.ActivityAsync(
eventType, actorType, actorId, tenantId, from, to, limit, offset, ct); eventType, actorType, actorId, tenantId, from, to, limit, offset, ct);
return Results.Ok(activity); IReadOnlyList<AuditRecordViewDto> view = await AuditViewFactory.ProjectAsync(activity.Items, tenantRepository, authStore, referenceResolver, ct);
return Results.Ok(new { items = view, total = activity.Total, limit = activity.Limit, offset = activity.Offset });
} }
/// <summary> /// <summary>
@@ -1,5 +1,6 @@
using Deal.Api.Extensions; using Deal.Api.Extensions;
using Deal.Api.Services; using Deal.Api.Services;
using Deal.Modules.Tenants.Application.Abstractions;
using Deal.Modules.Tenants.Application.Models; using Deal.Modules.Tenants.Application.Models;
using Deal.Modules.Tenants.Application.Services; using Deal.Modules.Tenants.Application.Services;
@@ -39,6 +40,9 @@ public static class OperatorAuditEndpoints
int? offset, int? offset,
HttpContext context, HttpContext context,
AuditService auditService, AuditService auditService,
ITenantRepository tenantRepository,
IAuthStore authStore,
IAuditReferenceResolver referenceResolver,
CancellationToken ct) CancellationToken ct)
{ {
var operatorIdentity = context.GetCurrentOperator(); var operatorIdentity = context.GetCurrentOperator();
@@ -51,7 +55,8 @@ public static class OperatorAuditEndpoints
eventType, actorType, tenantId, from, to, NormalizeLimit(limit), actorId, NormalizeOffset(offset)); eventType, actorType, tenantId, from, to, NormalizeLimit(limit), actorId, NormalizeOffset(offset));
IReadOnlyList<AuditRecordDto> items = await auditService.QueryAsync(filter, ct); IReadOnlyList<AuditRecordDto> items = await auditService.QueryAsync(filter, ct);
int total = await auditService.CountAsync(filter, ct); int total = await auditService.CountAsync(filter, ct);
return Results.Ok(new { items, total }); IReadOnlyList<AuditRecordViewDto> view = await AuditViewFactory.ProjectAsync(items, tenantRepository, authStore, referenceResolver, ct);
return Results.Ok(new { items = view, total });
} }
/// <summary> /// <summary>
@@ -64,7 +64,7 @@ public static class OperatorAuthEndpoints
ActorId: null, ActorId: null,
TenantId: null, TenantId: null,
Ip: ClientIp(context), Ip: ClientIp(context),
DetailJson: AuditService.ToDetailJson(new { login = attemptedLogin })), ct); DetailJson: AuditService.ToDetailJson([AuditDetails.Set(AuditFields.Login, attemptedLogin)])), ct);
} }
return EndpointResults.Unauthorized(InvalidCredentialsDetail); return EndpointResults.Unauthorized(InvalidCredentialsDetail);
@@ -78,7 +78,7 @@ public static class OperatorAuthEndpoints
ActorId: result.OperatorId, ActorId: result.OperatorId,
TenantId: null, TenantId: null,
Ip: ClientIp(context), Ip: ClientIp(context),
DetailJson: AuditService.ToDetailJson(new { login = result.Login })), ct); DetailJson: AuditService.ToDetailJson([AuditDetails.Set(AuditFields.Login, result.Login)])), ct);
SetOperatorSessionCookie(context, cookieOptions.Value, result.Token); SetOperatorSessionCookie(context, cookieOptions.Value, result.Token);
return Results.Ok(new { ok = true, login = result.Login }); return Results.Ok(new { ok = true, login = result.Login });
@@ -100,7 +100,7 @@ public static class OperatorAuthEndpoints
if (operatorIdentity is not null) if (operatorIdentity is not null)
{ {
await AuditAppender.AppendOperatorAsync(context, AuditEvents.OperatorLogout, new { login = operatorIdentity.Login }, ct); await AuditAppender.AppendOperatorAsync(context, AuditEvents.OperatorLogout, [AuditDetails.Set(AuditFields.Login, operatorIdentity.Login)], ct);
} }
return Results.Ok(new { ok = true }); return Results.Ok(new { ok = true });
@@ -92,7 +92,8 @@ public static class OperatorInvitesEndpoints
Ip: ClientIp(context), Ip: ClientIp(context),
// Код инвайта — capability-токен (по нему активируется приглашение): в аудит пишется // Код инвайта — capability-токен (по нему активируется приглашение): в аудит пишется
// только его SHA-256-хэш, чтобы утечка ленты не давала рабочие коды (Security review). // только его SHA-256-хэш, чтобы утечка ленты не давала рабочие коды (Security review).
DetailJson: AuditService.ToDetailJson(new { email = result.Invite.Email, codeHash = SessionTokens.HashToken(result.Invite.Code) })), ct); DetailJson: AuditService.ToDetailJson(
[AuditDetails.Set(AuditFields.Email, result.Invite.Email), AuditDetails.Set(AuditFields.CodeHash, SessionTokens.HashToken(result.Invite.Code))])), ct);
InviteDto invite = result.Invite; InviteDto invite = result.Invite;
return Results.Ok(new { invite.Code, invite.Email, invite.TenantId, invite.ExpiresAt, invite.Status }); return Results.Ok(new { invite.Code, invite.Email, invite.TenantId, invite.ExpiresAt, invite.Status });
@@ -126,7 +127,8 @@ public static class OperatorInvitesEndpoints
ActorId: operatorIdentity.OperatorId, ActorId: operatorIdentity.OperatorId,
TenantId: null, TenantId: null,
Ip: ClientIp(context), Ip: ClientIp(context),
DetailJson: AuditService.ToDetailJson(new { email = result.Invite!.Email, codeHash = SessionTokens.HashToken(result.Invite.Code) })), ct); DetailJson: AuditService.ToDetailJson(
[AuditDetails.Set(AuditFields.Email, result.Invite!.Email), AuditDetails.Set(AuditFields.CodeHash, SessionTokens.HashToken(result.Invite.Code))])), ct);
return Results.Ok(new { ok = true }); return Results.Ok(new { ok = true });
} }
@@ -58,6 +58,7 @@ public static class OperatorLimitsEndpoints
private static async Task<IResult> ListSummaryAsync( private static async Task<IResult> ListSummaryAsync(
HttpContext context, HttpContext context,
ITenantRepository tenantRepository, ITenantRepository tenantRepository,
IAuthStore authStore,
ITenantLimitStore limitStore, ITenantLimitStore limitStore,
CancellationToken ct) CancellationToken ct)
{ {
@@ -68,6 +69,8 @@ public static class OperatorLimitsEndpoints
} }
IReadOnlyList<TenantRecordDto> tenants = await tenantRepository.ListAsync(ct); IReadOnlyList<TenantRecordDto> tenants = await tenantRepository.ListAsync(ct);
IReadOnlyDictionary<Guid, string> owners = await authStore.FindOwnerLoginsByTenantIdsAsync(
tenants.Select(tenant => tenant.Id).ToArray(), ct);
var items = new List<object>(tenants.Count); var items = new List<object>(tenants.Count);
foreach (TenantRecordDto tenant in tenants) foreach (TenantRecordDto tenant in tenants)
{ {
@@ -75,6 +78,7 @@ public static class OperatorLimitsEndpoints
items.Add(new items.Add(new
{ {
tenantId = tenant.Id, tenantId = tenant.Id,
ownerLogin = owners.GetValueOrDefault(tenant.Id),
name = tenant.Name, name = tenant.Name,
budget = state.BudgetTokens, budget = state.BudgetTokens,
period = state.Period, period = state.Period,
@@ -92,6 +96,7 @@ public static class OperatorLimitsEndpoints
Guid id, Guid id,
HttpContext context, HttpContext context,
ITenantRepository tenantRepository, ITenantRepository tenantRepository,
IAuthStore authStore,
ITenantLimitStore limitStore, ITenantLimitStore limitStore,
CancellationToken ct) CancellationToken ct)
{ {
@@ -108,7 +113,7 @@ public static class OperatorLimitsEndpoints
} }
BudgetStateDto state = await limitStore.GetStateAsync(id, ct); BudgetStateDto state = await limitStore.GetStateAsync(id, ct);
return Results.Ok(BuildDetailDto(tenant.Name, state)); return Results.Ok(BuildDetailDto(tenant.Name, await ResolveOwnerLoginAsync(authStore, id, ct), state));
} }
// PATCH /api/operator/tenants/{id}/limit: смена бюджета/периода (сброс флагов + аудит tenant_limit_changed). // PATCH /api/operator/tenants/{id}/limit: смена бюджета/периода (сброс флагов + аудит tenant_limit_changed).
@@ -117,6 +122,7 @@ public static class OperatorLimitsEndpoints
OperatorLimitUpdateRequest? body, OperatorLimitUpdateRequest? body,
HttpContext context, HttpContext context,
ITenantRepository tenantRepository, ITenantRepository tenantRepository,
IAuthStore authStore,
ITenantLimitStore limitStore, ITenantLimitStore limitStore,
AuditService auditService, AuditService auditService,
CancellationToken ct) CancellationToken ct)
@@ -154,10 +160,11 @@ public static class OperatorLimitsEndpoints
BudgetStateDto current = await limitStore.GetStateAsync(id, ct); BudgetStateDto current = await limitStore.GetStateAsync(id, ct);
long newBudget = body.Budget ?? current.BudgetTokens; long newBudget = body.Budget ?? current.BudgetTokens;
string newPeriod = body.Period ?? current.Period; string newPeriod = body.Period ?? current.Period;
string? ownerLogin = await ResolveOwnerLoginAsync(authStore, id, ct);
if (newBudget == current.BudgetTokens && newPeriod == current.Period) if (newBudget == current.BudgetTokens && newPeriod == current.Period)
{ {
// Идемпотентный повторный PATCH: без изменения хранилища и без дубля аудита. // Идемпотентный повторный PATCH: без изменения хранилища и без дубля аудита.
return Results.Ok(BuildDetailDto(tenant.Name, current)); return Results.Ok(BuildDetailDto(tenant.Name, ownerLogin, current));
} }
BudgetStateDto updated = await limitStore.UpdateBudgetAsync(id, newBudget, newPeriod, ct); BudgetStateDto updated = await limitStore.UpdateBudgetAsync(id, newBudget, newPeriod, ct);
@@ -167,16 +174,10 @@ public static class OperatorLimitsEndpoints
ActorId: operatorIdentity.OperatorId, ActorId: operatorIdentity.OperatorId,
TenantId: id, TenantId: id,
Ip: ClientIp(context), Ip: ClientIp(context),
DetailJson: AuditService.ToDetailJson(new DetailJson: AuditService.ToDetailJson(
{ [AuditDetails.Change(AuditFields.Budget, current.BudgetTokens, newBudget), AuditDetails.Change(AuditFields.Period, current.Period, newPeriod)])), ct);
tenantId = id,
oldBudget = current.BudgetTokens,
oldPeriod = current.Period,
budgetTokens = newBudget,
period = newPeriod,
})), ct);
return Results.Ok(BuildDetailDto(tenant.Name, updated)); return Results.Ok(BuildDetailDto(tenant.Name, ownerLogin, updated));
} }
/// <summary> /// <summary>
@@ -197,11 +198,13 @@ public static class OperatorLimitsEndpoints
// Форма деталей лимита тенанта (GET и ответ PATCH — единая). // Форма деталей лимита тенанта (GET и ответ PATCH — единая).
// name: Имя тенанта (реестр). // name: Имя тенанта (реестр).
// ownerLogin: Логин владельца пространства; null, если пользователей нет.
// state: Состояние бюджета (после ленивого reset). // state: Состояние бюджета (после ленивого reset).
// Возвращает: Объект ответа: лимит + расход + флаги порогов + статус тенанта. // Возвращает: Объект ответа: лимит + расход + флаги порогов + статус тенанта.
private static object BuildDetailDto(string name, BudgetStateDto state) => new private static object BuildDetailDto(string name, string? ownerLogin, BudgetStateDto state) => new
{ {
tenantId = state.TenantId, tenantId = state.TenantId,
ownerLogin,
name, name,
status = state.Status, status = state.Status,
allowed = state.Allowed, allowed = state.Allowed,
@@ -215,6 +218,17 @@ public static class OperatorLimitsEndpoints
notifiedExhausted = state.NotifiedExhausted, notifiedExhausted = state.NotifiedExhausted,
}; };
// Логин владельца пространства (первый пользователь по времени создания).
// authStore: Хранилище пользователей.
// tenantId: Идентификатор пространства.
// ct: Токен отмены.
// Возвращает: Логин либо null.
private static async Task<string?> ResolveOwnerLoginAsync(IAuthStore authStore, Guid tenantId, CancellationToken ct)
{
IReadOnlyDictionary<Guid, string> owners = await authStore.FindOwnerLoginsByTenantIdsAsync([tenantId], ct);
return owners.GetValueOrDefault(tenantId);
}
// IP-адрес клиента для аудита (без порта; null, если недоступен). // IP-адрес клиента для аудита (без порта; null, если недоступен).
// context: Контекст запроса. // context: Контекст запроса.
// Возвращает: Строковое представление IP или null. // Возвращает: Строковое представление IP или null.
@@ -128,7 +128,8 @@ public static class OperatorSettingsEndpoints
ActorId: operatorIdentity.OperatorId, ActorId: operatorIdentity.OperatorId,
TenantId: null, TenantId: null,
Ip: ClientIp(context), Ip: ClientIp(context),
DetailJson: AuditService.ToDetailJson(new { apiId = effectiveApiId, apiHashSet = true })), ct); DetailJson: AuditService.ToDetailJson(
[AuditDetails.Set(AuditFields.ApiId, effectiveApiId), AuditDetails.Set(AuditFields.ApiHashSet, true)])), ct);
TelegramKeysMaskedDto snapshot = await keys.GetMaskedAsync(ct); TelegramKeysMaskedDto snapshot = await keys.GetMaskedAsync(ct);
return Results.Ok(snapshot); return Results.Ok(snapshot);
@@ -113,7 +113,8 @@ public static class OperatorTenantsEndpoints
ActorId: operatorIdentity.OperatorId, ActorId: operatorIdentity.OperatorId,
TenantId: createdTenant.Id, TenantId: createdTenant.Id,
Ip: ClientIp(context), Ip: ClientIp(context),
DetailJson: AuditService.ToDetailJson(new { tenantId = createdTenant.Id, name = createdTenant.Name, email = result.OwnerLogin })), ct); DetailJson: AuditService.ToDetailJson(
[AuditDetails.Set(AuditFields.Name, createdTenant.Name), AuditDetails.Set(AuditFields.Email, result.OwnerLogin)])), ct);
if (result.OwnerLogin is not null) if (result.OwnerLogin is not null)
{ {
@@ -144,12 +145,7 @@ public static class OperatorTenantsEndpoints
return EndpointResults.Unauthorized(AuthHelpers.OperatorUnauthorizedDetail); return EndpointResults.Unauthorized(AuthHelpers.OperatorUnauthorizedDetail);
} }
TenantDetailDto? tenant = await tenantAdminService.GetAsync(id, ct); TenantDetailDto tenant = await tenantAdminService.GetAsync(id, ct);
if (tenant is null)
{
return EndpointResults.NotFound(TenantNotFoundDetail);
}
return Results.Ok(tenant); return Results.Ok(tenant);
} }
@@ -207,7 +203,8 @@ public static class OperatorTenantsEndpoints
ActorId: operatorIdentity.OperatorId, ActorId: operatorIdentity.OperatorId,
TenantId: result.Tenant.Id, TenantId: result.Tenant.Id,
Ip: ClientIp(context), Ip: ClientIp(context),
DetailJson: AuditService.ToDetailJson(new { tenantId = result.Tenant.Id, status = result.Tenant.Status })), ct); DetailJson: AuditService.ToDetailJson(
[AuditDetails.Change(AuditFields.Status, result.PreviousStatus, result.Tenant.Status)])), ct);
} }
return Results.Ok(new { ok = true, status = result.Tenant.Status }); return Results.Ok(new { ok = true, status = result.Tenant.Status });
@@ -247,7 +244,7 @@ public static class OperatorTenantsEndpoints
ActorId: operatorIdentity.OperatorId, ActorId: operatorIdentity.OperatorId,
TenantId: result.TenantId, TenantId: result.TenantId,
Ip: ClientIp(context), Ip: ClientIp(context),
DetailJson: AuditService.ToDetailJson(new { targetLogin = result.Login, tenantId = result.TenantId })), ct); DetailJson: AuditService.ToDetailJson([AuditDetails.Set(AuditFields.TargetLogin, result.Login)])), ct);
// Токен — это tenant-сессия (как после /api/auth/login): СТАВИМ ту же httpOnly-куку deal_session // Токен — это tenant-сессия (как после /api/auth/login): СТАВИМ ту же httpOnly-куку deal_session
// на ответ, чтобы браузер оператора сразу получил tenant-сессию (JS не может записать httpOnly-куку). // на ответ, чтобы браузер оператора сразу получил tenant-сессию (JS не может записать httpOnly-куку).
@@ -33,8 +33,6 @@ public static class PipelineEndpoints
// Путь возврата записи отсева в обработку (POST). // Путь возврата записи отсева в обработку (POST).
private const string RejectedReturnPath = "/rejected/{rejId}/return"; private const string RejectedReturnPath = "/rejected/{rejId}/return";
private const string RejectedNotFoundDetail = "Запись не найдена";
private const int DefaultPageSize = 100; private const int DefaultPageSize = 100;
/// <summary> /// <summary>
@@ -140,11 +138,7 @@ public static class PipelineEndpoints
} }
PipelineProcessingService processing = context.RequestServices.GetRequiredService<PipelineProcessingService>(); PipelineProcessingService processing = context.RequestServices.GetRequiredService<PipelineProcessingService>();
RejectReturnResultDto? result = await processing.ReturnAsync(rejId, body.Reason ?? string.Empty, ct); RejectReturnResultDto result = await processing.ReturnAsync(rejId, body.Reason ?? string.Empty, ct);
if (result is null)
{
return EndpointResults.NotFound(RejectedNotFoundDetail);
}
return result.Error is not null return result.Error is not null
? EndpointResults.BadRequest(result.Error) ? EndpointResults.BadRequest(result.Error)
@@ -5,6 +5,7 @@ using Deal.Modules.Settings.Application.Abstractions;
using Deal.Modules.Settings.Application.Models; using Deal.Modules.Settings.Application.Models;
using Deal.Modules.Settings.Application.Services; using Deal.Modules.Settings.Application.Services;
using Deal.Modules.Tenants.Application.Models; using Deal.Modules.Tenants.Application.Models;
using Deal.SharedKernel.Utilities;
namespace Deal.Api.Endpoints; namespace Deal.Api.Endpoints;
@@ -18,6 +19,15 @@ public static class SettingsEndpoints
private const string SettingsOpenApiTag = "settings"; private const string SettingsOpenApiTag = "settings";
private const string InvalidBodyDetail = "Тело запроса должно быть JSON-объектом"; private const string InvalidBodyDetail = "Тело запроса должно быть JSON-объектом";
// Максимальная длина значения в деталях аудита (промпты/списки бывают длинными).
private const int MaxAuditValueLength = 120;
// Пометка для составных настроек, чьи значения в аудит не пишутся (секреты, снимки состояния).
private const string ChangedMarker = "изменено";
// Опции сериализации снимка настроек в деталях аудита (camelCase, как на wire).
private static readonly JsonSerializerOptions AuditJsonOptions = new(JsonSerializerDefaults.Web);
/// <summary> /// <summary>
/// Регистрирует GET/PATCH /api/settings. /// Регистрирует GET/PATCH /api/settings.
/// </summary> /// </summary>
@@ -72,9 +82,10 @@ public static class SettingsEndpoints
} }
SettingsService settingsService = context.RequestServices.GetRequiredService<SettingsService>(); SettingsService settingsService = context.RequestServices.GetRequiredService<SettingsService>();
PublicSettingsDto before = await settingsService.GetPublicAsync(ct);
PublicSettingsDto result = await settingsService.ApplyPatchAsync(body, ct); PublicSettingsDto result = await settingsService.ApplyPatchAsync(body, ct);
await AuditAppender.AppendTenantAsync(context, AuditEvents.SettingsUpdated, new { fields = body.Keys }, ct); await AuditAppender.AppendTenantAsync(context, AuditEvents.SettingsUpdated, BuildSettingsChanges(before, result, body.Keys), ct);
if (ShouldScheduleRatesRefresh(body)) if (ShouldScheduleRatesRefresh(body))
{ {
@@ -84,6 +95,60 @@ public static class SettingsEndpoints
return Results.Ok(result); return Results.Ok(result);
} }
// Строит изменения настроек «поле: было → стало» по применённому PATCH.
// before: Снимок настроек до применения.
// after: Снимок настроек после применения.
// keys: Ключи PATCH-тела (изменённые).
// Возвращает: Изменения по каждому ключу (составные — пометкой «изменено»).
private static IReadOnlyList<AuditChangeDto> BuildSettingsChanges(
PublicSettingsDto before,
PublicSettingsDto after,
IEnumerable<string> keys)
{
using JsonDocument beforeDoc = JsonDocument.Parse(JsonSerializer.Serialize(before, AuditJsonOptions));
using JsonDocument afterDoc = JsonDocument.Parse(JsonSerializer.Serialize(after, AuditJsonOptions));
var changes = new List<AuditChangeDto>();
var keyList = new List<string>();
foreach (string key in keys)
{
keyList.Add(key);
SettingKind? kind = SettingsKeys.FindPublicKind(key);
if (kind is SettingKind.Dict or SettingKind.MyPrompts or SettingKind.AiConfigs)
{
changes.Add(AuditDetails.Set(key, ChangedMarker));
continue;
}
changes.Add(AuditDetails.Change(
key,
ReadSettingText(beforeDoc.RootElement, key),
ReadSettingText(afterDoc.RootElement, key)));
}
return changes.Count > 0
? changes
: [AuditDetails.Set(AuditFields.Fields, string.Join(", ", keyList))];
}
// Читает значение ключа снимка настроек как текст для деталей аудита.
private static string? ReadSettingText(JsonElement root, string key) =>
root.TryGetProperty(key, out JsonElement value) ? ToText(value) : null;
// Приводит значение JSON к строке отображения (длинные значения обрезаются).
private static string? ToText(JsonElement value) => value.ValueKind switch
{
JsonValueKind.Null or JsonValueKind.Undefined => null,
JsonValueKind.String => Truncate(value.GetString()),
JsonValueKind.True => BoolText.True,
JsonValueKind.False => BoolText.False,
JsonValueKind.Array => Truncate(string.Join(", ", value.EnumerateArray().Select(ToText))),
_ => Truncate(value.GetRawText()),
};
// Обрезает длинное значение деталей аудита.
private static string? Truncate(string? value) =>
value is not null && value.Length > MaxAuditValueLength ? value[..MaxAuditValueLength] + "…" : value;
private static bool ShouldScheduleRatesRefresh(Dictionary<string, JsonElement> body) private static bool ShouldScheduleRatesRefresh(Dictionary<string, JsonElement> body)
{ {
if (!body.TryGetValue(SettingsKeys.RateSource, out JsonElement element)) if (!body.TryGetValue(SettingsKeys.RateSource, out JsonElement element))
@@ -67,7 +67,12 @@ public static class TelegramEndpoints
private const string NotConnectedReason = "not-connected"; private const string NotConnectedReason = "not-connected";
private const string ReadyPhase = "ready"; private const string ReadyPhase = TelegramAuthPhases.Ready;
// Русские подписи видов диалогов для операторского UI.
private const string RussianChannelLabel = "канал";
private const string RussianGroupLabel = "группа";
private const string RussianChatLabel = "чат";
private const int PreviewDefaultLimit = 24; private const int PreviewDefaultLimit = 24;
@@ -170,7 +175,7 @@ public static class TelegramEndpoints
TelegramAuthResultDto result = await gateway.StartQrAsync(apiId, keys.ApiHash, ct); TelegramAuthResultDto result = await gateway.StartQrAsync(apiId, keys.ApiHash, ct);
if (result.Phase == ReadyPhase) if (result.Phase == ReadyPhase)
{ {
await AuditAppender.AppendTenantAsync(context, AuditEvents.TelegramLinked, new { phase = result.Phase }, ct); await AuditAppender.AppendTenantAsync(context, AuditEvents.TelegramLinked, [AuditDetails.Set(AuditFields.Phase, result.Phase)], ct);
} }
return Results.Ok(new { phase = result.Phase, qrUrl = result.QrUrl ?? string.Empty }); return Results.Ok(new { phase = result.Phase, qrUrl = result.QrUrl ?? string.Empty });
@@ -197,7 +202,7 @@ public static class TelegramEndpoints
string phase = await gateway.SendCodeAsync((body.Code ?? string.Empty).Trim(), ct); string phase = await gateway.SendCodeAsync((body.Code ?? string.Empty).Trim(), ct);
if (phase == ReadyPhase) if (phase == ReadyPhase)
{ {
await AuditAppender.AppendTenantAsync(context, AuditEvents.TelegramLinked, new { phase }, ct); await AuditAppender.AppendTenantAsync(context, AuditEvents.TelegramLinked, [AuditDetails.Set(AuditFields.Phase, phase)], ct);
} }
return Results.Ok(new { phase }); return Results.Ok(new { phase });
@@ -224,7 +229,7 @@ public static class TelegramEndpoints
string phase = await gateway.SendPasswordAsync(body.Password ?? string.Empty, ct); string phase = await gateway.SendPasswordAsync(body.Password ?? string.Empty, ct);
if (phase == ReadyPhase) if (phase == ReadyPhase)
{ {
await AuditAppender.AppendTenantAsync(context, AuditEvents.TelegramLinked, new { phase }, ct); await AuditAppender.AppendTenantAsync(context, AuditEvents.TelegramLinked, [AuditDetails.Set(AuditFields.Phase, phase)], ct);
} }
return Results.Ok(new { phase }); return Results.Ok(new { phase });
@@ -343,7 +348,7 @@ public static class TelegramEndpoints
if (body.Enabled) if (body.Enabled)
{ {
await AuditAppender.AppendTenantAsync(context, AuditEvents.ChannelEnabled, new { all = true, count = result.Count }, ct); await AuditAppender.AppendTenantAsync(context, AuditEvents.ChannelEnabled, [AuditDetails.Set(AuditFields.All, true), AuditDetails.Set(AuditFields.Count, result.Count)], ct);
} }
return Results.Ok(new { ok = true, count = result.Count, enabled = body.Enabled }); return Results.Ok(new { ok = true, count = result.Count, enabled = body.Enabled });
@@ -393,7 +398,7 @@ public static class TelegramEndpoints
if (result.Enabled) if (result.Enabled)
{ {
await AuditAppender.AppendTenantAsync(context, AuditEvents.ChannelEnabled, new { dialogId = dialog_id }, ct); await AuditAppender.AppendTenantAsync(context, AuditEvents.ChannelEnabled, [AuditDetails.Set(AuditFields.DialogId, dialog_id)], ct);
} }
return Results.Ok(new { ok = true, enabled = result.Enabled }); return Results.Ok(new { ok = true, enabled = result.Enabled });
@@ -482,9 +487,9 @@ public static class TelegramEndpoints
{ {
return kind switch return kind switch
{ {
"channel" => "канал", TelegramDialogKinds.Channel => RussianChannelLabel,
"group" or "forum" => "группа", TelegramDialogKinds.Group or TelegramDialogKinds.Forum => RussianGroupLabel,
"chat" => "чат", TelegramDialogKinds.Chat => RussianChatLabel,
_ => kind, _ => kind,
}; };
} }
+2
View File
@@ -165,6 +165,8 @@ builder.Services.AddSingleton<TelegramBackfillScheduler>();
builder.Services.AddScoped<AdminTickOrchestrator>(); builder.Services.AddScoped<AdminTickOrchestrator>();
builder.Services.AddScoped<IAuditReferenceResolver, AuditReferenceResolver>();
builder.Services.AddScoped<FtsMaintenance>(); builder.Services.AddScoped<FtsMaintenance>();
builder.Services.AddSingleton<SseBroker>(); builder.Services.AddSingleton<SseBroker>();
+15 -13
View File
@@ -13,12 +13,14 @@ public static class AuditAppender
/// <summary> /// <summary>
/// Пишет событие действия пользователя тенанта /// Пишет событие действия пользователя тенанта
/// </summary> /// </summary>
/// <param name="context">Контекст запроса (сессия пользователя, IP).</param>
/// <param name="eventType">Тип события — константа <see cref="AuditEvents"/>.</param> /// <param name="eventType">Тип события — константа <see cref="AuditEvents"/>.</param>
/// <param name="details">Минимальные детали события (обычно анонимный объект) или null.</param> /// <param name="changes">Изменения параметров события.</param>
/// <param name="ct">Токен отмены.</param>
public static async Task AppendTenantAsync( public static async Task AppendTenantAsync(
HttpContext context, HttpContext context,
string eventType, string eventType,
object? details, IReadOnlyList<AuditChangeDto> changes,
CancellationToken ct) CancellationToken ct)
{ {
CurrentUser? user = context.GetCurrentUser(); CurrentUser? user = context.GetCurrentUser();
@@ -35,19 +37,21 @@ public static class AuditAppender
ActorId: user.UserId, ActorId: user.UserId,
TenantId: user.TenantId, TenantId: user.TenantId,
Ip: ClientIp(context), Ip: ClientIp(context),
DetailJson: DetailJson(details)), DetailJson: DetailJson(changes)),
ct); ct);
} }
/// <summary> /// <summary>
/// Пишет событие действия оператора /// Пишет событие действия оператора
/// </summary> /// </summary>
/// <param name="context">Контекст запроса (операторская сессия, IP).</param>
/// <param name="eventType">Тип события — константа <see cref="AuditEvents"/>.</param> /// <param name="eventType">Тип события — константа <see cref="AuditEvents"/>.</param>
/// <param name="details">Минимальные детали события (обычно анонимный объект) или null.</param> /// <param name="changes">Изменения параметров события.</param>
/// <param name="ct">Токен отмены.</param>
public static async Task AppendOperatorAsync( public static async Task AppendOperatorAsync(
HttpContext context, HttpContext context,
string eventType, string eventType,
object? details, IReadOnlyList<AuditChangeDto> changes,
CancellationToken ct) CancellationToken ct)
{ {
CurrentOperator? operatorIdentity = context.GetCurrentOperator(); CurrentOperator? operatorIdentity = context.GetCurrentOperator();
@@ -64,18 +68,16 @@ public static class AuditAppender
ActorId: operatorIdentity.OperatorId, ActorId: operatorIdentity.OperatorId,
TenantId: null, TenantId: null,
Ip: ClientIp(context), Ip: ClientIp(context),
DetailJson: DetailJson(details)), DetailJson: DetailJson(changes)),
ct); ct);
} }
// Сериализует детали события (null — деталей нет). // Сериализует детали события (пустой список — деталей нет).
// details: Объект деталей или null. // changes: Изменения параметров события.
// Возвращает: JSON деталей (camelCase) или null. // Возвращает: JSON деталей или null.
private static string? DetailJson(object? details) => private static string? DetailJson(IReadOnlyList<AuditChangeDto> changes) =>
details is null ? null : AuditService.ToDetailJson(details); changes.Count == 0 ? null : AuditService.ToDetailJson(changes);
// IP-адрес клиента для аудита (без порта; null, если недоступен). // IP-адрес клиента для аудита (без порта; null, если недоступен).
// context: Контекст запроса.
// Возвращает: Строковое представление IP или null.
private static string? ClientIp(HttpContext context) => context.Connection.RemoteIpAddress?.ToString(); private static string? ClientIp(HttpContext context) => context.Connection.RemoteIpAddress?.ToString();
} }
@@ -0,0 +1,133 @@
using Deal.Modules.Cards.Application.Models;
using Deal.Modules.Kanban.Application.Abstractions;
using Deal.Modules.Kanban.Application.Models;
using Deal.Modules.Tenants.Application.Models;
using Deal.SharedKernel.Tenants.Abstractions;
using Deal.SharedKernel.Tenants.Models;
namespace Deal.Api.Services;
/// <summary>
/// Резолвер ссылок аудита на данные тенанта
/// </summary>
/// <param name="scopeFactory">Фабрика scope для чтения схемы тенанта.</param>
/// <param name="logger">Логгер сбоев разрешения ссылок.</param>
public sealed class AuditReferenceResolver(
IServiceScopeFactory scopeFactory,
ILogger<AuditReferenceResolver> logger) : IAuditReferenceResolver
{
async Task<IReadOnlyDictionary<string, string>> IAuditReferenceResolver.ResolveAsync(
IReadOnlyList<AuditRecordDto> records,
CancellationToken ct)
{
var names = new Dictionary<string, string>(StringComparer.Ordinal);
foreach (IGrouping<Guid, AuditRecordDto> group in GroupByTenant(records))
{
await ResolveTenantAsync(group.Key, group, names, ct);
}
return names;
}
private static IEnumerable<IGrouping<Guid, AuditRecordDto>> GroupByTenant(IReadOnlyList<AuditRecordDto> records) =>
records
.Where(record => record.TenantId is not null)
.GroupBy(record => record.TenantId!.Value);
private async Task ResolveTenantAsync(
Guid tenantId,
IEnumerable<AuditRecordDto> records,
Dictionary<string, string> names,
CancellationToken ct)
{
var cardIds = new HashSet<string>(StringComparer.Ordinal);
var boardIds = new HashSet<string>(StringComparer.Ordinal);
foreach (AuditRecordDto record in records)
{
foreach (AuditChangeDto change in record.AuditChanges())
{
Collect(change.From, cardIds, boardIds);
Collect(change.To, cardIds, boardIds);
}
}
if (cardIds.Count == 0 && boardIds.Count == 0)
{
return;
}
await using AsyncServiceScope scope = scopeFactory.CreateAsyncScope();
ITenantContext tenantContext = scope.ServiceProvider.GetRequiredService<ITenantContext>();
try
{
tenantContext.SetTenant(new TenantId(tenantId.ToString("N")));
ICardStore store = scope.ServiceProvider.GetRequiredService<ICardStore>();
await ResolveCardsAsync(store, cardIds, names, ct);
await ResolveContainersAsync(store, boardIds, names, ct);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception exception)
{
logger.LogWarning(exception, "Резолвер аудита: ссылки тенанта {TenantId} не разрешены", tenantId);
}
finally
{
tenantContext.Reset();
}
}
private static void Collect(
string? value,
HashSet<string> cardIds,
HashSet<string> boardIds)
{
if (string.IsNullOrEmpty(value))
{
return;
}
if (value.StartsWith(CardIds.CardPrefix, StringComparison.Ordinal))
{
cardIds.Add(value);
}
else if (value.StartsWith(KanbanIdPrefixes.Board, StringComparison.Ordinal))
{
boardIds.Add(value);
}
}
private static async Task ResolveCardsAsync(
ICardStore store,
HashSet<string> cardIds,
Dictionary<string, string> names,
CancellationToken ct)
{
foreach (string cardId in cardIds)
{
CardDto? card = await store.GetCardAsync(cardId, ct);
if (card is not null && !string.IsNullOrWhiteSpace(card.Title))
{
names[cardId] = card.Title;
}
}
}
private static async Task ResolveContainersAsync(
ICardStore store,
HashSet<string> boardIds,
Dictionary<string, string> names,
CancellationToken ct)
{
foreach (string boardId in boardIds)
{
ContainerDto? container = await store.GetContainerAsync(boardId, ct);
if (container is not null && !string.IsNullOrWhiteSpace(container.Name))
{
names[boardId] = container.Name;
}
}
}
}
@@ -0,0 +1,138 @@
using Deal.Modules.Tenants.Application.Abstractions;
using Deal.Modules.Tenants.Application.Models;
namespace Deal.Api.Services;
/// <summary>
/// Проекция записей аудита для операторской консоли
/// </summary>
public static class AuditViewFactory
{
/// <summary>
/// Дополняет записи аудита реальными пользователями и разбирает изменения деталей
/// </summary>
/// <param name="records">Записи аудита.</param>
/// <param name="tenantRepository">Реестр пространств для разрешения имён.</param>
/// <param name="authStore">Хранилище пользователей для разрешения логинов владельцев.</param>
/// <param name="referenceResolver">Резолвер ссылок на карточки и колонки.</param>
/// <param name="ct">Токен отмены.</param>
/// <returns>Записи для чтения оператором.</returns>
public static async Task<IReadOnlyList<AuditRecordViewDto>> ProjectAsync(
IReadOnlyList<AuditRecordDto> records,
ITenantRepository tenantRepository,
IAuthStore authStore,
IAuditReferenceResolver referenceResolver,
CancellationToken ct)
{
Guid[] tenantIds = DistinctTenantIds(records);
IReadOnlyDictionary<Guid, string> names = await ResolveNamesAsync(tenantIds, tenantRepository, ct);
IReadOnlyDictionary<Guid, string> owners = await ResolveOwnerLoginsAsync(tenantIds, authStore, ct);
IReadOnlyDictionary<string, string> references = await referenceResolver.ResolveAsync(records, ct);
var view = new List<AuditRecordViewDto>(records.Count);
foreach (AuditRecordDto record in records)
{
string? tenantName = record.TenantId is { } tenantId && names.TryGetValue(tenantId, out string? name)
? name
: null;
view.Add(new AuditRecordViewDto(
record.EventType,
record.ActorType,
record.ActorId,
record.TenantId,
ResolveUserName(record, owners),
tenantName,
record.Ip,
ResolveChanges(record, references),
record.DetailJson,
record.At,
record.Id));
}
return view;
}
// Заменяет идентификаторы ссылок изменения значениями из карты имён и убирает пустые изменения.
// record: Запись аудита.
// references: Имена по идентификаторам ссылок.
// Возвращает: Изменения с читаемыми значениями ссылок без пар «без изменения».
private static IReadOnlyList<AuditChangeDto> ResolveChanges(
AuditRecordDto record,
IReadOnlyDictionary<string, string> references)
{
IReadOnlyList<AuditChangeDto> changes = record.AuditChanges();
var resolved = new List<AuditChangeDto>(changes.Count);
foreach (AuditChangeDto change in changes)
{
string? from = ResolveReference(references, change.From);
string? to = ResolveReference(references, change.To);
if (IsUnchanged(from, to))
{
continue;
}
resolved.Add(change with { From = from, To = to });
}
return resolved;
}
// Сравнивает значения «было» и «стало».
// from: Значение до изменения.
// to: Значение после изменения.
// Возвращает: True — значения заданы и совпадают.
private static bool IsUnchanged(string? from, string? to) =>
from is not null && to is not null && string.Equals(from, to, StringComparison.Ordinal);
// Подставляет имя вместо идентификатора ссылки, если оно известно.
// references: Имена по идентификаторам ссылок.
// value: Значение изменения.
// Возвращает: Имя ссылки либо исходное значение.
private static string? ResolveReference(
IReadOnlyDictionary<string, string> references,
string? value) =>
value is not null && references.TryGetValue(value, out string? name) ? name : value;
// Логин реального пользователя: владелец пространства, иначе логин/email из деталей события.
// record: Запись аудита.
// owners: Логины владельцев по идентификаторам пространств.
// Возвращает: Логин либо null.
private static string? ResolveUserName(
AuditRecordDto record,
IReadOnlyDictionary<Guid, string> owners)
{
if (record.TenantId is { } tenantId && owners.TryGetValue(tenantId, out string? owner))
{
return owner;
}
return record.DetailValue(AuditFields.Login) ?? record.DetailValue(AuditFields.Email);
}
// Уникальные идентификаторы пространств выборки.
private static Guid[] DistinctTenantIds(IReadOnlyList<AuditRecordDto> records) =>
records
.Select(record => record.TenantId)
.Where(id => id is not null)
.Select(id => id!.Value)
.Distinct()
.ToArray();
// Разрешает имена пространств выборкой по уникальным идентификаторам.
private static async Task<IReadOnlyDictionary<Guid, string>> ResolveNamesAsync(
Guid[] tenantIds,
ITenantRepository tenantRepository,
CancellationToken ct) =>
tenantIds.Length == 0
? new Dictionary<Guid, string>()
: await tenantRepository.FindNamesByIdsAsync(tenantIds, ct);
// Разрешает логины владельцев пространств выборкой по уникальным идентификаторам.
private static async Task<IReadOnlyDictionary<Guid, string>> ResolveOwnerLoginsAsync(
Guid[] tenantIds,
IAuthStore authStore,
CancellationToken ct) =>
tenantIds.Length == 0
? new Dictionary<Guid, string>()
: await authStore.FindOwnerLoginsByTenantIdsAsync(tenantIds, ct);
}
@@ -0,0 +1,19 @@
using Deal.Modules.Tenants.Application.Models;
namespace Deal.Api.Services;
/// <summary>
/// Разрешает идентификаторы ссылок аудита в читаемые имена
/// </summary>
public interface IAuditReferenceResolver
{
/// <summary>
/// Разрешает ссылки на карточки и колонки в их заголовки и имена
/// </summary>
/// <param name="records">Записи аудита выборки.</param>
/// <param name="ct">Токен отмены.</param>
/// <returns>Имена по идентификаторам ссылок; неразрешённые идентификаторы отсутствуют.</returns>
public Task<IReadOnlyDictionary<string, string>> ResolveAsync(
IReadOnlyList<AuditRecordDto> records,
CancellationToken ct);
}
@@ -12,6 +12,10 @@ public sealed class TelegramBackfillScheduler(
IHostApplicationLifetime applicationLifetime, IHostApplicationLifetime applicationLifetime,
ILogger<TelegramBackfillScheduler> logger) ILogger<TelegramBackfillScheduler> logger)
{ {
// Имена фоновых задач (метки запуска).
private const string BackfillMonitoredJob = "backfill_monitored";
private const string FirstBackfillJob = "first_backfill";
// Флаг in-flight «Перечитать» всех каналов (Interlocked): повторный вызов не плодит // Флаг in-flight «Перечитать» всех каналов (Interlocked): повторный вызов не плодит
// параллельные полные перечитывания (Security review, как RatesRefreshScheduler). // параллельные полные перечитывания (Security review, как RatesRefreshScheduler).
private int _readRecentInProgress; private int _readRecentInProgress;
@@ -27,7 +31,7 @@ public sealed class TelegramBackfillScheduler(
return; return;
} }
RunBackground("backfill_monitored", RunReadRecentAsync); RunBackground(BackfillMonitoredJob, RunReadRecentAsync);
} }
/// <summary> /// <summary>
@@ -50,7 +54,7 @@ public sealed class TelegramBackfillScheduler(
return; return;
} }
RunBackground("first_backfill", (provider, runLogger, ct) => RunFirstBackfillsAsync(provider, runLogger, dialogIds, ct)); RunBackground(FirstBackfillJob, (provider, runLogger, ct) => RunFirstBackfillsAsync(provider, runLogger, dialogIds, ct));
} }
// Выполняет работу в собственном scope; любые ошибки — warning в лог (как RatesRefreshScheduler). // Выполняет работу в собственном scope; любые ошибки — warning в лог (как RatesRefreshScheduler).
@@ -21,6 +21,11 @@ public sealed class SourceIngressGrpcService(
IngressTenantResolver tenants, IngressTenantResolver tenants,
ILogger<SourceIngressGrpcService> logger) : SourceIngressService.SourceIngressServiceBase ILogger<SourceIngressGrpcService> logger) : SourceIngressService.SourceIngressServiceBase
{ {
// Исходы приёма источника для лога аудита.
private const string DuplicateOutcome = "duplicate";
private const string NoOpOutcome = "no-op";
private const string QueuedOutcome = "queued";
/// <summary> /// <summary>
/// PushSource — запись источника в очередь пайплайна тенанта. /// PushSource — запись источника в очередь пайплайна тенанта.
/// </summary> /// </summary>
@@ -57,7 +62,7 @@ public sealed class SourceIngressGrpcService(
tenant.Id, tenant.Id,
item.Source.Kind, item.Source.Kind,
item.Source.ExternalId ?? "-", item.Source.ExternalId ?? "-",
result.Duplicate ? "duplicate" : result.Id is null ? "no-op" : "queued"); result.Duplicate ? DuplicateOutcome : result.Id is null ? NoOpOutcome : QueuedOutcome);
return new PushSourceReply return new PushSourceReply
{ {
@@ -26,6 +26,15 @@ public sealed class TelegramKeysService(IGlobalSettingsStore store, ISecretCiphe
private const string EncryptedPrefix = "enc:"; private const string EncryptedPrefix = "enc:";
// Ключи JSON значения telegramKeys (camelCase, как пишет SaveAsync).
private const string ApiIdProperty = "apiId";
private const string ApiHashProperty = "apiHash";
// Геометрия маски секрета: короткий (≤8) → «x…»; иначе «1234…5678».
private const int MaskShortMaxLength = 8;
private const int MaskShortVisibleChars = 1;
private const int MaskEdgeVisibleChars = 4;
// Опции JSON значения telegramKeys: camelCase (как пишет SaveAsync) + терпимость регистра. // Опции JSON значения telegramKeys: camelCase (как пишет SaveAsync) + терпимость регистра.
private static readonly JsonSerializerOptions KeysJsonOptions = new() private static readonly JsonSerializerOptions KeysJsonOptions = new()
{ {
@@ -49,8 +58,8 @@ public sealed class TelegramKeysService(IGlobalSettingsStore store, ISecretCiphe
{ {
using JsonDocument document = JsonDocument.Parse(row.ValueJson); using JsonDocument document = JsonDocument.Parse(row.ValueJson);
JsonElement root = document.RootElement; JsonElement root = document.RootElement;
string apiId = ReadString(root, "apiId"); string apiId = ReadString(root, ApiIdProperty);
string apiHash = ReadString(root, "apiHash"); string apiHash = ReadString(root, ApiHashProperty);
return new TgKeysSnapshot(apiId, cipher.Decrypt(apiHash)); return new TgKeysSnapshot(apiId, cipher.Decrypt(apiHash));
} }
catch (JsonException) catch (JsonException)
@@ -143,8 +152,11 @@ public sealed class TelegramKeysService(IGlobalSettingsStore store, ISecretCiphe
return value.Length switch return value.Length switch
{ {
0 => string.Empty, 0 => string.Empty,
<= 8 => string.Concat(value.AsSpan(0, 1), MaskEllipsis), <= MaskShortMaxLength => string.Concat(value.AsSpan(0, MaskShortVisibleChars), MaskEllipsis),
_ => string.Concat(value.AsSpan(0, 4), MaskEllipsis, value.AsSpan(value.Length - 4)), _ => string.Concat(
value.AsSpan(0, MaskEdgeVisibleChars),
MaskEllipsis,
value.AsSpan(value.Length - MaskEdgeVisibleChars)),
}; };
} }
} }
@@ -22,6 +22,7 @@ public sealed class TgStatusService(
TelegramKeysService keys) TelegramKeysService keys)
{ {
private const string IdlePhase = "idle"; private const string IdlePhase = "idle";
private const string ReadyPhase = "ready";
// Опции JSON KV-значений статуса: camelCase (как пишет ингресс) + терпимость регистра. // Опции JSON KV-значений статуса: camelCase (как пишет ингресс) + терпимость регистра.
private static readonly JsonSerializerOptions KvJsonOptions = new() private static readonly JsonSerializerOptions KvJsonOptions = new()
@@ -37,13 +38,19 @@ public sealed class TgStatusService(
public async Task<TgStatusDto> GetAsync(CancellationToken ct) public async Task<TgStatusDto> GetAsync(CancellationToken ct)
{ {
TelegramAccountStatusDto live = await ReadLiveAsync(ct).ConfigureAwait(false); TelegramAccountStatusDto live = await ReadLiveAsync(ct).ConfigureAwait(false);
string account = await ReadAccountAsync(ct).ConfigureAwait(false); // «Подключён» для UI = авторизован (phase ready). Транспортный connected сервиса
// означает лишь живость соединения и не гарантирует вход — в UI он даёт «зависание».
bool authorized = string.Equals(live.Phase, ReadyPhase, StringComparison.Ordinal);
// Живой account (имя из Telegram) приоритетнее KV: при QR-входе KV ещё не заполнен.
string account = string.IsNullOrEmpty(live.Account)
? await ReadAccountAsync(ct).ConfigureAwait(false)
: live.Account;
int monitored = (await dialogs.ListMonitoredIdsAsync(ct).ConfigureAwait(false)).Count; int monitored = (await dialogs.ListMonitoredIdsAsync(ct).ConfigureAwait(false)).Count;
TgKeysSnapshot snapshot = await keys.GetAsync(ct).ConfigureAwait(false); TgKeysSnapshot snapshot = await keys.GetAsync(ct).ConfigureAwait(false);
return new TgStatusDto( return new TgStatusDto(
Phase: live.Phase, Phase: live.Phase,
Connected: live.Connected, Connected: authorized,
Listener: live.Listener, Listener: live.Listener,
Account: account, Account: account,
Monitored: monitored, Monitored: monitored,
@@ -0,0 +1,42 @@
namespace Deal.Contracts.Integrations.Models;
/// <summary>
/// Типы контактов заказчика
/// </summary>
public static class CardContactTypes
{
/// <summary>
/// Telegram (@username)
/// </summary>
public const string Telegram = "tg";
/// <summary>
/// Телефон
/// </summary>
public const string Phone = "phone";
/// <summary>
/// Email
/// </summary>
public const string Email = "email";
/// <summary>
/// WhatsApp
/// </summary>
public const string WhatsApp = "whatsapp";
/// <summary>
/// LinkedIn
/// </summary>
public const string LinkedIn = "linkedin";
/// <summary>
/// Сайт
/// </summary>
public const string Site = "site";
/// <summary>
/// Прочее (не опознано)
/// </summary>
public const string Other = "other";
}
@@ -0,0 +1,37 @@
namespace Deal.Contracts.Integrations.Models;
/// <summary>
/// Фазы входа в аккаунт Telegram
/// </summary>
public static class TelegramAuthPhases
{
/// <summary>
/// Аккаунт не подключён
/// </summary>
public const string Idle = "idle";
/// <summary>
/// Ожидание номера телефона
/// </summary>
public const string Phone = "phone";
/// <summary>
/// Ожидание кода
/// </summary>
public const string Code = "code";
/// <summary>
/// Ожидание облачного пароля
/// </summary>
public const string Password = "password";
/// <summary>
/// Ожидание сканирования QR
/// </summary>
public const string Qr = "qr";
/// <summary>
/// Аккаунт авторизован
/// </summary>
public const string Ready = "ready";
}
@@ -0,0 +1,27 @@
namespace Deal.Contracts.Integrations.Models;
/// <summary>
/// Виды диалогов Telegram
/// </summary>
public static class TelegramDialogKinds
{
/// <summary>
/// Канал
/// </summary>
public const string Channel = "channel";
/// <summary>
/// Группа
/// </summary>
public const string Group = "group";
/// <summary>
/// Форум (темы внутри группы)
/// </summary>
public const string Forum = "forum";
/// <summary>
/// Личный чат
/// </summary>
public const string Chat = "chat";
}
@@ -0,0 +1,102 @@
namespace Deal.Contracts.Integrations.Models;
/// <summary>
/// Имена операций Telegram-гейта для логов и диагностики
/// </summary>
public static class TelegramOperations
{
/// <summary>
/// Статус аккаунта
/// </summary>
public const string Status = "status";
/// <summary>
/// Вход по номеру телефона
/// </summary>
public const string StartPhone = "start_phone";
/// <summary>
/// Вход по QR
/// </summary>
public const string StartQr = "start_qr";
/// <summary>
/// Подтверждение кода
/// </summary>
public const string SendCode = "send_code";
/// <summary>
/// Подтверждение облачного пароля
/// </summary>
public const string SendPassword = "send_password";
/// <summary>
/// Выход из аккаунта
/// </summary>
public const string Logout = "logout";
/// <summary>
/// Обновление каталога диалогов
/// </summary>
public const string RefreshDialogs = "refresh_dialogs";
/// <summary>
/// Включение/выключение мониторинга диалога
/// </summary>
public const string SetMonitor = "set_monitor";
/// <summary>
/// Включение/выключение мониторинга всех диалогов
/// </summary>
public const string SetMonitorAll = "set_monitor_all";
/// <summary>
/// Перечитать последние сообщения
/// </summary>
public const string Backfill = "backfill";
/// <summary>
/// Чтение последних сообщений
/// </summary>
public const string ReadRecent = "read_recent";
/// <summary>
/// Чтение исходного сообщения
/// </summary>
public const string ReadSource = "read_source";
/// <summary>
/// Поиск источников
/// </summary>
public const string Search = "search";
/// <summary>
/// Информация об источнике
/// </summary>
public const string GetInfo = "get_info";
/// <summary>
/// Чтение сообщений для оценки
/// </summary>
public const string ReadForEval = "read_for_eval";
/// <summary>
/// Вступление в источник
/// </summary>
public const string Join = "join";
/// <summary>
/// Выход из источника
/// </summary>
public const string Leave = "leave";
/// <summary>
/// Информация об источнике (Discovery)
/// </summary>
public const string DiscoveryInfo = "discovery_info";
/// <summary>
/// Чтение сообщений источника (Discovery)
/// </summary>
public const string DiscoveryRead = "discovery_read";
}
@@ -3,22 +3,17 @@ using Deal.SharedKernel.Tenants.Models;
namespace Deal.Infrastructure.Data; namespace Deal.Infrastructure.Data;
/// <summary>
/// Контекст тенанта на AsyncLocal
/// </summary>
public sealed class TenantContext : ITenantContext public sealed class TenantContext : ITenantContext
{ {
private static readonly AsyncLocal<TenantId?> Current = new(); private static readonly AsyncLocal<TenantId?> Current = new();
public TenantId? TenantId => Current.Value; TenantId? ITenantContext.TenantId => Current.Value;
public bool HasTenant => Current.Value is not null; bool ITenantContext.HasTenant => Current.Value is not null;
public string? SchemaName => Current.Value?.SchemaName; string? ITenantContext.SchemaName => Current.Value?.SchemaName;
/// <inheritdoc />
void ITenantContext.SetTenant(TenantId tenantId) => Current.Value = tenantId; void ITenantContext.SetTenant(TenantId tenantId) => Current.Value = tenantId;
/// <inheritdoc />
void ITenantContext.Reset() => Current.Value = null; void ITenantContext.Reset() => Current.Value = null;
} }
@@ -6,6 +6,9 @@ namespace Deal.Infrastructure.Integrations.Extensions;
// Расширения Uri для SSRF-гейта интеграций // Расширения Uri для SSRF-гейта интеграций
internal static class UriExtensions internal static class UriExtensions
{ {
// Имя loopback-хоста.
private const string LocalhostHost = "localhost";
/// <summary> /// <summary>
/// Проверяет, указывает ли URL на приватный/loopback/link-local адрес /// Проверяет, указывает ли URL на приватный/loopback/link-local адрес
/// </summary> /// </summary>
@@ -14,7 +17,7 @@ internal static class UriExtensions
public static bool IsPrivateEndpoint(this Uri uri) public static bool IsPrivateEndpoint(this Uri uri)
{ {
string host = uri.Host; string host = uri.Host;
if (string.Equals(host, "localhost", StringComparison.OrdinalIgnoreCase)) if (string.Equals(host, LocalhostHost, StringComparison.OrdinalIgnoreCase))
{ {
return true; return true;
} }
@@ -1,5 +1,7 @@
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Deal.SharedKernel.Utilities;
namespace Deal.Infrastructure.Integrations.Options; namespace Deal.Infrastructure.Integrations.Options;
/// <summary> /// <summary>
@@ -86,13 +88,16 @@ public sealed class MtlsOptions
}; };
} }
// Строковое представление включённого флага числом.
private const string OneLiteral = "1";
/// <summary> /// <summary>
/// Разбирает значение флага DEAL_MTLS_ENABLED /// Разбирает значение флага DEAL_MTLS_ENABLED
/// </summary> /// </summary>
/// <param name="rawValue">Сырое значение env (null/пусто — выключено).</param> /// <param name="rawValue">Сырое значение env (null/пусто — выключено).</param>
public static bool IsEnabled(string? rawValue) public static bool IsEnabled(string? rawValue)
=> string.Equals(rawValue, "1", StringComparison.Ordinal) => string.Equals(rawValue, OneLiteral, StringComparison.Ordinal)
|| string.Equals(rawValue, "true", StringComparison.OrdinalIgnoreCase); || string.Equals(rawValue, BoolText.True, StringComparison.OrdinalIgnoreCase);
// Обрезает путь конфигурации (env-значения с пробелами/кавычками не передаются в файловые API). // Обрезает путь конфигурации (env-значения с пробелами/кавычками не передаются в файловые API).
// rawValue: Сырое значение env. // rawValue: Сырое значение env.
@@ -6,9 +6,6 @@ using Deal.Modules.Settings.Application.Models;
namespace Deal.Infrastructure.Integrations.Services; namespace Deal.Infrastructure.Integrations.Services;
/// <summary>
/// HTTP-реализация проверки подключения к AI-провайдеру.
/// </summary>
public sealed class AiConnectionChecker : IAiConnectionChecker public sealed class AiConnectionChecker : IAiConnectionChecker
{ {
/// <summary> /// <summary>
@@ -78,7 +75,6 @@ public sealed class AiConnectionChecker : IAiConnectionChecker
_httpClient = httpClient; _httpClient = httpClient;
} }
/// <inheritdoc />
async Task<AiCheckResultDto> IAiConnectionChecker.CheckAsync(AiCheckRequest request, CancellationToken ct) async Task<AiCheckResultDto> IAiConnectionChecker.CheckAsync(AiCheckRequest request, CancellationToken ct)
{ {
ArgumentNullException.ThrowIfNull(request); ArgumentNullException.ThrowIfNull(request);
@@ -9,9 +9,6 @@ using Microsoft.Extensions.Logging;
namespace Deal.Infrastructure.Integrations.Services; namespace Deal.Infrastructure.Integrations.Services;
/// <summary>
/// Декоратор бюджетного гейта порта <see cref="IAiClassifier"/>
/// </summary>
public sealed class BudgetedAiClassifier : IAiClassifier public sealed class BudgetedAiClassifier : IAiClassifier
{ {
// Текст ошибки вызова вне tenant-контекста (гейт читает лимиты по тенанту). // Текст ошибки вызова вне tenant-контекста (гейт читает лимиты по тенанту).
@@ -54,7 +51,6 @@ public sealed class BudgetedAiClassifier : IAiClassifier
_logger = logger; _logger = logger;
} }
/// <inheritdoc />
async Task<AiFilterResultDto> IAiClassifier.FilterAsync(string text, CancellationToken ct) async Task<AiFilterResultDto> IAiClassifier.FilterAsync(string text, CancellationToken ct)
{ {
if (await IsPaidAllowedAsync(ct)) if (await IsPaidAllowedAsync(ct))
@@ -67,7 +63,6 @@ public sealed class BudgetedAiClassifier : IAiClassifier
return await _localClassifier.FilterAsync(text, ct); return await _localClassifier.FilterAsync(text, ct);
} }
/// <inheritdoc />
async Task<AiParsedCardDto> IAiClassifier.ClassifyAsync(string text, CancellationToken ct) async Task<AiParsedCardDto> IAiClassifier.ClassifyAsync(string text, CancellationToken ct)
{ {
if (await IsPaidAllowedAsync(ct)) if (await IsPaidAllowedAsync(ct))
@@ -9,9 +9,6 @@ using Microsoft.Extensions.Logging;
namespace Deal.Infrastructure.Integrations.Services; namespace Deal.Infrastructure.Integrations.Services;
/// <summary>
/// Декоратор бюджетного гейта порта <see cref="IAiTools"/>
/// </summary>
public sealed class BudgetedAiTools : IAiTools public sealed class BudgetedAiTools : IAiTools
{ {
private const string ExhaustedKeywordsError = "ИИ-бюджет исчерпан — генерация ключевых слов недоступна"; private const string ExhaustedKeywordsError = "ИИ-бюджет исчерпан — генерация ключевых слов недоступна";
@@ -50,7 +47,6 @@ public sealed class BudgetedAiTools : IAiTools
_logger = logger; _logger = logger;
} }
/// <inheritdoc />
async Task<AiGenerateKeywordsResultDto> IAiTools.GenerateKeywordsAsync(string description, CancellationToken ct) async Task<AiGenerateKeywordsResultDto> IAiTools.GenerateKeywordsAsync(string description, CancellationToken ct)
{ {
BudgetStateDto state = await GateStateAsync(ct); BudgetStateDto state = await GateStateAsync(ct);
@@ -69,7 +65,6 @@ public sealed class BudgetedAiTools : IAiTools
Error: state.Status == TenantStatuses.Suspended ? SuspendedKeywordsError : ExhaustedKeywordsError); Error: state.Status == TenantStatuses.Suspended ? SuspendedKeywordsError : ExhaustedKeywordsError);
} }
/// <inheritdoc />
async Task<AiEvaluateFitResultDto> IAiTools.EvaluateFitAsync( async Task<AiEvaluateFitResultDto> IAiTools.EvaluateFitAsync(
string text, string text,
string description, string description,
@@ -5,9 +5,6 @@ using Microsoft.Extensions.Logging;
namespace Deal.Infrastructure.Integrations.Services; namespace Deal.Infrastructure.Integrations.Services;
/// <summary>
/// HTTP-источник курсов ЦБ РФ
/// </summary>
public sealed class CbrRateSource : IRatesSource public sealed class CbrRateSource : IRatesSource
{ {
/// <summary> /// <summary>
@@ -47,7 +44,6 @@ public sealed class CbrRateSource : IRatesSource
_logger = logger; _logger = logger;
} }
/// <inheritdoc />
async Task<Dictionary<string, double>?> IRatesSource.FetchAsync(CancellationToken ct) async Task<Dictionary<string, double>?> IRatesSource.FetchAsync(CancellationToken ct)
{ {
try try
@@ -11,9 +11,6 @@ using Microsoft.Extensions.Logging;
namespace Deal.Infrastructure.Integrations.Services; namespace Deal.Infrastructure.Integrations.Services;
/// <summary>
/// gRPC-адаптер порта <see cref="IAiClassifier"/> к автономному ai-service.
/// </summary>
public sealed class GrpcAiClassifier : IAiClassifier public sealed class GrpcAiClassifier : IAiClassifier
{ {
/// <summary> /// <summary>
@@ -70,7 +67,6 @@ public sealed class GrpcAiClassifier : IAiClassifier
_logger = logger; _logger = logger;
} }
/// <inheritdoc />
async Task<AiFilterResultDto> IAiClassifier.FilterAsync(string text, CancellationToken ct) async Task<AiFilterResultDto> IAiClassifier.FilterAsync(string text, CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -106,7 +102,6 @@ public sealed class GrpcAiClassifier : IAiClassifier
} }
} }
/// <inheritdoc />
async Task<AiParsedCardDto> IAiClassifier.ClassifyAsync(string text, CancellationToken ct) async Task<AiParsedCardDto> IAiClassifier.ClassifyAsync(string text, CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -10,9 +10,6 @@ using Microsoft.Extensions.Logging;
namespace Deal.Infrastructure.Integrations.Services; namespace Deal.Infrastructure.Integrations.Services;
/// <summary>
/// gRPC-адаптер порта <see cref="IAiTools"/> к автономному ai-service.
/// </summary>
public sealed class GrpcAiTools : IAiTools public sealed class GrpcAiTools : IAiTools
{ {
/// <summary> /// <summary>
@@ -70,7 +67,6 @@ public sealed class GrpcAiTools : IAiTools
_logger = logger; _logger = logger;
} }
/// <inheritdoc />
async Task<AiGenerateKeywordsResultDto> IAiTools.GenerateKeywordsAsync(string description, CancellationToken ct) async Task<AiGenerateKeywordsResultDto> IAiTools.GenerateKeywordsAsync(string description, CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -104,7 +100,6 @@ public sealed class GrpcAiTools : IAiTools
} }
} }
/// <inheritdoc />
async Task<AiEvaluateFitResultDto> IAiTools.EvaluateFitAsync( async Task<AiEvaluateFitResultDto> IAiTools.EvaluateFitAsync(
string text, string text,
string description, string description,
@@ -16,9 +16,6 @@ using Microsoft.Extensions.Logging;
namespace Deal.Infrastructure.Integrations.Services; namespace Deal.Infrastructure.Integrations.Services;
/// <summary>
/// gRPC-адаптер порта IMlClient к автономному ml-service.
/// </summary>
public sealed class GrpcMlClient : IMlClient, IMlTrainClient public sealed class GrpcMlClient : IMlClient, IMlTrainClient
{ {
/// <summary> /// <summary>
@@ -97,7 +94,6 @@ public sealed class GrpcMlClient : IMlClient, IMlTrainClient
_logger = logger; _logger = logger;
} }
/// <inheritdoc />
async Task<MlStatusResponseDto> IMlClient.StatusAsync(CancellationToken ct) async Task<MlStatusResponseDto> IMlClient.StatusAsync(CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -122,7 +118,6 @@ public sealed class GrpcMlClient : IMlClient, IMlTrainClient
return new MlStatusResponseDto(Enabled: enabled, Service: snapshot.Service, Reachable: snapshot.Reachable, Stats: stats); return new MlStatusResponseDto(Enabled: enabled, Service: snapshot.Service, Reachable: snapshot.Reachable, Stats: stats);
} }
/// <inheritdoc />
async Task<MlPredictResultDto> IMlClient.PredictAsync(string text, CancellationToken ct) async Task<MlPredictResultDto> IMlClient.PredictAsync(string text, CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -143,7 +138,6 @@ public sealed class GrpcMlClient : IMlClient, IMlTrainClient
} }
} }
/// <inheritdoc />
async Task<MlResetResultDto> IMlClient.ResetAsync(CancellationToken ct) async Task<MlResetResultDto> IMlClient.ResetAsync(CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -171,7 +165,6 @@ public sealed class GrpcMlClient : IMlClient, IMlTrainClient
return new MlResetResultDto(Ok: true, Error: null); return new MlResetResultDto(Ok: true, Error: null);
} }
/// <inheritdoc />
async Task IMlClient.PushAsync( async Task IMlClient.PushAsync(
string text, string text,
string label, string label,
@@ -181,7 +174,6 @@ public sealed class GrpcMlClient : IMlClient, IMlTrainClient
await MlOutboxQueue.PushAsync(_learningStore, text, label, delta, ct); await MlOutboxQueue.PushAsync(_learningStore, text, label, delta, ct);
} }
/// <inheritdoc />
async Task<int> IMlTrainClient.TrainBatchAsync(IReadOnlyList<MlOutboxEntryDto> items, CancellationToken ct) async Task<int> IMlTrainClient.TrainBatchAsync(IReadOnlyList<MlOutboxEntryDto> items, CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -9,9 +9,6 @@ using Microsoft.Extensions.Logging;
namespace Deal.Infrastructure.Integrations.Services; namespace Deal.Infrastructure.Integrations.Services;
/// <summary>
/// gRPC-адаптер порта <see cref="ITelegramGateway"/> к автономному telegram-service.
/// </summary>
public sealed class GrpcTelegramClient : ITelegramGateway public sealed class GrpcTelegramClient : ITelegramGateway
{ {
/// <summary> /// <summary>
@@ -58,7 +55,6 @@ public sealed class GrpcTelegramClient : ITelegramGateway
_logger = logger; _logger = logger;
} }
/// <inheritdoc />
async Task<TelegramAccountStatusDto> ITelegramGateway.StatusAsync(CancellationToken ct) async Task<TelegramAccountStatusDto> ITelegramGateway.StatusAsync(CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -77,11 +73,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "status"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.Status);
} }
} }
/// <inheritdoc />
async Task<TelegramAuthResultDto> ITelegramGateway.StartPhoneAsync( async Task<TelegramAuthResultDto> ITelegramGateway.StartPhoneAsync(
string phone, string phone,
int apiId, int apiId,
@@ -100,11 +95,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "start_phone"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.StartPhone);
} }
} }
/// <inheritdoc />
async Task<TelegramAuthResultDto> ITelegramGateway.StartQrAsync( async Task<TelegramAuthResultDto> ITelegramGateway.StartQrAsync(
int apiId, int apiId,
string apiHash, string apiHash,
@@ -124,11 +118,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "start_qr"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.StartQr);
} }
} }
/// <inheritdoc />
async Task<string> ITelegramGateway.SendCodeAsync(string code, CancellationToken ct) async Task<string> ITelegramGateway.SendCodeAsync(string code, CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -142,11 +135,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "send_code"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.SendCode);
} }
} }
/// <inheritdoc />
async Task<string> ITelegramGateway.SendPasswordAsync(string password, CancellationToken ct) async Task<string> ITelegramGateway.SendPasswordAsync(string password, CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -160,11 +152,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "send_password"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.SendPassword);
} }
} }
/// <inheritdoc />
async Task ITelegramGateway.LogoutAsync(CancellationToken ct) async Task ITelegramGateway.LogoutAsync(CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -176,11 +167,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "logout"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.Logout);
} }
} }
/// <inheritdoc />
async Task<IReadOnlyList<TelegramDialogEntryDto>> ITelegramGateway.RefreshDialogsAsync(CancellationToken ct) async Task<IReadOnlyList<TelegramDialogEntryDto>> ITelegramGateway.RefreshDialogsAsync(CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -193,11 +183,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "refresh_dialogs"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.RefreshDialogs);
} }
} }
/// <inheritdoc />
async Task ITelegramGateway.SetMonitorAsync( async Task ITelegramGateway.SetMonitorAsync(
string dialogId, string dialogId,
bool enabled, bool enabled,
@@ -213,11 +202,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "set_monitor"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.SetMonitor);
} }
} }
/// <inheritdoc />
async Task ITelegramGateway.SetMonitorAllAsync(bool enabled, CancellationToken ct) async Task ITelegramGateway.SetMonitorAllAsync(bool enabled, CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -230,11 +218,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "set_monitor_all"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.SetMonitorAll);
} }
} }
/// <inheritdoc />
async Task<int> ITelegramGateway.BackfillAsync( async Task<int> ITelegramGateway.BackfillAsync(
string dialogId, string dialogId,
bool force, bool force,
@@ -251,11 +238,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "backfill"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.Backfill);
} }
} }
/// <inheritdoc />
async Task<IReadOnlyList<TelegramRecentMessageDto>> ITelegramGateway.ReadRecentAsync( async Task<IReadOnlyList<TelegramRecentMessageDto>> ITelegramGateway.ReadRecentAsync(
string dialogId, string dialogId,
int limit, int limit,
@@ -274,11 +260,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "read_recent"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.ReadRecent);
} }
} }
/// <inheritdoc />
async Task<TelegramSourceContentDto> ITelegramGateway.ReadSourceAsync( async Task<TelegramSourceContentDto> ITelegramGateway.ReadSourceAsync(
string dialogId, string dialogId,
long msgId, long msgId,
@@ -298,11 +283,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "read_source"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.ReadSource);
} }
} }
/// <inheritdoc />
async Task<IReadOnlyList<TelegramDialogEntryDto>> ITelegramGateway.SearchAsync( async Task<IReadOnlyList<TelegramDialogEntryDto>> ITelegramGateway.SearchAsync(
string query, string query,
int limit, int limit,
@@ -319,11 +303,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "search"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.Search);
} }
} }
/// <inheritdoc />
async Task<TelegramChannelInfoDto> ITelegramGateway.InfoAsync(string dialogId, CancellationToken ct) async Task<TelegramChannelInfoDto> ITelegramGateway.InfoAsync(string dialogId, CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -345,11 +328,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "get_info"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.GetInfo);
} }
} }
/// <inheritdoc />
async Task<TelegramEvalReadDto> ITelegramGateway.ReadForEvalAsync( async Task<TelegramEvalReadDto> ITelegramGateway.ReadForEvalAsync(
string dialogId, string dialogId,
int limit, int limit,
@@ -376,11 +358,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "read_for_eval"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.ReadForEval);
} }
} }
/// <inheritdoc />
async Task ITelegramGateway.JoinAsync(string username, CancellationToken ct) async Task ITelegramGateway.JoinAsync(string username, CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -393,11 +374,10 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "join"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.Join);
} }
} }
/// <inheritdoc />
async Task ITelegramGateway.LeaveAsync(string dialogId, CancellationToken ct) async Task ITelegramGateway.LeaveAsync(string dialogId, CancellationToken ct)
{ {
TenantId tenantId = RequireTenant(); TenantId tenantId = RequireTenant();
@@ -410,7 +390,7 @@ public sealed class GrpcTelegramClient : ITelegramGateway
} }
catch (Exception exception) catch (Exception exception)
{ {
throw TranslateTransportFailure(exception, tenantId, "leave"); throw TranslateTransportFailure(exception, tenantId, TelegramOperations.Leave);
} }
} }
@@ -3,20 +3,15 @@ using Deal.Contracts.Integrations.Models;
namespace Deal.Infrastructure.Integrations.Services; namespace Deal.Infrastructure.Integrations.Services;
/// <summary>
/// Локальная реализация <see cref="IAiTools"/> без внешнего ИИ-сервиса.
/// </summary>
public sealed class LocalAiTools : IAiTools public sealed class LocalAiTools : IAiTools
{ {
// Сообщение исключения методов (локальный режим = ai-service не подключён). // Сообщение исключения методов (локальный режим = ai-service не подключён).
private const string NotSupportedMessage = private const string NotSupportedMessage =
"ИИ-инструменты доступны только при подключённом ai-service (Services:Ai:UseLocal=false)."; "ИИ-инструменты доступны только при подключённом ai-service (Services:Ai:UseLocal=false).";
/// <inheritdoc />
Task<AiGenerateKeywordsResultDto> IAiTools.GenerateKeywordsAsync(string description, CancellationToken ct) Task<AiGenerateKeywordsResultDto> IAiTools.GenerateKeywordsAsync(string description, CancellationToken ct)
=> throw new NotSupportedException(NotSupportedMessage); => throw new NotSupportedException(NotSupportedMessage);
/// <inheritdoc />
Task<AiEvaluateFitResultDto> IAiTools.EvaluateFitAsync( Task<AiEvaluateFitResultDto> IAiTools.EvaluateFitAsync(
string text, string text,
string description, string description,
@@ -39,8 +39,6 @@ ContainersService containersService) : IColumnSuggester
private const string KeywordsEmptyReason = "ИИ не смог выделить ключи — попробуйте ещё раз"; private const string KeywordsEmptyReason = "ИИ не смог выделить ключи — попробуйте ещё раз";
private const string RulesModeAny = "any";
/// <inheritdoc /> /// <inheritdoc />
public async Task<SuggestColumnsResultDto> SuggestColumnsAsync(CancellationToken ct) public async Task<SuggestColumnsResultDto> SuggestColumnsAsync(CancellationToken ct)
{ {
@@ -121,7 +119,7 @@ ContainersService containersService) : IColumnSuggester
foreach (SuggestedColumnPlan plan in plans) foreach (SuggestedColumnPlan plan in plans)
{ {
var rules = new ContainerRulesDto( var rules = new ContainerRulesDto(
Mode: RulesModeAny, Mode: ColumnRuleModes.Any,
Direction: Array.Empty<string>(), Direction: Array.Empty<string>(),
Keywords: [plan.Word], Keywords: [plan.Word],
Stack: Array.Empty<string>(), Stack: Array.Empty<string>(),
@@ -3,21 +3,18 @@ using Deal.Contracts.Integrations.Models;
namespace Deal.Infrastructure.Integrations.Services; namespace Deal.Infrastructure.Integrations.Services;
/// <summary>
/// Локальная заглушка <see cref="ITelegramGateway"/> без telegram-service.
/// </summary>
public sealed class LocalTelegramGateway : ITelegramGateway public sealed class LocalTelegramGateway : ITelegramGateway
{ {
// Причина пустого чтения: сообщений в источнике нет.
private const string NoHistoryReason = "no_history";
// Фаза idle-формы (аккаунт не подключён — сервиса нет). // Фаза idle-формы (аккаунт не подключён — сервиса нет).
private const string IdlePhase = "idle"; private const string IdlePhase = "idle";
/// <inheritdoc />
Task<TelegramAccountStatusDto> ITelegramGateway.StatusAsync(CancellationToken ct) Task<TelegramAccountStatusDto> ITelegramGateway.StatusAsync(CancellationToken ct)
{ {
return Task.FromResult(new TelegramAccountStatusDto(IdlePhase, false, false, string.Empty, null, null)); return Task.FromResult(new TelegramAccountStatusDto(IdlePhase, false, false, string.Empty, null, null));
} }
/// <inheritdoc />
Task<TelegramAuthResultDto> ITelegramGateway.StartPhoneAsync( Task<TelegramAuthResultDto> ITelegramGateway.StartPhoneAsync(
string phone, string phone,
int apiId, int apiId,
@@ -25,76 +22,61 @@ public sealed class LocalTelegramGateway : ITelegramGateway
CancellationToken ct) CancellationToken ct)
=> Task.FromResult(new TelegramAuthResultDto(IdlePhase, null)); => Task.FromResult(new TelegramAuthResultDto(IdlePhase, null));
/// <inheritdoc />
Task<TelegramAuthResultDto> ITelegramGateway.StartQrAsync( Task<TelegramAuthResultDto> ITelegramGateway.StartQrAsync(
int apiId, int apiId,
string apiHash, string apiHash,
CancellationToken ct) CancellationToken ct)
=> Task.FromResult(new TelegramAuthResultDto(IdlePhase, null)); => Task.FromResult(new TelegramAuthResultDto(IdlePhase, null));
/// <inheritdoc /> Task<string> ITelegramGateway.SendCodeAsync(string code, CancellationToken ct) => Task.FromResult(IdlePhase);
public Task<string> SendCodeAsync(string code, CancellationToken ct) => Task.FromResult(IdlePhase);
/// <inheritdoc /> Task<string> ITelegramGateway.SendPasswordAsync(string password, CancellationToken ct) => Task.FromResult(IdlePhase);
public Task<string> SendPasswordAsync(string password, CancellationToken ct) => Task.FromResult(IdlePhase);
/// <inheritdoc />
Task ITelegramGateway.LogoutAsync(CancellationToken ct) => Task.CompletedTask; Task ITelegramGateway.LogoutAsync(CancellationToken ct) => Task.CompletedTask;
/// <inheritdoc />
Task<IReadOnlyList<TelegramDialogEntryDto>> ITelegramGateway.RefreshDialogsAsync(CancellationToken ct) Task<IReadOnlyList<TelegramDialogEntryDto>> ITelegramGateway.RefreshDialogsAsync(CancellationToken ct)
=> Task.FromResult<IReadOnlyList<TelegramDialogEntryDto>>([]); => Task.FromResult<IReadOnlyList<TelegramDialogEntryDto>>([]);
/// <inheritdoc />
Task ITelegramGateway.SetMonitorAsync( Task ITelegramGateway.SetMonitorAsync(
string dialogId, string dialogId,
bool enabled, bool enabled,
CancellationToken ct) => Task.CompletedTask; CancellationToken ct) => Task.CompletedTask;
/// <inheritdoc />
Task ITelegramGateway.SetMonitorAllAsync(bool enabled, CancellationToken ct) => Task.CompletedTask; Task ITelegramGateway.SetMonitorAllAsync(bool enabled, CancellationToken ct) => Task.CompletedTask;
/// <inheritdoc /> Task<int> ITelegramGateway.BackfillAsync(
public Task<int> BackfillAsync(
string dialogId, string dialogId,
bool force, bool force,
CancellationToken ct) => Task.FromResult(0); CancellationToken ct) => Task.FromResult(0);
/// <inheritdoc />
Task<IReadOnlyList<TelegramRecentMessageDto>> ITelegramGateway.ReadRecentAsync( Task<IReadOnlyList<TelegramRecentMessageDto>> ITelegramGateway.ReadRecentAsync(
string dialogId, string dialogId,
int limit, int limit,
CancellationToken ct) CancellationToken ct)
=> Task.FromResult<IReadOnlyList<TelegramRecentMessageDto>>([]); => Task.FromResult<IReadOnlyList<TelegramRecentMessageDto>>([]);
/// <inheritdoc />
Task<TelegramSourceContentDto> ITelegramGateway.ReadSourceAsync( Task<TelegramSourceContentDto> ITelegramGateway.ReadSourceAsync(
string dialogId, string dialogId,
long msgId, long msgId,
CancellationToken ct) CancellationToken ct)
=> Task.FromResult(new TelegramSourceContentDto(false, null, null)); => Task.FromResult(new TelegramSourceContentDto(false, null, null));
/// <inheritdoc />
Task<IReadOnlyList<TelegramDialogEntryDto>> ITelegramGateway.SearchAsync( Task<IReadOnlyList<TelegramDialogEntryDto>> ITelegramGateway.SearchAsync(
string query, string query,
int limit, int limit,
CancellationToken ct) CancellationToken ct)
=> Task.FromResult<IReadOnlyList<TelegramDialogEntryDto>>([]); => Task.FromResult<IReadOnlyList<TelegramDialogEntryDto>>([]);
/// <inheritdoc />
Task<TelegramChannelInfoDto> ITelegramGateway.InfoAsync(string dialogId, CancellationToken ct) Task<TelegramChannelInfoDto> ITelegramGateway.InfoAsync(string dialogId, CancellationToken ct)
=> Task.FromResult(new TelegramChannelInfoDto(dialogId, string.Empty, string.Empty, string.Empty, SourceDefaults.DefaultHue, null, false)); => Task.FromResult(new TelegramChannelInfoDto(dialogId, string.Empty, string.Empty, string.Empty, SourceDefaults.DefaultHue, null, false));
/// <inheritdoc />
Task<TelegramEvalReadDto> ITelegramGateway.ReadForEvalAsync( Task<TelegramEvalReadDto> ITelegramGateway.ReadForEvalAsync(
string dialogId, string dialogId,
int limit, int limit,
CancellationToken ct) CancellationToken ct)
=> Task.FromResult(new TelegramEvalReadDto(false, "no_history", [])); => Task.FromResult(new TelegramEvalReadDto(false, NoHistoryReason, []));
/// <inheritdoc />
Task ITelegramGateway.JoinAsync(string username, CancellationToken ct) => Task.CompletedTask; Task ITelegramGateway.JoinAsync(string username, CancellationToken ct) => Task.CompletedTask;
/// <inheritdoc />
Task ITelegramGateway.LeaveAsync(string dialogId, CancellationToken ct) => Task.CompletedTask; Task ITelegramGateway.LeaveAsync(string dialogId, CancellationToken ct) => Task.CompletedTask;
} }
@@ -1,8 +1,13 @@
using Deal.SharedKernel.Utilities;
namespace Deal.Infrastructure.Integrations.Storage.Extensions; namespace Deal.Infrastructure.Integrations.Storage.Extensions;
// Расширения string для разбора конфигурационных значений. // Расширения string для разбора конфигурационных значений.
internal static class StringExtensions internal static class StringExtensions
{ {
// Строковое представление включённого флага числом.
private const string OneLiteral = "1";
/// <summary> /// <summary>
/// Разбирает строковое значение как булев флаг конфигурации /// Разбирает строковое значение как булев флаг конфигурации
/// </summary> /// </summary>
@@ -10,6 +15,6 @@ internal static class StringExtensions
/// <returns>True — значение распознано как включённое.</returns> /// <returns>True — значение распознано как включённое.</returns>
public static bool IsTrue(this string raw) public static bool IsTrue(this string raw)
{ {
return string.Equals(raw, "true", StringComparison.OrdinalIgnoreCase) || raw == "1"; return string.Equals(raw, BoolText.True, StringComparison.OrdinalIgnoreCase) || raw == OneLiteral;
} }
} }
@@ -3,9 +3,6 @@ using Deal.Contracts.Integrations.Models;
namespace Deal.Infrastructure.Integrations.Storage.Services; namespace Deal.Infrastructure.Integrations.Storage.Services;
/// <summary>
/// Локальное файловое хранилище вложений — каталог на диске.
/// </summary>
public sealed class LocalFileStorage : IFileStorage public sealed class LocalFileStorage : IFileStorage
{ {
// Размер буфера чтения при скачивании (async FileStream). // Размер буфера чтения при скачивании (async FileStream).
@@ -31,7 +28,6 @@ public sealed class LocalFileStorage : IFileStorage
/// <returns>Строка вида <c>LocalFileStorage (root: …)</c>.</returns> /// <returns>Строка вида <c>LocalFileStorage (root: …)</c>.</returns>
public override string ToString() => $"LocalFileStorage (root: {_rootPath})"; public override string ToString() => $"LocalFileStorage (root: {_rootPath})";
/// <inheritdoc />
async Task<string> IFileStorage.PutAsync( async Task<string> IFileStorage.PutAsync(
string objectKey, string objectKey,
Stream content, Stream content,
@@ -55,7 +51,6 @@ public sealed class LocalFileStorage : IFileStorage
return objectKey; return objectKey;
} }
/// <inheritdoc />
Task<Stream?> IFileStorage.GetAsync(string objectKey, CancellationToken ct) Task<Stream?> IFileStorage.GetAsync(string objectKey, CancellationToken ct)
{ {
string path = ResolvePath(objectKey); string path = ResolvePath(objectKey);
@@ -68,7 +63,6 @@ public sealed class LocalFileStorage : IFileStorage
return Task.FromResult<Stream?>(stream); return Task.FromResult<Stream?>(stream);
} }
/// <inheritdoc />
Task<FileMeta?> IFileStorage.StatAsync(string objectKey, CancellationToken ct) Task<FileMeta?> IFileStorage.StatAsync(string objectKey, CancellationToken ct)
{ {
string path = ResolvePath(objectKey); string path = ResolvePath(objectKey);
@@ -81,7 +75,6 @@ public sealed class LocalFileStorage : IFileStorage
return Task.FromResult<FileMeta?>(new FileMeta(objectKey, info.Length, string.Empty)); return Task.FromResult<FileMeta?>(new FileMeta(objectKey, info.Length, string.Empty));
} }
/// <inheritdoc />
Task IFileStorage.DeleteAsync(string objectKey, CancellationToken ct) Task IFileStorage.DeleteAsync(string objectKey, CancellationToken ct)
{ {
string path = ResolvePath(objectKey); string path = ResolvePath(objectKey);
@@ -9,9 +9,6 @@ using Minio.Exceptions;
namespace Deal.Infrastructure.Integrations.Storage.Services; namespace Deal.Infrastructure.Integrations.Storage.Services;
/// <summary>
/// Хранилище вложений на MinIO
/// </summary>
public sealed class MinioFileStorage : IFileStorage public sealed class MinioFileStorage : IFileStorage
{ {
private const string DefaultContentType = "application/octet-stream"; private const string DefaultContentType = "application/octet-stream";
@@ -66,7 +63,6 @@ public sealed class MinioFileStorage : IFileStorage
/// <returns>Строка вида <c>MinioFileStorage (endpoint: …; bucket: …)</c>.</returns> /// <returns>Строка вида <c>MinioFileStorage (endpoint: …; bucket: …)</c>.</returns>
public override string ToString() => $"MinioFileStorage (endpoint: {_endpoint}; bucket: {_bucket})"; public override string ToString() => $"MinioFileStorage (endpoint: {_endpoint}; bucket: {_bucket})";
/// <inheritdoc />
async Task<string> IFileStorage.PutAsync( async Task<string> IFileStorage.PutAsync(
string objectKey, string objectKey,
Stream content, Stream content,
@@ -98,7 +94,6 @@ public sealed class MinioFileStorage : IFileStorage
return objectKey; return objectKey;
} }
/// <inheritdoc />
async Task<Stream?> IFileStorage.GetAsync(string objectKey, CancellationToken ct) async Task<Stream?> IFileStorage.GetAsync(string objectKey, CancellationToken ct)
{ {
MemoryStream buffer = new(); MemoryStream buffer = new();
@@ -128,7 +123,6 @@ public sealed class MinioFileStorage : IFileStorage
return buffer; return buffer;
} }
/// <inheritdoc />
async Task<FileMeta?> IFileStorage.StatAsync(string objectKey, CancellationToken ct) async Task<FileMeta?> IFileStorage.StatAsync(string objectKey, CancellationToken ct)
{ {
try try
@@ -144,7 +138,6 @@ public sealed class MinioFileStorage : IFileStorage
} }
} }
/// <inheritdoc />
async Task IFileStorage.DeleteAsync(string objectKey, CancellationToken ct) async Task IFileStorage.DeleteAsync(string objectKey, CancellationToken ct)
{ {
try try
@@ -1,3 +1,4 @@
using Deal.Modules.Cards.Application.Models;
using NpgsqlTypes; using NpgsqlTypes;
namespace Deal.Infrastructure.Persistence.Entities; namespace Deal.Infrastructure.Persistence.Entities;
@@ -130,7 +131,7 @@ public sealed class CardEntity
/// <summary> /// <summary>
/// Предыдущая колонка /// Предыдущая колонка
/// </summary> /// </summary>
public string PrevCol { get; set; } = "inbox"; public string PrevCol { get; set; } = CardIds.Inbox;
/// <summary> /// <summary>
/// Время помещения в архив /// Время помещения в архив
@@ -1,3 +1,4 @@
using Deal.Modules.Kanban.Application.Models;
using NpgsqlTypes; using NpgsqlTypes;
namespace Deal.Infrastructure.Persistence.Entities; namespace Deal.Infrastructure.Persistence.Entities;
@@ -35,12 +36,12 @@ public sealed class ContainerEntity
/// <summary> /// <summary>
/// Вид контейнера: board /// Вид контейнера: board
/// </summary> /// </summary>
public string Kind { get; set; } = "board"; public string Kind { get; set; } = ContainerKinds.Board;
/// <summary> /// <summary>
/// Пространство: dashboard | selected /// Пространство: dashboard | selected
/// </summary> /// </summary>
public string Space { get; set; } = "dashboard"; public string Space { get; set; } = ContainerSpaces.Dashboard;
/// <summary> /// <summary>
/// Свёрнутость колонки на дашборде /// Свёрнутость колонки на дашборде
@@ -1,3 +1,5 @@
using Deal.Modules.Discovery.Application.Models;
namespace Deal.Infrastructure.Persistence.Entities; namespace Deal.Infrastructure.Persistence.Entities;
/// <summary> /// <summary>
@@ -28,7 +30,7 @@ public sealed class DiscCandidateEntity
/// <summary> /// <summary>
/// Тип источника: channel|group|forum. /// Тип источника: channel|group|forum.
/// </summary> /// </summary>
public string Kind { get; set; } = "channel"; public string Kind { get; set; } = DiscoveryCandidateKinds.Channel;
/// <summary> /// <summary>
/// Цвет источника из палитры DIALOG_HUES /// Цвет источника из палитры DIALOG_HUES
@@ -63,7 +65,7 @@ public sealed class DiscCandidateEntity
/// <summary> /// <summary>
/// Статус кандидата /// Статус кандидата
/// </summary> /// </summary>
public string Status { get; set; } = "new"; public string Status { get; set; } = DiscoveryCandidateStatuses.New;
/// <summary> /// <summary>
/// Вступили автоматически /// Вступили автоматически
@@ -1,3 +1,5 @@
using Deal.Modules.Discovery.Application.Models;
namespace Deal.Infrastructure.Persistence.Entities; namespace Deal.Infrastructure.Persistence.Entities;
/// <summary> /// <summary>
@@ -55,7 +57,7 @@ public sealed class DiscTaskEntity
/// <summary> /// <summary>
/// Статус задачи: draft|running|paused|done|failed. /// Статус задачи: draft|running|paused|done|failed.
/// </summary> /// </summary>
public string Status { get; set; } = "draft"; public string Status { get; set; } = DiscoveryTaskStatuses.Draft;
/// <summary> /// <summary>
/// Индекс текущего ключа поиска /// Индекс текущего ключа поиска
@@ -1,3 +1,5 @@
using Deal.Modules.Tenants.Application.Models;
namespace Deal.Infrastructure.Persistence.Entities; namespace Deal.Infrastructure.Persistence.Entities;
/// <summary> /// <summary>
@@ -20,7 +22,7 @@ public sealed class InviteEntity
/// </summary> /// </summary>
public Guid? TenantId { get; set; } public Guid? TenantId { get; set; }
public string Status { get; set; } = "pending"; public string Status { get; set; } = InviteStatuses.Pending;
public DateTimeOffset ExpiresAt { get; set; } public DateTimeOffset ExpiresAt { get; set; }
@@ -1,3 +1,5 @@
using Deal.Modules.Tenants.Application.Models;
namespace Deal.Infrastructure.Persistence.Entities; namespace Deal.Infrastructure.Persistence.Entities;
/// <summary> /// <summary>
@@ -14,7 +16,7 @@ public sealed class OperatorEntity
public string PasswordHash { get; set; } = string.Empty; public string PasswordHash { get; set; } = string.Empty;
public string Status { get; set; } = "active"; public string Status { get; set; } = TenantStatuses.Active;
public DateTimeOffset CreatedAt { get; set; } public DateTimeOffset CreatedAt { get; set; }
} }
@@ -1,3 +1,5 @@
using Deal.Modules.Pipeline.Application.Models;
namespace Deal.Infrastructure.Persistence.Entities; namespace Deal.Infrastructure.Persistence.Entities;
/// <summary> /// <summary>
@@ -38,7 +40,7 @@ public sealed class QueueItemEntity
/// <summary> /// <summary>
/// Статус строки: <c>new</c> — ждёт разбора воркером, <c>filtered</c> — прошла фильтры и ждёт ИИ/ML. /// Статус строки: <c>new</c> — ждёт разбора воркером, <c>filtered</c> — прошла фильтры и ждёт ИИ/ML.
/// </summary> /// </summary>
public string Status { get; set; } = "new"; public string Status { get; set; } = PipelineQueueStatuses.New;
/// <summary> /// <summary>
/// Признак возврата из отсева /// Признак возврата из отсева
@@ -1,5 +1,7 @@
using NpgsqlTypes; using NpgsqlTypes;
using Deal.Modules.Pipeline.Application.Models;
namespace Deal.Infrastructure.Persistence.Entities; namespace Deal.Infrastructure.Persistence.Entities;
/// <summary> /// <summary>
@@ -50,7 +52,7 @@ public sealed class RejectedItemEntity
/// <summary> /// <summary>
/// Кто вынес решение /// Кто вынес решение
/// </summary> /// </summary>
public string Source { get; set; } = "stop"; public string Source { get; set; } = PipelineRejectSources.Rules;
/// <summary> /// <summary>
/// Время получения исходного сообщения /// Время получения исходного сообщения
@@ -1,3 +1,5 @@
using Deal.Modules.Tenants.Application.Models;
namespace Deal.Infrastructure.Persistence.Entities; namespace Deal.Infrastructure.Persistence.Entities;
/// <summary> /// <summary>
@@ -9,7 +11,7 @@ public sealed class TenantEntity
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
public string Status { get; set; } = "active"; public string Status { get; set; } = TenantStatuses.Active;
public DateTimeOffset CreatedAt { get; set; } public DateTimeOffset CreatedAt { get; set; }
} }
@@ -1,3 +1,5 @@
using Deal.Modules.Tenants.Application.Models;
namespace Deal.Infrastructure.Persistence.Entities; namespace Deal.Infrastructure.Persistence.Entities;
/// <summary> /// <summary>
@@ -18,7 +20,7 @@ public sealed class TenantLimitEntity
/// <summary> /// <summary>
/// Тип периода: month|day. /// Тип периода: month|day.
/// </summary> /// </summary>
public string Period { get; set; } = "month"; public string Period { get; set; } = TenantLimitPeriods.Month;
/// <summary> /// <summary>
/// Начало текущего периода /// Начало текущего периода
@@ -1,3 +1,5 @@
using Deal.Modules.Tenants.Application.Models;
namespace Deal.Infrastructure.Persistence.Entities; namespace Deal.Infrastructure.Persistence.Entities;
/// <summary> /// <summary>
@@ -16,7 +18,7 @@ public sealed class UserEntity
public string PasswordHash { get; set; } = string.Empty; public string PasswordHash { get; set; } = string.Empty;
public string Status { get; set; } = "active"; public string Status { get; set; } = TenantStatuses.Active;
public DateTimeOffset CreatedAt { get; set; } public DateTimeOffset CreatedAt { get; set; }
} }
@@ -67,6 +67,32 @@ public sealed class AuthStore(DealDbContext dbContext) : IAuthStore
return entities; return entities;
} }
public async Task<IReadOnlyDictionary<Guid, string>> FindOwnerLoginsByTenantIdsAsync(
IReadOnlyCollection<Guid> tenantIds,
CancellationToken ct)
{
if (tenantIds.Count == 0)
{
return new Dictionary<Guid, string>();
}
var users = await dbContext.Users
.AsNoTracking()
.Where(u => tenantIds.Contains(u.TenantId))
.OrderBy(u => u.CreatedAt)
.ThenBy(u => u.Login)
.Select(u => new { u.TenantId, u.Login })
.ToListAsync(ct);
var owners = new Dictionary<Guid, string>();
foreach (var user in users)
{
owners.TryAdd(user.TenantId, user.Login);
}
return owners;
}
/// <inheritdoc /> /// <inheritdoc />
public async Task CreateSessionAsync(SessionDto session, CancellationToken ct) public async Task CreateSessionAsync(SessionDto session, CancellationToken ct)
{ {
@@ -59,7 +59,7 @@ public sealed partial class DiscoveryStore
Username = row.Username, Username = row.Username,
Kind = row.Kind, Kind = row.Kind,
Hue = row.Hue, Hue = row.Hue,
Status = "new", Status = DiscoveryCandidateStatuses.New,
CreatedAt = now, CreatedAt = now,
UpdatedAt = now, UpdatedAt = now,
}); });
@@ -123,7 +123,7 @@ public sealed partial class DiscoveryStore
return false; return false;
} }
row.Status = "joined"; row.Status = DiscoveryCandidateStatuses.Joined;
row.AutoJoined = autoJoined; row.AutoJoined = autoJoined;
row.UpdatedAt = DateTimeOffset.UtcNow; row.UpdatedAt = DateTimeOffset.UtcNow;
await _dbContext.SaveChangesAsync(ct); await _dbContext.SaveChangesAsync(ct);
@@ -135,7 +135,7 @@ public sealed partial class DiscoveryStore
{ {
DiscCandidateEntity? row = await _dbContext.DiscCandidates DiscCandidateEntity? row = await _dbContext.DiscCandidates
.FirstOrDefaultAsync(candidate => candidate.DialogId == dialogId, ct); .FirstOrDefaultAsync(candidate => candidate.DialogId == dialogId, ct);
if (row is null || row.Status != "review") if (row is null || row.Status != DiscoveryCandidateStatuses.Review)
{ {
return null; return null;
} }
@@ -156,7 +156,7 @@ public sealed partial class DiscoveryStore
return false; return false;
} }
row.Status = "rejected"; row.Status = DiscoveryCandidateStatuses.Rejected;
row.UpdatedAt = DateTimeOffset.UtcNow; row.UpdatedAt = DateTimeOffset.UtcNow;
await _dbContext.SaveChangesAsync(ct); await _dbContext.SaveChangesAsync(ct);
return true; return true;
@@ -45,7 +45,7 @@ public sealed partial class DiscoveryStore
SampleSize = row.SampleSize, SampleSize = row.SampleSize,
PlanJoins = row.PlanJoins, PlanJoins = row.PlanJoins,
AutoJoin = row.AutoJoin, AutoJoin = row.AutoJoin,
Status = "draft", Status = DiscoveryTaskStatuses.Draft,
CreatedAt = now, CreatedAt = now,
UpdatedAt = now, UpdatedAt = now,
}); });
@@ -101,7 +101,7 @@ public sealed partial class DiscoveryStore
return false; return false;
} }
row.Status = "running"; row.Status = DiscoveryTaskStatuses.Running;
if (resetProgress) if (resetProgress)
{ {
row.SearchIdx = 0; row.SearchIdx = 0;
@@ -127,7 +127,7 @@ public sealed partial class DiscoveryStore
return false; return false;
} }
row.Status = "paused"; row.Status = DiscoveryTaskStatuses.Paused;
row.UpdatedAt = DateTimeOffset.UtcNow; row.UpdatedAt = DateTimeOffset.UtcNow;
await _dbContext.SaveChangesAsync(ct); await _dbContext.SaveChangesAsync(ct);
return true; return true;
@@ -143,7 +143,7 @@ public sealed partial class DiscoveryStore
return false; return false;
} }
row.Status = "done"; row.Status = DiscoveryTaskStatuses.Done;
row.UpdatedAt = DateTimeOffset.UtcNow; row.UpdatedAt = DateTimeOffset.UtcNow;
await _dbContext.SaveChangesAsync(ct); await _dbContext.SaveChangesAsync(ct);
return true; return true;
@@ -211,7 +211,7 @@ public sealed partial class DiscoveryStore
async Task<int> IDiscoveryStore.SumActivePlanAsync(string? excludeTaskId, CancellationToken ct) async Task<int> IDiscoveryStore.SumActivePlanAsync(string? excludeTaskId, CancellationToken ct)
{ {
IQueryable<DiscTaskEntity> query = _dbContext.DiscTasks IQueryable<DiscTaskEntity> query = _dbContext.DiscTasks
.Where(task => task.Status != "done" && task.Status != "failed"); .Where(task => task.Status != DiscoveryTaskStatuses.Done && task.Status != DiscoveryTaskStatuses.Failed);
if (excludeTaskId is not null) if (excludeTaskId is not null)
{ {
query = query.Where(task => task.Id != excludeTaskId); query = query.Where(task => task.Id != excludeTaskId);
@@ -5,9 +5,6 @@ using Deal.Modules.Discovery.Application.Models;
namespace Deal.Infrastructure.Persistence.Repositories; namespace Deal.Infrastructure.Persistence.Repositories;
/// <summary>
/// EF-адаптер хранилища Discovery
/// </summary>
public sealed partial class DiscoveryStore : IDiscoveryStore public sealed partial class DiscoveryStore : IDiscoveryStore
{ {
private readonly TenantDbContext _dbContext; private readonly TenantDbContext _dbContext;
@@ -9,9 +9,6 @@ using Microsoft.EntityFrameworkCore;
namespace Deal.Infrastructure.Persistence.Repositories; namespace Deal.Infrastructure.Persistence.Repositories;
/// <summary>
/// EF-адаптер хранилища карточек и контейнеров
/// </summary>
public sealed partial class KanbanStore : ICardStore public sealed partial class KanbanStore : ICardStore
{ {
private readonly TenantDbContext _dbContext; private readonly TenantDbContext _dbContext;
@@ -6,9 +6,6 @@ using Microsoft.EntityFrameworkCore;
namespace Deal.Infrastructure.Persistence.Repositories; namespace Deal.Infrastructure.Persistence.Repositories;
/// <summary>
/// EF-адаптер хранилища лимитов ИИ-бюджета
/// </summary>
public sealed class TenantLimitStore : ITenantLimitStore public sealed class TenantLimitStore : ITenantLimitStore
{ {
private readonly DealDbContext _dbContext; private readonly DealDbContext _dbContext;
@@ -58,7 +55,6 @@ public sealed class TenantLimitStore : ITenantLimitStore
_utcNow = utcNow; _utcNow = utcNow;
} }
/// <inheritdoc />
async Task<TenantLimitDto> ITenantLimitStore.GetOrCreateAsync( async Task<TenantLimitDto> ITenantLimitStore.GetOrCreateAsync(
Guid tenantId, Guid tenantId,
CancellationToken ct, CancellationToken ct,
@@ -68,7 +64,6 @@ public sealed class TenantLimitStore : ITenantLimitStore
return ToLimitDto(entity); return ToLimitDto(entity);
} }
/// <inheritdoc />
async Task<BudgetStateDto> ITenantLimitStore.GetStateAsync(Guid tenantId, CancellationToken ct) async Task<BudgetStateDto> ITenantLimitStore.GetStateAsync(Guid tenantId, CancellationToken ct)
{ {
TenantLimitEntity entity = await LoadOrCreateAsync(tenantId, _defaults, ct); TenantLimitEntity entity = await LoadOrCreateAsync(tenantId, _defaults, ct);
@@ -76,7 +71,6 @@ public sealed class TenantLimitStore : ITenantLimitStore
return await ToStateDtoAsync(entity, ct); return await ToStateDtoAsync(entity, ct);
} }
/// <inheritdoc />
async Task<BudgetStateDto> ITenantLimitStore.AddUsageAsync( async Task<BudgetStateDto> ITenantLimitStore.AddUsageAsync(
Guid tenantId, Guid tenantId,
long tokens, long tokens,
@@ -109,7 +103,6 @@ public sealed class TenantLimitStore : ITenantLimitStore
return await ToStateDtoAsync(entity, ct); return await ToStateDtoAsync(entity, ct);
} }
/// <inheritdoc />
async Task<BudgetStateDto> ITenantLimitStore.UpdateBudgetAsync( async Task<BudgetStateDto> ITenantLimitStore.UpdateBudgetAsync(
Guid tenantId, Guid tenantId,
long budgetTokens, long budgetTokens,
@@ -132,7 +125,6 @@ public sealed class TenantLimitStore : ITenantLimitStore
return await ToStateDtoAsync(entity, ct); return await ToStateDtoAsync(entity, ct);
} }
/// <inheritdoc />
async Task<bool> ITenantLimitStore.TryMarkWarnedAsync(Guid tenantId, CancellationToken ct) async Task<bool> ITenantLimitStore.TryMarkWarnedAsync(Guid tenantId, CancellationToken ct)
{ {
TenantLimitEntity entity = await LoadOrCreateAsync(tenantId, _defaults, ct); TenantLimitEntity entity = await LoadOrCreateAsync(tenantId, _defaults, ct);
@@ -148,7 +140,6 @@ public sealed class TenantLimitStore : ITenantLimitStore
return true; return true;
} }
/// <inheritdoc />
async Task<bool> ITenantLimitStore.TryMarkNotifiedExhaustedAsync(Guid tenantId, CancellationToken ct) async Task<bool> ITenantLimitStore.TryMarkNotifiedExhaustedAsync(Guid tenantId, CancellationToken ct)
{ {
TenantLimitEntity entity = await LoadOrCreateAsync(tenantId, _defaults, ct); TenantLimitEntity entity = await LoadOrCreateAsync(tenantId, _defaults, ct);
@@ -219,7 +210,6 @@ public sealed class TenantLimitStore : ITenantLimitStore
return true; return true;
} }
/// <inheritdoc />
async Task<int> ITenantLimitStore.ResetExpiredPeriodsAsync(DateTimeOffset now, CancellationToken ct) async Task<int> ITenantLimitStore.ResetExpiredPeriodsAsync(DateTimeOffset now, CancellationToken ct)
{ {
// Трогаем только строки с накоплениями (расход/флаги): строки без накоплений чистить нечего. // Трогаем только строки с накоплениями (расход/флаги): строки без накоплений чистить нечего.
@@ -61,6 +61,23 @@ public sealed class TenantRepository(DealDbContext dbContext) : ITenantRepositor
return entities; return entities;
} }
public async Task<IReadOnlyDictionary<Guid, string>> FindNamesByIdsAsync(
IReadOnlyCollection<Guid> ids,
CancellationToken ct)
{
if (ids.Count == 0)
{
return new Dictionary<Guid, string>();
}
var rows = await dbContext.Tenants
.AsNoTracking()
.Where(t => ids.Contains(t.Id))
.Select(t => new { t.Id, t.Name })
.ToListAsync(ct);
return rows.ToDictionary(row => row.Id, row => row.Name);
}
/// <inheritdoc /> /// <inheritdoc />
public async Task<bool> UpdateStatusAsync( public async Task<bool> UpdateStatusAsync(
Guid id, Guid id,
@@ -4,9 +4,6 @@ using Deal.Modules.Settings.Application.Abstractions;
namespace Deal.Infrastructure.Security; namespace Deal.Infrastructure.Security;
/// <summary>
/// AES-256-GCM-шифр секретов
/// </summary>
public sealed class AesGcmSecretCipher : ISecretCipher public sealed class AesGcmSecretCipher : ISecretCipher
{ {
// Префикс зашифрованного значения (маркер формата в хранилище). // Префикс зашифрованного значения (маркер формата в хранилище).
@@ -39,7 +36,6 @@ public sealed class AesGcmSecretCipher : ISecretCipher
_key = key; _key = key;
} }
/// <inheritdoc />
string ISecretCipher.Encrypt(string plainText) string ISecretCipher.Encrypt(string plainText)
{ {
if (string.IsNullOrEmpty(plainText)) if (string.IsNullOrEmpty(plainText))
@@ -65,7 +61,6 @@ public sealed class AesGcmSecretCipher : ISecretCipher
return EncryptedPrefix + Convert.ToBase64String(payload); return EncryptedPrefix + Convert.ToBase64String(payload);
} }
/// <inheritdoc />
string ISecretCipher.Decrypt(string cipherText) string ISecretCipher.Decrypt(string cipherText)
{ {
if (string.IsNullOrEmpty(cipherText) || !cipherText.StartsWith(EncryptedPrefix, StringComparison.Ordinal)) if (string.IsNullOrEmpty(cipherText) || !cipherText.StartsWith(EncryptedPrefix, StringComparison.Ordinal))
@@ -22,8 +22,6 @@ public sealed class CardMover(CardsService cardsService) : ICardMover
CardResultDto result = CardsDefaultContainers.Contains(toContainerId) CardResultDto result = CardsDefaultContainers.Contains(toContainerId)
? await cardsService.MoveStageCardAsync(cardId, toContainerId, ct) ? await cardsService.MoveStageCardAsync(cardId, toContainerId, ct)
: await cardsService.MoveDashboardCardAsync(cardId, toContainerId, ct); : await cardsService.MoveDashboardCardAsync(cardId, toContainerId, ct);
return result.Error is not null return new CardMoveResultDto(result.Error, From: result.Card?.PrevCol, To: result.Card?.Col);
? new CardMoveResultDto(result.Error, Exists: true)
: new CardMoveResultDto(null, Exists: result.Card is not null);
} }
} }
@@ -1,5 +1,6 @@
using Deal.Modules.Cards.Application.Dtos; using Deal.Modules.Cards.Application.Dtos;
using Deal.Modules.Cards.Application.Models; using Deal.Modules.Cards.Application.Models;
using Deal.SharedKernel.Errors;
namespace Deal.Modules.Cards.Application.Abstractions; namespace Deal.Modules.Cards.Application.Abstractions;
@@ -14,7 +15,8 @@ public interface ICardMover
/// <param name="cardId">Id карточки.</param> /// <param name="cardId">Id карточки.</param>
/// <param name="toContainerId">Id контейнера назначения (стадия «Выбранных» либо дашборд-контейнер).</param> /// <param name="toContainerId">Id контейнера назначения (стадия «Выбранных» либо дашборд-контейнер).</param>
/// <param name="ctx">Контекст перехода (инициатор, причина, обучение).</param> /// <param name="ctx">Контекст перехода (инициатор, причина, обучение).</param>
/// <returns>Результат: Error (400-текст отказа) | Exists=false (карточки нет, 404) | успех (Exists=true).</returns> /// <returns>Результат: Error (400-текст отказа) либо null (успех).</returns>
/// <exception cref="NotFoundException">Карточка не найдена.</exception>
public Task<CardMoveResultDto> MoveAsync( public Task<CardMoveResultDto> MoveAsync(
string cardId, string cardId,
string toContainerId, string toContainerId,
@@ -5,6 +5,7 @@ namespace Deal.Modules.Cards.Application.Dtos;
/// <summary> /// <summary>
/// Результат перехода карточки единым механизмом <see cref="ICardMover"/>. /// Результат перехода карточки единым механизмом <see cref="ICardMover"/>.
/// </summary> /// </summary>
/// <param name="Error">Текст 400-ошибки либо null.</param> /// <param name="Error">Текст 400-ошибки либо null (успех).</param>
/// <param name="Exists">True — карточка найдена и переход выполнен (либо перенос был no-op).</param> /// <param name="From">Контейнер-источник после перехода; null — переход не выполнен.</param>
public sealed record CardMoveResultDto(string? Error, bool Exists); /// <param name="To">Контейнер-назначение после перехода; null — переход не выполнен.</param>
public sealed record CardMoveResultDto(string? Error, string? From = null, string? To = null);
@@ -1,3 +1,5 @@
using Deal.Contracts.Integrations.Models;
namespace Deal.Modules.Cards.Application.Models; namespace Deal.Modules.Cards.Application.Models;
/// <summary> /// <summary>
@@ -8,7 +10,7 @@ public sealed record CardContact
/// <summary> /// <summary>
/// Тип контакта: tg/phone/email/linkedin/whatsapp/site/other. /// Тип контакта: tg/phone/email/linkedin/whatsapp/site/other.
/// </summary> /// </summary>
public string Type { get; init; } = "other"; public string Type { get; init; } = CardContactTypes.Other;
/// <summary> /// <summary>
/// Значение: @username, +7…, name@mail, url. /// Значение: @username, +7…, name@mail, url.
@@ -23,7 +23,7 @@ public sealed record CardFile
/// <summary> /// <summary>
/// Тип контента: image/video/audio/archive/document/other /// Тип контента: image/video/audio/archive/document/other
/// </summary> /// </summary>
public string Kind { get; init; } = "other"; public string Kind { get; init; } = CardFileKinds.Other;
public string ObjectKey { get; init; } = string.Empty; public string ObjectKey { get; init; } = string.Empty;
} }
@@ -0,0 +1,37 @@
namespace Deal.Modules.Cards.Application.Models;
/// <summary>
/// Категории вложений карточки
/// </summary>
public static class CardFileKinds
{
/// <summary>
/// Изображение
/// </summary>
public const string Image = "image";
/// <summary>
/// Видео
/// </summary>
public const string Video = "video";
/// <summary>
/// Аудио
/// </summary>
public const string Audio = "audio";
/// <summary>
/// Документ
/// </summary>
public const string Document = "document";
/// <summary>
/// Архив
/// </summary>
public const string Archive = "archive";
/// <summary>
/// Прочее (не опознано)
/// </summary>
public const string Other = "other";
}
@@ -19,4 +19,24 @@ public static class DiscoveryCandidateKinds
/// Форум (группа с темами; topics заполняются оценкой). /// Форум (группа с темами; topics заполняются оценкой).
/// </summary> /// </summary>
public const string Forum = "forum"; public const string Forum = "forum";
/// <summary>
/// Русское написание «канал» (как приходит из Telegram)
/// </summary>
public const string ChannelAliasRu = "канал";
/// <summary>
/// Русское написание «форум»
/// </summary>
public const string ForumAliasRu = "форум";
/// <summary>
/// Английское написание «chat»
/// </summary>
public const string ChatAlias = "chat";
/// <summary>
/// Русское написание «чат»
/// </summary>
public const string ChatAliasRu = "чат";
} }
@@ -28,7 +28,7 @@ public sealed record DiscoveryCandidateRow
/// <summary> /// <summary>
/// Тип источника: channel|group|forum. /// Тип источника: channel|group|forum.
/// </summary> /// </summary>
public string Kind { get; init; } = "channel"; public string Kind { get; init; } = DiscoveryCandidateKinds.Channel;
/// <summary> /// <summary>
/// Цвет источника /// Цвет источника
@@ -0,0 +1,17 @@
namespace Deal.Modules.Discovery.Application.Models;
/// <summary>
/// Источники решения о соответствии сообщения
/// </summary>
public static class DiscoveryFitOrigins
{
/// <summary>
/// Эвристика по ключевым словам
/// </summary>
public const string Heuristic = "heuristic";
/// <summary>
/// Локальная ML-модель
/// </summary>
public const string Ml = "ml";
}
@@ -0,0 +1,17 @@
namespace Deal.Modules.Discovery.Application.Models;
/// <summary>
/// Языки источников Discovery
/// </summary>
public static class DiscoveryLanguages
{
/// <summary>
/// Русскоязычные источники
/// </summary>
public const string Russian = "ru";
/// <summary>
/// Любой язык
/// </summary>
public const string Any = "any";
}
@@ -2,6 +2,7 @@ using Deal.Contracts.Integrations.Models;
using Deal.Modules.Discovery.Application.Abstractions; using Deal.Modules.Discovery.Application.Abstractions;
using Deal.Modules.Discovery.Application.Exceptions; using Deal.Modules.Discovery.Application.Exceptions;
using Deal.Modules.Discovery.Application.Models; using Deal.Modules.Discovery.Application.Models;
using Deal.SharedKernel.Errors;
namespace Deal.Modules.Discovery.Application.Services; namespace Deal.Modules.Discovery.Application.Services;
@@ -13,6 +14,10 @@ public sealed class DiscoveryCandidatesService(
DiscoveryLogService log, DiscoveryLogService log,
DiscoveryBlacklistService blacklist) DiscoveryBlacklistService blacklist)
{ {
private const string CandidateEntityName = "Кандидат";
private const string TaskEntityName = "Задача поиска";
/// <summary> /// <summary>
/// 400 mark_rejected /// 400 mark_rejected
/// </summary> /// </summary>
@@ -53,10 +58,12 @@ public sealed class DiscoveryCandidatesService(
/// Кандидат по dialog_id. /// Кандидат по dialog_id.
/// </summary> /// </summary>
/// <param name="dialogId">Подписанный id источника.</param> /// <param name="dialogId">Подписанный id источника.</param>
/// <returns>Кандидат или null (404 «Кандидат не найден»).</returns> /// <returns>Кандидат.</returns>
public Task<DiscoveryCandidateDto?> GetAsync(string dialogId, CancellationToken ct) /// <exception cref="NotFoundException">Кандидат не найден.</exception>
public async Task<DiscoveryCandidateDto> GetAsync(string dialogId, CancellationToken ct)
{ {
return store.GetCandidateAsync(dialogId, ct); return await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false)
?? throw new NotFoundException(CandidateEntityName, dialogId);
} }
/// <summary> /// <summary>
@@ -78,11 +85,8 @@ public sealed class DiscoveryCandidatesService(
string hue, string hue,
CancellationToken ct) CancellationToken ct)
{ {
DiscoveryTaskDto? task = await store.GetTaskAsync(taskId, ct).ConfigureAwait(false); DiscoveryTaskDto task = await store.GetTaskAsync(taskId, ct).ConfigureAwait(false)
if (task is null) ?? throw new NotFoundException(TaskEntityName, taskId);
{
return null;
}
if (await store.IsDialogMonitoredAsync(dialogId, ct).ConfigureAwait(false)) if (await store.IsDialogMonitoredAsync(dialogId, ct).ConfigureAwait(false))
{ {
@@ -125,7 +129,8 @@ public sealed class DiscoveryCandidatesService(
}; };
await store.CreateCandidateAsync(row, ct).ConfigureAwait(false); await store.CreateCandidateAsync(row, ct).ConfigureAwait(false);
await store.BumpTaskCounterAsync(taskId, DiscoveryCounterField.Found, 1, ct).ConfigureAwait(false); await store.BumpTaskCounterAsync(taskId, DiscoveryCounterField.Found, 1, ct).ConfigureAwait(false);
return await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false); return await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false)
?? throw new NotFoundException(CandidateEntityName, dialogId);
} }
/// <summary> /// <summary>
@@ -134,18 +139,21 @@ public sealed class DiscoveryCandidatesService(
/// <param name="taskId">Id задачи.</param> /// <param name="taskId">Id задачи.</param>
/// <param name="dialogId">Подписанный id источника.</param> /// <param name="dialogId">Подписанный id источника.</param>
/// <param name="patch">Изменяемые поля (null — не менять).</param> /// <param name="patch">Изменяемые поля (null — не менять).</param>
/// <returns>Обновлённый кандидат либо null — задачи/кандидата нет (или кандидат другой задачи).</returns> /// <returns>Обновлённый кандидат.</returns>
public async Task<DiscoveryCandidateDto?> SetAsync( /// <exception cref="NotFoundException">Задача или кандидат не найдены.</exception>
public async Task<DiscoveryCandidateDto> SetAsync(
string taskId, string taskId,
string dialogId, string dialogId,
DiscoveryCandidatePatch patch, DiscoveryCandidatePatch patch,
CancellationToken ct) CancellationToken ct)
{ {
DiscoveryTaskDto? task = await store.GetTaskAsync(taskId, ct).ConfigureAwait(false); _ = await store.GetTaskAsync(taskId, ct).ConfigureAwait(false)
DiscoveryCandidateDto? current = await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false); ?? throw new NotFoundException(TaskEntityName, taskId);
if (task is null || current is null || current.TaskId != taskId) DiscoveryCandidateDto current = await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false)
?? throw new NotFoundException(CandidateEntityName, dialogId);
if (current.TaskId != taskId)
{ {
return null; throw new NotFoundException(CandidateEntityName, dialogId);
} }
DiscoveryCandidatePatch normalized = NormalizePatch(patch, current); DiscoveryCandidatePatch normalized = NormalizePatch(patch, current);
@@ -159,7 +167,8 @@ public sealed class DiscoveryCandidatesService(
} }
await store.PatchCandidateAsync(dialogId, normalized, ct).ConfigureAwait(false); await store.PatchCandidateAsync(dialogId, normalized, ct).ConfigureAwait(false);
return await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false); return await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false)
?? throw new NotFoundException(CandidateEntityName, dialogId);
} }
/// <summary> /// <summary>
@@ -167,9 +176,10 @@ public sealed class DiscoveryCandidatesService(
/// </summary> /// </summary>
/// <param name="dialogId">Подписанный id источника.</param> /// <param name="dialogId">Подписанный id источника.</param>
/// <param name="status">Новый статус: new|review.</param> /// <param name="status">Новый статус: new|review.</param>
/// <returns>Обновлённый кандидат либо null (кандидата нет).</returns> /// <returns>Обновлённый кандидат.</returns>
/// <exception cref="NotFoundException">Кандидат не найден.</exception>
/// <exception cref="DiscoveryValidationException">Статус не new/review.</exception> /// <exception cref="DiscoveryValidationException">Статус не new/review.</exception>
public async Task<DiscoveryCandidateDto?> SetStatusAsync( public async Task<DiscoveryCandidateDto> SetStatusAsync(
string dialogId, string dialogId,
string status, string status,
CancellationToken ct) CancellationToken ct)
@@ -179,11 +189,8 @@ public sealed class DiscoveryCandidatesService(
throw new DiscoveryValidationException(string.Format(TransitionNotAllowedFormat, status)); throw new DiscoveryValidationException(string.Format(TransitionNotAllowedFormat, status));
} }
DiscoveryCandidateDto? current = await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false); DiscoveryCandidateDto current = await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false)
if (current is null) ?? throw new NotFoundException(CandidateEntityName, dialogId);
{
return null;
}
await store.SetCandidateStatusAsync(dialogId, status, ct).ConfigureAwait(false); await store.SetCandidateStatusAsync(dialogId, status, ct).ConfigureAwait(false);
if (status == DiscoveryCandidateStatuses.Review) if (status == DiscoveryCandidateStatuses.Review)
@@ -195,7 +202,8 @@ public sealed class DiscoveryCandidatesService(
ct).ConfigureAwait(false); ct).ConfigureAwait(false);
} }
return await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false); return await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false)
?? throw new NotFoundException(CandidateEntityName, dialogId);
} }
/// <summary> /// <summary>
@@ -203,17 +211,15 @@ public sealed class DiscoveryCandidatesService(
/// </summary> /// </summary>
/// <param name="dialogId">Подписанный id источника.</param> /// <param name="dialogId">Подписанный id источника.</param>
/// <param name="auto">True — авто-вступление воркера; false — ручное.</param> /// <param name="auto">True — авто-вступление воркера; false — ручное.</param>
/// <returns>Кандидат в joined либо null (кандидата нет). Повторный вызов для joined — идемпотентен.</returns> /// <returns>Кандидат в joined. Повторный вызов для joined — идемпотентен.</returns>
public async Task<DiscoveryCandidateDto?> MarkJoinedAsync( /// <exception cref="NotFoundException">Кандидат не найден.</exception>
public async Task<DiscoveryCandidateDto> MarkJoinedAsync(
string dialogId, string dialogId,
bool auto, bool auto,
CancellationToken ct) CancellationToken ct)
{ {
DiscoveryCandidateDto? current = await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false); DiscoveryCandidateDto current = await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false)
if (current is null) ?? throw new NotFoundException(CandidateEntityName, dialogId);
{
return null;
}
if (current.Status == DiscoveryCandidateStatuses.Joined) if (current.Status == DiscoveryCandidateStatuses.Joined)
{ {
@@ -224,7 +230,8 @@ public sealed class DiscoveryCandidatesService(
await store.BumpTaskCounterAsync(current.TaskId, DiscoveryCounterField.Joined, 1, ct).ConfigureAwait(false); await store.BumpTaskCounterAsync(current.TaskId, DiscoveryCounterField.Joined, 1, ct).ConfigureAwait(false);
string logEvent = auto ? DiscoveryLogEvents.JoinAuto : DiscoveryLogEvents.JoinManual; string logEvent = auto ? DiscoveryLogEvents.JoinAuto : DiscoveryLogEvents.JoinManual;
await log.AddAsync(current.TaskId, logEvent, string.Format(JoinedLogFormat, dialogId), ct).ConfigureAwait(false); await log.AddAsync(current.TaskId, logEvent, string.Format(JoinedLogFormat, dialogId), ct).ConfigureAwait(false);
return await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false); return await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false)
?? throw new NotFoundException(CandidateEntityName, dialogId);
} }
/// <summary> /// <summary>
@@ -232,18 +239,16 @@ public sealed class DiscoveryCandidatesService(
/// </summary> /// </summary>
/// <param name="dialogId">Подписанный id источника.</param> /// <param name="dialogId">Подписанный id источника.</param>
/// <param name="reason">Причина отклонения (в лог/чёрный список; «отклонено вручную» — эндпоинт reject).</param> /// <param name="reason">Причина отклонения (в лог/чёрный список; «отклонено вручную» — эндпоинт reject).</param>
/// <returns>Кандидат в rejected либо null (кандидата нет). Повторный вызов для rejected — идемпотентен.</returns> /// <returns>Кандидат в rejected. Повторный вызов для rejected — идемпотентен.</returns>
/// <exception cref="NotFoundException">Кандидат не найден.</exception>
/// <exception cref="DiscoveryValidationException">Источник уже joined — отклонить нельзя.</exception> /// <exception cref="DiscoveryValidationException">Источник уже joined — отклонить нельзя.</exception>
public async Task<DiscoveryCandidateDto?> MarkRejectedAsync( public async Task<DiscoveryCandidateDto> MarkRejectedAsync(
string dialogId, string dialogId,
string reason, string reason,
CancellationToken ct) CancellationToken ct)
{ {
DiscoveryCandidateDto? current = await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false); DiscoveryCandidateDto current = await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false)
if (current is null) ?? throw new NotFoundException(CandidateEntityName, dialogId);
{
return null;
}
if (current.Status == DiscoveryCandidateStatuses.Joined) if (current.Status == DiscoveryCandidateStatuses.Joined)
{ {
@@ -260,7 +265,8 @@ public sealed class DiscoveryCandidatesService(
string text = string.IsNullOrEmpty(reason) ? string.Format(RejectedLogFormat, dialogId) : reason; string text = string.IsNullOrEmpty(reason) ? string.Format(RejectedLogFormat, dialogId) : reason;
await log.AddAsync(current.TaskId, DiscoveryLogEvents.Reject, text, ct).ConfigureAwait(false); await log.AddAsync(current.TaskId, DiscoveryLogEvents.Reject, text, ct).ConfigureAwait(false);
await blacklist.AddAsync(dialogId, current.Name, reason ?? string.Empty, ct).ConfigureAwait(false); await blacklist.AddAsync(dialogId, current.Name, reason ?? string.Empty, ct).ConfigureAwait(false);
return await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false); return await store.GetCandidateAsync(dialogId, ct).ConfigureAwait(false)
?? throw new NotFoundException(CandidateEntityName, dialogId);
} }
/// <summary> /// <summary>
@@ -116,7 +116,7 @@ public sealed class DiscoveryEvaluator
string raw = text ?? string.Empty; string raw = text ?? string.Empty;
if (raw.Trim().Length < MinTextLength) if (raw.Trim().Length < MinTextLength)
{ {
return new DiscoveryMessageFit(false, "слишком короткое", "heuristic"); return new DiscoveryMessageFit(false, "слишком короткое", DiscoveryFitOrigins.Heuristic);
} }
if (settingsSnapshot.GetBool(SettingsKeys.MlEnabled, SettingsDefaults.MlEnabled)) if (settingsSnapshot.GetBool(SettingsKeys.MlEnabled, SettingsDefaults.MlEnabled))
@@ -167,11 +167,11 @@ public sealed class DiscoveryEvaluator
string kw = (keyword ?? string.Empty).Trim(); string kw = (keyword ?? string.Empty).Trim();
if (kw.Length > 0 && haystack.Contains(kw.ToLowerInvariant(), StringComparison.Ordinal)) if (kw.Length > 0 && haystack.Contains(kw.ToLowerInvariant(), StringComparison.Ordinal))
{ {
return new DiscoveryMessageFit(true, $"совпал ключ \"{kw}\"", "heuristic"); return new DiscoveryMessageFit(true, $"совпал ключ \"{kw}\"", DiscoveryFitOrigins.Heuristic);
} }
} }
return new DiscoveryMessageFit(false, "нет совпадений с ключами", "heuristic"); return new DiscoveryMessageFit(false, "нет совпадений с ключами", DiscoveryFitOrigins.Heuristic);
} }
/// <summary> /// <summary>
@@ -3,9 +3,6 @@ using Deal.Modules.Discovery.Application.Abstractions;
namespace Deal.Modules.Discovery.Application.Services; namespace Deal.Modules.Discovery.Application.Services;
/// <summary>
/// Потокобезопасная реализация <see cref="IDiscoverySearchErrorCounter"/>
/// </summary>
public sealed class DiscoverySearchErrorCounter : IDiscoverySearchErrorCounter public sealed class DiscoverySearchErrorCounter : IDiscoverySearchErrorCounter
{ {
/// <summary> /// <summary>
@@ -43,7 +40,6 @@ public sealed class DiscoverySearchErrorCounter : IDiscoverySearchErrorCounter
_utcNow = utcNow; _utcNow = utcNow;
} }
/// <inheritdoc />
int IDiscoverySearchErrorCounter.Next(string taskId) int IDiscoverySearchErrorCounter.Next(string taskId)
{ {
EvictExpired(); EvictExpired();
@@ -55,7 +51,6 @@ public sealed class DiscoverySearchErrorCounter : IDiscoverySearchErrorCounter
return fresh.Count; return fresh.Count;
} }
/// <inheritdoc />
void IDiscoverySearchErrorCounter.Reset(string taskId) void IDiscoverySearchErrorCounter.Reset(string taskId)
{ {
EvictExpired(); EvictExpired();
@@ -65,7 +65,7 @@ public sealed class DiscoveryTasksService(IDiscoveryStore store, DiscoveryPlanGu
await planGuard.AssertPlanAsync(planJoins, ct).ConfigureAwait(false); await planGuard.AssertPlanAsync(planJoins, ct).ConfigureAwait(false);
await planGuard.AssertBudgetAsync(planJoins, excludeTaskId: null, ct).ConfigureAwait(false); await planGuard.AssertBudgetAsync(planJoins, excludeTaskId: null, ct).ConfigureAwait(false);
string lang = draft.Lang is "ru" or "any" ? draft.Lang : "ru"; string lang = draft.Lang is DiscoveryLanguages.Russian or DiscoveryLanguages.Any ? draft.Lang : DiscoveryLanguages.Russian;
// Дефолты threshold/sampleSize — из типизированного снимка настроек (C30: один GetAllAsync на создание). // Дефолты threshold/sampleSize — из типизированного снимка настроек (C30: один GetAllAsync на создание).
TenantSettingsSnapshot settingsSnapshot = await TenantSettingsSnapshot.LoadAsync(settings, ct); TenantSettingsSnapshot settingsSnapshot = await TenantSettingsSnapshot.LoadAsync(settings, ct);
@@ -230,7 +230,7 @@ public sealed class DiscoveryTasksService(IDiscoveryStore store, DiscoveryPlanGu
int? threshold = patch.Threshold is int value ? Clamp(value, min: 1, max: 100) : null; int? threshold = patch.Threshold is int value ? Clamp(value, min: 1, max: 100) : null;
int? sampleSize = patch.SampleSize is int sample ? Math.Max(1, sample) : null; int? sampleSize = patch.SampleSize is int sample ? Math.Max(1, sample) : null;
int? minSubscribers = patch.MinSubscribers is int min ? Math.Max(0, min) : null; int? minSubscribers = patch.MinSubscribers is int min ? Math.Max(0, min) : null;
string? lang = patch.Lang is "ru" or "any" ? patch.Lang : patch.Lang is null ? null : "ru"; string? lang = patch.Lang is DiscoveryLanguages.Russian or DiscoveryLanguages.Any ? patch.Lang : patch.Lang is null ? null : DiscoveryLanguages.Russian;
string? name = patch.Name is null ? null : patch.Name.Trim(); string? name = patch.Name is null ? null : patch.Name.Trim();
string? description = patch.Description; string? description = patch.Description;
return new DiscoveryTaskPatch return new DiscoveryTaskPatch
@@ -7,12 +7,16 @@ namespace Deal.Modules.Discovery.Application.Services;
// источника (KindCode) и тексты сообщений для оценки (GroupTexts). // источника (KindCode) и тексты сообщений для оценки (GroupTexts).
public sealed partial class DiscoveryWorkerService public sealed partial class DiscoveryWorkerService
{ {
// Префиксы сообщения об ошибке Telegram при FloodWait.
private const string FloodPrefix = "flood:";
private const string FloodSpacePrefix = "flood ";
internal static bool IsFlood(Exception exception) internal static bool IsFlood(Exception exception)
{ {
string message = exception.Message ?? string.Empty; string message = exception.Message ?? string.Empty;
return message.StartsWith("flood:", StringComparison.OrdinalIgnoreCase) return message.StartsWith(FloodPrefix, StringComparison.OrdinalIgnoreCase)
|| message.StartsWith("flood ", StringComparison.OrdinalIgnoreCase) || message.StartsWith(FloodSpacePrefix, StringComparison.OrdinalIgnoreCase)
|| message.Contains("flood:", StringComparison.OrdinalIgnoreCase); || message.Contains(FloodPrefix, StringComparison.OrdinalIgnoreCase);
} }
private static string KindCode(string kind, bool isForum = false) private static string KindCode(string kind, bool isForum = false)
@@ -24,9 +28,9 @@ public sealed partial class DiscoveryWorkerService
return kind.Trim().ToLowerInvariant() switch return kind.Trim().ToLowerInvariant() switch
{ {
DiscoveryCandidateKinds.Channel or "канал" => DiscoveryCandidateKinds.Channel, DiscoveryCandidateKinds.Channel or DiscoveryCandidateKinds.ChannelAliasRu => DiscoveryCandidateKinds.Channel,
DiscoveryCandidateKinds.Forum or "форум" => DiscoveryCandidateKinds.Forum, DiscoveryCandidateKinds.Forum or DiscoveryCandidateKinds.ForumAliasRu => DiscoveryCandidateKinds.Forum,
"chat" or "чат" => DiscoveryCandidateKinds.Group, DiscoveryCandidateKinds.ChatAlias or DiscoveryCandidateKinds.ChatAliasRu => DiscoveryCandidateKinds.Group,
_ => DiscoveryCandidateKinds.Group, _ => DiscoveryCandidateKinds.Group,
}; };
} }
@@ -56,7 +56,7 @@ public sealed partial class DiscoveryWorkerService
foreach (TelegramDialogEntryDto item in results) foreach (TelegramDialogEntryDto item in results)
{ {
string rawKind = (item.Kind ?? string.Empty).Trim().ToLowerInvariant(); string rawKind = (item.Kind ?? string.Empty).Trim().ToLowerInvariant();
if (rawKind == "чат" || rawKind == "chat") if (rawKind == DiscoveryCandidateKinds.ChatAliasRu || rawKind == DiscoveryCandidateKinds.ChatAlias)
{ {
await _log.AddAsync(taskId, DiscoveryLogEvents.Skip, $"{item.Name}: личный чат/бот", ct) await _log.AddAsync(taskId, DiscoveryLogEvents.Skip, $"{item.Name}: личный чат/бот", ct)
.ConfigureAwait(false); .ConfigureAwait(false);
@@ -56,7 +56,7 @@ public static class ColumnMatcher
bool budgetOk = !budgetEnabled || BudgetInRange.IsInRange(AmountParser.Parse(text), rules.Budget!, rates); bool budgetOk = !budgetEnabled || BudgetInRange.IsInRange(AmountParser.Parse(text), rules.Budget!, rates);
bool pricesOk = !pricesEnabled || BudgetInRange.IsInRange(AmountParser.Parse(text), rules.Prices!, rates); bool pricesOk = !pricesEnabled || BudgetInRange.IsInRange(AmountParser.Parse(text), rules.Prices!, rates);
if (string.Equals(rules.Mode, "any", StringComparison.OrdinalIgnoreCase)) if (string.Equals(rules.Mode, ColumnRuleModes.Any, StringComparison.OrdinalIgnoreCase))
{ {
// «любое»: совпасть должна хотя бы одна ВКЛЮЧЁННАЯ (непустая) группа. // «любое»: совпасть должна хотя бы одна ВКЛЮЧЁННАЯ (непустая) группа.
return (keywords.Count > 0 && keywordsOk) return (keywords.Count > 0 && keywordsOk)
@@ -69,7 +69,7 @@ public static class RulesDescriber
return NoRulesText; return NoRulesText;
} }
string mode = string.Equals(rules.Mode, "any", StringComparison.OrdinalIgnoreCase) string mode = string.Equals(rules.Mode, ColumnRuleModes.Any, StringComparison.OrdinalIgnoreCase)
? AnyModePrefix ? AnyModePrefix
: AllModePrefix; : AllModePrefix;
return mode + " · " + string.Join("; ", parts); return mode + " · " + string.Join("; ", parts);
@@ -0,0 +1,17 @@
namespace Deal.Modules.Kanban.Application.Models;
/// <summary>
/// Режимы сопоставления правил колонки
/// </summary>
public static class ColumnRuleModes
{
/// <summary>
/// Достаточно любого условия
/// </summary>
public const string Any = "any";
/// <summary>
/// Нужны все условия
/// </summary>
public const string All = "all";
}
@@ -15,8 +15,8 @@ public sealed record ContainerCreateDto(
string Name, string Name,
string Description = "", string Description = "",
string? Color = null, string? Color = null,
string Space = "dashboard", string Space = ContainerSpaces.Dashboard,
string Kind = "board", string Kind = ContainerKinds.Board,
bool Suggested = false, bool Suggested = false,
ContainerRulesDto? Rules = null, ContainerRulesDto? Rules = null,
string Note = ""); string Note = "");
@@ -0,0 +1,52 @@
namespace Deal.Modules.Kanban.Application.Models;
/// <summary>
/// Ключи wire-контракта канбана (ответы API и патчи карточки)
/// </summary>
public static class KanbanWireKeys
{
/// <summary>
/// Счётчик новых карточек
/// </summary>
public const string New = "new";
/// <summary>
/// Счётчик в обучении ML
/// </summary>
public const string Learning = "learning";
/// <summary>
/// Счётчик решённых ML
/// </summary>
public const string Ml = "ml";
/// <summary>
/// Счётчик решённых ИИ
/// </summary>
public const string Ai = "ai";
/// <summary>
/// Признак свёрнутой колонки
/// </summary>
public const string Collapsed = "collapsed";
/// <summary>
/// Ширина колонки
/// </summary>
public const string Width = "width";
/// <summary>
/// Нижняя граница бюджета
/// </summary>
public const string From = "from";
/// <summary>
/// Верхняя граница бюджета
/// </summary>
public const string To = "to";
/// <summary>
/// Валюта бюджета
/// </summary>
public const string Currency = "cur";
}
@@ -27,9 +27,16 @@ public sealed partial class CardsService
/// </summary> /// </summary>
/// <param name="cardId">Id карточки (<c>c_...</c>).</param> /// <param name="cardId">Id карточки (<c>c_...</c>).</param>
/// <returns>Карточка или null — строки нет (эндпоинт отвечает 404 «Карточка не найдена»).</returns> /// <returns>Карточка или null — строки нет (эндпоинт отвечает 404 «Карточка не найдена»).</returns>
public Task<CardDto?> GetCardAsync(string cardId, CancellationToken ct) /// <summary>
/// Одна карточка по id.
/// </summary>
/// <param name="cardId">Id карточки (<c>c_...</c>).</param>
/// <returns>Карточка.</returns>
/// <exception cref="NotFoundException">Карточка не найдена.</exception>
public async Task<CardDto> GetCardAsync(string cardId, CancellationToken ct)
{ {
return _store.GetCardAsync(cardId, ct); return await _store.GetCardAsync(cardId, ct)
?? throw new NotFoundException(CardEntityName, cardId);
} }
@@ -57,7 +64,7 @@ public sealed partial class CardsService
CardDto? card = await _store.GetCardAsync(cardId, ct); CardDto? card = await _store.GetCardAsync(cardId, ct);
if (card is null) if (card is null)
{ {
return new CardResultDto(null, null); throw new NotFoundException(CardEntityName, cardId);
} }
if (card.Col == CardIds.Archive || card.Col == CardIds.Trash) if (card.Col == CardIds.Archive || card.Col == CardIds.Trash)
@@ -81,7 +88,10 @@ public sealed partial class CardsService
await _mlClient.PushAsync(text, toCol, PushWeightUser, ct); await _mlClient.PushAsync(text, toCol, PushWeightUser, ct);
} }
return new CardResultDto(null, await _store.GetCardAsync(cardId, ct)); return new CardResultDto(
null,
await _store.GetCardAsync(cardId, ct)
?? throw new NotFoundException(CardEntityName, cardId));
} }
/// <summary> /// <summary>
@@ -1,5 +1,6 @@
using Deal.Modules.Kanban.Application.Models; using Deal.Modules.Kanban.Application.Models;
using Deal.Modules.Settings.Application.Models; using Deal.Modules.Settings.Application.Models;
using Deal.SharedKernel.Errors;
namespace Deal.Modules.Kanban.Application.Services; namespace Deal.Modules.Kanban.Application.Services;
@@ -22,17 +23,15 @@ public sealed partial class CardsService
/// </summary> /// </summary>
/// <param name="cardId">Id карточки (<c>c_...</c>).</param> /// <param name="cardId">Id карточки (<c>c_...</c>).</param>
/// <param name="atMs">Время напоминания, epoch-ms.</param> /// <param name="atMs">Время напоминания, epoch-ms.</param>
/// <returns>Результат: Error <see cref="RemindersDisabledDetail"/> (400) | Card=null без Error (404) | Card — карточка с напоминанием.</returns> /// <returns>Результат: Error <see cref="RemindersDisabledDetail"/> (400) | Card — карточка с напоминанием.</returns>
/// <exception cref="NotFoundException">Карточка не найдена.</exception>
public async Task<CardResultDto> SetReminderAsync( public async Task<CardResultDto> SetReminderAsync(
string cardId, string cardId,
long atMs, long atMs,
CancellationToken ct) CancellationToken ct)
{ {
CardDto? card = await _store.GetCardAsync(cardId, ct); CardDto card = await _store.GetCardAsync(cardId, ct)
if (card is null) ?? throw new NotFoundException(CardEntityName, cardId);
{
return new CardResultDto(null, null);
}
if (!await ReadRemindersEnabledAsync(ct)) if (!await ReadRemindersEnabledAsync(ct))
{ {
@@ -41,7 +40,7 @@ public sealed partial class CardsService
await _store.SetReminderAsync(cardId, atMs, ct); await _store.SetReminderAsync(cardId, atMs, ct);
CardDto saved = await _store.GetCardAsync(cardId, ct) CardDto saved = await _store.GetCardAsync(cardId, ct)
?? throw new InvalidOperationException("Карточка не прочиталась после установки напоминания: " + cardId); ?? throw new NotFoundException(CardEntityName, cardId);
return new CardResultDto(null, saved); return new CardResultDto(null, saved);
} }
@@ -49,35 +48,27 @@ public sealed partial class CardsService
/// Снимает напоминание карточки. /// Снимает напоминание карточки.
/// </summary> /// </summary>
/// <param name="cardId">Id карточки (<c>c_...</c>).</param> /// <param name="cardId">Id карточки (<c>c_...</c>).</param>
/// <returns>True — карточка есть и напоминание снято; false — карточки нет (404).</returns> /// <exception cref="NotFoundException">Карточка не найдена.</exception>
public async Task<bool> ClearReminderAsync(string cardId, CancellationToken ct) public async Task ClearReminderAsync(string cardId, CancellationToken ct)
{ {
CardDto? card = await _store.GetCardAsync(cardId, ct); _ = await _store.GetCardAsync(cardId, ct)
if (card is null) ?? throw new NotFoundException(CardEntityName, cardId);
{
return false;
}
await _store.ClearReminderAsync(cardId, ct); await _store.ClearReminderAsync(cardId, ct);
return true;
} }
/// <summary> /// <summary>
/// «Напомнить позже» /// «Напомнить позже»
/// </summary> /// </summary>
/// <param name="cardId">Id карточки (<c>c_...</c>).</param> /// <param name="cardId">Id карточки (<c>c_...</c>).</param>
/// <returns>True — карточка есть и напоминание отложено; false — карточки нет (404).</returns> /// <exception cref="NotFoundException">Карточка не найдена.</exception>
public async Task<bool> SnoozeReminderAsync(string cardId, CancellationToken ct) public async Task SnoozeReminderAsync(string cardId, CancellationToken ct)
{ {
CardDto? card = await _store.GetCardAsync(cardId, ct); _ = await _store.GetCardAsync(cardId, ct)
if (card is null) ?? throw new NotFoundException(CardEntityName, cardId);
{
return false;
}
long snoozedAtMs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + ReminderSnoozeMs; long snoozedAtMs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + ReminderSnoozeMs;
await _store.SetReminderAsync(cardId, snoozedAtMs, ct); await _store.SetReminderAsync(cardId, snoozedAtMs, ct);
return true;
} }
/// <summary> /// <summary>

Some files were not shown because too many files have changed in this diff Show More