Skip to content

Commit eb708f8

Browse files
committed
fix: copperCRM custom field issue
1 parent 7bc8dd1 commit eb708f8

File tree

2 files changed

+73
-48
lines changed

2 files changed

+73
-48
lines changed

frontend-dev/src/components/AllIntegrations/CopperCRM/CopperCRMFieldMap.jsx

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
/* eslint-disable no-console */
22
import { useRecoilValue } from 'recoil'
33
import { __ } from '../../../Utils/i18nwrap'
4-
import { addFieldMap, delFieldMap, handleFieldMapping } from './IntegrationHelpers'
4+
import { addFieldMap, delFieldMap, handleCustomValue, handleFieldMapping } from './IntegrationHelpers'
55
import { SmartTagField } from '../../../Utils/StaticData/SmartTagField'
66
import { $btcbi } from '../../../GlobalStates'
77
import { generateMappedField } from './CopperCRMCommonFunc'
88
import CustomField from './CustomField'
9+
import TagifyInput from '../../Utilities/TagifyInput'
910

10-
export default function CopperCRMFieldMap({
11-
i,
12-
formFields,
13-
field,
14-
coppercrmConf,
15-
setCopperCRMConf
16-
}) {
11+
export default function CopperCRMFieldMap({ i, formFields, field, coppercrmConf, setCopperCRMConf }) {
1712
let allFields = []
1813
let newFields = []
1914
if (coppercrmConf.actionName === 'company') {
@@ -26,8 +21,8 @@ export default function CopperCRMFieldMap({
2621
allFields = coppercrmConf?.taskFields
2722
}
2823
// newFields = [...allFields, ...coppercrmConf?.customFields]
29-
const requiredFields = allFields.filter((fld) => fld.required === true) || []
30-
const nonRequiredFields = allFields.filter((fld) => fld.required === false) || []
24+
const requiredFields = allFields.filter(fld => fld.required === true) || []
25+
const nonRequiredFields = allFields.filter(fld => fld.required === false) || []
3126
const allNonRequiredFields = coppercrmConf.customFields
3227
? [...nonRequiredFields, ...coppercrmConf?.customFields]
3328
: nonRequiredFields
@@ -50,10 +45,10 @@ export default function CopperCRMFieldMap({
5045
className="btcd-paper-inp mr-2"
5146
name="formField"
5247
value={field.formField || ''}
53-
onChange={(ev) => handleFieldMapping(ev, i, coppercrmConf, setCopperCRMConf)}>
48+
onChange={ev => handleFieldMapping(ev, i, coppercrmConf, setCopperCRMConf)}>
5449
<option value="">{__('Select Field', 'bit-integrations')}</option>
5550
<optgroup label={__('Form Fields', 'bit-integrations')}>
56-
{formFields?.map((f) => (
51+
{formFields?.map(f => (
5752
<option key={`ff-rm-${f.name}`} value={f.name}>
5853
{f.label}
5954
</option>
@@ -66,7 +61,7 @@ export default function CopperCRMFieldMap({
6661
isPro ? '' : `(${__('Pro', 'bit-integrations')})`
6762
)}>
6863
{isPro &&
69-
SmartTagField?.map((f) => (
64+
SmartTagField?.map(f => (
7065
<option key={`ff-rm-${f.name}`} value={f.name}>
7166
{f.label}
7267
</option>
@@ -75,25 +70,23 @@ export default function CopperCRMFieldMap({
7570
</select>
7671

7772
{field.formField === 'custom' && (
78-
<CustomField
79-
field={field}
80-
index={i}
81-
conf={coppercrmConf}
82-
setConf={setCopperCRMConf}
83-
fieldValue="customValue"
84-
fieldLabel="Custom Value"
73+
<TagifyInput
74+
onChange={e => handleCustomValue(e, i, coppercrmConf, setCopperCRMConf)}
75+
label={__('Custom Value', 'bit-integrations')}
8576
className="mr-2"
77+
type="text"
78+
value={field.customValue}
79+
placeholder={__('Custom Value', 'bit-integrations')}
80+
formFields={formFields}
8681
/>
8782
)}
8883

8984
<select
9085
className="btcd-paper-inp"
9186
disabled={i < requiredFields.length}
9287
name="coppercrmFormField"
93-
value={
94-
i < requiredFields ? requiredFields[i].label || '' : field.coppercrmFormField || ''
95-
}
96-
onChange={(ev) => handleFieldMapping(ev, i, coppercrmConf, setCopperCRMConf)}>
88+
value={i < requiredFields ? requiredFields[i].label || '' : field.coppercrmFormField || ''}
89+
onChange={ev => handleFieldMapping(ev, i, coppercrmConf, setCopperCRMConf)}>
9790
<option value="">{__('Select Field', 'bit-integrations')}</option>
9891
{i < requiredFields.length ? (
9992
<option key={requiredFields[i].key} value={requiredFields[i].key}>

includes/Actions/CopperCRM/RecordApiHelper.php

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
namespace BitCode\FI\Actions\CopperCRM;
88

9-
use BitCode\FI\Log\LogHandler;
109
use BitCode\FI\Core\Util\Common;
1110
use BitCode\FI\Core\Util\HttpHelper;
11+
use BitCode\FI\Log\LogHandler;
1212

1313
/**
1414
* Provide functionality for Record insert, upsert
@@ -67,7 +67,7 @@ public function addCompany($finalData)
6767
} else {
6868
$requestParams['custom_fields'][] = (object) [
6969
'value' => $value,
70-
'custom_field_definition_id' => $key
70+
'custom_field_definition_id' => $this->getCustomFieldId($key, 'company')
7171
];
7272
}
7373
}
@@ -81,7 +81,7 @@ public function addCompany($finalData)
8181

8282
$apiEndpoint = $this->apiEmail . '/companies';
8383

84-
return $response = HttpHelper::post($apiEndpoint, wp_json_encode($requestParams), $this->defaultHeader);
84+
return HttpHelper::post($apiEndpoint, wp_json_encode($requestParams), $this->defaultHeader);
8585
}
8686

8787
public function addPerson($finalData)
@@ -116,7 +116,7 @@ public function addPerson($finalData)
116116
} else {
117117
$requestParams['custom_fields'][] = (object) [
118118
'value' => $value,
119-
'custom_field_definition_id' => $key
119+
'custom_field_definition_id' => $this->getCustomFieldId($key, 'person')
120120
];
121121
}
122122
}
@@ -130,7 +130,7 @@ public function addPerson($finalData)
130130

131131
$apiEndpoint = $this->apiEmail . '/people';
132132

133-
return $response = HttpHelper::post($apiEndpoint, wp_json_encode($requestParams), $this->defaultHeader);
133+
return HttpHelper::post($apiEndpoint, wp_json_encode($requestParams), $this->defaultHeader);
134134
}
135135

136136
public function addOpportunity($finalData)
@@ -150,7 +150,7 @@ public function addOpportunity($finalData)
150150
} else {
151151
$requestParams['custom_fields'][] = (object) [
152152
'value' => $value,
153-
'custom_field_definition_id' => $key
153+
'custom_field_definition_id' => $this->getCustomFieldId($key, 'opportunity')
154154
];
155155
}
156156
}
@@ -176,7 +176,7 @@ public function addOpportunity($finalData)
176176

177177
$apiEndpoint = $this->apiEmail . '/opportunities';
178178

179-
return $response = HttpHelper::post($apiEndpoint, wp_json_encode($requestParams), $this->defaultHeader);
179+
return HttpHelper::post($apiEndpoint, wp_json_encode($requestParams), $this->defaultHeader);
180180
}
181181

182182
public function addTask($finalData)
@@ -197,7 +197,7 @@ public function addTask($finalData)
197197
} else {
198198
$requestParams['custom_fields'][] = (object) [
199199
'value' => $value,
200-
'custom_field_definition_id' => $key
200+
'custom_field_definition_id' => $this->getCustomFieldId($key, 'task')
201201
];
202202
}
203203
}
@@ -211,28 +211,22 @@ public function addTask($finalData)
211211

212212
$apiEndpoint = $this->apiEmail . '/tasks';
213213

214-
return $response = HttpHelper::post($apiEndpoint, wp_json_encode($requestParams), $this->defaultHeader);
214+
return HttpHelper::post($apiEndpoint, wp_json_encode($requestParams), $this->defaultHeader);
215215
}
216216

217217
public function generateReqDataFromFieldMap($data, $fieldMap)
218218
{
219219
$dataFinal = [];
220220
foreach ($fieldMap as $value) {
221-
$triggerValue = $value->formField;
222-
$actionValue = $value->coppercrmFormField;
223-
if ($triggerValue === 'custom') {
224-
if ($actionValue === 'custom_fields') {
225-
$dataFinal[$value->customFieldKey] = Common::replaceFieldWithValue($value->customValue, $data);
226-
} else {
227-
$dataFinal[$actionValue] = Common::replaceFieldWithValue($value->customValue, $data);
228-
}
229-
} elseif (!\is_null($data[$triggerValue])) {
230-
if ($actionValue === 'custom_fields') {
231-
$dataFinal[$value->customFieldKey] = $data[$triggerValue];
232-
} else {
233-
$dataFinal[$actionValue] = $data[$triggerValue];
234-
}
235-
}
221+
$triggerValue = $value->formField === 'custom' && !empty($value->customValue)
222+
? Common::replaceFieldWithValue($value->customValue, $data)
223+
: $value->formField;
224+
225+
$actionValue = $value->coppercrmFormField === 'customFieldKey' && !empty($value->customFieldKey)
226+
? Common::replaceFieldWithValue($value->customFieldKey, $data)
227+
: $value->coppercrmFormField;
228+
229+
$dataFinal[$actionValue] = $data[$triggerValue];
236230
}
237231

238232
return $dataFinal;
@@ -260,4 +254,42 @@ public function execute($fieldValues, $fieldMap, $actionName)
260254

261255
return $apiResponse;
262256
}
257+
258+
private function getCustomFieldId($key, $module)
259+
{
260+
$apiEndpoint = $this->apiEmail . '/custom_field_definitions';
261+
262+
$customFields = HttpHelper::get($apiEndpoint, null, $this->defaultHeader);
263+
264+
foreach ($customFields as $field) {
265+
if ($field->name === $key && \in_array($module, $field->available_on)) {
266+
return $field->id;
267+
}
268+
}
269+
270+
$body = [
271+
'name' => $key,
272+
'data_type' => 'String',
273+
'available_on' => [$module]
274+
];
275+
276+
$fieldDefinition = HttpHelper::post($apiEndpoint, wp_json_encode($body), $this->defaultHeader);
277+
278+
$status = $fieldDefinition->id ? 'success' : 'error';
279+
$message = $fieldDefinition->id ? 'Custom field created successfully' : 'Custom field creation failed';
280+
281+
LogHandler::save(
282+
$this->integrationId,
283+
wp_json_encode(
284+
[
285+
'type' => 'Create Custom Field',
286+
'type_name' => $message
287+
]
288+
),
289+
$status,
290+
wp_json_encode($fieldDefinition)
291+
);
292+
293+
return $fieldDefinition->id ?? $key;
294+
}
263295
}

0 commit comments

Comments
 (0)