type Freshdesk = {
apiKey: string
baseUrl: string
}
type TicketStatus = 'Open' | 'Pending' | 'Resolved' | 'Closed'
export async function main(resource: Freshdesk, ticketId: string) {
const statusMap: { [key: number]: TicketStatus } = {
2: 'Open',
3: 'Pending',
4: 'Resolved',
5: 'Closed'
}
// Remove the trailing slash from base URL
const baseUrl = resource.baseUrl.replace(/\/$/, '')
const url = `${baseUrl}/api/v2/tickets/${ticketId}`
// Base64 encode the API key with a colon and 'X'
const encodedKey = btoa(`${resource.apiKey}:X`)
const response = await fetch(url, {
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()
const status = data.status
const responderId = data.responder_id
let assignedStatusFriendlyValue = ''
let ticketStatusFriendlyValue: TicketStatus | number
if (responderId == null) {
assignedStatusFriendlyValue = 'NOTASSIGNED'
} else {
assignedStatusFriendlyValue = 'ASSIGNED'
}
ticketStatusFriendlyValue = statusMap[status] ?? status
const result = {
ticket_status: ticketStatusFriendlyValue,
assigned_status: assignedStatusFriendlyValue,
assigned_id: responderId
}
return result
}
Submitted by hugo697 652 days ago