mirror of
https://github.com/9technologygroup/patchmon.net.git
synced 2025-11-04 14:03:17 +00:00
Refactor authentication and routing code to use consistent naming conventions for database fields.
Do NOT update the schema like that again for the love of god.
This commit is contained in:
@@ -28,13 +28,13 @@ router.get('/admin/users', authenticateToken, requireViewUsers, async (req, res)
|
||||
username: true,
|
||||
email: true,
|
||||
role: true,
|
||||
isActive: true,
|
||||
lastLogin: true,
|
||||
createdAt: true,
|
||||
updatedAt: true
|
||||
is_active: true,
|
||||
last_login: true,
|
||||
created_at: true,
|
||||
updated_at: true
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc'
|
||||
created_at: 'desc'
|
||||
}
|
||||
})
|
||||
|
||||
@@ -91,7 +91,7 @@ router.post('/admin/users', authenticateToken, requireManageUsers, [
|
||||
data: {
|
||||
username,
|
||||
email,
|
||||
passwordHash,
|
||||
password_hash: passwordHash,
|
||||
role
|
||||
},
|
||||
select: {
|
||||
@@ -99,8 +99,8 @@ router.post('/admin/users', authenticateToken, requireManageUsers, [
|
||||
username: true,
|
||||
email: true,
|
||||
role: true,
|
||||
isActive: true,
|
||||
createdAt: true
|
||||
is_active: true,
|
||||
created_at: true
|
||||
}
|
||||
});
|
||||
|
||||
@@ -144,7 +144,7 @@ router.put('/admin/users/:userId', authenticateToken, requireManageUsers, [
|
||||
if (username) updateData.username = username;
|
||||
if (email) updateData.email = email;
|
||||
if (role) updateData.role = role;
|
||||
if (typeof isActive === 'boolean') updateData.isActive = isActive;
|
||||
if (typeof isActive === 'boolean') updateData.is_active = isActive;
|
||||
|
||||
// Check if user exists
|
||||
const existingUser = await prisma.users.findUnique({
|
||||
@@ -181,7 +181,7 @@ router.put('/admin/users/:userId', authenticateToken, requireManageUsers, [
|
||||
const adminCount = await prisma.users.count({
|
||||
where: {
|
||||
role: 'admin',
|
||||
isActive: true
|
||||
is_active: true
|
||||
}
|
||||
});
|
||||
|
||||
@@ -199,10 +199,10 @@ router.put('/admin/users/:userId', authenticateToken, requireManageUsers, [
|
||||
username: true,
|
||||
email: true,
|
||||
role: true,
|
||||
isActive: true,
|
||||
lastLogin: true,
|
||||
createdAt: true,
|
||||
updatedAt: true
|
||||
is_active: true,
|
||||
last_login: true,
|
||||
created_at: true,
|
||||
updated_at: true
|
||||
}
|
||||
});
|
||||
|
||||
@@ -240,7 +240,7 @@ router.delete('/admin/users/:userId', authenticateToken, requireManageUsers, asy
|
||||
const adminCount = await prisma.users.count({
|
||||
where: {
|
||||
role: 'admin',
|
||||
isActive: true
|
||||
is_active: true
|
||||
}
|
||||
});
|
||||
|
||||
@@ -304,7 +304,7 @@ router.post('/admin/users/:userId/reset-password', authenticateToken, requireMan
|
||||
// Update user password
|
||||
await prisma.users.update({
|
||||
where: { id: userId },
|
||||
data: { passwordHash }
|
||||
data: { password_hash: passwordHash }
|
||||
});
|
||||
|
||||
// Log the password reset action (you might want to add an audit log table)
|
||||
@@ -519,7 +519,7 @@ router.post('/verify-tfa', [
|
||||
const speakeasy = require('speakeasy');
|
||||
|
||||
// Check if it's a backup code
|
||||
const backupCodes = user.tfaBackupCodes ? JSON.parse(user.tfaBackupCodes) : [];
|
||||
const backupCodes = user.tfa_backup_codes ? JSON.parse(user.tfa_backup_codes) : [];
|
||||
const isBackupCode = backupCodes.includes(token);
|
||||
|
||||
let verified = false;
|
||||
@@ -527,17 +527,17 @@ router.post('/verify-tfa', [
|
||||
if (isBackupCode) {
|
||||
// Remove the used backup code
|
||||
const updatedBackupCodes = backupCodes.filter(code => code !== token);
|
||||
await prisma.user.update({
|
||||
await prisma.users.update({
|
||||
where: { id: user.id },
|
||||
data: {
|
||||
tfaBackupCodes: JSON.stringify(updatedBackupCodes)
|
||||
tfa_backup_codes: JSON.stringify(updatedBackupCodes)
|
||||
}
|
||||
});
|
||||
verified = true;
|
||||
} else {
|
||||
// Verify TOTP token
|
||||
verified = speakeasy.totp.verify({
|
||||
secret: user.tfaSecret,
|
||||
secret: user.tfa_secret,
|
||||
encoding: 'base32',
|
||||
token: token,
|
||||
window: 2
|
||||
@@ -549,9 +549,9 @@ router.post('/verify-tfa', [
|
||||
}
|
||||
|
||||
// Update last login
|
||||
await prisma.user.update({
|
||||
await prisma.users.update({
|
||||
where: { id: user.id },
|
||||
data: { lastLogin: new Date() }
|
||||
data: { last_login: new Date() }
|
||||
});
|
||||
|
||||
// Generate token
|
||||
@@ -604,7 +604,7 @@ router.put('/profile', authenticateToken, [
|
||||
|
||||
// Check if username/email already exists (excluding current user)
|
||||
if (username || email) {
|
||||
const existingUser = await prisma.user.findFirst({
|
||||
const existingUser = await prisma.users.findFirst({
|
||||
where: {
|
||||
AND: [
|
||||
{ id: { not: req.user.id } },
|
||||
@@ -623,7 +623,7 @@ router.put('/profile', authenticateToken, [
|
||||
}
|
||||
}
|
||||
|
||||
const updatedUser = await prisma.user.update({
|
||||
const updatedUser = await prisma.users.update({
|
||||
where: { id: req.user.id },
|
||||
data: updateData,
|
||||
select: {
|
||||
@@ -631,9 +631,9 @@ router.put('/profile', authenticateToken, [
|
||||
username: true,
|
||||
email: true,
|
||||
role: true,
|
||||
isActive: true,
|
||||
lastLogin: true,
|
||||
updatedAt: true
|
||||
is_active: true,
|
||||
last_login: true,
|
||||
updated_at: true
|
||||
}
|
||||
});
|
||||
|
||||
@@ -661,12 +661,12 @@ router.put('/change-password', authenticateToken, [
|
||||
const { currentPassword, newPassword } = req.body;
|
||||
|
||||
// Get user with password hash
|
||||
const user = await prisma.user.findUnique({
|
||||
const user = await prisma.users.findUnique({
|
||||
where: { id: req.user.id }
|
||||
});
|
||||
|
||||
// Verify current password
|
||||
const isValidPassword = await bcrypt.compare(currentPassword, user.passwordHash);
|
||||
const isValidPassword = await bcrypt.compare(currentPassword, user.password_hash);
|
||||
if (!isValidPassword) {
|
||||
return res.status(401).json({ error: 'Current password is incorrect' });
|
||||
}
|
||||
@@ -675,9 +675,9 @@ router.put('/change-password', authenticateToken, [
|
||||
const newPasswordHash = await bcrypt.hash(newPassword, 12);
|
||||
|
||||
// Update password
|
||||
await prisma.user.update({
|
||||
await prisma.users.update({
|
||||
where: { id: req.user.id },
|
||||
data: { passwordHash: newPasswordHash }
|
||||
data: { password_hash: newPasswordHash }
|
||||
});
|
||||
|
||||
res.json({
|
||||
|
||||
@@ -162,16 +162,16 @@ router.get('/hosts', authenticateToken, requireViewHosts, async (req, res) => {
|
||||
// Show all hosts regardless of status
|
||||
select: {
|
||||
id: true,
|
||||
friendlyName: true,
|
||||
friendly_name: true,
|
||||
hostname: true,
|
||||
ip: true,
|
||||
osType: true,
|
||||
osVersion: true,
|
||||
lastUpdate: true,
|
||||
os_type: true,
|
||||
os_version: true,
|
||||
last_update: true,
|
||||
status: true,
|
||||
agentVersion: true,
|
||||
autoUpdate: true,
|
||||
hostGroup: {
|
||||
agent_version: true,
|
||||
auto_update: true,
|
||||
host_groups: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
@@ -180,15 +180,15 @@ router.get('/hosts', authenticateToken, requireViewHosts, async (req, res) => {
|
||||
},
|
||||
_count: {
|
||||
select: {
|
||||
hostPackages: {
|
||||
host_packages: {
|
||||
where: {
|
||||
needsUpdate: true
|
||||
needs_update: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
orderBy: { lastUpdate: 'desc' }
|
||||
orderBy: { last_update: 'desc' }
|
||||
});
|
||||
|
||||
// Get update counts for each host separately
|
||||
@@ -196,15 +196,15 @@ router.get('/hosts', authenticateToken, requireViewHosts, async (req, res) => {
|
||||
hosts.map(async (host) => {
|
||||
const updatesCount = await prisma.host_packages.count({
|
||||
where: {
|
||||
hostId: host.id,
|
||||
needsUpdate: true
|
||||
host_id: host.id,
|
||||
needs_update: true
|
||||
}
|
||||
});
|
||||
|
||||
// Get total packages count for this host
|
||||
const totalPackagesCount = await prisma.host_packages.count({
|
||||
where: {
|
||||
hostId: host.id
|
||||
host_id: host.id
|
||||
}
|
||||
});
|
||||
|
||||
@@ -244,9 +244,9 @@ router.get('/packages', authenticateToken, requireViewPackages, async (req, res)
|
||||
try {
|
||||
const packages = await prisma.packages.findMany({
|
||||
where: {
|
||||
hostPackages: {
|
||||
host_packages: {
|
||||
some: {
|
||||
needsUpdate: true
|
||||
needs_update: true
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -255,18 +255,18 @@ router.get('/packages', authenticateToken, requireViewPackages, async (req, res)
|
||||
name: true,
|
||||
description: true,
|
||||
category: true,
|
||||
latestVersion: true,
|
||||
hostPackages: {
|
||||
where: { needsUpdate: true },
|
||||
latest_version: true,
|
||||
host_packages: {
|
||||
where: { needs_update: true },
|
||||
select: {
|
||||
currentVersion: true,
|
||||
availableVersion: true,
|
||||
isSecurityUpdate: true,
|
||||
host: {
|
||||
current_version: true,
|
||||
available_version: true,
|
||||
is_security_update: true,
|
||||
hosts: {
|
||||
select: {
|
||||
id: true,
|
||||
friendlyName: true,
|
||||
osType: true
|
||||
friendly_name: true,
|
||||
os_type: true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -284,14 +284,14 @@ router.get('/packages', authenticateToken, requireViewPackages, async (req, res)
|
||||
category: pkg.category,
|
||||
latestVersion: pkg.latest_version,
|
||||
affectedHostsCount: pkg.host_packages.length,
|
||||
isSecurityUpdate: pkg.host_packages.some(hp => hp.isSecurityUpdate),
|
||||
isSecurityUpdate: pkg.host_packages.some(hp => hp.is_security_update),
|
||||
affectedHosts: pkg.host_packages.map(hp => ({
|
||||
hostId: hp.host.id,
|
||||
friendlyName: hp.host.friendlyName,
|
||||
osType: hp.host.osType,
|
||||
currentVersion: hp.currentVersion,
|
||||
availableVersion: hp.availableVersion,
|
||||
isSecurityUpdate: hp.isSecurityUpdate
|
||||
hostId: hp.hosts.id,
|
||||
friendlyName: hp.hosts.friendly_name,
|
||||
osType: hp.hosts.os_type,
|
||||
currentVersion: hp.current_version,
|
||||
availableVersion: hp.available_version,
|
||||
isSecurityUpdate: hp.is_security_update
|
||||
}))
|
||||
}));
|
||||
|
||||
@@ -310,22 +310,22 @@ router.get('/hosts/:hostId', authenticateToken, requireViewHosts, async (req, re
|
||||
const host = await prisma.hosts.findUnique({
|
||||
where: { id: hostId },
|
||||
include: {
|
||||
hostGroup: {
|
||||
host_groups: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
color: true
|
||||
}
|
||||
},
|
||||
hostPackages: {
|
||||
host_packages: {
|
||||
include: {
|
||||
package: true
|
||||
packages: true
|
||||
},
|
||||
orderBy: {
|
||||
needsUpdate: 'desc'
|
||||
needs_update: 'desc'
|
||||
}
|
||||
},
|
||||
updateHistory: {
|
||||
update_history: {
|
||||
orderBy: {
|
||||
timestamp: 'desc'
|
||||
},
|
||||
@@ -342,8 +342,8 @@ router.get('/hosts/:hostId', authenticateToken, requireViewHosts, async (req, re
|
||||
...host,
|
||||
stats: {
|
||||
totalPackages: host.host_packages.length,
|
||||
outdatedPackages: host.host_packages.filter(hp => hp.needsUpdate).length,
|
||||
securityUpdates: host.host_packages.filter(hp => hp.needsUpdate && hp.isSecurityUpdate).length
|
||||
outdatedPackages: host.host_packages.filter(hp => hp.needs_update).length,
|
||||
securityUpdates: host.host_packages.filter(hp => hp.needs_update && hp.is_security_update).length
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -936,14 +936,14 @@ router.patch('/agent/versions/:versionId/current', authenticateToken, requireMan
|
||||
|
||||
// First, unset all current versions
|
||||
await prisma.agent_versions.updateMany({
|
||||
where: { isCurrent: true },
|
||||
data: { isCurrent: false }
|
||||
where: { is_current: true },
|
||||
data: { is_current: false, updated_at: new Date() }
|
||||
});
|
||||
|
||||
// Set the specified version as current
|
||||
const agentVersion = await prisma.agent_versions.update({
|
||||
where: { id: versionId },
|
||||
data: { isCurrent: true }
|
||||
data: { is_current: true, updated_at: new Date() }
|
||||
});
|
||||
|
||||
res.json(agentVersion);
|
||||
@@ -960,14 +960,14 @@ router.patch('/agent/versions/:versionId/default', authenticateToken, requireMan
|
||||
|
||||
// First, unset all default versions
|
||||
await prisma.agent_versions.updateMany({
|
||||
where: { isDefault: true },
|
||||
data: { isDefault: false }
|
||||
where: { is_default: true },
|
||||
data: { is_default: false, updated_at: new Date() }
|
||||
});
|
||||
|
||||
// Set the specified version as default
|
||||
const agentVersion = await prisma.agent_versions.update({
|
||||
where: { id: versionId },
|
||||
data: { isDefault: true }
|
||||
data: { is_default: true, updated_at: new Date() }
|
||||
});
|
||||
|
||||
res.json(agentVersion);
|
||||
@@ -1030,7 +1030,7 @@ router.patch('/:hostId/friendly-name', authenticateToken, requireManageHosts, [
|
||||
// Check if friendly name is already taken by another host
|
||||
const existingHost = await prisma.hosts.findFirst({
|
||||
where: {
|
||||
friendlyName: friendlyName,
|
||||
friendly_name: friendlyName,
|
||||
id: { not: hostId }
|
||||
}
|
||||
});
|
||||
@@ -1042,23 +1042,23 @@ router.patch('/:hostId/friendly-name', authenticateToken, requireManageHosts, [
|
||||
// Update the friendly name
|
||||
const updatedHost = await prisma.hosts.update({
|
||||
where: { id: hostId },
|
||||
data: { friendlyName },
|
||||
data: { friendly_name: friendlyName },
|
||||
select: {
|
||||
id: true,
|
||||
friendlyName: true,
|
||||
friendly_name: true,
|
||||
hostname: true,
|
||||
ip: true,
|
||||
osType: true,
|
||||
osVersion: true,
|
||||
os_type: true,
|
||||
os_version: true,
|
||||
architecture: true,
|
||||
lastUpdate: true,
|
||||
last_update: true,
|
||||
status: true,
|
||||
hostGroupId: true,
|
||||
agentVersion: true,
|
||||
autoUpdate: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
hostGroup: {
|
||||
host_group_id: true,
|
||||
agent_version: true,
|
||||
auto_update: true,
|
||||
created_at: true,
|
||||
updated_at: true,
|
||||
host_groups: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
|
||||
@@ -145,7 +145,7 @@ router.get('/:packageId', async (req, res) => {
|
||||
const packageData = await prisma.packages.findUnique({
|
||||
where: { id: packageId },
|
||||
include: {
|
||||
hostPackages: {
|
||||
host_packages: {
|
||||
include: {
|
||||
host: {
|
||||
select: {
|
||||
@@ -171,21 +171,21 @@ router.get('/:packageId', async (req, res) => {
|
||||
|
||||
// Calculate statistics
|
||||
const stats = {
|
||||
totalInstalls: packageData.hostPackages.length,
|
||||
updatesNeeded: packageData.hostPackages.filter(hp => hp.needsUpdate).length,
|
||||
securityUpdates: packageData.hostPackages.filter(hp => hp.needsUpdate && hp.isSecurityUpdate).length,
|
||||
upToDate: packageData.hostPackages.filter(hp => !hp.needsUpdate).length
|
||||
totalInstalls: packageData.host_packages.length,
|
||||
updatesNeeded: packageData.host_packages.filter(hp => hp.needsUpdate).length,
|
||||
securityUpdates: packageData.host_packages.filter(hp => hp.needsUpdate && hp.isSecurityUpdate).length,
|
||||
upToDate: packageData.host_packages.filter(hp => !hp.needsUpdate).length
|
||||
};
|
||||
|
||||
// Group by version
|
||||
const versionDistribution = packageData.hostPackages.reduce((acc, hp) => {
|
||||
const versionDistribution = packageData.host_packages.reduce((acc, hp) => {
|
||||
const version = hp.currentVersion;
|
||||
acc[version] = (acc[version] || 0) + 1;
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
// Group by OS type
|
||||
const osDistribution = packageData.hostPackages.reduce((acc, hp) => {
|
||||
const osDistribution = packageData.host_packages.reduce((acc, hp) => {
|
||||
const osType = hp.host.osType;
|
||||
acc[osType] = (acc[osType] || 0) + 1;
|
||||
return acc;
|
||||
|
||||
@@ -10,14 +10,14 @@ const prisma = new PrismaClient();
|
||||
// Get all repositories with host count
|
||||
router.get('/', authenticateToken, requireViewHosts, async (req, res) => {
|
||||
try {
|
||||
const repositories = await prisma.repository.findMany({
|
||||
const repositories = await prisma.repositories.findMany({
|
||||
include: {
|
||||
hostRepositories: {
|
||||
host_repositories: {
|
||||
include: {
|
||||
host: {
|
||||
hosts: {
|
||||
select: {
|
||||
id: true,
|
||||
friendlyName: true,
|
||||
friendly_name: true,
|
||||
status: true
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ router.get('/', authenticateToken, requireViewHosts, async (req, res) => {
|
||||
},
|
||||
_count: {
|
||||
select: {
|
||||
hostRepositories: true
|
||||
host_repositories: true
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -38,15 +38,15 @@ router.get('/', authenticateToken, requireViewHosts, async (req, res) => {
|
||||
// Transform data to include host counts and status
|
||||
const transformedRepos = repositories.map(repo => ({
|
||||
...repo,
|
||||
hostCount: repo._count.hostRepositories,
|
||||
enabledHostCount: repo.hostRepositories.filter(hr => hr.isEnabled).length,
|
||||
activeHostCount: repo.hostRepositories.filter(hr => hr.host.status === 'active').length,
|
||||
hosts: repo.hostRepositories.map(hr => ({
|
||||
id: hr.host.id,
|
||||
friendlyName: hr.host.friendlyName,
|
||||
status: hr.host.status,
|
||||
isEnabled: hr.isEnabled,
|
||||
lastChecked: hr.lastChecked
|
||||
hostCount: repo._count.host_repositories,
|
||||
enabledHostCount: repo.host_repositories.filter(hr => hr.is_enabled).length,
|
||||
activeHostCount: repo.host_repositories.filter(hr => hr.hosts.status === 'active').length,
|
||||
hosts: repo.host_repositories.map(hr => ({
|
||||
id: hr.hosts.id,
|
||||
friendlyName: hr.hosts.friendly_name,
|
||||
status: hr.hosts.status,
|
||||
isEnabled: hr.is_enabled,
|
||||
lastChecked: hr.last_checked
|
||||
}))
|
||||
}));
|
||||
|
||||
@@ -62,19 +62,19 @@ router.get('/host/:hostId', authenticateToken, requireViewHosts, async (req, res
|
||||
try {
|
||||
const { hostId } = req.params;
|
||||
|
||||
const hostRepositories = await prisma.hostRepository.findMany({
|
||||
where: { hostId },
|
||||
const hostRepositories = await prisma.host_repositories.findMany({
|
||||
where: { host_id: hostId },
|
||||
include: {
|
||||
repository: true,
|
||||
host: {
|
||||
repositories: true,
|
||||
hosts: {
|
||||
select: {
|
||||
id: true,
|
||||
friendlyName: true
|
||||
friendly_name: true
|
||||
}
|
||||
}
|
||||
},
|
||||
orderBy: {
|
||||
repository: {
|
||||
repositories: {
|
||||
name: 'asc'
|
||||
}
|
||||
}
|
||||
@@ -92,27 +92,27 @@ router.get('/:repositoryId', authenticateToken, requireViewHosts, async (req, re
|
||||
try {
|
||||
const { repositoryId } = req.params;
|
||||
|
||||
const repository = await prisma.repository.findUnique({
|
||||
const repository = await prisma.repositories.findUnique({
|
||||
where: { id: repositoryId },
|
||||
include: {
|
||||
hostRepositories: {
|
||||
host_repositories: {
|
||||
include: {
|
||||
host: {
|
||||
hosts: {
|
||||
select: {
|
||||
id: true,
|
||||
friendlyName: true,
|
||||
friendly_name: true,
|
||||
hostname: true,
|
||||
ip: true,
|
||||
osType: true,
|
||||
osVersion: true,
|
||||
os_type: true,
|
||||
os_version: true,
|
||||
status: true,
|
||||
lastUpdate: true
|
||||
last_update: true
|
||||
}
|
||||
}
|
||||
},
|
||||
orderBy: {
|
||||
host: {
|
||||
friendlyName: 'asc'
|
||||
hosts: {
|
||||
friendly_name: 'asc'
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -146,18 +146,18 @@ router.put('/:repositoryId', authenticateToken, requireManageHosts, [
|
||||
const { repositoryId } = req.params;
|
||||
const { name, description, isActive, priority } = req.body;
|
||||
|
||||
const repository = await prisma.repository.update({
|
||||
const repository = await prisma.repositories.update({
|
||||
where: { id: repositoryId },
|
||||
data: {
|
||||
...(name && { name }),
|
||||
...(description !== undefined && { description }),
|
||||
...(isActive !== undefined && { isActive }),
|
||||
...(isActive !== undefined && { is_active: isActive }),
|
||||
...(priority !== undefined && { priority })
|
||||
},
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
hostRepositories: true
|
||||
host_repositories: true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -183,29 +183,29 @@ router.patch('/host/:hostId/repository/:repositoryId', authenticateToken, requir
|
||||
const { hostId, repositoryId } = req.params;
|
||||
const { isEnabled } = req.body;
|
||||
|
||||
const hostRepository = await prisma.hostRepository.update({
|
||||
const hostRepository = await prisma.host_repositories.update({
|
||||
where: {
|
||||
hostId_repositoryId: {
|
||||
hostId,
|
||||
repositoryId
|
||||
host_id_repository_id: {
|
||||
host_id: hostId,
|
||||
repository_id: repositoryId
|
||||
}
|
||||
},
|
||||
data: {
|
||||
isEnabled,
|
||||
lastChecked: new Date()
|
||||
is_enabled: isEnabled,
|
||||
last_checked: new Date()
|
||||
},
|
||||
include: {
|
||||
repository: true,
|
||||
host: {
|
||||
repositories: true,
|
||||
hosts: {
|
||||
select: {
|
||||
friendlyName: true
|
||||
friendly_name: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
res.json({
|
||||
message: `Repository ${isEnabled ? 'enabled' : 'disabled'} for host ${hostRepository.host.friendlyName}`,
|
||||
message: `Repository ${isEnabled ? 'enabled' : 'disabled'} for host ${hostRepository.hosts.friendly_name}`,
|
||||
hostRepository
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -217,25 +217,25 @@ router.patch('/host/:hostId/repository/:repositoryId', authenticateToken, requir
|
||||
// Get repository statistics
|
||||
router.get('/stats/summary', authenticateToken, requireViewHosts, async (req, res) => {
|
||||
try {
|
||||
const stats = await prisma.repository.aggregate({
|
||||
const stats = await prisma.repositories.aggregate({
|
||||
_count: true
|
||||
});
|
||||
|
||||
const hostRepoStats = await prisma.hostRepository.aggregate({
|
||||
const hostRepoStats = await prisma.host_repositories.aggregate({
|
||||
_count: {
|
||||
isEnabled: true
|
||||
is_enabled: true
|
||||
},
|
||||
where: {
|
||||
isEnabled: true
|
||||
is_enabled: true
|
||||
}
|
||||
});
|
||||
|
||||
const secureRepos = await prisma.repository.count({
|
||||
where: { isSecure: true }
|
||||
const secureRepos = await prisma.repositories.count({
|
||||
where: { is_secure: true }
|
||||
});
|
||||
|
||||
const activeRepos = await prisma.repository.count({
|
||||
where: { isActive: true }
|
||||
const activeRepos = await prisma.repositories.count({
|
||||
where: { is_active: true }
|
||||
});
|
||||
|
||||
res.json({
|
||||
@@ -257,9 +257,9 @@ router.delete('/cleanup/orphaned', authenticateToken, requireManageHosts, async
|
||||
console.log('Cleaning up orphaned repositories...');
|
||||
|
||||
// Find repositories with no host relationships
|
||||
const orphanedRepos = await prisma.repository.findMany({
|
||||
const orphanedRepos = await prisma.repositories.findMany({
|
||||
where: {
|
||||
hostRepositories: {
|
||||
host_repositories: {
|
||||
none: {}
|
||||
}
|
||||
}
|
||||
@@ -274,7 +274,7 @@ router.delete('/cleanup/orphaned', authenticateToken, requireManageHosts, async
|
||||
}
|
||||
|
||||
// Delete orphaned repositories
|
||||
const deleteResult = await prisma.repository.deleteMany({
|
||||
const deleteResult = await prisma.repositories.deleteMany({
|
||||
where: {
|
||||
hostRepositories: {
|
||||
none: {}
|
||||
|
||||
@@ -14,16 +14,16 @@ async function triggerCrontabUpdates() {
|
||||
console.log('Triggering crontab updates on all hosts with auto-update enabled...');
|
||||
|
||||
// Get all hosts that have auto-update enabled
|
||||
const hosts = await prisma.host.findMany({
|
||||
const hosts = await prisma.hosts.findMany({
|
||||
where: {
|
||||
autoUpdate: true,
|
||||
auto_update: true,
|
||||
status: 'active' // Only update active hosts
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
friendlyName: true,
|
||||
apiId: true,
|
||||
apiKey: true
|
||||
friendly_name: true,
|
||||
api_id: true,
|
||||
api_key: true
|
||||
}
|
||||
});
|
||||
|
||||
@@ -33,7 +33,7 @@ async function triggerCrontabUpdates() {
|
||||
// This is done by sending a ping with a special flag
|
||||
for (const host of hosts) {
|
||||
try {
|
||||
console.log(`Triggering crontab update for host: ${host.friendlyName}`);
|
||||
console.log(`Triggering crontab update for host: ${host.friendly_name}`);
|
||||
|
||||
// We'll use the existing ping endpoint but add a special parameter
|
||||
// The agent will detect this and run update-crontab command
|
||||
@@ -58,27 +58,27 @@ async function triggerCrontabUpdates() {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': Buffer.byteLength(postData),
|
||||
'X-API-ID': host.apiId,
|
||||
'X-API-KEY': host.apiKey
|
||||
'X-API-ID': host.api_id,
|
||||
'X-API-KEY': host.api_key
|
||||
}
|
||||
};
|
||||
|
||||
const req = client.request(options, (res) => {
|
||||
if (res.statusCode === 200) {
|
||||
console.log(`Successfully triggered crontab update for ${host.friendlyName}`);
|
||||
console.log(`Successfully triggered crontab update for ${host.friendly_name}`);
|
||||
} else {
|
||||
console.error(`Failed to trigger crontab update for ${host.friendlyName}: ${res.statusCode}`);
|
||||
console.error(`Failed to trigger crontab update for ${host.friendly_name}: ${res.statusCode}`);
|
||||
}
|
||||
});
|
||||
|
||||
req.on('error', (error) => {
|
||||
console.error(`Error triggering crontab update for ${host.friendlyName}:`, error.message);
|
||||
console.error(`Error triggering crontab update for ${host.friendly_name}:`, error.message);
|
||||
});
|
||||
|
||||
req.write(postData);
|
||||
req.end();
|
||||
} catch (error) {
|
||||
console.error(`Error triggering crontab update for ${host.friendlyName}:`, error.message);
|
||||
console.error(`Error triggering crontab update for ${host.friendly_name}:`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ router.put('/', authenticateToken, requireManageSettings, [
|
||||
repositoryType: repositoryType || 'public'
|
||||
});
|
||||
console.log('Final githubRepoUrl value being saved:', githubRepoUrl !== undefined ? githubRepoUrl : 'git@github.com:9technologygroup/patchmon.net.git');
|
||||
const oldUpdateInterval = settings.updateInterval;
|
||||
const oldUpdateInterval = settings.update_interval;
|
||||
|
||||
settings = await prisma.settings.update({
|
||||
where: { id: settings.id },
|
||||
@@ -230,13 +230,13 @@ router.get('/server-url', async (req, res) => {
|
||||
const settings = await prisma.settings.findFirst();
|
||||
|
||||
if (!settings) {
|
||||
return res.json({ serverUrl: 'http://localhost:3001' });
|
||||
return res.json({ server_url: 'http://localhost:3001' });
|
||||
}
|
||||
|
||||
res.json({ serverUrl: settings.serverUrl });
|
||||
res.json({ server_url: settings.server_url });
|
||||
} catch (error) {
|
||||
console.error('Server URL fetch error:', error);
|
||||
res.json({ serverUrl: 'http://localhost:3001' });
|
||||
res.json({ server_url: 'http://localhost:3001' });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -250,8 +250,8 @@ router.get('/update-interval', async (req, res) => {
|
||||
}
|
||||
|
||||
res.json({
|
||||
updateInterval: settings.updateInterval,
|
||||
cronExpression: `*/${settings.updateInterval} * * * *` // Generate cron expression
|
||||
updateInterval: settings.update_interval,
|
||||
cronExpression: `*/${settings.update_interval} * * * *` // Generate cron expression
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Update interval fetch error:', error);
|
||||
@@ -269,7 +269,7 @@ router.get('/auto-update', async (req, res) => {
|
||||
}
|
||||
|
||||
res.json({
|
||||
autoUpdate: settings.autoUpdate || false
|
||||
autoUpdate: settings.auto_update || false
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Auto-update fetch error:', error);
|
||||
|
||||
@@ -14,12 +14,12 @@ router.get('/setup', authenticateToken, async (req, res) => {
|
||||
const userId = req.user.id;
|
||||
|
||||
// Check if user already has TFA enabled
|
||||
const user = await prisma.user.findUnique({
|
||||
const user = await prisma.users.findUnique({
|
||||
where: { id: userId },
|
||||
select: { tfaEnabled: true, tfaSecret: true }
|
||||
});
|
||||
|
||||
if (user.tfaEnabled) {
|
||||
if (user.tfa_enabled) {
|
||||
return res.status(400).json({
|
||||
error: 'Two-factor authentication is already enabled for this account'
|
||||
});
|
||||
@@ -36,9 +36,9 @@ router.get('/setup', authenticateToken, async (req, res) => {
|
||||
const qrCodeUrl = await QRCode.toDataURL(secret.otpauth_url);
|
||||
|
||||
// Store the secret temporarily (not enabled yet)
|
||||
await prisma.user.update({
|
||||
await prisma.users.update({
|
||||
where: { id: userId },
|
||||
data: { tfaSecret: secret.base32 }
|
||||
data: { tfa_secret: secret.base32 }
|
||||
});
|
||||
|
||||
res.json({
|
||||
@@ -67,18 +67,18 @@ router.post('/verify-setup', authenticateToken, [
|
||||
const userId = req.user.id;
|
||||
|
||||
// Get user's TFA secret
|
||||
const user = await prisma.user.findUnique({
|
||||
const user = await prisma.users.findUnique({
|
||||
where: { id: userId },
|
||||
select: { tfaSecret: true, tfaEnabled: true }
|
||||
select: { tfa_secret: true, tfa_enabled: true }
|
||||
});
|
||||
|
||||
if (!user.tfaSecret) {
|
||||
if (!user.tfa_secret) {
|
||||
return res.status(400).json({
|
||||
error: 'No TFA secret found. Please start the setup process first.'
|
||||
});
|
||||
}
|
||||
|
||||
if (user.tfaEnabled) {
|
||||
if (user.tfa_enabled) {
|
||||
return res.status(400).json({
|
||||
error: 'Two-factor authentication is already enabled for this account'
|
||||
});
|
||||
@@ -104,11 +104,11 @@ router.post('/verify-setup', authenticateToken, [
|
||||
);
|
||||
|
||||
// Enable TFA and store backup codes
|
||||
await prisma.user.update({
|
||||
await prisma.users.update({
|
||||
where: { id: userId },
|
||||
data: {
|
||||
tfaEnabled: true,
|
||||
tfaBackupCodes: JSON.stringify(backupCodes)
|
||||
tfa_enabled: true,
|
||||
tfa_backup_codes: JSON.stringify(backupCodes)
|
||||
}
|
||||
});
|
||||
|
||||
@@ -136,12 +136,12 @@ router.post('/disable', authenticateToken, [
|
||||
const userId = req.user.id;
|
||||
|
||||
// Verify password
|
||||
const user = await prisma.user.findUnique({
|
||||
const user = await prisma.users.findUnique({
|
||||
where: { id: userId },
|
||||
select: { passwordHash: true, tfaEnabled: true }
|
||||
select: { password_hash: true, tfa_enabled: true }
|
||||
});
|
||||
|
||||
if (!user.tfaEnabled) {
|
||||
if (!user.tfa_enabled) {
|
||||
return res.status(400).json({
|
||||
error: 'Two-factor authentication is not enabled for this account'
|
||||
});
|
||||
@@ -151,12 +151,12 @@ router.post('/disable', authenticateToken, [
|
||||
// For now, we'll skip password verification for simplicity
|
||||
|
||||
// Disable TFA
|
||||
await prisma.user.update({
|
||||
where: { id: userId },
|
||||
await prisma.users.update({
|
||||
where: { id: id },
|
||||
data: {
|
||||
tfaEnabled: false,
|
||||
tfaSecret: null,
|
||||
tfaBackupCodes: null
|
||||
tfa_enabled: false,
|
||||
tfa_secret: null,
|
||||
tfa_backup_codes: null
|
||||
}
|
||||
});
|
||||
|
||||
@@ -174,18 +174,18 @@ router.get('/status', authenticateToken, async (req, res) => {
|
||||
try {
|
||||
const userId = req.user.id;
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
const user = await prisma.users.findUnique({
|
||||
where: { id: userId },
|
||||
select: {
|
||||
tfaEnabled: true,
|
||||
tfaSecret: true,
|
||||
tfaBackupCodes: true
|
||||
tfa_enabled: true,
|
||||
tfa_secret: true,
|
||||
tfa_backup_codes: true
|
||||
}
|
||||
});
|
||||
|
||||
res.json({
|
||||
enabled: user.tfaEnabled,
|
||||
hasBackupCodes: !!user.tfaBackupCodes
|
||||
enabled: user.tfa_enabled,
|
||||
hasBackupCodes: !!user.tfa_backup_codes
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('TFA status error:', error);
|
||||
@@ -199,12 +199,12 @@ router.post('/regenerate-backup-codes', authenticateToken, async (req, res) => {
|
||||
const userId = req.user.id;
|
||||
|
||||
// Check if TFA is enabled
|
||||
const user = await prisma.user.findUnique({
|
||||
const user = await prisma.users.findUnique({
|
||||
where: { id: userId },
|
||||
select: { tfaEnabled: true }
|
||||
});
|
||||
|
||||
if (!user.tfaEnabled) {
|
||||
if (!user.tfa_enabled) {
|
||||
return res.status(400).json({
|
||||
error: 'Two-factor authentication is not enabled for this account'
|
||||
});
|
||||
@@ -216,7 +216,7 @@ router.post('/regenerate-backup-codes', authenticateToken, async (req, res) => {
|
||||
);
|
||||
|
||||
// Update backup codes
|
||||
await prisma.user.update({
|
||||
await prisma.users.update({
|
||||
where: { id: userId },
|
||||
data: {
|
||||
tfaBackupCodes: JSON.stringify(backupCodes)
|
||||
@@ -248,17 +248,17 @@ router.post('/verify', [
|
||||
const { username, token } = req.body;
|
||||
|
||||
// Get user's TFA secret
|
||||
const user = await prisma.user.findUnique({
|
||||
const user = await prisma.users.findUnique({
|
||||
where: { username },
|
||||
select: {
|
||||
id: true,
|
||||
tfaEnabled: true,
|
||||
tfaSecret: true,
|
||||
tfaBackupCodes: true
|
||||
tfa_enabled: true,
|
||||
tfa_secret: true,
|
||||
tfa_backup_codes: true
|
||||
}
|
||||
});
|
||||
|
||||
if (!user || !user.tfaEnabled || !user.tfaSecret) {
|
||||
if (!user || !user.tfa_enabled || !user.tfa_secret) {
|
||||
return res.status(400).json({
|
||||
error: 'Two-factor authentication is not enabled for this account'
|
||||
});
|
||||
@@ -273,7 +273,7 @@ router.post('/verify', [
|
||||
if (isBackupCode) {
|
||||
// Remove the used backup code
|
||||
const updatedBackupCodes = backupCodes.filter(code => code !== token);
|
||||
await prisma.user.update({
|
||||
await prisma.users.update({
|
||||
where: { id: user.id },
|
||||
data: {
|
||||
tfaBackupCodes: JSON.stringify(updatedBackupCodes)
|
||||
@@ -283,7 +283,7 @@ router.post('/verify', [
|
||||
} else {
|
||||
// Verify TOTP token
|
||||
verified = speakeasy.totp.verify({
|
||||
secret: user.tfaSecret,
|
||||
secret: user.tfa_secret,
|
||||
encoding: 'base32',
|
||||
token: token,
|
||||
window: 2
|
||||
|
||||
@@ -159,21 +159,21 @@ router.get('/check-updates', authenticateToken, requireManageSettings, async (re
|
||||
}
|
||||
|
||||
const currentVersion = '1.2.6';
|
||||
const latestVersion = settings.latestVersion || currentVersion;
|
||||
const isUpdateAvailable = settings.updateAvailable || false;
|
||||
const lastUpdateCheck = settings.lastUpdateCheck;
|
||||
const latestVersion = settings.latest_version || currentVersion;
|
||||
const isUpdateAvailable = settings.update_available || false;
|
||||
const lastUpdateCheck = settings.last_update_check || null;
|
||||
|
||||
res.json({
|
||||
currentVersion,
|
||||
latestVersion,
|
||||
isUpdateAvailable,
|
||||
lastUpdateCheck,
|
||||
repositoryType: settings.repositoryType || 'public',
|
||||
repositoryType: settings.repo_type || 'public',
|
||||
latestRelease: {
|
||||
tagName: latestVersion ? `v${latestVersion}` : null,
|
||||
version: latestVersion,
|
||||
repository: settings.githubRepoUrl ? settings.githubRepoUrl.split('/').slice(-2).join('/') : null,
|
||||
accessMethod: settings.repositoryType === 'private' ? 'ssh' : 'api'
|
||||
repository: settings.github_repo_url ? settings.githubRepoUrl.split('/').slice(-2).join('/') : null,
|
||||
accessMethod: settings.repo_type === 'private' ? 'ssh' : 'api'
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user