add to project email

This commit is contained in:
Kavalar 2024-02-06 18:01:05 +03:00
parent ea588a8aee
commit 4845f7944b
4 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,10 @@
<?php
/* @var $this yii\web\View */
/* @var $user common\models\User */
/* @var $project \common\models\Project */
?>
<p>Здравствуйте, <b><?= $user->username ?></b>,</p>
Вас добавили в проект <a href="https://itguild.info/tracker/project/<?= $project->id ?>"><?= $project->name ?></a> .

View File

@ -0,0 +1 @@
<?php

View File

@ -0,0 +1,25 @@
<?php
namespace common\models\email;
use common\models\Project;
use common\models\ProjectUser;
use common\models\User;
use Yii;
class AddToProjectEmail extends Email
{
/**
* @param User $user
* @param Project $project
*/
public function __construct(User $user, Project $project)
{
$this->sendTo = $user->email;
$this->subject = 'Вас добавили в проект';
$this->mailLayout = ['html' => 'addToProjectByEmail-html', 'text' => 'addToProjectByEmail-text'];
$this->params = ['user' => $user, 'project' => $project];
}
}

View File

@ -2,10 +2,12 @@
namespace frontend\modules\api\controllers; namespace frontend\modules\api\controllers;
use common\models\email\AddToProjectEmail;
use common\models\ProjectTaskCategory; use common\models\ProjectTaskCategory;
use common\models\Status; use common\models\Status;
use common\models\User; use common\models\User;
use common\models\UseStatus; use common\models\UseStatus;
use common\services\EmailService;
use frontend\modules\api\models\Manager; use frontend\modules\api\models\Manager;
use frontend\modules\api\models\project\Project; use frontend\modules\api\models\project\Project;
use frontend\modules\api\models\project\ProjectStatistic; use frontend\modules\api\models\project\ProjectStatistic;
@ -20,12 +22,19 @@ use yii\web\NotFoundHttpException;
class ProjectController extends ApiController class ProjectController extends ApiController
{ {
public EmailService $emailService;
public $modelClass = 'frontend\modules\api\models\Project'; public $modelClass = 'frontend\modules\api\models\Project';
public $serializer = [ public $serializer = [
'class' => 'yii\rest\Serializer', 'class' => 'yii\rest\Serializer',
'collectionEnvelope' => 'projects', 'collectionEnvelope' => 'projects',
]; ];
public function __construct($id, $module, EmailService $emailService, $config = [])
{
$this->emailService = $emailService;
parent::__construct($id, $module, $config);
}
public function behaviors(): array public function behaviors(): array
{ {
return ArrayHelper::merge(parent::behaviors(), [ return ArrayHelper::merge(parent::behaviors(), [
@ -580,6 +589,7 @@ class ProjectController extends ApiController
* *
* @return ProjectUser * @return ProjectUser
* @throws NotFoundHttpException * @throws NotFoundHttpException
* @throws BadRequestHttpException
*/ */
public function actionAddUserByEmail(): ProjectUser public function actionAddUserByEmail(): ProjectUser
{ {
@ -594,11 +604,17 @@ class ProjectController extends ApiController
throw new NotFoundHttpException('The user not found'); throw new NotFoundHttpException('The user not found');
} }
if (\common\models\ProjectUser::find()->where(['user_id' => $user->id, 'project_id' => $project->id])->exists()){
throw new BadRequestHttpException('Пользователь уже добавлен в проект');
}
$model = new ProjectUser(); $model = new ProjectUser();
$model->user_id = $user->id; $model->user_id = $user->id;
$model->project_id = $project->id; $model->project_id = $project->id;
$model->save(); $model->save();
$this->emailService->sendEmail(new AddToProjectEmail($user, $project));
return $model; return $model;
} }
} }