Edits history of script submission #10176 for ' Creates multiple contacts (bulk) in a Xero organisation (xero)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Xero = {
    	token: string
    }
    /**
     * Creates multiple contacts (bulk) in a Xero organisation
     *
     */
    export async function main(
    	auth: Xero,
    	summarizeErrors: string | undefined,
    	xero_tenant_id: string,
    	Idempotency_Key: string,
    	body: {
    		pagination?: {
    			page?: number
    			pageSize?: number
    			pageCount?: number
    			itemCount?: number
    		}
    		Warnings?: { Message?: string }[]
    		Contacts?: {
    			ContactID?: string
    			MergedToContactID?: string
    			ContactNumber?: string
    			AccountNumber?: string
    			ContactStatus?: 'ACTIVE' | 'ARCHIVED' | 'GDPRREQUEST'
    			Name?: string
    			FirstName?: string
    			LastName?: string
    			CompanyNumber?: string
    			EmailAddress?: string
    			ContactPersons?: {
    				FirstName?: string
    				LastName?: string
    				EmailAddress?: string
    				IncludeInEmails?: false | true
    			}[]
    			BankAccountDetails?: string
    			TaxNumber?: string
    			AccountsReceivableTaxType?: string
    			AccountsPayableTaxType?: string
    			Addresses?: {
    				AddressType?: 'POBOX' | 'STREET'
    				AddressLine1?: string
    				AddressLine2?: string
    				AddressLine3?: string
    				AddressLine4?: string
    				City?: string
    				Region?: string
    				PostalCode?: string
    				Country?: string
    				AttentionTo?: string
    			}[]
    			Phones?: {
    				PhoneType?: 'DEFAULT' | 'DDI' | 'MOBILE' | 'FAX' | 'OFFICE'
    				PhoneNumber?: string
    				PhoneAreaCode?: string
    				PhoneCountryCode?: string
    			}[]
    			IsSupplier?: false | true
    			IsCustomer?: false | true
    			SalesDefaultLineAmountType?: 'INCLUSIVE' | 'EXCLUSIVE' | 'NONE'
    			PurchasesDefaultLineAmountType?: 'INCLUSIVE' | 'EXCLUSIVE' | 'NONE'
    			DefaultCurrency?:
    				| 'AED'
    				| 'AFN'
    				| 'ALL'
    				| 'AMD'
    				| 'ANG'
    				| 'AOA'
    				| 'ARS'
    				| 'AUD'
    				| 'AWG'
    				| 'AZN'
    				| 'BAM'
    				| 'BBD'
    				| 'BDT'
    				| 'BGN'
    				| 'BHD'
    				| 'BIF'
    				| 'BMD'
    				| 'BND'
    				| 'BOB'
    				| 'BRL'
    				| 'BSD'
    				| 'BTN'
    				| 'BWP'
    				| 'BYN'
    				| 'BYR'
    				| 'BZD'
    				| 'CAD'
    				| 'CDF'
    				| 'CHF'
    				| 'CLF'
    				| 'CLP'
    				| 'CNY'
    				| 'COP'
    				| 'CRC'
    				| 'CUC'
    				| 'CUP'
    				| 'CVE'
    				| 'CZK'
    				| 'DJF'
    				| 'DKK'
    				| 'DOP'
    				| 'DZD'
    				| 'EEK'
    				| 'EGP'
    				| 'ERN'
    				| 'ETB'
    				| 'EUR'
    				| 'FJD'
    				| 'FKP'
    				| 'GBP'
    				| 'GEL'
    				| 'GHS'
    				| 'GIP'
    				| 'GMD'
    				| 'GNF'
    				| 'GTQ'
    				| 'GYD'
    				| 'HKD'
    				| 'HNL'
    				| 'HRK'
    				| 'HTG'
    				| 'HUF'
    				| 'IDR'
    				| 'ILS'
    				| 'INR'
    				| 'IQD'
    				| 'IRR'
    				| 'ISK'
    				| 'JMD'
    				| 'JOD'
    				| 'JPY'
    				| 'KES'
    				| 'KGS'
    				| 'KHR'
    				| 'KMF'
    				| 'KPW'
    				| 'KRW'
    				| 'KWD'
    				| 'KYD'
    				| 'KZT'
    				| 'LAK'
    				| 'LBP'
    				| 'LKR'
    				| 'LRD'
    				| 'LSL'
    				| 'LTL'
    				| 'LVL'
    				| 'LYD'
    				| 'MAD'
    				| 'MDL'
    				| 'MGA'
    				| 'MKD'
    				| 'MMK'
    				| 'MNT'
    				| 'MOP'
    				| 'MRO'
    				| 'MRU'
    				| 'MUR'
    				| 'MVR'
    				| 'MWK'
    				| 'MXN'
    				| 'MXV'
    				| 'MYR'
    				| 'MZN'
    				| 'NAD'
    				| 'NGN'
    				| 'NIO'
    				| 'NOK'
    				| 'NPR'
    				| 'NZD'
    				| 'OMR'
    				| 'PAB'
    				| 'PEN'
    				| 'PGK'
    				| 'PHP'
    				| 'PKR'
    				| 'PLN'
    				| 'PYG'
    				| 'QAR'
    				| 'RON'
    				| 'RSD'
    				| 'RUB'
    				| 'RWF'
    				| 'SAR'
    				| 'SBD'
    				| 'SCR'
    				| 'SDG'
    				| 'SEK'
    				| 'SGD'
    				| 'SHP'
    				| 'SKK'
    				| 'SLE'
    				| 'SLL'
    				| 'SOS'
    				| 'SRD'
    				| 'STD'
    				| 'STN'
    				| 'SVC'
    				| 'SYP'
    				| 'SZL'
    				| 'THB'
    				| 'TJS'
    				| 'TMT'
    				| 'TND'
    				| 'TOP'
    				| 'TRY'
    				| 'TTD'
    				| 'TWD'
    				| 'TZS'
    				| 'UAH'
    				| 'UGX'
    				| 'USD'
    				| 'UYU'
    				| 'UZS'
    				| 'VEF'
    				| 'VES'
    				| 'VND'
    				| 'VUV'
    				| 'WST'
    				| 'XAF'
    				| 'XCD'
    				| 'XOF'
    				| 'XPF'
    				| 'YER'
    				| 'ZAR'
    				| 'ZMW'
    				| 'ZMK'
    				| 'ZWD'
    			XeroNetworkKey?: string
    			SalesDefaultAccountCode?: string
    			PurchasesDefaultAccountCode?: string
    			SalesTrackingCategories?: {
    				TrackingCategoryName?: string
    				TrackingOptionName?: string
    			}[]
    			PurchasesTrackingCategories?: {
    				TrackingCategoryName?: string
    				TrackingOptionName?: string
    			}[]
    			TrackingCategoryName?: string
    			TrackingCategoryOption?: string
    			PaymentTerms?: {
    				Bills?: {
    					Day?: number
    					Type?: 'DAYSAFTERBILLDATE' | 'DAYSAFTERBILLMONTH' | 'OFCURRENTMONTH' | 'OFFOLLOWINGMONTH'
    				}
    				Sales?: {
    					Day?: number
    					Type?: 'DAYSAFTERBILLDATE' | 'DAYSAFTERBILLMONTH' | 'OFCURRENTMONTH' | 'OFFOLLOWINGMONTH'
    				}
    			}
    			UpdatedDateUTC?: string
    			ContactGroups?: {
    				Name?: string
    				Status?: 'ACTIVE' | 'DELETED'
    				ContactGroupID?: string
    				Contacts?: {}[]
    			}[]
    			Website?: string
    			BrandingTheme?: {
    				BrandingThemeID?: string
    				Name?: string
    				LogoUrl?: string
    				Type?: 'INVOICE'
    				SortOrder?: number
    				CreatedDateUTC?: string
    			}
    			BatchPayments?: {
    				BankAccountNumber?: string
    				BankAccountName?: string
    				Details?: string
    				Code?: string
    				Reference?: string
    			}
    			Discount?: number
    			Balances?: {
    				AccountsReceivable?: { Outstanding?: number; Overdue?: number }
    				AccountsPayable?: { Outstanding?: number; Overdue?: number }
    			}
    			Attachments?: {
    				AttachmentID?: string
    				FileName?: string
    				Url?: string
    				MimeType?: string
    				ContentLength?: number
    				IncludeOnline?: false | true
    			}[]
    			HasAttachments?: never
    			ValidationErrors?: { Message?: string }[]
    			HasValidationErrors?: never
    			StatusAttributeString?: string
    		}[]
    	}
    ) {
    	const url = new URL(`https://api.xero.com/api.xro/2.0/Contacts`)
    	for (const [k, v] of [['summarizeErrors', summarizeErrors]]) {
    		if (v !== undefined && v !== '' && k !== undefined) {
    			url.searchParams.append(k, v)
    		}
    	}
    	const response = await fetch(url, {
    		method: 'PUT',
    		headers: {
    			Accept: 'application/json',
    			'xero-tenant-id': xero_tenant_id,
    			'Idempotency-Key': Idempotency_Key,
    			'Content-Type': 'application/json',
    			Authorization: 'Bearer ' + auth.token
    		},
    		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 515 days ago