quickbooks-api
Version:
A modular TypeScript SDK for seamless integration with Intuit QuickBooks APIs. Provides robust authentication handling and future-ready foundation for accounting, payments, and commerce operations.
38 lines (37 loc) • 1.32 kB
JavaScript
// Import the Query Builder
import { plainToClass } from 'class-transformer';
import { Invoice } from '../../../../types/types.js';
/**
* Get Invoice by ID
* @param this - The Invoice API
* @param id - The ID of the invoice
* @returns The Invoice
*/
export async function getInvoiceById(id, options = {}) {
// Get the Query Builder
const queryBuilder = await this.getQueryBuilder();
// Setup ID Filter
queryBuilder.whereId(id);
// Filter by Status (if provided)
if (options.status)
queryBuilder.whereStatus(options.status);
// Setup the Search Options (if provided)
if (options.searchOptions)
queryBuilder.setSearchOptions(options.searchOptions);
// Setup the URL
const url = queryBuilder.build();
// Get the Invoice
const response = await this.apiClient.runRequest(url, { method: 'GET' });
// Check if the Response Failed to find an Invoice
if (!response)
return null;
// Format the Response
const invoices = this.formatResponse(response);
// Convert the Invoice to a Class
const invoice = invoices[0] ? plainToClass(Invoice, invoices[0]) : null;
// Check if the Invoice is valid and set the API Client
if (invoice)
invoice.setApiClient(this.apiClient);
// Return the Invoice
return invoice;
}