Skip to content

Commit

Permalink
Add 'associated' resolver and refactor
Browse files Browse the repository at this point in the history
This commit adds a new resolver 'associated' that returns an array of
IDs of users associated with the passed queries, i.e. users directly
associated and all staff. As part of this, refactoring was necessary so
other resovlers could make use of this.

Includes:
- 'associated' resolver
- 'allStaff' resolver
- Modify upsertQueryUsers resolver to use 'associated' and 'allStaff'
  • Loading branch information
Andrew Isherwood committed Sep 16, 2020
1 parent 7386eef commit 7ac473d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
14 changes: 14 additions & 0 deletions resolvers/queries.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const pool = require('../config');

const userResolvers = require('./users');

// We refactor this out of the resolvers object so we can reference it
// from queryResolvers.upsertQuery & queryResolvers.updateBulk
const upsertQuery = ({ params, body }) => {
Expand Down Expand Up @@ -91,6 +93,18 @@ const queryResolvers = {
},
// The following functions return data that is based on a query,
// as such they don't receive the request object from the API
//
// Associated is all users who can access the passed queries, either people
// who are directly connected with them or staff, who can see any query.
// Returns a promise resolving to an array of user IDs
associated: async (query_ids) => {
const participants = await queryResolvers.participants(query_ids);
const allStaff = await userResolvers.allStaff();
let deDup = {};
participants.rows.forEach((pRow) => (deDup[pRow.creator_id] = 1));
allStaff.rows.forEach((sRow) => (deDup[sRow.id] = 1));
return Object.keys(deDup).map((key) => parseInt(key));
},
initiators: (query_ids) => {
const placeholders = query_ids
.map((param, idx) => `$${idx + 1}`)
Expand Down
14 changes: 4 additions & 10 deletions resolvers/queryuser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const pool = require('../config');
const queries = require('./queries');
const users = require('./users');

const queryuserResolvers = {
updateMostRecentSeen: async ({ params, body }) => {
Expand Down Expand Up @@ -42,19 +41,14 @@ const queryuserResolvers = {
upsertQueryUsers: async ({ query_id, creator }) => {
// Determine who needs adding / updating. This needs to be anyone
// who can see this query, i.e. participants & STAFF, except the sender
const participants = await queries.participants([query_id]);
const staff = await users.allUsers({ query: { role_code: 'STAFF' } });
let upsertDeDup = {};
participants.rows.forEach((pRow) => (upsertDeDup[pRow.creator_id] = 1));
staff.rows.forEach((sRow) => (upsertDeDup[sRow.id] = 1));
const users = await queries.associated([query_id]);
// Remove the sender from the list of users needing
// creating / updating
delete upsertDeDup[creator];
const toUpsert = Object.keys(upsertDeDup);
for (let i = 0; i < toUpsert.length; i++) {
delete users[creator];
for (let i = 0; i < users.length; i++) {
await queryuserResolvers.upsertQueryUser({
query_id,
user_id: toUpsert[i]
user_id: users[i]
});
}
},
Expand Down
3 changes: 3 additions & 0 deletions resolvers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const userResolvers = {
sql += ' ORDER BY name ASC';
return pool.query(sql, params);
},
allStaff: () => {
return userResolvers.allUsers({ query: { role_code: 'STAFF' } });
},
getUser: ({ params }) =>
pool.query(
'SELECT u.*, r.code AS role_code FROM ems_user u INNER JOIN role r ON r.id = u.role_id WHERE u.id = $1',
Expand Down

0 comments on commit 7ac473d

Please sign in to comment.