apps-script-showcase/src/Script.js

117 lines
3.7 KiB
JavaScript

let scriptProperties = PropertiesService.getScriptProperties();
let supportedChecks = [
'used_payment_methods',
];
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Monitoring 👀')
.addItem('Deploy configurations', 'deploy')
.addToUi();
}
function doGet() {
return ContentService.createTextOutput(JSON.stringify(getStoredChecks())).setMimeType(ContentService.MimeType.JSON);
}
function storeChecksById(checksById) {
scriptProperties.setProperties(checksById);
}
function getStoredChecks() {
arr = [];
Object.entries(scriptProperties.getProperties()).forEach(([key, value]) => {
obj = {};
obj[key] = JSON.parse(value);
arr.push(obj);
});
return arr;
}
function getStoredCheckIds() {
return Object.keys(getStoredChecks());
}
function getCheckId(check) {
return check['organisation_identifier'] + '__' + check['check_name'];
}
function validateCheck(check) {
let errors = [];
if (getCheckId(check).split('__').includes('')) {
errors.push('Check is missing `organisation_identifier` or `check_name`');
}
if (!supportedChecks.includes(check['check_name']) && check['check_name'] !== '') {
errors.push('Check is not supported: `' + check['check_name'] + '`');
}
if (check['schedule'] && !/^[1-7]+((,[1-7])|(-[1-7]))*$/.test(check['schedule'])) {
errors.push('Please specify the schedule as a comma separated list and/or range of weekday numbers between 1 and 7, like for example: `1-5`, `1,3,5,7` or `1-3,5`');
} else {
check['schedule'] = '* * * * ' + check['schedule'];
}
if ('' !== check['included_payment_methods'] !== check['excluded_payment_methods']) {
re = /^([A-Z]+,)*[A-Z]+$|^$/;
if (!re.test(check['included_payment_methods']) || !re.test(check['excluded_payment_methods'])) {
errors.push('Please specify payment methods as a comma separated list, like for example: `VIS,ECA` ');
}
if (!check['included_payment_methods'] === !check['excluded_payment_methods']) {
errors.push('Please specify only either `included_payment_methods` or `excluded_payment_methods`, or leave both fields empty to check all payment methods');
}
}
return errors;
}
function getValidatedChecksById(checks) {
let arr = [];
checks.forEach(function(check) {
let obj = {};
obj[getCheckId(check)] = {}
obj[getCheckId(check)]['check'] = check;
obj[getCheckId(check)]['errors'] = validateCheck(check);
arr.push(obj)
});
return arr;
}
function validateAndStoreChecks(checks) {
let validatedChecksById = getValidatedChecksById(checks);
let validChecks = validatedChecksById.filter(check => {return check[Object.keys(check)[0]]['errors'].length === 0});
let checksToStore = {};
validChecks.forEach(function (check) {
let key = Object.keys(check)[0];
let value = JSON.stringify(check[key]['check']);
checksToStore[key] = value;
});
storeChecksById(checksToStore);
let storedCheckIdsToRemove = getStoredCheckIds().map(c => c).filter(check => !validChecks.map(c => Object.keys(c)[0]).includes(check));
storedCheckIdsToRemove.forEach(function (check_name) {
scriptProperties.deleteProperty(check_name);
});
return validatedChecksById;
}
function deploy() {
let html = HtmlService.createTemplateFromFile('DeploymentsSidebarTemplate');
let data = SpreadsheetApp.getActive().getSheetByName("Payments Monitoring Configurations").getDataRange().getValues();
scriptProperties.deleteAllProperties();
let checks = [];
for (let i = 0; i < data.length; i++) {
check = {};
for (let j = 0; j < data[0].length; j++) {
check[data[0][j]] = data[i][j];
}
checks.push(check);
}
checks.shift();
html.deployments = validateAndStoreChecks(checks);
SpreadsheetApp.getUi().showSidebar(html.evaluate());
}