|
| 1 | +import { createLogger } from '@sim/logger' |
| 2 | +import type { |
| 3 | + HubSpotCreateLineItemParams, |
| 4 | + HubSpotCreateLineItemResponse, |
| 5 | +} from '@/tools/hubspot/types' |
| 6 | +import { LINE_ITEM_OBJECT_OUTPUT } from '@/tools/hubspot/types' |
| 7 | +import type { ToolConfig } from '@/tools/types' |
| 8 | + |
| 9 | +const logger = createLogger('HubSpotCreateLineItem') |
| 10 | + |
| 11 | +export const hubspotCreateLineItemTool: ToolConfig< |
| 12 | + HubSpotCreateLineItemParams, |
| 13 | + HubSpotCreateLineItemResponse |
| 14 | +> = { |
| 15 | + id: 'hubspot_create_line_item', |
| 16 | + name: 'Create Line Item in HubSpot', |
| 17 | + description: 'Create a new line item in HubSpot. Requires at least a name property', |
| 18 | + version: '1.0.0', |
| 19 | + |
| 20 | + oauth: { |
| 21 | + required: true, |
| 22 | + provider: 'hubspot', |
| 23 | + }, |
| 24 | + |
| 25 | + params: { |
| 26 | + accessToken: { |
| 27 | + type: 'string', |
| 28 | + required: true, |
| 29 | + visibility: 'hidden', |
| 30 | + description: 'The access token for the HubSpot API', |
| 31 | + }, |
| 32 | + properties: { |
| 33 | + type: 'object', |
| 34 | + required: true, |
| 35 | + visibility: 'user-or-llm', |
| 36 | + description: |
| 37 | + 'Line item properties as JSON object (e.g., {"name": "Product A", "quantity": "2", "price": "50.00", "hs_sku": "SKU-001"})', |
| 38 | + }, |
| 39 | + associations: { |
| 40 | + type: 'array', |
| 41 | + required: false, |
| 42 | + visibility: 'user-or-llm', |
| 43 | + description: |
| 44 | + 'Array of associations to create with the line item as JSON. Each object should have "to.id" and "types" array with "associationCategory" and "associationTypeId"', |
| 45 | + }, |
| 46 | + }, |
| 47 | + |
| 48 | + request: { |
| 49 | + url: () => 'https://api.hubapi.com/crm/v3/objects/line_items', |
| 50 | + method: 'POST', |
| 51 | + headers: (params) => { |
| 52 | + if (!params.accessToken) { |
| 53 | + throw new Error('Access token is required') |
| 54 | + } |
| 55 | + return { |
| 56 | + Authorization: `Bearer ${params.accessToken}`, |
| 57 | + 'Content-Type': 'application/json', |
| 58 | + } |
| 59 | + }, |
| 60 | + body: (params) => { |
| 61 | + let properties = params.properties |
| 62 | + if (typeof properties === 'string') { |
| 63 | + try { |
| 64 | + properties = JSON.parse(properties) |
| 65 | + } catch (e) { |
| 66 | + throw new Error('Invalid JSON format for properties. Please provide a valid JSON object.') |
| 67 | + } |
| 68 | + } |
| 69 | + const body: Record<string, unknown> = { properties } |
| 70 | + if (params.associations && params.associations.length > 0) { |
| 71 | + body.associations = params.associations |
| 72 | + } |
| 73 | + return body |
| 74 | + }, |
| 75 | + }, |
| 76 | + |
| 77 | + transformResponse: async (response: Response) => { |
| 78 | + const data = await response.json() |
| 79 | + if (!response.ok) { |
| 80 | + logger.error('HubSpot API request failed', { data, status: response.status }) |
| 81 | + throw new Error(data.message || 'Failed to create line item in HubSpot') |
| 82 | + } |
| 83 | + return { |
| 84 | + success: true, |
| 85 | + output: { lineItem: data, lineItemId: data.id, success: true }, |
| 86 | + } |
| 87 | + }, |
| 88 | + |
| 89 | + outputs: { |
| 90 | + lineItem: LINE_ITEM_OBJECT_OUTPUT, |
| 91 | + lineItemId: { type: 'string', description: 'The created line item ID' }, |
| 92 | + success: { type: 'boolean', description: 'Operation success status' }, |
| 93 | + }, |
| 94 | +} |
0 commit comments