Skip to content

Commit 597e6ff

Browse files
committed
feat: Add User API resource and send welcome email after user's creation
1 parent 1232e00 commit 597e6ff

File tree

6 files changed

+211
-0
lines changed

6 files changed

+211
-0
lines changed

api/config/packages/mailer.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
framework:
2+
mailer:
3+
dsn: '%env(MAILER_DSN)%'
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20241030153234 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql('DROP SEQUENCE greeting_id_seq CASCADE');
24+
$this->addSql('CREATE SEQUENCE user_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
25+
$this->addSql('CREATE TABLE "user" (id INT NOT NULL, first_name TEXT NOT NULL, last_name TEXT NOT NULL, birth_date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, email TEXT DEFAULT NULL, PRIMARY KEY(id))');
26+
$this->addSql('DROP TABLE greeting');
27+
}
28+
29+
public function down(Schema $schema): void
30+
{
31+
// this down() migration is auto-generated, please modify it to your needs
32+
$this->addSql('CREATE SCHEMA public');
33+
$this->addSql('DROP SEQUENCE user_id_seq CASCADE');
34+
$this->addSql('CREATE SEQUENCE greeting_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
35+
$this->addSql('CREATE TABLE greeting (id INT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
36+
$this->addSql('DROP TABLE "user"');
37+
}
38+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20241030154053 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql('ALTER TABLE "user" ALTER birth_date TYPE DATE');
24+
}
25+
26+
public function down(Schema $schema): void
27+
{
28+
// this down() migration is auto-generated, please modify it to your needs
29+
$this->addSql('CREATE SCHEMA public');
30+
$this->addSql('ALTER TABLE "user" ALTER birth_date TYPE TIMESTAMP(0) WITHOUT TIME ZONE');
31+
}
32+
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20241030155254 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql('ALTER TABLE "user" ALTER first_name TYPE VARCHAR(255)');
24+
$this->addSql('ALTER TABLE "user" ALTER last_name TYPE VARCHAR(255)');
25+
$this->addSql('ALTER TABLE "user" ALTER birth_date TYPE TIMESTAMP(0) WITHOUT TIME ZONE');
26+
$this->addSql('ALTER TABLE "user" ALTER email TYPE VARCHAR(255)');
27+
$this->addSql('COMMENT ON COLUMN "user".birth_date IS \'(DC2Type:datetime_immutable)\'');
28+
}
29+
30+
public function down(Schema $schema): void
31+
{
32+
// this down() migration is auto-generated, please modify it to your needs
33+
$this->addSql('CREATE SCHEMA public');
34+
$this->addSql('ALTER TABLE "user" ALTER first_name TYPE TEXT');
35+
$this->addSql('ALTER TABLE "user" ALTER last_name TYPE TEXT');
36+
$this->addSql('ALTER TABLE "user" ALTER birth_date TYPE DATE');
37+
$this->addSql('ALTER TABLE "user" ALTER email TYPE TEXT');
38+
$this->addSql('COMMENT ON COLUMN "user".birth_date IS NULL');
39+
}
40+
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20241030160055 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql('DROP SEQUENCE user_id_seq CASCADE');
24+
$this->addSql('CREATE SEQUENCE users_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
25+
$this->addSql('CREATE TABLE users (id INT NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, birth_date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, email VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))');
26+
$this->addSql('COMMENT ON COLUMN users.birth_date IS \'(DC2Type:datetime_immutable)\'');
27+
$this->addSql('DROP TABLE "user"');
28+
}
29+
30+
public function down(Schema $schema): void
31+
{
32+
// this down() migration is auto-generated, please modify it to your needs
33+
$this->addSql('CREATE SCHEMA public');
34+
$this->addSql('DROP SEQUENCE users_id_seq CASCADE');
35+
$this->addSql('CREATE SEQUENCE user_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
36+
$this->addSql('CREATE TABLE "user" (id INT NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, birth_date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, email VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))');
37+
$this->addSql('COMMENT ON COLUMN "user".birth_date IS \'(DC2Type:datetime_immutable)\'');
38+
$this->addSql('DROP TABLE users');
39+
}
40+
}
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace App\EventSubscriber;
4+
5+
use ApiPlatform\Symfony\EventListener\EventPriorities;
6+
use App\Entity\User;
7+
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
8+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9+
use Symfony\Component\HttpFoundation\Request;
10+
use Symfony\Component\HttpKernel\Event\ViewEvent;
11+
use Symfony\Component\HttpKernel\KernelEvents;
12+
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
13+
use Symfony\Component\Mailer\MailerInterface;
14+
15+
class UserSubscriber implements EventSubscriberInterface
16+
{
17+
public function __construct(
18+
private MailerInterface $mailer
19+
)
20+
{
21+
}
22+
23+
public static function getSubscribedEvents(): array
24+
{
25+
return [
26+
KernelEvents::VIEW => ['sendMail', EventPriorities::POST_WRITE],
27+
];
28+
}
29+
30+
/**
31+
* @throws TransportExceptionInterface
32+
*/
33+
public function sendMail(ViewEvent $event): void
34+
{
35+
$user = $event->getControllerResult();
36+
$method = $event->getRequest()->getMethod();
37+
38+
if (!$user instanceof User || Request::METHOD_POST !== $method) {
39+
return;
40+
}
41+
42+
if (!$user->email) {
43+
return;
44+
}
45+
46+
$message = (new TemplatedEmail())
47+
48+
->to($user->email)
49+
->subject('Hello in Recruitment System')
50+
->htmlTemplate('emails/welcome.html.twig')
51+
->context([
52+
'firstName' => $user->firstName,
53+
'lastName' => $user->lastName,
54+
]);
55+
56+
$this->mailer->send($message);
57+
}
58+
}

0 commit comments

Comments
 (0)