refactoring 1. step
This commit is contained in:
117
public/js/utils/api.js
Normal file
117
public/js/utils/api.js
Normal file
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* API Utility
|
||||
* Centralized API calls for the frontend
|
||||
*/
|
||||
|
||||
const API = {
|
||||
// Customer API
|
||||
customers: {
|
||||
getAll: () => fetch('/api/customers').then(r => r.json()),
|
||||
get: (id) => fetch(`/api/customers/${id}`).then(r => r.json()),
|
||||
create: (data) => fetch('/api/customers', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
}).then(r => r.json()),
|
||||
update: (id, data) => fetch(`/api/customers/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
}).then(r => r.json()),
|
||||
delete: (id) => fetch(`/api/customers/${id}`, { method: 'DELETE' }).then(r => r.json()),
|
||||
exportToQbo: (id) => fetch(`/api/customers/${id}/export-qbo`, { method: 'POST' }).then(r => r.json())
|
||||
},
|
||||
|
||||
// Quote API
|
||||
quotes: {
|
||||
getAll: () => fetch('/api/quotes').then(r => r.json()),
|
||||
get: (id) => fetch(`/api/quotes/${id}`).then(r => r.json()),
|
||||
create: (data) => fetch('/api/quotes', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
}).then(r => r.json()),
|
||||
update: (id, data) => fetch(`/api/quotes/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
}).then(r => r.json()),
|
||||
delete: (id) => fetch(`/api/quotes/${id}`, { method: 'DELETE' }).then(r => r.json()),
|
||||
convertToInvoice: (id) => fetch(`/api/quotes/${id}/convert-to-invoice`, { method: 'POST' }).then(r => r.json()),
|
||||
getPdf: (id) => window.open(`/api/quotes/${id}/pdf`, '_blank'),
|
||||
getHtml: (id) => window.open(`/api/quotes/${id}/html`, '_blank')
|
||||
},
|
||||
|
||||
// Invoice API
|
||||
invoices: {
|
||||
getAll: () => fetch('/api/invoices').then(r => r.json()),
|
||||
get: (id) => fetch(`/api/invoices/${id}`).then(r => r.json()),
|
||||
getNextNumber: () => fetch('/api/invoices/next-number').then(r => r.json()),
|
||||
create: (data) => fetch('/api/invoices', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
}).then(r => r.json()),
|
||||
update: (id, data) => fetch(`/api/invoices/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
}).then(r => r.json()),
|
||||
delete: (id) => fetch(`/api/invoices/${id}`, { method: 'DELETE' }).then(r => r.json()),
|
||||
exportToQbo: (id) => fetch(`/api/invoices/${id}/export`, { method: 'POST' }).then(r => r.json()),
|
||||
updateQbo: (id) => fetch(`/api/invoices/${id}/update-qbo`, { method: 'POST' }).then(r => r.json()),
|
||||
markPaid: (id, paidDate) => fetch(`/api/invoices/${id}/mark-paid`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ paid_date: paidDate })
|
||||
}).then(r => r.json()),
|
||||
markUnpaid: (id) => fetch(`/api/invoices/${id}/mark-unpaid`, { method: 'PATCH' }).then(r => r.json()),
|
||||
resetQbo: (id) => fetch(`/api/invoices/${id}/reset-qbo`, { method: 'PATCH' }).then(r => r.json()),
|
||||
setEmailStatus: (id, status) => fetch(`/api/invoices/${id}/email-status`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ status })
|
||||
}).then(r => r.json()),
|
||||
getPdf: (id) => window.open(`/api/invoices/${id}/pdf`, '_blank'),
|
||||
getHtml: (id) => window.open(`/api/invoices/${id}/html`, '_blank')
|
||||
},
|
||||
|
||||
// Payment API
|
||||
payments: {
|
||||
getAll: () => fetch('/api/payments').then(r => r.json()),
|
||||
record: (data) => fetch('/api/qbo/record-payment', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(data)
|
||||
}).then(r => r.json())
|
||||
},
|
||||
|
||||
// QBO API
|
||||
qbo: {
|
||||
getStatus: () => fetch('/api/qbo/status').then(r => r.json()),
|
||||
getAccounts: () => fetch('/api/qbo/accounts').then(r => r.json()),
|
||||
getPaymentMethods: () => fetch('/api/qbo/payment-methods').then(r => r.json()),
|
||||
getLaborRate: () => fetch('/api/qbo/labor-rate').then(r => r.json()),
|
||||
getLastSync: () => fetch('/api/qbo/last-sync').then(r => r.json()),
|
||||
getOverdue: () => fetch('/api/qbo/overdue').then(r => r.json()),
|
||||
importUnpaid: () => fetch('/api/qbo/import-unpaid', { method: 'POST' }).then(r => r.json()),
|
||||
syncPayments: () => fetch('/api/qbo/sync-payments', { method: 'POST' }).then(r => r.json()),
|
||||
auth: () => window.location.href = '/auth/qbo'
|
||||
},
|
||||
|
||||
// Settings API
|
||||
settings: {
|
||||
getLogo: () => fetch('/api/logo-info').then(r => r.json()),
|
||||
uploadLogo: (file) => {
|
||||
const formData = new FormData();
|
||||
formData.append('logo', file);
|
||||
return fetch('/api/upload-logo', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
}).then(r => r.json());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Make globally available
|
||||
window.API = API;
|
||||
Reference in New Issue
Block a user