diff --git a/.gitignore b/.gitignore index 946997d..eec901c 100644 --- a/.gitignore +++ b/.gitignore @@ -143,3 +143,4 @@ manage-patchmon.sh setup-installer-site.sh install-server.* notify-clients-upgrade.sh +debug-agent.sh diff --git a/backend/src/routes/hostRoutes.js b/backend/src/routes/hostRoutes.js index 7b15beb..289ef11 100644 --- a/backend/src/routes/hostRoutes.js +++ b/backend/src/routes/hostRoutes.js @@ -332,15 +332,34 @@ router.post('/update', validateApiCredentials, [ }); } - // Create host repository relationship - await tx.hostRepository.create({ - data: { - hostId: host.id, - repositoryId: repo.id, - isEnabled: repoData.isEnabled !== false, // Default to enabled - lastChecked: new Date() + // Create host repository relationship (handle duplicates gracefully) + try { + await tx.hostRepository.create({ + data: { + hostId: host.id, + repositoryId: repo.id, + isEnabled: repoData.isEnabled !== false, // Default to enabled + lastChecked: new Date() + } + }); + } catch (error) { + // If it's a duplicate constraint error, update the existing record + if (error.code === 'P2002') { + await tx.hostRepository.updateMany({ + where: { + hostId: host.id, + repositoryId: repo.id + }, + data: { + isEnabled: repoData.isEnabled !== false, + lastChecked: new Date() + } + }); + } else { + // Re-throw other errors + throw error; } - }); + } } } });