- Updated LoanTable component to fetch loan data from new API endpoint and display notes. - Enhanced UserTable component to include additional user fields (first name, last name, email, admin status) and updated input handling. - Modified fetcher utility to use new user data API endpoint. - Adjusted login functionality to point to the new admin login endpoint and handle unauthorized access. - Refactored user actions utility to align with updated API endpoints for user management. - Updated backend routes for user and loan data management to reflect new structure and naming conventions. - Revised SQL schema and mock data to accommodate new fields and constraints. - Changed Docker configuration to use the new database name.
80 lines
2.2 KiB
JavaScript
80 lines
2.2 KiB
JavaScript
import mysql from "mysql2";
|
|
import dotenv from "dotenv";
|
|
dotenv.config();
|
|
|
|
const pool = mysql
|
|
.createPool({
|
|
host: process.env.DB_HOST,
|
|
user: process.env.DB_USER,
|
|
password: process.env.DB_PASSWORD,
|
|
database: process.env.DB_NAME,
|
|
})
|
|
.promise();
|
|
|
|
export const createUser = async (
|
|
username,
|
|
role,
|
|
password,
|
|
isAdmin,
|
|
email,
|
|
first_name,
|
|
last_name
|
|
) => {
|
|
const [result] = await pool.query(
|
|
"INSERT INTO users (username, role, password, is_admin, email, first_name, last_name) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
[username, role, password, isAdmin, email, first_name, last_name]
|
|
);
|
|
if (result.affectedRows > 0) return { success: true };
|
|
return { success: false };
|
|
};
|
|
|
|
export const deleteUserById = async (userId) => {
|
|
const [result] = await pool.query("DELETE FROM users WHERE id = ?", [userId]);
|
|
if (result.affectedRows > 0) return { success: true };
|
|
return { success: false };
|
|
};
|
|
|
|
export const changePassword = async (userId, newPassword) => {
|
|
const [result] = await pool.query(
|
|
"UPDATE users SET password = ? WHERE id = ?",
|
|
[newPassword, userId]
|
|
);
|
|
if (result.affectedRows > 0) return { success: true };
|
|
return { success: false };
|
|
};
|
|
|
|
export const editUserById = async (
|
|
userId,
|
|
first_name,
|
|
last_name,
|
|
role,
|
|
email,
|
|
is_admin
|
|
) => {
|
|
const [result] = await pool.query(
|
|
"UPDATE users SET first_name = ?, last_name = ?, role = ?, email = ?, is_admin = ? WHERE id = ?",
|
|
[first_name, last_name, role, email, is_admin, userId]
|
|
);
|
|
if (result.affectedRows > 0) return { success: true };
|
|
return { success: false };
|
|
};
|
|
|
|
export const getAllUsers = async () => {
|
|
const [result] = await pool.query(
|
|
"SELECT id, username, first_name, last_name, role, email, is_admin, entry_created_at, entry_updated_at FROM users"
|
|
);
|
|
if (result.length > 0) return { success: true, data: result };
|
|
return { success: false };
|
|
};
|
|
|
|
export const getUserById = async (userId) => {
|
|
const [rows] = await pool.query(
|
|
"SELECT id, username, first_name, last_name, role, email, is_admin FROM users WHERE id = ?",
|
|
[userId]
|
|
);
|
|
if (rows.length === 0) {
|
|
return { success: false };
|
|
}
|
|
return { success: true, data: rows[0] };
|
|
};
|