2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2023-01-27 03:22:44 -08:00
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2020-11-24 14:19:41 -08:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment';
|
2022-08-17 08:50:24 -07:00
|
|
|
import { spontitApiRequest } from './GenericFunctions';
|
2020-11-24 14:19:41 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { pushFields, pushOperations } from './PushDescription';
|
2020-11-24 14:19:41 -08:00
|
|
|
|
|
|
|
export class Spontit implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Spontit',
|
|
|
|
name: 'spontit',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-11-24 14:19:41 -08:00
|
|
|
icon: 'file:spontit.png',
|
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Spontit API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Spontit',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'spontitApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-11-24 14:19:41 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Push',
|
|
|
|
value: 'push',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'push',
|
|
|
|
},
|
|
|
|
...pushOperations,
|
|
|
|
...pushFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
const timezone = this.getTimezone();
|
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-11-24 14:19:41 -08:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'push') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const content = this.getNodeParameter('content', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2020-11-24 14:19:41 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const body: IDataObject = {
|
|
|
|
content,
|
|
|
|
};
|
2020-11-24 14:19:41 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, additionalFields);
|
2020-11-24 14:19:41 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.pushToFollowers) {
|
|
|
|
body.pushToFollowers = (body.pushToFollowers as string).split(',');
|
|
|
|
}
|
2020-11-24 14:19:41 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.pushToPhoneNumbers) {
|
|
|
|
body.pushToPhoneNumbers = (body.pushToPhoneNumbers as string).split(',');
|
|
|
|
}
|
2020-11-24 14:19:41 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.pushToEmails) {
|
|
|
|
body.pushToEmails = (body.pushToEmails as string).split(',');
|
|
|
|
}
|
2020-11-24 14:19:41 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.schedule) {
|
|
|
|
body.scheduled = moment.tz(body.schedule, timezone).unix();
|
|
|
|
}
|
2020-11-24 14:19:41 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (body.expirationStamp) {
|
|
|
|
body.expirationStamp = moment.tz(body.expirationStamp, timezone).unix();
|
|
|
|
}
|
2020-11-24 14:19:41 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await spontitApiRequest.call(this, 'POST', '/push', body);
|
2020-11-24 14:19:41 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.data;
|
|
|
|
}
|
2020-11-24 14:19:41 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
} else if (responseData !== undefined) {
|
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-11-24 14:19:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|