This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
2021-10-24 04:06:16 +02:00

31 lines
868 B
TypeScript

import { Request, Response, Router } from 'express'
import User from '../../../models/User'
import UserSetting from '../../../models/UserSetting'
import { NotFoundError } from '../../../tools/errors/NotFoundError'
export const getUsersRouter = Router()
getUsersRouter.get(
'/users/:userId',
[],
async (req: Request, res: Response) => {
const { userId } = req.params as { userId: string }
const user = await User.findOne({ where: { id: userId } })
if (user == null) {
throw new NotFoundError()
}
const userSettings = await UserSetting.findOne({
where: { userId: user.id }
})
if (userSettings == null) {
throw new NotFoundError()
}
const result = Object.assign({}, user.toJSON())
if (!userSettings.isPublicEmail) {
delete result.email
}
return res.status(200).json({ user: result })
}
)