o2o-negosium-original/negodata/backend/services/company_settings_service.py

46 lines
1.8 KiB
Python

import uuid
from fastapi import Depends
from common.database.db_session_manager import DB_SESSION_MNG
from common.database.model.models import companies
from common.enums import DBWRType, ErrorType
from crud.user_crud import IUserCRUD, UserCRUD
from router.v1.company.protocol import Req_UpdateCompanySettings, Res_CompanySettings
class CompanySettingsService:
"""회사별 커스터마이징 설정(companies.settings JSONB) 조회/수정.
- 조회는 로그인 유저 전원(브랜딩/라벨을 앱 부팅 시 로드), 수정은 라우터에서 RequireOwner 로 게이트.
- company_id 는 토큰값만 쓴다 → 남의 회사 설정 접근 불가.
"""
def __init__(self, user_crud: IUserCRUD = Depends(UserCRUD)):
self.user_crud = user_crud
async def get_settings(self, company_id: str) -> Res_CompanySettings:
res = Res_CompanySettings()
err_type, company = await DB_SESSION_MNG.execute_lambda(
companies.DBType(),
DBWRType.DB_READ.value,
lambda s: self.user_crud.get_company(s, uuid.UUID(company_id)),
)
if err_type != ErrorType.SUCCESS or company is None:
res.result.SetResult(err_type if err_type != ErrorType.SUCCESS else ErrorType.ACCOUNT_NOT_FOUND)
return res
res.settings = company.settings
return res
async def update_settings(self, company_id: str, req: Req_UpdateCompanySettings) -> Res_CompanySettings:
res = Res_CompanySettings()
err_type = await DB_SESSION_MNG.execute_lambda_run(
[companies.DBType()],
[lambda s: self.user_crud.update_company_settings(s, uuid.UUID(company_id), req.settings)],
)
if err_type != ErrorType.SUCCESS:
res.result.SetResult(err_type)
return res
res.settings = req.settings
return res