2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2020-11-25 02:44:50 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
profitWellApiRequest,
|
|
|
|
simplifyDailyMetrics,
|
|
|
|
simplifyMontlyMetrics,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { companyOperations } from './CompanyDescription';
|
2020-11-25 02:44:50 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { metricFields, metricOperations } from './MetricDescription';
|
2020-11-25 02:44:50 -08:00
|
|
|
|
|
|
|
export class ProfitWell implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'ProfitWell',
|
|
|
|
name: 'profitWell',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-11-25 02:44:50 -08:00
|
|
|
icon: 'file:profitwell.png',
|
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume ProfitWell API',
|
|
|
|
defaults: {
|
|
|
|
name: 'ProfitWell',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'profitWellApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-11-25 02:44:50 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Company',
|
|
|
|
value: 'company',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Metric',
|
|
|
|
value: 'metric',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'metric',
|
|
|
|
},
|
|
|
|
// COMPANY
|
|
|
|
...companyOperations,
|
|
|
|
// METRICS
|
|
|
|
...metricOperations,
|
|
|
|
...metricFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2022-08-17 08:50:24 -07:00
|
|
|
async getPlanIds(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2020-11-25 02:44:50 -08:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const planIds = await profitWellApiRequest.call(this, 'GET', '/metrics/plans');
|
2020-11-25 02:44:50 -08:00
|
|
|
for (const planId of planIds.plan_ids) {
|
|
|
|
returnData.push({
|
|
|
|
name: planId,
|
|
|
|
value: planId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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-11-25 02:44:50 -08:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-11-25 02:44:50 -08:00
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'company') {
|
|
|
|
if (operation === 'getSetting') {
|
|
|
|
responseData = await profitWellApiRequest.call(this, 'GET', `/company/settings/`);
|
|
|
|
}
|
2020-11-25 02:44:50 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'metric') {
|
|
|
|
if (operation === 'get') {
|
|
|
|
const type = this.getNodeParameter('type', i) as string;
|
2020-11-25 02:44:50 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const simple = this.getNodeParameter('simple', 0) as boolean;
|
2020-11-25 02:44:50 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (type === 'daily') {
|
|
|
|
qs.month = this.getNodeParameter('month', i) as string;
|
|
|
|
}
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2020-11-25 02:44:50 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, options);
|
2020-11-25 02:44:50 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (qs.dailyMetrics) {
|
|
|
|
qs.metrics = (qs.dailyMetrics as string[]).join(',');
|
|
|
|
delete qs.dailyMetrics;
|
|
|
|
}
|
2020-11-25 02:44:50 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (qs.monthlyMetrics) {
|
|
|
|
qs.metrics = (qs.monthlyMetrics as string[]).join(',');
|
|
|
|
delete qs.monthlyMetrics;
|
|
|
|
}
|
2020-11-25 02:44:50 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await profitWellApiRequest.call(this, 'GET', `/metrics/${type}`, {}, qs);
|
|
|
|
responseData = responseData.data;
|
2020-11-25 02:44:50 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (simple === true) {
|
|
|
|
if (type === 'daily') {
|
|
|
|
responseData = simplifyDailyMetrics(responseData);
|
|
|
|
} else {
|
|
|
|
responseData = simplifyMontlyMetrics(responseData);
|
|
|
|
}
|
2020-11-25 02:44:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
} else {
|
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-11-25 02:44:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|