Skip to content

Commit 66f176e

Browse files
committed
fix: freshdesk contact creating & custom field issue fixed
1 parent a57c841 commit 66f176e

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

includes/Actions/Freshdesk/FreshdeskController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
namespace BitCode\FI\Actions\Freshdesk;
88

9-
use WP_Error;
109
use BitCode\FI\Core\Util\HttpHelper;
10+
use WP_Error;
1111

1212
/**
1313
* Provide functionality for Freshdesk integration
@@ -161,7 +161,11 @@ public function getAllContactFields($tokenRequestParams)
161161
$responseData = [];
162162
foreach ($apiResponse as $value) {
163163
if (!\in_array($value->name, $excludingFields)) {
164-
$responseData[] = (object) ['key' => $value->name, 'label' => $value->label, 'required' => $value->name == 'email' || $value->name == 'name' ? true : $value->required_for_agents];
164+
$responseData[] = (object) [
165+
'key' => $value->default ? $value->name : "btcbi_cf_{$value->name}",
166+
'label' => $value->label,
167+
'required' => $value->name == 'email' || $value->name == 'name' ? true : $value->required_for_agents
168+
];
165169
}
166170
}
167171

includes/Actions/Freshdesk/RecordApiHelper.php

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,32 @@ public function generateReqDataFromFieldMap($data, $fieldMap)
8282

8383
public function generateReqDataFromFieldMapContact($data, $fieldMapContact)
8484
{
85-
$dataFinalContact = [];
85+
$fieldMap = [];
86+
$customFields = [];
8687

87-
foreach ($fieldMapContact as $key => $value) {
88-
$triggerValue = $value->formField;
89-
$actionValue = $value->contactFreshdeskFormField;
90-
if ($triggerValue === 'custom') {
91-
$dataFinalContact[$actionValue] = Common::replaceFieldWithValue($value->customValue, $data);
92-
} elseif (!\is_null($data[$triggerValue])) {
93-
$dataFinalContact[$actionValue] = $data[$triggerValue];
88+
foreach ($fieldMapContact as $key => $field) {
89+
$actionValue = $field->contactFreshdeskFormField;
90+
91+
if ($field->formField === 'custom' && isset($field->customValue)) {
92+
$triggerValue = Common::replaceFieldWithValue($field->customValue, $data);
93+
} else {
94+
$triggerValue = $field->formField;
95+
}
96+
97+
if (strpos($actionValue, 'btcbi_cf_') === 0) {
98+
$fieldName = substr($actionValue, 9);
99+
100+
$customFields[$fieldName] = $data[$triggerValue];
101+
} else {
102+
$fieldMap[$actionValue] = $data[$triggerValue];
94103
}
95104
}
96105

97-
return $dataFinalContact;
106+
if (!empty($customFields)) {
107+
$fieldMap['custom_fields'] = $customFields;
108+
}
109+
110+
return $fieldMap;
98111
}
99112

100113
public function fetchContact($app_base_domamin, $email, $api_key)
@@ -137,21 +150,22 @@ public function insertContact($app_base_domamin, $finalDataContact, $api_key, $a
137150
400
138151
);
139152
}
140-
$header = [
141-
'Authorization' => base64_encode("{$api_key}"),
142-
'Content-Type' => 'multipart/form-data'
143-
];
144153

145-
$data = $finalDataContact;
146154
$apiEndpoint = $app_base_domamin . '/api/v2/contacts/';
155+
147156
if ($avatar) {
148157
$data = $finalDataContact + ['avatar' => static::getAvatar($avatar)];
149158
$sendPhotoApiHelper = new FilesApiHelper();
150159

151160
return $sendPhotoApiHelper->uploadFiles($apiEndpoint, $data, $api_key);
152161
}
153162

154-
return HttpHelper::post($apiEndpoint, $data, $header);
163+
$header = [
164+
'Authorization' => base64_encode("{$api_key}"),
165+
'Content-Type' => 'application/json'
166+
];
167+
168+
return HttpHelper::post($apiEndpoint, wp_json_encode($finalDataContact), $header);
155169
}
156170

157171
public function updateContact($app_base_domamin, $finalDataContact, $api_key, $contactId)
@@ -187,7 +201,6 @@ public function execute(
187201
$integrationDetails,
188202
$app_base_domamin
189203
) {
190-
$fieldData = [];
191204
$finalData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap);
192205
$finalData = $finalData + ['status' => json_decode($integrationDetails->status)] + ['priority' => json_decode($integrationDetails->priority)];
193206

@@ -207,36 +220,43 @@ public function execute(
207220
$finalData['responder_id'] = (int) $integrationDetails->selected_ticket_agent;
208221
}
209222

210-
if ($integrationDetails->updateContact && $integrationDetails->contactShow) {
223+
if ($integrationDetails->contactShow) {
211224
$finalDataContact = $this->generateReqDataFromFieldMapContact($fieldValues, $fieldMapContact);
212225
$avatarFieldName = $integrationDetails->actions->attachments;
213226
$avatar = $fieldValues[$avatarFieldName];
214227
$apiResponseFetchContact = $this->fetchContact($app_base_domamin, $finalDataContact['email'], $integrationDetails->api_key);
228+
215229
if (empty($apiResponseFetchContact)) {
230+
$typeName = 'create-contact';
216231
$apiResponseContact = $this->insertContact($app_base_domamin, $finalDataContact, $integrationDetails->api_key, $avatar);
217-
} else {
232+
} elseif ($integrationDetails->updateContact) {
233+
$typeName = 'update-contact';
218234
$contactId = $apiResponseFetchContact[0]->id;
219235
$apiResponseContact = $this->updateContact($app_base_domamin, $finalDataContact, $integrationDetails->api_key, $contactId);
236+
} else {
237+
$typeName = 'fetch-contact';
238+
$apiResponseContact = ['message' => 'Contact already exists'];
220239
}
221-
}
222240

223-
if ($integrationDetails->contactShow) {
224-
$finalDataContact = $this->generateReqDataFromFieldMapContact($fieldValues, $fieldMapContact);
225-
$avatarFieldName = $integrationDetails->actions->attachments;
226-
$avatar = $fieldValues[$avatarFieldName];
227-
$apiResponseFetchContact = $this->fetchContact($app_base_domamin, $finalDataContact['email'], $integrationDetails->api_key);
228-
if (empty($apiResponseFetchContact)) {
229-
$apiResponseContact = $this->insertContact($app_base_domamin, $finalDataContact, $integrationDetails->api_key, $avatar);
241+
$responseType = 'error';
242+
if (isset($apiResponseContact->id)) {
243+
$finalData['requester_id'] = $apiResponseContact->id;
244+
$responseType = 'success';
230245
}
246+
247+
$finalData['requester_id'] = isset($apiResponseContact->id) ? $apiResponseContact->id : '';
248+
249+
LogHandler::save($this->_integrationID, wp_json_encode(['type' => 'contact', 'type_name' => $typeName]), $responseType, wp_json_encode($apiResponseContact));
231250
}
251+
232252
$attachmentsFieldName = $integrationDetails->actions->file;
233253
$fileTicket = $fieldValues[$attachmentsFieldName];
234254
$apiResponse = $this->insertTicket($apiEndpoint, $finalData, $integrationDetails->api_key, $fileTicket);
235255

236256
if (property_exists($apiResponse, 'errors')) {
237-
LogHandler::save($this->_integrationID, wp_json_encode(['type' => 'contact', 'type_name' => 'add-contact']), 'error', wp_json_encode($apiResponse));
257+
LogHandler::save($this->_integrationID, wp_json_encode(['type' => 'ticket', 'type_name' => 'create-ticket']), 'error', wp_json_encode($apiResponse));
238258
} else {
239-
LogHandler::save($this->_integrationID, wp_json_encode(['type' => 'record', 'type_name' => 'add-contact']), 'success', wp_json_encode($apiResponse));
259+
LogHandler::save($this->_integrationID, wp_json_encode(['type' => 'ticket', 'type_name' => 'create-ticket']), 'success', wp_json_encode($apiResponse));
240260
}
241261

242262
return $apiResponse;

0 commit comments

Comments
 (0)