248 lines
9.2 KiB
JavaScript
248 lines
9.2 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getMe = getMe;
|
|
exports.login = login;
|
|
exports.getAllUsers = getAllUsers;
|
|
exports.createUser = createUser;
|
|
exports.getUser = getUser;
|
|
exports.updateUser = updateUser;
|
|
exports.deleteUser = deleteUser;
|
|
exports.getIntegrationStatus = getIntegrationStatus;
|
|
exports.disconnectAniList = disconnectAniList;
|
|
exports.changePassword = changePassword;
|
|
const userService = __importStar(require("./user.service"));
|
|
const database_1 = require("../../shared/database");
|
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
async function getMe(req, reply) {
|
|
const userId = req.user?.id;
|
|
if (!userId) {
|
|
return reply.code(401).send({ error: "Unauthorized" });
|
|
}
|
|
const user = await (0, database_1.queryOne)(`SELECT username, profile_picture_url FROM User WHERE id = ?`, [userId], 'userdata');
|
|
if (!user) {
|
|
return reply.code(404).send({ error: "User not found" });
|
|
}
|
|
return reply.send({
|
|
username: user.username,
|
|
avatar: user.profile_picture_url
|
|
});
|
|
}
|
|
async function login(req, reply) {
|
|
const { userId, password } = req.body;
|
|
if (!userId || typeof userId !== "number" || userId <= 0) {
|
|
return reply.code(400).send({ error: "Invalid userId provided" });
|
|
}
|
|
const user = await userService.getUserById(userId);
|
|
if (!user) {
|
|
return reply.code(404).send({ error: "User not found in local database" });
|
|
}
|
|
// Si el usuario tiene contraseña, debe proporcionarla
|
|
if (user.has_password) {
|
|
if (!password) {
|
|
return reply.code(401).send({
|
|
error: "Password required",
|
|
requiresPassword: true
|
|
});
|
|
}
|
|
const isValid = await userService.verifyPassword(userId, password);
|
|
if (!isValid) {
|
|
return reply.code(401).send({ error: "Incorrect password" });
|
|
}
|
|
}
|
|
const token = jsonwebtoken_1.default.sign({ id: userId }, process.env.JWT_SECRET, { expiresIn: "7d" });
|
|
return reply.code(200).send({
|
|
success: true,
|
|
token
|
|
});
|
|
}
|
|
async function getAllUsers(req, reply) {
|
|
try {
|
|
const users = await userService.getAllUsers();
|
|
return { users };
|
|
}
|
|
catch (err) {
|
|
console.error("Get All Users Error:", err.message);
|
|
return reply.code(500).send({ error: "Failed to retrieve user list" });
|
|
}
|
|
}
|
|
async function createUser(req, reply) {
|
|
try {
|
|
const { username, profilePictureUrl, password } = req.body;
|
|
if (!username) {
|
|
return reply.code(400).send({ error: "Missing required field: username" });
|
|
}
|
|
const result = await userService.createUser(username, profilePictureUrl, password);
|
|
return reply.code(201).send({
|
|
success: true,
|
|
userId: result.lastID,
|
|
username
|
|
});
|
|
}
|
|
catch (err) {
|
|
if (err.message.includes('SQLITE_CONSTRAINT')) {
|
|
return reply.code(409).send({ error: "Username already exists." });
|
|
}
|
|
console.error("Create User Error:", err.message);
|
|
return reply.code(500).send({ error: "Failed to create user" });
|
|
}
|
|
}
|
|
async function getUser(req, reply) {
|
|
try {
|
|
const { id } = req.params;
|
|
const userId = parseInt(id, 10);
|
|
const user = await userService.getUserById(userId);
|
|
if (!user) {
|
|
return reply.code(404).send({ error: "User not found" });
|
|
}
|
|
return { user };
|
|
}
|
|
catch (err) {
|
|
console.error("Get User Error:", err.message);
|
|
return reply.code(500).send({ error: "Failed to retrieve user" });
|
|
}
|
|
}
|
|
async function updateUser(req, reply) {
|
|
try {
|
|
const { id } = req.params;
|
|
const userId = parseInt(id, 10);
|
|
const updates = req.body;
|
|
if (Object.keys(updates).length === 0) {
|
|
return reply.code(400).send({ error: "No update fields provided" });
|
|
}
|
|
const result = await userService.updateUser(userId, updates);
|
|
if (result && result.changes > 0) {
|
|
return { success: true, message: "User updated successfully" };
|
|
}
|
|
else {
|
|
return reply.code(404).send({ error: "User not found or nothing to update" });
|
|
}
|
|
}
|
|
catch (err) {
|
|
if (err.message.includes('SQLITE_CONSTRAINT')) {
|
|
return reply.code(409).send({ error: "Username already exists or is invalid." });
|
|
}
|
|
console.error("Update User Error:", err.message);
|
|
return reply.code(500).send({ error: "Failed to update user" });
|
|
}
|
|
}
|
|
async function deleteUser(req, reply) {
|
|
try {
|
|
const { id } = req.params;
|
|
const userId = parseInt(id, 10);
|
|
if (!userId || isNaN(userId)) {
|
|
return reply.code(400).send({ error: "Invalid user id" });
|
|
}
|
|
const result = await userService.deleteUser(userId);
|
|
if (result && result.changes > 0) {
|
|
return { success: true, message: "User deleted successfully" };
|
|
}
|
|
else {
|
|
return reply.code(404).send({ error: "User not found" });
|
|
}
|
|
}
|
|
catch (err) {
|
|
console.error("Delete User Error:", err.message);
|
|
return reply.code(500).send({ error: "Failed to delete user" });
|
|
}
|
|
}
|
|
async function getIntegrationStatus(req, reply) {
|
|
try {
|
|
const { id } = req.params;
|
|
const userId = parseInt(id, 10);
|
|
if (!userId || isNaN(userId)) {
|
|
return reply.code(400).send({ error: "Invalid user id" });
|
|
}
|
|
const integration = await userService.getAniListIntegration(userId);
|
|
return reply.code(200).send(integration);
|
|
}
|
|
catch (err) {
|
|
console.error("Get Integration Status Error:", err.message);
|
|
return reply.code(500).send({ error: "Failed to check integration status" });
|
|
}
|
|
}
|
|
async function disconnectAniList(req, reply) {
|
|
try {
|
|
const { id } = req.params;
|
|
const userId = parseInt(id, 10);
|
|
if (!userId || isNaN(userId)) {
|
|
return reply.code(400).send({ error: "Invalid user id" });
|
|
}
|
|
const result = await userService.removeAniListIntegration(userId);
|
|
if (result.changes === 0) {
|
|
return reply.code(404).send({ error: "AniList integration not found" });
|
|
}
|
|
return reply.send({ success: true });
|
|
}
|
|
catch (err) {
|
|
console.error("Disconnect AniList Error:", err);
|
|
return reply.code(500).send({ error: "Failed to disconnect AniList" });
|
|
}
|
|
}
|
|
async function changePassword(req, reply) {
|
|
try {
|
|
const { id } = req.params;
|
|
const { currentPassword, newPassword } = req.body;
|
|
const userId = parseInt(id, 10);
|
|
if (!userId || isNaN(userId)) {
|
|
return reply.code(400).send({ error: "Invalid user id" });
|
|
}
|
|
const user = await userService.getUserById(userId);
|
|
if (!user) {
|
|
return reply.code(404).send({ error: "User not found" });
|
|
}
|
|
// Si el usuario tiene contraseña actual, debe proporcionar la contraseña actual
|
|
if (user.has_password && currentPassword) {
|
|
const isValid = await userService.verifyPassword(userId, currentPassword);
|
|
if (!isValid) {
|
|
return reply.code(401).send({ error: "Current password is incorrect" });
|
|
}
|
|
}
|
|
// Actualizar la contraseña (null para eliminarla, string para establecerla)
|
|
await userService.updateUser(userId, { password: newPassword });
|
|
return reply.send({
|
|
success: true,
|
|
message: newPassword ? "Password updated successfully" : "Password removed successfully"
|
|
});
|
|
}
|
|
catch (err) {
|
|
console.error("Change Password Error:", err);
|
|
return reply.code(500).send({ error: "Failed to change password" });
|
|
}
|
|
}
|