Fixed bundling into an exe not working as intended.
This commit is contained in:
144
electron/api/user/user.service.js
Normal file
144
electron/api/user/user.service.js
Normal file
@@ -0,0 +1,144 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.userExists = userExists;
|
||||
exports.createUser = createUser;
|
||||
exports.updateUser = updateUser;
|
||||
exports.deleteUser = deleteUser;
|
||||
exports.getAllUsers = getAllUsers;
|
||||
exports.getUserById = getUserById;
|
||||
exports.verifyPassword = verifyPassword;
|
||||
exports.getAniListIntegration = getAniListIntegration;
|
||||
exports.removeAniListIntegration = removeAniListIntegration;
|
||||
const database_1 = require("../../shared/database");
|
||||
const bcrypt_1 = __importDefault(require("bcrypt"));
|
||||
const USER_DB_NAME = 'userdata';
|
||||
const SALT_ROUNDS = 10;
|
||||
async function userExists(id) {
|
||||
const sql = 'SELECT 1 FROM User WHERE id = ?';
|
||||
const row = await (0, database_1.queryOne)(sql, [id], USER_DB_NAME);
|
||||
return !!row;
|
||||
}
|
||||
async function createUser(username, profilePictureUrl, password) {
|
||||
let passwordHash = null;
|
||||
if (password && password.trim()) {
|
||||
passwordHash = await bcrypt_1.default.hash(password.trim(), SALT_ROUNDS);
|
||||
}
|
||||
const sql = `
|
||||
INSERT INTO User (username, profile_picture_url, password_hash)
|
||||
VALUES (?, ?, ?)
|
||||
`;
|
||||
const params = [username, profilePictureUrl || null, passwordHash];
|
||||
const result = await (0, database_1.run)(sql, params, USER_DB_NAME);
|
||||
return { lastID: result.lastID };
|
||||
}
|
||||
async function updateUser(userId, updates) {
|
||||
const fields = [];
|
||||
const values = [];
|
||||
if (updates.username !== undefined) {
|
||||
fields.push('username = ?');
|
||||
values.push(updates.username);
|
||||
}
|
||||
if (updates.profilePictureUrl !== undefined) {
|
||||
fields.push('profile_picture_url = ?');
|
||||
values.push(updates.profilePictureUrl);
|
||||
}
|
||||
if (updates.password !== undefined) {
|
||||
if (updates.password === null || updates.password === '') {
|
||||
// Eliminar contraseña
|
||||
fields.push('password_hash = ?');
|
||||
values.push(null);
|
||||
}
|
||||
else {
|
||||
// Actualizar contraseña
|
||||
const hash = await bcrypt_1.default.hash(updates.password.trim(), SALT_ROUNDS);
|
||||
fields.push('password_hash = ?');
|
||||
values.push(hash);
|
||||
}
|
||||
}
|
||||
if (fields.length === 0) {
|
||||
return { changes: 0, lastID: userId };
|
||||
}
|
||||
const setClause = fields.join(', ');
|
||||
const sql = `UPDATE User SET ${setClause} WHERE id = ?`;
|
||||
values.push(userId);
|
||||
return await (0, database_1.run)(sql, values, USER_DB_NAME);
|
||||
}
|
||||
async function deleteUser(userId) {
|
||||
await (0, database_1.run)(`DELETE FROM ListEntry WHERE user_id = ?`, [userId], USER_DB_NAME);
|
||||
await (0, database_1.run)(`DELETE FROM UserIntegration WHERE user_id = ?`, [userId], USER_DB_NAME);
|
||||
await (0, database_1.run)(`DELETE FROM favorites WHERE user_id = ?`, [userId], 'favorites');
|
||||
const result = await (0, database_1.run)(`DELETE FROM User WHERE id = ?`, [userId], USER_DB_NAME);
|
||||
return result;
|
||||
}
|
||||
async function getAllUsers() {
|
||||
const sql = `
|
||||
SELECT
|
||||
id,
|
||||
username,
|
||||
profile_picture_url,
|
||||
CASE WHEN password_hash IS NOT NULL THEN 1 ELSE 0 END as has_password
|
||||
FROM User
|
||||
ORDER BY id
|
||||
`;
|
||||
const users = await (0, database_1.queryAll)(sql, [], USER_DB_NAME);
|
||||
return users.map((user) => ({
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
profile_picture_url: user.profile_picture_url || null,
|
||||
has_password: !!user.has_password
|
||||
}));
|
||||
}
|
||||
async function getUserById(id) {
|
||||
const sql = `
|
||||
SELECT
|
||||
id,
|
||||
username,
|
||||
profile_picture_url,
|
||||
CASE WHEN password_hash IS NOT NULL THEN 1 ELSE 0 END as has_password
|
||||
FROM User
|
||||
WHERE id = ?
|
||||
`;
|
||||
const user = await (0, database_1.queryOne)(sql, [id], USER_DB_NAME);
|
||||
if (!user)
|
||||
return null;
|
||||
return {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
profile_picture_url: user.profile_picture_url || null,
|
||||
has_password: !!user.has_password
|
||||
};
|
||||
}
|
||||
async function verifyPassword(userId, password) {
|
||||
const sql = 'SELECT password_hash FROM User WHERE id = ?';
|
||||
const user = await (0, database_1.queryOne)(sql, [userId], USER_DB_NAME);
|
||||
if (!user || !user.password_hash) {
|
||||
return false;
|
||||
}
|
||||
return await bcrypt_1.default.compare(password, user.password_hash);
|
||||
}
|
||||
async function getAniListIntegration(userId) {
|
||||
const sql = `
|
||||
SELECT anilist_user_id, expires_at
|
||||
FROM UserIntegration
|
||||
WHERE user_id = ? AND platform = ?
|
||||
`;
|
||||
const row = await (0, database_1.queryOne)(sql, [userId, "AniList"], USER_DB_NAME);
|
||||
if (!row) {
|
||||
return { connected: false };
|
||||
}
|
||||
return {
|
||||
connected: true,
|
||||
anilistUserId: row.anilist_user_id,
|
||||
expiresAt: row.expires_at
|
||||
};
|
||||
}
|
||||
async function removeAniListIntegration(userId) {
|
||||
const sql = `
|
||||
DELETE FROM UserIntegration
|
||||
WHERE user_id = ? AND platform = ?
|
||||
`;
|
||||
return (0, database_1.run)(sql, [userId, "AniList"], USER_DB_NAME);
|
||||
}
|
||||
Reference in New Issue
Block a user