Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
lelemm and coderabbitai[bot] authored Nov 11, 2024
1 parent f9c4175 commit 81cda3f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
8 changes: 7 additions & 1 deletion src/load-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ const finalConfig = {
? process.env.ACTUAL_LOGIN_METHOD.toLowerCase()
: config.loginMethod,
multiuser: process.env.ACTUAL_MULTIUSER
? process.env.ACTUAL_MULTIUSER.toLowerCase() === 'true'
? (() => {
const value = process.env.ACTUAL_MULTIUSER.toLowerCase();
if (!['true', 'false'].includes(value)) {
throw new Error('ACTUAL_MULTIUSER must be either "true" or "false"');
}
return value === 'true';
})()
: config.multiuser,
trustedProxies: process.env.ACTUAL_TRUSTED_PROXIES
? process.env.ACTUAL_TRUSTED_PROXIES.split(',').map((q) => q.trim())
Expand Down
37 changes: 26 additions & 11 deletions src/services/user-service.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import getAccountDb from '../account-db.js';

export function getUserByUsername(userName) {
if (!userName || typeof userName !== 'string') {
return null;
}
const { id } =
getAccountDb().first('SELECT id FROM users WHERE user_name = ?', [
userName,
]) || {};
return id;
return id || null;
}

export function getUserById(userId) {
if (!userId) {
return null;
}
const { id } =
getAccountDb().first('SELECT id FROM users WHERE id = ?', [userId]) || {};
return id;
getAccountDb().first('SELECT * FROM users WHERE id = ?', [userId]) || {};
return id || null;
}

export function getFileById(fileId) {
if (!fileId) {
return null;
}
const { id } =
getAccountDb().first('SELECT id FROM files WHERE files.id = ?', [fileId]) ||
getAccountDb().first('SELECT * FROM files WHERE files.id = ?', [fileId]) ||
{};
return id;
return id || null;
}

export function validateRole(roleId) {
Expand Down Expand Up @@ -173,14 +182,20 @@ export function deleteUserAccessByFileId(userIds, fileId) {
const CHUNK_SIZE = 999;
let totalChanges = 0;

for (let i = 0; i < userIds.length; i += CHUNK_SIZE) {
const chunk = userIds.slice(i, i + CHUNK_SIZE);
const placeholders = chunk.map(() => '?').join(',');
try {
getAccountDb().transaction(() => {
for (let i = 0; i < userIds.length; i += CHUNK_SIZE) {
const chunk = userIds.slice(i, i + CHUNK_SIZE);
const placeholders = chunk.map(() => '?').join(',');

const sql = `DELETE FROM user_access WHERE user_id IN (${placeholders}) AND file_id = ?`;
const sql = `DELETE FROM user_access WHERE user_id IN (${placeholders}) AND file_id = ?`;

const result = getAccountDb().mutate(sql, [...chunk, fileId]);
totalChanges += result.changes;
const result = getAccountDb().mutate(sql, [...chunk, fileId]);
totalChanges += result.changes;
}
});
} catch (error) {
throw new Error(`Failed to delete user access: ${error.message}`);
}

return totalChanges;
Expand Down

0 comments on commit 81cda3f

Please sign in to comment.