Reduce cognitive complexity, improve test coverage, fix SonarQube issues
Cognitive complexity refactors: - cart.js: extract buildCart, handleCheckout, updateStockLabel, updateItemStock, startStockPolling (21→~8) - tabs.js: use .at(-1) instead of [length-1] - MeilisearchConsistencyCommand: extract checkAllIndexes, accumulate, reportSummary (18→~8) - TranslateCommand: extract processDomain, processLanguage, loadExisting, findMissingKeys, removeObsoleteKeys, handleUpToDate, mergeAndOrder (36→~10) - AccountController::index: extract computeFinanceStats with statusMap pattern (19→~12) Test coverage additions: - HomeController: expired invitation view, stock not found, stock with billets, search+city with mock results - AdminController: delete/resend invitation not found (404) - AccountController: item without billet (codeCoverageIgnore - NOT NULL in DB) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,100 @@ function formatEur(value) {
|
||||
return value.toFixed(2).replace('.', ',') + ' \u20AC'
|
||||
}
|
||||
|
||||
function buildCart(items) {
|
||||
const cart = []
|
||||
for (const item of items) {
|
||||
const qty = Number.parseInt(item.querySelector('[data-cart-qty]').value, 10) || 0
|
||||
if (qty > 0) {
|
||||
cart.push({ billetId: item.dataset.billetId, qty })
|
||||
}
|
||||
}
|
||||
|
||||
return cart
|
||||
}
|
||||
|
||||
function handleCheckout(checkoutBtn, items, errorEl, errorText) {
|
||||
const cart = buildCart(items)
|
||||
if (cart.length === 0) return
|
||||
|
||||
const orderUrl = checkoutBtn.dataset.orderUrl
|
||||
if (!orderUrl) return
|
||||
|
||||
checkoutBtn.disabled = true
|
||||
checkoutBtn.textContent = 'Chargement...'
|
||||
if (errorEl) errorEl.classList.add('hidden')
|
||||
|
||||
globalThis.fetch(orderUrl, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(cart),
|
||||
})
|
||||
.then(r => {
|
||||
if (!r.ok) throw new Error(r.status)
|
||||
return r.json()
|
||||
})
|
||||
.then(data => {
|
||||
if (data.redirect) {
|
||||
globalThis.location.href = data.redirect
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
checkoutBtn.disabled = false
|
||||
checkoutBtn.textContent = 'Commander'
|
||||
if (errorEl && errorText) {
|
||||
errorText.textContent = 'Une erreur est survenue. Veuillez reessayer.'
|
||||
errorEl.classList.remove('hidden')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function updateStockLabel(label, max) {
|
||||
if (max === 0) {
|
||||
label.innerHTML = '<span class="text-red-600">Rupture de stock</span>'
|
||||
} else if (max <= 10) {
|
||||
label.innerHTML = '<span class="text-orange-500">Plus que ' + max + ' place' + (max > 1 ? 's' : '') + ' !</span>'
|
||||
} else {
|
||||
label.innerHTML = '<span class="text-gray-400">' + max + ' places disponibles</span>'
|
||||
}
|
||||
}
|
||||
|
||||
function updateItemStock(item) {
|
||||
const qtyInput = item.querySelector('[data-cart-qty]')
|
||||
const max = Number.parseInt(item.dataset.max, 10) || 0
|
||||
const current = Number.parseInt(qtyInput.value, 10) || 0
|
||||
|
||||
if (max > 0 && current > max) {
|
||||
qtyInput.value = max
|
||||
}
|
||||
|
||||
const label = item.querySelector('[data-stock-label]')
|
||||
if (label) {
|
||||
updateStockLabel(label, max)
|
||||
if (max === 0 && current > 0) {
|
||||
qtyInput.value = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function startStockPolling(stockUrl, items, updateTotals) {
|
||||
setInterval(() => {
|
||||
globalThis.fetch(stockUrl)
|
||||
.then(r => r.json())
|
||||
.then(stock => {
|
||||
for (const item of items) {
|
||||
const qty = stock[item.dataset.billetId]
|
||||
if (qty === undefined || qty === null) continue
|
||||
|
||||
item.dataset.max = String(qty)
|
||||
item.querySelector('[data-cart-qty]').max = qty
|
||||
updateItemStock(item)
|
||||
}
|
||||
updateTotals()
|
||||
})
|
||||
.catch(() => {})
|
||||
}, 30000)
|
||||
}
|
||||
|
||||
export function initCart() {
|
||||
const billetterie = document.getElementById('billetterie')
|
||||
if (!billetterie) return
|
||||
@@ -63,90 +157,13 @@ export function initCart() {
|
||||
}
|
||||
|
||||
if (checkoutBtn) {
|
||||
checkoutBtn.addEventListener('click', () => {
|
||||
const cart = []
|
||||
for (const item of items) {
|
||||
const qty = Number.parseInt(item.querySelector('[data-cart-qty]').value, 10) || 0
|
||||
if (qty > 0) {
|
||||
cart.push({ billetId: item.dataset.billetId, qty })
|
||||
}
|
||||
}
|
||||
|
||||
if (cart.length === 0) return
|
||||
|
||||
const orderUrl = checkoutBtn.dataset.orderUrl
|
||||
if (!orderUrl) return
|
||||
|
||||
checkoutBtn.disabled = true
|
||||
checkoutBtn.textContent = 'Chargement...'
|
||||
if (errorEl) errorEl.classList.add('hidden')
|
||||
|
||||
globalThis.fetch(orderUrl, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(cart),
|
||||
})
|
||||
.then(r => {
|
||||
if (!r.ok) throw new Error(r.status)
|
||||
return r.json()
|
||||
})
|
||||
.then(data => {
|
||||
if (data.redirect) {
|
||||
globalThis.location.href = data.redirect
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
checkoutBtn.disabled = false
|
||||
checkoutBtn.textContent = 'Commander'
|
||||
if (errorEl && errorText) {
|
||||
errorText.textContent = 'Une erreur est survenue. Veuillez reessayer.'
|
||||
errorEl.classList.remove('hidden')
|
||||
}
|
||||
})
|
||||
})
|
||||
checkoutBtn.addEventListener('click', () => handleCheckout(checkoutBtn, items, errorEl, errorText))
|
||||
}
|
||||
|
||||
updateTotals()
|
||||
|
||||
const stockUrl = billetterie.dataset.stockUrl
|
||||
if (stockUrl) {
|
||||
setInterval(() => {
|
||||
globalThis.fetch(stockUrl)
|
||||
.then(r => r.json())
|
||||
.then(stock => {
|
||||
for (const item of items) {
|
||||
const billetId = item.dataset.billetId
|
||||
const qty = stock[billetId]
|
||||
if (qty === undefined || qty === null) continue
|
||||
|
||||
const max = qty
|
||||
item.dataset.max = String(max)
|
||||
|
||||
const qtyInput = item.querySelector('[data-cart-qty]')
|
||||
qtyInput.max = max
|
||||
|
||||
const current = Number.parseInt(qtyInput.value, 10) || 0
|
||||
if (max > 0 && current > max) {
|
||||
qtyInput.value = max
|
||||
}
|
||||
|
||||
const label = item.querySelector('[data-stock-label]')
|
||||
if (label) {
|
||||
if (max === 0) {
|
||||
label.innerHTML = '<span class="text-red-600">Rupture de stock</span>'
|
||||
if (current > 0) {
|
||||
qtyInput.value = 0
|
||||
}
|
||||
} else if (max <= 10) {
|
||||
label.innerHTML = '<span class="text-orange-500">Plus que ' + max + ' place' + (max > 1 ? 's' : '') + ' !</span>'
|
||||
} else {
|
||||
label.innerHTML = '<span class="text-gray-400">' + max + ' places disponibles</span>'
|
||||
}
|
||||
}
|
||||
}
|
||||
updateTotals()
|
||||
})
|
||||
.catch(() => {})
|
||||
}, 30000)
|
||||
startStockPolling(stockUrl, items, updateTotals)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ export function initTabs() {
|
||||
target = tabs[0]
|
||||
} else if (e.key === 'End') {
|
||||
e.preventDefault()
|
||||
target = tabs[tabs.length - 1]
|
||||
target = tabs.at(-1)
|
||||
}
|
||||
|
||||
if (target) {
|
||||
|
||||
Reference in New Issue
Block a user