diff --git a/src/config/auth/localStrat.ts b/src/config/auth/localStrat.ts index 84df138..9d8681d 100644 --- a/src/config/auth/localStrat.ts +++ b/src/config/auth/localStrat.ts @@ -1,11 +1,11 @@ import Local from 'passport-local'; -import { findUserByUsername } from '@/services/users/auth'; +import { findUserByUsernameService } from '@/services/users/auth'; import ServerError from '../util/ServerError'; import { validatePassword } from './passwordFns'; const localStrat = new Local.Strategy(async (username, password, done) => { try { - const user = await findUserByUsername({ username }); + const user = await findUserByUsernameService({ username }); if (!user) { throw new ServerError('Username or password is incorrect.', 401); } diff --git a/src/config/nextConnect/middleware/getCurrentUser.ts b/src/config/nextConnect/middleware/getCurrentUser.ts index e907a27..4f2c3ba 100644 --- a/src/config/nextConnect/middleware/getCurrentUser.ts +++ b/src/config/nextConnect/middleware/getCurrentUser.ts @@ -2,9 +2,9 @@ import { NextApiResponse } from 'next'; import { NextHandler } from 'next-connect'; import ServerError from '@/config/util/ServerError'; -import { getLoginSession } from '../../auth/session'; -import { UserExtendedNextApiRequest } from '../../auth/types'; -import { findUserById } from '@/services/users/auth'; +import { findUserByIdService } from '@/services/users/auth'; +import { getLoginSession } from '@/config/auth/session'; +import { UserExtendedNextApiRequest } from '@/config/auth/types'; /** Get the current user from the session. Adds the user to the request object. */ const getCurrentUser = async ( @@ -13,7 +13,7 @@ const getCurrentUser = async ( next: NextHandler, ) => { const session = await getLoginSession(req); - const user = await findUserById({ userId: session?.id }); + const user = await findUserByIdService({ userId: session?.id }); if (!user) { throw new ServerError('User is not logged in.', 401); diff --git a/src/controllers/posts/breweries/index.ts b/src/controllers/posts/breweries/index.ts index 658cb50..4e5cdfd 100644 --- a/src/controllers/posts/breweries/index.ts +++ b/src/controllers/posts/breweries/index.ts @@ -12,10 +12,12 @@ import { createBreweryPostLocationService, getMapBreweryPostsService, getBreweryPostByIdService, + updateBreweryPostService, + deleteBreweryPostService, } from '@/services/posts/brewery-post'; import { getBeerPostsByBreweryIdService } from '@/services/posts/beer-post'; import { NextHandler } from 'next-connect'; -import DBClient from '@/prisma/DBClient'; + import { BreweryPostRequest, CreateBreweryPostRequest, @@ -188,10 +190,7 @@ export const editBreweryPost = async ( query: { id }, } = req; - await DBClient.instance.breweryPost.update({ - where: { id }, - data: body, - }); + await updateBreweryPostService({ breweryPostId: id, body }); res.status(200).json({ message: 'Brewery post updated successfully', @@ -204,11 +203,8 @@ export const deleteBreweryPost = async ( req: BreweryPostRequest, res: NextApiResponse, ) => { - const { - query: { id }, - } = req; - - const deleted = await DBClient.instance.breweryPost.delete({ where: { id } }); + const { id } = req.query; + const deleted = await deleteBreweryPostService({ breweryPostId: id }); if (!deleted) { throw new ServerError('Brewery post not found', 404); diff --git a/src/controllers/users/auth/index.ts b/src/controllers/users/auth/index.ts index 4477f9e..e2109d1 100644 --- a/src/controllers/users/auth/index.ts +++ b/src/controllers/users/auth/index.ts @@ -18,15 +18,15 @@ import { verifyConfirmationToken } from '@/config/jwt'; import { hashPassword } from '@/config/auth/passwordFns'; import { - createNewUser, - deleteUserById, - findUserByEmail, - findUserByUsername, - sendConfirmationEmail, - sendResetPasswordEmail, - updateUserById, - updateUserPassword, - updateUserToBeConfirmedById, + createNewUserService, + deleteUserService, + findUserByEmailService, + findUserByUsernameService, + sendConfirmationEmailService, + sendResetPasswordEmailService, + updateUserService, + updateUserPasswordService, + confirmUserService, } from '@/services/users/auth'; import { EditUserRequest, UserRouteRequest } from '@/controllers/users/profile/types'; @@ -98,8 +98,8 @@ export const registerUser = async ( ) => { const [usernameTaken, emailTaken] = ( await Promise.all([ - findUserByUsername({ username: req.body.username }), - findUserByEmail({ email: req.body.email }), + findUserByUsernameService({ username: req.body.username }), + findUserByEmailService({ email: req.body.email }), ]) ).map((user) => !!user); @@ -117,14 +117,14 @@ export const registerUser = async ( ); } - const user = await createNewUser(req.body); + const user = await createNewUserService(req.body); await setLoginSession(res, { id: user.id, username: user.username, }); - await sendConfirmationEmail({ + await sendConfirmationEmailService({ email: user.email, username: user.username, userId: user.id, @@ -155,7 +155,7 @@ export const confirmUser = async ( throw new ServerError('Could not confirm user.', 401); } - await updateUserToBeConfirmedById({ userId: id }); + await confirmUserService({ userId: id }); res.status(200).json({ message: 'User confirmed successfully.', @@ -170,10 +170,10 @@ export const resetPassword = async ( ) => { const { email } = req.body; - const user = await findUserByEmail({ email }); + const user = await findUserByEmailService({ email }); if (user) { - await sendResetPasswordEmail({ + await sendResetPasswordEmailService({ email: user.email, username: user.username, userId: user.id, @@ -204,7 +204,7 @@ export const sendCurrentUser = async ( export const checkEmail = async (req: CheckEmailRequest, res: NextApiResponse) => { const { email: emailToCheck } = req.query; - const email = await findUserByEmail({ email: emailToCheck }); + const email = await findUserByEmailService({ email: emailToCheck }); res.json({ success: true, @@ -217,7 +217,7 @@ export const checkEmail = async (req: CheckEmailRequest, res: NextApiResponse) = export const checkUsername = async (req: CheckUsernameRequest, res: NextApiResponse) => { const { username: usernameToCheck } = req.query; - const username = await findUserByUsername({ username: usernameToCheck }); + const username = await findUserByUsernameService({ username: usernameToCheck }); res.json({ success: true, @@ -234,7 +234,10 @@ export const updatePassword = async ( const user = req.user!; const { password } = req.body; - await updateUserPassword({ userId: user.id, password: await hashPassword(password) }); + await updateUserPasswordService({ + userId: user.id, + password: await hashPassword(password), + }); res.json({ message: 'Updated user password.', @@ -249,7 +252,7 @@ export const resendConfirmation = async ( ) => { const user = req.user!; - await sendConfirmationEmail({ + await sendConfirmationEmailService({ userId: user.id, username: user.username, email: user.email, @@ -267,7 +270,7 @@ export const editUserInfo = async ( ) => { const { email, firstName, lastName, username } = req.body; - const updatedUser = await updateUserById({ + const updatedUser = await updateUserService({ userId: req.user!.id, data: { email, firstName, lastName, username }, }); @@ -285,7 +288,7 @@ export const deleteAccount = async ( res: NextApiResponse>, ) => { const { id } = req.query; - const deletedUser = await deleteUserById({ userId: id }); + const deletedUser = await deleteUserService({ userId: id }); if (!deletedUser) { throw new ServerError('Could not find a user with that id.', 400); diff --git a/src/controllers/users/profile/index.ts b/src/controllers/users/profile/index.ts index 357a26e..e84fabd 100644 --- a/src/controllers/users/profile/index.ts +++ b/src/controllers/users/profile/index.ts @@ -7,7 +7,7 @@ import { NextHandler } from 'next-connect'; import { UserExtendedNextApiRequest } from '@/config/auth/types'; -import { findUserById } from '@/services/users/auth'; +import { findUserByIdService } from '@/services/users/auth'; import { createUserFollow, @@ -32,7 +32,7 @@ export const followUser = async ( ) => { const { id } = req.query; - const user = await findUserById({ userId: id }); + const user = await findUserByIdService({ userId: id }); if (!user) { throw new ServerError('User not found', 404); } @@ -70,7 +70,7 @@ export const getUserFollowers = async ( // eslint-disable-next-line @typescript-eslint/naming-convention const { id, page_num, page_size } = req.query; - const user = await findUserById({ userId: id }); + const user = await findUserByIdService({ userId: id }); if (!user) { throw new ServerError('User not found', 404); } @@ -101,7 +101,7 @@ export const getUsersFollowed = async ( // eslint-disable-next-line @typescript-eslint/naming-convention const { id, page_num, page_size } = req.query; - const user = await findUserById({ userId: id }); + const user = await findUserByIdService({ userId: id }); if (!user) { throw new ServerError('User not found', 404); } @@ -131,7 +131,7 @@ export const checkIfUserIsFollowedBySessionUser = async ( ) => { const { id } = req.query; - const user = await findUserById({ userId: id }); + const user = await findUserByIdService({ userId: id }); if (!user) { throw new ServerError('User not found', 404); } @@ -162,7 +162,7 @@ export const checkIfUserCanEditUser = async ( ) => { const authenticatedUser = req.user!; - const userToUpdate = await findUserById({ userId: req.query.id }); + const userToUpdate = await findUserByIdService({ userId: req.query.id }); if (!userToUpdate) { throw new ServerError('User not found', 404); } diff --git a/src/pages/users/[id].tsx b/src/pages/users/[id].tsx index dfa99aa..170a8ff 100644 --- a/src/pages/users/[id].tsx +++ b/src/pages/users/[id].tsx @@ -7,7 +7,7 @@ import { FC } from 'react'; import { z } from 'zod'; import withPageAuthRequired from '@/util/withPageAuthRequired'; import UserHeader from '@/components/UserPage/UserHeader'; -import { findUserById } from '@/services/users/auth'; +import { findUserByIdService } from '@/services/users/auth'; interface UserInfoPageProps { user: z.infer; @@ -40,7 +40,7 @@ export default UserInfoPage; export const getServerSideProps = withPageAuthRequired( async (context) => { const { id } = context.params!; - const user = await findUserById({ userId: id as string }); + const user = await findUserByIdService({ userId: id as string }); return user ? { props: { user: JSON.parse(JSON.stringify(user)) } } : { notFound: true }; diff --git a/src/pages/users/reset-password.tsx b/src/pages/users/reset-password.tsx index 78dea22..ec33445 100644 --- a/src/pages/users/reset-password.tsx +++ b/src/pages/users/reset-password.tsx @@ -1,7 +1,7 @@ import { setLoginSession } from '@/config/auth/session'; import { verifyResetPasswordToken } from '@/config/jwt'; import ServerError from '@/config/util/ServerError'; -import { findUserById } from '@/services/users/auth'; +import { findUserByIdService } from '@/services/users/auth'; import { GetServerSideProps, NextApiResponse, NextPage } from 'next'; @@ -29,7 +29,7 @@ export const getServerSideProps: GetServerSideProps = async (context) => { const { id } = await verifyResetPasswordToken(token as string); - const user = await findUserById({ userId: id as string }); + const user = await findUserByIdService({ userId: id as string }); if (!user) { throw new ServerError('User not found', 404); } diff --git a/src/services/posts/brewery-post/index.ts b/src/services/posts/brewery-post/index.ts index a055c68..0c3875f 100644 --- a/src/services/posts/brewery-post/index.ts +++ b/src/services/posts/brewery-post/index.ts @@ -7,6 +7,7 @@ import { GetAllBreweryPostsByPostedById, GetBreweryPostById, GetMapBreweryPosts, + UpdateBreweryPost, } from './types'; /** @@ -205,3 +206,36 @@ export const getMapBreweryPostsService: GetMapBreweryPosts = async ({ const count = await DBClient.instance.breweryPost.count(); return { breweryPosts, count }; }; + +/** + * Updates a brewery post. + * + * @param args - The arguments to update a brewery post. + * @param args.breweryPostId - The ID of the brewery post to update. + * @param args.body - The body of the request. + * @param args.body.name - The name of the brewery. + * @param args.body.description - The description of the brewery. + * @param args.body.dateEstablished - The date the brewery was established. + * @returns The updated brewery post. + */ +export const updateBreweryPostService: UpdateBreweryPost = async ({ + breweryPostId, + body, +}) => { + const breweryPost = await DBClient.instance.breweryPost.update({ + where: { id: breweryPostId }, + data: body, + select: breweryPostSelect, + }); + + return breweryPost as Awaited>; +}; + +export const deleteBreweryPostService: GetBreweryPostById = async ({ breweryPostId }) => { + const breweryPost = await DBClient.instance.breweryPost.delete({ + where: { id: breweryPostId }, + select: breweryPostSelect, + }); + + return breweryPost as Awaited>; +}; diff --git a/src/services/posts/brewery-post/types/index.ts b/src/services/posts/brewery-post/types/index.ts index 92f5221..4fd2bd0 100644 --- a/src/services/posts/brewery-post/types/index.ts +++ b/src/services/posts/brewery-post/types/index.ts @@ -46,3 +46,12 @@ export type GetMapBreweryPosts = (args: { breweryPosts: z.infer[]; count: number; }>; + +export type UpdateBreweryPost = (args: { + breweryPostId: string; + body: { + name: string; + description: string; + dateEstablished: Date; + }; +}) => Promise>; diff --git a/src/services/users/auth/index.ts b/src/services/users/auth/index.ts index 050f2a6..69e8081 100644 --- a/src/services/users/auth/index.ts +++ b/src/services/users/auth/index.ts @@ -76,7 +76,7 @@ const authUserSelect = { * @param args.username The username of the user to create. * @returns The user. */ -export const createNewUser: CreateNewUser = async ({ +export const createNewUserService: CreateNewUser = async ({ email, password, firstName, @@ -108,7 +108,7 @@ export const createNewUser: CreateNewUser = async ({ * @param args.userId The id of the user to delete. * @returns The user that was deleted if found, otherwise null. */ -export const deleteUserById: DeleteUserById = ({ userId }) => { +export const deleteUserService: DeleteUserById = ({ userId }) => { return DBClient.instance.user.delete({ where: { id: userId }, select: authUserSelect }); }; @@ -120,7 +120,7 @@ export const deleteUserById: DeleteUserById = ({ userId }) => { * @returns The user if found, otherwise null. */ -export const findUserByUsername: FindUserByUsername = async ({ username }) => { +export const findUserByUsernameService: FindUserByUsername = async ({ username }) => { return DBClient.instance.user.findUnique({ where: { username }, select: authUserSelect, @@ -133,7 +133,7 @@ export const findUserByUsername: FindUserByUsername = async ({ username }) => { * @param args The arguments for service. * @param args.email The email of the user to find. */ -export const findUserByEmail: FindUserByEmail = async ({ email }) => { +export const findUserByEmailService: FindUserByEmail = async ({ email }) => { return DBClient.instance.user.findUnique({ where: { email }, select: userSelect }); }; @@ -144,7 +144,7 @@ export const findUserByEmail: FindUserByEmail = async ({ email }) => { * @param args.userId The id of the user to find. * @returns The user if found, otherwise null. */ -export const findUserById: FindUserById = ({ userId }) => { +export const findUserByIdService: FindUserById = ({ userId }) => { return DBClient.instance.user.findUnique({ where: { id: userId }, select: userSelect }); }; @@ -157,7 +157,7 @@ export const findUserById: FindUserById = ({ userId }) => { * @param args.email The email of the user to send the confirmation email to. * @returns The user if found, otherwise null. */ -export const sendConfirmationEmail: SendConfirmationEmail = async ({ +export const sendConfirmationEmailService: SendConfirmationEmail = async ({ userId, username, email, @@ -189,7 +189,7 @@ export const sendConfirmationEmail: SendConfirmationEmail = async ({ * @param args.email The email of the user to send the reset password email to. * @returns A promise that resolves to void. */ -export const sendResetPasswordEmail: SendResetPasswordEmail = async ({ +export const sendResetPasswordEmailService: SendResetPasswordEmail = async ({ userId, username, email, @@ -221,9 +221,7 @@ export const sendResetPasswordEmail: SendResetPasswordEmail = async ({ * @param args.userId The id of the user to update. * @returns The user. */ -export const updateUserToBeConfirmedById: UpdateUserToBeConfirmedById = async ({ - userId, -}) => { +export const confirmUserService: UpdateUserToBeConfirmedById = async ({ userId }) => { return DBClient.instance.user.update({ where: { id: userId }, data: { accountIsVerified: true, updatedAt: new Date() }, @@ -231,7 +229,10 @@ export const updateUserToBeConfirmedById: UpdateUserToBeConfirmedById = async ({ }); }; -export const updateUserPassword: UpdateUserPassword = async ({ password, userId }) => { +export const updateUserPasswordService: UpdateUserPassword = async ({ + password, + userId, +}) => { const hash = await hashPassword(password); const user = await DBClient.instance.user.update({ @@ -254,7 +255,7 @@ export const updateUserPassword: UpdateUserPassword = async ({ password, userId * @param args.data.lastName The last name of the user to update. * @param args.data.username The username of the user to update. */ -export const updateUserById: UpdateUserById = async ({ userId, data }) => { +export const updateUserService: UpdateUserById = async ({ userId, data }) => { const user = await DBClient.instance.user.findUnique({ where: { id: userId }, select: userSelect, @@ -272,12 +273,12 @@ export const updateUserById: UpdateUserById = async ({ userId, data }) => { } as const; if (updatedFields.email) { - const emailIsTaken = await findUserByEmail({ email: data.email }); + const emailIsTaken = await findUserByEmailService({ email: data.email }); if (emailIsTaken) { throw new ServerError('Email is already taken', 400); } - await sendConfirmationEmail({ + await sendConfirmationEmailService({ userId, username: data.username, email: data.email, @@ -285,7 +286,7 @@ export const updateUserById: UpdateUserById = async ({ userId, data }) => { } if (updatedFields.username) { - const usernameIsTaken = await findUserByUsername({ username: data.username }); + const usernameIsTaken = await findUserByUsernameService({ username: data.username }); if (usernameIsTaken) { throw new ServerError('Username is already taken', 400); }