[refactor] negodata/backend: 서비스 계층 입력을 dict→타입드 Req 패킷으로 통일

라우터가 req.model_dump(exclude_unset=True)로 dict를 만들어 넘기던 것을
req(Req_*) 객체 그대로 전달하도록 변경. 서비스 시그니처를 전부 타입드로 통일.

- create 5개(quotation/card/item/supplier/quotation_setting): ORM은 명시 kwargs
  조립(item만 컬럼 16개라 model_dump 펼침). 경계 검증·타입 유지, **data 결합 제거.
- update 4개: 서비스가 req 받아 내부에서 model_dump(exclude_unset=True) 생성 후
  CRUD(dict)로 전달 — 공식 PATCH 메커니즘 유지.
- 서비스 계층 data: dict 시그니처 0개. CRUD는 dict 유지(의도).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
minaChoiandClaude Opus 4.8 committed 2026-06-23 09:27:04 +09:00
1 parent fd2e5c1cc2
commit d47476dc04
10 files changed
+97 -79

No files matched your search

+35 -47
View File
@@ -11,11 +11,11 @@ from common.utils.gtime import GTime
from config.server_configs import web_server_config
from crud.quotation_crud import IQuotationCRUD, QuotationCRUD
from router.v1.quotation.protocol import (
AsyncJob,
ChatMessageData,
QuotationCardData,
QuotationData,
SessionData,
Req_CreateQuotation,
Res_CreateQuotation,
Res_DeleteQuotation,
Res_Quotation,
@@ -133,28 +133,22 @@ class QuotationService:
res.quotation = QuotationData.model_validate(quotation)
return res
async def create_quotation(self, user_id: str, data: dict) -> Res_CreateQuotation:
async def create_quotation(self, user_id: str, req: Req_CreateQuotation) -> Res_CreateQuotation:
res = Res_CreateQuotation()
# 세션 생성용 입력은 quotations 컬럼이 아니므로 분리한다(상품 × 공급사 조합마다 세션 1개).
item_ids = data.pop("item_ids", []) or []
supplier_ids = data.pop("supplier_ids", []) or []
card_ids = data.pop("card_ids", []) or []
item_ids = req.item_ids
supplier_ids = req.supplier_ids
card_ids = req.card_ids
# NOT NULL 컬럼 보정(프론트 미전송 시 서버 디폴트).
if not data.get("version_id"):
data["version_id"] = self.DEFAULT_VERSION_ID
if not data.get("start_time"):
data["start_time"] = GTime.UTC()
if not data.get("number"):
data["number"] = self._gen_number()
if not data.get("status"):
data["status"] = QuotationStatus.CREATED.value
if not data.get("round"):
data["round"] = 1 # ORM 기본값은 flush 시점이라, 세션 스냅샷용으로 미리 확정한다
version_id = req.version_id or self.DEFAULT_VERSION_ID
number = req.number or self._gen_number()
status = req.status or QuotationStatus.CREATED.value
round_ = req.round or 1 # ORM 기본값은 flush 시점이라, 세션 스냅샷용으로 미리 확정한다
# DB 컬럼이 naive 라, tz-aware 로 들어온 시각(프론트 toISOString)을 UTC naive 로 맞춘다.
data["start_time"] = self._naive_utc(data.get("start_time"))
data["end_time"] = self._naive_utc(data.get("end_time"))
start_time = self._naive_utc(req.start_time or GTime.UTC())
end_time = self._naive_utc(req.end_time)
# 세션 목표가 입력(상품 단가 + 견적 세팅 목표 마진율). 읽기 트랜잭션에서 먼저 조회.
prices = {}
@@ -168,7 +162,7 @@ class QuotationService:
_err, margin = await DB_SESSION_MNG.execute_lambda(
quotations.DBType(),
DBWRType.DB_READ.value,
lambda s: self.quotation_crud.get_target_margin(s, data["qt_setting_id"]),
lambda s: self.quotation_crud.get_target_margin(s, req.qt_setting_id),
)
margin = margin if _err == ErrorType.SUCCESS else None
@@ -188,7 +182,7 @@ class QuotationService:
version_id=new_version_id,
user_id=uuid.UUID(user_id),
code=0,
name=(data.get("number") or "견적버전")[:10],
name=(number or "견적버전")[:10],
)
for cid in card_ids:
t = card_types.get(cid)
@@ -196,11 +190,27 @@ class QuotationService:
link_rows.append(version_nego_cards(version_id=new_version_id, nego_card_id=cid))
elif t == 2:
link_rows.append(version_wild_cards(version_id=new_version_id, wild_card_id=cid))
data["version_id"] = new_version_id
version_id = new_version_id
# qt_id 를 미리 발급해 세션 FK(quotation_id)와 묶고, 한 트랜잭션에 함께 insert 한다.
qt_id = uuid.uuid4()
quotation = quotations(**data, qt_id=qt_id, user_id=uuid.UUID(user_id))
quotation = quotations(
qt_id=qt_id,
user_id=uuid.UUID(user_id),
qt_setting_id=req.qt_setting_id,
version_id=version_id,
name=req.name,
number=number,
type=req.type,
status=status,
round=round_,
start_time=start_time,
end_time=end_time,
manager_name=req.manager_name,
manager_email=req.manager_email,
manager_contact_number=req.manager_contact_number,
memo=req.memo,
)
session_objs = []
for iid in item_ids:
@@ -236,32 +246,10 @@ class QuotationService:
res.result.SetResult(err_type)
return res
# 서버 기본값(created_at/updated_at) 로드 위해 재조회.
f_err, fresh = await DB_SESSION_MNG.execute_lambda(
quotations.DBType(),
DBWRType.DB_READ.value,
lambda s: self.quotation_crud.get_by_id(s, qt_id),
)
res.quotation = QuotationData.model_validate(fresh if f_err == ErrorType.SUCCESS and fresh is not None else quotation)
# 생성된 세션 + 각 세션 chat 실행 URL 을 함께 반환(협상리스트에서 바로 진입 가능).
res.sessions = [
SessionData(
session_id=so.session_id,
qt_id=so.quotation_id,
supplier_id=so.supplier_id,
item_id=so.item_id,
qt_number=so.qt_number,
qt_round=so.qt_round,
qt_type=so.qt_type,
target_price=so.target_price,
status=so.status,
end_time=so.end_time,
url=self._session_chat_url(so.session_id),
)
for so in session_objs
]
res.async_job = AsyncJob(status="created", message=f"협상 세션 {len(session_objs)}건 생성")
# 프론트는 생성 응답 본문을 화면에 쓰지 않고 qt_id 로 목록/상세를 재조회한다.
# 그래서 새 견적 id 와 세션 수만 돌려준다(재조회 get_by_id·세션 풀바디·url 생략).
res.qt_id = qt_id
res.session_count = len(session_objs)
return res
async def stop_quotation(self, qt_id: str) -> Res_Quotation: