2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
2021-02-13 08:46:35 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2021-02-13 08:46:35 -08:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { demioApiRequest } from './GenericFunctions';
|
2021-02-13 08:46:35 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { eventFields, eventOperations } from './EventDescription';
|
2021-02-13 08:46:35 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { reportFields, reportOperations } from './ReportDescription';
|
2021-02-13 08:46:35 -08:00
|
|
|
|
|
|
|
export class Demio implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Demio',
|
|
|
|
name: 'demio',
|
|
|
|
icon: 'file:demio.svg',
|
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume the Demio API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Demio',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'demioApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-02-13 08:46:35 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Event',
|
|
|
|
value: 'event',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Report',
|
|
|
|
value: 'report',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'event',
|
|
|
|
},
|
|
|
|
// Event
|
|
|
|
...eventOperations,
|
|
|
|
...eventFields,
|
|
|
|
// Report
|
|
|
|
...reportOperations,
|
|
|
|
...reportFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the events to display them to user so that he can
|
|
|
|
// select them easily
|
2022-08-01 13:47:55 -07:00
|
|
|
async getEvents(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2021-02-13 08:46:35 -08:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-12-29 03:20:43 -08:00
|
|
|
const events = await demioApiRequest.call(this, 'GET', '/events', {}, { type: 'upcoming' });
|
2021-02-13 08:46:35 -08:00
|
|
|
for (const event of events) {
|
|
|
|
returnData.push({
|
|
|
|
name: event.name,
|
|
|
|
value: event.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get all the sessions to display them to user so that he can
|
|
|
|
// select them easily
|
2022-08-01 13:47:55 -07:00
|
|
|
async getEventSessions(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2021-02-13 08:46:35 -08:00
|
|
|
const eventId = this.getCurrentNodeParameter('eventId') as string;
|
|
|
|
const qs: IDataObject = {};
|
|
|
|
|
|
|
|
const resource = this.getCurrentNodeParameter('resource') as string;
|
|
|
|
|
|
|
|
if (resource !== 'report') {
|
|
|
|
qs.active = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-01 13:47:55 -07:00
|
|
|
const { dates } = await demioApiRequest.call(this, 'GET', `/event/${eventId}`, {});
|
2021-02-13 08:46:35 -08:00
|
|
|
for (const date of dates) {
|
|
|
|
returnData.push({
|
|
|
|
name: date.datetime,
|
|
|
|
value: date.date_id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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;
|
2021-02-13 08:46:35 -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);
|
2021-02-13 08:46:35 -08:00
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
if (resource === 'event') {
|
|
|
|
if (operation === 'get') {
|
|
|
|
const id = this.getNodeParameter('eventId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
if (additionalFields.date_id !== undefined) {
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await demioApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/event/${id}/date/${additionalFields.date_id}`,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
Object.assign(qs, additionalFields);
|
|
|
|
responseData = await demioApiRequest.call(this, 'GET', `/event/${id}`, {}, qs);
|
|
|
|
}
|
2021-02-13 08:46:35 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'getAll') {
|
2022-11-18 07:29:44 -08:00
|
|
|
const filters = this.getNodeParameter('filters', i);
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2021-02-13 08:46:35 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, filters);
|
2021-02-13 08:46:35 -08:00
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await demioApiRequest.call(this, 'GET', '/events', {}, qs);
|
2021-02-13 08:46:35 -08:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.splice(0, limit);
|
|
|
|
}
|
2021-02-13 08:46:35 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (operation === 'register') {
|
|
|
|
const eventId = this.getNodeParameter('eventId', i) as string;
|
|
|
|
const firstName = this.getNodeParameter('firstName', i) as string;
|
|
|
|
const email = this.getNodeParameter('email', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
|
|
|
const body: IDataObject = {
|
|
|
|
name: firstName,
|
|
|
|
email,
|
|
|
|
id: eventId,
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(body, additionalFields);
|
|
|
|
|
|
|
|
if (additionalFields.customFieldsUi) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const customFields =
|
2022-12-02 12:54:28 -08:00
|
|
|
((additionalFields.customFieldsUi as IDataObject)
|
|
|
|
?.customFieldsValues as IDataObject[]) || [];
|
2022-08-01 13:47:55 -07:00
|
|
|
const data = customFields.reduce(
|
|
|
|
(obj, value) => Object.assign(obj, { [`${value.fieldId}`]: value.value }),
|
|
|
|
{},
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(body, data);
|
|
|
|
delete additionalFields.customFields;
|
|
|
|
}
|
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await demioApiRequest.call(this, 'PUT', '/event/register', body);
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2021-02-13 08:46:35 -08:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (resource === 'report') {
|
|
|
|
if (operation === 'get') {
|
|
|
|
const sessionId = this.getNodeParameter('dateId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const filters = this.getNodeParameter('filters', i);
|
2021-02-13 08:46:35 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
Object.assign(qs, filters);
|
2021-02-13 08:46:35 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
responseData = await demioApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/report/${sessionId}/participants`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = responseData.participants;
|
|
|
|
}
|
2021-02-13 08:46:35 -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;
|
2021-02-13 08:46:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|