n8n-nodes-base
Version:
Base nodes of n8n
235 lines • 11.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SeaTableTriggerV2 = void 0;
const moment_timezone_1 = __importDefault(require("moment-timezone"));
const n8n_workflow_1 = require("n8n-workflow");
const GenericFunctions_1 = require("./GenericFunctions");
const methods_1 = require("./methods");
class SeaTableTriggerV2 {
description;
constructor(baseDescription) {
this.description = {
...baseDescription,
version: 2,
subtitle: '={{$parameter["event"]}}',
defaults: {
name: 'SeaTable Trigger',
},
credentials: [
{
name: 'seaTableApi',
required: true,
},
],
polling: true,
inputs: [],
outputs: [n8n_workflow_1.NodeConnectionTypes.Main],
properties: [
{
displayName: 'Event',
name: 'event',
type: 'options',
options: [
{
name: 'New Row',
value: 'newRow',
description: 'Trigger on newly created rows',
},
{
name: 'New or Updated Row',
value: 'updatedRow',
description: 'Trigger has recently created or modified rows',
},
{
name: 'New Signature',
value: 'newAsset',
description: 'Trigger on new signatures',
},
],
default: 'newRow',
},
{
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-wrong-for-dynamic-options
displayName: 'Table Name',
name: 'tableName',
type: 'options',
required: true,
typeOptions: {
loadOptionsMethod: 'getTableNames',
},
default: '',
// eslint-disable-next-line n8n-nodes-base/node-param-description-wrong-for-dynamic-options
description: 'The name of SeaTable table to access. Choose from the list, or specify the name using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-wrong-for-dynamic-options
displayName: 'View Name',
name: 'viewName',
type: 'options',
displayOptions: {
show: {
event: ['newRow', 'updatedRow'],
},
},
typeOptions: {
loadOptionsDependsOn: ['tableName'],
loadOptionsMethod: 'getTableViews',
},
default: '',
// eslint-disable-next-line n8n-nodes-base/node-param-description-wrong-for-dynamic-options
description: 'The name of SeaTable view to access. Choose from the list, or specify the name using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-wrong-for-dynamic-options
displayName: 'Signature Column',
name: 'assetColumn',
type: 'options',
required: true,
displayOptions: {
show: {
event: ['newAsset'],
},
},
typeOptions: {
loadOptionsDependsOn: ['tableName'],
loadOptionsMethod: 'getSignatureColumns',
},
default: '',
// eslint-disable-next-line n8n-nodes-base/node-param-description-wrong-for-dynamic-options
description: 'Select the digital-signature column that should be tracked. Choose from the list, or specify the name using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Option',
default: {},
options: [
{
displayName: 'Simplify',
name: 'simple',
type: 'boolean',
default: true,
description: 'Whether to return a simplified version of the response instead of the raw data',
},
{
displayName: 'Return Column Names',
name: 'convert',
type: 'boolean',
default: true,
description: 'Whether to return the column keys (false) or the column names (true)',
displayOptions: {
show: {
'/event': ['newRow', 'updatedRow'],
},
},
},
],
},
{
displayName: '"Fetch Test Event" returns max. three items of the last hour.',
name: 'notice',
type: 'notice',
default: '',
},
],
};
}
methods = { loadOptions: methods_1.loadOptions };
async poll() {
const webhookData = this.getWorkflowStaticData('node');
const event = this.getNodeParameter('event');
const tableName = this.getNodeParameter('tableName');
const viewName = (event !== 'newAsset' ? this.getNodeParameter('viewName') : '');
const assetColumn = (event === 'newAsset' ? this.getNodeParameter('assetColumn') : '');
const options = this.getNodeParameter('options');
const ctx = {};
const startDate = this.getMode() === 'manual'
? (0, moment_timezone_1.default)().utc().subtract(1, 'h').format()
: webhookData.lastTimeChecked;
const endDate = (webhookData.lastTimeChecked = (0, moment_timezone_1.default)().utc().format());
const filterField = event === 'newRow' ? '_ctime' : '_mtime';
let requestMeta;
let requestRows;
let metadata = [];
let rows;
let sqlResult;
const limit = this.getMode() === 'manual' ? 3 : 1000;
// New Signature
if (event === 'newAsset') {
const endpoint = '/api-gateway/api/v2/dtables/{{dtable_uuid}}/sql';
sqlResult = await GenericFunctions_1.seaTableApiRequest.call(this, ctx, 'POST', endpoint, {
sql: `SELECT _id, _ctime, _mtime, \`${assetColumn}\` FROM ${tableName} WHERE \`${assetColumn}\` IS NOT NULL ORDER BY _mtime DESC LIMIT ${limit}`,
convert_keys: options.convert ?? true,
});
metadata = sqlResult.metadata;
const columnType = metadata.find((obj) => obj.name == assetColumn);
const assetColumnType = columnType?.type || null;
// remove unwanted entries
rows = sqlResult.results.filter((obj) => new Date(obj._mtime) > new Date(startDate));
// split the objects into new lines (not necessary for digital-sign)
const newRows = [];
for (const row of rows) {
if (assetColumnType === 'digital-sign') {
const signature = row[assetColumn] || {
sign_time: undefined,
};
if (signature.sign_time) {
if (new Date(signature.sign_time) > new Date(startDate)) {
newRows.push(signature);
}
}
}
}
}
// View => use getRows.
else if (viewName || viewName === '') {
requestMeta = await GenericFunctions_1.seaTableApiRequest.call(this, ctx, 'GET', '/api-gateway/api/v2/dtables/{{dtable_uuid}}/metadata/');
requestRows = await GenericFunctions_1.seaTableApiRequest.call(this, ctx, 'GET', '/api-gateway/api/v2/dtables/{{dtable_uuid}}/rows/', {}, {
table_name: tableName,
view_name: viewName,
limit,
convert_keys: options.convert ?? true,
});
metadata =
requestMeta.metadata.tables.find((table) => table.name === tableName)?.columns ?? [];
// remove unwanted rows that are too old (compare startDate with _ctime or _mtime)
if (this.getMode() === 'manual') {
rows = requestRows.rows;
}
else {
rows = requestRows.rows.filter((obj) => new Date(obj[filterField]) > new Date(startDate));
}
}
else {
const endpoint = '/api-gateway/api/v2/dtables/{{dtable_uuid}}/sql';
const sqlQuery = `SELECT * FROM \`${tableName}\` WHERE ${filterField} BETWEEN "${(0, moment_timezone_1.default)(startDate).format('YYYY-MM-D HH:mm:ss')}" AND "${(0, moment_timezone_1.default)(endDate).format('YYYY-MM-D HH:mm:ss')}" ORDER BY ${filterField} DESC LIMIT ${limit}`;
sqlResult = await GenericFunctions_1.seaTableApiRequest.call(this, ctx, 'POST', endpoint, {
sql: sqlQuery,
convert_keys: options.convert ?? true,
});
metadata = sqlResult.metadata;
rows = sqlResult.results;
}
const collaboratorsResult = await GenericFunctions_1.seaTableApiRequest.call(this, ctx, 'GET', '/api-gateway/api/v2/dtables/{{dtable_uuid}}/related-users/');
const collaborators = collaboratorsResult.user_list || [];
if (Array.isArray(rows) && rows.length > 0) {
const simple = options.simple ?? true;
// remove columns starting with _ if simple;
if (simple) {
rows = rows.map((row) => (0, GenericFunctions_1.simplify_new)(row));
}
// enrich column types like {collaborator, creator, last_modifier}, {image, file}
// remove button column from rows
rows = rows.map((row) => (0, GenericFunctions_1.enrichColumns)(row, metadata, collaborators));
// prepare for final output
return [this.helpers.returnJsonArray(rows)];
}
return null;
}
}
exports.SeaTableTriggerV2 = SeaTableTriggerV2;
//# sourceMappingURL=SeaTableTriggerV2.node.js.map