1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 05:09:43 +02:00

feat: Display comments total on front of cards (#1146)

Closes #1136
This commit is contained in:
Hannes 2025-05-23 17:31:28 +02:00 committed by GitHub
parent f9d3e73651
commit 4049b4c396
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 165 additions and 13 deletions

View file

@ -10,7 +10,25 @@ const defaultFind = (criteria, { limit } = {}) =>
/* Query methods */
const createOne = (values) => Comment.create({ ...values }).fetch();
const createOne = (values) =>
sails.getDatastore().transaction(async (db) => {
const comment = await Comment.create({ ...values })
.fetch()
.usingConnection(db);
const queryResult = await sails
.sendNativeQuery(
'UPDATE card SET comments_total = comments_total + 1, updated_at = $1 WHERE id = $2',
[new Date().toISOString(), comment.cardId],
)
.usingConnection(db);
if (queryResult.rowCount === 0) {
throw 'cardNotFound';
}
return comment;
});
const getByIds = (ids) => defaultFind(ids);
@ -35,9 +53,65 @@ const update = (criteria, values) => Comment.update(criteria).set(values).fetch(
const updateOne = (criteria, values) => Comment.updateOne(criteria).set({ ...values });
// eslint-disable-next-line no-underscore-dangle
const delete_ = (criteria) => Comment.destroy(criteria).fetch();
const delete_ = (criteria) =>
sails.getDatastore().transaction(async (db) => {
const comments = await Comment.destroy(criteria).fetch().usingConnection(db);
const deleteOne = (criteria) => Comment.destroyOne(criteria);
if (comments.length > 0) {
const commentsByCardId = _.groupBy(comments, 'cardId');
const cardIdsByTotal = Object.entries(commentsByCardId).reduce(
(result, [cardId, commentsItem]) => ({
...result,
[commentsItem.length]: [...(result[commentsItem.length] || []), cardId],
}),
{},
);
const queryValues = [];
let query = 'UPDATE card SET comments_total = comments_total - CASE ';
Object.entries(cardIdsByTotal).forEach(([total, cardIds]) => {
const inValues = cardIds.map((cardId) => {
queryValues.push(cardId);
return `$${queryValues.length}`;
});
queryValues.push(total);
query += `WHEN id IN (${inValues.join(', ')}) THEN $${queryValues.length}::int `;
});
const inValues = Object.keys(commentsByCardId).map((cardId) => {
queryValues.push(cardId);
return `$${queryValues.length}`;
});
queryValues.push(new Date().toISOString());
query += `END, updated_at = $${queryValues.length} WHERE id IN (${inValues.join(', ')})`;
await sails.sendNativeQuery(query, queryValues).usingConnection(db);
}
return comments;
});
const deleteOne = (criteria) =>
sails.getDatastore().transaction(async (db) => {
const comment = await Comment.destroyOne(criteria).usingConnection(db);
const queryResult = await sails
.sendNativeQuery(
'UPDATE card SET comments_total = comments_total - 1, updated_at = $1 WHERE id = $2',
[new Date().toISOString(), comment.cardId],
)
.usingConnection(db);
if (queryResult.rowCount === 0) {
throw 'cardNotFound';
}
return comment;
});
module.exports = {
createOne,