Edits history of script submission #14958 for ' Create direct cost (codat)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Codat = {
    	encodedKey: string
    }
    /**
     * Create direct cost
     * The *Create direct cost* endpoint creates a new [direct cost](https://docs.codat.io/lending-api#/schemas/DirectCost) for a given company's connection.
    
    [Direct costs](https://docs.codat.io/lending-api#/schemas/DirectCost) are business expenses that don't impact Accounts Payable.
    
    **Integration-specific behaviour**
    
    Required data may vary by integration. To see what data to post, first call [Get create direct cost model](https://docs.codat.io/lending-api#/operations/get-create-directCosts-model).
     */
    export async function main(
    	auth: Codat,
    	companyId: string,
    	connectionId: string,
    	timeoutInMinutes: string | undefined,
    	allowSyncOnPushComplete: string | undefined,
    	body: {
    		reference?: string
    		note?: string
    		contactRef?: { id: string; dataType?: 'customers' | 'suppliers' }
    		issueDate: string
    		currency: string
    		currencyRate?: number
    		lineItems: {
    			description?: string
    			unitAmount: number
    			quantity: number
    			discountAmount?: number
    			discountPercentage?: number
    			subTotal?: number
    			taxAmount?: number
    			totalAmount?: number
    			accountRef?: { id?: string; name?: string }
    			taxRateRef?: { id?: string; name?: string; effectiveTaxRate?: number }
    			itemRef?: { id: string; name?: string }
    			trackingCategoryRefs?: { id: string; name?: string }[]
    			tracking?: {
    				recordRefs: {
    					id?: string
    					dataType?: 'customers' | 'suppliers' | 'trackingCategories'
    				}[]
    				invoiceTo?: { id?: string; dataType?: string }
    			}
    		}[]
    		paymentAllocations: {
    			payment: {
    				id?: string
    				note?: string
    				reference?: string
    				accountRef?: { id?: string; name?: string }
    				currency?: string
    				currencyRate?: number
    				paidOnDate?: string
    				totalAmount?: number
    			}
    			allocation: {
    				currency?: string
    				currencyRate?: number
    				allocatedOnDate?: string
    				totalAmount?: number
    			}
    		}[]
    		subTotal: number
    		taxAmount: number
    		totalAmount: number
    		supplementalData?: { content?: {} }
    	}
    ) {
    	const url = new URL(
    		`https://api.codat.io/companies/${companyId}/connections/${connectionId}/push/directCosts`
    	)
    	for (const [k, v] of [
    		['timeoutInMinutes', timeoutInMinutes],
    		['allowSyncOnPushComplete', allowSyncOnPushComplete]
    	]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    
    	const response = await fetch(url, {
    		method: 'POST',
    		headers: {
    			'Content-Type': 'application/json',
    			Authorization: `Basic ${auth.encodedKey}`
    		},
    		body: JSON.stringify(body)
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 235 days ago