160 lines
4.5 KiB
JavaScript
160 lines
4.5 KiB
JavaScript
/*
|
|
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
* See LICENSE in the project root for license information.
|
|
*/
|
|
|
|
/* global document, Office */
|
|
import axios from "axios";
|
|
|
|
// axios conf
|
|
axios.defaults.headers.common["api-language"] = "nl";
|
|
|
|
const localApiUrl = "https://bykhztyhjw.sharedwithexpose.com/api/zoek_bestelling";
|
|
const orderUrlEdit = "https://bykhztyhjw.sharedwithexpose.com/bestellingen/edit/";
|
|
const orderUrlIndex = "https://bykhztyhjw.sharedwithexpose.com/bestellingen";
|
|
const apiCredentialUrl = "https://bykhztyhjw.sharedwithexpose.com/api/api_credentials";
|
|
|
|
Office.onReady((info) => {
|
|
if (info.host === Office.HostType.Outlook) {
|
|
document.getElementById("app-body").style.display = "flex";
|
|
document.getElementById("run").onclick = run;
|
|
}
|
|
});
|
|
|
|
export async function run() {
|
|
// Set load animation in button
|
|
loadAnimation(true);
|
|
|
|
// btoa(getLastName(Office.context.mailbox.item.sender.displayName))
|
|
// btoa(getInitials(Office.context.mailbox.item.sender.displayName))
|
|
// btoa(Office.context.mailbox.item.sender.emailAddress)
|
|
//senderName: btoa(Office.context.mailbox.item.sender.displayName)
|
|
|
|
let searchInfo = {
|
|
senderLastName: btoa("DIT BESTAAT SWS NIET"),
|
|
senderInitials: btoa("NAHH"),
|
|
senderName: btoa("NO WAY DAT DIT BESTAAT!"),
|
|
senderEmail: btoa("Anjameijer1958@outlook.com"),
|
|
senderSubject: btoa(Office.context.mailbox.item.subject),
|
|
postalCodes: btoa(["1234AA, 0910AA"]),
|
|
};
|
|
|
|
// searchInfo.postalCodes = await new Promise((resolve) => {
|
|
// Office.context.mailbox.item.body.getAsync("text", function (async) {
|
|
// resolve(getPostalCodesFromText(async.value));
|
|
// });
|
|
// });
|
|
|
|
// Make API call to see if we can find an order for the e-mail.
|
|
let results;
|
|
await search(searchInfo).then((res) => (results = res));
|
|
|
|
// Disable load animation in button
|
|
loadAnimation(false);
|
|
|
|
// Load results via buttons
|
|
showResults(results);
|
|
}
|
|
|
|
async function getApiCredential() {
|
|
let key = [];
|
|
|
|
await axios
|
|
.get(apiCredentialUrl)
|
|
.then(function (res) {
|
|
key = res.data.key ?? "";
|
|
})
|
|
.catch((err) => err);
|
|
|
|
return key;
|
|
}
|
|
|
|
// Animation function for loader on button
|
|
function loadAnimation(animate) {
|
|
const loadButton = document.getElementById("run");
|
|
const loader = document.getElementById("loader");
|
|
const btnText = document.getElementById("btn-text");
|
|
|
|
if (animate) {
|
|
loadButton.disabled = true;
|
|
btnText.style.display = "none";
|
|
loader.style.display = "inline-block";
|
|
}
|
|
|
|
if (!animate) {
|
|
loadButton.disabled = false;
|
|
loader.style.display = "none";
|
|
btnText.style.display = "block";
|
|
}
|
|
}
|
|
|
|
// OVAS API call
|
|
async function search(searchInfo) {
|
|
let results = [];
|
|
|
|
let key = await getApiCredential();
|
|
|
|
await axios
|
|
.get(localApiUrl, {
|
|
params: searchInfo,
|
|
headers: {
|
|
"api-key": key,
|
|
},
|
|
})
|
|
.then(function (res) {
|
|
results = res.data.results ?? [];
|
|
})
|
|
.catch((err) => err);
|
|
|
|
return results;
|
|
}
|
|
|
|
function getLastName(name) {
|
|
const nameParts = name.trim().split(" ");
|
|
const lastName = nameParts.slice(-1)[0]; // Pak het laatste deel als achternaam
|
|
|
|
return lastName;
|
|
}
|
|
|
|
function getInitials(name) {
|
|
const nameParts = name.trim().split(" ");
|
|
const firstName = nameParts[0];
|
|
const initials = firstName.charAt(0).toUpperCase() + ".";
|
|
|
|
return initials;
|
|
}
|
|
|
|
function getPostalCodesFromText(emailBody) {
|
|
const regex = /\b\d{4}\s?[A-Z]{2}\b/g;
|
|
let postalCodes = emailBody.match(regex);
|
|
|
|
let postalCodesWithoutWhiteSpaces = postalCodes.map((i) => i.replace(/\s/g, "").toUpperCase());
|
|
|
|
return btoa(postalCodesWithoutWhiteSpaces);
|
|
}
|
|
|
|
function showResults(results) {
|
|
let resultDiv = document.getElementById("results");
|
|
resultDiv.innerHTML = "";
|
|
|
|
if (results.length > 0) {
|
|
results.forEach((result) => {
|
|
resultDiv.innerHTML += resultsFoundButton(result.id, result.lastname, result.url_infix);
|
|
});
|
|
} else {
|
|
resultDiv.innerHTML = noResultsFoundButton();
|
|
}
|
|
}
|
|
|
|
function resultsFoundButton(id, customer, urlInfix) {
|
|
return `<button type="button" target="_blank" onclick="location.href='${orderUrlEdit}${id}?bv=${urlInfix}'" class="result-button" style="text-decoration: none;">
|
|
<span class="btn-text" style="display: block;">Order ID: ${id} - ${customer}</span>
|
|
</button>`;
|
|
}
|
|
|
|
function noResultsFoundButton() {
|
|
return `<button type="button" onclick="location.href='${orderUrlIndex}'" class="no-result-button" style="text-decoration: none;">
|
|
<span class="btn-text" style="display: block;">Geen bestellingen gevonden...</span>
|
|
</button>`;
|
|
}
|