feat(oa): add list_instance_history with scoped preview fields
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
416c4a0838
commit
32824fdbf5
2 files changed
+86
-2
No files matched your search
@@ -927,11 +927,13 @@ def _enrich_list_rows(
|
||||
tenant_no: str,
|
||||
rows: List[Dict[str, Any]],
|
||||
instances: List[OaInstance],
|
||||
preview_limit: int = 3,
|
||||
) -> List[Dict[str, Any]]:
|
||||
if not rows:
|
||||
return []
|
||||
from app.services.oa_related_approval import flatten_form_fields_for_summary
|
||||
|
||||
limit = max(0, int(preview_limit or 3))
|
||||
inst_by_id = {i.id: i for i in instances}
|
||||
app_ids = list({i.app_id for i in instances if i.app_id})
|
||||
apps = (
|
||||
@@ -1004,11 +1006,16 @@ def _enrich_list_rows(
|
||||
form_data = _loads(inst.form_data_json, {}) or {}
|
||||
preview = [
|
||||
{"label": f.get("label") or "", "value": f.get("value") or ""}
|
||||
for f in flatten_form_fields_for_summary(schema, form_data)[:3]
|
||||
for f in flatten_form_fields_for_summary(schema, form_data)[:limit]
|
||||
]
|
||||
merged = dict(row)
|
||||
merged.update(summary)
|
||||
merged["previewFields"] = preview
|
||||
st = (inst.status or "").strip()
|
||||
if st in _FINISH_STATUSES and inst.update_time:
|
||||
merged["finishTime"] = inst.update_time.strftime("%Y-%m-%d %H:%M:%S")
|
||||
else:
|
||||
merged["finishTime"] = None
|
||||
out.append(merged)
|
||||
return out
|
||||
|
||||
@@ -2139,6 +2146,7 @@ def mark_cc_tasks_read_batch(
|
||||
_INSTANCE_STATUS_FILTER_VALUES = frozenset(
|
||||
{"running", "completed", "rejected", "returned", "cancelled"}
|
||||
)
|
||||
_FINISH_STATUSES = frozenset({"completed", "rejected", "cancelled"})
|
||||
|
||||
|
||||
def _parse_day_start(s: Optional[str]) -> Optional[datetime]:
|
||||
@@ -2234,6 +2242,81 @@ def _apply_instance_list_filters(
|
||||
return q, joined_app, joined_initiator
|
||||
|
||||
|
||||
def list_instance_history(
|
||||
db: Session,
|
||||
*,
|
||||
tenant_no: str,
|
||||
user_id: str,
|
||||
can_view_all: bool,
|
||||
app_id: str,
|
||||
instance_id: Optional[str] = None,
|
||||
initiator_name: Optional[str] = None,
|
||||
apply_time_from: Optional[str] = None,
|
||||
apply_time_to: Optional[str] = None,
|
||||
finish_time_from: Optional[str] = None,
|
||||
finish_time_to: Optional[str] = None,
|
||||
instance_status: Optional[str] = None,
|
||||
page: int = 1,
|
||||
page_size: int = 20,
|
||||
) -> Dict[str, Any]:
|
||||
app_id = (app_id or "").strip()
|
||||
if not app_id:
|
||||
raise OaEngineError("appId 不能为空")
|
||||
page = max(1, int(page or 1))
|
||||
page_size = min(100, max(1, int(page_size or 20)))
|
||||
offset = (page - 1) * page_size
|
||||
|
||||
q = db.query(OaInstance).filter(
|
||||
OaInstance.tenant_no == tenant_no,
|
||||
OaInstance.del_flag == 0,
|
||||
OaInstance.app_id == app_id,
|
||||
)
|
||||
if not can_view_all:
|
||||
q = q.filter(OaInstance.initiator_user_id == user_id)
|
||||
initiator_name = None
|
||||
|
||||
iid = (instance_id or "").strip()
|
||||
if iid:
|
||||
q = q.filter(OaInstance.id.like(f"%{iid}%"))
|
||||
|
||||
q, _, _ = _apply_instance_list_filters(
|
||||
q,
|
||||
initiator_name=initiator_name,
|
||||
apply_time_from=apply_time_from,
|
||||
apply_time_to=apply_time_to,
|
||||
instance_status=instance_status,
|
||||
)
|
||||
|
||||
ft0 = _parse_day_start(finish_time_from)
|
||||
ft1 = _parse_day_end(finish_time_to)
|
||||
if ft0 is not None or ft1 is not None:
|
||||
q = q.filter(OaInstance.status.in_(tuple(_FINISH_STATUSES)))
|
||||
if ft0 is not None:
|
||||
q = q.filter(OaInstance.update_time >= ft0)
|
||||
if ft1 is not None:
|
||||
q = q.filter(OaInstance.update_time <= ft1)
|
||||
|
||||
total = q.count()
|
||||
insts = (
|
||||
q.order_by(OaInstance.create_time.desc())
|
||||
.offset(offset)
|
||||
.limit(page_size)
|
||||
.all()
|
||||
)
|
||||
rows = [_instance_to_dict(i) for i in insts]
|
||||
return {
|
||||
"list": _enrich_list_rows(
|
||||
db,
|
||||
tenant_no=tenant_no,
|
||||
rows=rows,
|
||||
instances=insts,
|
||||
preview_limit=6,
|
||||
),
|
||||
"total": int(total),
|
||||
"canViewAll": bool(can_view_all),
|
||||
}
|
||||
|
||||
|
||||
def list_tasks(
|
||||
db: Session,
|
||||
*,
|
||||
|
||||
@@ -8,7 +8,7 @@ from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from app.models.base import Base
|
||||
from app.models.models import OaApp, OaAppVersion, OaInstance, SysUser
|
||||
from app.models.models import OaApp, OaAppVersion, OaInstance, OaTask, SysUser
|
||||
from app.services import oa_engine
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ class TestOaInstanceHistory(unittest.TestCase):
|
||||
OaAppVersion.__table__,
|
||||
SysUser.__table__,
|
||||
OaInstance.__table__,
|
||||
OaTask.__table__,
|
||||
],
|
||||
)
|
||||
Session = sessionmaker(bind=self.engine, autoflush=False, autocommit=False)
|
||||
|
||||
Reference in new issue
Block a user