Skip to content

Commit

Permalink
chore(accounts): account creation for new system
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Apr 14, 2019
1 parent 35dbef8 commit 82c2f44
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/client/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ api.tickets.createTag = ({ name }) => {

api.accounts = {}
api.accounts.create = payload => {
return axios.post('/api/v1/users/create', payload).then(res => {
return axios.post('/api/v2/accounts', payload).then(res => {
return res.data
})
}
Expand Down
1 change: 1 addition & 0 deletions src/client/containers/Groups/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class GroupsContainer extends React.Component {
<p style="font-size: 12px;">
Agents may lose access to resources once this group is deleted.
</p>
<span>Groups that are associated with ticket cannot be deleted.</span>
`,
() => {
this.props.deleteGroup({ _id })
Expand Down
31 changes: 17 additions & 14 deletions src/client/containers/Modals/CreateAccountModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class CreateAccountModal extends React.Component {
else this.roleSelectErrorMessage.classList.add('hide')
}
onGroupSelectChange (values) {
onGroupSelectChange () {
const selectedGroups = this.groupSelect.getSelected()
if (!selectedGroups || selectedGroups.length < 1) this.groupSelectErrorMessage.classList.remove('hide')
else this.groupSelectErrorMessage.classList.add('hide')
Expand All @@ -91,23 +91,26 @@ class CreateAccountModal extends React.Component {
if (isValid) isValid = false
} else this.roleSelectErrorMessage.classList.add('hide')
const selectedGroups = this.groupSelect.getSelected()
if (!selectedGroups || selectedGroups.length < 1) {
this.groupSelectErrorMessage.classList.remove('hide')
if (isValid) isValid = false
} else this.groupSelectErrorMessage.classList.add('hide')
const selectedGroups = this.groupSelect ? this.groupSelect.getSelected() : undefined
if (selectedGroups) {
if (selectedGroups.length < 1) {
this.groupSelectErrorMessage.classList.remove('hide')
if (isValid) isValid = false
} else this.groupSelectErrorMessage.classList.add('hide')
}
if (!isValid) return
const payload = {
aUsername: this.username,
aPass: this.password,
aPassConfirm: this.passwordConfirm,
aFullname: this.fullname,
aTitle: this.title,
aEmail: this.email,
aRole: this.selectedRole,
aGrps: this.groupSelect.getSelected()
username: this.username,
fullname: this.fullname,
title: this.title,
email: this.email,
groups: this.groupSelect ? this.groupSelect.getSelected() : undefined,
teams: this.teamSelect ? this.teamSelect.getSelected() : undefined,
role: this.selectedRole,
password: this.password.length > 1 ? this.password : undefined,
passwordConfirm: this.passwordConfirm.length > 1 ? this.passwordConfirm : undefined
}
this.props.createAccount(payload)
Expand Down
2 changes: 1 addition & 1 deletion src/client/containers/Modals/EditDepartmentModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class EditDepartmentModal extends React.Component {
componentDidMount () {
this.props.fetchTeams()
this.props.fetchGroups()
this.props.fetchGroups({ type: 'all' })
this.name = this.props.department.get('name')
this.allGroups = this.props.department.get('allGroups')
Expand Down
6 changes: 3 additions & 3 deletions src/client/containers/TicketsContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ class TicketsContainer extends React.Component {
const cells = []
for (let k = 0; k < 10; k++) {
cells.push(
<TableCell className={'vam'}>
<TableCell key={k} className={'vam'}>
<div className={'loadingTextAnimation'} />
</TableCell>
)
}

loadingItems.push(<TableRow>{cells}</TableRow>)
loadingItems.push(<TableRow key={Math.random()}>{cells}</TableRow>)
}

return (
Expand Down Expand Up @@ -253,7 +253,7 @@ class TicketsContainer extends React.Component {
<TableHeader key={3} width={'23%'} text={'Subject'} />,
<TableHeader key={4} width={110} text={'Created'} />,
<TableHeader key={5} width={125} text={'Requester'} />,
<TableHeader key={6} text={'Customer'} />,
<TableHeader key={6} width={175} text={'Customer'} />,
<TableHeader key={7} text={'Assignee'} />,
<TableHeader key={8} width={110} text={'Due Date'} />,
<TableHeader key={9} text={'Updated'} />
Expand Down
6 changes: 6 additions & 0 deletions src/client/reducers/accountsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ const reducer = handleActions(

[CREATE_ACCOUNT.SUCCESS]: (state, action) => {
const resAccount = action.response.account

if (!resAccount.role.isAgent && !resAccount.role.isAdmin && state.type !== 'customers') return { ...state }
if (resAccount.role.isAgent || (resAccount.role.isAdmin && state.type === 'customers')) return { ...state }
if (resAccount.role.isAdmin && !resAccount.role.isAgent && state.type === 'agents') return { ...state }
if (resAccount.role.isAgent && !resAccount.role.isAdmin && state.type === 'admins') return { ...state }

const insertedAccount = state.accounts.push(fromJS(resAccount))
return {
...state,
Expand Down
93 changes: 93 additions & 0 deletions src/controllers/api/v2/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,99 @@ var accountsApi = {}

accountsApi.create = function (req, res) {
var postData = req.body
if (!postData) return apiUtil.sendApiError_InvalidPostData(res)

var savedId = null

async.series(
{
user: function (next) {
User.create(
{
username: postData.username,
email: postData.email,
password: postData.password,
fullname: postData.fullname,
title: postData.title,
role: postData.role
},
function (err, user) {
if (err) return apiUtil.sendApiError(res, 500, err.message)

savedId = user._id

return user.populate('role', next)
}
)
},
groups: function (next) {
if (!postData.groups) return next(null, [])

Group.getGroups(postData.groups, function (err, groups) {
if (err) return next(err)

async.each(
groups,
function (group, callback) {
group.addMember(savedId, function (err) {
if (err) return callback(err)
group.save(callback)
})
},
function (err) {
if (err) return next(err)

return next(null, groups)
}
)
})
},
teams: function (next) {
if (!postData.teams) return next()

Team.getTeamsByIds(postData.teams, function (err, teams) {
if (err) return next(err)

async.each(
teams,
function (team, callback) {
team.addMember(savedId, function () {
team.save(callback)
})
},
function (err) {
if (err) return next(err)

return next(null, teams)
}
)
})
},
departments: function (next) {
Department.getUserDepartments(savedId, next)
}
},
function (err, results) {
if (err) return apiUtil.sendApiError(res, 500, err.message)

var user = results.user.toJSON()
user.groups = results.groups.map(function (g) {
return { _id: g._id, name: g.name }
})

if ((user.role.isAgent || user.role.isAdmin) && results.teams) {
user.teams = results.teams.map(function (t) {
return { _id: t._id, name: t.name }
})

user.departments = results.departments.map(function (d) {
return { _id: d._id, name: d.name }
})
}

return apiUtil.sendApiSuccess(res, { account: user })
}
)
}

accountsApi.get = function (req, res) {
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/api/v2/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ module.exports = function (middleware, router, controllers) {
router.post('/api/v2/token', controllers.api.v2.common.token)

// Accounts
router.get('/api/v2/accounts', middleware.api, apiv2.accounts.get)
router.get('/api/v2/accounts', apiv2Auth, apiv2.accounts.create)
router.put('/api/v2/accounts/:username', middleware.api, apiv2.accounts.update)
router.get('/api/v2/accounts', apiv2Auth, apiv2.accounts.get)
router.post('/api/v2/accounts', apiv2Auth, apiv2.accounts.create)
router.put('/api/v2/accounts/:username', apiv2Auth, apiv2.accounts.update)

// Tickets
router.get('/api/v2/tickets', apiv2Auth, apiv2.tickets.get)
Expand Down
10 changes: 10 additions & 0 deletions src/models/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ groupSchema.statics.getAllPublicGroups = function (callback) {
return q.exec(callback)
}

groupSchema.statics.getGroups = function (groupIds, callback) {
if (_.isUndefined(groupIds)) return callback('Invalid Array of Group IDs - GroupSchema.GetGroups()')

this.model(COLLECTION)
.find({ _id: { $in: groupIds } })
.populate('members', '_id username fullname email role preferences image title')
.sort('name')
.exec(callback)
}

groupSchema.statics.getAllGroupsOfUser = function (userId, callback) {
if (_.isUndefined(userId)) return callback('Invalid UserId - GroupSchema.GetAllGroupsOfUser()')

Expand Down
8 changes: 8 additions & 0 deletions src/models/team.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ teamSchema.statics.getTeams = function (callback) {
return q.exec(callback)
}

teamSchema.statics.getTeamsByIds = function (ids, callback) {
return this.model(COLLECTION)
.find({ _id: { $in: ids } })
.populate('members', '_id username fullname email image title')
.sort('name')
.exec(callback)
}

teamSchema.statics.getTeamsNoPopulate = function (callback) {
var q = this.model(COLLECTION)
.find({})
Expand Down
4 changes: 2 additions & 2 deletions src/sass/partials/loader.sass
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@
display: block
height: 10px
width: 75%
+borderRadius(5px)
+borderRadius(5px)
background: darken($page_content_border_color, 7%)
animation: shim 1.5s infinite linear
animation: shim 1s infinite linear

@keyframes shim
1%
Expand Down

0 comments on commit 82c2f44

Please sign in to comment.