Assign extensions to managers

For more convenient business, you can provide matching between the extensions provided by the VoIP service and the managers of the Kommo account, in order to provide more information about the calls. This information will be used when storing the call like which manager is responsible for this call, and in smart forwarding if it’s implemented.

Here’s an example on how to implement this. The table VoIPUsers contains the extensions provided by the VoIP service. Some of them are already assigned and some are not. We need to assign an unassigned extension to a new user. Review the suggested database diagram.

function assignExtension(int $KommoAccountId, int $KommoUserId, int $extensionId)
{ 
  $managerExtension =  VoIPUsers::query()
            ->where('KommoAccountId', '=', $KommoAccountId)
            ->where('KommoUserId', '=', $KommoUserId)
            ->where('extensionId', '=', $extensionId);

  if ($managerExtension) {
            throw BusinessLogicException::create('Extension already used or Manager already assigned');
  }
  //if the no user returned then we create a user
  return VoIPUsers::query()
            ->where('KommoAccountId', '=', $KommoAccountId)
            ->where('extensionId', '=', $extensionId)
            ->first()
            ?->setKommoUserId($KommoUserId)
            ->save();
}

Next smart forwarding use case.