1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Api\Field\Mutation; |
6
|
|
|
|
7
|
|
|
use Application\Api\Exception; |
8
|
|
|
use Application\Api\Field\FieldInterface; |
9
|
|
|
use Application\Api\Helper; |
10
|
|
|
use Application\DBAL\Types\BookingStatusType; |
11
|
|
|
use Application\DBAL\Types\BookingTypeType; |
12
|
|
|
use Application\Model\Bookable; |
13
|
|
|
use Application\Model\Booking; |
14
|
|
|
use Application\Model\User; |
15
|
|
|
use Application\Repository\BookableRepository; |
16
|
|
|
use Application\Repository\UserRepository; |
17
|
|
|
use Cake\Chronos\Chronos; |
18
|
|
|
use GraphQL\Type\Definition\Type; |
19
|
|
|
use Mezzio\Session\SessionInterface; |
20
|
|
|
|
21
|
|
|
abstract class ConfirmRegistration implements FieldInterface |
22
|
|
|
{ |
23
|
2 |
|
public static function build(): array |
24
|
|
|
{ |
25
|
|
|
return [ |
26
|
1 |
|
'name' => 'confirmRegistration', |
27
|
1 |
|
'type' => Type::nonNull(Type::boolean()), |
28
|
1 |
|
'description' => 'First step to register as a new user.', |
29
|
|
|
'args' => [ |
30
|
1 |
|
'token' => Type::nonNull(_types()->get('Token')), |
|
|
|
|
31
|
1 |
|
'input' => Type::nonNull(_types()->get('ConfirmRegistrationInput')), |
32
|
|
|
], |
33
|
|
|
'resolve' => function ($root, array $args, SessionInterface $session): bool { |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** @var UserRepository $repository */ |
36
|
2 |
|
$repository = _em()->getRepository(User::class); |
37
|
|
|
|
38
|
|
|
/** @var User $user */ |
39
|
|
|
$user = $repository->getAclFilter()->runWithoutAcl(function () use ($repository, $args) { |
40
|
2 |
|
return $repository->findOneByToken($args['token']); |
|
|
|
|
41
|
2 |
|
}); |
42
|
|
|
|
43
|
2 |
|
if (!$user) { |
44
|
1 |
|
throw new Exception('Cannot confirm registration with an invalid token'); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
if (!$user->isTokenValid()) { |
48
|
|
|
throw new Exception('Le lien que vous avez suivi est périmé. Veuillez effectuer une nouvelle demande.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
$input = $args['input']; |
52
|
|
|
|
53
|
|
|
$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
|
|
|
// Create mandatory booking for him |
66
|
1 |
|
User::setCurrent($user); |
67
|
|
|
|
68
|
|
|
/** @var BookableRepository $bookableRepository */ |
69
|
1 |
|
$bookableRepository = _em()->getRepository(Bookable::class); |
70
|
1 |
|
$mandatoryBookables = $bookableRepository->findByBookingType(BookingTypeType::MANDATORY); |
|
|
|
|
71
|
1 |
|
foreach ($mandatoryBookables as $bookable) { |
72
|
1 |
|
$booking = new Booking(); |
73
|
1 |
|
_em()->persist($booking); |
74
|
|
|
|
75
|
1 |
|
$booking->setOwner($user); |
76
|
1 |
|
$booking->setStatus(BookingStatusType::BOOKED); |
77
|
1 |
|
$booking->setStartDate(new Chronos()); |
78
|
1 |
|
$booking->setBookable($bookable); |
79
|
|
|
|
80
|
|
|
// Non-periodic bookable must be terminated immediately |
81
|
1 |
|
if ($bookable->getPeriodicPrice()->isZero()) { |
82
|
1 |
|
$booking->terminate('Terminé automatiquement parce que paiement ponctuel uniquement'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
_em()->flush(); |
87
|
|
|
|
88
|
1 |
|
return true; |
89
|
1 |
|
}, |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|