Skip to content

Commit

Permalink
feat: add user model validation
Browse files Browse the repository at this point in the history
  • Loading branch information
hariscs committed Feb 25, 2024
1 parent 7462fdb commit 3c9cbb0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
31 changes: 14 additions & 17 deletions apps/api/src/controllers/user_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { log } from 'console'
export async function post_user(req: Request, res: Response): Promise<void> {
try {
const user_data = req.body

// check if user email already exists
const { email } = req.body
const existing_user = await USER_SCHEMA.findOne({ email })
Expand All @@ -19,39 +18,37 @@ export async function post_user(req: Request, res: Response): Promise<void> {
}

//* 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' })
}
}

//* @desc Get user
//* route GET /api/user/:id
//* route GET /api/user
//! @access Private
export async function get_user(req: Request, res: Response): Promise<void> {
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)
res.status(500).json({ error: 'Internal server error' })
}
}

//* @desc Delete job
//* @desc Delete user
//* route DELETE /api/user/:id
//! @access Private
export async function delete_user(req: Request, res: Response): Promise<void> {
Expand Down Expand Up @@ -95,11 +92,11 @@ export async function update_user(req: Request, res: Response): Promise<void> {
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
}

Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/models/user_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
},
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/routes/user_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/validations/user_validation.ts
Original file line number Diff line number Diff line change
@@ -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),
})

0 comments on commit 3c9cbb0

Please sign in to comment.