n8n/packages/nodes-base/nodes/SeaTable/SeaTableTrigger.node.ts
Iván Ovejero 70ae90fa3c
refactor: Apply more eslint-plugin-n8n-nodes-base autofixable rules (#3432)
*  Update `lintfix` script

* 👕 Remove unneeded lint exceptions

* 👕 Run baseline `lintfix`

* 👕 Apply `node-param-description-miscased-url` (#3441)

* 👕 Apply `rule node-param-placeholder-miscased-id` (#3443)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-param-option-name-wrong-for-upsert` (#3446)

* 👕 Apply `node-param-min-value-wrong-for-limit` (#3442)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* Apply `node-param-display-name-wrong-for-dynamic-options` (#3454)

* 🔨 fix

*  Fix `Assigned To` fields

Co-authored-by: Michael Kret <michael.k@radency.com>

* 👕 Apply `rule node-param-default-wrong-for-number` (#3453)

* 👕 Apply `node-param-default-wrong-for-string` (#3452)

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* Apply `node-param-display-name-miscased` (#3449)

* 🔨 fix

* 🔨 exceptions

*  review fixes

* 👕 Apply `node-param-description-lowercase-first-char` (#3451)

*  fix

*  review fixes

*  fix

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Apply `node-param-description-wrong-for-dynamic-options` (#3456)

* Rule working as intended

* Add rule

* 🔥 Remove repetitions

* 👕 Add exceptions

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

* 👕 Small fix for `node-param-description-wrong-for-dynamic-options`

* 👕 Apply `node-param-default-wrong-for-fixed-collection` (#3460)

* 👕 Apply `node-param-description-line-break-html-tag` (#3462)

* 👕 Run baseline `lintfix`

* 👕 Apply `node-param-options-type-unsorted-items` (#3459)

*  fix

* 🔨 exceptions

* Add exception for Salesmate and Zoom

Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>

*  Restore `lintfix` command

Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: brianinoa <54530642+brianinoa@users.noreply.github.com>
2022-06-03 19:23:49 +02:00

165 lines
4.1 KiB
TypeScript

import {
IPollFunctions,
} from 'n8n-core';
import {
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
getColumns,
rowFormatColumns,
seaTableApiRequest,
simplify,
} from './GenericFunctions';
import {
ICtx,
IRow,
IRowResponse,
} from './Interfaces';
import moment from 'moment';
export class SeaTableTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'SeaTable Trigger',
name: 'seaTableTrigger',
icon: 'file:seaTable.svg',
group: ['trigger'],
version: 1,
description: 'Starts the workflow when SeaTable events occur',
subtitle: '={{$parameter["event"]}}',
defaults: {
name: 'SeaTable Trigger',
},
credentials: [
{
name: 'seaTableApi',
required: true,
},
],
polling: true,
inputs: [],
outputs: ['main'],
properties: [
{
displayName: 'Table Name or ID',
name: 'tableName',
type: 'options',
required: true,
typeOptions: {
loadOptionsMethod: 'getTableNames',
},
default: '',
description: 'The name of SeaTable table to access. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/nodes/expressions.html#expressions">expression</a>.',
},
{
displayName: 'Event',
name: 'event',
type: 'options',
options: [
{
name: 'Row Created',
value: 'rowCreated',
description: 'Trigger on newly created rows',
},
// {
// name: 'Row Modified',
// value: 'rowModified',
// description: 'Trigger has recently modified rows',
// },
],
default: 'rowCreated',
},
{
displayName: 'Simplify',
name: 'simple',
type: 'boolean',
default: true,
description: 'Whether to return a simplified version of the response instead of the raw data',
},
],
};
methods = {
loadOptions: {
async getTableNames(this: ILoadOptionsFunctions) {
const returnData: INodePropertyOptions[] = [];
const { metadata: { tables } } = await seaTableApiRequest.call(this, {}, 'GET', `/dtable-server/api/v1/dtables/{{dtable_uuid}}/metadata`);
for (const table of tables) {
returnData.push({
name: table.name,
value: table.name,
});
}
return returnData;
},
},
};
async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {
const webhookData = this.getWorkflowStaticData('node');
const tableName = this.getNodeParameter('tableName') as string;
const simple = this.getNodeParameter('simple') as boolean;
const event = this.getNodeParameter('event') as string;
const ctx: ICtx = {};
const credentials = await this.getCredentials('seaTableApi');
const timezone = credentials.timezone as string || 'Europe/Berlin';
const now = moment().utc().format();
const startDate = webhookData.lastTimeChecked as string || now;
const endDate = now;
webhookData.lastTimeChecked = endDate;
let rows;
const filterField = (event === 'rowCreated') ? '_ctime' : '_mtime';
const endpoint = `/dtable-db/api/v1/query/{{dtable_uuid}}/`;
if (this.getMode() === 'manual') {
rows = await seaTableApiRequest.call(this, ctx, 'POST', endpoint, { sql: `SELECT * FROM ${tableName} LIMIT 1` }) as IRowResponse;
} else {
rows = (await seaTableApiRequest.call(this, ctx, 'POST', endpoint, {
sql: `SELECT * FROM ${tableName}
WHERE ${filterField} BETWEEN "${moment(startDate)
.tz(timezone)
.format('YYYY-MM-D HH:mm:ss')}"
AND "${moment(endDate)
.tz(timezone)
.format('YYYY-MM-D HH:mm:ss')}"`,
})) as IRowResponse;
}
let response;
if (rows.metadata && rows.results) {
const columns = getColumns(rows);
if (simple === true) {
response = simplify(rows, columns);
} else {
response = rows.results;
}
const allColumns = rows.metadata.map((meta) => meta.name);
response = response
//@ts-ignore
.map((row: IRow) => rowFormatColumns(row, allColumns))
.map((row: IRow) => ({ json: row }));
}
if (Array.isArray(response) && response.length) {
return [response];
}
return null;
}
}