add project statistic to api

This commit is contained in:
iIronside
2023-11-21 11:23:38 +03:00
parent 91607cc99b
commit 75329a8835
24 changed files with 929 additions and 25 deletions

View File

@ -7,6 +7,7 @@ use common\models\Status;
use common\models\UseStatus;
use frontend\modules\api\models\Manager;
use frontend\modules\api\models\project\Project;
use frontend\modules\api\models\project\ProjectStatistic;
use frontend\modules\api\models\project\ProjectUser;
use Yii;
use yii\base\InvalidConfigException;
@ -501,4 +502,40 @@ class ProjectController extends ApiController
return $project;
}
/**
*
* @OA\Get(path="/project/statistic",
* summary="Получить статистику проекта",
* description="Метод для получения статистики проета",
* security={
* {"bearerAuth": {}}
* },
* tags={"TaskManager"},
* @OA\Parameter(
* name="project_id",
* in="query",
* required=true,
* @OA\Schema(
* type="integer",
* default=null
* )
* ),
* @OA\Response(
* response=200,
* description="Возвращает объект статистики проекта",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(ref="#/components/schemas/ProjectStatisticExample"),
* ),
* ),
* )
*
* @param $project_id
* @return array|ActiveRecord|null
*/
public function actionStatistic($project_id): array|ActiveRecord|null
{
return ProjectStatistic::find()->where(['id' => $project_id])->one();
}
}

View File

@ -0,0 +1,142 @@
<?php
namespace frontend\modules\api\models\project;
/**
*
* @OA\Schema(
* schema="ProjectParticipants",
* @OA\Property(
* property="avatar",
* type="string",
* example="/hbfhdb/b4.png",
* description="Ссылка на аватар профиля"
* ),
* @OA\Property(
* property="username",
* type="string",
* example="username",
* description="ФИО пользователя"
* ),
* @OA\Property(
* property="email",
* type="string",
* example="test@email.com",
* description="Email пользователя"
* ),
* @OA\Property(
* property="role",
* type="string",
* example="Разработчик",
* description="Роль пользователя на проекте"
* ),
* @OA\Property(
* property="status",
* type="int",
* example="0",
* description="Статус (0 - не активен, 1 - активен)"
* ),
*)
*
* @OA\Schema(
* schema="ProjectParticipantsExample",
* type="array",
* example={"avatar": "/hbfhdb/b4.png", "username": "username", "email": "test@email.com", "role": "Разработчик", "status": 0},
* @OA\Items(
* type="object",
* @OA\Property(
* property="avatar",
* type="string",
* example="/hbfhdb/b4.png",
* ),
* @OA\Property(
* property="username",
* type="string",
* example="username",
* ),
* @OA\Property(
* property="email",
* type="string",
* example="test@email.com",
* ),
* @OA\Property(
* property="role",
* type="string",
* example="Разработчик",
* ),
* @OA\Property(
* property="status",
* type="int",
* example="0",
* ),
* ),
* )
*
*
*
*
* @OA\Schema(
* schema="ProjectParticipantsExampleArr",
* type="array",
* example={
* {"avatar": "/hbfhdb/b4.png", "username": "username", "email": "test@email.com", "role": "Разработчик", "status": 0},
* {"avatar": "/hbfhdb/b4.png", "username": "username", "email": "test@email.com", "role": "Разработчик", "status": 1},
* },
* @OA\Items(
* type="object",
* @OA\Property(
* property="avatar",
* type="string",
* example="/hbfhdb/b4.png",
* ),
* @OA\Property(
* property="username",
* type="string",
* example="username",
* ),
* @OA\Property(
* property="email",
* type="string",
* example="test@email.com",
* ),
* @OA\Property(
* property="role",
* type="string",
* example="Разработчик",
* ),
* @OA\Property(
* property="status",
* type="int",
* example="0",
* ),
* ),
*)
*
*/
class ProjectParticipants extends ProjectUser
{
/**
* @return string[]
*/
public function fields(): array
{
return [
'avatar' => function () {
return $this->user->userCard->photo ?? '';
},
'username' => function () {
return $this->card->fio ?? null;
},
'email' => function () {
return $this->card->email ?? $this->user->email;
},
'role' => function () {
return $this->projectRole->title ?? null;
},
'status' => function () {
return $this->status ?? null;
},
];
}
}

View File

@ -0,0 +1,139 @@
<?php
namespace frontend\modules\api\models\project;
use frontend\modules\api\models\Company;
use yii\db\ActiveQuery;
use yii\helpers\Url;
use yii\web\Link;
/**
*
* @OA\Schema(
* schema="ProjectStatistic",
* @OA\Property(
* property="creator",
* ref="#/components/schemas/ProjectParticipants",
* ),
* @OA\Property(
* property="open_tasks_count",
* type="int",
* example="10",
* ),
* @OA\Property(
* property="task_on_work_count",
* type="int",
* example="15",
* ),
* @OA\Property(
* property="closed_task_count",
* type="int",
* example="234",
* ),
* @OA\Property(
* property="participants",
* ref="#/components/schemas/ProjectParticipants",
* ),
*)
*
* @OA\Schema(
* schema="ProjectStatisticExample",
* type="array",
* @OA\Items(
* type="object",
* @OA\Property(
* property="creator",
* ref="#/components/schemas/ProjectParticipantsExample",
* ),
* @OA\Property(
* property="open_tasks_count",
* type="int",
* example="10"
* ),
* @OA\Property(
* property="task_on_work_count",
* type="int",
* example="15"
* ),
* @OA\Property(
* property="closed_task_count",
* type="integer",
* example="324"
* ),
* @OA\Property(
* property="participants",
* ref="#/components/schemas/ProjectParticipantsExampleArr",
* ),
* ),
*)
*
*
* @property Company $company
* @property ProjectParticipants $owner
* @property ProjectUser[] $participants
*/
class ProjectStatistic extends \common\models\Project
{
public function fields(): array
{
return [
'creator' => function () {
return $this->owner;
},
'open_tasks_count'=> function () {
return $this->getProjectTask()->where(['status' => ProjectTask::STATUS_ACTIVE])->count();
},
'task_on_work_count'=> function () {
return $this->getProjectTask()->where(['status' => ProjectTask::STATUS_AT_WORK])->count();
},
'closed_task_count'=> function () {
return $this->getProjectTask()->where(['status' => ProjectTask::STATUS_ARCHIVE])->count();
},
'participants'=> function () {
return $this->participants;
},
];
}
/**
* @return string[]
*/
public function extraFields(): array
{
return [];
}
/**
* @return ActiveQuery
*/
public function getOwner()
{
return $this->hasOne(ProjectParticipants::class, ['id' => 'owner_id']);
}
/**
* @return ActiveQuery
*/
public function getProjectUsers()
{
return $this->hasMany(ProjectUser::class, ['project_id' => 'id']);
}
public function getLinks(): array
{
return [
Link::REL_SELF => Url::to(['index', 'project_id' => $this->id], true),
];
}
public function getCompany(): ActiveQuery
{
return $this->hasOne(Company::class, ['id' => 'company_id']);
}
public function getParticipants()
{
return $this->hasMany(ProjectParticipants::class, ['project_id' => 'id']);
}
}