2020-12-19 05:08:31 -08:00
import {
IExecuteFunctions ,
} from 'n8n-core' ;
import {
IDataObject ,
ILoadOptionsFunctions ,
INodeExecutionData ,
INodePropertyOptions ,
INodeType ,
INodeTypeDescription ,
} from 'n8n-workflow' ;
import {
egoiApiRequest ,
egoiApiRequestAllItems ,
simplify ,
} from './GenericFunctions' ;
import {
ICreateMemberBody ,
} from './Interfaces' ;
2022-04-08 14:32:08 -07:00
import moment from 'moment-timezone' ;
2020-12-19 05:08:31 -08:00
export class Egoi implements INodeType {
description : INodeTypeDescription = {
2021-01-04 23:46:07 -08:00
displayName : 'E-goi' ,
2020-12-19 05:08:31 -08:00
name : 'egoi' ,
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
2020-12-19 05:08:31 -08:00
icon : 'file:egoi.png' ,
group : [ 'output' ] ,
version : 1 ,
subtitle : '={{$parameter["operation"] + ": " + $parameter["resource"]}}' ,
2021-01-04 23:46:07 -08:00
description : 'Consume E-goi API' ,
2020-12-19 05:08:31 -08:00
defaults : {
2021-01-04 23:46:07 -08:00
name : 'E-goi' ,
2020-12-19 05:08:31 -08:00
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'egoiApi' ,
required : true ,
} ,
] ,
properties : [
{
displayName : 'Resource' ,
name : 'resource' ,
type : 'options' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2020-12-19 05:08:31 -08:00
required : true ,
options : [
{
name : 'Contact' ,
value : 'contact' ,
} ,
] ,
default : 'contact' ,
} ,
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2020-12-19 05:08:31 -08:00
required : true ,
options : [
{
name : 'Create' ,
value : 'create' ,
description : 'Create a member' ,
} ,
{
name : 'Get' ,
value : 'get' ,
description : 'Get a member' ,
} ,
{
name : 'Get All' ,
value : 'getAll' ,
description : 'Get all members' ,
} ,
{
name : 'Update' ,
value : 'update' ,
description : 'Update a member' ,
} ,
] ,
default : 'create' ,
} ,
{
2022-06-03 10:23:49 -07:00
displayName : 'List Name or ID' ,
2020-12-19 05:08:31 -08:00
name : 'list' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getLists' ,
} ,
displayOptions : {
show : {
operation : [
'getAll' ,
'create' ,
'update' ,
'get' ,
] ,
} ,
} ,
default : '' ,
2022-06-03 10:23:49 -07:00
description : 'ID of list to operate on. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/nodes/expressions.html#expressions">expression</a>.' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Email' ,
name : 'email' ,
type : 'string' ,
2022-06-20 07:54:01 -07:00
placeholder : 'name@email.com' ,
2020-12-19 05:08:31 -08:00
displayOptions : {
show : {
operation : [
'create' ,
] ,
} ,
} ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Email address for a subscriber' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Contact ID' ,
name : 'contactId' ,
type : 'string' ,
displayOptions : {
show : {
resource : [
'contact' ,
] ,
operation : [
'update' ,
] ,
} ,
} ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Contact ID of the subscriber' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Resolve Data' ,
name : 'resolveData' ,
type : 'boolean' ,
displayOptions : {
show : {
operation : [
'create' ,
'update' ,
] ,
} ,
} ,
default : true ,
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
2022-05-06 14:01:25 -07:00
description : 'By default the response just includes the contact ID. If this option gets activated, it will resolve the data automatically.' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Additional Fields' ,
name : 'additionalFields' ,
type : 'collection' ,
placeholder : 'Add Field' ,
displayOptions : {
show : {
operation : [
'create' ,
] ,
resource : [
'contact' ,
] ,
} ,
} ,
default : { } ,
options : [
{
displayName : 'Birth Date' ,
name : 'birth_date' ,
type : 'dateTime' ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Birth date of a subscriber' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Cellphone' ,
name : 'cellphone' ,
type : 'string' ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Cellphone of a subscriber' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Extra Fields' ,
name : 'extraFieldsUi' ,
type : 'fixedCollection' ,
placeholder : 'Add Field' ,
default : { } ,
typeOptions : {
multipleValues : true ,
} ,
options : [
{
displayName : 'Extra Field' ,
name : 'extraFieldValues' ,
typeOptions : {
multipleValueButtonText : 'Add Field' ,
} ,
values : [
{
2022-06-03 10:23:49 -07:00
displayName : 'Field Name or ID' ,
2020-12-19 05:08:31 -08:00
name : 'field_id' ,
type : 'options' ,
2022-06-20 07:54:01 -07:00
description : 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/nodes/expressions.html#expressions">expression</a>' ,
2020-12-19 05:08:31 -08:00
typeOptions : {
loadOptionsMethod : 'getExtraFields' ,
loadOptionsDependsOn : [
'list' ,
] ,
} ,
default : '' ,
} ,
{
displayName : 'Value' ,
name : 'value' ,
type : 'string' ,
default : '' ,
} ,
] ,
} ,
] ,
} ,
{
displayName : 'First Name' ,
name : 'first_name' ,
type : 'string' ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Name of a subscriber' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Last Name' ,
name : 'last_name' ,
type : 'string' ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Name of a subscriber' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Status' ,
name : 'status' ,
type : 'options' ,
options : [
{
name : 'Unconfirmed' ,
value : 'unconfirmed' ,
} ,
{
name : 'Active' ,
value : 'active' ,
} ,
{
name : 'Inactive' ,
value : 'inactive' ,
} ,
{
name : 'Removed' ,
value : 'removed' ,
} ,
] ,
default : 'active' ,
2022-05-06 14:01:25 -07:00
description : 'Subscriber\'s current status' ,
2020-12-19 05:08:31 -08:00
} ,
{
2022-06-20 07:54:01 -07:00
displayName : 'Tag Names or IDs' ,
2020-12-19 05:08:31 -08:00
name : 'tagIds' ,
type : 'multiOptions' ,
typeOptions : {
loadOptionsMethod : 'getListTags' ,
} ,
default : [ ] ,
2022-06-20 07:54:01 -07:00
description : 'List of tag IDs to be added. Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/nodes/expressions.html#expressions">expression</a>.' ,
2020-12-19 05:08:31 -08:00
} ,
] ,
} ,
//--------------------
//----UPDATE MEMBER---
//--------------------
{
displayName : 'Update Fields' ,
name : 'updateFields' ,
type : 'collection' ,
placeholder : 'Add Field' ,
default : { } ,
displayOptions : {
show : {
operation : [
'update' ,
] ,
} ,
} ,
options : [
{
displayName : 'Birth Date' ,
name : 'birth_date' ,
type : 'dateTime' ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Birth date of subscriber' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Cellphone' ,
name : 'cellphone' ,
type : 'string' ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Cellphone of subscriber' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Email' ,
name : 'email' ,
type : 'string' ,
2022-06-20 07:54:01 -07:00
placeholder : 'name@email.com' ,
2020-12-19 05:08:31 -08:00
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Email address for subscriber' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Extra Fields' ,
name : 'extraFieldsUi' ,
type : 'fixedCollection' ,
placeholder : 'Add Field' ,
default : { } ,
typeOptions : {
multipleValues : true ,
} ,
options : [
{
displayName : 'Extra Field' ,
name : 'extraFieldValues' ,
typeOptions : {
multipleValueButtonText : 'Add Field' ,
} ,
values : [
{
2022-06-03 10:23:49 -07:00
displayName : 'Field Name or ID' ,
2020-12-19 05:08:31 -08:00
name : 'field_id' ,
type : 'options' ,
2022-06-20 07:54:01 -07:00
description : 'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/nodes/expressions.html#expressions">expression</a>' ,
2020-12-19 05:08:31 -08:00
typeOptions : {
loadOptionsMethod : 'getExtraFields' ,
loadOptionsDependsOn : [
'list' ,
] ,
} ,
default : '' ,
} ,
{
displayName : 'Value' ,
name : 'value' ,
type : 'string' ,
default : '' ,
} ,
] ,
} ,
] ,
} ,
{
displayName : 'First Name' ,
name : 'first_name' ,
type : 'string' ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Name of subscriber' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Last Name' ,
name : 'last_name' ,
type : 'string' ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Name of subscriber' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Status' ,
name : 'status' ,
type : 'options' ,
options : [
{
name : 'Unconfirmed' ,
value : 'unconfirmed' ,
} ,
{
name : 'Active' ,
value : 'active' ,
} ,
{
name : 'Inactive' ,
value : 'inactive' ,
} ,
{
name : 'Removed' ,
value : 'removed' ,
} ,
] ,
default : 'active' ,
2022-05-06 14:01:25 -07:00
description : 'Subscriber\'s current status' ,
2020-12-19 05:08:31 -08:00
} ,
{
2022-06-20 07:54:01 -07:00
displayName : 'Tag Names or IDs' ,
2020-12-19 05:08:31 -08:00
name : 'tagIds' ,
type : 'multiOptions' ,
typeOptions : {
loadOptionsMethod : 'getListTags' ,
} ,
default : [ ] ,
2022-06-20 07:54:01 -07:00
description : 'List of tag IDs to be added. Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/nodes/expressions.html#expressions">expression</a>.' ,
2020-12-19 05:08:31 -08:00
} ,
] ,
} ,
{
displayName : 'By' ,
name : 'by' ,
type : 'options' ,
options : [
{
name : 'Contact ID' ,
value : 'id' ,
} ,
{
name : 'Email' ,
value : 'email' ,
} ,
] ,
displayOptions : {
show : {
operation : [
'get' ,
] ,
resource : [
'contact' ,
] ,
} ,
} ,
default : 'id' ,
description : 'Search by' ,
} ,
{
displayName : 'Contact ID' ,
name : 'contactId' ,
type : 'string' ,
displayOptions : {
show : {
resource : [
'contact' ,
] ,
operation : [
'get' ,
] ,
by : [
'id' ,
] ,
} ,
} ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Contact ID of the subscriber' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Email' ,
name : 'email' ,
type : 'string' ,
2022-06-20 07:54:01 -07:00
placeholder : 'name@email.com' ,
2020-12-19 05:08:31 -08:00
displayOptions : {
show : {
resource : [
'contact' ,
] ,
operation : [
'get' ,
] ,
by : [
'email' ,
] ,
} ,
} ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'Email address for subscriber' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Return All' ,
name : 'returnAll' ,
type : 'boolean' ,
displayOptions : {
show : {
operation : [
'getAll' ,
] ,
resource : [
'contact' ,
] ,
} ,
} ,
default : false ,
2022-05-06 14:01:25 -07:00
description : 'Whether to return all results or only up to a given limit' ,
2020-12-19 05:08:31 -08:00
} ,
{
displayName : 'Limit' ,
name : 'limit' ,
type : 'number' ,
displayOptions : {
show : {
operation : [
'getAll' ,
] ,
resource : [
'contact' ,
] ,
returnAll : [
false ,
] ,
} ,
} ,
typeOptions : {
minValue : 1 ,
maxValue : 500 ,
} ,
default : 100 ,
2022-05-06 14:01:25 -07:00
description : 'Max number of results to return' ,
2020-12-19 05:08:31 -08:00
} ,
{
2022-05-20 14:47:24 -07:00
displayName : 'Simplify' ,
2020-12-19 05:08:31 -08:00
name : 'simple' ,
type : 'boolean' ,
displayOptions : {
show : {
operation : [
'get' ,
'getAll' ,
] ,
resource : [
'contact' ,
] ,
} ,
} ,
default : true ,
2022-05-20 14:47:24 -07:00
description : 'Whether to return a simplified version of the response instead of the raw data' ,
2020-12-19 05:08:31 -08:00
} ,
] ,
} ;
methods = {
loadOptions : {
async getLists ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
const returnData : INodePropertyOptions [ ] = [ ] ;
const lists = await egoiApiRequestAllItems . call ( this , 'items' , 'GET' , '/lists' ) ;
for ( const list of lists ) {
const listName = list . internal_name ;
const listId = list . list_id ;
returnData . push ( {
name : listName ,
value : listId ,
} ) ;
}
return returnData ;
} ,
async getExtraFields ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
const returnData : INodePropertyOptions [ ] = [ ] ;
const listId = this . getCurrentNodeParameter ( 'list' ) ;
const extraFields = await egoiApiRequest . call ( this , 'GET' , ` /lists/ ${ listId } /fields ` ) ;
for ( const field of extraFields ) {
if ( field . type === 'extra' ) {
const fieldName = field . name ;
const fieldId = field . field_id ;
returnData . push ( {
name : fieldName ,
value : fieldId ,
} ) ;
}
}
return returnData ;
} ,
async getListTags ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
const returnData : INodePropertyOptions [ ] = [ ] ;
const tagList = await egoiApiRequestAllItems . call ( this , 'items' , 'GET' , '/tags' ) ;
for ( const tag of tagList ) {
const tagName = tag . name ;
const tagId = tag . tag_id ;
returnData . push ( {
name : tagName ,
value : tagId ,
} ) ;
}
return returnData ;
} ,
} ,
} ;
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
let responseData ;
const returnData : IDataObject [ ] = [ ] ;
const items = this . getInputData ( ) ;
2022-04-22 09:29:51 -07:00
const length = items . length ;
2020-12-19 05:08:31 -08:00
const operation = this . getNodeParameter ( 'operation' , 0 ) as string ;
const resource = this . getNodeParameter ( 'resource' , 0 ) as string ;
for ( let i = 0 ; i < length ; i ++ ) {
try {
if ( resource === 'contact' ) {
if ( operation === 'create' ) {
const listId = this . getNodeParameter ( 'list' , i ) as string ;
const email = this . getNodeParameter ( 'email' , i ) as string ;
const resolveData = this . getNodeParameter ( 'resolveData' , i ) as boolean ;
const additionalFields = this . getNodeParameter ( 'additionalFields' , i ) as IDataObject ;
const body : ICreateMemberBody = {
base : {
email ,
} ,
extra : [ ] ,
} ;
if ( additionalFields . birth_date ) {
additionalFields . birth_date = moment ( additionalFields . birth_date as string ) . format ( 'YYYY-MM-DD' ) ;
}
if ( additionalFields . extraFieldsUi ) {
const extraFields = ( additionalFields . extraFieldsUi as IDataObject ) . extraFieldValues as IDataObject [ ] ;
if ( extraFields ) {
body . extra = extraFields as unknown as [ ] ;
}
}
Object . assign ( body . base , additionalFields ) ;
responseData = await egoiApiRequest . call ( this , 'POST' , ` /lists/ ${ listId } /contacts ` , body ) ;
const contactId = responseData . contact_id ;
if ( additionalFields . tagIds ) {
const tags = additionalFields . tagIds as string [ ] ;
for ( const tag of tags ) {
await egoiApiRequest . call ( this , 'POST' , ` /lists/ ${ listId } /contacts/actions/attach-tag ` , { tag_id : tag , contacts : [ contactId ] } ) ;
}
}
if ( resolveData ) {
responseData = await egoiApiRequest . call ( this , 'GET' , ` /lists/ ${ listId } /contacts/ ${ contactId } ` ) ;
}
}
if ( operation === 'get' ) {
const listId = this . getNodeParameter ( 'list' , i ) as string ;
const simple = this . getNodeParameter ( 'simple' , i ) as boolean ;
const by = this . getNodeParameter ( 'by' , 0 ) as string ;
let endpoint = '' ;
if ( by === 'id' ) {
const contactId = this . getNodeParameter ( 'contactId' , i ) as string ;
endpoint = ` /lists/ ${ listId } /contacts/ ${ contactId } ` ;
} else {
const email = this . getNodeParameter ( 'email' , i ) as string ;
endpoint = ` /lists/ ${ listId } /contacts?email= ${ email } ` ;
}
responseData = await egoiApiRequest . call ( this , 'GET' , endpoint , { } ) ;
if ( responseData . items ) {
responseData = responseData . items ;
}
if ( simple === true ) {
const data = ( await simplify . call ( this , [ responseData ] , listId ) ) [ 0 ] ;
responseData = {
. . . data ,
email_stats : responseData.email_stats ,
sms_stats : responseData.sms_stats ,
push_stats : responseData.push_stats ,
webpush_stats : responseData.webpush_stats ,
voice_stats : responseData.voice_stats ,
2020-12-19 14:03:24 -08:00
} ;
2020-12-19 05:08:31 -08:00
}
}
if ( operation === 'getAll' ) {
const listId = this . getNodeParameter ( 'list' , i ) as string ;
const returnAll = this . getNodeParameter ( 'returnAll' , 0 ) as boolean ;
const simple = this . getNodeParameter ( 'simple' , i ) as boolean ;
if ( returnAll ) {
responseData = await egoiApiRequestAllItems . call ( this , 'items' , 'GET' , ` /lists/ ${ listId } /contacts ` , { } ) ;
} else {
const limit = this . getNodeParameter ( 'limit' , i ) as number ;
responseData = await egoiApiRequest . call ( this , 'GET' , ` /lists/ ${ listId } /contacts ` , { } , { limit } ) ;
responseData = responseData . items ;
}
if ( simple === true ) {
responseData = await simplify . call ( this , responseData , listId ) ;
}
}
if ( operation === 'update' ) {
const listId = this . getNodeParameter ( 'list' , i ) as string ;
const contactId = this . getNodeParameter ( 'contactId' , i ) as string ;
const resolveData = this . getNodeParameter ( 'resolveData' , i ) as boolean ;
const updateFields = this . getNodeParameter ( 'updateFields' , i ) as IDataObject ;
const body : ICreateMemberBody = {
base : {
} ,
extra : [ ] ,
} ;
if ( updateFields . birth_date ) {
updateFields . birth_date = moment ( updateFields . birth_date as string ) . format ( 'YYYY-MM-DD' ) ;
}
if ( updateFields . extraFieldsUi ) {
const extraFields = ( updateFields . extraFieldsUi as IDataObject ) . extraFieldValues as IDataObject [ ] ;
if ( extraFields ) {
body . extra = extraFields as unknown as [ ] ;
}
}
Object . assign ( body . base , updateFields ) ;
responseData = await egoiApiRequest . call ( this , 'PATCH' , ` /lists/ ${ listId } /contacts/ ${ contactId } ` , body ) ;
if ( updateFields . tagIds ) {
const tags = updateFields . tagIds as string [ ] ;
for ( const tag of tags ) {
await egoiApiRequest . call ( this , 'POST' , ` /lists/ ${ listId } /contacts/actions/attach-tag ` , { tag_id : tag , contacts : [ contactId ] } ) ;
}
}
if ( resolveData ) {
responseData = await egoiApiRequest . call ( this , 'GET' , ` /lists/ ${ listId } /contacts/ ${ contactId } ` ) ;
}
}
}
} catch ( error ) {
if ( this . continueOnFail ( ) !== true ) {
throw error ;
} else {
// Return the actual reason as error
returnData . push (
{
error : error.message ,
} ,
) ;
continue ;
}
}
if ( Array . isArray ( responseData ) ) {
returnData . push . apply ( returnData , responseData as IDataObject [ ] ) ;
} else {
returnData . push ( responseData as IDataObject ) ;
}
}
return [ this . helpers . returnJsonArray ( returnData ) ] ;
}
}