-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In the response to the request to add or remove a label to/from a query, we need to return the updated query object. In order to achieve this in a way that didn't lead to a tonne of duplicate code, I have refactored the 'addEmbeds' function into a new helper. Also, the controllers for adding a label to a query, and removing a label from a query were, unsurprisingly, almost identical. I have therefore replaced them with a single function. Includes: - addLabelToQuery & removeLabelFromQuery replaced with addRemove - The relevant router now passes an argument to addRemove to indicate what operation it should carry out - New helpers/queries.js helper - New units tests where appropriate - Modified unit tests where appropriate
- Loading branch information
Andrew Isherwood
committed
Jul 7, 2020
1 parent
76b213e
commit c3f930a
Showing
7 changed files
with
260 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,33 @@ | ||
const db = require('../../ems-db'); | ||
|
||
const helpers = require('../helpers/queries'); | ||
|
||
const querylabel = { | ||
addLabelToQuery: (req, res, next) => | ||
db.resolvers.querylabel | ||
.addLabelToQuery(req) | ||
.then((result) => { | ||
if (result.rowCount === 1) { | ||
res.status(201); | ||
res.json(result.rows[0]); | ||
next(); | ||
} else { | ||
res.status(500); | ||
res.send(); | ||
next(); | ||
} | ||
}) | ||
.catch((err) => next(err)), | ||
removeLabelFromQuery: (req, res, next) => | ||
db.resolvers.querylabel | ||
.removeLabelFromQuery(req) | ||
.then((result) => { | ||
if (result.rowCount === 0) { | ||
res.status(404); | ||
res.send(); | ||
next(); | ||
} else if (result.rowCount === 1) { | ||
res.status(204); | ||
res.json({}); | ||
next(); | ||
} else { | ||
res.status(500); | ||
res.send(); | ||
next(); | ||
} | ||
}) | ||
.catch((err) => next(err)) | ||
addRemove: (req, res, next, action) => { | ||
const func = db.resolvers.querylabel[action]; | ||
return func(req).then(async (response) => { | ||
if (response.rowCount === 0) { | ||
res.status(404); | ||
res.send(); | ||
next(); | ||
} else if (response.rowCount === 1) { | ||
// Fetch and return the updated query object | ||
const queries = await db.resolvers.queries.getQuery({ | ||
params: { id: req.params.query_id } | ||
}); | ||
const toSend = await helpers.addEmbeds(queries); | ||
res.status(200); | ||
res.json(toSend[0]); | ||
next(); | ||
} else { | ||
res.status(500); | ||
res.send(); | ||
next(); | ||
} | ||
}) | ||
.catch((err) => next(err)); | ||
|
||
} | ||
}; | ||
|
||
module.exports = querylabel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const db = require('../../ems-db'); | ||
|
||
const queries = { | ||
addEmbeds: async (queries) => { | ||
// We need the IDs of all queries we received | ||
const query_ids = queries.rows.map(query => query.id); | ||
// Don't proceed if we don't need to | ||
if (query_ids.length === 0) { | ||
return []; | ||
} | ||
// Now get the initiators for all the queries we've | ||
// received, we also receive their associated query ID | ||
const initiators = await db.resolvers.queries.initiators( | ||
query_ids | ||
); | ||
// Now get the participants of all retrieved queries | ||
const participants = await db.resolvers.queries.participants( | ||
query_ids | ||
); | ||
// Now get the labels for all retrieved queries | ||
const labels = await db.resolvers.queries.labels( | ||
query_ids | ||
); | ||
// Finally get the most recent message for each retrieved query | ||
const latest = await db.resolvers.queries.latestMessages( | ||
query_ids | ||
); | ||
// Now we have everything, we can bundle it all up together | ||
return queries.rows.map(query => { | ||
// The initiator for this query | ||
const queryInitiator = initiators.rows.find( | ||
init => init.id === query.id | ||
).initiator; | ||
// The participants of this query | ||
const queryParticipants = participants.rows.filter( | ||
participant => participant.query_id === query.id | ||
).map(final => final.creator_id); | ||
const queryLabels = labels.rows.filter( | ||
label => label.query_id === query.id | ||
).map(final => final.label_id); | ||
// The most recent message for this query | ||
const queryLatest = latest.rows.find( | ||
latestMessage => latestMessage.query_id === query.id | ||
); | ||
return { | ||
...query, | ||
initiator: queryInitiator, | ||
participants: queryParticipants, | ||
latestMessage: queryLatest, | ||
labels: queryLabels | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = queries; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.