Edits history of script submission #8992 for ' Get All Tickets By Status (freshdesk)'

  • nativets
    One script reply has been approved by the moderators
    Ap­pro­ved
    type Freshdesk = {
    	apiKey: string
    	baseUrl: string
    }
    
    export async function main(
    	resource: Freshdesk,
    	status: 'Open' | 'Pending' | 'Resolved' | 'Closed'
    ) {
    	// Map status to corresponding Freshdesk status code
    	const statusMap: { [key: string]: number } = {
    		Open: 2,
    		Pending: 3,
    		Resolved: 4,
    		Closed: 5
    	}
    
    	// Get the corresponding status code
    	const statusCode = statusMap[status]
    	if (!statusCode) {
    		throw new Error(`Invalid status: ${status}`)
    	}
    
    	// Remove the trailing slash from base URL.
    	const baseUrl = resource.baseUrl.replace(/\/$/, '')
    
    	// Base64 encode the API key with a colon and 'X'
    	const encodedKey = btoa(`${resource.apiKey}:X`)
    
    	// Construct the query parameter and encode it
    	const query = encodeURIComponent(`"status:${statusCode}"`)
    
    	const endpoint = `${baseUrl}/api/v2/search/tickets?query=${query}`
    
    	const response = await fetch(endpoint, {
    		method: 'GET',
    		headers: {
    			Authorization: `Basic ${encodedKey}`,
    			'Content-Type': 'application/json'
    		}
    	})
    
    	if (!response.ok) {
    		throw new Error(`HTTP error! status: ${response.status}`)
    	}
    
    	const data = await response.json()
    
    	return data.results
    }
    

    Submitted by hugo697 652 days ago