From 63d5b887101f848197622ffeafe8db38cedbdca3 Mon Sep 17 00:00:00 2001 From: TheLeo Date: Tue, 1 Oct 2024 13:17:47 -0500 Subject: [PATCH] refactored app code and fixed functionality --- app/scripts/app.js | 94 ++++++++++++++++++++++++-------------------- config/requests.json | 13 ++++++ manifest.json | 3 ++ 3 files changed, 68 insertions(+), 42 deletions(-) create mode 100644 config/requests.json diff --git a/app/scripts/app.js b/app/scripts/app.js index 3e99074..6d14ddc 100644 --- a/app/scripts/app.js +++ b/app/scripts/app.js @@ -1,11 +1,11 @@ -var client; -let apiKey = 'wzGvV5YzPzuOCRBnQ09'; - init(); async function init() { client = await app.initialized(); - client.events.on('app.activated', function () { renderText('init')}); + + client.events.on('app.activated', function () { + renderText('init'); + }); } async function getInfo() { @@ -27,17 +27,19 @@ async function getInfo() { status: 2, priority: 1, source: ticket.source, - attachments: ticket.attachments ? ticket.attachments.map(attachment => ({ - name: attachment.name, - url: attachment.attachment_url, - size: attachment.size - })) : [] }; + 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' + error); + renderText(`Error gathering ticket data:\n${JSON.stringify(error)}`); throw error; } } @@ -49,62 +51,70 @@ async function encodeAttachments(fileUrl) { const reader = new FileReader(); return new Promise((resolve, reject) => { - reader.onloadend = () => resolve(reader.result, split(',')[1]); + reader.onloadend = () => { + const base64Data = reader.result.split(',')[1]; + resolve(base64Data); + }; reader.onerror = reject; reader.readAsDataURL(blob); }); } catch (error) { - renderText('Error encoding attachment data:\n' + error); + renderText(`Error encoding attachment data:\n${JSON.stringify(error)}`); throw error; } } async function sendTicket(ticketInfo) { try { - const encodedAttachments = await Promise.all( - ticketInfo.attachments.map(async (att) => ({ - name: att.name, - content: await encodeAttachments(att.url) - })) - ) + let encodedAttachments = []; - const response = await fetch('https://cramalotsales.freshdesk.com/api/v2/tickets', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': 'Basic ' + btoa(apiKey + ':X') - }, - body: JSON.stringify({ - name: ticketInfo.name, - email: ticketInfo.email, - phone: ticketInfo.phone, - subject: ticketInfo.subject, - description: ticketInfo.description, - status: ticketInfo.status, - priority: ticketInfo.priority, - source: ticketInfo.source, - attachments: 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.ok) { - console.log('Ticket successfully transferred:', response.status, response.statusText); + if (response.status === 201) { renderText('success'); } else { - renderText('Ticket creation refused by server:\n', response.status, response.statusText); + renderText(`Ticket creation refused by server:\n ${response.status} ${response.response}`); } + } catch (error) { - renderText('Error sending ticket:\n'+ 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'+ error); + renderText('Error sending ticket:\n' + JSON.strigity(error)); throw error; } } @@ -113,11 +123,11 @@ async function renderText(status) { const textElement = document.getElementById('apptext'); if (status === 'init') { - textElement.innerHTML = '

Ticket Ready to be Transferred


'; + textElement.innerHTML = '

Ticket Ready to be Transferred\n


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

Ticket Transfer Successful!

'; } else { - textElement.innerHtml = status; + textElement.innerHTML = status; } } /* diff --git a/config/requests.json b/config/requests.json new file mode 100644 index 0000000..c10014e --- /dev/null +++ b/config/requests.json @@ -0,0 +1,13 @@ +{ + "createTicket": { + "schema": { + "method": "POST", + "host": "cramalotsales.freshdesk.com", + "path": "/api/v2/tickets", + "headers": { + "Authorization": "Basic d3pHdlY1WXpQenVPQ1JCblEwOTpY", + "Content-Type": "application/json" + } + } + } +} \ No newline at end of file diff --git a/manifest.json b/manifest.json index 7465198..63b52cb 100644 --- a/manifest.json +++ b/manifest.json @@ -7,6 +7,9 @@ "url": "index.html", "icon": "styles/images/icon.svg" } + }, + "requests": { + "createTicket": {} } } },