forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsat.py
More file actions
73 lines (60 loc) · 2.38 KB
/
Copy pathcsat.py
File metadata and controls
73 lines (60 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from database import csat
from utils.other import endpoints as auth
router = APIRouter(tags=['csat'])
class CsatConfigResponse(BaseModel):
enabled: bool
title: str
body: str
thank_you_text: str
refer_cta_text: str
question_threshold: int
comment_max_score: int
revision: int
class CsatRatingReceipt(BaseModel):
id: str
created: bool
class CsatRatingRequest(BaseModel):
platform: str
app_version: str = ''
score: int
comment: Optional[str] = None
revision: int = 0
@router.get('/v1/csat/config', response_model=CsatConfigResponse)
def get_csat_config(
platform: str = 'macos',
uid: str = Depends(auth.get_current_user_uid),
) -> CsatConfigResponse:
# `platform` is accepted and reserved so Windows/iOS/Android callers can
# attach later without a contract change; v1 serves the same product-wide
# singleton for every platform. A missing doc returns defaults — never 404.
return CsatConfigResponse(**csat.get_product_config())
@router.post('/v1/csat/ratings', response_model=CsatRatingReceipt, status_code=201)
def submit_csat_rating(
payload: CsatRatingRequest,
uid: str = Depends(auth.get_current_user_uid),
):
if payload.platform not in csat.PLATFORMS:
raise HTTPException(status_code=400, detail=f'platform must be one of {sorted(csat.PLATFORMS)}')
if not 1 <= payload.score <= 5:
raise HTTPException(status_code=400, detail='score must be between 1 and 5')
if payload.revision < 0:
raise HTTPException(status_code=400, detail='revision must be >= 0')
app_version = payload.app_version.strip()[: csat.MAX_APP_VERSION_LENGTH]
comment = (payload.comment or '').strip()[: csat.MAX_COMMENT_LENGTH]
# The comment is never logged; only the clamped fields above travel on.
doc_id, created = csat.submit_rating(
uid=uid,
platform=payload.platform,
app_version=app_version,
score=payload.score,
comment=comment,
revision=payload.revision,
)
if not created:
# One rating per user per platform; the existing answer stands.
return JSONResponse(status_code=409, content={'id': doc_id, 'created': False})
return CsatRatingReceipt(id=doc_id, created=True)