💡 Ejemplo completo
- JS (Fetch)
Obtención de operaciones (ejemplo)
const API_URL = 'http://api.meridianosoft.com.ar/api/v1/combined_updates';
const API_CHECK_AS_PROCESSED_URL = 'http://api.meridianosoft.com.ar/api/v1/combined_updates/mark_as_processed';
const API_TOKEN = 'su-token-privado';
async function RetrieveAppOperationsWithFetch(){
const headers = new Headers();
headers.append("Authorization", `Bearer ${API_TOKEN}`);
const requestOptions = {
method: "GET",
headers: headers,
redirect: "follow"
};
// Iniciar transacción (SQL)
// ...
try {
const response = await fetch(API_URL, requestOptions);
const result = await response.json();
const request_uuid = response.headers.get('x-request-uuid');
if(response.ok === false){
throw new Error(result.message);
}
const {
new_clients,
orders,
orders_items,
payment_collections,
payment_collections_debts,
payment_collections_methods
} = result;
if(new_clients.length === 0 && orders.length === 0 && payment_collections.length === 0){
console.log('No hay nuevas operaciones.');
return;
}
// Procesar operaciones
processNewClients(new_clients);
processOrders(orders, orders_items);
processPaymentCollections(payment_collections, payment_collections_debts, payment_collections_methods);
// Marcar como procesado
await markAsProcessed(request_uuid);
// Implementar commit (SQL)
// ...
console.log('¡Operaciones importadas con éxito!');
} catch (error) {
console.error('Error al obtener operaciones:', error.message);
// Implementar rollback (SQL)
// ...
}
}
// Actualiza el estado de las operaciones obtenidas a: "Procesado"
async function markAsProcessed(request_uuid) {
if (!request_uuid) return;
const headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append("Authorization", `Bearer ${API_TOKEN}`);
const body = JSON.stringify({
request_uuid: request_uuid,
});
const requestOptions = {
method: "POST",
headers: headers,
body: body,
redirect: "follow",
};
const response = await fetch(API_CHECK_AS_PROCESSED_URL, requestOptions);
const result = await response.json();
if (response.ok === false) {
throw new Error(result.message);
}
}
function processNewClients(new_clients) {
if (new_clients.length === 0) {
return;
}
new_clients.forEach((new_client) => {
const {
new_client_id, // Integer
created_by_sales_agent_id, // Integer
first_name, // String
last_name, // String
legal_name, // String
tax_condition_code, // String
is_a_legal_person, // Boolean
identification, // String
state_id, // Integer
state, // String
locality_id, // Integer
locality, // String
postal_code, // String
street_address, // String
street_number, // String
address, // String
phone_country_code, // String
phone_prefix, // String
phone_line_number, // String
phone, // String
first_opening_time, // String
first_closing_time, // String
second_opening_time, // String
second_closing_time, // String
email, // String
discount, // Number
sales_agent_id, // Integer
price_list_id, // Integer
payment_term_id, // Integer (Nullable)
zone_id, // Integer (Nullable)
observations, // String
created_at, // String (ISO 8601)
} = new_client;
// Procesar nuevo cliente
// ...
console.log("Nuevo cliente: " + legal_name);
});
}
function processOrders(orders, orders_items) {
if (orders.length === 0) {
return;
}
orders.forEach((order) => {
const {
order_id, // Integer
client_id, // Integer
is_a_new_client, // Boolean
sales_agent_id, // Integer
deadline, // String (Nullable, ISO 8601)
final_price_with_iva, // Number
final_price_without_iva, // Number
items_total_discounts_with_iva, // Number
items_total_discounts_without_iva, // Number
global_adjustment_type, // String
global_adjustment_value, // Number
global_adjustment_total_with_iva, // Number
global_adjustment_total_without_iva, // Number
payment_term_id, // Integer (Nullable)
payment_term_adjustment_type, // String
payment_term_adjustment_value, // Number
payment_term_adjustment_total_with_iva, // Number
payment_term_adjustment_total_without_iva, // Number
transaction_type, // String (Nullable)
observations, // String
created_at, // String (ISO 8601)
} = order;
// *!!*
// Tener en cuenta si se trata de un nuevo cliente (is_a_new_client === true)
// En tal caso, client_id corresponderá al ID del nuevo cliente (new_client_id, en tabla NewClients)
// Procesar pedido
// ...
console.log(`Nuevo pedido: [${order_id}] $${final_price_with_iva}`);
// Procesar items del pedido
orders_items
.filter((order_item) => order_item.order_id === order_id)
.forEach((order_item) => {
const {
order_item_id, // Integer
order_id, // Integer
product_id, // Integer
quantity, // Number
discount, // Number
price_list_id, // Integer
price_multiplier, // Number
unit_price_with_iva, // Number
unit_price_without_iva, // Number
final_price_with_iva, // Number
final_price_without_iva, // Number
observations, // String
} = order_item;
// Procesar item
// ...
});
});
}
function processPaymentCollections(
payment_collections,
payment_collections_debts,
payment_collections_methods
) {
if (payment_collections.length === 0) {
return;
}
payment_collections.forEach((payment_collection) => {
const {
payment_collection_id, // Integer
client_id, // Integer
payment_collections_agent_id, // Integer
debts_total_amount, // Number
methods_total_amount, // Number
observations, // String
created_at, // String (ISO 8601)
} = payment_collection;
// Procesar cobranza
// ...
console.log(`Nueva cobranza: [${payment_collection_id}] $${methods_total_amount}`);
// Procesar comprobantes
payment_collections_debts
.filter((debt) => debt.payment_collection_id === payment_collection_id)
.forEach((debt) => {
const {
payment_collection_debt_id, // Integer
payment_collection_id, // Integer
client_debt_id, // Integer
invoice_code, // String
paid_amount, // Number
unpaid_amount, // Number
} = debt;
// Procesar comprobante
// ...
});
// Procesar pagos
payment_collections_methods
.filter((method) => method.payment_collection_id === payment_collection_id)
.forEach((method) => {
const {
payment_collection_method_id, // Integer
payment_collection_id, // Integer
payment_method_code, // String
bank_id, // Integer (Nullable)
bank_account_id, // Integer (Nullable)
check_issuance_date, // String (Nullable, ISO 8601)
check_expiration_date, // String (Nullable, ISO 8601)
identification, // String
wire_date, // String (Nullable, ISO 8601)
description, // String
amount, // Number
observations, // String
} = method;
// Procesar pago
// ...
});
});
}