refactoring InterviewRequest, Profile
This commit is contained in:
parent
55089accb5
commit
e00f2b84ea
27
common/services/InterviewRequestService.php
Normal file
27
common/services/InterviewRequestService.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace common\services;
|
||||||
|
|
||||||
|
use common\models\InterviewRequest;
|
||||||
|
use Yii;
|
||||||
|
|
||||||
|
class InterviewRequestService
|
||||||
|
{
|
||||||
|
public static function createInterviewRequest($interviewRequestParams)
|
||||||
|
{
|
||||||
|
$interviewRequest = new InterviewRequest();
|
||||||
|
$attributes = $interviewRequestParams;
|
||||||
|
|
||||||
|
$interviewRequest->attributes = $attributes;
|
||||||
|
|
||||||
|
$interviewRequest->created_at = time();
|
||||||
|
$interviewRequest->user_id = \Yii::$app->user->id;
|
||||||
|
|
||||||
|
if ($interviewRequest->save()) {
|
||||||
|
\Yii::$app->telegram_bot->sendRenderedMessage('interview_request', $attributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $interviewRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -4,59 +4,101 @@ namespace common\services;
|
|||||||
|
|
||||||
use common\models\Manager;
|
use common\models\Manager;
|
||||||
use common\models\ManagerEmployee;
|
use common\models\ManagerEmployee;
|
||||||
|
use common\models\UserCard;
|
||||||
|
use frontend\modules\api\models\ProfileSearchForm;
|
||||||
|
use Yii;
|
||||||
|
use yii\web\BadRequestHttpException;
|
||||||
|
|
||||||
class ProfileService
|
class ProfileService
|
||||||
{
|
{
|
||||||
private $searcherID;
|
/**
|
||||||
private $id;
|
* @throws BadRequestHttpException
|
||||||
|
*/
|
||||||
public function __construct($searcherID, $id)
|
public static function getProfile($id, $request): ?array
|
||||||
{
|
{
|
||||||
$this->searcherID = $searcherID;
|
$searchModel = new ProfileSearchForm();
|
||||||
$this->id = $id;
|
$searchModel->attributes = $request;
|
||||||
|
|
||||||
|
if ($id) {
|
||||||
|
return $searchModel->byId();
|
||||||
|
}
|
||||||
|
return $searchModel->byParams();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function checkReportePermission()
|
/**
|
||||||
|
* @throws BadRequestHttpException
|
||||||
|
*/
|
||||||
|
public static function getProfileWithReportPermission($user_card_id): ?array
|
||||||
{
|
{
|
||||||
if ($this->isMyProfile() or $this->isMyEmployee()) {
|
if (UserCard::find()->where(['id' => $user_card_id])->exists()) {
|
||||||
|
|
||||||
|
$searchModel = new ProfileSearchForm();
|
||||||
|
$searchModel->id = $user_card_id;
|
||||||
|
$profile = $searchModel->byId();
|
||||||
|
|
||||||
|
self::addPermission($profile, $user_card_id);
|
||||||
|
return $profile;
|
||||||
|
}
|
||||||
|
throw new BadRequestHttpException(json_encode('There is no user with this id'));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function addPermission(&$profile, $user_card_id)
|
||||||
|
{
|
||||||
|
$searcherCardID = self::getSearcherCardID(Yii::$app->user->getId());
|
||||||
|
if (self::checkReportPermission($user_card_id, $searcherCardID)) {
|
||||||
|
$profile += ['report_permission' => '1'];
|
||||||
|
} else {
|
||||||
|
$profile += ['report_permission' => '0'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function getSearcherCardID($user_id): int
|
||||||
|
{
|
||||||
|
return UserCard::findOne(['id_user' => $user_id])->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function checkReportPermission($user_card_id, $searcherCardID): bool
|
||||||
|
{
|
||||||
|
if (self::isMyProfile($user_card_id, $searcherCardID)
|
||||||
|
or self::isMyEmployee($user_card_id, $searcherCardID)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function isMyProfile()
|
private static function isMyProfile($user_card_id, $searcherCardID): bool
|
||||||
{
|
{
|
||||||
if ($this->id == $this->searcherID) {
|
if ($user_card_id == $searcherCardID) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function isMyEmployee()
|
private static function isMyEmployee($user_card_id, $searcherCardID): bool
|
||||||
{
|
{
|
||||||
if (!$this->amIManager()) {
|
if (!self::amIManager($searcherCardID)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->isMyEmploee()) {
|
if (self::isMyEmployer($user_card_id, $searcherCardID)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function amIManager()
|
private static function amIManager($searcherCardID): bool
|
||||||
{
|
{
|
||||||
if (Manager::find()->where(['user_card_id' => $this->searcherID])->exists()) {
|
if (Manager::find()->where(['user_card_id' => $searcherCardID])->exists()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function isMyEmploee()
|
private static function isMyEmployer($user_card_id, $searcherCardID): bool
|
||||||
{
|
{
|
||||||
$manager = Manager::find()->where(['user_card_id' => $this->searcherID])->one();
|
$manager = Manager::find()->where(['user_card_id' => $searcherCardID])->one();
|
||||||
$exist = ManagerEmployee::find()
|
$exist = ManagerEmployee::find()
|
||||||
->where(['manager_id' => $manager->id, 'user_card_id' => $this->id])
|
->where(['manager_id' => $manager->id, 'user_card_id' => $user_card_id])
|
||||||
->exists();
|
->exists();
|
||||||
|
|
||||||
if ($exist) {
|
if ($exist) {
|
||||||
@ -64,7 +106,4 @@ class ProfileService
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace frontend\modules\api\controllers;
|
||||||
|
|
||||||
|
use common\models\InterviewRequest;
|
||||||
|
use common\services\InterviewRequestService;
|
||||||
|
use Yii;
|
||||||
|
use yii\base\InvalidConfigException;
|
||||||
|
use yii\web\ServerErrorHttpException;
|
||||||
|
|
||||||
|
|
||||||
|
class InterviewRequestController extends ApiController
|
||||||
|
{
|
||||||
|
public function verbs(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'create-interview-request' => ['post']
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws InvalidConfigException
|
||||||
|
* @throws ServerErrorHttpException
|
||||||
|
*/
|
||||||
|
public function actionCreateInterviewRequest(): InterviewRequest
|
||||||
|
{
|
||||||
|
$InterviewRequestModel = InterviewRequestService::createInterviewRequest(Yii::$app->getRequest()->getBodyParams());
|
||||||
|
if ($InterviewRequestModel->errors) {
|
||||||
|
throw new ServerErrorHttpException(json_encode($InterviewRequestModel->errors));
|
||||||
|
}
|
||||||
|
return $InterviewRequestModel;
|
||||||
|
}
|
||||||
|
}
|
@ -2,117 +2,29 @@
|
|||||||
|
|
||||||
namespace frontend\modules\api\controllers;
|
namespace frontend\modules\api\controllers;
|
||||||
|
|
||||||
use common\behaviors\GsCors;
|
|
||||||
use common\classes\Debug;
|
|
||||||
use common\models\InterviewRequest;
|
|
||||||
use common\models\User;
|
|
||||||
use common\models\UserCard;
|
|
||||||
use common\services\ProfileService;
|
use common\services\ProfileService;
|
||||||
use frontend\modules\api\models\ProfileSearchForm;
|
|
||||||
use kavalar\BotNotificationTemplateProcessor;
|
|
||||||
use kavalar\TelegramBotService;
|
|
||||||
use Yii;
|
|
||||||
use yii\filters\auth\CompositeAuth;
|
|
||||||
use yii\filters\auth\HttpBearerAuth;
|
|
||||||
use yii\filters\auth\QueryParamAuth;
|
|
||||||
use yii\filters\ContentNegotiator;
|
|
||||||
use yii\helpers\ArrayHelper;
|
|
||||||
use yii\web\BadRequestHttpException;
|
use yii\web\BadRequestHttpException;
|
||||||
use yii\web\Response;
|
|
||||||
|
|
||||||
|
|
||||||
class ProfileController extends ApiController
|
class ProfileController extends ApiController
|
||||||
{
|
{
|
||||||
|
public function verbs(): array
|
||||||
public function behaviors()
|
|
||||||
{
|
{
|
||||||
$parent = parent::behaviors();
|
return [
|
||||||
$b = [
|
'' => ['get'],
|
||||||
[
|
'profile-with-report-permission' => ['post', 'patch']
|
||||||
'class' => ContentNegotiator::className(),
|
|
||||||
'formats' => [
|
|
||||||
'application/json' => Response::FORMAT_JSON,
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'authenticator' => [
|
|
||||||
'class' => CompositeAuth::class,
|
|
||||||
'authMethods' => [
|
|
||||||
HttpBearerAuth::class,
|
|
||||||
],
|
|
||||||
]
|
|
||||||
];
|
];
|
||||||
|
|
||||||
return array_merge($parent, $b);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function actionIndex($id = null)
|
public function actionIndex($id = null)
|
||||||
{
|
{
|
||||||
$searchModel = new ProfileSearchForm();
|
return ProfileService::getProfile($id, \Yii::$app->request->get());
|
||||||
$searchModel->attributes = \Yii::$app->request->get();
|
|
||||||
|
|
||||||
if ($id) {
|
|
||||||
return $searchModel->byId();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $searchModel->byParams();
|
/**
|
||||||
}
|
* @throws BadRequestHttpException
|
||||||
|
*/
|
||||||
public function actionProfileWithReportPermission($id)
|
public function actionProfileWithReportPermission($id): ?array
|
||||||
{
|
{
|
||||||
$searchModel = new ProfileSearchForm();
|
return ProfileService::getProfileWithReportPermission($id);
|
||||||
$searchModel->attributes = \Yii::$app->request->get();
|
|
||||||
|
|
||||||
$searcherUser = Yii::$app->user->getId();
|
|
||||||
$searcherProfileId = UserCard::findOne($searcherUser)->id;
|
|
||||||
|
|
||||||
if ($id && $searcherProfileId) {
|
|
||||||
if(!UserCard::find()->where(['id' => $id])->exists())
|
|
||||||
{
|
|
||||||
throw new BadRequestHttpException(json_encode('There is no user with this id'));
|
|
||||||
}
|
|
||||||
$profile = $searchModel->byId();
|
|
||||||
|
|
||||||
$profileService = new ProfileService($searcherProfileId, $id);
|
|
||||||
|
|
||||||
if($profileService->checkReportePermission()) {
|
|
||||||
$profile += ['report_permission' => '1'];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$profile += ['report_permission' => '0'];
|
|
||||||
}
|
|
||||||
return $profile;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new BadRequestHttpException(json_encode('Missing required parameter'));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function actionAddToInterview()
|
|
||||||
{
|
|
||||||
if (\Yii::$app->request->isPost) {
|
|
||||||
$attributes = \Yii::$app->request->post();
|
|
||||||
|
|
||||||
$model = new InterviewRequest();
|
|
||||||
$model->attributes = $attributes;
|
|
||||||
$model->created_at = time();
|
|
||||||
$model->user_id = \Yii::$app->user->id;
|
|
||||||
if ($model->save()) {
|
|
||||||
\Yii::$app->telegram_bot->sendRenderedMessage('interview_request', $attributes);
|
|
||||||
return ['status' => 'success'];
|
|
||||||
}
|
|
||||||
|
|
||||||
\Yii::$app->response->statusCode = 400;
|
|
||||||
return ['status' => 'error', 'errors' => $model->errors];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function actionMe()
|
|
||||||
{
|
|
||||||
if(isset(\Yii::$app->user->id)){
|
|
||||||
$user = User::find()->with('userCard')->where(['id' => \Yii::$app->user->id])->one();
|
|
||||||
}
|
|
||||||
|
|
||||||
\Yii::$app->response->statusCode = 401;
|
|
||||||
return ['status' => 'error', 'errors' => 'No authorized'];
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user