diff --git a/qbo_query.js b/qbo_query.js new file mode 100644 index 0000000..06c3fc3 --- /dev/null +++ b/qbo_query.js @@ -0,0 +1,49 @@ +#!/usr/bin/env node +// qbo_query.js — Quick QBO Query Tool +// +// Usage: +// node qbo_query.js invoice 110444 # By DocNumber +// node qbo_query.js invoice-id 37973 # By QBO ID +// node qbo_query.js payment 38733 # By Payment ID +// node qbo_query.js query "SELECT * FROM Invoice WHERE Balance = '0' MAXRESULTS 3" + +require('dotenv').config(); +const { makeQboApiCall, getOAuthClient } = require('./qbo_helper'); + +async function main() { + const [type, value] = process.argv.slice(2); + if (!type || !value) { + console.log('Usage:\n node qbo_query.js invoice \n node qbo_query.js invoice-id \n node qbo_query.js payment \n node qbo_query.js query ""'); + process.exit(1); + } + + const oauthClient = getOAuthClient(); + const companyId = oauthClient.getToken().realmId; + const base = process.env.QBO_ENVIRONMENT === 'production' + ? 'https://quickbooks.api.intuit.com' + : 'https://sandbox-quickbooks.api.intuit.com'; + + let url; + if (type === 'invoice') { + url = `${base}/v3/company/${companyId}/query?query=${encodeURI(`SELECT * FROM Invoice WHERE DocNumber = '${value}'`)}`; + } else if (type === 'invoice-id') { + url = `${base}/v3/company/${companyId}/invoice/${value}`; + } else if (type === 'payment') { + url = `${base}/v3/company/${companyId}/payment/${value}`; + } else if (type === 'query') { + url = `${base}/v3/company/${companyId}/query?query=${encodeURI(value)}`; + } else { + console.error('Unknown type:', type); + process.exit(1); + } + + try { + const response = await makeQboApiCall({ url, method: 'GET' }); + const data = response.getJson ? response.getJson() : response.json; + console.log(JSON.stringify(data, null, 2)); + } catch (e) { + console.error('Error:', e.message); + } +} + +main(); \ No newline at end of file