1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-09 23:40:06 +00:00

chore: added incremental field 'id' in user collection

This commit is contained in:
Saad Jutt
2021-11-05 03:07:20 +05:00
parent 882f36d30e
commit 2b7dfeb2ea
15 changed files with 289 additions and 168 deletions

View File

@@ -15,11 +15,13 @@ import bcrypt from 'bcryptjs'
import User, { UserPayload } from '../model/User'
interface userResponse {
id: number
username: string
displayName: string
}
interface userDetailsResponse {
id: number
displayName: string
username: string
isActive: boolean
@@ -34,10 +36,12 @@ export default class UserController {
*/
@Example<userResponse[]>([
{
id: 123,
username: 'johnusername',
displayName: 'John'
},
{
id: 456,
username: 'starkusername',
displayName: 'Stark'
}
@@ -52,6 +56,7 @@ export default class UserController {
*
*/
@Example<userDetailsResponse>({
id: 1234,
displayName: 'John Snow',
username: 'johnSnow01',
isAdmin: false,
@@ -64,42 +69,55 @@ export default class UserController {
return createUser(body)
}
/**
* Get user properties - such as group memberships, userName, displayName.
* @param userId The user's identifier
* @example userId 1234
*/
@Get('{userId}')
public async getUser(@Path() userId: number): Promise<userDetailsResponse> {
return getUser(userId)
}
/**
* Update user properties - such as displayName. Can be performed either by admins, or the user in question.
* @param username The user's identifier
* @example username "johnSnow01"
* @param userId The user's identifier
* @example userId "1234"
*/
@Example<userDetailsResponse>({
id: 1234,
displayName: 'John Snow',
username: 'johnSnow01',
isAdmin: false,
isActive: true
})
@Patch('{username}')
@Patch('{userId}')
public async updateUser(
@Path() username: string,
@Path() userId: number,
@Body() body: UserPayload
): Promise<userDetailsResponse> {
return updateUser(username, body)
return updateUser(userId, body)
}
/**
* Delete a user. Can be performed either by admins, or the user in question.
* @param username The user's identifier
* @example username "johnSnow01"
* @param userId The user's identifier
* @example userId 1234
*/
@Delete('{username}')
@Delete('{userId}')
public async deleteUser(
@Path() username: string,
@Path() userId: number,
@Body() body: { password?: string },
@Query() @Hidden() isAdmin: boolean = false
) {
return deleteUser(username, isAdmin, body)
return deleteUser(userId, isAdmin, body)
}
}
const getAllUsers = async () =>
await User.find({}).select({ _id: 0, username: 1, displayName: 1 }).exec()
const getAllUsers = async (): Promise<userResponse[]> =>
await User.find({})
.select({ _id: 0, id: 1, username: 1, displayName: 1 })
.exec()
const createUser = async (data: any): Promise<userDetailsResponse> => {
const { displayName, username, password, isAdmin, isActive } = data
@@ -123,26 +141,29 @@ const createUser = async (data: any): Promise<userDetailsResponse> => {
const savedUser = await user.save()
return {
displayName: savedUser.displayName,
username: savedUser.username,
isAdmin: savedUser.isAdmin,
isActive: savedUser.isActive
}
return savedUser
}
const updateUser = async (currentUsername: string, data: any) => {
const getUser = async (id: number) => {
const user = await User.findOne({ id })
.select({
_id: 0,
id: 1,
username: 1,
displayName: 1,
isAdmin: 1,
isActive: 1
})
.exec()
if (!user) throw new Error('User is not found.')
return user
}
const updateUser = async (id: number, data: any) => {
const { displayName, username, password, isAdmin, isActive } = data
const params: any = { displayName, isAdmin, isActive }
if (username && currentUsername !== username) {
// Checking if username is already in the database
const usernameExist = await User.findOne({ username })
if (usernameExist) throw new Error('Username already exists.')
params.username = username
}
const params: any = { displayName, username, isAdmin, isActive }
if (password) {
// Hash passwords
@@ -150,31 +171,31 @@ const updateUser = async (currentUsername: string, data: any) => {
params.password = await bcrypt.hash(password, salt)
}
const updatedUser = await User.findOneAndUpdate(
{ username: currentUsername },
params,
{ new: true }
)
const updatedUser = await User.findOneAndUpdate({ id }, params, { new: true })
.select({
_id: 0,
id: 1,
username: 1,
displayName: 1,
isAdmin: 1,
isActive: 1
})
.exec()
if (!updatedUser) throw new Error('Unable to update user')
return {
displayName: updatedUser.displayName,
username: updatedUser.username,
isAdmin: updatedUser.isAdmin,
isActive: updatedUser.isActive
}
return updatedUser
}
const deleteUser = async (username: string, isAdmin: boolean, data: any) => {
const deleteUser = async (id: number, isAdmin: boolean, data: any) => {
const { password } = data
const user = await User.findOne({ username })
if (!user) throw new Error('Username is not found.')
const user = await User.findOne({ id })
if (!user) throw new Error('User is not found.')
if (!isAdmin) {
const validPass = await bcrypt.compare(password, user.password)
if (!validPass) throw new Error('Invalid password.')
}
await User.deleteOne({ username })
await User.deleteOne({ id })
}