Hi,
I am trying to retrieve a list of invoices from the Factomos API, using a pagination of 50 records per GET requets, and requesting 5 pages.
But while the logs show that all 5 GET request have returned 50 records each, so 250 records in total, Coda displays only 140 records in the resulting synced table.
Can anyody help?
My code :
// Schema for an Invoice
const InvoiceSchema = coda.makeObjectSchema({
properties: {
invoice_formated_number: { type: coda.ValueType.String },
contact_pid: { type: coda.ValueType.String },
client_company_name: { type: coda.ValueType.String },
invoice_type: { type: coda.ValueType.String },
invoice_validated: { type: coda.ValueType.String },
invoice_status: { type: coda.ValueType.String },
},
displayProperty: "invoice_formated_number", // Display the formatted invoice number
idProperty: "contact_pid", // Use contact_pid as a unique identifier
});
pack.addSyncTable({
name: "Invoices",
identityName: "Invoice",
schema: InvoiceSchema,
connectionRequirement: coda.ConnectionRequirement.None,
formula: {
name: "SyncInvoices",
description: "Syncs the invoices.",
parameters: [],
execute: async function (params, context) {
const totalPages = 6; // We want the first 5 pages of invoices
// Create an array of fetch requests for each page
let fetchPromises = [];
for (let page = 1; page <= totalPages; page++) {
const url = `https://${myDomain}/invoices?page=${page}`;
fetchPromises.push(
context.fetcher.fetch({
method: "GET",
url: url,
headers: {
"Content-Type": "application/json",
"Accept": "application/vnd.status+json; version=1",
"Authorization": `Bearer ${token}`,
},
})
);
}
// Wait for all requests to complete
let responses = await Promise.all(fetchPromises);
// Check each response and concatenate the results
let allInvoices = [];
for (let i = 0; i < responses.length; i++) {
if (!responses[i].body || !Array.isArray(responses[i].body.results)) {
throw new Error(`Invalid response from the Factomos API on page ${i + 1}`);
}
allInvoices = allInvoices.concat(responses[i].body.results);
}
// Map the results to the desired format
let result = allInvoices.map(invoice => ({
invoice_formated_number: invoice.invoice_formated_number,
contact_pid: invoice.contact_pid,
client_company_name: invoice.client_company_name,
invoice_type: invoice.invoice_type,
invoice_validated: invoice.invoice_validated,
invoice_status: invoice.invoice_status,
}));
return {
result: result,
};
},
},
});