2022-08-17 08:50:24 -07:00
import { IExecuteFunctions } from 'n8n-core' ;
2020-12-11 23:58:58 -08:00
import {
IDataObject ,
ILoadOptionsFunctions ,
INodeExecutionData ,
INodePropertyOptions ,
INodeType ,
INodeTypeDescription ,
} from 'n8n-workflow' ;
2022-08-17 08:50:24 -07:00
import { pushcutApiRequest } from './GenericFunctions' ;
2020-12-11 23:58:58 -08:00
export class Pushcut implements INodeType {
description : INodeTypeDescription = {
displayName : 'Pushcut' ,
name : 'pushcut' ,
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
2020-12-11 23:58:58 -08:00
icon : 'file:pushcut.png' ,
group : [ 'input' ] ,
version : 1 ,
subtitle : '={{$parameter["operation"] + ": " + $parameter["resource"]}}' ,
2021-07-03 05:40:16 -07:00
description : 'Consume Pushcut API' ,
2020-12-11 23:58:58 -08:00
defaults : {
name : 'Pushcut' ,
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'pushcutApi' ,
required : true ,
} ,
] ,
properties : [
{
displayName : 'Resource' ,
name : 'resource' ,
type : 'options' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2020-12-11 23:58:58 -08:00
options : [
{
name : 'Notification' ,
value : 'notification' ,
} ,
] ,
default : 'notification' ,
} ,
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2020-12-11 23:58:58 -08:00
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'notification' ] ,
2020-12-11 23:58:58 -08:00
} ,
} ,
options : [
{
name : 'Send' ,
value : 'send' ,
description : 'Send a notification' ,
2022-07-10 13:50:51 -07:00
action : 'Send a notification' ,
2020-12-11 23:58:58 -08:00
} ,
] ,
default : 'send' ,
} ,
{
2022-06-03 10:23:49 -07:00
displayName : 'Notification Name or ID' ,
2020-12-11 23:58:58 -08:00
name : 'notificationName' ,
type : 'options' ,
2022-08-17 08:50:24 -07:00
description :
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>' ,
2020-12-11 23:58:58 -08:00
typeOptions : {
loadOptionsMethod : 'getNotifications' ,
} ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'notification' ] ,
operation : [ 'send' ] ,
2020-12-11 23:58:58 -08:00
} ,
} ,
2020-12-11 23:59:25 -08:00
default : '' ,
2020-12-11 23:58:58 -08:00
} ,
{
displayName : 'Additional Fields' ,
name : 'additionalFields' ,
type : 'collection' ,
placeholder : 'Add Field' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
operation : [ 'send' ] ,
resource : [ 'notification' ] ,
2020-12-11 23:58:58 -08:00
} ,
} ,
default : { } ,
options : [
{
2022-06-20 07:54:01 -07:00
displayName : 'Device Names or IDs' ,
2020-12-11 23:58:58 -08:00
name : 'devices' ,
type : 'multiOptions' ,
typeOptions : {
loadOptionsMethod : 'getDevices' ,
} ,
2022-04-22 09:29:51 -07:00
default : [ ] ,
2022-08-17 08:50:24 -07:00
description :
'List of devices this notification is sent to. (default is all devices). Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.' ,
2020-12-11 23:58:58 -08:00
} ,
{
displayName : 'Input' ,
name : 'input' ,
type : 'string' ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Value that is passed as input to the notification action' ,
2020-12-11 23:58:58 -08:00
} ,
{
displayName : 'Text' ,
name : 'text' ,
type : 'string' ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Text that is used instead of the one defined in the app' ,
2020-12-11 23:58:58 -08:00
} ,
{
displayName : 'Title' ,
name : 'title' ,
type : 'string' ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Title that is used instead of the one defined in the app' ,
2020-12-11 23:58:58 -08:00
} ,
] ,
} ,
] ,
} ;
methods = {
loadOptions : {
// Get all the available devices to display them to user so that he can
// select them easily
async getDevices ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
const returnData : INodePropertyOptions [ ] = [ ] ;
const devices = await pushcutApiRequest . call ( this , 'GET' , '/devices' ) ;
for ( const device of devices ) {
returnData . push ( {
name : device.id ,
value : device.id ,
} ) ;
}
return returnData ;
} ,
// Get all the available notifications to display them to user so that he can
// select them easily
async getNotifications ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
const returnData : INodePropertyOptions [ ] = [ ] ;
const notifications = await pushcutApiRequest . call ( this , 'GET' , '/notifications' ) ;
for ( const notification of notifications ) {
returnData . push ( {
name : notification.title ,
value : notification.id ,
} ) ;
}
return returnData ;
} ,
} ,
} ;
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
const items = this . getInputData ( ) ;
const returnData : IDataObject [ ] = [ ] ;
2022-04-22 09:29:51 -07:00
const length = items . length ;
2020-12-11 23:58:58 -08:00
let responseData ;
const resource = this . getNodeParameter ( 'resource' , 0 ) as string ;
const operation = this . getNodeParameter ( 'operation' , 0 ) as string ;
for ( let i = 0 ; i < length ; i ++ ) {
if ( resource === 'notification' ) {
if ( operation === 'send' ) {
const notificationName = this . getNodeParameter ( 'notificationName' , i ) as string ;
const additionalFields = this . getNodeParameter ( 'additionalFields' , i ) as IDataObject ;
const body : IDataObject = { } ;
Object . assign ( body , additionalFields ) ;
responseData = await pushcutApiRequest . call (
this ,
'POST' ,
` /notifications/ ${ encodeURI ( notificationName ) } ` ,
body ,
) ;
}
}
}
if ( Array . isArray ( responseData ) ) {
returnData . push . apply ( returnData , responseData as IDataObject [ ] ) ;
} else if ( responseData !== undefined ) {
returnData . push ( responseData as IDataObject ) ;
}
return [ this . helpers . returnJsonArray ( returnData ) ] ;
}
}