Fix sync errors for duplicate projects and add modal scrolling

Root cause: Projects with duplicate (code, variant) pairs fail to sync
due to unique constraint on server. Example: multiple "OPS-1934" projects
with variant="Dell" where one already exists on server.

Fixes:
1. Sync service now detects duplicate (code, variant) on server and links
   local project to existing server project instead of failing
2. Local repair checks for duplicate (code, variant) pairs and deduplicates
   by appending UUID suffix to variant
3. Modal now scrollable with fixed header/footer (max-h-90vh)

This allows users to sync projects that were created offline with
conflicting codes/variants without losing data.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-16 21:25:22 +03:00
parent b153afbf51
commit 8508ee2921
3 changed files with 48 additions and 9 deletions

View File

@@ -1088,7 +1088,9 @@ func (l *LocalDB) RepairPendingChanges() (int, []string, error) {
return repaired, remainingErrors, nil
}
// repairProjectChange validates and fixes project data
// repairProjectChange validates and fixes project data.
// Note: This only validates local data. Server-side conflicts (like duplicate code+variant)
// are handled by sync service layer with deduplication logic.
func (l *LocalDB) repairProjectChange(change *PendingChange) error {
project, err := l.GetProjectByUUID(change.EntityUUID)
if err != nil {
@@ -1123,6 +1125,20 @@ func (l *LocalDB) repairProjectChange(change *PendingChange) error {
modified = true
}
// Check for local duplicates with same (code, variant)
var duplicate LocalProject
err = l.db.Where("code = ? AND variant = ? AND uuid != ?", project.Code, project.Variant, project.UUID).
First(&duplicate).Error
if err == nil {
// Found local duplicate - deduplicate by appending UUID suffix to variant
if project.Variant == "" {
project.Variant = project.UUID[:8]
} else {
project.Variant = project.Variant + "-" + project.UUID[:8]
}
modified = true
}
if modified {
if err := l.SaveProject(project); err != nil {
return fmt.Errorf("saving repaired project: %w", err)

View File

@@ -856,10 +856,29 @@ func (s *Service) pushProjectChange(change *localdb.PendingChange) error {
}
}
if err := projectRepo.UpsertByUUID(&project); err != nil {
return fmt.Errorf("upsert project on server: %w", err)
// Try upsert by UUID first
err = projectRepo.UpsertByUUID(&project)
if err != nil {
// Check if it's a duplicate (code, variant) constraint violation
// In this case, find existing project with same (code, variant) and link to it
var existing models.Project
lookupErr := mariaDB.Where("code = ? AND variant = ?", project.Code, project.Variant).First(&existing).Error
if lookupErr == nil {
// Found duplicate - link local project to existing server project
slog.Info("project duplicate found, linking to existing",
"local_uuid", project.UUID,
"server_uuid", existing.UUID,
"server_id", existing.ID,
"code", project.Code,
"variant", project.Variant)
project.ID = existing.ID
} else {
// Not a duplicate issue, return original error
return fmt.Errorf("upsert project on server: %w", err)
}
}
// Update local project with server ID
localProject, localErr := s.localDB.GetProjectByUUID(project.UUID)
if localErr == nil {
if project.ID > 0 {

View File

@@ -45,10 +45,10 @@
<div id="toast" class="fixed bottom-4 right-4 z-50"></div>
<!-- Sync Info Modal -->
<div id="sync-modal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden">
<div class="bg-white rounded-lg shadow-xl max-w-lg w-full mx-4">
<div class="p-6">
<div class="flex justify-between items-center mb-4">
<div id="sync-modal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden p-4">
<div class="bg-white rounded-lg shadow-xl max-w-lg w-full max-h-[90vh] flex flex-col">
<div class="p-6 border-b border-gray-200 flex-shrink-0">
<div class="flex justify-between items-center">
<h3 class="text-lg font-medium text-gray-900">Информация о синхронизации</h3>
<button onclick="closeSyncModal()" class="text-gray-400 hover:text-gray-500">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
@@ -56,8 +56,10 @@
</svg>
</button>
</div>
</div>
<div class="space-y-5">
<div class="overflow-y-auto flex-1">
<div class="p-6 space-y-5">
<!-- Section 1: DB Connection -->
<div>
<h4 class="font-medium text-gray-900 mb-2">Подключение к БД</h4>
@@ -144,8 +146,10 @@
</div>
</div>
</div>
</div>
<div class="mt-6 flex justify-end">
<div class="p-6 border-t border-gray-200 flex-shrink-0">
<div class="flex justify-end">
<button onclick="closeSyncModal()" class="px-4 py-2 bg-gray-600 text-white rounded-md hover:bg-gray-700">
Закрыть
</button>