refactored app code and fixed functionality

This commit is contained in:
TheLeo 2024-10-01 13:17:47 -05:00
parent 6b5fabdbbe
commit 63d5b88710
Signed by: TheLeo
SSH Key Fingerprint: SHA256:I6y87aNvnNIcpGGnHQ3Bvoryknh/q9lVPhB+4JjgrfM
3 changed files with 68 additions and 42 deletions

View File

@ -1,11 +1,11 @@
var client;
let apiKey = 'wzGvV5YzPzuOCRBnQ09';
init(); init();
async function init() { async function init() {
client = await app.initialized(); client = await app.initialized();
client.events.on('app.activated', function () { renderText('init')});
client.events.on('app.activated', function () {
renderText('init');
});
} }
async function getInfo() { async function getInfo() {
@ -27,17 +27,19 @@ async function getInfo() {
status: 2, status: 2,
priority: 1, priority: 1,
source: ticket.source, 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; return ticketInfo;
} catch (error) { } catch (error) {
renderText('<font color="#B21508">Error gathering ticket data:\n</font>' + error); renderText(`<font color="#B21508">Error gathering ticket data:\n</font>${JSON.stringify(error)}`);
throw error; throw error;
} }
} }
@ -49,62 +51,70 @@ async function encodeAttachments(fileUrl) {
const reader = new FileReader(); const reader = new FileReader();
return new Promise((resolve, reject) => { 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.onerror = reject;
reader.readAsDataURL(blob); reader.readAsDataURL(blob);
}); });
} catch (error) { } catch (error) {
renderText('<font color="#B21508">Error encoding attachment data:</font>\n' + error); renderText(`<font color="#B21508">Error encoding attachment data:</font>\n${JSON.stringify(error)}`);
throw error; throw error;
} }
} }
async function sendTicket(ticketInfo) { async function sendTicket(ticketInfo) {
try { try {
const encodedAttachments = await Promise.all( let encodedAttachments = [];
ticketInfo.attachments.map(async (att) => ({
name: att.name,
content: await encodeAttachments(att.url)
}))
)
const response = await fetch('https://cramalotsales.freshdesk.com/api/v2/tickets', { const ticketRequestBody = {
method: 'POST', name: ticketInfo.name,
headers: { email: ticketInfo.email,
'Content-Type': 'application/json', phone: ticketInfo.phone,
'Authorization': 'Basic ' + btoa(apiKey + ':X') subject: ticketInfo.subject,
}, description: ticketInfo.description,
body: JSON.stringify({ status: ticketInfo.status,
name: ticketInfo.name, priority: ticketInfo.priority,
email: ticketInfo.email, source: ticketInfo.source,
phone: ticketInfo.phone, }
subject: ticketInfo.subject,
description: ticketInfo.description, if (ticketInfo.attachments && ticketInfo.attchments.length > 0) {
status: ticketInfo.status, encodedAttachments = await Promise.all(
priority: ticketInfo.priority, ticketInfo.attachments.map(async (att) => ({
source: ticketInfo.source, name: att.name,
attachments: encodedAttachments 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) { if (response.status === 201) {
console.log('Ticket successfully transferred:', response.status, response.statusText);
renderText('success'); renderText('success');
} else { } else {
renderText('Ticket creation refused by server:\n', response.status, response.statusText); renderText(`<font color="#B21508">Ticket creation refused by server:</font>\n ${response.status} ${response.response}`);
} }
} catch (error) { } catch (error) {
renderText('<font color="#B21508">Error sending ticket:</font>\n'+ error); renderText(`<font color="#B21508">Error sending ticket:</font>\n${JSON.stringify(error)}`);
throw error; throw error;
} }
} }
async function sendTicketHandler() { async function sendTicketHandler() {
try { try {
const ticketInfo = await getInfo(); const ticketInfo = await getInfo();
await sendTicket(ticketInfo); await sendTicket(ticketInfo);
} catch { } catch {
renderText('<font color="#B21508">Error sending ticket:</font>\n'+ error); renderText('<font color="#B21508">Error sending ticket:</font>\n' + JSON.strigity(error));
throw error; throw error;
} }
} }
@ -113,11 +123,11 @@ async function renderText(status) {
const textElement = document.getElementById('apptext'); const textElement = document.getElementById('apptext');
if (status === 'init') { if (status === 'init') {
textElement.innerHTML = '<p>Ticket Ready to be Transferred</p><br><button onclick="sendTicketHandler()">Transfer</button>'; textElement.innerHTML = '<p>Ticket Ready to be Transferred\n</p><br><button onclick="sendTicketHandler()">Transfer</button>';
} else if (status === 'success') { } else if (status === 'success') {
textElement.innerHTML = '<p><font color="#9900FF">Ticket Transfer Successful!</font></p>'; textElement.innerHTML = '<p><font color="#9900FF">Ticket Transfer Successful!</font></p>';
} else { } else {
textElement.innerHtml = status; textElement.innerHTML = status;
} }
} }
/* /*

13
config/requests.json Normal file
View File

@ -0,0 +1,13 @@
{
"createTicket": {
"schema": {
"method": "POST",
"host": "cramalotsales.freshdesk.com",
"path": "/api/v2/tickets",
"headers": {
"Authorization": "Basic d3pHdlY1WXpQenVPQ1JCblEwOTpY",
"Content-Type": "application/json"
}
}
}
}

View File

@ -7,6 +7,9 @@
"url": "index.html", "url": "index.html",
"icon": "styles/images/icon.svg" "icon": "styles/images/icon.svg"
} }
},
"requests": {
"createTicket": {}
} }
} }
}, },