This commit is contained in:
1 parent
710229879e
commit
e4843e7d1d
3 files changed
+124
-3
No files matched your search
@@ -15,6 +15,7 @@ export default {
|
||||
return createFeedback(request, env);
|
||||
}
|
||||
if (/^\/api\/feedbacks\/[^/]+\/completion-confirmation$/.test(url.pathname)) return completionConfirmation(request, env, url);
|
||||
if (/^\/api\/feedbacks\/[^/]+\/comments\/[^/]+$/.test(url.pathname)) return feedbackCommentItem(request, env, url);
|
||||
if (/^\/api\/feedbacks\/[^/]+\/comments$/.test(url.pathname)) return feedbackComments(request, env, url);
|
||||
if (/^\/api\/feedbacks\/[^/]+$/.test(url.pathname)) return feedbackDetail(request, env, url);
|
||||
|
||||
@@ -626,6 +627,62 @@ async function feedbackComments(request, env, url) {
|
||||
return json({ id: result.id || result.data?.id || result.comment?.id || commentId, comment: result.comment || result.data || result });
|
||||
}
|
||||
|
||||
async function feedbackCommentItem(request, env, url) {
|
||||
if (request.method !== 'PATCH' && request.method !== 'DELETE') return json({ error: 'method_not_allowed' }, 405, { allow: 'PATCH, DELETE' });
|
||||
const user = await getSession(request, env);
|
||||
if (!user) return json({ error: 'unauthenticated' }, 401);
|
||||
if (!env.ABC_API_KEY) return json({ error: 'feedback_api_not_configured' }, 503);
|
||||
if (!env.ABC_PROJECT_ID || !env.ABC_CHANNEL_ID) return json({ error: 'feedback_target_not_configured' }, 503);
|
||||
|
||||
const pathParts = url.pathname.split('/');
|
||||
const feedbackId = decodeURIComponent(pathParts[3] || '');
|
||||
const commentId = decodeURIComponent(pathParts[5] || '');
|
||||
if (!isUuidv7(feedbackId)) return json({ error: 'invalid_feedback_id' }, 400);
|
||||
if (!isUuidv7(commentId)) return json({ error: 'invalid_comment_id' }, 400);
|
||||
|
||||
const requesterId = String(user.requesterId || user.ssoSubject || user.userUuid || '');
|
||||
const requesterTenantId = String(user.requesterTenantId || user.tenantId || '');
|
||||
if (!requesterId || !requesterTenantId) return json({ error: 'requester_identity_missing' }, 422);
|
||||
|
||||
const apiBaseUrl = String(env.ABC_API_BASE_URL || 'https://feedback.hmac.kr').replace(/\/$/, '');
|
||||
const endpoint = `${apiBaseUrl}/api/projects/${encodeURIComponent(env.ABC_PROJECT_ID)}/channels/${encodeURIComponent(env.ABC_CHANNEL_ID)}/feedbacks/${encodeURIComponent(feedbackId)}/comments/${encodeURIComponent(commentId)}`;
|
||||
const headers = {
|
||||
accept: 'application/json',
|
||||
'x-api-key': env.ABC_API_KEY,
|
||||
'x-requester-id': requesterId,
|
||||
'x-requester-tenant-id': requesterTenantId
|
||||
};
|
||||
let body;
|
||||
if (request.method === 'PATCH') {
|
||||
let input;
|
||||
try {
|
||||
input = await request.json();
|
||||
} catch (error) {
|
||||
return json({ error: 'invalid_json' }, 400);
|
||||
}
|
||||
const content = String(input && input.content || '').trim();
|
||||
if (!content) return json({ error: 'comment_content_required' }, 400);
|
||||
if (content.length > 5000) return json({ error: 'comment_content_too_long' }, 400);
|
||||
headers['content-type'] = 'application/json';
|
||||
body = JSON.stringify({ content });
|
||||
}
|
||||
|
||||
let response;
|
||||
try {
|
||||
response = await fetch(endpoint, { method: request.method, headers, body });
|
||||
} catch (error) {
|
||||
console.error('feedback_comment_item_api_request_failed', error instanceof Error ? error.message : 'unknown');
|
||||
return json({ error: 'feedback_comments_api_unreachable' }, 502);
|
||||
}
|
||||
const result = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
const upstreamMessage = String(result.message || result.error || result.code || '').slice(0, 240);
|
||||
return json({ error: 'feedback_comments_api_rejected', message: upstreamMessage || '댓글 수정 또는 삭제에 실패했습니다.', status: response.status }, response.status >= 500 ? 502 : response.status);
|
||||
}
|
||||
if (request.method === 'DELETE') return json({ ok: true, id: commentId });
|
||||
return json({ comment: result.comment || result.data || result });
|
||||
}
|
||||
|
||||
async function feedbackDetail(request, env, url) {
|
||||
if (!['GET', 'PATCH', 'DELETE'].includes(request.method)) return json({ error: 'method_not_allowed' }, 405, { allow: 'GET, PATCH, DELETE' });
|
||||
const user = await getSession(request, env);
|
||||
|
||||
Reference in new issue
Block a user