Add configuration revisions system and project variant deletion

Features:
- Configuration versioning: immutable snapshots in local_configuration_versions
- Revisions UI: /configs/:uuid/revisions page to view version history
- Clone from version: ability to clone configuration from specific revision
- Project variant deletion: DELETE /api/projects/:uuid endpoint
- Updated CLAUDE.md with new architecture details and endpoints

Architecture updates:
- local_configuration_versions table for immutable snapshots
- Version tracking on each configuration save
- Rollback capability to previous versions
- Variant deletion with main variant protection

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-16 22:30:33 +03:00
parent 8508ee2921
commit 2e973b6d78
10 changed files with 407 additions and 17 deletions

View File

@@ -0,0 +1,202 @@
{{define "title"}}Ревизии - QuoteForge{{end}}
{{define "content"}}
<div class="space-y-4">
<div class="flex items-center gap-3">
<a href="/projects" class="text-gray-500 hover:text-gray-700" title="Все проекты">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 9.75L12 3l9 6.75v9A2.25 2.25 0 0118.75 21h-13.5A2.25 2.25 0 013 18.75v-9z"></path>
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 21v-6h6v6"></path>
</svg>
</a>
<div class="text-2xl font-bold flex items-center gap-2" id="revisions-breadcrumbs">
<a id="breadcrumb-code-link" href="/projects" class="text-blue-700 hover:underline">
<span id="breadcrumb-code"></span>
</a>
<span class="text-gray-400">-</span>
<a id="breadcrumb-variant-link" href="/projects" class="text-blue-700 hover:underline">
<span id="breadcrumb-variant">main</span>
</a>
<span class="text-gray-400">-</span>
<a id="breadcrumb-config-link" href="/configurator" class="text-blue-700 hover:underline">
<span id="breadcrumb-config"></span>
</a>
<span class="text-gray-400">-</span>
<span class="text-gray-600">Ревизии</span>
</div>
</div>
<div id="revisions-list">
<div class="text-center py-8 text-gray-500">Загрузка...</div>
</div>
</div>
<script>
const configUUID = '{{.ConfigUUID}}';
let configData = null;
let projectData = null;
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text || '';
return div.innerHTML;
}
async function loadConfigInfo() {
try {
const resp = await fetch('/api/configs/' + configUUID);
if (!resp.ok) {
document.getElementById('revisions-list').innerHTML =
'<div class="bg-white rounded-lg shadow p-8 text-center text-red-600">Конфигурация не найдена</div>';
return false;
}
configData = await resp.json();
document.getElementById('breadcrumb-config').textContent = configData.name || 'Конфигурация';
document.getElementById('breadcrumb-config-link').href = '/configurator?uuid=' + configUUID;
if (configData.project_uuid) {
const projResp = await fetch('/api/projects/' + configData.project_uuid);
if (projResp.ok) {
projectData = await projResp.json();
document.getElementById('breadcrumb-code').textContent = projectData.code || '—';
const variant = (projectData.variant || '').trim();
document.getElementById('breadcrumb-variant').textContent = variant === '' ? 'main' : variant;
document.getElementById('breadcrumb-variant-link').href = '/projects/' + projectData.uuid;
// Find main variant for code link
const allResp = await fetch('/api/projects/all');
if (allResp.ok) {
const allProjects = await allResp.json();
const main = (Array.isArray(allProjects) ? allProjects : []).find(
p => p.code === projectData.code && (p.variant || '').trim() === ''
);
if (main) {
document.getElementById('breadcrumb-code-link').href = '/projects/' + main.uuid;
}
}
}
}
return true;
} catch (e) {
document.getElementById('revisions-list').innerHTML =
'<div class="bg-white rounded-lg shadow p-8 text-center text-red-600">Ошибка загрузки</div>';
return false;
}
}
async function loadVersions() {
try {
const resp = await fetch('/api/configs/' + configUUID + '/versions?limit=200');
if (!resp.ok) {
document.getElementById('revisions-list').innerHTML =
'<div class="bg-white rounded-lg shadow p-8 text-center text-red-600">Ошибка загрузки ревизий</div>';
return;
}
const data = await resp.json();
const versions = data.versions || [];
renderVersions(versions);
} catch (e) {
document.getElementById('revisions-list').innerHTML =
'<div class="bg-white rounded-lg shadow p-8 text-center text-red-600">Ошибка загрузки</div>';
}
}
function renderVersions(versions) {
if (versions.length === 0) {
document.getElementById('revisions-list').innerHTML =
'<div class="bg-white rounded-lg shadow p-8 text-center text-gray-500">Нет ревизий</div>';
return;
}
let html = '<div class="bg-white rounded-lg shadow overflow-hidden"><table class="w-full">';
html += '<thead class="bg-gray-50"><tr>';
html += '<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Версия</th>';
html += '<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Дата</th>';
html += '<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Автор</th>';
html += '<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Примечание</th>';
html += '<th class="px-4 py-3 text-right text-xs font-medium text-gray-500 uppercase">Действия</th>';
html += '</tr></thead><tbody class="divide-y">';
versions.forEach((v, idx) => {
const date = new Date(v.created_at).toLocaleString('ru-RU');
const author = v.created_by || '—';
const note = v.change_note || '—';
const isCurrent = idx === 0;
html += '<tr class="hover:bg-gray-50' + (isCurrent ? ' bg-blue-50' : '') + '">';
html += '<td class="px-4 py-3 text-sm font-medium">';
html += 'v' + v.version_no;
if (isCurrent) html += ' <span class="text-xs text-blue-600 font-normal">(текущая)</span>';
html += '</td>';
html += '<td class="px-4 py-3 text-sm text-gray-500">' + escapeHtml(date) + '</td>';
html += '<td class="px-4 py-3 text-sm text-gray-500">' + escapeHtml(author) + '</td>';
html += '<td class="px-4 py-3 text-sm text-gray-500">' + escapeHtml(note) + '</td>';
html += '<td class="px-4 py-3 text-sm text-right space-x-2">';
// Open in configurator (readonly view)
html += '<a href="/configurator?uuid=' + configUUID + '&version=' + v.version_no + '" class="text-blue-600 hover:text-blue-800 inline-block" title="Открыть">';
html += '<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path></svg></a>';
// Clone from this version
html += '<button onclick="cloneFromVersion(' + v.version_no + ')" class="text-green-600 hover:text-green-800" title="Скопировать из этой ревизии">';
html += '<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"></path></svg></button>';
// Rollback (not for current version)
if (!isCurrent) {
html += '<button onclick="rollbackToVersion(' + v.version_no + ')" class="text-orange-600 hover:text-orange-800" title="Восстановить эту ревизию">';
html += '<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h10a8 8 0 018 8v2M3 10l6 6m-6-6l6-6"></path></svg></button>';
}
html += '</td></tr>';
});
html += '</tbody></table></div>';
document.getElementById('revisions-list').innerHTML = html;
}
async function cloneFromVersion(versionNo) {
const name = prompt('Название копии:', (configData ? configData.name : '') + ' (v' + versionNo + ')');
if (!name) return;
const resp = await fetch('/api/configs/' + configUUID + '/clone', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({name: name, from_version: versionNo})
});
if (!resp.ok) {
const data = await resp.json().catch(() => ({}));
alert(data.error || 'Не удалось скопировать');
return;
}
const created = await resp.json();
showToast('Копия создана', 'success');
if (created && created.uuid) {
window.location.href = '/configurator?uuid=' + created.uuid;
}
}
async function rollbackToVersion(versionNo) {
if (!confirm('Восстановить конфигурацию до ревизии v' + versionNo + '?')) return;
const resp = await fetch('/api/configs/' + configUUID + '/rollback', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({target_version: versionNo})
});
if (!resp.ok) {
const data = await resp.json().catch(() => ({}));
alert(data.error || 'Не удалось восстановить');
return;
}
showToast('Ревизия восстановлена', 'success');
loadVersions();
}
document.addEventListener('DOMContentLoaded', async function() {
const ok = await loadConfigInfo();
if (!ok) return;
await loadVersions();
});
</script>
{{end}}
{{template "base" .}}

