import { hash } from 'bcryptjs'; import { pick } from 'lodash'; // import httpStatus from 'http-status'; import messages from '../../../config/messages'; // import { hash, compare } from 'bcryptjs'; import { handler as ErrorHandler } from '../../middlewares/error'; import User from '../../../common/models/user.model'; import uploadAdapter from '../../../common/services/adapters/upload-adapter'; /** * Create * * @public * @param {StorySchema} body * @returns {Promise, APIException>} */ exports.create = async (req, res, next) => { // transform data req.body.created_by = pick(req.user, ['id', 'name']); const params = req.body; params.type = User.Types.INDIVIDUAL; params.service = User.Services.INDIVIDUAL; // save data await User.create(req.body) .then(data => { uploadAdapter.createDefaultFolder({ id: data.id }); res.json({ code: 0, message: messages.CREATE_SUCCESS, data: User.transform(data) }); }).catch(ex => { ErrorHandler(ex, req, res, next); }); }; /** * List * * @public * @param {StorySchema} query * @returns {Promise, APIException>} */ exports.list = async (req, res, next) => { req.query.services = User.Services.USER; User.list( req.query ).then(result => { res.json({ code: 0, count: req.totalRecords, data: result.map( x => User.transform(x) ) }); }).catch(ex => { ErrorHandler(ex, req, res, next); }); }; /** * Detail * * @public * @param {params} userId, * @returns {Promise, APIException>} */ exports.get = async (req, res, next) => res.json({ data: User.transform(req.locals.user) }); /** * Update * * @public * @param {params} userId * @returns {Promise, APIException>} */ exports.update = async (req, res, next) => { const { user } = req.locals; const dataChanged = User.getChangedProperties(req.body); const new_properties = pick(req.body, dataChanged); // const updateUser = Object.assign( // user, // pick(req.body, dataChanged) // ); // const currentUser = await User.get(user.id) // console.log(dataChanged); return User.update( new_properties, { where: { id: user.id } } ).then(() => { res.json({ code: 0, message: messages.UPDATE_SUCCESS, // dataChanged: dataChanged }); }).catch(ex => { ErrorHandler(ex, req, res, next); }); }; /** * delete * * @public * @param {params} userId * @returns {Promise, APIException>} */ exports.delete = async (req, res, next) => { const { user } = req.locals; // const new_user = Object.assign( // user, // isactive : false // ) return User.update( { is_active: false}, { where: { id: user.id } } ).then( () => { res.json({ code: 0, message: messages.REMOVE_SUCCESS }); }).catch(ex => { ErrorHandler(ex, req, res, next); }); }; exports.getStaffPermission = async (req, res, next) => { const { story } = req.locals; return res.json({ code: 0, data: story }); }; // exports.addRole = async(req,res,next) => { // const {user} = req.locals; // const admin = ['administrator']; // // const user.permissions[0] = admin; // return User.update({ // permissions: admin, // service: 'administrator' // }, // { // where : {id : user.id} // } // ).then( async () => { // const new_user = await User.get(req.params.id); // console.log(new_user); // res.json( // { // code: 0, // messages: messages.CREATE_SUCCESS // } // ); // }).catch( ex => { // ErrorHandler(ex, req, res, next); // }) // } exports.updatePassword = async(req,res,next) => { const {new_password} = req.body; const {user} = req.locals; // console.log(user); if (user) { const rounds = 10; const new_pass = await hash(new_password, rounds); return User.update( {password: new_pass}, { where: { id: user.id } }, ).then( async () => { res.json({ code: 0, message: messages.UPDATE_SUCCESS }); }).catch(ex => { ErrorHandler(ex, req, res, next); }); }; };