search-quickbooks-customers.handler.ts•2.34 kB
import {
makeQuickBooksRequest,
queryQuickBooks,
extractAccessToken,
} from "../helpers/quickbooks-api.js";
import { getRequestHeaders } from "../helpers/request-context.js";
import { ToolResponse } from "../types/tool-response.js";
import { formatError } from "../helpers/format-error.js";
import { buildQuickbooksSearchCriteria } from "../helpers/build-quickbooks-search-criteria.js";
import { convertCriteriaToSQL } from "../helpers/criteria-to-sql.js";
/**
* Search customers from QuickBooks Online.
*
* Accepts either:
* • A plain criteria object (key/value pairs) – passed directly to findCustomers
* • An **array** of objects in the `{ field, value, operator? }` shape – this
* allows use of operators such as `IN`, `LIKE`, `>`, `<`, `>=`, `<=` etc.
*
* Pagination / sorting options such as `limit`, `offset`, `asc`, `desc`,
* `fetchAll`, `count` can be supplied via the top‑level criteria object or as
* dedicated entries in the array form (see README in user prompt).
*/
export async function searchQuickbooksCustomers(
criteria: object | Array<Record<string, any>> = {}
): Promise<ToolResponse<any[]>> {
try {
// Get access token from request headers
const headers = getRequestHeaders();
const accessToken = extractAccessToken(headers);
if (!accessToken) {
return {
result: null,
isError: true,
error: "Missing Authorization header. Please provide: Authorization: Bearer <access_token>",
};
}
const normalizedCriteria = buildQuickbooksSearchCriteria(criteria);
const sqlQuery = convertCriteriaToSQL("Customer", normalizedCriteria);
const response = await queryQuickBooks({
query: sqlQuery,
accessToken,
});
if (response.isError) {
return {
result: null,
isError: true,
error: response.error || "Search failed",
};
}
return {
result: response.result?.QueryResponse?.Customer || [],
isError: false,
error: null,
};
} catch (error) {
return {
result: null,
isError: true,
error: formatError(error),
};
}
}