Send SMS on customer warranty register / update #540

This commit is contained in:
Kendrick Chan 2021-03-26 21:26:46 +08:00
parent 17669e0831
commit fff5b46eda
3 changed files with 50 additions and 6 deletions

View file

@ -13,6 +13,7 @@ use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
@ -2978,7 +2979,7 @@ class APIController extends Controller implements LoggedController
return $serial . '/' . $filename;
}
public function warrantyRegister($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel)
public function warrantyRegister($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel, RisingTideGateway $rt, TranslatorInterface $trans)
{
// check required parameters and api key
$required_params = [
@ -3008,14 +3009,14 @@ class APIController extends Controller implements LoggedController
// $cust = $this->updateCustomerInfo($req, $em);
// update warranty
$res = $this->updateWarranty($res, $em, $req, $serial, $inv_filename, $wcard_filename);
$res = $this->updateWarranty($res, $em, $trans, $req, $serial, $inv_filename, $wcard_filename);
$em->flush();
return $res->getReturnResponse();
}
protected function updateWarranty($res, $em, $req, $serial, $inv_filename = null, $wcard_filename = null)
protected function updateWarranty($res, $em, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null)
{
// get serial
$warr_serial = $em->getRepository(WarrantySerial::class)->find($serial);
@ -3051,10 +3052,12 @@ class APIController extends Controller implements LoggedController
return $res;
}
$sms_msg = $trans->trans('warranty_update_confirm');
}
else
{
$warr = new Warranty();
$sms_msg = $trans->trans('warranty_register_confirm');
}
// get sap battery
@ -3121,6 +3124,10 @@ class APIController extends Controller implements LoggedController
// set data to retrun to user
$res->setData($data);
// send sms
error_log('sending sms to - ' . $this->session->getPhoneNumber());
$rt->sendSMS($this->session->getPhoneNumber(), 'MOTOLITE', $sms_msg);
return $res;
}

View file

@ -5,12 +5,15 @@ namespace App\Controller\CAPI;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Doctrine\ORM\Query;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Catalyst\APIBundle\Controller\APIController;
use Catalyst\APIBundle\Response\APIResponse;
use App\Service\RisingTideGateway;
use App\Entity\WarrantySerial;
use App\Entity\Warranty;
@ -266,7 +269,7 @@ class CustomerWarrantyController extends APIController
}
public function register($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel)
public function register($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel, RisingTideGateway $rt, TranslatorInterface $trans)
{
error_log('HERE - register');
// check required parameters
@ -290,7 +293,7 @@ class CustomerWarrantyController extends APIController
$wcard_filename = $this->handlePictureUpload($warr_card, $upload_dir, $serial, 'wcard');
// do actual registering
$res = $this->updateWarranty($em, $req, $serial, $inv_filename, $wcard_filename);
$res = $this->updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename, $wcard_filename);
// flush to db
$em->flush();
@ -331,7 +334,7 @@ class CustomerWarrantyController extends APIController
return $serial . '/' . $filename;
}
protected function updateWarranty($em, $req, $serial, $inv_filename = null, $wcard_filename = null)
protected function updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null)
{
$plate_num = $this->cleanPlateNumber($req->request->get('plate_num'));
@ -348,6 +351,7 @@ class CustomerWarrantyController extends APIController
// skip warranty if it already exists
$cust = null;
$sms_message = '';
if ($warr != null)
{
$warr_plate_num = $this->cleanPlateNumber($warr->getPlateNumber());
@ -359,10 +363,13 @@ class CustomerWarrantyController extends APIController
// get customer
$cust = $warr->getCustomer();
$sms_message = $trans->trans('warranty_update_confirm');
}
else
{
$warr = new Warranty();
$sms_message = $trans->trans('warranty_register_confirm');
}
error_log('sap battery check');
@ -463,7 +470,33 @@ class CustomerWarrantyController extends APIController
// TODO: check if we need to do anyting else
$data = [];
// send sms confirmation
$this->sendSMSConfirmation($rt, $req->request->get('contact_num'), $sms_message);
return new APIResponse(true, 'Warranty registered.', $data);
}
protected function sendSMSConfirmation($rt, $num, $message)
{
$clean_num = trim($num);
// check if number is valid
// number should have 11 to 12 characters
if (strlen($clean_num) < 11 || strlen($clean_num) > 12)
return false;
// check if numeric
if (!is_numeric($clean_num))
return false;
// number should begin with 0 or 6
if ($clean_num[0] != '0' && $clean_num[0] != '6')
return false;
error_log('sending sms to - ' . $clean_num);
$rt->sendSMS($clean_num, 'MOTOLITE', $message);
}
}

View file

@ -26,3 +26,7 @@ image_jo_pdf: /public/assets/images/logo-resq.png
default_lat: 14.6091
default_long: 121.0223
default_region: ph
# warranty text
warranty_register_confirm: Congratulations! Your warranty has been successfully registered! Read about Motolite's privacy policy at https://www.motolite.com/privacy/.
warranty_update_confirm: Congratulations! Your warranty has been successfully updated! Read about Motolite's privacy policy at https://www.motolite.com/privacy/.