feat(performance): 광고비 탭 [광고비 업데이트] 진행 창·자동 업데이트·업데이트 시각

- 채널별 [구글/메타 광고 불러오기] 버튼을 없애고 매출 탭과 같은 모양으로
  마지막 업데이트 · 다음 업데이트까지(카운트다운) · [광고비 업데이트]
- 진행 창 작업에 종류(sales/ads) 추가 — 광고 채널별 진행 막대(수집 → 저장
  → 완료), 채널별 중지·전체 취소는 저장 직전까지, 진행 중이면 합류
- POST /api/ad-sync, GET /api/ad-sync/current
- 자동 업데이트 회차에서 매출과 광고비를 동시에 각각 진행 창 작업으로
- 마지막 광고비 업데이트 = 광고 API 마지막 성공 가져오기 시각

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-20 13:44:45 +09:00
parent 18b3ae007d
commit 232695c004
12 changed files with 423 additions and 106 deletions
@@ -315,3 +315,93 @@ def test_sync_endpoint(monkeypatch):
svc.conf = {"google": False}
r = client.post("/performance/api/ad-sources/google/sync", json={})
assert r.status_code == 400 and "설정" in r.json()["detail"]
def test_sync_all_with_job_reports_channel_progress(monkeypatch):
"""[광고비 업데이트](2026-10-20) — 채널별 진행 막대: 완료(일·광고비)·실패·중지(저장 안 함)."""
from app.modules.performance import jobs
from app.modules.performance.ad_sources.base import AdDay
store = FakeStore()
src = ga.GoogleAdsSource()
monkeypatch.setattr(src, "fetch_days", lambda ctx, s, e: [AdDay(date(2026, 10, 1), 1000, None, {}),
AdDay(date(2026, 10, 2), 2000, None, {})])
monkeypatch.setattr(ad_sync, "get_source", lambda code: src)
svc = _service(store, src)
job = jobs.create_job(user="king@x", start=None, end=None, kind="ads")
res = svc.sync_all(date(2026, 10, 1), date(2026, 10, 2), user="king@x", job=job)
assert res[0]["ok"] and job.kind == "ads"
shop = job.snapshot()["shops"][0]
assert shop["state"] == "done" and shop["upserted"] == 2 and shop["message"] == "광고비 ₩3,000" and shop["fraction"] == 1.0
# 수집 뒤 저장 직전에 [중지] → 저장하지 않음
job2 = jobs.create_job(user="king@x", start=None, end=None, kind="ads")
def fetch_then_stop(ctx, s, e):
job2.stop_shop("google")
return [AdDay(date(2026, 10, 1), 1000, None, {})]
monkeypatch.setattr(src, "fetch_days", fetch_then_stop)
before = len(store.replaced)
res = svc.sync_all(date(2026, 10, 1), date(2026, 10, 1), job=job2)
assert res[0].get("cancelled") and len(store.replaced) == before
assert job2.snapshot()["shops"][0]["state"] == "cancelled"
# 실패 → 막대에 오류(상태·문구)
def boom(ctx, s, e):
raise AuthFailed("거부")
monkeypatch.setattr(src, "fetch_days", boom)
job3 = jobs.create_job(user="king@x", start=None, end=None, kind="ads")
svc.sync_all(date(2026, 10, 1), date(2026, 10, 1), job=job3)
s3 = job3.snapshot()["shops"][0]
assert s3["state"] == "error" and s3["status"] == "auth_failed"
for j in (job, job2, job3):
j.finish()
def test_ad_sync_job_endpoint(monkeypatch):
import importlib
from fastapi import FastAPI
from fastapi.testclient import TestClient
from app.modules.performance import common, jobs
perf_router = importlib.import_module("app.modules.performance.router")
ran = []
class Svc:
conf = {"google": True, "meta": False}
def configured(self):
return self.conf
monkeypatch.setattr(common, "require_store", lambda request, admin=False: (object(), {"email": "king@x"}))
monkeypatch.setattr(perf_router, "today_kst", lambda: date(2026, 10, 15))
monkeypatch.setattr(perf_router, "_ad_services", lambda store: (None, Svc()))
monkeypatch.setattr(ad_sync, "run_ad_job", lambda store, ring, job, s, e, user="": ran.append((job.id, s, e, user)))
app = FastAPI()
app.include_router(perf_router.router)
client = TestClient(app)
assert client.get("/performance/api/ad-sync/current").json() == {"job": None}
r = client.post("/performance/api/ad-sync", json={"from": "2026-10-01", "to": "2026-10-31"})
body = r.json()
assert r.status_code == 200 and not body["joined"] and body["job"]["kind"] == "ads"
assert body["job"]["to"] == "2026-10-15" # 오늘 뒤는 잘라냄
import time
for _ in range(50):
if ran:
break
time.sleep(0.02)
assert ran and ran[0][1:] == (date(2026, 10, 1), date(2026, 10, 15), "king@x")
# 진행 중이면 새로 시작하지 않고 합류(매출 업데이트와 별개)
r = client.post("/performance/api/ad-sync", json={})
assert r.json()["joined"] and r.json()["job"]["id"] == body["job"]["id"]
assert client.get("/performance/api/ad-sync/current").json()["job"]["id"] == body["job"]["id"]
assert jobs.running_job("sales") is None or jobs.running_job("sales").kind == "sales"
jobs.get_job(body["job"]["id"]).finish()
Svc.conf = {"google": False}
r = client.post("/performance/api/ad-sync", json={})
assert r.status_code == 400 and "광고 API" in r.json()["detail"]