From 01a9bfaf4e4b5fff462fb3797e3d5489c5d9aa5b Mon Sep 17 00:00:00 2001 From: Aaron William Po Date: Thu, 7 Dec 2023 21:12:17 -0500 Subject: [PATCH] fix update-avatar --- .../api/users/[id]/profile/update-avatar.ts | 48 ++----------------- .../{update-bio.ts => update-profile.ts} | 0 ....ts.ts => sendUpdateUserProfileRequest.ts} | 2 +- 3 files changed, 4 insertions(+), 46 deletions(-) rename src/pages/api/users/[id]/profile/{update-bio.ts => update-profile.ts} (100%) rename src/requests/Account/{sendUpdateUserProfileRequest.ts.ts => sendUpdateUserProfileRequest.ts} (97%) diff --git a/src/pages/api/users/[id]/profile/update-avatar.ts b/src/pages/api/users/[id]/profile/update-avatar.ts index c08de09..7cc918e 100644 --- a/src/pages/api/users/[id]/profile/update-avatar.ts +++ b/src/pages/api/users/[id]/profile/update-avatar.ts @@ -1,54 +1,12 @@ -import { UserExtendedNextApiRequest } from '@/config/auth/types'; import { singleUploadMiddleware } from '@/config/multer/uploadMiddleware'; import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser'; - -import ServerError from '@/config/util/ServerError'; +import { checkIfUserCanUpdateProfile, updateAvatar } from '@/controllers/users/profile'; +import { UpdateProfileRequest } from '@/controllers/users/profile/types'; import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema'; import { NextApiResponse } from 'next'; -import { NextHandler, createRouter } from 'next-connect'; +import { createRouter } from 'next-connect'; import { z } from 'zod'; -import updateUserAvatarById, { - UpdateUserAvatarByIdParams, -} from '@/services/UserAccount/UpdateUserAvatarByIdParams'; - -interface UpdateProfileRequest extends UserExtendedNextApiRequest { - file: Express.Multer.File; - body: { - bio: string; - }; -} - -const checkIfUserCanUpdateProfile = async ( - req: UpdateProfileRequest, - res: NextApiResponse, - next: NextHandler, -) => { - const user = req.user!; - - if (user.id !== req.query.id) { - throw new ServerError('You can only update your own profile.', 403); - } - - await next(); -}; - -const updateAvatar = async (req: UpdateProfileRequest, res: NextApiResponse) => { - const { file, user } = req; - - const avatar: UpdateUserAvatarByIdParams['data']['avatar'] = { - alt: file.originalname, - path: file.path, - caption: '', - }; - - await updateUserAvatarById({ id: user!.id, data: { avatar } }); - res.status(200).json({ - message: 'User avatar updated successfully.', - statusCode: 200, - success: true, - }); -}; const router = createRouter< UpdateProfileRequest, diff --git a/src/pages/api/users/[id]/profile/update-bio.ts b/src/pages/api/users/[id]/profile/update-profile.ts similarity index 100% rename from src/pages/api/users/[id]/profile/update-bio.ts rename to src/pages/api/users/[id]/profile/update-profile.ts diff --git a/src/requests/Account/sendUpdateUserProfileRequest.ts.ts b/src/requests/Account/sendUpdateUserProfileRequest.ts similarity index 97% rename from src/requests/Account/sendUpdateUserProfileRequest.ts.ts rename to src/requests/Account/sendUpdateUserProfileRequest.ts index 8d421d6..b55fb8a 100644 --- a/src/requests/Account/sendUpdateUserProfileRequest.ts.ts +++ b/src/requests/Account/sendUpdateUserProfileRequest.ts @@ -10,7 +10,7 @@ const sendUpdateUserProfileRequest = async ({ bio, userId, }: UpdateProfileRequestParams) => { - const response = await fetch(`/api/users/${userId}/profile/update-bio`, { + const response = await fetch(`/api/users/${userId}/profile/update-profile`, { method: 'PUT', body: JSON.stringify({ bio }), headers: { 'Content-Type': 'application/json' },