View File

@@ -20,7 +20,9 @@
<span id="breadcrumb-project-variant">main</span>
</a>
<span class="text-gray-400">-</span>
<span id="breadcrumb-config-name">Конфигуратор</span>
<a id="breadcrumb-config-name-link" href="#" class="text-blue-700 hover:underline">
<span id="breadcrumb-config-name">Конфигуратор</span>
</a>
<span class="text-gray-400">-</span>
<span id="breadcrumb-config-version">v1</span>
</div>
@@ -338,6 +340,7 @@ let ASSIGNED_CATEGORIES = Object.values(TAB_CONFIG)
// State
let configUUID = '{{.ConfigUUID}}';
let configName = '';
let currentVersionNo = 1;
let projectUUID = '';
let projectName = '';
let projectCode = '';
@@ -399,7 +402,11 @@ function updateConfigBreadcrumbs() {
codeEl.textContent = code;
variantEl.textContent = variant;
configEl.textContent = configName || 'Конфигурация';
versionEl.textContent = 'v1';
versionEl.textContent = 'v' + (currentVersionNo || 1);
const configNameLinkEl = document.getElementById('breadcrumb-config-name-link');
if (configNameLinkEl && configUUID) {
configNameLinkEl.href = '/configs/' + configUUID + '/revisions';
}
}
let currentTab = 'base';
let allComponents = [];
@@ -688,6 +695,7 @@ document.addEventListener('DOMContentLoaded', async function() {
const config = await resp.json();
configName = config.name;
currentVersionNo = config.current_version_no || 1;
projectUUID = config.project_uuid || '';
await loadProjectIndex();
updateConfigBreadcrumbs();
@@ -1937,6 +1945,13 @@ async function saveConfig(showNotification = true) {
return;
}
const saved = await resp.json();
if (saved && saved.current_version_no) {
currentVersionNo = saved.current_version_no;
const versionEl = document.getElementById('breadcrumb-config-version');
if (versionEl) versionEl.textContent = 'v' + currentVersionNo;
}
if (showNotification) {
showToast('Сохранено', 'success');
}

View File

@@ -30,7 +30,7 @@
</div>
</div>
<div id="action-buttons" class="mt-4 grid grid-cols-1 sm:grid-cols-4 gap-3">
<div id="action-buttons" class="mt-4 grid grid-cols-1 sm:grid-cols-5 gap-3">
<button onclick="openNewVariantModal()" class="py-2 bg-purple-600 text-white rounded-lg hover:bg-purple-700 font-medium">
+ Новый вариант
</button>
@@ -43,6 +43,9 @@
<button onclick="openProjectSettingsModal()" class="py-2 bg-gray-700 text-white rounded-lg hover:bg-gray-800 font-medium">
Параметры
</button>
<button id="delete-variant-btn" onclick="deleteVariant()" class="py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 font-medium hidden">
Удалить вариант
</button>
</div>
<div class="mt-2">
<a id="tracker-link" href="https://tracker.yandex.ru/OPS-1933" target="_blank" rel="noopener noreferrer" class="text-sm text-blue-600 hover:text-blue-800 hover:underline">
@@ -196,6 +199,24 @@
</div>
</div>
<div id="transfer-modal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
<div class="bg-white rounded-lg shadow-xl w-full max-w-md mx-4 p-6">
<h2 class="text-xl font-semibold mb-4">Перенести квоту в другой вариант</h2>
<div class="space-y-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Целевой вариант</label>
<select id="transfer-variant-select" class="w-full px-3 py-2 border rounded focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
</select>
<input type="hidden" id="transfer-config-uuid">
</div>
</div>
<div class="flex justify-end space-x-3 mt-6">
<button onclick="closeTransferModal()" class="px-4 py-2 text-gray-600 hover:text-gray-800">Отмена</button>
<button onclick="transferConfig()" class="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700">Перенести</button>
</div>
</div>
</div>
<script>
const projectUUID = '{{.ProjectUUID}}';
let configStatusMode = 'active';
@@ -241,7 +262,7 @@ async function loadVariantsForCode(code) {
const data = await resp.json();
const allProjects = Array.isArray(data) ? data : (data.projects || []);
projectVariants = allProjects
.filter(p => (p.code || '').trim() === code)
.filter(p => (p.code || '').trim() === code && p.is_active !== false)
.map(p => ({uuid: p.uuid, variant: (p.variant || '').trim()}));
projectVariants.sort((a, b) => normalizeVariantLabel(a.variant).localeCompare(normalizeVariantLabel(b.variant)));
} catch (e) {
@@ -341,6 +362,7 @@ function renderConfigs(configs) {
html += '<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Цена (за 1 шт)</th>';
html += '<th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Кол-во</th>';
html += '<th class="px-4 py-3 text-right text-xs font-medium text-gray-500 uppercase">Сумма</th>';
html += '<th class="px-4 py-3 text-center text-xs font-medium text-gray-500 uppercase">Ревизия</th>';
html += '<th class="px-4 py-3 text-right text-xs font-medium text-gray-500 uppercase">Действия</th>';
html += '</tr></thead><tbody class="divide-y">';
@@ -363,11 +385,17 @@ function renderConfigs(configs) {
html += '<td class="px-4 py-3 text-sm text-gray-500">$' + unitPrice.toLocaleString('en-US', {minimumFractionDigits: 2}) + '</td>';
html += '<td class="px-4 py-3 text-sm text-gray-500">' + serverCount + '</td>';
html += '<td class="px-4 py-3 text-sm text-right">$' + total.toLocaleString('en-US', {minimumFractionDigits: 2}) + '</td>';
const versionNo = c.current_version_no || 1;
html += '<td class="px-4 py-3 text-sm text-center text-gray-500">v' + versionNo + '</td>';
html += '<td class="px-4 py-3 text-sm text-right space-x-2">';
if (configStatusMode === 'archived') {
html += '<button onclick="reactivateConfig(\'' + c.uuid + '\')" class="text-emerald-600 hover:text-emerald-800" title="Восстановить">';
html += '<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg></button>';
} else {
html += '<a href="/configs/' + c.uuid + '/revisions" class="text-purple-600 hover:text-purple-800 inline-block" title="Ревизии">';
html += '<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path></svg></a>';
html += '<button onclick="openTransferModal(\'' + c.uuid + '\')" class="text-indigo-600 hover:text-indigo-800" title="Перенести в другой вариант">';
html += '<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7h12m0 0l-4-4m4 4l-4 4m0 6H4m0 0l4 4m-4-4l4-4"></path></svg></button>';
html += '<button onclick="openCloneModal(\'' + c.uuid + '\', \'' + escapeHtml(c.name).replace(/'/g, "\\'") + '\')" class="text-green-600 hover:text-green-800" title="Копировать">';
html += '<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"></path></svg></button>';
html += '<button onclick="openRenameModal(\'' + c.uuid + '\', \'' + escapeHtml(c.name).replace(/'/g, "\\'") + '\')" class="text-blue-600 hover:text-blue-800" title="Переименовать">';
@@ -385,6 +413,7 @@ function renderConfigs(configs) {
html += '<td class="px-4 py-3 text-sm text-gray-700">' + configs.length + '</td>';
html += '<td class="px-4 py-3 text-sm text-right font-semibold text-gray-900">$' + totalSum.toLocaleString('en-US', {minimumFractionDigits: 2}) + '</td>';
html += '<td class="px-4 py-3"></td>';
html += '<td class="px-4 py-3"></td>';
html += '</tr>';
html += '</tfoot>';
html += '</table></div>';
@@ -757,12 +786,83 @@ function wildcardMatch(value, pattern) {
return regex.test(value);
}
async function deleteVariant() {
if (!project) return;
const variantLabel = normalizeVariantLabel(project.variant);
if (!confirm('Удалить вариант «' + variantLabel + '»? Все квоты будут архивированы.')) return;
const resp = await fetch('/api/projects/' + projectUUID, {method: 'DELETE'});
if (!resp.ok) {
const data = await resp.json().catch(() => ({}));
alert(data.error || 'Не удалось удалить вариант');
return;
}
// Redirect to main variant or projects list
const mainVariant = projectVariants.find(v => normalizeVariantLabel(v.variant) === 'main');
if (mainVariant && mainVariant.uuid !== projectUUID) {
window.location.href = '/projects/' + mainVariant.uuid;
} else {
window.location.href = '/projects';
}
}
function updateDeleteVariantButton() {
const btn = document.getElementById('delete-variant-btn');
if (!btn || !project) return;
if ((project.variant || '').trim() !== '') {
btn.classList.remove('hidden');
} else {
btn.classList.add('hidden');
}
}
function openTransferModal(configUUID) {
const select = document.getElementById('transfer-variant-select');
select.innerHTML = '';
const otherVariants = projectVariants.filter(v => v.uuid !== projectUUID);
if (otherVariants.length === 0) {
alert('Нет других вариантов для переноса');
return;
}
otherVariants.forEach(v => {
const opt = document.createElement('option');
opt.value = v.uuid;
opt.textContent = normalizeVariantLabel(v.variant);
select.appendChild(opt);
});
document.getElementById('transfer-config-uuid').value = configUUID;
document.getElementById('transfer-modal').classList.remove('hidden');
document.getElementById('transfer-modal').classList.add('flex');
}
function closeTransferModal() {
document.getElementById('transfer-modal').classList.add('hidden');
document.getElementById('transfer-modal').classList.remove('flex');
}
async function transferConfig() {
const configUUID = document.getElementById('transfer-config-uuid').value;
const targetProjectUUID = document.getElementById('transfer-variant-select').value;
if (!configUUID || !targetProjectUUID) return;
const resp = await fetch('/api/configs/' + configUUID + '/project', {
method: 'PATCH',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({project_uuid: targetProjectUUID})
});
if (!resp.ok) {
alert('Не удалось перенести квоту');
return;
}
closeTransferModal();
loadConfigs();
}
document.getElementById('create-modal').addEventListener('click', function(e) { if (e.target === this) closeCreateModal(); });
document.getElementById('rename-modal').addEventListener('click', function(e) { if (e.target === this) closeRenameModal(); });
document.getElementById('new-variant-modal').addEventListener('click', function(e) { if (e.target === this) closeNewVariantModal(); });
document.getElementById('clone-modal').addEventListener('click', function(e) { if (e.target === this) closeCloneModal(); });
document.getElementById('import-modal').addEventListener('click', function(e) { if (e.target === this) closeImportModal(); });
document.getElementById('project-settings-modal').addEventListener('click', function(e) { if (e.target === this) closeProjectSettingsModal(); });
document.getElementById('transfer-modal').addEventListener('click', function(e) { if (e.target === this) closeTransferModal(); });
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
closeCreateModal();
@@ -770,6 +870,7 @@ document.addEventListener('keydown', function(e) {
closeCloneModal();
closeImportModal();
closeProjectSettingsModal();
closeTransferModal();
}
});
@@ -777,6 +878,7 @@ document.addEventListener('DOMContentLoaded', async function() {
applyStatusModeUI();
const ok = await loadProject();
if (!ok) return;
updateDeleteVariantButton();
await loadConfigs();
});
</script>