初始化
This commit is contained in:
1 parent
34474bd5fc
commit
b638b541d0
3008 files changed
+837699
No files matched your search
+26
@@ -0,0 +1,26 @@
|
||||
"""Persistence layer for 岗位 (positions) and per-resource tag bindings."""
|
||||
|
||||
from deerflow.persistence.positions.base import PositionStore
|
||||
from deerflow.persistence.positions.memory import MemoryPositionStore
|
||||
from deerflow.persistence.positions.model import (
|
||||
POSITION_RESOURCE_TYPES,
|
||||
PositionRow,
|
||||
PositionTagBindingRow,
|
||||
)
|
||||
from deerflow.persistence.positions.sql import PositionRepository
|
||||
|
||||
__all__ = [
|
||||
"PositionStore",
|
||||
"MemoryPositionStore",
|
||||
"PositionRepository",
|
||||
"PositionRow",
|
||||
"PositionTagBindingRow",
|
||||
"POSITION_RESOURCE_TYPES",
|
||||
"make_position_store",
|
||||
]
|
||||
|
||||
|
||||
def make_position_store(session_factory):
|
||||
if session_factory is None:
|
||||
return MemoryPositionStore()
|
||||
return PositionRepository(session_factory)
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
"""Abstract persistence API for 岗位 (positions) and their tag bindings."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any
|
||||
|
||||
|
||||
class PositionStore(ABC):
|
||||
# -------------------------------------------------------------- positions
|
||||
|
||||
@abstractmethod
|
||||
async def list_positions(self) -> list[dict[str, Any]]:
|
||||
"""List all positions. Each record carries a derived ``member_count``."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def get_position(self, position_id: str) -> dict[str, Any] | None:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def create_position(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Create a position. ``data`` must carry ``id`` and ``name``.
|
||||
|
||||
Raises ``ValueError`` if ``name`` is not unique. When ``is_default`` is
|
||||
truthy, any other default position is cleared first.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def update_position(self, position_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
|
||||
"""Update ``name`` / ``description`` / ``is_default``.
|
||||
|
||||
Returns ``None`` if the position does not exist. Raises ``ValueError``
|
||||
on a name collision. Setting ``is_default=True`` clears the flag on the
|
||||
other positions.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def delete_position(self, position_id: str) -> bool:
|
||||
"""Delete a position, its tag bindings, and unassign its members."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def get_default_position_id(self) -> str | None:
|
||||
"""Return the id of the ``is_default`` position, or ``None``."""
|
||||
raise NotImplementedError
|
||||
|
||||
# ------------------------------------------------------------ tag bindings
|
||||
|
||||
@abstractmethod
|
||||
async def get_tag_bindings(self, position_id: str) -> dict[str, list[str]]:
|
||||
"""Return ``{resource_type: [tag_id, ...]}`` for one position.
|
||||
|
||||
Resource types with no bound tags are omitted (callers treat a missing
|
||||
key as "use the default set").
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def set_tag_bindings(self, position_id: str, bindings: dict[str, list[str]]) -> dict[str, list[str]]:
|
||||
"""Replace the full set of tag bindings for a position.
|
||||
|
||||
``bindings`` maps each resource_type to its desired tag-id list. A
|
||||
resource_type omitted from ``bindings`` (or mapped to ``[]``) ends up
|
||||
with no rows — i.e. it falls back to the default set.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
# --------------------------------------------------------------- members
|
||||
|
||||
@abstractmethod
|
||||
async def get_user_position_id(self, user_id: str) -> str | None:
|
||||
"""Return the position id explicitly assigned to a user, or ``None``."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def list_members(self, position_id: str) -> list[dict[str, Any]]:
|
||||
"""Return ``[{id, email}, ...]`` of users assigned to this position."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def assign_users(self, position_id: str, user_ids: list[str]) -> int:
|
||||
"""Assign a position to many users (1:1 — overwrites any prior position).
|
||||
|
||||
Returns the number of users updated.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def unassign_users(self, user_ids: list[str]) -> int:
|
||||
"""Clear the position of many users. Returns the number updated."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def list_member_ids(self, position_id: str) -> list[str]:
|
||||
"""Return just the user ids assigned to this position (sync fan-out)."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def list_assigned_user_ids(self) -> list[str]:
|
||||
"""Return every user id that has an explicit position (for bulk resync)."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
async def list_all_users(self) -> list[dict[str, Any]]:
|
||||
"""Return ``[{id, email, system_role, position_id}, ...]`` for every user.
|
||||
|
||||
Powers the admin member-picker and the per-user position editor.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
"""In-memory ``PositionStore`` implementation for tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _now() -> str:
|
||||
return datetime.now(UTC).isoformat()
|
||||
|
||||
|
||||
class MemoryPositionStore:
|
||||
def __init__(self) -> None:
|
||||
self._positions: dict[str, dict[str, Any]] = {}
|
||||
# position_id -> {resource_type -> [tag_id]}
|
||||
self._bindings: dict[str, dict[str, list[str]]] = {}
|
||||
# user_id -> position_id
|
||||
self._user_position: dict[str, str] = {}
|
||||
# user_id -> email (optional; populated via register_user for nicer member lists)
|
||||
self._user_email: dict[str, str] = {}
|
||||
|
||||
# test helper — not part of the abstract API
|
||||
def register_user(self, user_id: str, email: str = "") -> None:
|
||||
self._user_email[user_id] = email
|
||||
|
||||
def _member_count(self, position_id: str) -> int:
|
||||
return sum(1 for pid in self._user_position.values() if pid == position_id)
|
||||
|
||||
def _to_dict(self, record: dict[str, Any]) -> dict[str, Any]:
|
||||
out = copy.deepcopy(record)
|
||||
out["member_count"] = self._member_count(record["id"])
|
||||
return out
|
||||
|
||||
# -------------------------------------------------------------- positions
|
||||
|
||||
async def list_positions(self) -> list[dict[str, Any]]:
|
||||
rows = sorted(self._positions.values(), key=lambda r: r["created_at"])
|
||||
return [self._to_dict(r) for r in rows]
|
||||
|
||||
async def get_position(self, position_id: str) -> dict[str, Any] | None:
|
||||
record = self._positions.get(position_id)
|
||||
return self._to_dict(record) if record is not None else None
|
||||
|
||||
def _clear_default(self, keep_id: str | None) -> None:
|
||||
for r in self._positions.values():
|
||||
if r["id"] != keep_id:
|
||||
r["is_default"] = False
|
||||
|
||||
async def create_position(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
name = data["name"]
|
||||
if any(r["name"] == name for r in self._positions.values()):
|
||||
raise ValueError(f"Position '{name}' already exists")
|
||||
is_default = bool(data.get("is_default"))
|
||||
if is_default:
|
||||
self._clear_default(keep_id=data["id"])
|
||||
record = {
|
||||
"id": data["id"],
|
||||
"name": name,
|
||||
"description": data.get("description") or "",
|
||||
"is_default": is_default,
|
||||
"created_at": _now(),
|
||||
"updated_at": _now(),
|
||||
}
|
||||
self._positions[data["id"]] = record
|
||||
return self._to_dict(record)
|
||||
|
||||
async def update_position(self, position_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
|
||||
record = self._positions.get(position_id)
|
||||
if record is None:
|
||||
return None
|
||||
new_name = data.get("name", record["name"])
|
||||
if new_name is not None and any(r["id"] != position_id and r["name"] == new_name for r in self._positions.values()):
|
||||
raise ValueError(f"Position '{new_name}' already exists")
|
||||
if "name" in data and data["name"] is not None:
|
||||
record["name"] = data["name"]
|
||||
if "description" in data and data["description"] is not None:
|
||||
record["description"] = data["description"]
|
||||
if "is_default" in data and data["is_default"] is not None:
|
||||
record["is_default"] = bool(data["is_default"])
|
||||
if record["is_default"]:
|
||||
self._clear_default(keep_id=position_id)
|
||||
record["updated_at"] = _now()
|
||||
return self._to_dict(record)
|
||||
|
||||
async def delete_position(self, position_id: str) -> bool:
|
||||
if position_id not in self._positions:
|
||||
return False
|
||||
del self._positions[position_id]
|
||||
self._bindings.pop(position_id, None)
|
||||
for uid in [uid for uid, pid in self._user_position.items() if pid == position_id]:
|
||||
del self._user_position[uid]
|
||||
return True
|
||||
|
||||
async def get_default_position_id(self) -> str | None:
|
||||
for r in self._positions.values():
|
||||
if r["is_default"]:
|
||||
return r["id"]
|
||||
return None
|
||||
|
||||
# ------------------------------------------------------------ tag bindings
|
||||
|
||||
async def get_tag_bindings(self, position_id: str) -> dict[str, list[str]]:
|
||||
return copy.deepcopy(self._bindings.get(position_id, {}))
|
||||
|
||||
async def set_tag_bindings(self, position_id: str, bindings: dict[str, list[str]]) -> dict[str, list[str]]:
|
||||
cleaned = {rt: list(dict.fromkeys(tag_ids)) for rt, tag_ids in bindings.items() if tag_ids}
|
||||
self._bindings[position_id] = cleaned
|
||||
return copy.deepcopy(cleaned)
|
||||
|
||||
# --------------------------------------------------------------- members
|
||||
|
||||
async def get_user_position_id(self, user_id: str) -> str | None:
|
||||
return self._user_position.get(user_id)
|
||||
|
||||
async def list_members(self, position_id: str) -> list[dict[str, Any]]:
|
||||
return [{"id": uid, "email": self._user_email.get(uid, "")} for uid, pid in self._user_position.items() if pid == position_id]
|
||||
|
||||
async def assign_users(self, position_id: str, user_ids: list[str]) -> int:
|
||||
for uid in user_ids:
|
||||
self._user_position[uid] = position_id
|
||||
return len(user_ids)
|
||||
|
||||
async def unassign_users(self, user_ids: list[str]) -> int:
|
||||
count = 0
|
||||
for uid in user_ids:
|
||||
if uid in self._user_position:
|
||||
del self._user_position[uid]
|
||||
count += 1
|
||||
return count
|
||||
|
||||
async def list_member_ids(self, position_id: str) -> list[str]:
|
||||
return [uid for uid, pid in self._user_position.items() if pid == position_id]
|
||||
|
||||
async def list_assigned_user_ids(self) -> list[str]:
|
||||
return list(self._user_position.keys())
|
||||
|
||||
async def list_all_users(self) -> list[dict[str, Any]]:
|
||||
return [
|
||||
{"id": uid, "email": email, "system_role": "user", "position_id": self._user_position.get(uid)}
|
||||
for uid, email in sorted(self._user_email.items(), key=lambda kv: kv[1])
|
||||
]
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
"""ORM models for 岗位 (positions) and their per-resource tag bindings.
|
||||
|
||||
A **position** (岗位) is an admin-managed aggregation layer that decides which
|
||||
agents / skills / scheduled tasks / recommended questions a user is subscribed
|
||||
to. Each user has at most one position (the link lives on
|
||||
``users.position_id``, a 1:1 relation — see ``UserRow``).
|
||||
|
||||
Two tables here:
|
||||
|
||||
- ``positions`` — the position itself. ``is_default`` marks the fallback
|
||||
position inherited by users who have not been assigned one explicitly.
|
||||
- ``position_tag_bindings`` — for each ``(position, resource_type)`` the set of
|
||||
tags whose resources are granted to the position's members. **No binding for
|
||||
a resource_type means "use the default set"** (see ``deerflow.services.
|
||||
position_sync`` for what "default" resolves to per type).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import Boolean, Index, String, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.types import BeijingDateTime
|
||||
|
||||
# Resource types a position can grant. The first three reuse the existing
|
||||
# typed-tag namespace (``deerflow.persistence.tags``); ``recommended_question``
|
||||
# was added alongside (see TAG_TYPES). The removed ``user_prompt`` type is no
|
||||
# longer accepted — ``get_tag_bindings`` drops any stale rows of that type.
|
||||
POSITION_RESOURCE_TYPES: tuple[str, ...] = (
|
||||
"agent",
|
||||
"skill",
|
||||
"scheduled_task",
|
||||
"recommended_question",
|
||||
)
|
||||
|
||||
|
||||
class PositionRow(Base):
|
||||
__tablename__ = "positions"
|
||||
__table_args__ = (UniqueConstraint("name", name="uq_positions_name"),)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(191), nullable=False)
|
||||
description: Mapped[str] = mapped_column(String(1024), nullable=False, default="")
|
||||
# Exactly one position should carry ``is_default=True`` at a time (the
|
||||
# router enforces "only one default" by clearing the flag on others). Users
|
||||
# with no explicit ``users.position_id`` inherit this position's grants.
|
||||
is_default: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, index=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
|
||||
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC), onupdate=lambda: datetime.now(UTC))
|
||||
|
||||
|
||||
class PositionTagBindingRow(Base):
|
||||
"""One bound tag for a ``(position, resource_type)`` pair.
|
||||
|
||||
The absence of *any* row for a ``(position, resource_type)`` means the
|
||||
position uses the **default set** for that resource type. One or more rows
|
||||
mean the position grants the *union* of the resources carrying those tags.
|
||||
"""
|
||||
|
||||
__tablename__ = "position_tag_bindings"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("position_id", "resource_type", "tag_id", name="uq_position_tag_bindings"),
|
||||
Index("ix_position_tag_bindings_position_type", "position_id", "resource_type"),
|
||||
)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
position_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
resource_type: Mapped[str] = mapped_column(String(32), nullable=False, index=True)
|
||||
tag_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
"""SQLAlchemy-backed position + tag-binding repository."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import delete, func, select, update
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.positions.base import PositionStore
|
||||
from deerflow.persistence.positions.model import POSITION_RESOURCE_TYPES, PositionRow, PositionTagBindingRow
|
||||
from deerflow.persistence.user.model import UserRow
|
||||
|
||||
|
||||
def _position_to_dict(row: PositionRow, member_count: int = 0) -> dict[str, Any]:
|
||||
data = row.to_dict()
|
||||
for key in ("created_at", "updated_at"):
|
||||
value = data.get(key)
|
||||
if isinstance(value, datetime):
|
||||
data[key] = value.isoformat()
|
||||
data["member_count"] = member_count
|
||||
return data
|
||||
|
||||
|
||||
class PositionRepository(PositionStore):
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
self._sf = session_factory
|
||||
|
||||
@staticmethod
|
||||
async def _member_counts(session: AsyncSession, position_ids: list[str]) -> dict[str, int]:
|
||||
if not position_ids:
|
||||
return {}
|
||||
stmt = select(UserRow.position_id, func.count()).where(UserRow.position_id.in_(position_ids)).group_by(UserRow.position_id)
|
||||
result = await session.execute(stmt)
|
||||
return {pid: count for pid, count in result.all() if pid is not None}
|
||||
|
||||
@staticmethod
|
||||
async def _clear_default(session: AsyncSession, keep_id: str | None) -> None:
|
||||
stmt = update(PositionRow).where(PositionRow.is_default.is_(True))
|
||||
if keep_id is not None:
|
||||
stmt = stmt.where(PositionRow.id != keep_id)
|
||||
await session.execute(stmt.values(is_default=False))
|
||||
|
||||
# -------------------------------------------------------------- positions
|
||||
|
||||
async def list_positions(self) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
rows = list((await session.execute(select(PositionRow).order_by(PositionRow.created_at.asc()))).scalars())
|
||||
counts = await self._member_counts(session, [r.id for r in rows])
|
||||
return [_position_to_dict(r, counts.get(r.id, 0)) for r in rows]
|
||||
|
||||
async def get_position(self, position_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(PositionRow, position_id)
|
||||
if row is None:
|
||||
return None
|
||||
counts = await self._member_counts(session, [position_id])
|
||||
return _position_to_dict(row, counts.get(position_id, 0))
|
||||
|
||||
async def create_position(self, data: dict[str, Any]) -> dict[str, Any]:
|
||||
now = datetime.now(UTC)
|
||||
is_default = bool(data.get("is_default"))
|
||||
row = PositionRow(
|
||||
id=data["id"],
|
||||
name=data["name"],
|
||||
description=data.get("description") or "",
|
||||
is_default=is_default,
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
async with self._sf() as session:
|
||||
if is_default:
|
||||
await self._clear_default(session, keep_id=row.id)
|
||||
session.add(row)
|
||||
try:
|
||||
await session.commit()
|
||||
except IntegrityError as exc:
|
||||
await session.rollback()
|
||||
raise ValueError(f"Position '{data['name']}' already exists") from exc
|
||||
await session.refresh(row)
|
||||
return _position_to_dict(row, 0)
|
||||
|
||||
async def update_position(self, position_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(PositionRow, position_id)
|
||||
if row is None:
|
||||
return None
|
||||
if "name" in data and data["name"] is not None:
|
||||
row.name = data["name"]
|
||||
if "description" in data and data["description"] is not None:
|
||||
row.description = data["description"]
|
||||
if "is_default" in data and data["is_default"] is not None:
|
||||
row.is_default = bool(data["is_default"])
|
||||
if row.is_default:
|
||||
await self._clear_default(session, keep_id=position_id)
|
||||
row.updated_at = datetime.now(UTC)
|
||||
try:
|
||||
await session.commit()
|
||||
except IntegrityError as exc:
|
||||
await session.rollback()
|
||||
raise ValueError(f"Position '{data.get('name')}' already exists") from exc
|
||||
await session.refresh(row)
|
||||
counts = await self._member_counts(session, [position_id])
|
||||
return _position_to_dict(row, counts.get(position_id, 0))
|
||||
|
||||
async def delete_position(self, position_id: str) -> bool:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(PositionRow, position_id)
|
||||
if row is None:
|
||||
return False
|
||||
await session.execute(delete(PositionTagBindingRow).where(PositionTagBindingRow.position_id == position_id))
|
||||
await session.execute(update(UserRow).where(UserRow.position_id == position_id).values(position_id=None))
|
||||
await session.delete(row)
|
||||
await session.commit()
|
||||
return True
|
||||
|
||||
async def get_default_position_id(self) -> str | None:
|
||||
async with self._sf() as session:
|
||||
return (await session.execute(select(PositionRow.id).where(PositionRow.is_default.is_(True)).limit(1))).scalar_one_or_none()
|
||||
|
||||
# ------------------------------------------------------------ tag bindings
|
||||
|
||||
async def get_tag_bindings(self, position_id: str) -> dict[str, list[str]]:
|
||||
async with self._sf() as session:
|
||||
rows = list(
|
||||
(
|
||||
await session.execute(
|
||||
select(PositionTagBindingRow).where(PositionTagBindingRow.position_id == position_id).order_by(PositionTagBindingRow.created_at.asc())
|
||||
)
|
||||
).scalars()
|
||||
)
|
||||
out: dict[str, list[str]] = {}
|
||||
for r in rows:
|
||||
# Drop bindings for resource types no longer supported (e.g. the
|
||||
# removed ``user_prompt`` type) so stale rows can't poison the
|
||||
# editor or be echoed back into a 422 on save.
|
||||
if r.resource_type not in POSITION_RESOURCE_TYPES:
|
||||
continue
|
||||
out.setdefault(r.resource_type, []).append(r.tag_id)
|
||||
return out
|
||||
|
||||
async def set_tag_bindings(self, position_id: str, bindings: dict[str, list[str]]) -> dict[str, list[str]]:
|
||||
now = datetime.now(UTC)
|
||||
async with self._sf() as session:
|
||||
await session.execute(delete(PositionTagBindingRow).where(PositionTagBindingRow.position_id == position_id))
|
||||
for resource_type, tag_ids in bindings.items():
|
||||
for tag_id in dict.fromkeys(tag_ids):
|
||||
session.add(
|
||||
PositionTagBindingRow(
|
||||
id=uuid.uuid4().hex,
|
||||
position_id=position_id,
|
||||
resource_type=resource_type,
|
||||
tag_id=tag_id,
|
||||
created_at=now,
|
||||
)
|
||||
)
|
||||
await session.commit()
|
||||
return await self.get_tag_bindings(position_id)
|
||||
|
||||
# --------------------------------------------------------------- members
|
||||
|
||||
async def get_user_position_id(self, user_id: str) -> str | None:
|
||||
async with self._sf() as session:
|
||||
return (await session.execute(select(UserRow.position_id).where(UserRow.id == user_id))).scalar_one_or_none()
|
||||
|
||||
async def list_members(self, position_id: str) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
rows = (await session.execute(select(UserRow.id, UserRow.email).where(UserRow.position_id == position_id).order_by(UserRow.email.asc()))).all()
|
||||
return [{"id": uid, "email": email} for uid, email in rows]
|
||||
|
||||
async def assign_users(self, position_id: str, user_ids: list[str]) -> int:
|
||||
if not user_ids:
|
||||
return 0
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(update(UserRow).where(UserRow.id.in_(user_ids)).values(position_id=position_id))
|
||||
await session.commit()
|
||||
return result.rowcount or 0
|
||||
|
||||
async def unassign_users(self, user_ids: list[str]) -> int:
|
||||
if not user_ids:
|
||||
return 0
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(update(UserRow).where(UserRow.id.in_(user_ids)).values(position_id=None))
|
||||
await session.commit()
|
||||
return result.rowcount or 0
|
||||
|
||||
async def list_member_ids(self, position_id: str) -> list[str]:
|
||||
async with self._sf() as session:
|
||||
return [uid for (uid,) in (await session.execute(select(UserRow.id).where(UserRow.position_id == position_id))).all()]
|
||||
|
||||
async def list_assigned_user_ids(self) -> list[str]:
|
||||
async with self._sf() as session:
|
||||
return [uid for (uid,) in (await session.execute(select(UserRow.id).where(UserRow.position_id.isnot(None)))).all()]
|
||||
|
||||
async def list_all_users(self) -> list[dict[str, Any]]:
|
||||
async with self._sf() as session:
|
||||
rows = (await session.execute(select(UserRow.id, UserRow.email, UserRow.system_role, UserRow.position_id).order_by(UserRow.email.asc()))).all()
|
||||
return [{"id": uid, "email": email, "system_role": role, "position_id": pid} for uid, email, role, pid in rows]
|
||||
Reference in new issue
Block a user