You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
3.0 KiB
135 lines
3.0 KiB
2 years ago
|
import { pick } from 'lodash';
|
||
|
// import httpStatus from 'http-status';
|
||
|
import messages from '../../../config/messages';
|
||
|
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) => {
|
||
|
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<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 updateUser = Object.assign(
|
||
|
user,
|
||
|
pick(req.body, dataChanged)
|
||
|
);
|
||
|
return User.update(
|
||
|
updateUser,
|
||
|
{
|
||
|
where: {
|
||
|
id: user.id
|
||
|
}
|
||
|
}
|
||
|
).then(() => {
|
||
|
res.json({
|
||
|
code: 0,
|
||
|
message: messages.UPDATE_SUCCESS
|
||
|
});
|
||
|
}).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 updateUser = Object.assign(
|
||
|
user,
|
||
|
{ is_active: true }
|
||
|
);
|
||
|
return User.update(
|
||
|
updateUser,
|
||
|
{
|
||
|
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
|
||
|
});
|
||
|
};
|