๐๏ธ Coupon Service
1. ์ฟ ํฐ ์์ฑ (๊ด๋ฆฌ์)
sequenceDiagram
actor Admin
participant Gateway
participant CouponService
participant CouponDB
participant Redis
Admin->>Gateway: POST /api/v1/coupons
Gateway->>CouponService: ์์ฒญ ์ ๋ฌ (X-User-Role: ROLE_ADMIN)
CouponService->>CouponDB: p_coupons ์ ์ฅ
CouponDB-->>CouponService: couponId ๋ฐํ
CouponService->>Redis: ์ฌ๊ณ ์ธํ
SET coupon:stock:{couponId} totalQuantity
Redis-->>CouponService: ์๋ฃ
CouponService-->>Admin: 201 Created
2. ์ ์ฐฉ์ ์ฟ ํฐ ๋ฐ๊ธ
sequenceDiagram
actor Client
participant Gateway
participant CouponService
participant Redis
participant CouponDB
participant OutboxDB
participant Kafka
Client->>Gateway: POST /api/v1/coupons/{couponId}/issue
Gateway->>CouponService: ์์ฒญ ์ ๋ฌ (X-User-Id ํค๋)
CouponService->>Redis: ์ค๋ณต ๋ฐ๊ธ ํ์ธ SISMEMBER coupon:issued:{couponId} userId
alt ์ด๋ฏธ ๋ฐ๊ธ๋จ
Redis-->>CouponService: true
CouponService-->>Client: 409 ์ด๋ฏธ ๋ฐ๊ธ๋ ์ฟ ํฐ
end
CouponService->>Redis: ์ฌ๊ณ ์ฐจ๊ฐ DECR coupon:stock:{couponId}
alt ์ฌ๊ณ ์์ (0 ์ดํ)
Redis-->>CouponService: 0 ์ดํ
CouponService->>Redis: INCR ๋ณต๊ตฌ
CouponService-->>Client: 409 ํ์
end
Redis-->>CouponService: ๋จ์ ์ฌ๊ณ ๋ฐํ
CouponService->>Redis: ๋ฐ๊ธ ๊ธฐ๋ก SADD coupon:issued:{couponId} userId
Note over CouponService,OutboxDB: ๊ฐ์ ํธ๋์ญ์
CouponService->>CouponDB: p_user_coupons ์ ์ฅ
CouponService->>OutboxDB: p_coupon_outbox ์ ์ฅ (PENDING)
CouponDB-->>CouponService: ์ ์ฅ ์๋ฃ
CouponService-->>Client: 201 Created
CouponService->>Kafka: ์์๋ฐ์ค ์ค์ผ์ค๋ฌ โ COUPON_ISSUED ์ด๋ฒคํธ ๋ฐํ
3. ์ฃผ๋ฌธ ์ ์ฟ ํฐ ์ฌ์ฉ (INTERNAL)
sequenceDiagram
participant OrderService
participant CouponService
participant CouponDB
OrderService->>CouponService: POST /api/v1/internal/coupons/{userCouponId}/use
CouponService->>CouponDB: ์ฟ ํฐ ์กฐํ
alt ์ฟ ํฐ ์์
CouponService-->>OrderService: 404 Not Found
end
alt ์์ ์ ๋ถ์ผ์น
CouponService-->>OrderService: 403 Forbidden
end
alt ๋ง๋ฃ๋ ์ฟ ํฐ (expired_at < NOW())
CouponService-->>OrderService: 410 Gone
end
alt ์ด๋ฏธ ์ฌ์ฉ๋ ์ฟ ํฐ
CouponService-->>OrderService: 409 Conflict
end
CouponDB-->>CouponService: ์ฟ ํฐ ์ ๋ณด ๋ฐํ
CouponService->>CouponDB: status = USED ๋ณ๊ฒฝ
CouponDB-->>CouponService: ์๋ฃ
CouponService-->>OrderService: 200 OK (discountType, discountValue, maxDiscountAmount, discountAmount, finalPrice)
Note over OrderService: Order Service๊ฐ ํ ์ธ ๊ธ์ก ๊ณ์ฐ
4. ์ฟ ํฐ ๋ณต๊ตฌ - SAGA ๋ณด์ ํธ๋์ญ์
sequenceDiagram
participant Kafka
participant CouponService
participant CouponDB
participant ProcessedEventsDB
Kafka->>CouponService: COUPON_RESTORE ์ด๋ฒคํธ ์์
CouponService->>ProcessedEventsDB: ๋ฉฑ๋ฑ์ฑ ํ์ธ (event_id ์กฐํ)
alt ์ด๋ฏธ ์ฒ๋ฆฌ๋ ์ด๋ฒคํธ
CouponService-->>Kafka: ์คํต
end
CouponService->>CouponDB: ์ฟ ํฐ ์กฐํ
alt ๋ง๋ฃ๋ ์ฟ ํฐ
CouponService->>ProcessedEventsDB: event_id ์ ์ฅ
Note over CouponService: ๋ณต๊ตฌ ๋ถ๊ฐ ๋ก๊ทธ ๊ธฐ๋ก
end
CouponService->>CouponDB: status = UNUSED ๋ณต๊ตฌ
CouponDB-->>CouponService: ์๋ฃ
CouponService->>ProcessedEventsDB: event_id ์ ์ฅ
5. ๋ง๋ฃ ์ค์ผ์ค๋ฌ
sequenceDiagram
participant Scheduler
participant CouponDB
Note over Scheduler: ๋งค์ผ ์์ ์คํ
Scheduler->>CouponDB: UPDATE p_user_coupons SET status = EXPIRED
Note over Scheduler,CouponDB: WHERE status = UNUSED AND expired_at < NOW()
CouponDB-->>Scheduler: ์๋ฃ
6. Redis ๋ณต๊ตฌ ๋ก์ง (์๋ฒ ์ฌ์์ ์)
sequenceDiagram
participant CouponService
participant CouponDB
participant Redis
Note over CouponService: @PostConstruct ์๋ฒ ์์ ์
CouponService->>CouponDB: ์ ์ฒด ์ฟ ํฐ ์กฐํ
loop ์ฟ ํฐ๋ณ
CouponService->>CouponDB: COUNT(p_user_coupons) WHERE coupon_id = ?
CouponDB-->>CouponService: ๋ฐ๊ธ๋ ์ ๋ฐํ
CouponService->>Redis: SET coupon:stock:{couponId} (totalQuantity - issued)
end
Redis-->>CouponService: ์ฌ์ธํ
์๋ฃ