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