fix(mappings): resolve 409 error on multi-CODMAT edit and make SKU editable
Batch create after soft-delete was rejected because create_mapping() treated soft-deleted records as conflicts. Added auto_restore param that restores+updates instead of 409 when called from edit flow. Also removed readOnly on SKU input in edit modal. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -280,7 +280,7 @@ async function openEditModal(sku, codmat, cantitate, procent) {
|
||||
editingMapping = { sku, codmat };
|
||||
document.getElementById('addModalTitle').textContent = 'Editare Mapare';
|
||||
document.getElementById('inputSku').value = sku;
|
||||
document.getElementById('inputSku').readOnly = true;
|
||||
document.getElementById('inputSku').readOnly = false;
|
||||
document.getElementById('pctWarning').style.display = 'none';
|
||||
|
||||
const container = document.getElementById('codmatLines');
|
||||
@@ -292,6 +292,15 @@ async function openEditModal(sku, codmat, cantitate, procent) {
|
||||
const data = await res.json();
|
||||
const allMappings = (data.mappings || []).filter(m => m.sku === sku && !m.sters);
|
||||
|
||||
// Show product name if available
|
||||
const productName = allMappings[0]?.product_name || '';
|
||||
const productNameEl = document.getElementById('addModalProductName');
|
||||
const productNameText = document.getElementById('inputProductName');
|
||||
if (productName && productNameEl && productNameText) {
|
||||
productNameText.textContent = productName;
|
||||
productNameEl.style.display = '';
|
||||
}
|
||||
|
||||
if (allMappings.length === 0) {
|
||||
// Fallback to single line with passed values
|
||||
addCodmatLine();
|
||||
@@ -307,6 +316,9 @@ async function openEditModal(sku, codmat, cantitate, procent) {
|
||||
const lines = container.querySelectorAll('.codmat-line');
|
||||
const line = lines[lines.length - 1];
|
||||
line.querySelector('.cl-codmat').value = m.codmat;
|
||||
if (m.denumire) {
|
||||
line.querySelector('.cl-selected').textContent = m.denumire;
|
||||
}
|
||||
line.querySelector('.cl-cantitate').value = m.cantitate_roa;
|
||||
line.querySelector('.cl-procent').value = m.procent_pret;
|
||||
}
|
||||
@@ -436,22 +448,23 @@ async function saveMapping() {
|
||||
});
|
||||
} else {
|
||||
// Multi-CODMAT set: delete all existing then create new batch
|
||||
const existRes = await fetch(`/api/mappings?search=${encodeURIComponent(editingMapping.sku)}&per_page=100`);
|
||||
const oldSku = editingMapping.sku;
|
||||
const existRes = await fetch(`/api/mappings?search=${encodeURIComponent(oldSku)}&per_page=100`);
|
||||
const existData = await existRes.json();
|
||||
const existing = (existData.mappings || []).filter(m => m.sku === editingMapping.sku && !m.sters);
|
||||
const existing = (existData.mappings || []).filter(m => m.sku === oldSku && !m.sters);
|
||||
|
||||
// Delete each existing CODMAT
|
||||
// Delete each existing CODMAT for old SKU
|
||||
for (const m of existing) {
|
||||
await fetch(`/api/mappings/${encodeURIComponent(m.sku)}/${encodeURIComponent(m.codmat)}`, {
|
||||
method: 'DELETE'
|
||||
});
|
||||
}
|
||||
|
||||
// Create new batch
|
||||
// Create new batch with auto_restore (handles just-soft-deleted records)
|
||||
res = await fetch('/api/mappings/batch', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ sku, mappings })
|
||||
body: JSON.stringify({ sku, mappings, auto_restore: true })
|
||||
});
|
||||
}
|
||||
} else if (mappings.length === 1) {
|
||||
|
||||
Reference in New Issue
Block a user