feat(performance): 실적(쇼핑몰 통합 매출) 모듈 1단계 — 대시보드·설정·보안·어댑터 구조

- 네 영역 분리: ① 수집(adapters/ 쇼핑몰 10곳 + sync.py) ② 보안(security/ + credentials.py)
  ③ 계산·저장(calc.py + db.py) ④ 화면(dashboard.py, router.py, templates, performance.css/js)
- 대시보드: 기간(오늘/이번주/이번달/올해/직접선택, 이전·다음), 쇼핑몰 필터, KPI 5개(진행 중 기간은
  직전 기간 같은 경과일 대비), 일자×쇼핑몰 표(주간합계, 총매출/수수료/차감매출 전환), 일별 추이·
  쇼핑몰 비중(SVG), 월 누적(비중)·연간 누적, 금액 클릭 → 주문 상세
- 설정(관리자): 사용/매출 집계, 수수료율 이력(적용 시작일), 쇼핑몰별 API 인증정보 입력·연결 테스트·
  연결 상태, 마스터 키 상태·재암호화, 테스트(모의) 데이터 생성/삭제
- 인증정보: 설정 화면 입력 → AES-256-GCM(키 버전, AAD=쇼핑몰+항목) 암호화 저장, secret 원문은
  화면으로 다시 보내지 않음, 오류·로그·raw 는 비밀값 가린 뒤 저장. 마스터 키는 APP_ENCRYPTION_KEY(.env)
- 매출 기준일: 결제=결제일, 취소·반품=발생일. 금액 원 단위 정수, 수수료 반올림은 calc.commission 한 곳
- 동기화: 같은 쇼핑몰 동시 실행 차단(running 부분 유니크), 쇼핑몰별 실패 격리, 중복 방지 UNIQUE+UPSERT
- 카페24 어댑터는 기존 app/integrations/cafe24 OAuth 토큰 재사용, 나머지는 입력칸만(수집은 다음 단계)
- performance_db 초기화 스크립트, 권한키 performance(설정은 is_admin), 문서·.env.example

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-29 16:07:46 +09:00
parent b95206dcf4
commit aa669553db
41 changed files with 4326 additions and 1 deletions
@@ -0,0 +1,49 @@
"""카페24 — 공통 연동 계층(app/integrations/cafe24) 재사용.
카페24는 이미 OAuth 로 연결해 쓰고 있다(상품관리). 실적 설정에서 인증정보를 따로
받지 않고, 같은 토큰 저장소(cafe24_db)의 연결 상태를 그대로 보여 준다.
주문 수집(다음 단계)에는 카페24 앱 권한에 mall.read_order 추가 + 재연결이 필요하다
(app/integrations/cafe24/config.py 의 ORDER_SCOPES).
"""
from __future__ import annotations
from .base import AdapterContext, AuthFailed, MarketplaceAdapter, NotConfigured, TokenExpired
class Cafe24Adapter(MarketplaceAdapter):
code = "cafe24"
name = "카페24"
auth_type = "shared"
docs_url = "https://developers.cafe24.com/docs/api/admin/"
note = "카페24 상품관리에서 연결한 OAuth 토큰을 함께 씁니다(여기서 따로 입력하지 않음)."
def is_configured(self, ctx: AdapterContext) -> bool:
return True
def authenticate(self, ctx: AdapterContext) -> None:
st = self.shared_status(ctx)
if not st.get("connected"):
if st.get("missing"):
raise NotConfigured("카페24 연동 환경변수가 설정되지 않았습니다.")
if st.get("needs_reauth") and st.get("reason", "").startswith("refresh"):
raise TokenExpired("카페24 연결이 만료되었습니다. 카페24 상품관리 → 시스템에서 재연결하세요.")
raise AuthFailed("카페24에 연결되어 있지 않습니다. 카페24 상품관리 → 시스템에서 연결하세요.")
def shared_status(self, ctx: AdapterContext) -> dict:
from app.integrations.cafe24.config import load_config # 지연 import
from app.integrations.cafe24.tokens import TokenService # 지연 import
repo = getattr(ctx.app_state, "cafe24_store", None) if ctx.app_state is not None else None
config = load_config()
if repo is None:
return {"connected": False, "missing": config.missing or ["CAFE24_DB_URL"], "needs_reauth": False}
return TokenService(repo, config).status()
def test_connection(self, ctx: AdapterContext) -> str:
self.authenticate(ctx)
st = self.shared_status(ctx)
scopes = st.get("scopes") or ""
if "mall.read_order" not in scopes:
return "카페24 연결 정상. 주문 수집에는 주문 읽기 권한(mall.read_order) 추가가 필요합니다."
return "카페24 연결 정상."