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');
}
public function getUser()
{
return $this->hasOne(User::class, ['id' => 'id_user']);
}
public static function getNameSkills()
{
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->save();
}
}

View File

@ -6,6 +6,7 @@ use common\behaviors\GsCors;
use common\classes\Debug;
use common\models\Reports;
use common\models\ReportsTask;
use common\models\UserCard;
use frontend\modules\api\models\ReportSearchForm;
use JsonException;
use Yii;
@ -49,6 +50,12 @@ class ReportsController extends ApiController
$reportsModel = new ReportSearchForm();
$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;
if(!$reportsModel->validate()){
@ -69,31 +76,25 @@ class ReportsController extends ApiController
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->attributes = $params;
$params['tasks'] = (is_array($params['tasks'])) ? $params['tasks'] : json_decode($params['tasks']);
if(!$reportsModel->validate()){
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();
$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(), ['tasks' => $tasks]);
return array_merge($reportsModel->toArray());
}
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()
{
@ -43,12 +55,12 @@ class ProfileSearchForm extends Model
public function byId()
{
if ($this->id) {
return UserCard::find()
return $this->exclude(UserCard::find()
->where(['id' => $this->id])
->with(['skillValues'])
->with(['achievements'])
->asArray()
->one();
->one());
}
return null;
@ -64,8 +76,7 @@ class ProfileSearchForm extends Model
$this->skills = explode(',', $this->skills);
$model->where(['card_skill.skill_id' => $this->skills]);
$model->having('COUNT(DISTINCT skill_id) = ' . count($this->skills));
}
else{
} else {
$model->joinWith('skillValues');
}
@ -78,8 +89,19 @@ class ProfileSearchForm extends Model
$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();
if(!$res){
return [];
}
$resArr = [];
foreach ($res as $re){
$resArr[] = $this->exclude($re);
}
return $resArr;
}
}