Init commit
This commit is contained in:
@@ -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,
|
||||
}
|
||||
Reference in New Issue
Block a user