|
|
|
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<StorySchema>, 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<StorySchema[]>, APIException>}
|
|
|
|
*/
|
|
|
|
exports.list = async (req, res, next) => {
|
|
|
|
req.query.services = User.Services.USER;
|
|
|
|
// console.log(req.query.services);
|
|
|
|
// User.list( {
|
|
|
|
// service : "user",
|
|
|
|
// is_active: true
|
|
|
|
// }
|
|
|
|
// ).then(result => {
|
|
|
|
// res.json({
|
|
|
|
// code: 0,
|
|
|
|
// count: req.totalRecords,
|
|
|
|
// data: result.map(
|
|
|
|
// x => User.transform(x)
|
|
|
|
// )
|
|
|
|
// });
|
|
|
|
// }).catch(ex => {
|
|
|
|
// ErrorHandler(ex, req, res, next);
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
return User.findAll({
|
|
|
|
where : {
|
|
|
|
service : "user",
|
|
|
|
is_active: true
|
|
|
|
}
|
|
|
|
}).then(result => {
|
|
|
|
res.json({
|
|
|
|
code: 0,
|
|
|
|
count: req.totalRecords,
|
|
|
|
data: result
|
|
|
|
});
|
|
|
|
}).catch(ex => {
|
|
|
|
ErrorHandler(ex, req, res, next);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detail
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @param {params} userId,
|
|
|
|
* @returns {Promise<StorySchema>, APIException>}
|
|
|
|
*/
|
|
|
|
exports.get = async (req, res, next) => res.json({ data: User.transform(req.locals.user) });
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @param {params} userId
|
|
|
|
* @returns {Promise<any>, 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<any>, 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.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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|