Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 57 additions & 4 deletions KTL.js
Original file line number Diff line number Diff line change
Expand Up @@ -21189,6 +21189,46 @@ function Ktl($, appInfo) {
});
},

/**
* Resolves a logged-in account field value from the Knack user attributes payload.
* @param {object} userAttr Logged-in user attributes returned by Knack.
* @param {string} fieldId Knack field id to resolve.
* @returns {*|undefined} Comparable field value for ktlCond.
*/
getLoggedInAccountFieldValue: function (userAttr, fieldId) {
const userValues = userAttr?.values;
const userValue = userValues?.[fieldId];
let resolvedValue;

if (userValue == null)
resolvedValue = userValue;
else if (Array.isArray(userValue) || typeof userValue !== 'object')
resolvedValue = userValue;
else if (userValue.full != null)
resolvedValue = userValue.full;
else if (userValue.email != null)
resolvedValue = userValue.email;
else if (userValue.identifier != null)
resolvedValue = userValue.identifier;
else if (userValue.label != null)
resolvedValue = userValue.label;
else if (userValue.date_formatted != null)
resolvedValue = userValue.date_formatted;
else
resolvedValue = Object.values(userValue).find(value => value != null && typeof value !== 'object');

return resolvedValue;
},

/**
* Evaluates ktlCond for hide and unhide keyword flows.
* @param {object} options Keyword options.
* @param {Function} hide Hide callback.
* @param {Function} unhide Unhide callback.
* @param {boolean} fixRows Whether table rows are being fixed.
* @param {string} keywordViewId View id owning the keyword.
* @returns {Promise<void|boolean>} Promise resolving when the condition finishes evaluating.
*/
hideUnhideValidateKtlCond: function (options = {}, hide, unhide, fixRows, keywordViewId) {
return new Promise(function (resolve) {
hide(fixRows);
Expand All @@ -21215,11 +21255,17 @@ function Ktl($, appInfo) {
if (view === 'ktlLoggedInAccount') {
const userAttr = Knack.getUserAttributes();
if (userAttr !== 'No user found' && field.startsWith('field_')) {
const userValue = userAttr['values'][field].full;
return resolve(ktlCompare(userValue, operator, value));
const userValue = ktl.views.getLoggedInAccountFieldValue(userAttr, field);
const conditionMatches = ktlCompare(userValue, operator, value);

if (!conditionMatches)
unhide();

return resolve();
} else {
console.error(`ktlCond - ktlLoggedInAccount in ${keywordViewId} requires a fieldId to compare against not a field label ${field}.`);
return resolve(false);
unhide();
return resolve();
}
}

Expand Down Expand Up @@ -21366,6 +21412,13 @@ function Ktl($, appInfo) {
});
},

/**
* Evaluates whether a ktlCond expression matches the current state.
* @param {object} options Keyword options.
* @param {object} recordObj Record object used by tables and lists.
* @param {string} keywordViewId View id owning the keyword.
* @returns {Promise<boolean>} Promise resolving to whether the condition matches.
*/
validateKtlCond: function (options = {}, recordObj = {} /*Used only with Tables and Lists*/, keywordViewId) {
return new Promise(function (resolve) {
if (!options.ktlCond) return resolve(true);
Expand All @@ -21389,7 +21442,7 @@ function Ktl($, appInfo) {
if (view === 'ktlLoggedInAccount') {
const userAttr = Knack.getUserAttributes();
if (userAttr !== 'No user found' && field.startsWith('field_')) {
const userValue = userAttr['values'][field].full;
const userValue = ktl.views.getLoggedInAccountFieldValue(userAttr, field);
return resolve(ktlCompare(userValue, operator, value));
} else {
console.error(`ktlCond - ktlLoggedInAccount in ${keywordViewId} requires a fieldId to compare against not a field label ${field}.`);
Expand Down