|
| 1 | +import { filterUndefined } from '@sim/utils/object' |
| 2 | +import { QUICKBOOKS_MAX_RESPONSE_BYTES } from '@/tools/quickbooks/client' |
| 3 | +import { ErrorExtractorId } from '@/tools/error-extractors' |
| 4 | +import type { |
| 5 | + QuickBooksCreateItemParams, |
| 6 | + QuickBooksItem, |
| 7 | + QuickBooksMutationResponse, |
| 8 | +} from '@/tools/quickbooks/types' |
| 9 | +import { QUICKBOOKS_ITEM_PROPERTIES, QUICKBOOKS_MUTATION_OUTPUTS } from '@/tools/quickbooks/types' |
| 10 | +import { |
| 11 | + buildQuickBooksEntityUrl, |
| 12 | + getQuickBooksToolHeaders, |
| 13 | + optionalQuickBooksString, |
| 14 | + quickBooksReference, |
| 15 | + requiredQuickBooksString, |
| 16 | + transformQuickBooksMutationResponse, |
| 17 | + validateQuickBooksOptionalNumber, |
| 18 | +} from '@/tools/quickbooks/utils' |
| 19 | +import type { ToolConfig } from '@/tools/types' |
| 20 | + |
| 21 | +const QUICKBOOKS_ITEM_TYPES = { |
| 22 | + service: 'Service', |
| 23 | + non_inventory: 'NonInventory', |
| 24 | +} as const |
| 25 | + |
| 26 | +export const quickbooksCreateItemTool: ToolConfig< |
| 27 | + QuickBooksCreateItemParams, |
| 28 | + QuickBooksMutationResponse<QuickBooksItem> |
| 29 | +> = { |
| 30 | + id: 'quickbooks_create_item', |
| 31 | + name: 'QuickBooks Create Item', |
| 32 | + description: 'Create a Service or Non-inventory item in QuickBooks Online', |
| 33 | + version: '1.0.0', |
| 34 | + params: { |
| 35 | + accessToken: { |
| 36 | + type: 'string', |
| 37 | + required: true, |
| 38 | + visibility: 'hidden', |
| 39 | + description: 'QuickBooks OAuth access token', |
| 40 | + }, |
| 41 | + realmId: { |
| 42 | + type: 'string', |
| 43 | + required: true, |
| 44 | + visibility: 'hidden', |
| 45 | + description: 'QuickBooks company ID derived from the connected credential', |
| 46 | + }, |
| 47 | + name: { |
| 48 | + type: 'string', |
| 49 | + required: true, |
| 50 | + visibility: 'user-or-llm', |
| 51 | + description: 'Unique item name', |
| 52 | + }, |
| 53 | + itemType: { |
| 54 | + type: 'string', |
| 55 | + required: true, |
| 56 | + visibility: 'user-or-llm', |
| 57 | + description: 'Writable item type: service or non_inventory', |
| 58 | + }, |
| 59 | + incomeAccountId: { |
| 60 | + type: 'string', |
| 61 | + required: true, |
| 62 | + visibility: 'user-or-llm', |
| 63 | + description: 'Income account ID used when the item is sold', |
| 64 | + }, |
| 65 | + description: { |
| 66 | + type: 'string', |
| 67 | + required: false, |
| 68 | + visibility: 'user-or-llm', |
| 69 | + description: 'Sales description', |
| 70 | + }, |
| 71 | + unitPrice: { |
| 72 | + type: 'number', |
| 73 | + required: false, |
| 74 | + visibility: 'user-or-llm', |
| 75 | + description: 'Sales price per unit', |
| 76 | + }, |
| 77 | + purchaseDescription: { |
| 78 | + type: 'string', |
| 79 | + required: false, |
| 80 | + visibility: 'user-or-llm', |
| 81 | + description: 'Purchase description', |
| 82 | + }, |
| 83 | + purchaseCost: { |
| 84 | + type: 'number', |
| 85 | + required: false, |
| 86 | + visibility: 'user-or-llm', |
| 87 | + description: 'Purchase cost per unit', |
| 88 | + }, |
| 89 | + expenseAccountId: { |
| 90 | + type: 'string', |
| 91 | + required: false, |
| 92 | + visibility: 'user-or-llm', |
| 93 | + description: 'Expense account ID used when the item is purchased', |
| 94 | + }, |
| 95 | + taxable: { |
| 96 | + type: 'boolean', |
| 97 | + required: false, |
| 98 | + visibility: 'user-or-llm', |
| 99 | + description: 'Whether the item is taxable', |
| 100 | + }, |
| 101 | + }, |
| 102 | + oauth: { |
| 103 | + required: true, |
| 104 | + provider: 'quickbooks', |
| 105 | + requiredScopes: ['com.intuit.quickbooks.accounting'], |
| 106 | + }, |
| 107 | + errorExtractor: ErrorExtractorId.QUICKBOOKS_FAULT, |
| 108 | + request: { |
| 109 | + url: (params) => buildQuickBooksEntityUrl(params.realmId, 'item').toString(), |
| 110 | + method: 'POST', |
| 111 | + headers: (params) => getQuickBooksToolHeaders(params.accessToken, 'application/json'), |
| 112 | + body: (params) => { |
| 113 | + const type = QUICKBOOKS_ITEM_TYPES[params.itemType] |
| 114 | + if (!type) throw new Error(`Unsupported writable QuickBooks item type: ${params.itemType}`) |
| 115 | + const purchaseDescription = optionalQuickBooksString(params.purchaseDescription) |
| 116 | + const purchaseCost = validateQuickBooksOptionalNumber(params.purchaseCost, 'purchaseCost') |
| 117 | + if ( |
| 118 | + (purchaseDescription !== undefined || purchaseCost !== undefined) && |
| 119 | + !params.expenseAccountId?.trim() |
| 120 | + ) { |
| 121 | + throw new Error('expenseAccountId is required when purchase fields are supplied') |
| 122 | + } |
| 123 | + return filterUndefined({ |
| 124 | + Name: requiredQuickBooksString(params.name, 'name'), |
| 125 | + Type: type, |
| 126 | + IncomeAccountRef: quickBooksReference(params.incomeAccountId, 'incomeAccountId'), |
| 127 | + Description: optionalQuickBooksString(params.description), |
| 128 | + UnitPrice: validateQuickBooksOptionalNumber(params.unitPrice, 'unitPrice'), |
| 129 | + PurchaseDesc: purchaseDescription, |
| 130 | + PurchaseCost: purchaseCost, |
| 131 | + ExpenseAccountRef: params.expenseAccountId |
| 132 | + ? quickBooksReference(params.expenseAccountId, 'expenseAccountId') |
| 133 | + : undefined, |
| 134 | + Taxable: params.taxable, |
| 135 | + }) |
| 136 | + }, |
| 137 | + retry: { enabled: false }, |
| 138 | + maxResponseBytes: QUICKBOOKS_MAX_RESPONSE_BYTES, |
| 139 | + }, |
| 140 | + transformResponse: (response) => |
| 141 | + transformQuickBooksMutationResponse<QuickBooksItem>(response, 'Item'), |
| 142 | + outputs: { |
| 143 | + record: { |
| 144 | + type: 'json', |
| 145 | + description: 'Created QuickBooks Item record', |
| 146 | + properties: QUICKBOOKS_ITEM_PROPERTIES, |
| 147 | + }, |
| 148 | + ...QUICKBOOKS_MUTATION_OUTPUTS, |
| 149 | + }, |
| 150 | +} |
0 commit comments