From 4845f7944bd8468434ad4da07e0bffd55592f481 Mon Sep 17 00:00:00 2001 From: Kavalar Date: Tue, 6 Feb 2024 18:01:05 +0300 Subject: [PATCH] add to project email --- common/mail/addToProjectByEmail-html.php | 10 ++++++++ common/mail/addToProjectByEmail-text.php | 1 + common/models/email/AddToProjectEmail.php | 25 +++++++++++++++++++ .../api/controllers/ProjectController.php | 16 ++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 common/mail/addToProjectByEmail-html.php create mode 100644 common/mail/addToProjectByEmail-text.php create mode 100644 common/models/email/AddToProjectEmail.php diff --git a/common/mail/addToProjectByEmail-html.php b/common/mail/addToProjectByEmail-html.php new file mode 100644 index 0000000..7929b13 --- /dev/null +++ b/common/mail/addToProjectByEmail-html.php @@ -0,0 +1,10 @@ + +

Здравствуйте, username ?>,

+ +Вас добавили в проект name ?> . \ No newline at end of file diff --git a/common/mail/addToProjectByEmail-text.php b/common/mail/addToProjectByEmail-text.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/common/mail/addToProjectByEmail-text.php @@ -0,0 +1 @@ +sendTo = $user->email; + $this->subject = 'Вас добавили в проект'; + $this->mailLayout = ['html' => 'addToProjectByEmail-html', 'text' => 'addToProjectByEmail-text']; + $this->params = ['user' => $user, 'project' => $project]; + } + +} \ No newline at end of file diff --git a/frontend/modules/api/controllers/ProjectController.php b/frontend/modules/api/controllers/ProjectController.php index 6ff5aab..1ca0828 100644 --- a/frontend/modules/api/controllers/ProjectController.php +++ b/frontend/modules/api/controllers/ProjectController.php @@ -2,10 +2,12 @@ namespace frontend\modules\api\controllers; +use common\models\email\AddToProjectEmail; use common\models\ProjectTaskCategory; use common\models\Status; use common\models\User; use common\models\UseStatus; +use common\services\EmailService; use frontend\modules\api\models\Manager; use frontend\modules\api\models\project\Project; use frontend\modules\api\models\project\ProjectStatistic; @@ -20,12 +22,19 @@ use yii\web\NotFoundHttpException; class ProjectController extends ApiController { + public EmailService $emailService; public $modelClass = 'frontend\modules\api\models\Project'; public $serializer = [ 'class' => 'yii\rest\Serializer', 'collectionEnvelope' => 'projects', ]; + public function __construct($id, $module, EmailService $emailService, $config = []) + { + $this->emailService = $emailService; + parent::__construct($id, $module, $config); + } + public function behaviors(): array { return ArrayHelper::merge(parent::behaviors(), [ @@ -580,6 +589,7 @@ class ProjectController extends ApiController * * @return ProjectUser * @throws NotFoundHttpException + * @throws BadRequestHttpException */ public function actionAddUserByEmail(): ProjectUser { @@ -594,11 +604,17 @@ class ProjectController extends ApiController 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->user_id = $user->id; $model->project_id = $project->id; $model->save(); + $this->emailService->sendEmail(new AddToProjectEmail($user, $project)); + return $model; } } \ No newline at end of file