Init commit

This commit is contained in:
2024-12-22 16:52:30 +03:30
parent 28ab698dfc
commit aeb38982ca
28 changed files with 1532 additions and 68 deletions
+78
View File
@@ -0,0 +1,78 @@
// userController.js
const catchAsync = require('../../globalErrorHandler/catchAsync')
const userService = require('./userService')
exports.loginUser = catchAsync(async (req, res, next) => {
const user = await userService.loginUser(req.body.rawPassword)
const maxAge = 60 * 60 * 24 * 90
res.cookie('raw-password', user.rawPassword, {
httpOnly: true,
maxAge: maxAge * 1000,
path: '/',
})
res.status(200).json({
status: 'success',
data: user,
})
})
exports.logoutUser = catchAsync(async (req, res, next) => {
res.cookie('raw-password', '', {
httpOnly: true,
maxAge: 0,
path: '/',
})
res.status(200).json({
status: 'success',
message: 'کاربر خارج شد',
})
})
exports.getAllUsers = catchAsync(async (req, res, next) => {
const users = await userService.getAllUsers()
res.status(200).json({
status: 'success',
results: users.length,
data: users,
})
})
exports.getUser = catchAsync(async (req, res, next) => {
const user = await userService.getUser(req.params.userId)
res.status(200).json({
status: 'success',
data: user,
})
})
exports.createUser = catchAsync(async (req, res, next) => {
const user = await userService.createUser(req.body)
res.status(201).json({
status: 'success',
data: user,
})
})
exports.updateUser = catchAsync(async (req, res, next) => {
const user = await userService.updateUser(req.params.userId, req.body)
res.status(200).json({
status: 'success',
data: user,
})
})
exports.deleteUser = catchAsync(async (req, res, next) => {
await userService.deleteUser(req.params.userId)
res.status(204).json({
status: 'success',
data: null,
})
})
+30
View File
@@ -0,0 +1,30 @@
const mongoose = require('mongoose')
const ObjectID = mongoose.Schema.Types.ObjectId
const userSchema = new mongoose.Schema(
{
name: String,
password: {
type: String,
required: true,
trim: true,
},
isActive: {
type: Boolean,
default: true,
},
role: {
type: ObjectID,
ref: 'Role'
},
isAdmin: {
type: Boolean,
default: false,
},
},
{ toJSON: { virtuals: true }, toObject: { virtuals: true }, timestamps: true }
)
const User = mongoose.model('User', userSchema)
module.exports = User
+24
View File
@@ -0,0 +1,24 @@
// userRoutes.js
const express = require('express')
const userController = require('./userController')
const loginCheck = require('../../middlewares/loginCheck')
const router = new express.Router()
router.post('/login', userController.loginUser)
router.post('/logout', userController.logoutUser)
router.use(loginCheck.loginProtect)
// router.use(loginCheck.adminProtect)
router.get('/', userController.getAllUsers)
router.get('/:userId', userController.getUser)
router.post('/', userController.createUser)
router.put('/:userId', userController.updateUser)
router.delete('/:userId', userController.deleteUser)
module.exports = router
+64
View File
@@ -0,0 +1,64 @@
// userService.js
const User = require('./userModel')
const AppError = require('../../globalErrorHandler/AppError')
const loginUser = async (rawPassword) => {
const user = await User.findOne({ rawPassword }, null, null)
if (!user) {
throw new AppError('WrongPassword', 404, {})
}
return user
}
const getAllUsers = async () => {
const users = await User.find({}, null, null)
for (let user of users) {
user.activatedCodes = await codeService.getCodeCount(user._id)
}
return users
}
const getUser = async (userId) => {
const user = await User.findById({ _id: userId }, null, null)
if (!user) {
throw AppError('NoUserFound', 404, {})
}
return user
}
const createUser = async (data) => {
return User.create(data, null)
}
const updateUser = async (userId, data) => {
const user = await User.findOneAndUpdate({ _id: userId }, data, { new: true })
if (!user) {
throw AppError('NoUserFound', 404, {})
}
return user
}
const deleteUser = async (userId) => {
const user = await User.findOneAndDelete({ _id: userId })
if (!user) {
throw AppError('NoUserFound', 404, {})
}
return null
}
module.exports = {
loginUser,
getUser,
getAllUsers,
deleteUser,
updateUser,
createUser,
}