|
2 | 2 |
|
3 | 3 | import { redirect } from 'next/navigation'; |
4 | 4 |
|
5 | | -import { api } from '@/lib/api-client'; |
| 5 | +import { ApiError, api } from '@/lib/api-client'; |
| 6 | +import { rethrowNextError } from '@/lib/server-action-utils'; |
| 7 | +import type { |
| 8 | + PageResponse, |
| 9 | + ProductReviewListItem, |
| 10 | + ReviewDetail, |
| 11 | + ReviewHelpfulData, |
| 12 | + ReviewStatsData, |
| 13 | +} from '@/types/domain/review'; |
6 | 14 |
|
7 | 15 | interface InitReviewResponseData { |
8 | 16 | reviewId: number; |
@@ -44,12 +52,80 @@ interface ActionResult<T = undefined> { |
44 | 52 | data?: T; |
45 | 53 | } |
46 | 54 |
|
| 55 | +export interface ProductReviewsQuery { |
| 56 | + reviewType?: 'INITIAL' | 'ONE_MONTH'; |
| 57 | + size?: string | string[]; |
| 58 | + color?: string | string[]; |
| 59 | + sort?: 'BEST' | 'RECENT' | 'RATING_HIGH' | 'RATING_LOW'; |
| 60 | + mySizeOnly?: boolean; |
| 61 | + page?: number; |
| 62 | + pageSize?: number; |
| 63 | +} |
| 64 | + |
| 65 | +const REVIEW_LIST_DEFAULT_PAGE_SIZE = 10; |
| 66 | + |
| 67 | +function createEmptyReviewSummary(): ReviewStatsData { |
| 68 | + return { |
| 69 | + averageRating: 0, |
| 70 | + initialReviewCount: 0, |
| 71 | + oneMonthReviewCount: 0, |
| 72 | + sizeSummary: { |
| 73 | + category: '사이즈', |
| 74 | + totalCount: 0, |
| 75 | + topAnswer: null, |
| 76 | + topAnswerCount: 0, |
| 77 | + answerStats: [], |
| 78 | + }, |
| 79 | + colorSummary: { |
| 80 | + category: '색감', |
| 81 | + totalCount: 0, |
| 82 | + topAnswer: null, |
| 83 | + topAnswerCount: 0, |
| 84 | + answerStats: [], |
| 85 | + }, |
| 86 | + materialSummary: { |
| 87 | + category: '소재', |
| 88 | + totalCount: 0, |
| 89 | + topAnswer: null, |
| 90 | + topAnswerCount: 0, |
| 91 | + answerStats: [], |
| 92 | + }, |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +function createEmptyReviewPage( |
| 97 | + page = 0, |
| 98 | + pageSize = 10, |
| 99 | +): PageResponse<ProductReviewListItem> { |
| 100 | + return { |
| 101 | + totalPages: 0, |
| 102 | + totalElements: 0, |
| 103 | + pageable: { |
| 104 | + paged: true, |
| 105 | + pageNumber: page, |
| 106 | + pageSize, |
| 107 | + offset: page * pageSize, |
| 108 | + sort: [], |
| 109 | + unpaged: false, |
| 110 | + }, |
| 111 | + first: true, |
| 112 | + last: true, |
| 113 | + size: pageSize, |
| 114 | + content: [], |
| 115 | + number: page, |
| 116 | + sort: [], |
| 117 | + numberOfElements: 0, |
| 118 | + empty: true, |
| 119 | + }; |
| 120 | +} |
| 121 | + |
47 | 122 | export async function initPendingReviewAction(formData: FormData) { |
48 | 123 | const rawOrderItemId = formData.get('orderItemId'); |
49 | 124 | const rawProductId = formData.get('productId'); |
50 | 125 | const orderItemId = |
51 | 126 | typeof rawOrderItemId === 'string' ? Number(rawOrderItemId) : NaN; |
52 | | - const productId = typeof rawProductId === 'string' ? Number(rawProductId) : NaN; |
| 127 | + const productId = |
| 128 | + typeof rawProductId === 'string' ? Number(rawProductId) : NaN; |
53 | 129 |
|
54 | 130 | if (!Number.isFinite(orderItemId)) { |
55 | 131 | return; |
@@ -123,21 +199,98 @@ export async function submitReviewAction( |
123 | 199 | payload: ReviewSubmitRequest, |
124 | 200 | ): Promise<ActionResult> { |
125 | 201 | try { |
126 | | - console.log('[review-submit] request', { |
127 | | - endpoint: `/reviews/${reviewId}/submit`, |
128 | | - reviewId, |
129 | | - payload, |
130 | | - }); |
131 | | - |
132 | 202 | await api.post<Record<string, never>, ReviewSubmitRequest>( |
133 | 203 | `/reviews/${reviewId}/submit`, |
134 | 204 | payload, |
135 | 205 | ); |
136 | 206 |
|
137 | | - console.log('[review-submit] success', { reviewId }); |
138 | 207 | return { success: true }; |
139 | 208 | } catch (error) { |
140 | 209 | console.error('리뷰 제출 실패:', error); |
141 | 210 | return { success: false, message: '리뷰 제출에 실패했습니다.' }; |
142 | 211 | } |
143 | 212 | } |
| 213 | + |
| 214 | +/** 리뷰 도움돼요 토글 */ |
| 215 | +export async function toggleReviewHelpfulAction( |
| 216 | + reviewId: number, |
| 217 | +): Promise<ActionResult<ReviewHelpfulData>> { |
| 218 | + try { |
| 219 | + const data = await api.post<ReviewHelpfulData, Record<string, never>>( |
| 220 | + `/reviews/${reviewId}/helpful`, |
| 221 | + {}, |
| 222 | + ); |
| 223 | + return { success: true, data }; |
| 224 | + } catch (error) { |
| 225 | + if (error instanceof ApiError && error.status === 404) { |
| 226 | + return { success: false, message: '리뷰를 찾을 수 없습니다.' }; |
| 227 | + } |
| 228 | + rethrowNextError(error); |
| 229 | + console.error('리뷰 도움돼요 토글 실패:', { error, reviewId }); |
| 230 | + return { success: false, message: '도움돼요 처리에 실패했습니다.' }; |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +/** 리뷰 상세 조회 */ |
| 235 | +export async function getReviewDetailAction( |
| 236 | + reviewId: number, |
| 237 | +): Promise<ReviewDetail | null> { |
| 238 | + try { |
| 239 | + const detail = await api.get<ReviewDetail>(`/reviews/${reviewId}/details`); |
| 240 | + return detail; |
| 241 | + } catch (error) { |
| 242 | + if (error instanceof ApiError && error.status === 404) { |
| 243 | + console.warn('리뷰 상세 조회: 리뷰를 찾을 수 없습니다.', { reviewId }); |
| 244 | + return null; |
| 245 | + } |
| 246 | + rethrowNextError(error); |
| 247 | + console.error('리뷰 상세 조회 실패:', error); |
| 248 | + throw new Error( |
| 249 | + error instanceof Error ? error.message : '리뷰 상세 조회에 실패했습니다.', |
| 250 | + ); |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +/** 상품별 리뷰 목록 조회 */ |
| 255 | +export async function getProductReviewsAction( |
| 256 | + productId: number, |
| 257 | + query: ProductReviewsQuery = {}, |
| 258 | +): Promise<PageResponse<ProductReviewListItem>> { |
| 259 | + const page = query.page ?? 0; |
| 260 | + const pageSize = query.pageSize ?? REVIEW_LIST_DEFAULT_PAGE_SIZE; |
| 261 | + const params: Record< |
| 262 | + string, |
| 263 | + string | number | boolean | string[] | undefined |
| 264 | + > = { ...query }; |
| 265 | + params.page = page; |
| 266 | + params.pageSize = pageSize; |
| 267 | + params.sort = query.sort ?? 'BEST'; |
| 268 | + |
| 269 | + try { |
| 270 | + return await api.get<PageResponse<ProductReviewListItem>>( |
| 271 | + `/products/${productId}/reviews`, |
| 272 | + { |
| 273 | + params, |
| 274 | + }, |
| 275 | + ); |
| 276 | + } catch (error) { |
| 277 | + rethrowNextError(error); |
| 278 | + console.error('상품 리뷰 목록 조회 실패:', { error, productId, params }); |
| 279 | + return createEmptyReviewPage(page, pageSize); |
| 280 | + } |
| 281 | +} |
| 282 | + |
| 283 | +/** 리뷰 통계 요약 조회 */ |
| 284 | +export async function getProductReviewsSummaryAction( |
| 285 | + productId: number, |
| 286 | +): Promise<ReviewStatsData> { |
| 287 | + try { |
| 288 | + return await api.get<ReviewStatsData>( |
| 289 | + `/products/${productId}/reviews/summary`, |
| 290 | + ); |
| 291 | + } catch (error) { |
| 292 | + rethrowNextError(error); |
| 293 | + console.error('리뷰 통계 요약 조회 실패:', { error, productId }); |
| 294 | + return createEmptyReviewSummary(); |
| 295 | + } |
| 296 | +} |
0 commit comments