```
refactor: 移除审批和回写功能模块 移除了整个审批(approvals)和官方回写(writebacks)功能模块, 包括相关模型、路由、服务和配置项。更新了数据库迁移文件, 删除了相关的审批请求表和官方回写运行表。同时从API路由器中 移除了相应的路由,并调整了安全常量和字段验证器以匹配变更。 ```
This commit is contained in:
@@ -1,24 +1,13 @@
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.constants import ActorValue
|
||||
from app.core.config import get_settings
|
||||
from app.modules.approvals.service import ApprovalService
|
||||
from app.modules.audit.constants import AuditAction, AuditSource
|
||||
from app.modules.audit.schemas import AuditLogCreate
|
||||
from app.modules.feishu.commands import FeishuCommandService
|
||||
from app.modules.feishu.constants import (
|
||||
FEISHU_APPROVAL_ACTION_INVALID,
|
||||
FEISHU_APPROVAL_APPROVER_IDS_REQUIRED,
|
||||
FEISHU_APPROVAL_CARD_ACTION_TARGET,
|
||||
FEISHU_APPROVER_NOT_ALLOWED,
|
||||
FeishuApprovalAction,
|
||||
FeishuApprovalValueKey,
|
||||
FeishuCardKey,
|
||||
FeishuCommandKey,
|
||||
FeishuEventReceiptKey,
|
||||
FeishuEventSource,
|
||||
@@ -90,86 +79,6 @@ class FeishuEventService:
|
||||
FeishuResponseKey.RESULT: result,
|
||||
}
|
||||
|
||||
def handle_approval_card_action(self, payload: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Handle Feishu interactive-card approval button callbacks."""
|
||||
|
||||
self.feishu.verify_event(payload)
|
||||
value = _approval_action_value(payload)
|
||||
ticket_id = str(value.get(FeishuApprovalValueKey.TICKET_ID) or "").strip()
|
||||
decision = str(
|
||||
value.get(FeishuApprovalValueKey.DECISION)
|
||||
or value.get(FeishuApprovalValueKey.ACTION)
|
||||
or ""
|
||||
).lower()
|
||||
if not ticket_id or decision not in set(FeishuApprovalAction):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail=FEISHU_APPROVAL_ACTION_INVALID,
|
||||
)
|
||||
comment = value.get(FeishuApprovalValueKey.COMMENT)
|
||||
actor = _approval_operator(payload)
|
||||
try:
|
||||
_ensure_approval_operator_allowed(actor)
|
||||
except HTTPException as exc:
|
||||
self.feishu.audit.log(
|
||||
AuditLogCreate(
|
||||
actor=actor,
|
||||
source=AuditSource.FEISHU,
|
||||
action=AuditAction.FEISHU_WEBHOOK_EVENT,
|
||||
target_type=FEISHU_APPROVAL_CARD_ACTION_TARGET,
|
||||
target_id=ticket_id,
|
||||
request_payload=payload,
|
||||
response_payload={
|
||||
FeishuResponseKey.OK: False,
|
||||
"status_code": exc.status_code,
|
||||
"detail": exc.detail,
|
||||
},
|
||||
)
|
||||
)
|
||||
raise
|
||||
event_identity = _approval_event_identity(payload, ticket_id, decision, actor)
|
||||
if not self._register_event(event_identity):
|
||||
ticket = ApprovalService(self.db).get_by_ticket(ticket_id)
|
||||
return {
|
||||
FeishuResponseKey.OK: True,
|
||||
FeishuResponseKey.HANDLED: True,
|
||||
FeishuResponseKey.DUPLICATE: True,
|
||||
FeishuResponseKey.RESULT: {
|
||||
FeishuApprovalValueKey.TICKET_ID: ticket.ticket_id,
|
||||
FeishuResponseKey.STATUS: ticket.status,
|
||||
FeishuResponseKey.APPROVER: ticket.approver,
|
||||
},
|
||||
}
|
||||
ticket = ApprovalService(self.db).decide(
|
||||
ticket_id,
|
||||
actor,
|
||||
approved=decision == FeishuApprovalAction.APPROVE,
|
||||
comment=str(comment) if comment is not None else None,
|
||||
)
|
||||
self.feishu.audit.log(
|
||||
AuditLogCreate(
|
||||
actor=actor,
|
||||
source=AuditSource.FEISHU,
|
||||
action=AuditAction.FEISHU_WEBHOOK_EVENT,
|
||||
target_type=FEISHU_APPROVAL_CARD_ACTION_TARGET,
|
||||
target_id=ticket_id,
|
||||
request_payload=payload,
|
||||
response_payload={
|
||||
FeishuResponseKey.STATUS: ticket.status,
|
||||
FeishuApprovalValueKey.DECISION: decision,
|
||||
},
|
||||
)
|
||||
)
|
||||
return {
|
||||
FeishuResponseKey.OK: True,
|
||||
FeishuResponseKey.HANDLED: True,
|
||||
FeishuResponseKey.RESULT: {
|
||||
FeishuApprovalValueKey.TICKET_ID: ticket.ticket_id,
|
||||
FeishuResponseKey.STATUS: ticket.status,
|
||||
FeishuResponseKey.APPROVER: ticket.approver,
|
||||
},
|
||||
}
|
||||
|
||||
def _register_event(self, event_identity: dict[str, str | None]) -> bool:
|
||||
receipt = FeishuEventReceipt(
|
||||
event_key=str(event_identity[FeishuEventReceiptKey.EVENT_KEY]),
|
||||
@@ -214,71 +123,3 @@ def _event_identity(
|
||||
FeishuEventReceiptKey.EVENT_ID: str(event_id) if event_id else None,
|
||||
FeishuEventReceiptKey.MESSAGE_ID: str(message_id) if message_id else None,
|
||||
}
|
||||
|
||||
|
||||
def _approval_action_value(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
action = payload.get(FeishuApprovalValueKey.ACTION) or {}
|
||||
event = payload.get(FeishuPayloadKey.EVENT) or {}
|
||||
event_action = event.get(FeishuApprovalValueKey.ACTION) or {}
|
||||
value = (
|
||||
action.get(FeishuCardKey.VALUE)
|
||||
or event_action.get(FeishuCardKey.VALUE)
|
||||
or payload.get(FeishuCardKey.VALUE)
|
||||
or {}
|
||||
)
|
||||
if isinstance(value, str):
|
||||
try:
|
||||
parsed = json.loads(value)
|
||||
except json.JSONDecodeError:
|
||||
return {}
|
||||
return parsed if isinstance(parsed, dict) else {}
|
||||
return value if isinstance(value, dict) else {}
|
||||
|
||||
|
||||
def _approval_operator(payload: dict[str, Any]) -> str:
|
||||
operator = payload.get("operator") or (payload.get(FeishuPayloadKey.EVENT) or {}).get(
|
||||
"operator"
|
||||
) or {}
|
||||
operator_id = operator.get("operator_id") or {}
|
||||
return (
|
||||
operator_id.get(FeishuPayloadKey.OPEN_ID)
|
||||
or operator_id.get(FeishuPayloadKey.USER_ID)
|
||||
or operator.get(FeishuPayloadKey.OPEN_ID)
|
||||
or operator.get(FeishuPayloadKey.USER_ID)
|
||||
or ActorValue.FEISHU
|
||||
)
|
||||
|
||||
|
||||
def _ensure_approval_operator_allowed(actor: str) -> None:
|
||||
allowed_ids = {
|
||||
item.strip()
|
||||
for item in get_settings().feishu_approval_approver_ids
|
||||
if item.strip()
|
||||
}
|
||||
if not allowed_ids:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail=FEISHU_APPROVAL_APPROVER_IDS_REQUIRED,
|
||||
)
|
||||
if actor not in allowed_ids:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail=FEISHU_APPROVER_NOT_ALLOWED,
|
||||
)
|
||||
|
||||
|
||||
def _approval_event_identity(
|
||||
payload: dict[str, Any],
|
||||
ticket_id: str,
|
||||
decision: str,
|
||||
actor: str,
|
||||
) -> dict[str, str | None]:
|
||||
header = payload.get(FeishuPayloadKey.HEADER) or {}
|
||||
event_id = header.get(FeishuPayloadKey.EVENT_ID)
|
||||
stable_id = event_id or f"{ticket_id}:{decision}:{actor}"
|
||||
return {
|
||||
FeishuEventReceiptKey.EVENT_KEY: f"{FeishuEventSource.WEBHOOK}:approval:{stable_id}",
|
||||
FeishuEventReceiptKey.SOURCE: FeishuEventSource.WEBHOOK,
|
||||
FeishuEventReceiptKey.EVENT_ID: str(event_id) if event_id else None,
|
||||
FeishuEventReceiptKey.MESSAGE_ID: None,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user