2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-11-25 02:44:50 -08:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2022-11-08 06:28:21 -08:00
|
|
|
import { IDataObject, NodeApiError } from 'n8n-workflow';
|
2020-11-25 02:44:50 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function profitWellApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2020-11-25 02:44:50 -08:00
|
|
|
try {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('profitWellApi');
|
2020-11-25 02:44:50 -08:00
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
2022-08-17 08:50:24 -07:00
|
|
|
Authorization: credentials.accessToken,
|
2020-11-25 02:44:50 -08:00
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
|
|
|
uri: uri || `https://api.profitwell.com/v2${resource}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-11-25 02:44:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function simplifyDailyMetrics(responseData: {
|
|
|
|
[key: string]: [{ date: string; value: number | null }];
|
|
|
|
}) {
|
2020-11-25 02:44:50 -08:00
|
|
|
const data: IDataObject[] = [];
|
|
|
|
const keys = Object.keys(responseData);
|
2022-08-17 08:50:24 -07:00
|
|
|
const dates = responseData[keys[0]].map((e) => e.date);
|
2020-11-25 02:44:50 -08:00
|
|
|
for (const [index, date] of dates.entries()) {
|
|
|
|
const element: IDataObject = {
|
|
|
|
date,
|
|
|
|
};
|
|
|
|
for (const key of keys) {
|
|
|
|
element[key] = responseData[key][index].value;
|
|
|
|
}
|
|
|
|
data.push(element);
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export function simplifyMontlyMetrics(responseData: {
|
|
|
|
[key: string]: [{ date: string; value: number | null }];
|
|
|
|
}) {
|
2020-11-25 02:44:50 -08:00
|
|
|
const data: IDataObject = {};
|
|
|
|
for (const key of Object.keys(responseData)) {
|
|
|
|
for (const [index] of responseData[key].entries()) {
|
|
|
|
data[key] = responseData[key][index].value;
|
2022-12-02 12:54:28 -08:00
|
|
|
data.date = responseData[key][index].date;
|
2020-11-25 02:44:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|