From 6b5fabdbbe1b7eba50c8352e6873d6a1a377898d Mon Sep 17 00:00:00 2001 From: TheLeo Date: Mon, 30 Sep 2024 17:21:44 -0500 Subject: [PATCH] Initial Commit --- .gitignore | 11 +++ README.md | 20 ++++++ app/index.html | 22 ++++++ app/scripts/app.js | 133 +++++++++++++++++++++++++++++++++++++ app/styles/images/icon.svg | 14 ++++ app/styles/style.css | 16 +++++ config/app_settings.json | 3 + config/iparams.json | 1 + manifest.json | 17 +++++ 9 files changed, 237 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 app/index.html create mode 100644 app/scripts/app.js create mode 100644 app/styles/images/icon.svg create mode 100644 app/styles/style.css create mode 100644 config/app_settings.json create mode 100644 config/iparams.json create mode 100644 manifest.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a81ac54 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +.DS_Store +.fdk/ +coverage/ +.report.json +.vscode/ +node_modules/ +test_data/ +dist/ +config/oauth_config.json +config/iparam_test_data.json +log/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..cce8df5 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +## Your First App + +This app displays the name of the requester of a freshdesk ticket in the ticket_sidebar placeholder + +### Files and Folders + . + ├── README.md A file for your future self and developer friends to learn about app + ├── app A folder to place all assets required for frontend components + │ ├── index.html A landing page for the user to use the app + │ ├── scripts JavaScript to place files frontend components business logic + │ │ └── app.js + │ └── styles A folder to place all the styles for app + │ ├── images + │ │ └── icon.svg + │ └── style.css + ├── config A folder to place all the configuration files + │ └── iparams.json + └── manifest.json A JSON file holding meta data for app to run on platform + +Explore [more of app sample apps](https://community.developers.freshworks.com/t/freshworks-sample-apps/3604) on the Freshworks github respository. diff --git a/app/index.html b/app/index.html new file mode 100644 index 0000000..5eadaa1 --- /dev/null +++ b/app/index.html @@ -0,0 +1,22 @@ + + + + + A Template App + + + + + + + + +
+

+
+ + + + + + diff --git a/app/scripts/app.js b/app/scripts/app.js new file mode 100644 index 0000000..3e99074 --- /dev/null +++ b/app/scripts/app.js @@ -0,0 +1,133 @@ +var client; +let apiKey = 'wzGvV5YzPzuOCRBnQ09'; + +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, + attachments: ticket.attachments ? ticket.attachments.map(attachment => ({ + name: attachment.name, + url: attachment.attachment_url, + size: attachment.size + })) : [] + }; + + return ticketInfo; + + } catch (error) { + renderText('Error gathering ticket data:\n' + 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 = () => resolve(reader.result, split(',')[1]); + reader.onerror = reject; + reader.readAsDataURL(blob); + }); + } catch (error) { + renderText('Error encoding attachment data:\n' + 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) + })) + ) + + 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 + }) + }); + + if (response.ok) { + console.log('Ticket successfully transferred:', response.status, response.statusText); + renderText('success'); + } else { + renderText('Ticket creation refused by server:\n', response.status, response.statusText); + } + } catch (error) { + renderText('Error sending ticket:\n'+ error); + throw error; + } +} + +async function sendTicketHandler() { + try { + const ticketInfo = await getInfo(); + await sendTicket(ticketInfo); + } catch { + renderText('Error sending ticket:\n'+ error); + throw error; + } +} + +async function renderText(status) { + const textElement = document.getElementById('apptext'); + + if (status === 'init') { + textElement.innerHTML = '

Ticket Ready to be Transferred


'; + } 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}`; +} +*/ diff --git a/app/styles/images/icon.svg b/app/styles/images/icon.svg new file mode 100644 index 0000000..8e72ff0 --- /dev/null +++ b/app/styles/images/icon.svg @@ -0,0 +1,14 @@ + + + + + diff --git a/app/styles/style.css b/app/styles/style.css new file mode 100644 index 0000000..d1f9beb --- /dev/null +++ b/app/styles/style.css @@ -0,0 +1,16 @@ +html { + box-sizing: border-box; +} + +*, +*:after, +*::before { + box-sizing: inherit; +} + +.main { + display: flex; + flex-flow: column wrap; + justify-content: center; + align-items: center; +} diff --git a/config/app_settings.json b/config/app_settings.json new file mode 100644 index 0000000..2d90f7a --- /dev/null +++ b/config/app_settings.json @@ -0,0 +1,3 @@ +{ + "api_key": {} + } \ No newline at end of file diff --git a/config/iparams.json b/config/iparams.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/config/iparams.json @@ -0,0 +1 @@ +{} diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..7465198 --- /dev/null +++ b/manifest.json @@ -0,0 +1,17 @@ +{ + "platform-version": "2.3", + "product": { + "freshdesk": { + "location": { + "ticket_sidebar": { + "url": "index.html", + "icon": "styles/images/icon.svg" + } + } + } + }, + "engines": { + "node": "18.20.4", + "fdk": "9.2.0" + } +}