Files
replypier/backend/app/repositories/rating.py
T
WangShen007 54b949b392 chore(brand): rename the product to ReplyPier
The upstream project name is gone from code, assets, docs and build
output: brand strings, domains (replypier.site and its app/api/docs/help
subdomains), env prefixes, image and network names, the widget's public
JS API (window.ReplyPier, replypierId/BaseUrl/Config/Token), CSS tokens
(--rp-*), file and directory names, and the WordPress plugin package.

- widget loader is now webclient/replypier.min.js; both bundles rebuilt
  from source (backend/assets/widget.{js,css}, frontend/public/webclient)
- NOTICE, llms.txt, CONTRIBUTING, HELP_CENTER_INFRA and config.dev.js
  rewritten rather than string-swapped; the demo config no longer ships
  the previous vendor's Firebase/Razorpay/Google keys
- new migration replypier_ai_model_001 adds REPLYPIER to the managed
  AIModelType and rewrites pre-rename rows
- local compose project, database, role and volume renamed; LOCAL_DEPLOY
  documents carrying the old local data over
2026-10-26 00:51:03 +08:00

97 lines
3.9 KiB
Python

"""
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 sqlalchemy.orm import Session
from app.models.rating import Rating
from typing import Optional, List
from uuid import UUID
from sqlalchemy import func
class RatingRepository:
def __init__(self, db: Session):
self.db = db
def create_rating(self, session_id: UUID, customer_id: UUID, user_id: UUID, agent_id: UUID,
organization_id: UUID, rating: int, feedback: Optional[str] = None) -> Rating:
"""Create a new rating"""
db_rating = Rating(
session_id=session_id,
customer_id=customer_id,
user_id=user_id,
agent_id=agent_id,
organization_id=organization_id,
rating=rating,
feedback=feedback
)
self.db.add(db_rating)
self.db.commit()
self.db.refresh(db_rating)
return db_rating
def upsert_rating(self, session_id: UUID, customer_id: UUID, user_id: UUID, agent_id: UUID,
organization_id: UUID, rating: int, feedback: Optional[str] = None) -> Rating:
"""Record the customer's rating for a session, replacing any earlier one.
One row per session: the widget can re-show the rating prompt, and
without this a customer could pile up rows that skew agent/org averages
and, since the score now flows onto the ticket, spam its activity feed.
"""
existing = self.get_rating_by_session(session_id)
if existing is None:
return self.create_rating(
session_id=session_id, customer_id=customer_id, user_id=user_id,
agent_id=agent_id, organization_id=organization_id,
rating=rating, feedback=feedback,
)
existing.rating = rating
existing.feedback = feedback
self.db.commit()
self.db.refresh(existing)
return existing
def get_rating_by_session(self, session_id: UUID) -> Optional[Rating]:
"""Get rating by session ID"""
return self.db.query(Rating).filter(Rating.session_id == session_id).first()
def get_ratings_by_agent(self, agent_id: UUID) -> List[Rating]:
"""Get all ratings for an agent"""
return self.db.query(Rating).filter(Rating.agent_id == agent_id).all()
def get_ratings_by_customer(self, customer_id: UUID) -> List[Rating]:
"""Get all ratings from a customer"""
return self.db.query(Rating).filter(Rating.customer_id == customer_id).all()
def get_average_rating_by_agent(self, agent_id: UUID) -> float:
"""Get average rating for an agent"""
result = self.db.query(func.avg(Rating.rating)).filter(
Rating.agent_id == agent_id
).scalar()
return float(result) if result else 0.0
def get_organization_ratings(self, organization_id: UUID,
limit: int = 100, offset: int = 0) -> List[Rating]:
"""Get all ratings for an organization with pagination"""
return self.db.query(Rating).filter(
Rating.organization_id == organization_id
).order_by(Rating.created_at.desc()).offset(offset).limit(limit).all()
def get_organization_average_rating(self, organization_id: UUID) -> float:
"""Get average rating for an organization"""
result = self.db.query(func.avg(Rating.rating)).filter(
Rating.organization_id == organization_id
).scalar()
return float(result) if result else 0.0