diff --git a/src/Action/Patient/Post.php b/src/Action/Patient/Post.php index a6101d8..97b69ee 100644 --- a/src/Action/Patient/Post.php +++ b/src/Action/Patient/Post.php @@ -92,7 +92,6 @@ public function __invoke(Request $request, PatientManager $pm, QuestionManager $ $parameters->get('upload_files_to_server') === 'true' ? true : false ), GenericEvents::FILE_UPLOAD); - return $this->jsonResponse( Response::HTTP_CREATED, 'Patient resource add success' diff --git a/src/Action/Sms/SendVerificationSMS.php b/src/Action/Sms/SendVerificationSMS.php index 9f3537b..01a8b62 100644 --- a/src/Action/Sms/SendVerificationSMS.php +++ b/src/Action/Sms/SendVerificationSMS.php @@ -5,6 +5,7 @@ use App\Action\BaseAction; use App\Dto\Phone; use App\Form\PhoneType; +use App\Manager\SMSManager; use App\Service\TTSMSing; use App\Util\Tools; use Exception; @@ -49,7 +50,7 @@ class SendVerificationSMS extends BaseAction * @param TTSMSing $ttSMSing * @return View|FormInterface */ - public function __invoke(Request $request, TTSMSing $ttSMSing) + public function __invoke(Request $request, TTSMSing $ttSMSing, SMSManager $sm) { $phone = new Phone(); @@ -70,6 +71,8 @@ public function __invoke(Request $request, TTSMSing $ttSMSing) ); } + $sm->newSMS($verificationCode, $phone->getNumber()); + return $this->jsonResponse( Response::HTTP_OK, 'Verification SMS successfully sent to patient', diff --git a/src/Action/Sms/VerifySMS.php b/src/Action/Sms/VerifySMS.php new file mode 100644 index 0000000..2e9416d --- /dev/null +++ b/src/Action/Sms/VerifySMS.php @@ -0,0 +1,86 @@ + + */ +class VerifySMS extends BaseAction +{ + /** + * Check if verification sms entered by the patient is valid + * + * + * @Rest\Post("/api/v1/sms/authentication/check") + * + * @SWG\Parameter( + * name="verificationCode", + * in="body", + * required=true, + * @Model(type=VerificationCodeType::class) + * ) + * + * @SWG\Response(response=200, description="Verification code check success") + * @SWG\Response(response=400, description="Validation Failed") + * @SWG\Response(response=500, description="No SMS found for this phone number") + * + * @SWG\Tag(name="SMS") + * + * @Rest\View() + * @param Request $request + * @param SMSManager $sm + * @return View|FormInterface + */ + public function __invoke(Request $request, SMSManager $sm) + { + $verificationCode = new VerificationCodeDto(); + + $form = $this->createForm(VerificationCodeType::class, $verificationCode); + $form->submit($request->request->all()); + if (!$form->isValid()) { + return $form; + } + + try{ + $sms = $sm->getByPhoneNumber($verificationCode->getNumber()); + } catch (Exception $exception) { + return $this->jsonResponse( + Response::HTTP_INTERNAL_SERVER_ERROR, + $exception->getMessage() + ); + } + + if($sms->getVerificationCode() != $verificationCode->getCode()) { + return $this->jsonResponse( + Response::HTTP_BAD_REQUEST, + "Validation Failed" + ); + } + + $sm->remove($sms); + + return $this->jsonResponse( + Response::HTTP_OK, + 'Verification code check success' + ); + } +} diff --git a/src/Dto/VerificationCodeDto.php b/src/Dto/VerificationCodeDto.php new file mode 100644 index 0000000..175e402 --- /dev/null +++ b/src/Dto/VerificationCodeDto.php @@ -0,0 +1,74 @@ + + */ +class VerificationCodeDto +{ + /** + * @var string content + * + * @ORM\Column(type="string") + * + * @Assert\NotBlank + * @Assert\Length( + * min = 6, + * max = 6, + * exactMessage="The verification code should have exactly {{ limit }} characters" + * ) + */ + private $code; + + /** + * @var int number + * + * @ORM\Column(type="integer") + * + * @Assert\NotBlank + * @Assert\Length( + * min = 8, + * max = 8, + * exactMessage="The verification code should have exactly {{ limit }} characters" + * ) + */ + private $number; + + /** + * @return string|null + */ + public function getCode(): ?string + { + return $this->code; + } + + /** + * @param string $content + */ + public function setCode(string $code): void + { + $this->code = $code; + } + + /** + * @return int|null + */ + public function getNumber(): ?int + { + return $this->number; + } + + /** + * @param int $number + */ + public function setNumber(int $number): void + { + $this->number = $number; + } +} \ No newline at end of file diff --git a/src/Entity/VerificationSMS.php b/src/Entity/VerificationSMS.php new file mode 100644 index 0000000..87dc527 --- /dev/null +++ b/src/Entity/VerificationSMS.php @@ -0,0 +1,67 @@ +id; + } + + public function getPhoneNumber(): ?int + { + return $this->phoneNumber; + } + + public function setPhoneNumber(int $phoneNumber): self + { + $this->phoneNumber = $phoneNumber; + + return $this; + } + + public function getVerificationCode(): ?string + { + return $this->verificationCode; + } + + public function setVerificationCode(string $verificationCode): self + { + $this->verificationCode = $verificationCode; + + return $this; + } +} diff --git a/src/Form/VerificationCodeType.php b/src/Form/VerificationCodeType.php new file mode 100644 index 0000000..0ef6594 --- /dev/null +++ b/src/Form/VerificationCodeType.php @@ -0,0 +1,31 @@ + + */ +class VerificationCodeType extends AbstractType +{ + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add('code') + ->add('number', IntegerType::class); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => VerificationCodeDto::class, + ]); + } +} diff --git a/src/Manager/SMSManager.php b/src/Manager/SMSManager.php new file mode 100644 index 0000000..2ec9f1c --- /dev/null +++ b/src/Manager/SMSManager.php @@ -0,0 +1,65 @@ + + */ +class SMSManager +{ + /** + * @var EntityManagerInterface + */ + private $em; + + /** + * SMSManager constructor. + * + * @param EntityManagerInterface $em + */ + public function __construct(EntityManagerInterface $em) + { + $this->em = $em; + } + + /** + * create a new verification sms entry + * + * @param string $code + * @param int $phone + */ + public function newSMS(string $code, int $phone): void + { + $verificationSms = new VerificationSMS(); + $verificationSms->setPhoneNumber($phone); + $verificationSms->setVerificationCode($code); + + $this->em->persist($verificationSms); + $this->em->flush(); + } + + public function getByPhoneNumber(int $number): VerificationSMS + { + $sms = $this->em->getRepository(VerificationSMS::class)->findOneBy([ + "phoneNumber"=>$number + ]); + + if($sms) { + return $sms ; + } else { + throw new \Exception("No SMS found for this phone number"); + } + } + + public function remove(VerificationSMS $sms): void + { + $this->em->remove($sms); + $this->em->flush(); + } + +} diff --git a/src/Repository/VerificationSMSRepository.php b/src/Repository/VerificationSMSRepository.php new file mode 100644 index 0000000..db9eefd --- /dev/null +++ b/src/Repository/VerificationSMSRepository.php @@ -0,0 +1,50 @@ +createQueryBuilder('v') + ->andWhere('v.exampleField = :val') + ->setParameter('val', $value) + ->orderBy('v.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } + */ + + /* + public function findOneBySomeField($value): ?VerificationSMS + { + return $this->createQueryBuilder('v') + ->andWhere('v.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } + */ +} diff --git a/symfony.lock b/symfony.lock index e087293..c397da7 100644 --- a/symfony.lock +++ b/symfony.lock @@ -268,7 +268,7 @@ "version": "v2.1.3" }, "php": { - "version": "7.3" + "version": "7.2" }, "phpdocumentor/reflection-common": { "version": "2.0.0"