Skip to content

Commit

Permalink
Show presence of users in user management
Browse files Browse the repository at this point in the history
  • Loading branch information
googol committed Jul 21, 2016
1 parent 3850a26 commit a663ad9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/client/actions/RegistryUserActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function getRegistryUserActions(alt, registryUserResource, errorActions)
loadRegistryUserList() {
return dispatch => {
dispatch();
registryUserResource.findAll()
registryUserResource.findAll('includePresence=true')
.then(registryUserList => this.registryUserListUpdated(registryUserList),
err => errorActions.error(err, 'Käyttäjiä ei voitu ladata'));
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { Table, Button } from 'react-bootstrap';
import { Presence } from '../../components';

const RegistryUserRow = props => {
const {
Expand All @@ -15,6 +16,7 @@ const RegistryUserRow = props => {
phoneNumber,
email,
status,
presence,
} = registryUser;

const blockStatusToggleButton = status === 'blocked'
Expand All @@ -23,6 +25,7 @@ const RegistryUserRow = props => {

return (
<tr>
<td><Presence value={ presence } /></td>
<td>{ `${firstName} ${lastName}` }</td>
<td>{ memberNumber }</td>
<td>{ phoneNumber }</td>
Expand All @@ -49,6 +52,7 @@ export function RegistryUserTable(props) {
<Table striped responsive condensed>
<thead>
<tr>
<th>Tila</th>
<th>Nimi</th>
<th>Jäsennumero</th>
<th>Puhelinnumero</th>
Expand Down
25 changes: 25 additions & 0 deletions src/common/models/registryuser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import app from '../../server/server.js';
import Promise from 'bluebird';
import loopback from 'loopback';
import crypto from 'crypto';
import _ from 'lodash';

export default function(Registryuser) {
Registryuser.afterRemote('create', (ctx, registryuserInstance, next) => {
Expand Down Expand Up @@ -61,6 +62,30 @@ export default function(Registryuser) {
updateRegistryUser({ id: userId }, { status: null }).asCallback(callback);
};

Registryuser.afterRemote('find', (ctx, instance, next) => {
Promise.try(() => {
const includePresence = ctx && ctx.result && ctx.req && ctx.req.query && ctx.req.query.includePresence;
if (includePresence) {
const findParticipants = Promise.promisify(app.models.Participant.find, { context: app.models.Participant });

const memberNumbers = ctx.result.map(user => user.memberNumber);

// To object is an undocumented function of loopback models, which is used here to force the model object into a plain javascript object.
// Without the call setting a new property on the objects below wouldn't work.
const keyedUsers = _.keyBy(ctx.result.map(user => user.toObject()), 'memberNumber');

return findParticipants({ where: { memberNumber: { inq: memberNumbers } } })
.each(participant => {
const user = keyedUsers[participant.memberNumber];
if (user) {
user.presence = participant.presence;
}
})
.tap(() => ctx.result = _.values(keyedUsers));
}
}).asCallback(next);
});

Registryuser.remoteMethod('block',
{
http: { path: '/:id/block', verb: 'post' },
Expand Down

0 comments on commit a663ad9

Please sign in to comment.