from sqlalchemy.ext.asyncio import AsyncSession from app.core.branding_images import BrandingImageError, resolve_image_field from app.core.oem_resolve import get_or_create_oem_for_user, resolve_oem_id_for_user_id from app.models.oem import Oem from app.schemas.admin_oem_branding import OemBrandingUpdate class AdminOemBrandingError(Exception): def __init__(self, message: str) -> None: self.message = message super().__init__(message) def _map_image_error(exc: BrandingImageError) -> AdminOemBrandingError: return AdminOemBrandingError(exc.message) async def get_or_create_default_oem(db: AsyncSession, *, user_id: int) -> Oem: try: return await get_or_create_oem_for_user(db, user_id) except ValueError as exc: raise AdminOemBrandingError(str(exc)) from exc async def get_default_oem_branding(db: AsyncSession, *, user_id: int) -> Oem: return await get_or_create_default_oem(db, user_id=user_id) async def update_default_oem_branding( db: AsyncSession, body: OemBrandingUpdate, *, user_id: int, ) -> Oem: await resolve_oem_id_for_user_id(db, user_id) oem = await get_or_create_default_oem(db, user_id=user_id) try: logo_path = resolve_image_field( body.logo_path, oem.logo_path, file_prefix="logo" ) wechat_qrcode = resolve_image_field( body.wechat_qrcode, oem.wechat_qrcode, file_prefix="wechat_qr" ) qq_qrcode = resolve_image_field( body.qq_qrcode, oem.qq_qrcode, file_prefix="qq_qr" ) except BrandingImageError as exc: raise _map_image_error(exc) from exc oem.software_name = body.software_name oem.logo_path = logo_path oem.wechat = body.wechat oem.qq = body.qq oem.wechat_qrcode = wechat_qrcode oem.qq_qrcode = qq_qrcode await db.flush() await db.refresh(oem) return oem