11package com .swyp .point .controller ;
22import com .swyp .point .dto .PointAddRequest ;
33import com .swyp .point .dto .PointBalanceResponse ;
4- import com .swyp .point .dto .PointDedeductRequest ;
4+ import com .swyp .point .dto .PointDeductRequest ;
5+ import com .swyp .point .enums .PointType ;
56import com .swyp .point .service .PointService ;
67import com .swyp .social_login .entity .AuthUser ;
78import com .swyp .social_login .repository .UserRepository ;
@@ -109,44 +110,84 @@ public ResponseEntity<Map<String, Object>> addPoints(
109110 //포인트 차감
110111 @ Operation (
111112 summary = "포인트 차감" ,
112- description = "사용자의 포인트를 차감합니다 ." ,
113+ description = "사용자의 포인트를 차감하고 선택한 쿠폰(스타벅스, 편의점 등)을 발급합니다 ." ,
113114 responses = {
114115 @ ApiResponse (
115116 responseCode = "200" ,
116- description = "포인트 차감 성공" ,
117+ description = "포인트 차감 및 쿠폰 발급 성공" ,
117118 content = @ Content (
118119 mediaType = "application/json" ,
119- schema = @ Schema (example = "{\" status\" : \" success\" , \" message\" : \" 포인트가 차감됨 \" , \" totalPoints\" : 450}" )
120+ schema = @ Schema (example = "{\" status\" : \" success\" , \" message\" : \" 포인트가 차감되었습니다 \" , \" totalPoints\" : 450}" )
120121 )
121122 ),
122123 @ ApiResponse (
123124 responseCode = "400" ,
124- description = "포인트가 부족함 " ,
125+ description = "포인트가 부족하거나 쿠폰 발급 조건 불충족 " ,
125126 content = @ Content (
126127 mediaType = "application/json" ,
127- schema = @ Schema (example = "{\" status\" : \" fail\" , \" message\" : \" 포인트가 부족함 \" }" )
128+ schema = @ Schema (example = "{\" status\" : \" fail\" , \" message\" : \" 포인트가 부족합니다. 현재 포인트: 1000, 필요 포인트: 5000 \" }" )
128129 )
130+ ),
131+ @ ApiResponse (
132+ responseCode = "404" ,
133+ description = "사용자를 찾을 수 없음"
134+ ),
135+ @ ApiResponse (
136+ responseCode = "500" ,
137+ description = "서버 내부 오류"
129138 )
130139 }
131140 )
132141 @ PostMapping ("/{userId}/deduct" )
133142 public ResponseEntity <Map <String , Object >> deductPoints (
134143 @ PathVariable ("userId" ) Long pathUserId ,
135- @ RequestBody PointDedeductRequest pointRequest ,
144+ @ RequestBody PointDeductRequest pointRequest ,
136145 HttpServletRequest request ) {
137146 try {
138147 // userId로 사용자 찾기
139148 AuthUser authUser = findAuthUser (pathUserId );
140149
141150 try {
151+ // 쿠폰 타입 검증 및 포인트 검증
152+ if (pointRequest .getPointType () != null &&
153+ (pointRequest .getPointType () == PointType .GIFT_STARBUCKS ||
154+ pointRequest .getPointType () == PointType .GIFT_COUPON )) {
155+
156+ // 스타벅스 쿠폰은 최소 5,000 포인트 필요
157+ if (pointRequest .getPointType () == PointType .GIFT_STARBUCKS && pointRequest .getPoints () < 5000 ) {
158+ Map <String , Object > response = new HashMap <>();
159+ response .put ("status" , "fail" );
160+ response .put ("message" , "스타벅스 쿠폰은 최소 5,000 포인트가 필요합니다." );
161+ return ResponseEntity .badRequest ().body (response );
162+ }
163+
164+ // 편의점 쿠폰은 최소 10,000 포인트 필요
165+ if (pointRequest .getPointType () == PointType .GIFT_COUPON && pointRequest .getPoints () < 10000 ) {
166+ Map <String , Object > response = new HashMap <>();
167+ response .put ("status" , "fail" );
168+ response .put ("message" , "편의점 쿠폰은 최소 10,000 포인트가 필요합니다." );
169+ return ResponseEntity .badRequest ().body (response );
170+ }
171+ }
172+
142173 // 포인트 차감 처리
143174 boolean success = pointService .deductPoints (authUser , pointRequest .getPoints (), pointRequest .getPointType (), pointRequest .getDescription (), pointRequest .getGoalId ());
175+
144176 if (success ) {
145177 int updatedPoints = pointService .getUserPoints (authUser );
146178 Map <String , Object > response = new HashMap <>();
147- response .put ("status" , "success" );
148- response .put ("message" , "포인트가 성공적으로 차감되었습니다" );
149- response .put ("totalPoints" , updatedPoints );
179+
180+ if (pointRequest .getPointType () == PointType .GIFT_STARBUCKS || pointRequest .getPointType () == PointType .GIFT_COUPON ) {
181+ response .put ("status" , "success" );
182+ response .put ("message" , "포인트가 차감되었습니다" );
183+ response .put ("totalPoints" , updatedPoints );
184+ response .put ("couponType" , pointRequest .getPointType ().name ());
185+ } else {
186+ response .put ("status" , "success" );
187+ response .put ("message" , "포인트가 차감되었습니다" );
188+ response .put ("totalPoints" , updatedPoints );
189+ }
190+
150191 return ResponseEntity .ok (response );
151192 } else {
152193 Map <String , Object > response = new HashMap <>();
@@ -166,9 +207,13 @@ public ResponseEntity<Map<String, Object>> deductPoints(
166207 errorResponse .put ("message" , "사용자를 찾을 수 없습니다" );
167208 return ResponseEntity .status (404 ).body (errorResponse );
168209 } catch (Exception e ) {
210+ // 자세한 예외 로깅 추가
211+ e .printStackTrace ();
169212 Map <String , Object > errorResponse = new HashMap <>();
170213 errorResponse .put ("status" , "error" );
171214 errorResponse .put ("message" , "서버 오류가 발생했습니다: " + e .getMessage ());
215+ // 개발 환경에서만 스택 트레이스 포함
216+ errorResponse .put ("stackTrace" , e .toString ());
172217 return ResponseEntity .status (500 ).body (errorResponse );
173218 }
174219 }
0 commit comments