profile and reports fix

This commit is contained in:
andrey 2021-11-30 16:54:04 +03:00
parent df5823730f
commit 941a3964c6
3 changed files with 52 additions and 22 deletions

View File

@ -201,6 +201,11 @@ class UserCard extends \yii\db\ActiveRecord
return $this->hasMany(CardSkill::class, ['card_id' => 'id'])->with('skill'); return $this->hasMany(CardSkill::class, ['card_id' => 'id'])->with('skill');
} }
public function getUser()
{
return $this->hasOne(User::class, ['id' => 'id_user']);
}
public static function getNameSkills() public static function getNameSkills()
{ {
return ArrayHelper::map(Skill::find()->all(), 'id', 'name'); return ArrayHelper::map(Skill::find()->all(), 'id', 'name');
@ -268,4 +273,6 @@ class UserCard extends \yii\db\ActiveRecord
$user_card->id_user = $user_id; $user_card->id_user = $user_id;
$user_card->save(); $user_card->save();
} }
} }

View File

@ -6,6 +6,7 @@ use common\behaviors\GsCors;
use common\classes\Debug; use common\classes\Debug;
use common\models\Reports; use common\models\Reports;
use common\models\ReportsTask; use common\models\ReportsTask;
use common\models\UserCard;
use frontend\modules\api\models\ReportSearchForm; use frontend\modules\api\models\ReportSearchForm;
use JsonException; use JsonException;
use Yii; use Yii;
@ -49,6 +50,12 @@ class ReportsController extends ApiController
$reportsModel = new ReportSearchForm(); $reportsModel = new ReportSearchForm();
$params = Yii::$app->request->get(); $params = Yii::$app->request->get();
if(!isset($params['user_card_id'])){
$userCard = UserCard::find()->where(['id_user' => Yii::$app->user->id])->one();
if($userCard){
$params['user_card_id'] = $userCard->id;
}
}
$reportsModel->attributes = $params; $reportsModel->attributes = $params;
if(!$reportsModel->validate()){ if(!$reportsModel->validate()){
@ -69,31 +76,25 @@ class ReportsController extends ApiController
throw new BadRequestHttpException('Нет параметра tasks'); throw new BadRequestHttpException('Нет параметра tasks');
} }
if(!isset($params['user_card_id'])){
$userCard = UserCard::find()->where(['id_user' => Yii::$app->user->id])->one();
if($userCard){
$params['user_card_id'] = $userCard->id;
}
}
$reportsModel = new Reports(); $reportsModel = new Reports();
$reportsModel->attributes = $params; $reportsModel->attributes = $params;
$params['tasks'] = (is_array($params['tasks'])) ? $params['tasks'] : json_decode($params['tasks']);
if(!$reportsModel->validate()){ if(!$reportsModel->validate()){
throw new BadRequestHttpException(json_encode($reportsModel->errors)); throw new BadRequestHttpException(json_encode($reportsModel->errors));
} }
$tasks = [];
foreach (json_decode($params['tasks'], true) as $jsonTask){
$task = new ReportsTask();
$task->scenario = ReportsTask::SCENARIO_WITHOUT_REPORT_ID;
$task->attributes = $jsonTask;
if (!$task->validate()) {
throw new BadRequestHttpException(json_encode($task->errors));
}
$tasks []= $task->attributes;
}
$attributes = $task->attributes();
$reportsModel->save(); $reportsModel->save();
$tasks = array_map(function ($task)use($reportsModel){$task['report_id'] = $reportsModel->id; return $task;}, $tasks);
Yii::$app->db->createCommand()->batchInsert(ReportsTask::tableName(), $attributes, $tasks)->execute(); return array_merge($reportsModel->toArray());
return array_merge($reportsModel->toArray(), ['tasks' => $tasks]);
} }
public function actionDelete() public function actionDelete()

View File

@ -32,6 +32,18 @@ class ProfileSearchForm extends Model
]; ];
} }
public function exclude($arr)
{
$ex = ['fio', 'passport', 'resume', 'link_vk', 'link_telegram', 'email', 'salary'];
foreach ($ex as $remove) {
if (isset($arr[$remove])) {
unset($arr[$remove]);
}
}
return $arr;
}
public function checkIsArray() public function checkIsArray()
{ {
@ -43,12 +55,12 @@ class ProfileSearchForm extends Model
public function byId() public function byId()
{ {
if ($this->id) { if ($this->id) {
return UserCard::find() return $this->exclude(UserCard::find()
->where(['id' => $this->id]) ->where(['id' => $this->id])
->with(['skillValues']) ->with(['skillValues'])
->with(['achievements']) ->with(['achievements'])
->asArray() ->asArray()
->one(); ->one());
} }
return null; return null;
@ -64,8 +76,7 @@ class ProfileSearchForm extends Model
$this->skills = explode(',', $this->skills); $this->skills = explode(',', $this->skills);
$model->where(['card_skill.skill_id' => $this->skills]); $model->where(['card_skill.skill_id' => $this->skills]);
$model->having('COUNT(DISTINCT skill_id) = ' . count($this->skills)); $model->having('COUNT(DISTINCT skill_id) = ' . count($this->skills));
} } else {
else{
$model->joinWith('skillValues'); $model->joinWith('skillValues');
} }
@ -78,8 +89,19 @@ class ProfileSearchForm extends Model
$model->groupBy('card_skill.card_id'); $model->groupBy('card_skill.card_id');
return $model->limit($this->limit) $res = $model->limit($this->limit)
->offset($this->offset)->orderBy('updated_at DESC')->asArray()->all(); ->offset($this->offset)->orderBy('updated_at DESC')->asArray()->all();
if(!$res){
return [];
}
$resArr = [];
foreach ($res as $re){
$resArr[] = $this->exclude($re);
}
return $resArr;
} }
} }