Skip to content

Commit

Permalink
Refactor and fix unseen incrementing bugs
Browse files Browse the repository at this point in the history
Includes:

- Renamed and refactored incrementUnseenCount & incrementUnseenCounts to
upsertQueryUser & upsertQueryUsers as this more accurately reflects what
they do. They may increment the unseen could, but they may just create a
new row
- Added the creation of a queryuser entry when an upload is added, this
is inline with what happens when a new message is created.
- Modified the default value for queryuser.unseen_count. This column
does not have a default value in the DB, we explicitly set it to 0 when
a new row is created. We used to set it to 1, but this is no longer
appropriate since a queryuser row can be created when a new query is
created.
  • Loading branch information
Andrew Isherwood committed Sep 11, 2020
1 parent 7f80d9c commit d6e9325
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
19 changes: 14 additions & 5 deletions resolvers/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ const messageResolvers = {
'INSERT INTO message VALUES (DEFAULT, $1, $2, $3, NOW(), NOW()) RETURNING *',
[body.query_id, body.creator_id, body.content]
);
// Increment the unseen count for those who need it
await queryuser.incrementUnseenCounts(
// Add / update queryuser relationships for those
// that need it
await queryuser.upsertQueryUsers(
{ query_id: body.query_id, creator: user.id }
);
return newMessage;
Expand All @@ -46,11 +47,19 @@ const messageResolvers = {
await queryuser.decrementMessageDelete({ message })
return ret;
},
insertUpload: ({ filename, originalName, queryId, userId }) =>
pool.query(
insertUpload: ({ filename, originalName, queryId, userId }) => {
return pool.query(
'INSERT INTO message (query_id, creator_id, updated_at, filename, originalname) VALUES ($1, $2, NOW(), $3, $4) RETURNING *',
[queryId, userId, filename, originalName]
)
).then(async (result) => {
// Add / update queryuser relationships for those
// that need it
await queryuser.upsertQueryUsers(
{ query_id: queryId, creator: userId }
);
return result;
});
}
};

module.exports = messageResolvers;
32 changes: 16 additions & 16 deletions resolvers/queryuser.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,30 @@ const queryuserResolvers = {
[unseen.rows[0].rowcount, query_id, user_id]
);
},
// Increment the unseen count on a single query / user
incrementUnseenCount: ({ query_id, user_id }) => {
// Upsert a queryuser row
upsertQueryUser: ({ query_id, user_id }) => {
// This query attempts to insert a new row into queryuser, if it conflicts
// it increments the existing row
const sql = 'INSERT INTO queryuser VALUES ($1, $2, NOW(), NOW(), 0, 1) ON CONFLICT ON CONSTRAINT userquery_pkey DO UPDATE SET unseen_count = queryuser.unseen_count + 1 WHERE queryuser.query_id = $3 AND queryuser.user_id = $4';
const sql = 'INSERT INTO queryuser VALUES ($1, $2, NOW(), NOW(), 0, 0) ON CONFLICT ON CONSTRAINT userquery_pkey DO UPDATE SET unseen_count = queryuser.unseen_count + 1 WHERE queryuser.query_id = $3 AND queryuser.user_id = $4';
return pool.query(sql, [query_id, user_id, query_id, user_id]);
},
// Increment the unseen counts for a given query for all users except
// Upsert queryuser rows for a given query for all users except
// the passed one
incrementUnseenCounts: async ({ query_id, creator }) => {
// Determine who's unseen counter needs incrementing. This needs to be anyone
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 toIncrementDeDup = {};
participants.rows.forEach((pRow) => toIncrementDeDup[pRow.creator_id] = 1);
staff.rows.forEach((sRow) => toIncrementDeDup[sRow.id] = 1);
// Remove the sender from the list of users needing their unseen count
// incrementing
delete toIncrementDeDup[creator];
const toIncrement = Object.keys(toIncrementDeDup);
for (let i = 0; i < toIncrement.length; i++) {
await queryuserResolvers.incrementUnseenCount(
{ query_id, user_id: toIncrement[i] }
let upsertDeDup = {};
participants.rows.forEach((pRow) => upsertDeDup[pRow.creator_id] = 1);
staff.rows.forEach((sRow) => upsertDeDup[sRow.id] = 1);
// 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++) {
await queryuserResolvers.upsertQueryUser(
{ query_id, user_id: toUpsert[i] }
);
}
},
Expand Down

0 comments on commit d6e9325

Please sign in to comment.