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';
|
2024-08-29 06:55:53 -07:00
|
|
|
import { NodeConnectionType } from 'n8n-workflow';
|
2022-08-17 08:50:24 -07:00
|
|
|
|
2024-01-15 06:45:33 -08:00
|
|
|
import moment from 'moment-timezone';
|
2022-08-17 08:50:24 -07:00
|
|
|
import { ouraApiRequest } from './GenericFunctions';
|
|
|
|
|
|
|
|
import { profileOperations } from './ProfileDescription';
|
|
|
|
|
|
|
|
import { summaryFields, summaryOperations } from './SummaryDescription';
|
2021-04-02 09:29:20 -07:00
|
|
|
|
|
|
|
export class Oura implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Oura',
|
|
|
|
name: 'oura',
|
2024-06-06 04:34:30 -07:00
|
|
|
icon: { light: 'file:oura.svg', dark: 'file:oura.dark.svg' },
|
2021-04-02 09:29:20 -07:00
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Oura API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Oura',
|
|
|
|
},
|
2024-08-29 06:55:53 -07:00
|
|
|
inputs: [NodeConnectionType.Main],
|
|
|
|
outputs: [NodeConnectionType.Main],
|
2021-04-02 09:29:20 -07:00
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'ouraApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-04-02 09:29:20 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Profile',
|
|
|
|
value: 'profile',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Summary',
|
|
|
|
value: 'summary',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'summary',
|
|
|
|
},
|
|
|
|
...profileOperations,
|
|
|
|
...summaryOperations,
|
|
|
|
...summaryFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const length = items.length;
|
|
|
|
|
|
|
|
let responseData;
|
2024-11-07 03:53:05 -08:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2021-04-02 09:29:20 -07:00
|
|
|
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-04-02 09:29:20 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
2024-11-07 03:53:05 -08:00
|
|
|
try {
|
|
|
|
if (resource === 'profile') {
|
|
|
|
// *********************************************************************
|
|
|
|
// profile
|
|
|
|
// *********************************************************************
|
2021-04-02 09:29:20 -07:00
|
|
|
|
2024-11-07 03:53:05 -08:00
|
|
|
// https://cloud.ouraring.com/docs/personal-info
|
2021-04-02 09:29:20 -07:00
|
|
|
|
2024-11-07 03:53:05 -08:00
|
|
|
if (operation === 'get') {
|
|
|
|
// ----------------------------------
|
|
|
|
// profile: get
|
|
|
|
// ----------------------------------
|
2021-04-02 09:29:20 -07:00
|
|
|
|
2024-11-07 03:53:05 -08:00
|
|
|
responseData = await ouraApiRequest.call(this, 'GET', '/usercollection/personal_info');
|
|
|
|
}
|
|
|
|
} else if (resource === 'summary') {
|
|
|
|
// *********************************************************************
|
|
|
|
// summary
|
|
|
|
// *********************************************************************
|
2021-04-02 09:29:20 -07:00
|
|
|
|
2024-11-07 03:53:05 -08:00
|
|
|
// https://cloud.ouraring.com/docs/daily-summaries
|
2021-04-02 09:29:20 -07:00
|
|
|
|
2024-11-07 03:53:05 -08:00
|
|
|
const qs: IDataObject = {};
|
2021-04-02 09:29:20 -07:00
|
|
|
|
2024-11-07 03:53:05 -08:00
|
|
|
const { start, end } = this.getNodeParameter('filters', i) as {
|
|
|
|
start: string;
|
|
|
|
end: string;
|
|
|
|
};
|
2021-04-02 09:29:20 -07:00
|
|
|
|
2024-11-07 03:53:05 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2021-04-02 09:29:20 -07:00
|
|
|
|
2024-11-07 03:53:05 -08:00
|
|
|
if (start) {
|
|
|
|
qs.start_date = moment(start).format('YYYY-MM-DD');
|
2021-04-02 09:29:20 -07:00
|
|
|
}
|
|
|
|
|
2024-11-07 03:53:05 -08:00
|
|
|
if (end) {
|
|
|
|
qs.end_date = moment(end).format('YYYY-MM-DD');
|
2021-04-02 09:29:20 -07:00
|
|
|
}
|
|
|
|
|
2024-11-07 03:53:05 -08:00
|
|
|
if (operation === 'getActivity') {
|
|
|
|
// ----------------------------------
|
|
|
|
// profile: getActivity
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
responseData = await ouraApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/usercollection/daily_activity',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
responseData = responseData.data;
|
|
|
|
|
|
|
|
if (!returnAll) {
|
|
|
|
const limit = this.getNodeParameter('limit', 0);
|
|
|
|
responseData = responseData.splice(0, limit);
|
|
|
|
}
|
|
|
|
} else if (operation === 'getReadiness') {
|
|
|
|
// ----------------------------------
|
|
|
|
// profile: getReadiness
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
responseData = await ouraApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/usercollection/daily_readiness',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
responseData = responseData.data;
|
|
|
|
|
|
|
|
if (!returnAll) {
|
|
|
|
const limit = this.getNodeParameter('limit', 0);
|
|
|
|
responseData = responseData.splice(0, limit);
|
|
|
|
}
|
|
|
|
} else if (operation === 'getSleep') {
|
|
|
|
// ----------------------------------
|
|
|
|
// profile: getSleep
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
responseData = await ouraApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/usercollection/daily_sleep',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
responseData = responseData.data;
|
|
|
|
|
|
|
|
if (!returnAll) {
|
|
|
|
const limit = this.getNodeParameter('limit', 0);
|
|
|
|
responseData = responseData.splice(0, limit);
|
|
|
|
}
|
2021-04-02 09:29:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-07 03:53:05 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-04-02 09:29:20 -07:00
|
|
|
}
|
2024-11-07 03:53:05 -08:00
|
|
|
return [returnData];
|
2021-04-02 09:29:20 -07:00
|
|
|
}
|
|
|
|
}
|