2021-11-17 12:24:02 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace frontend\modules\api\controllers;
|
|
|
|
|
|
|
|
use common\models\ManagerEmployee;
|
|
|
|
use common\models\User;
|
2022-01-05 17:11:31 +03:00
|
|
|
use common\models\UserCard;
|
2021-11-17 12:24:02 +03:00
|
|
|
use Yii;
|
|
|
|
use yii\filters\auth\HttpBearerAuth;
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
use yii\web\NotFoundHttpException;
|
2022-01-11 13:37:30 +03:00
|
|
|
use yii\rest\Controller;
|
2021-11-17 12:24:02 +03:00
|
|
|
|
2022-01-11 13:37:30 +03:00
|
|
|
class ManagerController extends Controller
|
2021-11-17 12:24:02 +03:00
|
|
|
{
|
|
|
|
public function behaviors(): array
|
|
|
|
{
|
|
|
|
$behaviors = parent::behaviors();
|
|
|
|
|
|
|
|
$behaviors['authenticator']['authMethods'] = [
|
|
|
|
HttpBearerAuth::className(),
|
|
|
|
];
|
|
|
|
|
|
|
|
return $behaviors;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function verbs(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'get-manager' => ['get'],
|
|
|
|
'get-employees-manager' => ['get'],
|
|
|
|
'get-manager-list' => ['get'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionGetManagerList(): array
|
|
|
|
{
|
2022-01-05 17:11:31 +03:00
|
|
|
$managers = UserCard::find()->select(['fio','manager.id' , 'email'])
|
|
|
|
->joinWith('manager')->where(['NOT',['manager.user_card_id' => null]])->all();
|
2021-11-17 12:24:02 +03:00
|
|
|
|
|
|
|
if(empty($managers)) {
|
|
|
|
throw new NotFoundHttpException('Managers are not assigned');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $managers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
*/
|
|
|
|
public function actionGetEmployeesManager()
|
|
|
|
{
|
|
|
|
$manager_id = Yii::$app->request->get('manager_id');
|
|
|
|
if(empty($manager_id) or !is_numeric($manager_id))
|
|
|
|
{
|
|
|
|
throw new NotFoundHttpException('Incorrect manager ID');
|
|
|
|
}
|
|
|
|
|
2022-01-05 17:11:31 +03:00
|
|
|
$users_list = UserCard::find()
|
|
|
|
->select(['manager_employee.id', 'user_card.fio', 'user_card.email'])
|
2021-11-17 12:24:02 +03:00
|
|
|
->joinWith('managerEmployee')
|
|
|
|
->where(['manager_employee.manager_id' => $manager_id])
|
|
|
|
->all();
|
|
|
|
|
|
|
|
if(empty($users_list)) {
|
|
|
|
throw new NotFoundHttpException('Managers are not assigned or employees are not assigned to him');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $users_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws NotFoundHttpException
|
|
|
|
*/
|
|
|
|
public function actionGetManager(): array
|
|
|
|
{
|
|
|
|
$manager_id = Yii::$app->request->get('manager_id');
|
|
|
|
if(empty($manager_id) or !is_numeric($manager_id))
|
|
|
|
{
|
|
|
|
throw new NotFoundHttpException('Incorrect manager ID');
|
|
|
|
}
|
|
|
|
|
2022-01-05 17:11:31 +03:00
|
|
|
$manager = UserCard::find()
|
|
|
|
->select(['manager.id', 'fio', 'email', 'photo', 'gender'])
|
2021-11-17 12:24:02 +03:00
|
|
|
->joinWith('manager')->where(['manager.id' => $manager_id])
|
|
|
|
->all();
|
|
|
|
|
|
|
|
|
|
|
|
if(empty($manager)) {
|
|
|
|
throw new NotFoundHttpException('There is no such manager');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $manager;
|
|
|
|
}
|
|
|
|
}
|