From 3c9cbb01c7f5a3fd86fe34c4bbff7d683be0015f Mon Sep 17 00:00:00 2001 From: Haris Shah Date: Sun, 25 Feb 2024 22:43:53 +0500 Subject: [PATCH] feat: add user model validation --- apps/api/src/controllers/user_controller.ts | 31 ++++++++++----------- apps/api/src/models/user_model.ts | 2 +- apps/api/src/routes/user_route.ts | 2 +- apps/api/src/validations/user_validation.ts | 2 +- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/apps/api/src/controllers/user_controller.ts b/apps/api/src/controllers/user_controller.ts index 8cd189b..6da5468 100644 --- a/apps/api/src/controllers/user_controller.ts +++ b/apps/api/src/controllers/user_controller.ts @@ -9,7 +9,6 @@ import { log } from 'console' export async function post_user(req: Request, res: Response): Promise { try { const user_data = req.body - // check if user email already exists const { email } = req.body const existing_user = await USER_SCHEMA.findOne({ email }) @@ -19,8 +18,8 @@ export async function post_user(req: Request, res: Response): Promise { } //* post user - const new_job = await USER_SCHEMA.create(user_data) - res.status(201).json(new_job) + const user = await USER_SCHEMA.create(user_data) + res.status(201).json(user) } catch (error) { log('Error posting user:', error) res.status(500).json({ error: 'Internal server error' }) @@ -28,22 +27,20 @@ export async function post_user(req: Request, res: Response): Promise { } //* @desc Get user -//* route GET /api/user/:id +//* route GET /api/user //! @access Private export async function get_user(req: Request, res: Response): Promise { try { - const { id } = req.params - //* check if id is valid - if (!isValidObjectId(id)) { - res.status(400).json({ error: 'Invalid id' }) - return - } - //* check if user exists - const user = await USER_SCHEMA.findById(id) - if (!user) { - res.status(404).json({ error: 'user not found' }) + //* get user by email and password + const { email, password } = req.body + const user = await USER_SCHEMA.findOne({ email, password }) + + //* check if user email or password is incorrect + if (!user || user.email !== email || user.password !== password) { + res.status(401).json({ error: 'Incorrect email or password' }) return } + res.status(200).json(user) } catch (error) { log('Error fetching user:', error) @@ -51,7 +48,7 @@ export async function get_user(req: Request, res: Response): Promise { } } -//* @desc Delete job +//* @desc Delete user //* route DELETE /api/user/:id //! @access Private export async function delete_user(req: Request, res: Response): Promise { @@ -95,11 +92,11 @@ export async function update_user(req: Request, res: Response): Promise { return } - // check if user email already exists + //* check if user email already exists const { email } = req.body const existing_user = await USER_SCHEMA.findOne({ email }) if (existing_user && existing_user._id.toString() !== id) { - res.status(400).json({ error: 'Email already exists' }) + res.status(409).json({ error: 'Email already exists' }) return } diff --git a/apps/api/src/models/user_model.ts b/apps/api/src/models/user_model.ts index 9d9d77b..34a5d00 100644 --- a/apps/api/src/models/user_model.ts +++ b/apps/api/src/models/user_model.ts @@ -2,7 +2,7 @@ import mongoose, { Schema } from 'mongoose' const user_schema = new Schema( { - name: { type: String, required: true }, + name: { type: String }, email: { type: String, required: true, unique: true }, password: { type: String, required: true }, }, diff --git a/apps/api/src/routes/user_route.ts b/apps/api/src/routes/user_route.ts index f9e4d86..beecbc3 100644 --- a/apps/api/src/routes/user_route.ts +++ b/apps/api/src/routes/user_route.ts @@ -15,7 +15,7 @@ router.post('/', validate_schema(USER_VALIDATION_SCHEMA), post_user) //* @desc Get user //! @access Private -router.get('/:id', get_user) +router.get('/', validate_schema(USER_VALIDATION_SCHEMA), get_user) //* @desc Delete user //! @access Private diff --git a/apps/api/src/validations/user_validation.ts b/apps/api/src/validations/user_validation.ts index 15b960f..3efbbd1 100644 --- a/apps/api/src/validations/user_validation.ts +++ b/apps/api/src/validations/user_validation.ts @@ -1,7 +1,7 @@ import { z } from 'zod' export const USER_VALIDATION_SCHEMA = z.object({ - name: z.string(), + name: z.string().optional(), email: z.string().email(), password: z.string().min(6), })