Skip to content

Commit

Permalink
fix(Pipedrive Node): Fix resolve properties when multi option field i…
Browse files Browse the repository at this point in the history
…s used (#3277)

* Fixed Pipedrive properties not resolving when using multiple options field

* ⚡ Improvements

Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
  • Loading branch information
Joffcom and RicardoE105 authored May 16, 2022
1 parent c75d58c commit 7eb1261
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions packages/nodes-base/nodes/Pipedrive/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
export interface ICustomInterface {
name: string;
key: string;
field_type: string;
options?: Array<{
id: number;
label: string;
Expand Down Expand Up @@ -171,7 +172,6 @@ export async function pipedriveGetCustomProperties(this: IHookFunctions | IExecu
for (const customPropertyData of responseData.data) {
customProperties[customPropertyData.key] = customPropertyData;
}

return customProperties;
}

Expand Down Expand Up @@ -226,30 +226,57 @@ export function pipedriveResolveCustomProperties(customProperties: ICustomProper

// Itterate over all keys and replace the custom ones
for (const key of Object.keys(item)) {

if (customProperties[key] !== undefined) {
// Is a custom property
customPropertyData = customProperties[key];

// Check if also the value has to be resolved or just the key
if (item[key] !== null && item[key] !== undefined && customPropertyData.options !== undefined && Array.isArray(customPropertyData.options)) {
// Has an option key so get the actual option-value
const propertyOption = customPropertyData.options.find(option => option.id.toString() === item[key]!.toString());
// value is not set, so nothing to resolve
if (item[key] === null) {
item[customPropertyData.name] = item[key];
delete item[key];
continue;
}

if ([
'date',
'address',
'double',
'monetary',
'org',
'people',
'phone',
'text',
'time',
'user',
'varchar',
'varchar_auto',
'int',
'time',
'timerange',
].includes(customPropertyData.field_type)) {
item[customPropertyData.name as string] = item[key];
delete item[key];
// type options
} else if (['enum', 'visible_to'].includes(customPropertyData.field_type) && customPropertyData.options) {
const propertyOption = customPropertyData.options.find(option => option.id.toString() === item[key]!.toString());
if (propertyOption !== undefined) {
item[customPropertyData.name as string] = propertyOption.label;
delete item[key];
}
} else {
// Does already represent the actual value or is null
item[customPropertyData.name as string] = item[key];
// type multioptions
} else if (['set'].includes(customPropertyData.field_type) && customPropertyData.options) {
const selectedIds = (item[key] as string).split(',');
const selectedLabels = customPropertyData.options.
filter(option => selectedIds.includes(option.id.toString())).
map(option => option.label);
item[customPropertyData.name] = selectedLabels;
delete item[key];
}
}
}

}


export function sortOptionParameters(optionParameters: INodePropertyOptions[]): INodePropertyOptions[] {
optionParameters.sort((a, b) => {
const aName = a.name.toLowerCase();
Expand Down

0 comments on commit 7eb1261

Please sign in to comment.