1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Api\Field\Mutation; |
6
|
|
|
|
7
|
|
|
use Application\Api\Helper; |
8
|
|
|
use Application\DBAL\Types\BookingStatusType; |
9
|
|
|
use Application\DBAL\Types\BookingTypeType; |
10
|
|
|
use Application\Model\Account; |
11
|
|
|
use Application\Model\Bookable; |
12
|
|
|
use Application\Model\Booking; |
13
|
|
|
use Application\Model\User; |
14
|
|
|
use Application\Repository\AccountRepository; |
15
|
|
|
use Application\Repository\BookableRepository; |
16
|
|
|
use Application\Repository\UserRepository; |
17
|
|
|
use Cake\Chronos\Chronos; |
18
|
|
|
use Ecodev\Felix\Api\Exception; |
19
|
|
|
use Ecodev\Felix\Api\ExceptionWithoutMailLogging; |
20
|
|
|
use Ecodev\Felix\Api\Field\FieldInterface; |
21
|
|
|
use GraphQL\Type\Definition\Type; |
22
|
|
|
use Mezzio\Session\SessionInterface; |
23
|
|
|
|
24
|
|
|
abstract class ConfirmRegistration implements FieldInterface |
25
|
|
|
{ |
26
|
2 |
|
public static function build(): array |
27
|
|
|
{ |
28
|
1 |
|
return [ |
29
|
1 |
|
'name' => 'confirmRegistration', |
30
|
1 |
|
'type' => Type::nonNull(Type::boolean()), |
31
|
1 |
|
'description' => 'Second step to register as a new user.', |
32
|
1 |
|
'args' => [ |
33
|
1 |
|
'token' => Type::nonNull(_types()->get('Token')), |
34
|
1 |
|
'input' => Type::nonNull(_types()->get('ConfirmRegistrationInput')), |
35
|
1 |
|
], |
36
|
1 |
|
'resolve' => function ($root, array $args, SessionInterface $session): bool { |
37
|
|
|
/** @var UserRepository $repository */ |
38
|
2 |
|
$repository = _em()->getRepository(User::class); |
39
|
|
|
|
40
|
|
|
/** @var null|User $user */ |
41
|
2 |
|
$user = $repository->getAclFilter()->runWithoutAcl(fn () => $repository->findOneByToken($args['token'])); |
42
|
|
|
|
43
|
2 |
|
if (!$user) { |
44
|
1 |
|
throw new ExceptionWithoutMailLogging('La session a expiré ou le lien n\'est pas valable. Effectue une nouvelle demande.'); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
if (!$user->isTokenValid()) { |
48
|
|
|
throw new ExceptionWithoutMailLogging('Le lien que tu as suivi est périmé. Effectue une nouvelle demande.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
$input = $args['input']; |
52
|
|
|
|
53
|
1 |
|
$repository->getAclFilter()->runWithoutAcl(function () use ($repository, $input): void { |
54
|
1 |
|
if ($repository->findOneByLogin($input['login'])) { |
55
|
|
|
throw new Exception('Ce nom d\'utilisateur est déjà attribué et ne peut être utilisé'); |
56
|
|
|
} |
57
|
1 |
|
}); |
58
|
|
|
|
59
|
|
|
// Do it |
60
|
1 |
|
Helper::hydrate($user, $input); |
61
|
|
|
|
62
|
|
|
// Active the member |
63
|
1 |
|
$user->initialize(); |
64
|
|
|
|
65
|
|
|
// Login |
66
|
1 |
|
Login::doLogin($session, $user); |
67
|
|
|
|
68
|
|
|
// Create account so the user can top-up money and start purchasing services |
69
|
|
|
// but only if it's head of the family |
70
|
1 |
|
if ($user->isFamilyOwner()) { |
71
|
|
|
/** @var AccountRepository $accountRepository */ |
72
|
1 |
|
$accountRepository = _em()->getRepository(Account::class); |
73
|
1 |
|
$accountRepository->getOrCreate($user); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Create mandatory booking for him |
77
|
|
|
/** @var BookableRepository $bookableRepository */ |
78
|
1 |
|
$bookableRepository = _em()->getRepository(Bookable::class); |
79
|
1 |
|
$mandatoryBookables = $bookableRepository->findByBookingType(BookingTypeType::MANDATORY); |
80
|
1 |
|
foreach ($mandatoryBookables as $bookable) { |
81
|
1 |
|
$booking = new Booking(); |
82
|
1 |
|
_em()->persist($booking); |
83
|
|
|
|
84
|
1 |
|
$booking->setOwner($user); |
85
|
1 |
|
$booking->setStatus(BookingStatusType::BOOKED); |
86
|
1 |
|
$booking->setStartDate(new Chronos()); |
87
|
1 |
|
$booking->setBookable($bookable); |
88
|
|
|
|
89
|
|
|
// Non-periodic bookable must be terminated immediately |
90
|
1 |
|
if ($bookable->getPeriodicPrice()->isZero()) { |
91
|
1 |
|
$booking->terminate('Terminé automatiquement parce que paiement ponctuel uniquement'); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
_em()->flush(); |
96
|
|
|
|
97
|
1 |
|
return true; |
98
|
1 |
|
}, |
99
|
1 |
|
]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|