migrate/resources/js/customers.js

116 lines
4.2 KiB
JavaScript

// Customers page JavaScript
let routes = {};
let csrfToken = '';
// Initialize on page load
document.addEventListener('DOMContentLoaded', function() {
if (window.customerRoutes) {
routes = window.customerRoutes;
csrfToken = document.querySelector('meta[name="csrf-token"]')?.content || '';
}
});
function startCustomerMigration(dryRun) {
const button = dryRun ? document.getElementById('dryRunCustomerMigrationBtn') : document.getElementById('startCustomerMigrationBtn');
const otherButton = dryRun ? document.getElementById('startCustomerMigrationBtn') : document.getElementById('dryRunCustomerMigrationBtn');
const originalText = button.textContent;
button.disabled = true;
otherButton.disabled = true;
button.textContent = dryRun ? 'Running Dry Run...' : 'Migrating...';
button.style.cursor = 'not-allowed';
const logContent = document.getElementById('customerMigrationLogContent');
logContent.innerHTML = '<div class="log-entry">' + (dryRun ? 'Running dry run...' : 'Starting migration...') + '</div>';
fetch(routes.migrateCustomers, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': csrfToken
},
body: JSON.stringify({ dry_run: dryRun })
})
.then(response => response.json())
.then(data => {
button.disabled = false;
otherButton.disabled = false;
button.textContent = originalText;
button.style.cursor = 'pointer';
if (data.success) {
const successEntry = document.createElement('div');
successEntry.className = 'log-entry success';
successEntry.textContent = `${dryRun ? 'Dry run' : 'Migration'} completed! Added: ${data.added || 0}, Updated: ${data.updated || 0}, Errors: ${data.errors || 0}`;
logContent.appendChild(successEntry);
if (data.log && data.log.length > 0) {
data.log.forEach(log => {
const entry = document.createElement('div');
entry.className = 'log-entry ' + (log.includes('ERROR') ? 'error' : 'success');
entry.textContent = log;
logContent.appendChild(entry);
});
}
const logContainer = document.getElementById('customerMigrationLogContainer');
logContainer.scrollTop = logContainer.scrollHeight;
} else {
const errorEntry = document.createElement('div');
errorEntry.className = 'log-entry error';
errorEntry.textContent = '✗ ' + (dryRun ? 'Dry run' : 'Migration') + ' failed: ' + (data.message || 'Unknown error');
logContent.appendChild(errorEntry);
}
})
.catch(error => {
button.disabled = false;
otherButton.disabled = false;
button.textContent = originalText;
button.style.cursor = 'pointer';
const errorEntry = document.createElement('div');
errorEntry.className = 'log-entry error';
errorEntry.textContent = '✗ Error: ' + error.message;
logContent.appendChild(errorEntry);
});
}
function deleteM2Customer(customerId, email, firstname, lastname) {
const customerName = firstname !== 'N/A' && lastname !== 'N/A'
? `${firstname} ${lastname}`
: email !== 'N/A'
? email
: `ID ${customerId}`;
if (!confirm(`Are you sure you want to delete customer "${customerName}" (Email: ${email})?`)) {
return;
}
const url = routes.deleteCustomer.replace(':id', customerId);
fetch(url, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': csrfToken
}
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('Customer deleted successfully!');
location.reload();
} else {
alert('Error: ' + (data.message || 'Failed to delete customer'));
}
})
.catch(error => {
alert('Error: ' + error.message);
});
}
// Make functions available globally
window.startCustomerMigration = startCustomerMigration;
window.deleteM2Customer = deleteM2Customer;