Skip to content

Commit

Permalink
ApiV1ContactsController: PUT: Always update column contact.username
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Jun 18, 2024
1 parent 83037ce commit e535180
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions application/controllers/ApiV1ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ function (Filter\Condition $condition) {
$contactId = $this->getContactId($identifier);
if ($contactId !== null) {
if (! empty($data['username'])) {
$this->assertUniqueUsername($data['username']);
$this->assertUniqueUsername($data['username'], $contactId);
}

$db->update('contact', [
Expand Down Expand Up @@ -447,19 +447,24 @@ private function addContact(array $data): void
* Assert that the username is unique
*
* @param string $username
* @param int $contactId The id of the contact to exclude
*
* @return void
*
* @throws HttpException if the username already exists
*/
private function assertUniqueUsername(string $username): void
private function assertUniqueUsername(string $username, int $contactId = null): void
{
$user = Database::get()->fetchOne(
(new Select())
->from('contact')
->columns('1')
->where(['username = ?' => $username])
);
$stmt = (new Select())
->from('contact')
->columns('1')
->where(['username = ?' => $username]);

if ($contactId) {
$stmt->where(['id != ?' => $contactId]);
}

$user = Database::get()->fetchOne($stmt);

if ($user !== false) {
throw new HttpException(422, 'Username already exists');
Expand Down

0 comments on commit e535180

Please sign in to comment.