Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | 1x 1x 1x 1x 1x 1x 10x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 10x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 10x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 3x 1x 5x 5x 5x 5x 5x 5x 3x 3x 1x 2x 5x 5x 5x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x | // Services
import { lunchMenusService } from '@/services';
// Constants
import { STATUS_CODE, STATUS_MESSAGES } from '@shared/constants';
// Types
import { CommentAction } from '@/types';
import { STATUS_ERROR_CODE } from '@/constants';
// Utils
import { getBearerTokenFromRequest } from '@/utils/auth';
/**
* POST handler for lunch menu comment create, update, and delete.
*
* @param context.request - Must include:
* - Header: `Authorization: Bearer <token>`
* - Body: { lunchMenuId: string, type: CommentAction, content?: string, isAnonymous?: boolean }
*/
export const POST = async ({ request }: { request: Request }) => {
try {
const token = getBearerTokenFromRequest(request);
if (!token) {
return new Response(
JSON.stringify({
success: false,
error: {
code: STATUS_ERROR_CODE.UNAUTHORIZED,
status: STATUS_CODE.UNAUTHORIZED,
message: STATUS_MESSAGES[STATUS_CODE.UNAUTHORIZED],
},
}),
{
status: STATUS_CODE.UNAUTHORIZED,
headers: { 'Content-Type': 'application/json' },
},
);
}
const { lunchMenuId, type, content, isAnonymous } = await request.json();
if (!lunchMenuId || !type) {
return new Response(
JSON.stringify({
success: false,
error: {
code: STATUS_ERROR_CODE.INVALID_REQUEST,
status: STATUS_CODE.BAD_REQUEST,
message: STATUS_MESSAGES[STATUS_CODE.BAD_REQUEST],
},
}),
{
status: STATUS_CODE.BAD_REQUEST,
headers: { 'Content-Type': 'application/json' },
},
);
}
if (type === CommentAction.CREATE || type === CommentAction.UPDATE) {
if (!content || typeof isAnonymous !== 'boolean') {
return new Response(
JSON.stringify({
data: null,
error: {
code: STATUS_ERROR_CODE.INVALID_REQUEST,
status: STATUS_CODE.BAD_REQUEST,
message: STATUS_MESSAGES[STATUS_CODE.BAD_REQUEST],
},
}),
{
status: STATUS_CODE.BAD_REQUEST,
headers: { 'Content-Type': 'application/json' },
},
);
}
const commentFn =
type === CommentAction.CREATE
? lunchMenusService.createComment
: lunchMenusService.updateComment;
const { data, error } = await commentFn(
lunchMenuId,
content,
isAnonymous,
token,
);
return new Response(JSON.stringify({ data, error }), {
status: error
? (error.status ?? STATUS_CODE.BAD_REQUEST)
: STATUS_CODE.OK,
headers: { 'Content-Type': 'application/json' },
});
}
if (type === CommentAction.DELETE) {
const { success, error } = await lunchMenusService.deleteComment(
lunchMenuId,
token,
);
return new Response(JSON.stringify({ success, error }), {
status: error
? (error.status ?? STATUS_CODE.BAD_REQUEST)
: STATUS_CODE.OK,
headers: { 'Content-Type': 'application/json' },
});
}
return new Response(
JSON.stringify({
success: false,
error: {
code: STATUS_ERROR_CODE.INVALID_REQUEST,
status: STATUS_CODE.BAD_REQUEST,
message: STATUS_MESSAGES[STATUS_CODE.BAD_REQUEST],
},
}),
{
status: STATUS_CODE.BAD_REQUEST,
headers: { 'Content-Type': 'application/json' },
},
);
} catch (err) {
return new Response(JSON.stringify({ success: false, error: err }), {
status: STATUS_CODE.INTERNAL_SERVER_ERROR,
headers: { 'Content-Type': 'application/json' },
});
}
};
|