165 lines
6.4 KiB
Python
165 lines
6.4 KiB
Python
import uuid
|
|
|
|
from fastapi import Depends
|
|
|
|
from common.database.db_session_manager import DB_SESSION_MNG
|
|
from common.database.model.models import items
|
|
from common.enums import DBWRType, ErrorType
|
|
from common.logger import LOG
|
|
from common.models.gmodel import PageParams
|
|
from crud.item_crud import IItemCRUD, ItemCRUD
|
|
from router.v1.item.protocol import (
|
|
ItemCategory,
|
|
ItemData,
|
|
Res_CheckCodes,
|
|
Res_DeleteItem,
|
|
Res_Item,
|
|
Res_ItemCategories,
|
|
Res_ItemList,
|
|
)
|
|
|
|
|
|
class ItemService:
|
|
"""상품 비즈니스 로직. company_id 로 소유권을 확인한다(멀티테넌트)."""
|
|
|
|
def __init__(self, item_crud: IItemCRUD = Depends(ItemCRUD)):
|
|
self.item_crud = item_crud
|
|
|
|
async def _fetch_owned(self, company_uuid: uuid.UUID, item_id: uuid.UUID):
|
|
"""item 조회 + 소유권 확인. (ErrorType, item|None) 반환."""
|
|
err_type, item = await DB_SESSION_MNG.execute_lambda(
|
|
items.DBType(),
|
|
DBWRType.DB_READ.value,
|
|
lambda s: self.item_crud.get_by_id(s, item_id),
|
|
)
|
|
if err_type != ErrorType.SUCCESS or item is None:
|
|
return ErrorType.ITEM_NOT_FOUND, None
|
|
if item.company_id != company_uuid:
|
|
return ErrorType.ITEM_NOT_FOUND, None
|
|
return ErrorType.SUCCESS, item
|
|
|
|
async def list_items(self, company_id: str, search, category, pg: PageParams) -> Res_ItemList:
|
|
res = Res_ItemList(page=pg.page, size=pg.size)
|
|
company_uuid = uuid.UUID(company_id)
|
|
|
|
err_type, rows, total = await DB_SESSION_MNG.execute_lambda(
|
|
items.DBType(),
|
|
DBWRType.DB_READ.value,
|
|
lambda s: self.item_crud.search(s, company_uuid, search, category, pg.skip, pg.size),
|
|
)
|
|
if err_type != ErrorType.SUCCESS:
|
|
res.result.SetResult(err_type)
|
|
return res
|
|
res.items = [ItemData.model_validate(r) for r in rows]
|
|
res.total = total
|
|
return res
|
|
|
|
async def list_categories(self, company_id: str) -> Res_ItemCategories:
|
|
"""회사 전체 상품에서 distinct 카테고리(이름+타입)를 돌려준다. 카테고리 목록은 백엔드가 책임진다."""
|
|
res = Res_ItemCategories()
|
|
company_uuid = uuid.UUID(company_id)
|
|
err_type, rows = await DB_SESSION_MNG.execute_lambda(
|
|
items.DBType(),
|
|
DBWRType.DB_READ.value,
|
|
lambda s: self.item_crud.distinct_categories(s, company_uuid),
|
|
)
|
|
if err_type != ErrorType.SUCCESS:
|
|
res.result.SetResult(err_type)
|
|
return res
|
|
res.categories = [ItemCategory(name=r[0], category_type=r[1] if r[1] is not None else 1) for r in rows]
|
|
return res
|
|
|
|
async def get_item(self, company_id: str, item_id: str) -> Res_Item:
|
|
res = Res_Item()
|
|
err_type, item = await self._fetch_owned(uuid.UUID(company_id), uuid.UUID(item_id))
|
|
if err_type != ErrorType.SUCCESS:
|
|
res.result.SetResult(err_type)
|
|
return res
|
|
res.item = ItemData.model_validate(item)
|
|
return res
|
|
|
|
async def check_codes(self, company_id: str, codes: list) -> Res_CheckCodes:
|
|
"""업로드 즉시 호출: codes 중 같은 회사 DB 에 이미 있는 코드를 돌려준다(미리보기 사전검사)."""
|
|
res = Res_CheckCodes()
|
|
company_uuid = uuid.UUID(company_id)
|
|
err_type, existing = await DB_SESSION_MNG.execute_lambda(
|
|
items.DBType(),
|
|
DBWRType.DB_READ.value,
|
|
lambda s: self.item_crud.existing_codes(s, company_uuid, codes),
|
|
)
|
|
if err_type != ErrorType.SUCCESS:
|
|
res.result.SetResult(err_type)
|
|
return res
|
|
res.existing = list(existing)
|
|
return res
|
|
|
|
async def create_item(self, company_id: str, user_id: str, data: dict) -> Res_Item:
|
|
res = Res_Item()
|
|
company_uuid = uuid.UUID(company_id)
|
|
|
|
# DB 중복코드 검증: 같은 회사에 동일 code 가 이미 있으면 거부(프론트는 받아온 목록만 보므로 여기서 최종 차단).
|
|
code = data.get("code")
|
|
if code:
|
|
dup_err, exists = await DB_SESSION_MNG.execute_lambda(
|
|
items.DBType(),
|
|
DBWRType.DB_READ.value,
|
|
lambda s: self.item_crud.code_exists(s, company_uuid, code),
|
|
)
|
|
if dup_err != ErrorType.SUCCESS:
|
|
res.result.SetResult(dup_err)
|
|
return res
|
|
if exists:
|
|
res.result.SetResult(ErrorType.ITEM_CODE_DUPLICATE)
|
|
return res
|
|
|
|
item = items(**data, company_id=company_uuid, user_id=uuid.UUID(user_id))
|
|
err_type = await DB_SESSION_MNG.execute_lambda_run(
|
|
[items.DBType()],
|
|
[lambda s: self.item_crud.add_item(s, item)],
|
|
)
|
|
if err_type != ErrorType.SUCCESS:
|
|
res.result.SetResult(err_type)
|
|
return res
|
|
# 서버 기본값(created_at/updated_at)은 insert 후 Python 객체에 실리지 않으므로 재조회한다.
|
|
return await self.get_item(company_id, str(item.item_id))
|
|
|
|
async def update_item(self, company_id: str, item_id: str, data: dict) -> Res_Item:
|
|
res = Res_Item()
|
|
company_uuid = uuid.UUID(company_id)
|
|
item_uuid = uuid.UUID(item_id)
|
|
|
|
# 소유권 확인
|
|
err_type, _ = await self._fetch_owned(company_uuid, item_uuid)
|
|
if err_type != ErrorType.SUCCESS:
|
|
res.result.SetResult(err_type)
|
|
return res
|
|
|
|
err_type = await DB_SESSION_MNG.execute_lambda_run(
|
|
[items.DBType()],
|
|
[lambda s: self.item_crud.update_item(s, item_uuid, data)],
|
|
)
|
|
if err_type != ErrorType.SUCCESS:
|
|
res.result.SetResult(err_type)
|
|
return res
|
|
|
|
# 갱신 후 재조회
|
|
return await self.get_item(company_id, item_id)
|
|
|
|
async def delete_item(self, company_id: str, item_id: str) -> Res_DeleteItem:
|
|
res = Res_DeleteItem()
|
|
company_uuid = uuid.UUID(company_id)
|
|
item_uuid = uuid.UUID(item_id)
|
|
|
|
err_type, _ = await self._fetch_owned(company_uuid, item_uuid)
|
|
if err_type != ErrorType.SUCCESS:
|
|
res.result.SetResult(err_type)
|
|
return res
|
|
|
|
err_type = await DB_SESSION_MNG.execute_lambda_run(
|
|
[items.DBType()],
|
|
[lambda s: self.item_crud.soft_delete(s, item_uuid)],
|
|
)
|
|
if err_type != ErrorType.SUCCESS:
|
|
res.result.SetResult(err_type)
|
|
return res
|