n8n-nodes-smartsuite
Version:
n8n community node for SmartSuite
173 lines • 6.93 kB
JavaScript
;
// src/nodes/SmartSuite/SmartSuiteTrigger.node.ts
Object.defineProperty(exports, "__esModule", { value: true });
exports.SmartSuiteTrigger = void 0;
const smartSuiteApi_1 = require("./transport/smartSuiteApi");
const utils_1 = require("./helpers/utils");
const loadOptions_1 = require("./methods/loadOptions");
const listSearch_1 = require("./methods/listSearch");
class SmartSuiteTrigger {
description = {
displayName: 'SmartSuite Trigger',
name: 'smartSuiteTrigger',
icon: 'file:SmartSuite.svg',
group: ['trigger'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Watch for new or updated records in SmartSuite',
defaults: { name: 'SmartSuite Trigger' },
credentials: [
{
name: 'smartSuiteApi',
required: true,
testedBy: 'smartSuiteApiTest',
},
],
polling: true,
inputs: ["main" /* NodeConnectionType.Main */],
outputs: ["main" /* NodeConnectionType.Main */],
properties: [
{
displayName: 'Solution',
name: 'solutionId',
type: 'resourceLocator',
noDataExpression: false,
default: { mode: 'list', value: '' },
modes: [
{
name: 'list',
displayName: 'From List',
type: 'list',
typeOptions: {
searchListMethod: 'solutionSearch',
searchable: true,
},
},
{
name: 'id',
displayName: 'By ID',
type: 'string',
placeholder: 'Solution ID',
},
],
description: 'Select the SmartSuite solution to watch',
},
{
displayName: 'Table',
name: 'tableId',
type: 'resourceLocator',
noDataExpression: false,
default: { mode: 'list', value: '' },
modes: [
{
name: 'list',
displayName: 'From List',
type: 'list',
typeOptions: {
searchListMethod: 'tableSearch',
searchable: true,
},
},
{
name: 'id',
displayName: 'By ID',
type: 'string',
placeholder: 'Table ID',
},
],
description: 'Select the table to watch',
},
{
displayName: 'Trigger Field',
name: 'triggerField',
type: 'options',
noDataExpression: true,
options: [
{ name: 'First Created', value: 'first_created', description: 'Trigger when a record is first created' },
{ name: 'Last Updated', value: 'last_updated', description: 'Trigger when a record is updated' },
],
default: 'last_updated',
description: 'Which timestamp to use for change detection',
},
],
};
methods = {
credentialTest: {
async smartSuiteApiTest() {
try {
await smartSuiteApi_1.apiRequest.call(this, 'GET', '/solutions/', {}, { limit: 1, offset: 0 });
return { status: 'OK', message: 'Connection successful!' };
}
catch (err) {
return { status: 'Error', message: err.message };
}
},
},
loadOptions: {
getOperations: loadOptions_1.getOperations,
getTableFields: loadOptions_1.getTableFields,
getMutableTableFields: loadOptions_1.getMutableTableFields,
getFilterOptionsForFieldType: loadOptions_1.getFilterOptionsForFieldType,
},
listSearch: {
solutionSearch: listSearch_1.solutionSearch,
tableSearch: listSearch_1.tableSearch,
},
};
async poll() {
const staticData = this.getWorkflowStaticData('node');
const mode = this.getMode();
let runPrefix = '';
if (mode !== 'manual') {
const prevCount = staticData.runCount || 0;
const runCount = prevCount + 1;
staticData.runCount = runCount;
const runIndex = String(runCount).padStart(3, '0');
runPrefix = `${runIndex}-`;
}
(0, utils_1.debugLog)(`[SmartSuiteTrigger] Trigger Run-${runPrefix}${new Date().toISOString()}`, {}, 3);
const tableId = (0, utils_1.asIdString)(this.getNodeParameter('tableId', 0));
const triggerField = this.getNodeParameter('triggerField', 0);
const path = `/applications/${tableId}/records/list/`;
const qs = {};
const body = {};
if (mode === 'manual') {
qs.limit = 1;
}
else {
body.filter = {
operator: 'and',
fields: [
{ field: triggerField, comparison: 'is_on_or_after', value: { date_mode: 'today', date_mode_value: null } },
],
};
}
const response = await smartSuiteApi_1.apiRequest.call(this, 'POST', path, body, qs);
const rawItems = response.records ?? response.items ?? [];
let filtered = rawItems;
if (mode !== 'manual') {
const lastCheckedRaw = staticData.LastCheckedDate || new Date(0).toISOString();
const lastMs = new Date(lastCheckedRaw).getTime();
filtered = rawItems.filter((item) => {
const on = item[triggerField]?.on;
return typeof on === 'string' && new Date(on).getTime() > lastMs;
});
if (filtered.length) {
const newest = filtered
.map((i) => i[triggerField].on)
.sort()
.pop();
staticData.LastCheckedDate = newest;
(0, utils_1.debugLog)(`[SmartSuiteTrigger] Updated LastCheckedDate = ${newest}`, {}, 3);
}
}
(0, utils_1.debugLog)('[SmartSuiteTrigger] Records →', filtered, 3);
if (filtered.length) {
return [this.helpers.returnJsonArray(filtered)];
}
return null;
}
}
exports.SmartSuiteTrigger = SmartSuiteTrigger;
exports.default = SmartSuiteTrigger;
//# sourceMappingURL=SmartSuiteTrigger.node.js.map