refactored app code and fixed functionality
This commit is contained in:
parent
6b5fabdbbe
commit
63d5b88710
@ -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,32 +51,24 @@ 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',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Authorization': 'Basic ' + btoa(apiKey + ':X')
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
name: ticketInfo.name,
|
name: ticketInfo.name,
|
||||||
email: ticketInfo.email,
|
email: ticketInfo.email,
|
||||||
phone: ticketInfo.phone,
|
phone: ticketInfo.phone,
|
||||||
@ -83,28 +77,44 @@ async function sendTicket(ticketInfo) {
|
|||||||
status: ticketInfo.status,
|
status: ticketInfo.status,
|
||||||
priority: ticketInfo.priority,
|
priority: ticketInfo.priority,
|
||||||
source: ticketInfo.source,
|
source: ticketInfo.source,
|
||||||
attachments: encodedAttachments
|
}
|
||||||
})
|
|
||||||
|
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) {
|
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
13
config/requests.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -7,6 +7,9 @@
|
|||||||
"url": "index.html",
|
"url": "index.html",
|
||||||
"icon": "styles/images/icon.svg"
|
"icon": "styles/images/icon.svg"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"requests": {
|
||||||
|
"createTicket": {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user