mirror of
https://github.com/plankanban/planka.git
synced 2025-12-19 17:23:27 +03:00
ref: Little refactoring
This commit is contained in:
@@ -1,31 +0,0 @@
|
|||||||
/*!
|
|
||||||
* Copyright (c) 2024 PLANKA Software GmbH
|
|
||||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
|
||||||
*/
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
sync: true,
|
|
||||||
|
|
||||||
inputs: {
|
|
||||||
recordOrRecords: {
|
|
||||||
type: 'ref',
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
fn(inputs) {
|
|
||||||
const users = _.isPlainObject(inputs.recordOrRecords)
|
|
||||||
? [inputs.recordOrRecords]
|
|
||||||
: inputs.recordOrRecords;
|
|
||||||
|
|
||||||
const fileManager = sails.hooks['file-manager'].getInstance();
|
|
||||||
|
|
||||||
users.forEach(async (user) => {
|
|
||||||
if (user.avatar) {
|
|
||||||
await fileManager.deleteDir(
|
|
||||||
`${sails.config.custom.userAvatarsPathSegment}/${user.avatar.dirname}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@@ -21,8 +21,8 @@ const makeWhereQueryBuilder = (Model) => (criteria) => {
|
|||||||
throw new Error('Unknown column');
|
throw new Error('Unknown column');
|
||||||
}
|
}
|
||||||
|
|
||||||
parts.push(`${columnName} = $${index + 1}`);
|
|
||||||
values.push(value);
|
values.push(value);
|
||||||
|
parts.push(`${columnName} = $${values.length}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return [parts.join(' AND '), values];
|
return [parts.join(' AND '), values];
|
||||||
|
|||||||
@@ -206,12 +206,12 @@ const update = async (criteria, values) => {
|
|||||||
const cards = await Card.update(criteria).set(values).fetch().usingConnection(db);
|
const cards = await Card.update(criteria).set(values).fetch().usingConnection(db);
|
||||||
|
|
||||||
let tasks = [];
|
let tasks = [];
|
||||||
if (card) {
|
if (!_.isUndefined(values.isClosed)) {
|
||||||
tasks = await Task.update({
|
tasks = await Task.update({
|
||||||
linkedCardId: sails.helpers.utils.mapRecords(cards),
|
linkedCardId: sails.helpers.utils.mapRecords(cards),
|
||||||
})
|
})
|
||||||
.set({
|
.set({
|
||||||
isCompleted: card.isClosed,
|
isCompleted: values.isClosed,
|
||||||
})
|
})
|
||||||
.fetch()
|
.fetch()
|
||||||
.usingConnection(db);
|
.usingConnection(db);
|
||||||
@@ -232,8 +232,8 @@ const updateOne = async (criteria, values) => {
|
|||||||
.set({ ...values })
|
.set({ ...values })
|
||||||
.usingConnection(db);
|
.usingConnection(db);
|
||||||
|
|
||||||
let tasks = [];
|
let tasks;
|
||||||
if (card) {
|
if (!_.isUndefined(values.isClosed) && card) {
|
||||||
tasks = await Task.update({
|
tasks = await Task.update({
|
||||||
linkedCardId: card.id,
|
linkedCardId: card.id,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -52,19 +52,24 @@ const getOneTrashByBoardId = (boardId) =>
|
|||||||
});
|
});
|
||||||
|
|
||||||
const updateOne = async (criteria, values) => {
|
const updateOne = async (criteria, values) => {
|
||||||
if (!_.isUndefined(values.type)) {
|
if (values.type) {
|
||||||
return sails.getDatastore().transaction(async (db) => {
|
return sails.getDatastore().transaction(async (db) => {
|
||||||
const [whereQuery, whereQueryValues] = buildWhereQuery(criteria);
|
const [whereQuery, whereQueryValues] = buildWhereQuery(criteria);
|
||||||
|
|
||||||
const queryResult = await sails
|
const queryResult = await sails
|
||||||
.sendNativeQuery(`SELECT type FROM list WHERE ${whereQuery} FOR UPDATE`, whereQueryValues)
|
.sendNativeQuery(
|
||||||
|
`SELECT type FROM list WHERE ${whereQuery} LIMIT 1 FOR UPDATE`,
|
||||||
|
whereQueryValues,
|
||||||
|
)
|
||||||
.usingConnection(db);
|
.usingConnection(db);
|
||||||
|
|
||||||
if (queryResult.rowCount === 0) {
|
if (queryResult.rowCount === 0) {
|
||||||
return { list: null };
|
return { list: null };
|
||||||
}
|
}
|
||||||
|
|
||||||
const [{ type: prevType }] = queryResult.rows;
|
const prev = {
|
||||||
|
type: queryResult.rows[0].type,
|
||||||
|
};
|
||||||
|
|
||||||
const list = await List.updateOne(criteria)
|
const list = await List.updateOne(criteria)
|
||||||
.set({ ...values })
|
.set({ ...values })
|
||||||
@@ -74,18 +79,15 @@ const updateOne = async (criteria, values) => {
|
|||||||
let tasks = [];
|
let tasks = [];
|
||||||
|
|
||||||
if (list) {
|
if (list) {
|
||||||
const prevTypeState = List.TYPE_STATE_BY_TYPE[prevType];
|
const prevTypeState = List.TYPE_STATE_BY_TYPE[prev.type];
|
||||||
const typeState = List.TYPE_STATE_BY_TYPE[list.type];
|
const typeState = List.TYPE_STATE_BY_TYPE[list.type];
|
||||||
|
|
||||||
let isClosed;
|
const transitions = {
|
||||||
if (prevTypeState === List.TypeStates.CLOSED && typeState === List.TypeStates.OPENED) {
|
[`${List.TypeStates.CLOSED}->${List.TypeStates.OPENED}`]: false,
|
||||||
isClosed = false;
|
[`${List.TypeStates.OPENED}->${List.TypeStates.CLOSED}`]: true,
|
||||||
} else if (
|
};
|
||||||
prevTypeState === List.TypeStates.OPENED &&
|
|
||||||
typeState === List.TypeStates.CLOSED
|
const isClosed = transitions[`${prevTypeState}->${typeState}`];
|
||||||
) {
|
|
||||||
isClosed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!_.isUndefined(isClosed)) {
|
if (!_.isUndefined(isClosed)) {
|
||||||
cards = await Card.update({
|
cards = await Card.update({
|
||||||
|
|||||||
@@ -96,13 +96,13 @@ const updateOne = async (criteria, values) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let prevAvatar;
|
let prev;
|
||||||
if (!_.isUndefined(values.avatar)) {
|
if (!_.isUndefined(values.avatar)) {
|
||||||
const [whereQuery, whereQueryValues] = buildWhereQuery(criteria);
|
const [whereQuery, whereQueryValues] = buildWhereQuery(criteria);
|
||||||
|
|
||||||
const queryResult = await sails
|
const queryResult = await sails
|
||||||
.sendNativeQuery(
|
.sendNativeQuery(
|
||||||
`SELECT avatar FROM user_account WHERE ${whereQuery} FOR UPDATE`,
|
`SELECT avatar FROM user_account WHERE ${whereQuery} LIMIT 1 FOR UPDATE`,
|
||||||
whereQueryValues,
|
whereQueryValues,
|
||||||
)
|
)
|
||||||
.usingConnection(db);
|
.usingConnection(db);
|
||||||
@@ -111,7 +111,9 @@ const updateOne = async (criteria, values) => {
|
|||||||
return { user: null };
|
return { user: null };
|
||||||
}
|
}
|
||||||
|
|
||||||
[{ avatar: prevAvatar }] = queryResult.rows;
|
prev = {
|
||||||
|
avatar: queryResult.rows[0].avatar,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await User.updateOne(criteria)
|
const user = await User.updateOne(criteria)
|
||||||
@@ -119,12 +121,12 @@ const updateOne = async (criteria, values) => {
|
|||||||
.usingConnection(db);
|
.usingConnection(db);
|
||||||
|
|
||||||
let uploadedFile;
|
let uploadedFile;
|
||||||
if (hasAvatarChanged(user.avatar, prevAvatar)) {
|
if (!_.isUndefined(values.avatar) && hasAvatarChanged(user.avatar, prev.avatar)) {
|
||||||
if (prevAvatar) {
|
if (prev.avatar) {
|
||||||
const queryResult = await sails
|
const queryResult = await sails
|
||||||
.sendNativeQuery(
|
.sendNativeQuery(
|
||||||
'UPDATE uploaded_file SET references_total = CASE WHEN references_total > 1 THEN references_total - 1 END, updated_at = $1 WHERE id = $2 RETURNING *',
|
'UPDATE uploaded_file SET references_total = CASE WHEN references_total > 1 THEN references_total - 1 END, updated_at = $1 WHERE id = $2 RETURNING *',
|
||||||
[new Date().toISOString(), prevAvatar.uploadedFileId],
|
[new Date().toISOString(), prev.avatar.uploadedFileId],
|
||||||
)
|
)
|
||||||
.usingConnection(db);
|
.usingConnection(db);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user