144 lines
3.7 KiB
JavaScript
144 lines
3.7 KiB
JavaScript
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,
|
|
};
|
|
|
|
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(`<font color="#B21508">Error gathering ticket data:\n</font>${JSON.stringify(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 = () => {
|
|
const base64Data = reader.result.split(',')[1];
|
|
resolve(base64Data);
|
|
};
|
|
reader.onerror = reject;
|
|
reader.readAsDataURL(blob);
|
|
});
|
|
} catch (error) {
|
|
renderText(`<font color="#B21508">Error encoding attachment data:</font>\n${JSON.stringify(error)}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function sendTicket(ticketInfo) {
|
|
try {
|
|
let 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.status === 201) {
|
|
renderText('success');
|
|
} else {
|
|
renderText(`<font color="#B21508">Ticket creation refused by server:</font>\n ${response.status} ${response.response}`);
|
|
}
|
|
|
|
} catch (error) {
|
|
renderText(`<font color="#B21508">Error sending ticket:</font>\n${JSON.stringify(error)}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
|
|
async function sendTicketHandler() {
|
|
try {
|
|
const ticketInfo = await getInfo();
|
|
await sendTicket(ticketInfo);
|
|
} catch {
|
|
renderText('<font color="#B21508">Error sending ticket:</font>\n' + JSON.strigity(error));
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function renderText(status) {
|
|
const textElement = document.getElementById('apptext');
|
|
|
|
if (status === 'init') {
|
|
textElement.innerHTML = '<p>Ticket Ready to be Transferred\n</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}`;
|
|
}
|
|
*/
|