Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show presence of users in user management #165

Merged
merged 1 commit into from
Jul 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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