@skriptfabrik/n8n-nodes-moco
Version:
MOCO community nodes for n8n
619 lines • 35 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Moco = void 0;
const ActivityDescription_1 = require("./Descriptions/ActivityDescription");
const CompanyDescription_1 = require("./Descriptions/CompanyDescription");
const ContactDescription_1 = require("./Descriptions/ContactDescription");
const ProjectDescription_1 = require("./Descriptions/ProjectDescription");
const UserDescription_1 = require("./Descriptions/UserDescription");
const GenericFunctions_1 = require("./GenericFunctions");
const n8n_workflow_1 = require("n8n-workflow");
class Moco {
constructor() {
this.description = {
displayName: 'MOCO',
name: 'moco',
icon: 'file:../../icons/Moco.svg',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume MOCO API',
defaults: {
name: 'MOCO',
},
usableAsTool: true,
inputs: [n8n_workflow_1.NodeConnectionTypes.Main],
outputs: [n8n_workflow_1.NodeConnectionTypes.Main],
credentials: [
{
name: 'mocoApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Activity',
value: 'activity',
},
{
name: 'Company',
value: 'company',
},
{
name: 'Contact',
value: 'contacts',
},
{
name: 'Project',
value: 'project',
},
{
name: 'User',
value: 'user',
},
],
default: 'user',
},
...ActivityDescription_1.activityOperations,
...ActivityDescription_1.activityFields,
...CompanyDescription_1.companyOperations,
...CompanyDescription_1.companyFields,
...ContactDescription_1.contactOperations,
...ContactDescription_1.contactFields,
...ProjectDescription_1.projectOperations,
...ProjectDescription_1.projectFields,
...UserDescription_1.userOperations,
...UserDescription_1.userFields,
],
};
this.methods = {
loadOptions: {
async listCompanies() {
const returnData = [];
const companies = await (GenericFunctions_1.mocoApiRequestAllItems).call(this, undefined, 'GET', '/companies');
for (const company of companies) {
returnData.push({
name: company.name,
value: company.id,
});
}
return returnData.sort((a, b) => a.name.localeCompare(b.name));
},
async listCustomers() {
const returnData = [];
const companies = await (GenericFunctions_1.mocoApiRequestAllItems).call(this, undefined, 'GET', '/companies', { qs: { type: 'customer' } });
for (const company of companies) {
returnData.push({
name: company.name,
value: company.id,
});
}
return returnData.sort((a, b) => a.name.localeCompare(b.name));
},
async listLeads() {
const returnData = [];
const deals = await (GenericFunctions_1.mocoApiRequestAllItems).call(this, undefined, 'GET', '/deals');
for (const deal of deals) {
returnData.push({
name: deal.name,
value: deal.id,
});
}
return returnData.sort((a, b) => a.name.localeCompare(b.name));
},
async listProjects() {
const returnData = [];
const projects = await (GenericFunctions_1.mocoApiRequestAllItems).call(this, undefined, 'GET', '/projects');
for (const project of projects) {
returnData.push({
name: `${project.customer.name} > ${project.name}`,
value: project.id,
});
}
return returnData.sort((a, b) => a.name.localeCompare(b.name));
},
async listProjectTasks() {
const returnData = [];
const projectId = this.getCurrentNodeParameter('projectId') || undefined;
if (projectId === undefined) {
const projects = await (GenericFunctions_1.mocoApiRequestAllItems).call(this, undefined, 'GET', '/projects');
for (const project of projects) {
for (const projectTask of project.tasks) {
returnData.push({
name: `${project.customer.name} > ${project.name} > ${projectTask.name}`,
value: projectTask.id,
});
}
}
}
else {
const projectTasks = await (GenericFunctions_1.mocoApiRequestAllItems).call(this, undefined, 'GET', `/projects/${projectId}/tasks`);
for (const projectTask of projectTasks) {
returnData.push({
name: projectTask.name,
value: projectTask.id,
});
}
}
return returnData.sort((a, b) => a.name.localeCompare(b.name));
},
async listTeams() {
const returnData = [];
const units = await (GenericFunctions_1.mocoApiRequestAllItems).call(this, undefined, 'GET', '/units');
for (const unit of units) {
returnData.push({
name: unit.name,
value: unit.id,
});
}
return returnData.sort((a, b) => a.name.localeCompare(b.name));
},
async listUsers() {
const returnData = [];
const users = await (GenericFunctions_1.mocoApiRequestAllItems).call(this, undefined, 'GET', '/users');
for (const user of users) {
returnData.push({
name: `${user.firstname} ${user.lastname}`,
value: user.id,
});
}
return returnData.sort((a, b) => a.name.localeCompare(b.name));
},
},
};
}
async execute() {
var _a, _b;
const items = this.getInputData();
const returnData = [];
let responseData = {};
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
for (let item = 0; item < items.length; item++) {
try {
if (resource === 'activity') {
if (operation === 'create') {
const impersonateUserId = this.getNodeParameter('impersonateUserId', item) ||
undefined;
const body = {
date: this.getNodeParameter('date', item),
project_id: this.getNodeParameter('projectId', item),
task_id: this.getNodeParameter('taskId', item),
seconds: this.getNodeParameter('seconds', item),
description: this.getNodeParameter('description', item),
...GenericFunctions_1.createParametersFromNodeParameter.call(this, 'additionalFields', item, ['billable', 'tag', 'remoteService', 'remoteId', 'remoteUrl']),
};
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'POST', '/activities', { impersonateUserId, body }));
}
if (operation === 'delete') {
const activityId = this.getNodeParameter('activityId', item);
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'DELETE', `/activities/${activityId}`));
}
if (operation === 'get') {
const activityId = this.getNodeParameter('activityId', item);
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'GET', `/activities/${activityId}`));
}
if (operation === 'list') {
const returnAll = this.getNodeParameter('returnAll', item);
const qs = {
...(returnAll
? {}
: {
limit: this.getNodeParameter('limit', item) ||
undefined,
ids: this.getNodeParameter('ids', item) ||
undefined,
updated_after: GenericFunctions_1.createUTCStringFromNodeParameter.call(this, 'updatedAfter', item),
}),
...GenericFunctions_1.createParametersFromNodeParameter.call(this, 'additionalFields', item, [
'from',
'to',
'userId',
'projectId',
'taskId',
'companyId',
'term',
'sortBy',
]),
};
responseData = (await GenericFunctions_1.mocoApiRequestAllItems.call(this, item, 'GET', '/activities', { qs }));
}
if (operation === 'update') {
const activityId = this.getNodeParameter('activityId', item);
const body = {
date: this.getNodeParameter('date', item),
project_id: this.getNodeParameter('projectId', item),
task_id: this.getNodeParameter('taskId', item),
seconds: this.getNodeParameter('seconds', item),
description: this.getNodeParameter('description', item),
...GenericFunctions_1.createParametersFromNodeParameter.call(this, 'additionalFields', item, ['billable', 'tag', 'remoteService', 'remoteId', 'remoteUrl']),
};
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'PUT', `/activities/${activityId}`, { body }));
}
}
if (resource === 'company') {
if (operation === 'create' || operation === 'update') {
const type = this.getNodeParameter('type', item);
let typeSpecificParameters = {};
const additionalFields = [
'countryCode',
'vatIdentifier',
'alternativeCorrespondenceLanguage',
'website',
'fax',
'phone',
'email',
'billingEmailCc',
'address',
'info',
'customProperties',
'tags',
'footer',
];
if (type === 'customer') {
typeSpecificParameters = {
currency: this.getNodeParameter('currency', item),
identifier: this.getNodeParameter('identifier', item),
};
additionalFields.push('customerTax', 'defaultInvoiceDueDays', 'debitNumber');
}
if (type === 'supplier') {
additionalFields.push('iban', 'supplierTax', 'creditNumber');
}
const additionalFieldsParameters = GenericFunctions_1.createParametersFromNodeParameter.call(this, 'additionalFields', item, additionalFields);
if (additionalFieldsParameters.custom_properties &&
typeof additionalFieldsParameters.custom_properties ===
'object' &&
'values' in additionalFieldsParameters.custom_properties) {
additionalFieldsParameters.custom_properties = additionalFieldsParameters.custom_properties.values.reduce((properties, { key, value }) => {
properties[key] = value;
return properties;
}, {});
}
const body = {
name: this.getNodeParameter('name', item),
type,
...typeSpecificParameters,
...additionalFieldsParameters,
};
let companyId = undefined;
if (operation === 'update') {
companyId = this.getNodeParameter('companyId', item);
}
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, operation === 'create' ? 'POST' : 'PUT', operation === 'create' ? '/companies' : `/companies/${companyId}`, {
body,
}));
}
if (operation === 'delete') {
const companyId = this.getNodeParameter('companyId', item);
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'DELETE', `/companies/${companyId}`));
}
if (operation === 'get') {
const companyId = this.getNodeParameter('companyId', item);
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'GET', `/companies/${companyId}`));
}
if (operation === 'list') {
const returnAll = this.getNodeParameter('returnAll', item);
const qs = {
...(returnAll
? {}
: {
limit: this.getNodeParameter('limit', item) ||
undefined,
ids: this.getNodeParameter('ids', item) ||
undefined,
updated_after: GenericFunctions_1.createUTCStringFromNodeParameter.call(this, 'updatedAfter', item),
}),
...GenericFunctions_1.createParametersFromNodeParameter.call(this, 'additionalFields', item, ['type', 'tags', 'identifier', 'term', 'sortBy']),
};
responseData = (await GenericFunctions_1.mocoApiRequestAllItems.call(this, item, 'GET', '/companies', { qs }));
}
}
if (resource === 'project') {
if (operation === 'create') {
const body = {
name: this.getNodeParameter('name', item),
currency: this.getNodeParameter('currency', item),
start_date: this.getNodeParameter('startDate', item),
finish_date: this.getNodeParameter('finishDate', item),
fixed_price: this.getNodeParameter('fixedPrice', item),
retainer: this.getNodeParameter('retainer', item),
leader_id: this.getNodeParameter('leaderId', item),
customer_id: this.getNodeParameter('customerId', item),
budget_monthly: this.getNodeParameter('budgetMonthly', item),
identifier: this.getNodeParameter('identifier', item),
...GenericFunctions_1.createParametersFromNodeParameter.call(this, 'additionalFields', item, [
'coLeaderId',
'dealId',
'billingAddress',
'billingEmailTo',
'billingEmailCc',
'billingNotes',
'settingIncludeTimeReport',
'billingVariant',
'hourlyRate',
'budget',
'budgetExpenses',
'tags',
'customProperties',
'info',
]),
};
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'POST', '/projects', {
body,
}));
}
if (operation === 'delete') {
const projectId = this.getNodeParameter('projectId', item);
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'DELETE', `/projects/${projectId}`));
}
if (operation === 'get') {
const projectId = this.getNodeParameter('projectId', item);
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'GET', `/projects/${projectId}`));
}
if (operation === 'list') {
const returnAll = this.getNodeParameter('returnAll', item);
const qs = {
...(returnAll
? {}
: {
limit: this.getNodeParameter('limit', item) ||
undefined,
ids: this.getNodeParameter('ids', item) ||
undefined,
updated_after: GenericFunctions_1.createUTCStringFromNodeParameter.call(this, 'updatedAfter', item),
}),
...GenericFunctions_1.createParametersFromNodeParameter.call(this, 'additionalFields', item, [
'includeArchived',
'includeCompany',
'leaderId',
'companyId',
'createdFrom',
'createdTo',
'updatedFrom',
'updatedTo',
'tags',
'identifier',
'retainer',
'projectGroupId',
'sortBy',
]),
};
responseData = (await GenericFunctions_1.mocoApiRequestAllItems.call(this, item, 'GET', '/projects', { qs }));
}
if (operation === 'update') {
const projectId = this.getNodeParameter('projectId', item);
const body = {
name: this.getNodeParameter('name', item),
currency: this.getNodeParameter('currency', item),
start_date: this.getNodeParameter('startDate', item),
finish_date: this.getNodeParameter('finishDate', item),
fixed_price: this.getNodeParameter('fixedPrice', item),
retainer: this.getNodeParameter('retainer', item),
leader_id: this.getNodeParameter('leaderId', item),
customer_id: this.getNodeParameter('customerId', item),
budget_monthly: this.getNodeParameter('budgetMonthly', item),
identifier: this.getNodeParameter('identifier', item),
...GenericFunctions_1.createParametersFromNodeParameter.call(this, 'additionalFields', item, [
'coLeaderId',
'dealId',
'billingAddress',
'billingEmailTo',
'billingEmailCc',
'billingNotes',
'settingIncludeTimeReport',
'billingVariant',
'hourlyRate',
'budget',
'budgetExpenses',
'tags',
'customProperties',
'info',
]),
};
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'PUT', `/projects/${projectId}`, { body }));
}
}
if (resource === 'user') {
if (operation === 'create') {
const body = {
firstname: this.getNodeParameter('firstname', item),
lastname: this.getNodeParameter('lastname', item),
email: this.getNodeParameter('email', item),
password: this.getNodeParameter('password', item),
unit_id: this.getNodeParameter('unitId', item),
...GenericFunctions_1.createParametersFromNodeParameter.call(this, 'additionalFields', item, ['active', 'external']),
};
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'POST', '/users', {
body,
}));
}
if (operation === 'delete') {
const userId = this.getNodeParameter('userId', item);
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'DELETE', `/users/${userId}`));
}
if (operation === 'get') {
const userId = this.getNodeParameter('userId', item);
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'GET', `/users/${userId}`));
}
if (operation === 'list') {
const returnAll = this.getNodeParameter('returnAll', item);
const qs = {
...(returnAll
? {}
: {
limit: this.getNodeParameter('limit', item) ||
undefined,
ids: this.getNodeParameter('ids', item) ||
undefined,
updated_after: GenericFunctions_1.createUTCStringFromNodeParameter.call(this, 'updatedAfter', item),
}),
...GenericFunctions_1.createParametersFromNodeParameter.call(this, 'additionalFields', item, ['includeArchived', 'sortBy']),
};
responseData = (await GenericFunctions_1.mocoApiRequestAllItems.call(this, item, 'GET', '/users', { qs }));
}
if (operation === 'update') {
const userId = this.getNodeParameter('userId', item);
const body = {
firstname: this.getNodeParameter('firstname', item),
lastname: this.getNodeParameter('lastname', item),
email: this.getNodeParameter('email', item),
password: this.getNodeParameter('password', item),
unit_id: this.getNodeParameter('unitId', item),
...GenericFunctions_1.createParametersFromNodeParameter.call(this, 'additionalFields', item, ['active', 'external']),
};
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'PUT', `/users/${userId}`, { body }));
}
}
if (resource === 'contacts') {
if (operation === 'create') {
const additionalFields = this.getNodeParameter('additionalFields', item);
const body = {
firstname: this.getNodeParameter('firstname', item),
lastname: this.getNodeParameter('lastname', item),
company_id: this.getNodeParameter('companyId', item),
...(additionalFields.title && { title: additionalFields.title }),
...(additionalFields.jobPosition && {
job_position: additionalFields.jobPosition,
}),
...(additionalFields.workEmail && {
work_email: additionalFields.workEmail,
}),
...(additionalFields.homeEmail && {
home_email: additionalFields.homeEmail,
}),
...(additionalFields.mobilePhone && {
mobile_phone: additionalFields.mobilePhone,
}),
...(additionalFields.workPhone && {
work_phone: additionalFields.workPhone,
}),
...(additionalFields.workFax && {
work_fax: additionalFields.workFax,
}),
...(additionalFields.homeAddress && {
home_address: additionalFields.homeAddress,
}),
...(additionalFields.birthday && {
birthday: additionalFields.birthday,
}),
...(additionalFields.gender && {
gender: additionalFields.gender,
}),
...(additionalFields.info && { info: additionalFields.info }),
...(additionalFields.tags && {
tags: additionalFields.tags.split(',').map((tag) => tag.trim()),
}),
};
if ((_a = additionalFields.customProperties) === null || _a === void 0 ? void 0 : _a.values) {
body.custom_properties =
additionalFields.customProperties.values.reduce((acc, prop) => {
acc[prop.key] = prop.value;
return acc;
}, {});
}
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'POST', '/contacts/people', {
body,
}));
}
if (operation === 'delete') {
const contactId = this.getNodeParameter('contactId', item);
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'DELETE', `/contacts/people/${contactId}`));
}
if (operation === 'get') {
const contactId = this.getNodeParameter('contactId', item);
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'GET', `/contacts/people/${contactId}`));
}
if (operation === 'list') {
const returnAll = this.getNodeParameter('returnAll', item);
const qs = {
...(returnAll
? {}
: {
limit: this.getNodeParameter('limit', item) ||
undefined,
ids: this.getNodeParameter('ids', item) ||
undefined,
updated_after: GenericFunctions_1.createUTCStringFromNodeParameter.call(this, 'updatedAfter', item),
}),
...GenericFunctions_1.createParametersFromNodeParameter.call(this, 'additionalFields', item, ['phone', 'term']),
};
responseData = (await GenericFunctions_1.mocoApiRequestAllItems.call(this, item, 'GET', '/contacts/people', { qs }));
}
if (operation === 'update') {
const contactId = this.getNodeParameter('contactId', item);
const updateFields = this.getNodeParameter('updateFields', item);
const body = {
...(updateFields.firstname && {
firstname: updateFields.firstname,
}),
...(updateFields.lastname && { lastname: updateFields.lastname }),
...(updateFields.companyId && {
company_id: updateFields.companyId,
}),
...(updateFields.title && { title: updateFields.title }),
...(updateFields.jobPosition && {
job_position: updateFields.jobPosition,
}),
...(updateFields.workEmail && {
work_email: updateFields.workEmail,
}),
...(updateFields.homeEmail && {
home_email: updateFields.homeEmail,
}),
...(updateFields.mobilePhone && {
mobile_phone: updateFields.mobilePhone,
}),
...(updateFields.workPhone && {
work_phone: updateFields.workPhone,
}),
...(updateFields.workFax && {
work_fax: updateFields.workFax,
}),
...(updateFields.homeAddress && {
home_address: updateFields.homeAddress,
}),
...(updateFields.birthday && {
birthday: updateFields.birthday,
}),
...(updateFields.gender && { gender: updateFields.gender }),
...(updateFields.info && { info: updateFields.info }),
...(updateFields.tags && {
tags: updateFields.tags.split(',').map((tag) => tag.trim()),
}),
};
if ((_b = updateFields.customProperties) === null || _b === void 0 ? void 0 : _b.values) {
body.custom_properties =
updateFields.customProperties.values.reduce((acc, prop) => {
acc[prop.key] = prop.value;
return acc;
}, {});
}
responseData = (await GenericFunctions_1.mocoApiRequest.call(this, item, 'PUT', `/contacts/people/${contactId}`, { body }));
}
}
const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), { itemData: { item } });
returnData.push(...executionData);
}
catch (error) {
if (this.continueOnFail()) {
returnData.push({
json: { error: error.message },
pairedItem: { item },
});
continue;
}
throw error;
}
}
return [returnData];
}
}
exports.Moco = Moco;
//# sourceMappingURL=Moco.node.js.map