diff --git a/src/client/containers/Modals/CreateTicketModal.jsx b/src/client/containers/Modals/CreateTicketModal.jsx index 157143b74..966b93ad0 100644 --- a/src/client/containers/Modals/CreateTicketModal.jsx +++ b/src/client/containers/Modals/CreateTicketModal.jsx @@ -21,6 +21,8 @@ import { head, orderBy } from 'lodash' import axios from 'axios' import Log from '../../logger' import { createTicket } from 'actions/tickets' +import { fetchGroups } from 'actions/groups' + import $ from 'jquery' import helpers from 'lib/helpers' import socket from 'lib/socket' @@ -44,6 +46,7 @@ class CreateTicketModal extends React.Component { } componentDidMount () { + this.props.fetchGroups() helpers.UI.inputs() helpers.formvalidator() this.defaultTicketTypeWatcher = when( @@ -131,9 +134,12 @@ class CreateTicketModal extends React.Component { render () { const { viewdata } = this.props - const mappedGroups = this.props.viewdata.groups.map(grp => { - return { text: grp.name, value: grp._id } - }) + const mappedGroups = this.props.groups + .map(grp => { + return { text: grp.get('name'), value: grp.get('_id') } + }) + .toArray() + const mappedTicketTypes = this.props.viewdata.ticketTypes.map(type => { return { text: type.name, value: type._id } }) @@ -262,14 +268,17 @@ class CreateTicketModal extends React.Component { CreateTicketModal.propTypes = { viewdata: PropTypes.object.isRequired, - createTicket: PropTypes.func.isRequired + groups: PropTypes.object.isRequired, + createTicket: PropTypes.func.isRequired, + fetchGroups: PropTypes.func.isRequired } const mapStateToProps = state => ({ - viewdata: state.common + viewdata: state.common, + groups: state.groupsState.groups }) export default connect( mapStateToProps, - { createTicket } + { createTicket, fetchGroups } )(CreateTicketModal) diff --git a/src/client/containers/Modals/EditTeamModal.jsx b/src/client/containers/Modals/EditTeamModal.jsx index 85111c13f..154049c63 100644 --- a/src/client/containers/Modals/EditTeamModal.jsx +++ b/src/client/containers/Modals/EditTeamModal.jsx @@ -34,7 +34,7 @@ class EditTeamModal extends React.Component { @observable members = [] componentDidMount () { - this.props.fetchAccounts() + this.props.fetchAccounts({ type: 'all' }) this.name = this.props.team.name helpers.UI.inputs() diff --git a/src/client/reducers/accountsReducer.js b/src/client/reducers/accountsReducer.js index 94da47ead..8f15e49ad 100644 --- a/src/client/reducers/accountsReducer.js +++ b/src/client/reducers/accountsReducer.js @@ -38,7 +38,7 @@ const reducer = handleActions( return { ...state, accounts: fromJS(arr), - type: action.payload.payload.type + type: action.payload.payload && action.payload.payload.type ? action.payload.payload.type : 'customers' } }, diff --git a/src/client/reducers/groupsReducer.js b/src/client/reducers/groupsReducer.js index cd8be4b44..830fe47f5 100644 --- a/src/client/reducers/groupsReducer.js +++ b/src/client/reducers/groupsReducer.js @@ -23,9 +23,10 @@ const initialState = { const reducer = handleActions( { [FETCH_GROUPS.SUCCESS]: (state, action) => { + const groups = fromJS(action.response.groups) return { ...state, - groups: fromJS(action.response.groups) + groups: groups.sortBy(group => group.get('name')) } }, diff --git a/src/controllers/api/v1/groups.js b/src/controllers/api/v1/groups.js index fc4b59ad3..86de29858 100644 --- a/src/controllers/api/v1/groups.js +++ b/src/controllers/api/v1/groups.js @@ -42,21 +42,34 @@ apiGroups.get = function (req, res) { var permissions = require('../../../permissions') var hasPublic = permissions.canThis(user.role, 'tickets:public') - GroupSchema.getAllGroupsOfUser(user._id, function (err, groups) { - if (err) return res.status(400).json({ success: false, error: err.message }) + if (user.role.isAgent || user.role.isAdmin) { + GroupSchema.getAllGroups(function (err, groups) { + if (err) return res.status(400).json({ success: false, error: err.message }) - if (hasPublic) { - GroupSchema.getAllPublicGroups(function (err, grps) { - if (err) return res.status(400).json({ success: false, error: err }) + if (!hasPublic) + groups = _.filter(function (g) { + return !g.public + }) - groups = groups.concat(grps) + return res.json({ success: true, groups: groups }) + }) + } else { + GroupSchema.getAllGroupsOfUser(user._id, function (err, groups) { + if (err) return res.status(400).json({ success: false, error: err.message }) + if (hasPublic) { + GroupSchema.getAllPublicGroups(function (err, grps) { + if (err) return res.status(400).json({ success: false, error: err }) + + groups = groups.concat(grps) + + return res.json({ success: true, groups: groups }) + }) + } else { return res.json({ success: true, groups: groups }) - }) - } else { - return res.json({ success: true, groups: groups }) - } - }) + } + }) + } } /** diff --git a/src/controllers/api/v2/accounts.js b/src/controllers/api/v2/accounts.js index 83c271dbb..e5d5ff90e 100644 --- a/src/controllers/api/v2/accounts.js +++ b/src/controllers/api/v2/accounts.js @@ -34,6 +34,13 @@ accountsApi.get = function (req, res) { } switch (type) { + case 'all': + User.getUserWithObject(obj, function (err, accounts) { + if (err) return apiUtil.sendApiError(res, 500, err.message) + + return apiUtil.sendApiSuccess(res, { accounts: accounts, count: accounts.length }) + }) + break case 'customers': User.getCustomers(obj, function (err, accounts) { if (err) return apiUtil.sendApiError(res, 500, err.message) diff --git a/src/models/user.js b/src/models/user.js index de4ccae03..c6baf5a9f 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -432,7 +432,7 @@ userSchema.statics.getUserWithObject = function (object, callback) { q.where({ fullname: new RegExp('^' + search.toLowerCase(), 'i') }) } - q.exec(callback) + return q.exec(callback) } /**