init(); async function init() { client = await app.initialized(); client.events.on('app.activated', function () { renderText('init'); }); } async function getInfo() { try { const [ticketData, contactData] = await Promise.all([ client.data.get('ticket'), client.data.get('contact') ]); const { contact } = contactData; const { ticket } = ticketData; const ticketInfo = { name: contact.name, email: contact.email, phone: contact.phone, subject: ticket.subject, description: ticket.description, status: 2, priority: 1, source: ticket.source, }; if (ticket.attachments && ticket.attachments.length > 0) { ticketInfo.attachments = ticket.attachments.map(attachment => ({ name: attachment.name, url: attachment.attachment_url })); } return ticketInfo; } catch (error) { renderText(`Error gathering ticket data:\n${JSON.stringify(error)}`); throw error; } } async function encodeAttachments(fileUrl) { try { const response = await fetch(fileUrl); const blob = await response.blob(); const reader = new FileReader(); return new Promise((resolve, reject) => { reader.onloadend = () => { const base64Data = reader.result.split(',')[1]; resolve(base64Data); }; reader.onerror = reject; reader.readAsDataURL(blob); }); } catch (error) { renderText(`Error encoding attachment data:\n${JSON.stringify(error)}`); throw error; } } async function sendTicket(ticketInfo) { try { let encodedAttachments = []; const ticketRequestBody = { name: ticketInfo.name, email: ticketInfo.email, phone: ticketInfo.phone, subject: ticketInfo.subject, description: ticketInfo.description, status: ticketInfo.status, priority: ticketInfo.priority, source: ticketInfo.source, } if (ticketInfo.attachments && ticketInfo.attchments.length > 0) { encodedAttachments = await Promise.all( ticketInfo.attachments.map(async (att) => ({ name: att.name, content: await encodeAttachments(att.url) })) ); } if (encodedAttachments.length > 0) { ticketRequestBody = encodedAttachments; } const response = await client.request.invokeTemplate("createTicket", { body: JSON.stringify(ticketRequestBody) }); if (response.status === 201) { renderText('success'); } else { renderText(`Ticket creation refused by server:\n ${response.status} ${response.response}`); } } catch (error) { renderText(`Error sending ticket:\n${JSON.stringify(error)}`); throw error; } } async function sendTicketHandler() { try { const ticketInfo = await getInfo(); await sendTicket(ticketInfo); } catch { renderText('Error sending ticket:\n' + JSON.strigity(error)); throw error; } } async function renderText(status) { const textElement = document.getElementById('apptext'); if (status === 'init') { textElement.innerHTML = '

Ticket Ready to be Transferred\n


'; } else if (status === 'success') { textElement.innerHTML = '

Ticket Transfer Successful!

'; } else { textElement.innerHTML = status; } } /* async function renderText() { const textElement = document.getElementById('apptext'); const contactData = await client.data.get('contact'); const { contact: { name } } = contactData; textElement.innerHTML = `Ticket is created by ${name}`; } */