feat: 添加飞书用户模块和订阅功能支持 - 新增feishu_users模块用于处理飞书用户身份验证和权限管理 - 新增subscriptions模块用于处理订阅相关功能 - 新增personalization模块用于个性化服务 - 在alembic迁移配置中注册新的模型模块 - 在API路由器中添加feishu_users和subscriptions路由 - 实现事件调度服务的改进,包括错误处理和状态更新优化 - 添加飞书命令处理的权限检查机制 - 实现飞书应用票据事件处理 - 改进审计日志记录功能 ```
188 lines
6.8 KiB
Python
188 lines
6.8 KiB
Python
from typing import Any
|
|
|
|
from fastapi import HTTPException, status
|
|
from sqlalchemy import select
|
|
|
|
from app.core.constants import ActorValue
|
|
from app.core.utils.time import utc_now
|
|
from app.modules.audit.constants import AuditAction, AuditRiskLevel, AuditSource, AuditStatus
|
|
from app.modules.audit.schemas import AuditLogCreate
|
|
from app.modules.audit.service import AuditService
|
|
from app.modules.business.constants import BusinessDomain, SourceSystem, StatusValue
|
|
from app.modules.business.models import LegacySyncRun, Project
|
|
from app.modules.business.service import serialize_model
|
|
from app.modules.events.constants import (
|
|
EventAggregateType,
|
|
EventPayloadKey,
|
|
EventSource,
|
|
EventType,
|
|
)
|
|
from app.modules.events.services import EventService
|
|
from app.modules.legacy_mysql.constants import (
|
|
LEGACY_PROJECT_QUERY_SOURCE,
|
|
LEGACY_PROJECT_SYNC_NOTE,
|
|
LEGACY_SYNC_MISSING_ID_REASON,
|
|
LEGACY_SYNC_RUN_CODE_PREFIX,
|
|
LegacyProjectField,
|
|
LegacyQueryError,
|
|
LegacyQueryName,
|
|
LegacyResponseKey,
|
|
LegacySyncAction,
|
|
)
|
|
|
|
from app.modules.legacy_mysql.services.common import _query_name_text
|
|
|
|
|
|
class LegacyProjectSyncMixin:
|
|
def sync_projects(
|
|
self,
|
|
source_query: str | None = None,
|
|
source_query_name: str | None = None,
|
|
field_map: dict[str, str] | None = None,
|
|
limit: int = 100,
|
|
dry_run: bool = True,
|
|
actor: str = ActorValue.API,
|
|
) -> dict[str, Any]:
|
|
if self.db is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
detail=LegacyQueryError.APP_DB_UNAVAILABLE,
|
|
)
|
|
|
|
query_name = source_query_name or LegacyQueryName.PROJECTS
|
|
if source_query:
|
|
rows = self.execute_readonly(
|
|
source_query,
|
|
{LegacyResponseKey.LIMIT: limit},
|
|
limit=limit,
|
|
)[LegacyResponseKey.ROWS]
|
|
query_ref = LegacySyncAction.ALLOWLISTED_INLINE_SQL
|
|
else:
|
|
rows = self.execute_allowed_query(
|
|
query_name,
|
|
{LegacyResponseKey.LIMIT: limit},
|
|
limit=limit,
|
|
)[LegacyResponseKey.ROWS]
|
|
query_ref = _query_name_text(query_name)
|
|
field_map = field_map or {}
|
|
created = 0
|
|
updated = 0
|
|
skipped = 0
|
|
items: list[dict[str, Any]] = []
|
|
|
|
for row in rows:
|
|
payload = self._project_payload(row, field_map)
|
|
if not payload[LegacyProjectField.EXTERNAL_ID] and not payload[LegacyProjectField.CODE]:
|
|
skipped += 1
|
|
items.append(
|
|
{
|
|
LegacyResponseKey.ACTION: LegacySyncAction.SKIPPED,
|
|
LegacyResponseKey.REASON: LEGACY_SYNC_MISSING_ID_REASON,
|
|
LegacyResponseKey.SOURCE: row,
|
|
}
|
|
)
|
|
continue
|
|
|
|
stmt = select(Project).where(
|
|
Project.source_system == SourceSystem.LEGACY_MYSQL,
|
|
Project.external_id == payload[LegacyProjectField.EXTERNAL_ID],
|
|
)
|
|
record = self.db.execute(stmt).scalar_one_or_none()
|
|
if record is None:
|
|
record = self.db.execute(
|
|
select(Project).where(Project.code == payload[LegacyProjectField.CODE])
|
|
).scalar_one_or_none()
|
|
|
|
if record is None:
|
|
created += 1
|
|
action = LegacySyncAction.CREATE
|
|
result = payload
|
|
if not dry_run:
|
|
record = Project(**payload)
|
|
self.db.add(record)
|
|
self.db.flush()
|
|
result = serialize_model(record)
|
|
else:
|
|
updated += 1
|
|
action = LegacySyncAction.UPDATE
|
|
if not dry_run:
|
|
for key, value in payload.items():
|
|
setattr(record, key, value)
|
|
self.db.flush()
|
|
result = serialize_model(record)
|
|
else:
|
|
result = payload
|
|
items.append(
|
|
{
|
|
LegacyResponseKey.ACTION: action,
|
|
LegacyResponseKey.PROJECT: result,
|
|
LegacyResponseKey.SOURCE: row,
|
|
}
|
|
)
|
|
|
|
result = {
|
|
LegacyResponseKey.DRY_RUN: dry_run,
|
|
LegacyResponseKey.CREATED: created,
|
|
LegacyResponseKey.UPDATED: updated,
|
|
LegacyResponseKey.SKIPPED: skipped,
|
|
LegacyResponseKey.ITEMS: items,
|
|
}
|
|
sync_run = LegacySyncRun(
|
|
code=f"{LEGACY_SYNC_RUN_CODE_PREFIX}-{utc_now():%Y%m%d%H%M%S%f}",
|
|
domain=BusinessDomain.PROJECTS,
|
|
source_table=LEGACY_PROJECT_QUERY_SOURCE,
|
|
status=StatusValue.DRY_RUN if dry_run else AuditStatus.SUCCESS,
|
|
finished_at=utc_now(),
|
|
created_count=created,
|
|
updated_count=updated,
|
|
skipped_count=skipped,
|
|
note=LEGACY_PROJECT_SYNC_NOTE,
|
|
)
|
|
self.db.add(sync_run)
|
|
self.db.flush()
|
|
result[LegacyResponseKey.SYNC_RUN_CODE] = sync_run.code
|
|
|
|
AuditService(self.db).record(
|
|
AuditLogCreate(
|
|
actor=actor,
|
|
source=AuditSource.LEGACY_MYSQL,
|
|
action=AuditAction.LEGACY_SYNC_PROJECTS,
|
|
target_type=BusinessDomain.PROJECTS,
|
|
risk_level=AuditRiskLevel.MEDIUM,
|
|
request_payload={
|
|
LegacyResponseKey.SOURCE_QUERY: query_ref,
|
|
LegacyResponseKey.FIELD_MAP: field_map,
|
|
LegacyResponseKey.LIMIT: limit,
|
|
LegacyResponseKey.DRY_RUN: dry_run,
|
|
},
|
|
response_payload={
|
|
key: result[key]
|
|
for key in [
|
|
LegacyResponseKey.DRY_RUN,
|
|
LegacyResponseKey.CREATED,
|
|
LegacyResponseKey.UPDATED,
|
|
LegacyResponseKey.SKIPPED,
|
|
]
|
|
},
|
|
)
|
|
)
|
|
EventService(self.db).enqueue(
|
|
event_type=EventType.LEGACY_SYNC_COMPLETED,
|
|
source=EventSource.LEGACY_MYSQL,
|
|
aggregate_type=EventAggregateType.LEGACY_SYNC_RUN,
|
|
aggregate_id=sync_run.code,
|
|
actor=actor,
|
|
payload={
|
|
EventPayloadKey.CODE: sync_run.code,
|
|
EventPayloadKey.DOMAIN: BusinessDomain.PROJECTS,
|
|
EventPayloadKey.STATUS: sync_run.status,
|
|
EventPayloadKey.CREATED: created,
|
|
EventPayloadKey.UPDATED: updated,
|
|
EventPayloadKey.SKIPPED: skipped,
|
|
},
|
|
idempotency_key=f"legacy-sync:{sync_run.code}",
|
|
)
|
|
self.db.commit()
|
|
self.db.refresh(sync_run)
|
|
return result
|