134 lines
3.6 KiB
JavaScript
134 lines
3.6 KiB
JavaScript
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('<font color="#B21508">Error gathering ticket data:\n</font>' + 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('<font color="#B21508">Error encoding attachment data:</font>\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('<font color="#B21508">Error sending ticket:</font>\n'+ error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function sendTicketHandler() {
|
|
try {
|
|
const ticketInfo = await getInfo();
|
|
await sendTicket(ticketInfo);
|
|
} catch {
|
|
renderText('<font color="#B21508">Error sending ticket:</font>\n'+ error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function renderText(status) {
|
|
const textElement = document.getElementById('apptext');
|
|
|
|
if (status === 'init') {
|
|
textElement.innerHTML = '<p>Ticket Ready to be Transferred</p><br><button onclick="sendTicketHandler()">Transfer</button>';
|
|
} else if (status === 'success') {
|
|
textElement.innerHTML = '<p><font color="#9900FF">Ticket Transfer Successful!</font></p>';
|
|
} 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}`;
|
|
}
|
|
*/
|