Created
December 1, 2018 07:48
-
-
Save pasamio/426e49de4e321701503361410bb7f240 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ========== Logger Start ========== // | |
// NAME: Logger | |
// VERSION: 1.0.1 | |
/** | |
* Logger module provides some extra utility functions for logging data. | |
* This adds some useful functions for integrating with the console and | |
* for building script fields. | |
*/ | |
var logger = (function() { | |
var logdata = ""; | |
return { | |
/** | |
* Log a message | |
* | |
* Takes a `message` and adds it to the internal `logdata` variable | |
* and also logs it to the console with a timestamp. | |
*/ | |
logMessage: function(message) | |
{ | |
logdata += message + "\r\n"; | |
console.log(new Date() + '\t' + message) | |
}, | |
/** | |
* Get the internal log buffer | |
* | |
* Returns the raw log buffer to output elsewhere. | |
*/ | |
getLog: function() | |
{ | |
return logdata; | |
}, | |
/** | |
* Clear the internal log buffer | |
* | |
* Resets the buffer to an empty string. | |
*/ | |
clearLog: function() | |
{ | |
logdata = ""; | |
}, | |
/** | |
* Utility function to print a header when the script starts with optional form name | |
*/ | |
consoleHeader: function(scriptName, formName = "") | |
{ | |
var label = scriptName + (formName ? ` (${formName})` : ''); | |
console.log('=================================================================='); | |
console.log('Start "' + label + '" script execution at ' + new Date()); | |
console.log('==================================================================\n'); | |
}, | |
/** | |
* Utility function to print a footer when the script ends with optional form name | |
*/ | |
consoleFooter: function(scriptName, formName = "") | |
{ | |
var label = scriptName + (formName ? ` (${formName})` : ''); | |
console.log('\n=================================================================='); | |
console.log('End "' + label + '" script execution at ' + new Date()); | |
console.log('==================================================================\n'); | |
} | |
} | |
})(); | |
// ========== Logger End ========== // | |
// ========== setIfEmpty Start ========== // | |
// NAME: setIfEmpty | |
// VERSION: 1.0 | |
/** | |
* Set a field if it is currently empty or matches the default value. | |
* | |
* target: The record to use (TFFormEntry object) | |
* fieldId: The field ID (e.g. `fld-hash`) to set. | |
* value: The value to set in the field. | |
* defaultValue: The default value of the field. | |
* | |
* return: boolean true if set or boolean false if unset. | |
*/ | |
function setIfEmpty(target, fieldId, value, defaultValue) | |
{ | |
var current = target.getFieldValue(fieldId); | |
if ((!current || current == defaultValue) && current != value) | |
{ | |
console.log('setIfEmpty passed for ' + fieldId + ', setting to: ' + value); | |
target.setFieldValue(fieldId, value); | |
return true; | |
} | |
else | |
{ | |
console.log('setIfEmpty failed for ' + fieldId + ', skipping.'); | |
return false; | |
} | |
} | |
// ========== setIfEmpty End ========== // | |
function processStateFields() | |
{ | |
var state = record.getFieldValue('fld-9402f8c0d53c43b986fee4ebc3468929'); | |
var previous_state = record.getFieldValue('fld-636a7a4671c14877b1b17ea1b579cef5'); | |
var purchase_date = record.getFieldValue('fld-ccbd9a8f51d34246bebfb31aa4e397dd'); | |
var ship_date = record.getFieldValue('fld-cb3a9886ac7f4ec487447801a3911a1a'); | |
var received_date = record.getFieldValue('fld-bb17d48e41c7423692ab586f6c884d05'); | |
var updatePreviousState = false; | |
logger.logMessage(`Current State: ${state}`); | |
logger.logMessage(`Previous State: ${previous_state}`); | |
logger.logMessage(`Ship Date: ${ship_date}`); | |
logger.logMessage(`Received Date: ${received_date}`); | |
if (!previous_state) | |
{ | |
logger.logMessage("Previous state not set, resetting to current state and forcing update; this should only happen once."); | |
previous_state = state; | |
updatePreviousState = true; | |
} | |
if (state == undefined && purchase_date) | |
{ | |
logger.logMessage("Purchase date set and state is undefined, updating state to Purchased."); | |
record.setFieldValue('fld-9402f8c0d53c43b986fee4ebc3468929', "Purchased") | |
} | |
if (received_date) | |
{ | |
switch(state) | |
{ | |
case "Purchased": | |
case "Shipped": | |
case undefined: | |
logger.logMessage(`Updating state from ${state} to Delivered`); | |
record.setFieldValue('fld-9402f8c0d53c43b986fee4ebc3468929', "Delivered"); | |
break; | |
} | |
} else if (ship_date && (state == "Purchased" || state == undefined)) | |
{ | |
logger.logMessage("Ship date set and state is purchased, updating state to Shipped."); | |
record.setFieldValue('fld-9402f8c0d53c43b986fee4ebc3468929', "Shipped"); | |
} | |
if (state && updatePreviousState) | |
{ | |
logger.logMessage(`Updating state: ${previous_state} to ${state}`); | |
record.setFieldValue('fld-636a7a4671c14877b1b17ea1b579cef5', state); | |
} | |
document.saveAllChanges(); | |
} | |
logger.consoleHeader("State Watcher", "Purchases"); | |
processStateFields(); | |
logger.consoleFooter("State Watcher", "Purchases"); | |
// Return the log as the field value | |
logger.getLog(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment