102 lines
2.6 KiB
JavaScript
102 lines
2.6 KiB
JavaScript
class DjangoApi {
|
|
constructor() {
|
|
this.base = 'http://localhost:7000';
|
|
}
|
|
|
|
getCustomers() {
|
|
const url = this.base + '/don_confiao/api/customers/';
|
|
return this.getRequest(url);
|
|
}
|
|
|
|
getProducts() {
|
|
const url = this.base + '/don_confiao/api/products/';
|
|
return this.getRequest(url);
|
|
}
|
|
|
|
getPaymentMethods() {
|
|
const url = this.base + '/don_confiao/payment_methods/all/select_format';
|
|
return this.getRequest(url);
|
|
}
|
|
|
|
getSummaryPurchase(purchaseId) {
|
|
const url = this.base + `/don_confiao/resumen_compra_json/${purchaseId}`;
|
|
return this.getRequest(url);
|
|
}
|
|
|
|
getPurchasesForReconciliation() {
|
|
const url = this.base + '/don_confiao/purchases/for_reconciliation';
|
|
return this.getRequest(url);
|
|
}
|
|
|
|
getListReconcliations(page, itemsPerPage) {
|
|
const url = this.base + `/don_confiao/api/reconciliate_jar/?page=${page}&page_size=${itemsPerPage}`;
|
|
return this.getRequest(url);
|
|
}
|
|
|
|
getReconciliation(reconciliationId) {
|
|
const url = this.base + `/don_confiao/api/reconciliate_jar/${reconciliationId}/`;
|
|
return this.getRequest(url);
|
|
}
|
|
|
|
isValidAdminCode(code) {
|
|
const url = this.base + `/don_confiao/api/admin_code/validate/${code}`
|
|
return this.getRequest(url)
|
|
}
|
|
|
|
createPurchase(purchase) {
|
|
const url = this.base + '/don_confiao/api/sales/';
|
|
return this.postRequest(url, purchase);
|
|
}
|
|
|
|
createReconciliationJar(reconciliation) {
|
|
const url = this.base + '/don_confiao/reconciliate_jar';
|
|
return this.postRequest(url, reconciliation);
|
|
}
|
|
|
|
createCustomer(customer) {
|
|
const url = this.base + '/don_confiao/api/customers/';
|
|
return this.postRequest(url, customer);
|
|
}
|
|
|
|
getRequest(url) {
|
|
return new Promise ((resolve, reject) => {
|
|
fetch(url)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
resolve(data);
|
|
})
|
|
.catch(error => {
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
|
|
postRequest(url, content) {
|
|
return new Promise((resolve, reject) => {
|
|
fetch(url, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(content)
|
|
})
|
|
.then(response => {
|
|
if (!response.ok) {
|
|
reject(new Error(`Error ${response.status}: ${response.statusText}`));
|
|
} else {
|
|
response.json().then(data => {
|
|
if (!data) {
|
|
reject(new Error('La respuesta no es un JSON válido'));
|
|
} else {
|
|
resolve(data);
|
|
}
|
|
});
|
|
}
|
|
})
|
|
.catch(error => reject(error));
|
|
});
|
|
}
|
|
}
|
|
|
|
export default DjangoApi;
|