n8n-nodes-dataverse
Version:
n8n community node for communicating to dataverse
262 lines • 13.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Dataverse = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const dataverseAuth_credentials_1 = require("../credentials/dataverseAuth.credentials");
const operation_1 = require("./operation");
const operationOptions_1 = require("./operationOptions");
const getOperations_1 = require("./getOperations");
const patchOperations_1 = require("./patchOperations");
const postOperations_1 = require("./postOperations");
const optionsetOperations_1 = require("./optionsetOperations");
const globaloptionsetOperations_1 = require("./globaloptionsetOperations");
const entityLookupOperations_1 = require("./entityLookupOperations");
const properties_1 = require("./properties");
const operationType_1 = require("./operationType");
class Dataverse {
constructor() {
this.description = {
displayName: "Dataverse",
name: "dataverse",
icon: "file:./resources/Dataverse_scalable.svg",
group: ["transform"],
version: 1,
description: "Seamless integration with the Dynamics 365 Dataverse API",
subtitle: '={{ $parameter["operation"] }}',
defaults: {
name: "Dataverse",
},
inputs: ["main"],
outputs: ["main"],
credentials: [
{
name: "dataverseAuth",
required: true,
},
],
properties: [
operationOptions_1.operationOptions,
...getOperations_1.getOperations,
...patchOperations_1.patchOperations,
...postOperations_1.postOperations,
...optionsetOperations_1.optionsetOperations,
...globaloptionsetOperations_1.globaloptionsetOperations,
...entityLookupOperations_1.entityLookupOperations
],
};
this.methods = {
loadOptions: {
async getEntityList() {
const cachedTables = dataverseAuth_credentials_1.dataverseAuth.getCachedTables();
if (cachedTables) {
return cachedTables;
}
const credentials = await this.getCredentials("dataverseAuth");
const auth = new dataverseAuth_credentials_1.dataverseAuth();
await auth.authenticate(credentials, { url: "" });
const tablesResponse = await auth.ListTables();
const options = tablesResponse.tables.map((table) => ({
name: `${table.displayName} - (${table.logicalName})`,
value: table.logicalName,
}));
dataverseAuth_credentials_1.dataverseAuth.setCachedTables(options);
return options;
},
async getEntityColumns() {
const entityName = this.getCurrentNodeParameter(properties_1.Properties.ENTITYNAME);
if (!entityName)
return [];
const cachedColumns = dataverseAuth_credentials_1.dataverseAuth.getCachedEntityColumns(entityName);
if (cachedColumns) {
return cachedColumns;
}
const credentials = await this.getCredentials("dataverseAuth");
const auth = new dataverseAuth_credentials_1.dataverseAuth();
await auth.authenticate(credentials, { url: "" });
const columnsResponse = await auth.ListEntityColumns(entityName);
const options = columnsResponse.columns.map((col) => ({
name: `${col.displayName} - (${col.logicalName})`,
value: col.logicalName,
}));
dataverseAuth_credentials_1.dataverseAuth.setCachedEntityColumns(entityName, options);
return options;
},
},
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
const credentials = await this.getCredentials("dataverseAuth");
const auth = new dataverseAuth_credentials_1.dataverseAuth();
const operation = this.getNodeParameter("operation", 0);
await auth.authenticate(credentials, { url: "" });
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
try {
let type;
let columnsData;
let query;
const entityName = this.getNodeParameter(properties_1.Properties.ENTITYNAME, itemIndex, "");
const patch_recordid = this.getNodeParameter(properties_1.Properties.PATCH_RECORDID, itemIndex, "");
const patch_data = this.getNodeParameter(properties_1.Properties.PATCH_DATA, itemIndex, null);
const optionset_entityname = this.getNodeParameter(properties_1.Properties.OPTIONSET_ENTITYNAME, itemIndex, "");
const optionset_attributename = this.getNodeParameter(properties_1.Properties.OPTIONSET_ATTRIBUTENAME, itemIndex, "");
const global_attributeName = this.getNodeParameter(properties_1.Properties.GLOBAL_ATTRIBUTENAME, itemIndex, "");
const entityname = this.getNodeParameter(properties_1.Properties.ENTITYNAME, itemIndex, "");
const entity_id = this.getNodeParameter(properties_1.Properties.ENTITY_ID, itemIndex, "");
const entity_name = this.getNodeParameter(properties_1.Properties.ENTITY_NAME, itemIndex, "");
switch (operation) {
case operation_1.Operation.GET:
type = this.getNodeParameter("type", itemIndex);
({ query, columnsData } = await handleGetOperation(this, type, query, itemIndex, columnsData, entityName));
break;
case operation_1.Operation.PATCH:
columnsData = await handlePatchOperation(this, columnsData, itemIndex, entityName, patch_recordid, patch_data);
break;
case operation_1.Operation.POST:
columnsData = await handlePostOperation(this, columnsData, itemIndex, entityName, patch_data);
break;
case operation_1.Operation.OPTIONSET:
query = await handleOptionSetOperation(query, optionset_entityname, optionset_attributename, itemIndex);
break;
case operation_1.Operation.GLOBALOPTIONSET:
query = await handleGlobalOptionSetOperation(query, global_attributeName, itemIndex);
break;
case operation_1.Operation.ENTITY:
await handleEntityOperation(entityname, entity_id, entity_name, entityName, itemIndex);
break;
}
}
catch (error) {
if (this.continueOnFail()) {
items.push({
json: this.getInputData(itemIndex)[0].json,
error: error,
pairedItem: itemIndex,
});
}
else {
if (error.context) {
error.context.itemIndex = itemIndex;
throw error;
}
throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, {
itemIndex,
});
}
}
}
return this.prepareOutputData(returnData);
async function handleEntityOperation(entityname, entity_id, entity_name, entityName, itemIndex) {
const modifiedEntityLogicalName = auth.modifyEntityLogicalName(entityname);
const entityquery = `${modifiedEntityLogicalName}?$select=${entity_id},${entity_name}&$orderby=${entity_name} asc`;
const data = await auth.GetData(operationType_1.OperationType.ODATA, entityName, entityquery || "");
const mappedOptions = data.value.map((entry) => ({
Id: entry[entity_id],
Name: entry[entity_name],
}));
const result = { options: mappedOptions };
returnData.push({
json: result,
pairedItem: itemIndex,
});
}
async function handleGlobalOptionSetOperation(query, global_attributeName, itemIndex) {
query = `GlobalOptionSetDefinitions(Name='${global_attributeName}')`;
const data = await auth.GetData("ODATA", "", query);
let output;
if (data.Options && Array.isArray(data.Options)) {
const options = data.Options.map((entry) => ({
Id: entry.Value,
Name: entry.Label.LocalizedLabels[0].Label,
}));
output = { options };
}
else if (data.OptionSet && data.OptionSet.Options) {
const options = data.OptionSet.Options.map((entry) => ({
Id: entry.Value,
Name: entry.Label.LocalizedLabels[0].Label,
}));
output = { options };
}
else if (data.value &&
data.value.length > 0 &&
data.value[0].OptionSet &&
data.value[0].OptionSet.Options) {
const options = data.value[0].OptionSet.Options.map((entry) => ({
Id: entry.Value,
Name: entry.Label.LocalizedLabels[0].Label,
}));
output = { options };
}
else {
output = data;
}
returnData.push({
json: output,
pairedItem: itemIndex,
});
return query;
}
async function handleOptionSetOperation(query, optionset_entityname, optionset_attributename, itemIndex) {
query = `EntityDefinitions(LogicalName='${optionset_entityname}')/Attributes(LogicalName='${optionset_attributename}')/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName,DisplayName&$expand=OptionSet($select=Options)`;
const data = await auth.GetData("ODATA", "", query);
let output;
if (data.OptionSet && data.OptionSet.Options) {
const options = data.OptionSet.Options.map((entry) => ({
Id: entry.Value,
Name: entry.Label.LocalizedLabels[0].Label,
}));
output = { options };
}
else if (data.value &&
data.value.length > 0 &&
data.value[0].OptionSet &&
data.value[0].OptionSet.Options) {
const options = data.value[0].OptionSet.Options.map((entry) => ({
Id: entry.Value,
Name: entry.Label.LocalizedLabels[0].Label,
}));
output = { options };
}
else {
output = data;
}
returnData.push({
json: output,
pairedItem: itemIndex,
});
return query;
}
async function handlePostOperation(func, columnsData, itemIndex, entityName, patch_data) {
columnsData = func.getNodeParameter("postcolumn", itemIndex, "");
const updateResponse = await auth.CreateData(entityName, patch_data, columnsData);
returnData.push({
json: updateResponse,
pairedItem: itemIndex,
});
return columnsData;
}
async function handlePatchOperation(func, columnsData, itemIndex, entityName, patch_recordid, patch_data) {
debugger;
columnsData = func.getNodeParameter("column", itemIndex);
const updateResponse = await auth.UpdateData(entityName, patch_recordid, patch_data, columnsData);
returnData.push({
json: updateResponse,
pairedItem: itemIndex,
});
return columnsData;
}
async function handleGetOperation(func, type, query, itemIndex, columnsData, entityName) {
query = func.getNodeParameter("getQuery", itemIndex);
const data = await auth.GetData(type, entityName, query || "");
returnData.push({
json: data,
pairedItem: itemIndex,
});
return { query, columnsData };
}
}
}
exports.Dataverse = Dataverse;
//# sourceMappingURL=Dataverse.node.js.map