66
77namespace BitCode \FI \Actions \CopperCRM ;
88
9- use BitCode \FI \Log \LogHandler ;
109use BitCode \FI \Core \Util \Common ;
1110use 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