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 | 1x 1x 1x 1x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 3x 1x 6x 3x 3x 2x 1x 6x 6x 6x 1x 1x 1x 1x 1x 6x | // Services
import { STATUS_CODE, STATUS_MESSAGES } from '@shared/constants';
import { lunchMenusService } from '@/services';
// Types
import { UserReaction } from '@/types';
/**
* POST handler for user reactions (like/dislike) on lunch menus.
*
* @param {Object} context - Request context
* @param {Request} context.request - Must include:
* - Header: `Authorization: Bearer <token>`
* - Body: { lunchMenuId: string, type: "LIKE" | "DISLIKE" }
*
* @returns {Promise<Response>} JSON with:
* - success {boolean}
* - error {object|null} { code, status, message }
*/
export const POST = async ({ request }: { request: Request }) => {
try {
const authHeader = request.headers.get('Authorization');
if (!authHeader?.startsWith('Bearer ')) {
return new Response(
JSON.stringify({
success: false,
error: {
code: 'UNAUTHORIZED',
status: STATUS_CODE.UNAUTHORIZED,
message: STATUS_MESSAGES[STATUS_CODE.UNAUTHORIZED],
},
}),
{
status: STATUS_CODE.UNAUTHORIZED,
headers: { 'Content-Type': 'application/json' },
},
);
}
const token = authHeader.replace('Bearer ', '').trim();
const { lunchMenuId, type } = await request.json();
if (!lunchMenuId || !type) {
return new Response(
JSON.stringify({
success: false,
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' },
},
);
}
// Determine which action to perform (like or dislike)
const reactionFn =
type === UserReaction.LIKE
? lunchMenusService.like
: lunchMenusService.dislike;
// Execute the vote action
const { success, error } = await reactionFn(lunchMenuId, token);
return new Response(JSON.stringify({ success, error }), {
status: success
? STATUS_CODE.OK
: error?.status || STATUS_CODE.UNAUTHORIZED,
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' },
});
}
};
|