Skip to content

Commit

Permalink
ApiV1(*)Controller: GET: Don't omit optional fields when empty
Browse files Browse the repository at this point in the history
Return the default empty data type instead
  • Loading branch information
sukhwinder33445 committed Jun 27, 2024
1 parent 2910452 commit a976477
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 51 deletions.
18 changes: 5 additions & 13 deletions application/controllers/ApiV1ContactgroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,7 @@ function (Filter\Condition $condition) {
$this->httpNotFound('Contactgroup not found');
}

$users = $this->fetchUserIdentifiers($result->contactgroup_id);
if ($users) {
$result->users = $users;
}
$result->users = $this->fetchUserIdentifiers($result->contactgroup_id);

unset($result->contactgroup_id);
$results[] = $result;
Expand Down Expand Up @@ -137,10 +134,7 @@ function (Filter\Condition $condition) {
do {
/** @var stdClass $row */
foreach ($res as $i => $row) {
$users = $this->fetchUserIdentifiers($row->contactgroup_id);
if ($users) {
$row->users = $users;
}
$row->users = $this->fetchUserIdentifiers($row->contactgroup_id);

if ($i > 0 || $offset !== 0) {
echo ",\n";
Expand Down Expand Up @@ -260,20 +254,18 @@ function (Filter\Condition $condition) {
*
* @param int $contactgroupId
*
* @return ?string[]
* @return string[]
*/
private function fetchUserIdentifiers(int $contactgroupId): ?array
private function fetchUserIdentifiers(int $contactgroupId): array
{
$users = Database::get()->fetchCol(
return Database::get()->fetchCol(
(new Select())
->from('contactgroup_member cgm')
->columns('co.external_uuid')
->joinLeft('contact co', 'co.id = cgm.contact_id')
->where(['cgm.contactgroup_id = ?' => $contactgroupId])
->groupBy('co.external_uuid')
);

return $users ?: null;
}

/**
Expand Down
48 changes: 10 additions & 38 deletions application/controllers/ApiV1ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,8 @@ function (Filter\Condition $condition) {
$this->httpNotFound('Contact not found');
}

if ($result->username === null) {
unset($result->username);
}

$groups = $this->fetchGroupIdentifiers($result->contact_id);
if ($groups) {
$result->groups = $groups;
}

$addresses = $this->fetchContactAddresses($result->contact_id);
if ($addresses) {
$result->addresses = $addresses;
}
$result->groups = $this->fetchGroupIdentifiers($result->contact_id);
$result->addresses = $this->fetchContactAddresses($result->contact_id);

unset($result->contact_id);
$results[] = $result;
Expand Down Expand Up @@ -155,19 +144,8 @@ function (Filter\Condition $condition) {
do {
/** @var stdClass $row */
foreach ($res as $i => $row) {
if ($row->username === null) {
unset($row->username);
}

$groups = $this->fetchGroupIdentifiers($row->contact_id);
if ($groups) {
$row->groups = $groups;
}

$addresses = $this->fetchContactAddresses($row->contact_id);
if ($addresses) {
$row->addresses = $addresses;
}
$row->groups = $this->fetchGroupIdentifiers($row->contact_id);
$row->addresses = $this->fetchContactAddresses($row->contact_id);

if ($i > 0 || $offset !== 0) {
echo ",\n";
Expand Down Expand Up @@ -326,9 +304,9 @@ private function getChannelId(string $channelName): int
*
* @param int $contactId
*
* @return ?string
* @return string
*/
private function fetchContactAddresses(int $contactId): ?string
private function fetchContactAddresses(int $contactId): string
{
/** @var array<string, string> $addresses */
$addresses = Database::get()->fetchPairs(
Expand All @@ -338,32 +316,26 @@ private function fetchContactAddresses(int $contactId): ?string
->where(['contact_id = ?' => $contactId])
);

if (! empty($addresses)) {
return json_encode($addresses) ?: null;
}

return null;
return Json::sanitize($addresses, JSON_FORCE_OBJECT);
}

/**
* Fetch the group identifiers of the contact with the given id
*
* @param int $contactId
*
* @return ?string[]
* @return string[]
*/
private function fetchGroupIdentifiers(int $contactId): ?array
private function fetchGroupIdentifiers(int $contactId): array
{
$groups = Database::get()->fetchCol(
return Database::get()->fetchCol(
(new Select())
->from('contactgroup_member cgm')
->columns('cg.external_uuid')
->joinLeft('contactgroup cg', 'cg.id = cgm.contactgroup_id')
->where(['cgm.contact_id = ?' => $contactId])
->groupBy('cg.external_uuid')
);

return $groups ?: null;
}

/**
Expand Down

0 comments on commit a976477

Please sign in to comment.