feat(models): 组织、用户、角色、客户、会话与消息模型
按组织边界定义数据关系,补齐仓储层与通用工具,迁移脚本可重复执行。
This commit is contained in:
1 parent
977783c126
commit
9eb8613c05
126 files changed
+17436
No files matched your search
@@ -0,0 +1,86 @@
|
||||
"""
|
||||
Copyright 2024-2026 ReplyPier
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
"""
|
||||
|
||||
from typing import Iterable, List, Optional, Set
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.utils.uuids import as_uuid
|
||||
from app.models.notification_settings import (
|
||||
NOTIFICATION_PREFERENCE_DEFAULTS,
|
||||
UserNotificationSettings,
|
||||
)
|
||||
|
||||
|
||||
class UserNotificationSettingsRepository:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def get(self, user_id: UUID | str) -> Optional[UserNotificationSettings]:
|
||||
return (
|
||||
self.db.query(UserNotificationSettings)
|
||||
.filter(UserNotificationSettings.user_id == as_uuid(user_id))
|
||||
.first()
|
||||
)
|
||||
|
||||
def get_or_create(self, user_id: UUID | str) -> UserNotificationSettings:
|
||||
settings = self.get(user_id)
|
||||
if settings is None:
|
||||
settings = UserNotificationSettings(user_id=as_uuid(user_id))
|
||||
self.db.add(settings)
|
||||
try:
|
||||
self.db.commit()
|
||||
self.db.refresh(settings)
|
||||
except IntegrityError:
|
||||
# Two concurrent first-reads (e.g. two tabs) race on the
|
||||
# user_id PK; the loser re-reads the row the winner inserted
|
||||
# rather than surfacing a 500.
|
||||
self.db.rollback()
|
||||
settings = self.get(user_id)
|
||||
return settings
|
||||
|
||||
def filter_enabled(self, user_ids: Iterable[UUID | str], field: str) -> Set[UUID]:
|
||||
"""Of `user_ids`, return those who want notifications for `field`.
|
||||
|
||||
One query regardless of recipient count — transfer fans out to a whole
|
||||
group and new chats fan out across the org. Users with no row yet fall
|
||||
back to the column default rather than being dropped, so nothing has to
|
||||
be backfilled.
|
||||
"""
|
||||
if field not in NOTIFICATION_PREFERENCE_DEFAULTS:
|
||||
raise ValueError(f"Unknown notification preference: {field}")
|
||||
|
||||
candidates = {as_uuid(user_id) for user_id in user_ids if user_id}
|
||||
if not candidates:
|
||||
return set()
|
||||
|
||||
rows = (
|
||||
self.db.query(
|
||||
UserNotificationSettings.user_id,
|
||||
getattr(UserNotificationSettings, field),
|
||||
)
|
||||
.filter(UserNotificationSettings.user_id.in_(candidates))
|
||||
.all()
|
||||
)
|
||||
|
||||
stored = {user_id: enabled for user_id, enabled in rows}
|
||||
default = NOTIFICATION_PREFERENCE_DEFAULTS[field]
|
||||
return {
|
||||
user_id for user_id in candidates
|
||||
if stored.get(user_id, default)
|
||||
}
|
||||
Reference in new issue
Block a user