2022-08-17 08:50:24 -07:00
import { IPollFunctions } from 'n8n-core' ;
2021-09-29 16:28:27 -07:00
import {
ILoadOptionsFunctions ,
INodeExecutionData ,
INodePropertyOptions ,
INodeType ,
INodeTypeDescription ,
} from 'n8n-workflow' ;
2022-08-17 08:50:24 -07:00
import { getColumns , rowFormatColumns , seaTableApiRequest , simplify } from './GenericFunctions' ;
2021-09-29 16:28:27 -07:00
2022-08-17 08:50:24 -07:00
import { ICtx , IRow , IRowResponse } from './Interfaces' ;
2021-09-29 16:28:27 -07:00
2022-04-08 14:32:08 -07:00
import moment from 'moment' ;
2021-09-29 16:28:27 -07:00
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 : [
{
2022-06-03 10:23:49 -07:00
displayName : 'Table Name or ID' ,
2021-09-29 16:28:27 -07:00
name : 'tableName' ,
type : 'options' ,
required : true ,
typeOptions : {
loadOptionsMethod : 'getTableNames' ,
} ,
default : '' ,
2022-08-17 08:50:24 -07:00
description :
'The name of SeaTable table to access. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.' ,
2021-09-29 16:28:27 -07:00
} ,
{
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' ,
} ,
{
2022-05-20 14:47:24 -07:00
displayName : 'Simplify' ,
2021-09-29 16:28:27 -07:00
name : 'simple' ,
type : 'boolean' ,
default : true ,
2022-08-17 08:50:24 -07:00
description :
'Whether to return a simplified version of the response instead of the raw data' ,
2021-09-29 16:28:27 -07:00
} ,
] ,
} ;
methods = {
loadOptions : {
async getTableNames ( this : ILoadOptionsFunctions ) {
const returnData : INodePropertyOptions [ ] = [ ] ;
2022-08-17 08:50:24 -07:00
const {
metadata : { tables } ,
} = await seaTableApiRequest . call (
this ,
{ } ,
'GET' ,
` /dtable-server/api/v1/dtables/{{dtable_uuid}}/metadata ` ,
) ;
2021-09-29 16:28:27 -07:00
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 = { } ;
2022-04-14 23:00:47 -07:00
const credentials = await this . getCredentials ( 'seaTableApi' ) ;
2021-09-29 16:28:27 -07:00
2022-08-17 08:50:24 -07:00
const timezone = ( credentials . timezone as string ) || 'Europe/Berlin' ;
2021-09-29 16:28:27 -07:00
const now = moment ( ) . utc ( ) . format ( ) ;
2022-08-17 08:50:24 -07:00
const startDate = ( webhookData . lastTimeChecked as string ) || now ;
2021-09-29 16:28:27 -07:00
const endDate = now ;
webhookData . lastTimeChecked = endDate ;
let rows ;
2022-08-17 08:50:24 -07:00
const filterField = event === 'rowCreated' ? '_ctime' : '_mtime' ;
2021-09-29 16:28:27 -07:00
const endpoint = ` /dtable-db/api/v1/query/{{dtable_uuid}}/ ` ;
if ( this . getMode ( ) === 'manual' ) {
2022-08-17 08:50:24 -07:00
rows = ( await seaTableApiRequest . call ( this , ctx , 'POST' , endpoint , {
sql : ` SELECT * FROM ${ tableName } LIMIT 1 ` ,
} ) ) as IRowResponse ;
2021-09-29 16:28:27 -07:00
} else {
2022-03-13 01:45:26 -08:00
rows = ( await seaTableApiRequest . call ( this , ctx , 'POST' , endpoint , {
sql : ` SELECT * FROM ${ tableName }
2022-08-17 08:50:24 -07:00
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')}" ` ,
2022-03-13 01:45:26 -08:00
} ) ) as IRowResponse ;
2021-09-29 16:28:27 -07:00
}
let response ;
if ( rows . metadata && rows . results ) {
const columns = getColumns ( rows ) ;
2022-12-02 12:54:28 -08:00
if ( simple ) {
2021-09-29 16:28:27 -07:00
response = simplify ( rows , columns ) ;
} else {
response = rows . results ;
}
const allColumns = rows . metadata . map ( ( meta ) = > meta . name ) ;
response = response
2022-08-17 08:50:24 -07:00
//@ts-ignore
. map ( ( row : IRow ) = > rowFormatColumns ( row , allColumns ) )
. map ( ( row : IRow ) = > ( { json : row } ) ) ;
2021-09-29 16:28:27 -07:00
}
if ( Array . isArray ( response ) && response . length ) {
return [ response ] ;
}
return null ;
}
}