From a9e2d95ab4773f7c58f8c2367d432822c7de4684 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 9 Jan 2023 07:26:36 +0000 Subject: [PATCH 01/11] Add service to connect to insurance API. Add test command to test service. #727 --- config/services.yaml | 8 ++ src/Command/TestInsuranceConnectorCommand.php | 75 ++++++++++++++++++ src/Service/InsuranceConnector.php | 76 +++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 src/Command/TestInsuranceConnectorCommand.php create mode 100644 src/Service/InsuranceConnector.php diff --git a/config/services.yaml b/config/services.yaml index a5d7d05f..b2460c3f 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -328,3 +328,11 @@ services: App\Service\WarrantySerialLoadLogger: arguments: $em: "@doctrine.orm.entity_manager" + + # insurance connector + App\Service\InsuranceConnector: + arguments: + $base_url: "%env(INSURANCE_BASE_URL)%" + $username: "%env(INSURANCE_USERNAME)%" + $password: "%env(INSURANCE_PASSWORD)%" + $token: "%env(INSURANCE_TOKEN)%" diff --git a/src/Command/TestInsuranceConnectorCommand.php b/src/Command/TestInsuranceConnectorCommand.php new file mode 100644 index 00000000..7599321a --- /dev/null +++ b/src/Command/TestInsuranceConnectorCommand.php @@ -0,0 +1,75 @@ +setName('test:create-insurance-application') + ->setDescription('Test create insurance application service.') + ->setHelp('Test the create insuranace application service.'); + } + + public function __construct(InsuranceConnector $insurance) + { + $this->insurance = $insurance; + + parent::__construct(); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $client_info = [ + 'client_type' => 'I', + 'first_name' => '', + 'middle_name' => 'Resq', + 'surname' => 'Customer', + 'corporate_name' => 'Customer, Test Resq' + ]; + + $client_contact_info = [ + 'address_number' => '112', + 'address_street' => 'Test Street', + 'address_building' => 'Test Building', + 'address_barangay' => 'Legaspi Village', + 'address_city' => 'Makati City', + 'address_province' => 'Metro Manila', + 'zipcode' => '1200', + 'mobile_number' => '09171234567', + 'email_address' => 'test.resq@gmail.com' + ]; + + $car_info = [ + 'make' => 'HYUNDAI', + 'model' => 'ACCENT', + 'series' => '1.4 A/T', + 'color' => 'PHANTOM BLACK', + 'plate_number' => 'ABC1234', + 'mv_file_number' => '123456789012345', + 'motor_number' => 'E31TE-0075268', + 'serial_chasis' => 'PA0SEF210K0075701', + 'year_model' => '2020', + 'mv_type_id' => 1, + 'body_type' => 'SEDAN', + 'is_public' => false, + 'line' => 'pcoc' + ]; + + $result = $this->insurance->createApplication($client_info, $client_contact_info, $car_info); + + error_log(json_encode($result)); + + return 0; + } + +} diff --git a/src/Service/InsuranceConnector.php b/src/Service/InsuranceConnector.php new file mode 100644 index 00000000..b29d6225 --- /dev/null +++ b/src/Service/InsuranceConnector.php @@ -0,0 +1,76 @@ +base_url = $base_url; + $this->username = $username; + $this->password = $password; + $this->token = $token; + } + + public function createApplication($client_info = [], $client_contact_info = [], $car_info = []) + { + $body = [ + 'client_info' => $client_info, + 'client_contact_info' => $client_contact_info, + 'car_info' => $car_info, + ]; + + $body_text = json_encode($body); + + // generate token for basic authorization + $token = $this->generateAuthToken($this->username, $this->password); + + $res = $this->curlPost('api/v1/ctpl/applications', $body_text, $token); + + $app_res = json_decode($res, true); + + // check result + if ($app_res == null) + return []; + + return $app_res; + } + + protected function generateAuthToken($username, $password) + { + $token = base64_encode($username . ':' . $password); + + // error_log('token ' . $token); + + return $token; + } + + protected function curlPost($url, $body, $token) + { + $curl = curl_init(); + + $options = [ + CURLOPT_URL => $this->base_url . '/' . $url, + CURLOPT_POST => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POSTFIELDS => $body, + CURLOPT_HTTPHEADER => [ + 'Content-Type: application/json', + 'Authorization: Basic ' . $token, + ], + ]; + + curl_setopt_array($curl, $options); + $res = curl_exec($curl); + + curl_close($curl); + + return $res; + } +} -- 2.43.5 From f8327f5d59a1e6bc01d4fdf0efb512addf2c5f19 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 9 Jan 2023 10:14:55 +0000 Subject: [PATCH 02/11] Add CRUD for the motor vehicle types for the MV lookup. #727 --- config/acl.yaml | 14 + config/menu.yaml | 4 + config/routes/motor_vehicle_type.yaml | 35 +++ src/Controller/MotorVehicleTypeController.php | 258 ++++++++++++++++++ src/Entity/MotorVehicleType.php | 88 ++++++ src/Insurance/ClientData.php | 76 ++++++ src/Insurance/ClientType.php | 14 + src/Insurance/LineType.php | 18 ++ templates/motor-vehicle-type/form.html.twig | 151 ++++++++++ templates/motor-vehicle-type/list.html.twig | 154 +++++++++++ 10 files changed, 812 insertions(+) create mode 100644 config/routes/motor_vehicle_type.yaml create mode 100644 src/Controller/MotorVehicleTypeController.php create mode 100644 src/Entity/MotorVehicleType.php create mode 100644 src/Insurance/ClientData.php create mode 100644 src/Insurance/ClientType.php create mode 100644 src/Insurance/LineType.php create mode 100644 templates/motor-vehicle-type/form.html.twig create mode 100644 templates/motor-vehicle-type/list.html.twig diff --git a/config/acl.yaml b/config/acl.yaml index 64540ecf..51acb169 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -586,3 +586,17 @@ access_keys: label: Update - id: ownership_type.delete label: Delete + + - id: motor_vehicle_type + label: Motor Vehicle Type Access + acls: + - id: motor_vehicle_type.menu + label: Menu + - id: motor_vehicle_type.list + label: List + - id: motor_vehicle_type.add + label: Add + - id: motor_vehicle_type.update + label: Update + - id: motor_vehicle_type.delete + label: Delete diff --git a/config/menu.yaml b/config/menu.yaml index b75a9d38..c1a514ac 100644 --- a/config/menu.yaml +++ b/config/menu.yaml @@ -249,3 +249,7 @@ main_menu: acl: ownership_type.menu label: Ownership Types parent: database + - id: motor_vehicle_type_list + acl: motor_vehicle_type.menu + label: Motor Vehicle Types + parent: database diff --git a/config/routes/motor_vehicle_type.yaml b/config/routes/motor_vehicle_type.yaml new file mode 100644 index 00000000..867502e6 --- /dev/null +++ b/config/routes/motor_vehicle_type.yaml @@ -0,0 +1,35 @@ +motor_vehicle_type_list: + path: /motor-vehicle-types + controller: App\Controller\MotorVehicleTypeController::index + methods: [GET] + +motor_vehicle_type_rows: + path: /motor-vehicle-types/rowdata + controller: App\Controller\MotorVehicleTypeController::datatableRows + methods: [POST] + +motor_vehicle_type_add_form: + path: /motor-vehicle-types/newform + controller: App\Controller\MotorVehicleTypeController::addForm + methods: [GET] + +motor_vehicle_type_add_submit: + path: /motor-vehicle-types + controller: App\Controller\MotorVehicleTypeController::addSubmit + methods: [POST] + +motor_vehicle_type_update_form: + path: /motor-vehicle-types/{id} + controller: App\Controller\MotorVehicleTypeController::updateForm + methods: [GET] + +motor_vehicle_type_update_submit: + path: /motor-vehicle-types/{id} + controller: App\Controller\MotorVehicleTypeController::updateSubmit + methods: [POST] + +motor_vehicle_type_delete: + path: /motor-vehicle-types/{id} + controller: App\Controller\MotorVehicleTypeController::deleteSubmit + methods: [DELETE] + diff --git a/src/Controller/MotorVehicleTypeController.php b/src/Controller/MotorVehicleTypeController.php new file mode 100644 index 00000000..e77af66e --- /dev/null +++ b/src/Controller/MotorVehicleTypeController.php @@ -0,0 +1,258 @@ +denyAccessUnlessGranted('motor_vehicle_type.list', null, 'No access.'); + + return $this->render('motor-vehicle-type/list.html.twig'); + } + + /** + * @IsGranted("motor_vehicle_type.list") + */ + public function datatableRows(Request $req) + { + // get query builder + $qb = $this->getDoctrine() + ->getRepository(MotorVehicleType::class) + ->createQueryBuilder('q'); + + // get datatable params + $datatable = $req->request->get('datatable'); + + // count total records + $tquery = $qb->select('COUNT(q)'); + $this->setQueryFilters($datatable, $tquery); + $total = $tquery->getQuery() + ->getSingleScalarResult(); + + // get current page number + $page = $datatable['pagination']['page'] ?? 1; + + $perpage = $datatable['pagination']['perpage']; + $offset = ($page - 1) * $perpage; + + // add metadata + $meta = [ + 'page' => $page, + 'perpage' => $perpage, + 'pages' => ceil($total / $perpage), + 'total' => $total, + 'sort' => 'asc', + 'field' => 'id' + ]; + + // build query + $query = $qb->select('q'); + $this->setQueryFilters($datatable, $query); + + // check if sorting is present, otherwise use default + if (isset($datatable['sort']['field']) && !empty($datatable['sort']['field'])) { + $order = $datatable['sort']['sort'] ?? 'asc'; + $query->orderBy('q.' . $datatable['sort']['field'], $order); + } else { + $query->orderBy('q.id', 'asc'); + } + + // get rows for this page + $obj_rows = $query->setFirstResult($offset) + ->setMaxResults($perpage) + ->getQuery() + ->getResult(); + + // process rows + $rows = []; + foreach ($obj_rows as $orow) { + // add row data + $row['id'] = $orow->getID(); + $row['mv_type_id'] = $orow->getMvTypeID(); + $row['lto_mv_type'] = $orow->getLtoMvType(); + $row['vehicle_type'] = $orow->getVehicleType(); + + // add row metadata + $row['meta'] = [ + 'update_url' => '', + 'delete_url' => '' + ]; + + // add crud urls + if ($this->isGranted('motor_vehicle_type.update')) + $row['meta']['update_url'] = $this->generateUrl('motor_vehicle_type_update_form', ['id' => $row['id']]); + if ($this->isGranted('motor_vehicle_type.delete')) + $row['meta']['delete_url'] = $this->generateUrl('motor_vehicle_type_delete', ['id' => $row['id']]); + + $rows[] = $row; + } + + // response + return $this->json([ + 'meta' => $meta, + 'data' => $rows + ]); + } + + /** + * @Menu(selected="motor_vehicle_type.list") + * @IsGranted("motor_vehicle_type.add") + */ + public function addForm() + { + $motor_vehicle_type = new MotorVehicleType(); + $params = [ + 'motor_vehicle_type' => $motor_vehicle_type, + 'mode' => 'create', + ]; + + // response + return $this->render('motor-vehicle-type/form.html.twig', $params); + } + + /** + * @IsGranted("motor_vehicle_type.add") + */ + public function addSubmit(Request $req, EntityManagerInterface $em, ValidatorInterface $validator) + { + $motor_vehicle_type = new MotorVehicleType(); + + $this->setObject($motor_vehicle_type, $req); + + // validate + $errors = $validator->validate($motor_vehicle_type); + + // initialize error list + $error_array = []; + + // add errors to list + foreach ($errors as $error) { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + // check if any errors were found + if (!empty($error_array)) { + // return validation failure response + return $this->json([ + 'success' => false, + 'errors' => $error_array + ], 422); + } + + // validated! save the entity + $em->persist($motor_vehicle_type); + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + + } + + /** + * @Menu(selected="motor_vehicle_type_list") + * @ParamConverter("motor_vehicle_type", class="App\Entity\MotorVehicleType") + * @IsGranted("motor_vehicle_type.update") + */ + public function updateForm($id, EntityManagerInterface $em, MotorVehicleType $motor_vehicle_type) + { + $params = []; + $params['motor_vehicle_type'] = $motor_vehicle_type; + $params['mode'] = 'update'; + + // response + return $this->render('motor-vehicle-type/form.html.twig', $params); + } + + /** + * @ParamConverter("motor_vehicle_type", class="App\Entity\MotorVehicleType") + * @IsGranted("motor_vehicle_type.update") + */ + public function updateSubmit(Request $req, EntityManagerInterface $em, ValidatorInterface $validator, MotorVehicleType $motor_vehicle_type) + { + $this->setObject($motor_vehicle_type, $req); + + // validate + $errors = $validator->validate($motor_vehicle_type); + + // initialize error list + $error_array = []; + + // add errors to list + foreach ($errors as $error) { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + // check if any errors were found + if (!empty($error_array)) { + // return validation failure response + return $this->json([ + 'success' => false, + 'errors' => $error_array + ], 422); + } + + // validated! save the entity + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } + + /** + * @ParamConverter("motor_vehicle_type", class="App\Entity\MotorVehicleType") + * @IsGranted("motor_vehicle_type.update") + */ + public function deleteSubmit(EntityManagerInterface $em, MotorVehicleType $motor_vehicle_type) + { + // delete this object + $em->remove($motor_vehicle_type); + $em->flush(); + + // response + $response = new Response(); + $response->setStatusCode(Response::HTTP_OK); + $response->send(); + } + + + protected function setObject(MotorVehicleType $obj, Request $req) + { + // set and save values + $obj->setMvTypeID($req->request->get('mv_type_id')) + ->setLtoMvType($req->request->get('lto_mv_type')) + ->setVehicleType($req->request->get('vehicle_type')); + } + + protected function setQueryFilters($datatable, QueryBuilder $query) + { + if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) { + $query->where('q.lto_mv_type LIKE :filter') + ->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); + } + } + +} diff --git a/src/Entity/MotorVehicleType.php b/src/Entity/MotorVehicleType.php new file mode 100644 index 00000000..e0da8211 --- /dev/null +++ b/src/Entity/MotorVehicleType.php @@ -0,0 +1,88 @@ +mv_type_id = 0; + $this->lto_mv_type = ''; + $this->vehicle_type = ''; + } + + public function getID() + { + return $this->id; + } + + public function setMvTypeID($mv_type_id) + { + $this->mv_type_id = $mv_type_id; + return $this; + } + + public function getMvTypeID() + { + return $this->mv_type_id; + } + + public function setLtoMvType($lto_mv_type) + { + $this->lto_mv_type = $lto_mv_type; + return $this; + } + + public function getLtoMvType() + { + return $this->lto_mv_type; + } + + public function setVehicleType($vehicle_type) + { + $this->vehicle_type = $vehicle_type; + return $this; + } + + public function getVehicleType() + { + return $this->vehicle_type; + } +} diff --git a/src/Insurance/ClientData.php b/src/Insurance/ClientData.php new file mode 100644 index 00000000..d9d392e8 --- /dev/null +++ b/src/Insurance/ClientData.php @@ -0,0 +1,76 @@ +, . Mandatory. String. + + // client_contact_info + protected $address_number; // client house number address. Mandatory. String. + protected $address_street; // client street address. Optional. String. + protected $address_building; // client build address. Optional. String. + protected $address_barangay; // client barangay address. Mandatory. String. + protected $address_city; // client city address. Mandatory. String. + protected $address_province; // client province address. Mandatory. String. + protected $zipcode; // client zip code. Mandatory. Integer. + protected $mobile_number; // client mobile number. Mandatory. String. + protected $email_address; // client email address. Mandatory. String. + + // car_info + protected $make; // vehicle make. Mandatory. String. + protected $model; // vehicle model. Mandatory. String. + protected $series; // vehicle seris. Mandatory. String. + protected $color; // vehicle color. Mandatory. String. + protected $plate_number; // vehicle plate number. Mandatory. String. + protected $mv_file_number; // vehicle MV file number. Mandatory. String. + protected $motor_number; // vehicle motor number. Mandatory. String. + protected $serial_chassis; // vehicle serial or chassis number. Mandatory. String. + protected $year_model; // year model of vehicle. Mandatory. Integer. + protected $mv_type_id; // LTO mv type. Mandatory. Integer. + protected $body_type; // vehicle body type. Mandatory. String. + protected $is_public; // public vehicle or not? Mandatory. Boolean. + protected $line; // defines where vehicle should be part of. Mandatory. String. Valid values are: PCOC - Private Car, MCOC - Mototcycle/Motorcycle with Sidecar/Tricycle(is_public is false), CCOC - Commercial Vehicle, LCOC - Motorcycle with sidecar/Tricycle(is_public is true) + + public function __construct() + { + $this->client_type = 'I'; + $this->first_name = ''; + $this->middle_name = ''; + $this->surname = ''; + $this->corporate_name = ''; + + $this->address_number = ''; + $this->address_street = ''; + $this->address_bulding = ''; + $this->address_barangay = ''; + $this->address_city = ''; + $this->address_province = ''; + $this->zipcode = 0; + $this->mobile_number = ''; + $this->email_address = ''; + + $this->make = ''; + $this->model = ''; + $this->series = ''; + $this->color = ''; + $this->plate_number = ''; + $this->mv_file_number = ''; + $this->motor_number = ''; + $this->serial_chassis = ''; + $this->year_model = 0; + $this->mv_type_id = 0; + $this->body_type = ''; + $this->is_public = false; + $this->line = ''; + } + + public function setClientType($client_type) + { + } +} diff --git a/src/Insurance/ClientType.php b/src/Insurance/ClientType.php new file mode 100644 index 00000000..a63d058d --- /dev/null +++ b/src/Insurance/ClientType.php @@ -0,0 +1,14 @@ + 'Corporate', + 'I' => 'Individual', + ]; +} diff --git a/src/Insurance/LineType.php b/src/Insurance/LineType.php new file mode 100644 index 00000000..1f1834e1 --- /dev/null +++ b/src/Insurance/LineType.php @@ -0,0 +1,18 @@ + 'Private Car', + 'MCOC' => 'Private Motorcycle/Motorcycle with Sidecar/Tricycle', + 'CCOC' => 'Commercial Vehicle', + 'LCOC' => 'Public Motorcycle with Sidecar/Tricycle', + ]; +} diff --git a/templates/motor-vehicle-type/form.html.twig b/templates/motor-vehicle-type/form.html.twig new file mode 100644 index 00000000..5477fb13 --- /dev/null +++ b/templates/motor-vehicle-type/form.html.twig @@ -0,0 +1,151 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Motor Vehicle Types

+
+
+
+ +
+ +
+
+
+
+
+
+ + + +

+ {% if mode == 'update' %} + Edit Motor Vehicle Type + {{ motor_vehicle_type.getLtoMvType() }} + {% else %} + New Motor Vehicle Type + {% endif %} +

+
+
+
+
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+
+
+
+
+ + Back +
+
+
+
+
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} diff --git a/templates/motor-vehicle-type/list.html.twig b/templates/motor-vehicle-type/list.html.twig new file mode 100644 index 00000000..94e2cf88 --- /dev/null +++ b/templates/motor-vehicle-type/list.html.twig @@ -0,0 +1,154 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

+ Motor Vehicle Types +

+
+
+
+ +
+ +
+
+
+
+
+
+
+
+
+
+ + + + +
+
+
+
+ +
+
+ +
+ +
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} -- 2.43.5 From 101fe76ba471b88279ed17fdfcc5f279e3977cdd Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 9 Jan 2023 10:23:10 +0000 Subject: [PATCH 03/11] Add TODO comments. #727 --- src/Service/InsuranceConnector.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Service/InsuranceConnector.php b/src/Service/InsuranceConnector.php index b29d6225..f72a0b10 100644 --- a/src/Service/InsuranceConnector.php +++ b/src/Service/InsuranceConnector.php @@ -18,6 +18,12 @@ class InsuranceConnector $this->token = $token; } + // TODO: create a function that builds the client data to be sent + // might need to rethink ClientData. It might be easier to break ClientData apart into three + // separate structures + + // TODO: error handling + public function createApplication($client_info = [], $client_contact_info = [], $car_info = []) { $body = [ -- 2.43.5 From 8d2ac35c091f9ce352b72a72c4bf92e902c43da3 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 10 Jan 2023 09:27:44 +0000 Subject: [PATCH 04/11] Add function to form the data for the insurance application. #727 --- src/Insurance/ClientData.php | 299 ++++++++++++++++++++++++++++- src/Service/InsuranceConnector.php | 60 +++++- 2 files changed, 348 insertions(+), 11 deletions(-) diff --git a/src/Insurance/ClientData.php b/src/Insurance/ClientData.php index d9d392e8..68113f32 100644 --- a/src/Insurance/ClientData.php +++ b/src/Insurance/ClientData.php @@ -34,12 +34,12 @@ class ClientData protected $year_model; // year model of vehicle. Mandatory. Integer. protected $mv_type_id; // LTO mv type. Mandatory. Integer. protected $body_type; // vehicle body type. Mandatory. String. - protected $is_public; // public vehicle or not? Mandatory. Boolean. + protected $flag_public; // public vehicle or not? Mandatory. Boolean. protected $line; // defines where vehicle should be part of. Mandatory. String. Valid values are: PCOC - Private Car, MCOC - Mototcycle/Motorcycle with Sidecar/Tricycle(is_public is false), CCOC - Commercial Vehicle, LCOC - Motorcycle with sidecar/Tricycle(is_public is true) public function __construct() { - $this->client_type = 'I'; + $this->client_type = ''; $this->first_name = ''; $this->middle_name = ''; $this->surname = ''; @@ -66,11 +66,304 @@ class ClientData $this->year_model = 0; $this->mv_type_id = 0; $this->body_type = ''; - $this->is_public = false; + $this->flag_public = false; $this->line = ''; } public function setClientType($client_type) { + $this->client_type = $client_type; + return $this; + } + + public function getClientType() + { + return $this->client_type; + } + + public function setFirstName($first_name) + { + $this->first_name = $first_name; + return $this; + } + + public function getFirstName() + { + return $this->first_name; + } + + public function setMiddleName($middle_name) + { + $this->middle_name = $middle_name; + return $this; + } + + public function getMiddleName() + { + return $this->middle_name; + } + + public function setSurname($surname) + { + $this->surname = $surname; + return $this; + } + + public function getSurname() + { + return $this->surname; + } + + public function setCorporateName($corporate_name) + { + $this->corporate_name = $corporate_name; + return $this; + } + + public function getCorporateName() + { + return $this->corporate_name; + } + + public function setAddressNumber($address_number) + { + $this->address_number = $address_number; + return $this; + } + + public function getAddressNumber() + { + return $this->address_number; + } + + public function setAddressStreet($address_street) + { + $this->address_street = $address_street; + return $this; + } + + public function getAddressStreet() + { + return $this->address_street; + } + + public function setAdddressBuilding($address_building) + { + $this->address_building = $address_building; + return $this; + } + + public function getAdddressBuilding() + { + return $this->address_building; + } + + public function setAddressBarangay($address_barangay) + { + $this->address_barangay = $address_barangay; + return $this; + } + + public function getAddressBarangay() + { + return $this->address_barangay; + } + + public function setAddressCity($address_city) + { + $this->address_city = $address_city; + return $this; + } + + public function getAddressCity() + { + return $this->address_city; + } + + public function setAddressProvince($address_province) + { + $this->address_province = $address_province; + return $this; + } + + public function getAddressProvince() + { + return $this->address_province; + } + + public function setZipcode($zipcode) + { + $this->zipcode = $zipcode; + return $this; + } + + public function getZipcode() + { + return $this->zipcode; + } + + public function setMobileNumber($mobile_number) + { + $this->mobile_number = $mobile_number; + return $this; + } + + public function getMobileNumber() + { + return $this->mobile_number; + } + + public function setEmailAddress($email_address) + { + $this->email_address = $email_address; + return $this; + } + + public function getEmailAddress() + { + return $this->email_address; + } + + public function setMake($make) + { + $this->make = $make; + return $this; + } + + public function getMake() + { + return $this->make; + } + + public function setModel($model) + { + $this->model = $model; + return $this; + } + + public function getModel() + { + return $this->model; + } + + public function setSeries($series) + { + $this->series = $series; + return $this; + } + + public function getSeries() + { + return $this->series; + } + + public function setColor($color) + { + $this->color = $color; + return $this; + } + + public function getColor() + { + return $this->color; + } + + public function setPlateNumber($plate_number) + { + $this->plate_number = $plate_number; + return $this; + } + + public function getPlateNumber() + { + return $this->plate_number; + } + + public function setMvFileNumber($mv_file_number) + { + $this->mv_file_number = $mv_file_number; + return $this; + } + + public function getMvFileNumber() + { + return $this->mv_file_number; + } + + public function setMotorNumber($motor_number) + { + $this->motor_number = $motor_number; + return $this; + } + + public function getMotorNumber() + { + return $this->motor_number; + } + + public function setSerialChassis($serial_chassis) + { + $this->serial_chassis = $serial_chassis; + return $this; + } + + public function getSerialChassis() + { + return $this->serial_chassis; + } + + public function setYearModel($year_model) + { + $this->year_model = $year_model; + return $this; + } + + public function getYearModel() + { + return $this->year_model; + } + + public function setMvTypeID($mv_type_id) + { + $this->mv_type_id = $mv_type_id; + return $this; + } + + public function getMvTypeID() + { + return $this->mv_type_id; + } + + public function setBodyType($body_type) + { + $this->body_type = $body_type; + return $this; + } + + public function getBodyType() + { + return $this->body_type; + } + + public function setLine($line) + { + $this->line = $line; + return $this; + } + + public function getLine() + { + return $this->line; + } + + public function setPublic($flag_public = true) + { + $this->flag_public = $flag_public; + return $this; + } + + public function isPublic() + { + return $this->flag_public; } } diff --git a/src/Service/InsuranceConnector.php b/src/Service/InsuranceConnector.php index f72a0b10..fbf306f6 100644 --- a/src/Service/InsuranceConnector.php +++ b/src/Service/InsuranceConnector.php @@ -2,6 +2,12 @@ namespace App\Service; +use App\Entity\MotorVehicleType; + +use App\Insurance\ClientData; +use App\Insurance\LineType; +use App\Insurance\ClientType; + class InsuranceConnector { protected $base_url; @@ -18,20 +24,58 @@ class InsuranceConnector $this->token = $token; } - // TODO: create a function that builds the client data to be sent - // might need to rethink ClientData. It might be easier to break ClientData apart into three - // separate structures - - // TODO: error handling - - public function createApplication($client_info = [], $client_contact_info = [], $car_info = []) + public function createClientData(ClientData $client_data) { - $body = [ + // transform ClientData into the format needed for insurance application + $client_info = [ + 'client_type' => $client_data->getClientType(), + 'first_name' => $client_data->getFirstName(), + 'middle_name' => $client_data->getMiddleName(), + 'surname' => $client_data->getSurname(), + 'corporate_name' => $client_data->getCorporateName(), + ]; + + $client_contact_info = [ + 'address_number' => $client_data->getAddressNumber(), + 'address_street' => $client_data->getAddressStreet(), + 'address_building' => $client_data->getAddressBuilding(), + 'address_barangay' => $client_data->getAddressBarangay(), + 'address_city' => $client_data->getAddressCity(), + 'address_province' => $client_data->getAddressProvince(), + 'zipcode' => $client_data->getZipcode(), + 'mobile_number' => $cilent_data->getMobileNumber(), + 'email_address' => $client_data->getEmailAddress(), + ]; + + $car_info = [ + 'make' => $client_data->getMake(), + 'model' => $client_data->getModel(),, + 'series' => $client_data->getSeries(), + 'color' => $client_data->getColor(), + 'plate_number' => $client_data->getPlateNumber(), + 'mv_file_number' => $client_data->getMvFileNumber(), + 'motor_number' => $client_data->getMotorNumber(), + 'serial_chasis' => $client_data->getSerialChassis(), + 'year_model' => $client_data->getYearModel(), + 'mv_type_id' => $client_data->getMvTypeID(), + 'body_type' => $client_data->getBodyType(), + 'is_public' => $client_data->isPublic(), + 'line' => $client_data->getLine(), + ]; + + $app_data = [ 'client_info' => $client_info, 'client_contact_info' => $client_contact_info, 'car_info' => $car_info, ]; + $result = $this->createApplication($app_data); + + // TODO: error handling + } + + protected function createApplication($body) + { $body_text = json_encode($body); // generate token for basic authorization -- 2.43.5 From 0675637b52f8acdc5edb4916e6174e6659bb15dc Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 11 Jan 2023 07:55:11 +0000 Subject: [PATCH 05/11] Add function to transform ClientData into the format required by insurance. Modify test command. #727 --- src/Command/TestInsuranceConnectorCommand.php | 69 ++++++++++--------- src/Insurance/ClientData.php | 4 +- src/Service/InsuranceConnector.php | 57 ++++++++++++--- 3 files changed, 84 insertions(+), 46 deletions(-) diff --git a/src/Command/TestInsuranceConnectorCommand.php b/src/Command/TestInsuranceConnectorCommand.php index 7599321a..0b063ae4 100644 --- a/src/Command/TestInsuranceConnectorCommand.php +++ b/src/Command/TestInsuranceConnectorCommand.php @@ -9,6 +9,8 @@ use Symfony\Component\Console\Output\OutputInterface; use App\Service\InsuranceConnector; +use App\Insurance\ClientData; + class TestInsuranceConnectorCommand extends Command { protected $insurance; @@ -29,43 +31,42 @@ class TestInsuranceConnectorCommand extends Command protected function execute(InputInterface $input, OutputInterface $output) { - $client_info = [ - 'client_type' => 'I', - 'first_name' => '', - 'middle_name' => 'Resq', - 'surname' => 'Customer', - 'corporate_name' => 'Customer, Test Resq' - ]; + $client_data = new ClientData(); - $client_contact_info = [ - 'address_number' => '112', - 'address_street' => 'Test Street', - 'address_building' => 'Test Building', - 'address_barangay' => 'Legaspi Village', - 'address_city' => 'Makati City', - 'address_province' => 'Metro Manila', - 'zipcode' => '1200', - 'mobile_number' => '09171234567', - 'email_address' => 'test.resq@gmail.com' - ]; + // set client info part + $client_data->setClientType('I') + ->setFirstName('Test') + ->setMiddleName('Resq') + ->setSurname('Customer') + ->setCorporateName('Customer, Test Resq'); + + // set the client contact info part + $client_data->setAddressNumber('113') + ->setAddressStreet('Test Street') + ->setAddressBuilding('Test Building') + ->setAddressBarangay('Legaspi Village') + ->setAddressCity('Makati City') + ->setAddressProvince('Metro Manila') + ->setZipcode('1200') + ->setMobileNumber('09171234567') + ->setEmailAddress('test.resq@gmail.com'); - $car_info = [ - 'make' => 'HYUNDAI', - 'model' => 'ACCENT', - 'series' => '1.4 A/T', - 'color' => 'PHANTOM BLACK', - 'plate_number' => 'ABC1234', - 'mv_file_number' => '123456789012345', - 'motor_number' => 'E31TE-0075268', - 'serial_chasis' => 'PA0SEF210K0075701', - 'year_model' => '2020', - 'mv_type_id' => 1, - 'body_type' => 'SEDAN', - 'is_public' => false, - 'line' => 'pcoc' - ]; + // set the car info part + $client_data->setMake('HYUNDAI') + ->setModel('ACCENT') + ->setSeries('1.4 A/T') + ->setColor('PHANTOM BLACK') + ->setPlateNumber('ABC1234') + ->setMvFileNumber('123456789012345') + ->setMotorNumber('E31TE-0075268') + ->setSerialChassis('PA0SEF210K0075701') + ->setYearModel('2020') + ->setMvTypeID(1) + ->setBodyType('SEDAN') + ->setLine('pcoc') + ->setPublic(false); - $result = $this->insurance->createApplication($client_info, $client_contact_info, $car_info); + $result = $this->insurance->processApplication($client_data); error_log(json_encode($result)); diff --git a/src/Insurance/ClientData.php b/src/Insurance/ClientData.php index 68113f32..9707eadf 100644 --- a/src/Insurance/ClientData.php +++ b/src/Insurance/ClientData.php @@ -147,13 +147,13 @@ class ClientData return $this->address_street; } - public function setAdddressBuilding($address_building) + public function setAddressBuilding($address_building) { $this->address_building = $address_building; return $this; } - public function getAdddressBuilding() + public function getAddressBuilding() { return $this->address_building; } diff --git a/src/Service/InsuranceConnector.php b/src/Service/InsuranceConnector.php index fbf306f6..a58bd50f 100644 --- a/src/Service/InsuranceConnector.php +++ b/src/Service/InsuranceConnector.php @@ -24,7 +24,7 @@ class InsuranceConnector $this->token = $token; } - public function createClientData(ClientData $client_data) + public function processApplication(ClientData $client_data) { // transform ClientData into the format needed for insurance application $client_info = [ @@ -43,13 +43,13 @@ class InsuranceConnector 'address_city' => $client_data->getAddressCity(), 'address_province' => $client_data->getAddressProvince(), 'zipcode' => $client_data->getZipcode(), - 'mobile_number' => $cilent_data->getMobileNumber(), + 'mobile_number' => $client_data->getMobileNumber(), 'email_address' => $client_data->getEmailAddress(), ]; $car_info = [ 'make' => $client_data->getMake(), - 'model' => $client_data->getModel(),, + 'model' => $client_data->getModel(), 'series' => $client_data->getSeries(), 'color' => $client_data->getColor(), 'plate_number' => $client_data->getPlateNumber(), @@ -69,12 +69,17 @@ class InsuranceConnector 'car_info' => $car_info, ]; - $result = $this->createApplication($app_data); + $result = $this->sendApplication($app_data); - // TODO: error handling + $app_result = json_decode($result, true); + + // process response received from insurance + $res = $this->checkApplicationResult($app_result); + + return $res; } - protected function createApplication($body) + protected function sendApplication($body) { $body_text = json_encode($body); @@ -83,13 +88,45 @@ class InsuranceConnector $res = $this->curlPost('api/v1/ctpl/applications', $body_text, $token); - $app_res = json_decode($res, true); + return $res; + } - // check result - if ($app_res == null) + protected function checkApplicationResult($app_result) + { + if ($app_result == null) + { + // insurance api returned an empty json return []; + } - return $app_res; + // check if message is set + $message = ''; + if (isset($app_result['message'])) + $message = $app_result['message']; + + // check if id is set + $id = ''; + if (isset($app_result['id'])) + $id = $app_result['id']; + + // check if status is set, meaning, there's an error + $status = ''; + if (isset($app_result['status'])) + $status = 'error'; + else + $status = 'success'; + + $data = [ + 'id' => $id + ]; + + $processed_result = [ + 'status' => $status, + 'message' => $message, + 'data' => $data, + ]; + + return $processed_result; } protected function generateAuthToken($username, $password) -- 2.43.5 From 675bca15921c290f3c3496205b7a9b15738a54a7 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 12 Jan 2023 09:18:47 +0000 Subject: [PATCH 06/11] Add validation service for the insurance data. #727 --- src/Command/TestInsuranceConnectorCommand.php | 2 + src/Service/InsuranceClientDataValidator.php | 237 ++++++++++++++++++ 2 files changed, 239 insertions(+) create mode 100644 src/Service/InsuranceClientDataValidator.php diff --git a/src/Command/TestInsuranceConnectorCommand.php b/src/Command/TestInsuranceConnectorCommand.php index 0b063ae4..a2846e49 100644 --- a/src/Command/TestInsuranceConnectorCommand.php +++ b/src/Command/TestInsuranceConnectorCommand.php @@ -66,6 +66,8 @@ class TestInsuranceConnectorCommand extends Command ->setLine('pcoc') ->setPublic(false); + // TOOD: add validation here + $result = $this->insurance->processApplication($client_data); error_log(json_encode($result)); diff --git a/src/Service/InsuranceClientDataValidator.php b/src/Service/InsuranceClientDataValidator.php new file mode 100644 index 00000000..d14f4c61 --- /dev/null +++ b/src/Service/InsuranceClientDataValidator.php @@ -0,0 +1,237 @@ +getClientType())) + { + $error = 'Client type is required.'; + return $error; + } + + // client type values should either be C or I + if (($client_data->getClientType() != ClientType::CORPORATE) || + ($client_type->ClientType() != ClientType::INDIVIDUAL)) + { + $error = 'Invalid cilent type values. Values should only be C for corporate type or I for individual.'; + return $error; + } + + // first name is required + if (empty($client_data->getFirstName())) + { + $error = 'First name is required.'; + return $error; + } + + // surname is required + if (empty($client_data->getSurname())) + { + $error = 'Surname is required.'; + return $error; + } + + // corporate name is required + if (empty($client_data->getCorporateName())) + { + $error = 'Corporate name is required.'; + return $error; + } + + // number address is required + if (empty($client_data->getAddressNumber())) + { + $error = 'House number address is required.'; + return $error; + } + + // barangay address is required + if (empty($client_data->getAddressBarangay())) + { + $error = 'Barangay address is required.'; + return $error; + } + + // city address is required + if (empty($client_data->getAddressCity())) + { + $error = 'City address is required.'; + return $error; + } + + // province address is required + if (empty($client_data->getAddressProvince())) + { + $error = 'Province address is required.'; + return $error; + } + + // zipcode is required + if (empty($client_data->getZipcode())) + { + $error = 'Zipcode is required.'; + return $error; + } + + // mobile number is required + if (empty($client_data->getMobileNumber())) + { + $error = 'Mobile number is required.'; + return $error; + } + + // email address is required + if (empty($client_data->getEmailAddress())) + { + $error = 'Email address is required.'; + return $error; + } + + // make is required + if (empty($client_data->getMake())) + { + $error = 'Vehicle make is required.'; + return $error; + } + + // model is required + if (empty($client_data->getMake())) + { + $error = 'Vehicle make is required.'; + return $error; + } + + // series is required + if (empty($client_data->getSeries())) + { + $error = 'Vehicle series is required.'; + return $error; + } + + // color is required + if (empty($client_data->getColor())) + { + $error = 'Vehicle color is required.'; + return $error; + } + + // plate number is required + if (empty($client_data->getPlateNumber())) + { + $error = 'Plate number is required.'; + return $error; + } + + // plate number is correct format + $is_valid_plate = $this->checkPlateNumber($client_data->getPlateNumber()); + if (!($is_valid_plate)) + { + $error = 'Invalid plate number.'; + return $error; + } + + // MV file number is required + if (empty($client_data->getMvFileNumber())) + { + $error = 'MV file number is required.'; + return $error; + } + + // motor number is required + if (empty($client_data->getMotorNumber())) + { + $error = 'Motor number is required.'; + return $error; + } + + // serial or chassis number is required + if (empty($client_data->getSerialChassis())) + { + $error = 'Serial or chassis number is required.'; + return $error; + } + + // vehicle year model is required + if (empty($client_data->getYearModel())) + { + $error = 'Vehicle year model is required.'; + return $error; + } + + // motor vehicle type id is required + if ($client_data->getMvTypeID() == 0) + { + $error = 'Motor vehicle type must be set.'; + return $error; + } + + // TODO: should we check if it's a valid MV type? + + // body type is required + if ($client_data->getBodyType()) + { + $error = 'Vehicle body type is required.'; + return $error; + } + + // line is required + if (empty($client_data->getLine())) + { + $error = 'Line type is required.'; + return $error; + } + + // check line if valid + if (($client_data->getLine() !== LineType::PRIVATE_CAR) || + ($client_data->getLine() !== LineType::PRIVATE_MOTORCYCLE) || + ($client_data->getLine() !== LineType::COMMERCIAL_VEHICLE) || + ($client_data->getLine() !== LineType::PUBLIC_MOTORCYCLE)) + { + $error = 'Invalid line type.'; + return $error; + } + + // check line type and flag_public combination + // if line type is MCOC, flag_public should be false + // if line type is LCOC, flag_public should be true + if ($client_data->getLine() == LineType::MCOC) + { + if ($client_data->isPublic()) + { + $error = 'Line type is invalid for public vehicles.'; + return $error; + } + } + + if ($client_data->getLine() == LineType::LCOC) + { + if (!($client_data->isPublic())) + { + $error = 'Line type is invalid for private vehicles.'; + return $error; + } + } + + return null; + } + + protected function checkPlateNumber($plate) + { + // check if alphanumeric, max length is 11, no spaces, uppercase + $res = preg_match("/^[A-Z0-9]{1,11}+$/", $plate); + if ($res) + return true; + + return false; + } +} -- 2.43.5 From 987c865620e29d0f0f95264c7f8850b1468d867f Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 13 Jan 2023 07:27:42 +0000 Subject: [PATCH 07/11] Fix issues found during normal scenario testing. #727 --- config/services.yaml | 5 +++ src/Command/TestInsuranceConnectorCommand.php | 23 ++++++---- src/Insurance/ClientType.php | 4 +- src/Insurance/LineType.php | 4 +- src/Service/InsuranceClientDataValidator.php | 42 +++++++++++++------ 5 files changed, 56 insertions(+), 22 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index b2460c3f..053cce20 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -336,3 +336,8 @@ services: $username: "%env(INSURANCE_USERNAME)%" $password: "%env(INSURANCE_PASSWORD)%" $token: "%env(INSURANCE_TOKEN)%" + + # insurance data validator + App\Service\InsuranceDataValidator: + arguments: + $em: "@doctrine.orm.entity_manager" diff --git a/src/Command/TestInsuranceConnectorCommand.php b/src/Command/TestInsuranceConnectorCommand.php index a2846e49..f8ac2d7f 100644 --- a/src/Command/TestInsuranceConnectorCommand.php +++ b/src/Command/TestInsuranceConnectorCommand.php @@ -8,12 +8,16 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use App\Service\InsuranceConnector; +use App\Service\InsuranceDataValidator; use App\Insurance\ClientData; +use App\Insurance\ClientType; +use App\Insurance\LineType; class TestInsuranceConnectorCommand extends Command { protected $insurance; + protected $ins_validator; protected function configure() { @@ -22,9 +26,10 @@ class TestInsuranceConnectorCommand extends Command ->setHelp('Test the create insuranace application service.'); } - public function __construct(InsuranceConnector $insurance) + public function __construct(InsuranceConnector $insurance, InsuranceDataValidator $ins_validator) { $this->insurance = $insurance; + $this->ins_validator = $ins_validator; parent::__construct(); } @@ -34,7 +39,7 @@ class TestInsuranceConnectorCommand extends Command $client_data = new ClientData(); // set client info part - $client_data->setClientType('I') + $client_data->setClientType(ClientType::INDIVIDUAL) ->setFirstName('Test') ->setMiddleName('Resq') ->setSurname('Customer') @@ -63,14 +68,18 @@ class TestInsuranceConnectorCommand extends Command ->setYearModel('2020') ->setMvTypeID(1) ->setBodyType('SEDAN') - ->setLine('pcoc') + ->setLine(LineType::PRIVATE_CAR) ->setPublic(false); - // TOOD: add validation here + $error_message = $this->ins_validator->validateClientData($client_data); + if ($error_message == null) + { + $result = $this->insurance->processApplication($client_data); - $result = $this->insurance->processApplication($client_data); - - error_log(json_encode($result)); + error_log(json_encode($result)); + } + else + error_log($error_message); return 0; } diff --git a/src/Insurance/ClientType.php b/src/Insurance/ClientType.php index a63d058d..8b28d443 100644 --- a/src/Insurance/ClientType.php +++ b/src/Insurance/ClientType.php @@ -1,6 +1,8 @@ em = $em; + } + public function validateClientData(ClientData$client_data) { // client type is required @@ -20,10 +29,10 @@ class InsuranceClientDataValidator } // client type values should either be C or I - if (($client_data->getClientType() != ClientType::CORPORATE) || - ($client_type->ClientType() != ClientType::INDIVIDUAL)) + if (($client_data->getClientType() !== ClientType::CORPORATE) && + ($client_data->getClientType() !== ClientType::INDIVIDUAL)) { - $error = 'Invalid cilent type values. Values should only be C for corporate type or I for individual.'; + $error = 'Invalid client type values. Values should only be C for corporate type or I for individual.'; return $error; } @@ -175,10 +184,17 @@ class InsuranceClientDataValidator return $error; } - // TODO: should we check if it's a valid MV type? + // check if it's a valid MV type + $mv_type_id = $client_data->getMvTypeID(); + $mv_type = $this->em->getRepository(MotorVehicleType::class)->findBy(['mv_type_id' => $mv_type_id]); + if ($mv_type == null) + { + $error = 'Invalid motor vehicle type.'; + return $error; + } // body type is required - if ($client_data->getBodyType()) + if (empty($client_data->getBodyType())) { $error = 'Vehicle body type is required.'; return $error; @@ -192,9 +208,9 @@ class InsuranceClientDataValidator } // check line if valid - if (($client_data->getLine() !== LineType::PRIVATE_CAR) || - ($client_data->getLine() !== LineType::PRIVATE_MOTORCYCLE) || - ($client_data->getLine() !== LineType::COMMERCIAL_VEHICLE) || + if (($client_data->getLine() !== LineType::PRIVATE_CAR) && + ($client_data->getLine() !== LineType::PRIVATE_MOTORCYCLE) && + ($client_data->getLine() !== LineType::COMMERCIAL_VEHICLE) && ($client_data->getLine() !== LineType::PUBLIC_MOTORCYCLE)) { $error = 'Invalid line type.'; @@ -202,9 +218,9 @@ class InsuranceClientDataValidator } // check line type and flag_public combination - // if line type is MCOC, flag_public should be false - // if line type is LCOC, flag_public should be true - if ($client_data->getLine() == LineType::MCOC) + // if line type is MCOC/private motorcycle, flag_public should be false + // if line type is LCOC/public motorcycle, flag_public should be true + if ($client_data->getLine() == LineType::PRIVATE_MOTORCYCLE) { if ($client_data->isPublic()) { @@ -213,7 +229,7 @@ class InsuranceClientDataValidator } } - if ($client_data->getLine() == LineType::LCOC) + if ($client_data->getLine() == LineType::PUBLIC_MOTORCYCLE) { if (!($client_data->isPublic())) { -- 2.43.5 From ff32ee6154a8765d4d17a0674cc3ae7d327f9948 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 13 Jan 2023 08:09:23 +0000 Subject: [PATCH 08/11] Fix issues found during abnormal scenario testing. #727 --- src/Command/TestInsuranceConnectorCommand.php | 5 ++--- src/Service/InsuranceClientDataValidator.php | 11 +++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Command/TestInsuranceConnectorCommand.php b/src/Command/TestInsuranceConnectorCommand.php index f8ac2d7f..6cdb8f8c 100644 --- a/src/Command/TestInsuranceConnectorCommand.php +++ b/src/Command/TestInsuranceConnectorCommand.php @@ -23,7 +23,7 @@ class TestInsuranceConnectorCommand extends Command { $this->setName('test:create-insurance-application') ->setDescription('Test create insurance application service.') - ->setHelp('Test the create insuranace application service.'); + ->setHelp('Test the create insurance application service.'); } public function __construct(InsuranceConnector $insurance, InsuranceDataValidator $ins_validator) @@ -52,7 +52,7 @@ class TestInsuranceConnectorCommand extends Command ->setAddressBarangay('Legaspi Village') ->setAddressCity('Makati City') ->setAddressProvince('Metro Manila') - ->setZipcode('1200') + ->setZipcode(1200) ->setMobileNumber('09171234567') ->setEmailAddress('test.resq@gmail.com'); @@ -83,5 +83,4 @@ class TestInsuranceConnectorCommand extends Command return 0; } - } diff --git a/src/Service/InsuranceClientDataValidator.php b/src/Service/InsuranceClientDataValidator.php index 501e311d..33a01ddd 100644 --- a/src/Service/InsuranceClientDataValidator.php +++ b/src/Service/InsuranceClientDataValidator.php @@ -86,7 +86,7 @@ class InsuranceDataValidator } // zipcode is required - if (empty($client_data->getZipcode())) + if ($client_data->getZipcode() == 0) { $error = 'Zipcode is required.'; return $error; @@ -141,6 +141,8 @@ class InsuranceDataValidator return $error; } + // TODO: the insurance api doesn't care if the plate number is valid. + // do we still check for valid plate number? // plate number is correct format $is_valid_plate = $this->checkPlateNumber($client_data->getPlateNumber()); if (!($is_valid_plate)) @@ -180,7 +182,7 @@ class InsuranceDataValidator // motor vehicle type id is required if ($client_data->getMvTypeID() == 0) { - $error = 'Motor vehicle type must be set.'; + $error = 'Motor vehicle type is required.'; return $error; } @@ -220,7 +222,7 @@ class InsuranceDataValidator // check line type and flag_public combination // if line type is MCOC/private motorcycle, flag_public should be false // if line type is LCOC/public motorcycle, flag_public should be true - if ($client_data->getLine() == LineType::PRIVATE_MOTORCYCLE) + if ($client_data->getLine() == LineType::PRIVATE_MOTORCYCLE) { if ($client_data->isPublic()) { @@ -229,7 +231,8 @@ class InsuranceDataValidator } } - if ($client_data->getLine() == LineType::PUBLIC_MOTORCYCLE) + if (($client_data->getLine() == LineType::PUBLIC_MOTORCYCLE) || + ($client_data->getLine() == LineType::COMMERCIAL_VEHICLE)) { if (!($client_data->isPublic())) { -- 2.43.5 From 886a9e669e0d73c90fff407bd9d7273fbbb616da Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 13 Jan 2023 10:10:15 +0000 Subject: [PATCH 09/11] Add controller for mobile app for CTPL creation. #728 --- config/routes/api_insurance.yaml | 6 + .../Insurance/InsuranceAPIController.php | 159 ++++++++++++++++++ ...lidator.php => InsuranceDataValidator.php} | 0 3 files changed, 165 insertions(+) create mode 100644 config/routes/api_insurance.yaml create mode 100644 src/Controller/Insurance/InsuranceAPIController.php rename src/Service/{InsuranceClientDataValidator.php => InsuranceDataValidator.php} (100%) diff --git a/config/routes/api_insurance.yaml b/config/routes/api_insurance.yaml new file mode 100644 index 00000000..b50c3975 --- /dev/null +++ b/config/routes/api_insurance.yaml @@ -0,0 +1,6 @@ +# insurance api + +api_insurance_create: + path: /api/insurance/create + controller: App\Controller\Insurance\InsuranceAPIController::createCTPLApplication + methods: [POST] diff --git a/src/Controller/Insurance/InsuranceAPIController.php b/src/Controller/Insurance/InsuranceAPIController.php new file mode 100644 index 00000000..5807f1d6 --- /dev/null +++ b/src/Controller/Insurance/InsuranceAPIController.php @@ -0,0 +1,159 @@ +session = null; + } + + public function createCTPLApplication(Request $req, EntityManagerInterface $em, InsuranceDataValidator $ins_validator, + InsuranceConnector $insurance) + { + // TODO: are we letting the app fill in all the fields needed for the CTPL application? + // check parameters + $required_params = [ + '', + ]; + + // check required parameters and api key + $res = $this->checkParamsAndKey($req, $em, $required_params); + if ($res->isError()) + return $res->getReturnResponse(); + + // create client data + $client_data = new ClientData(); + + // TODO: set values for client data + + // check if client data values are valid + $error_mesage = $ins_validator->validateClientData($client_data); + if ($error_message != null) + { + // return error message + $res->setError(true) + ->setErrorMessage($error_message); + + return $res; + } + + $result = $insurance->processApplication($client_data); + + // check status of result + if ($result['status'] == 'error') + { + // get message and return error message + $message = $result['message']; + $res->setError(true) + ->setErrorMessage($message); + + return $res; + } + + // return data portion of result received from insurance api + $data = $result['data']; + + $res->setData($data); + + return $res->getReturnResponse(); + } + + protected function checkMissingParameters(Request $req, $params = []) + { + $missing = []; + + // check if parameters are there + foreach ($params as $param) + { + if ($req->getMethod() == 'GET') + { + $check = $req->query->get($param); + if (empty($check)) + $missing[] = $param; + } + else if ($req->getMethod() == 'POST') + { + $check = $req->request->get($param); + if (empty($check)) + $missing[] = $param; + } + else + return $params; + } + + return $missing; + } + + protected function checkAPIKey($em, $api_key) + { + // find the api key (session id) + $session = $em->getRepository(MobileSession::class)->find($api_key); + if ($session == null) + return null; + + return $session; + } + + protected function checkParamsAndKey(Request $req, $em, $params) + { + // returns APIResult object + $res = new APIResult(); + + // check for api_key in query string + $api_key = $req->query->get('api_key'); + if (empty($api_key)) + { + $res->setError(true) + ->setErrorMessage('Missing API key'); + return $res; + } + + // check missing parameters + $missing = $this->checkMissingParameters($req, $params); + if (count($missing) > 0) + { + $miss_string = implode(', ', $missing); + $res->setError(true) + ->setErrorMessage('Missing parameter(s): ' . $miss_string); + return $res; + } + + // check api key + $sess = $this->checkAPIKey($em, $req->query->get('api_key')); + if ($sess == null) + { + $res->setError(true) + ->setErrorMessage('Invalid API Key'); + return $res; + } + + // store session + $this->session = $sess; + + return $res; + } +} diff --git a/src/Service/InsuranceClientDataValidator.php b/src/Service/InsuranceDataValidator.php similarity index 100% rename from src/Service/InsuranceClientDataValidator.php rename to src/Service/InsuranceDataValidator.php -- 2.43.5 From ce133addfc1517c003f6d8666085eed5ebce74d4 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 16 Jan 2023 07:00:18 +0000 Subject: [PATCH 10/11] Fix issues found during testing. #728 --- src/Command/TestInsuranceConnectorCommand.php | 2 +- .../Insurance/InsuranceAPIController.php | 71 +++++++++++++++++-- src/Service/InsuranceDataValidator.php | 5 +- 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/src/Command/TestInsuranceConnectorCommand.php b/src/Command/TestInsuranceConnectorCommand.php index 6cdb8f8c..8c1ecaa8 100644 --- a/src/Command/TestInsuranceConnectorCommand.php +++ b/src/Command/TestInsuranceConnectorCommand.php @@ -65,7 +65,7 @@ class TestInsuranceConnectorCommand extends Command ->setMvFileNumber('123456789012345') ->setMotorNumber('E31TE-0075268') ->setSerialChassis('PA0SEF210K0075701') - ->setYearModel('2020') + ->setYearModel(2020) ->setMvTypeID(1) ->setBodyType('SEDAN') ->setLine(LineType::PRIVATE_CAR) diff --git a/src/Controller/Insurance/InsuranceAPIController.php b/src/Controller/Insurance/InsuranceAPIController.php index 5807f1d6..c1f2e0bc 100644 --- a/src/Controller/Insurance/InsuranceAPIController.php +++ b/src/Controller/Insurance/InsuranceAPIController.php @@ -34,10 +34,32 @@ class InsuranceAPIController extends Controller implements LoggedController public function createCTPLApplication(Request $req, EntityManagerInterface $em, InsuranceDataValidator $ins_validator, InsuranceConnector $insurance) { - // TODO: are we letting the app fill in all the fields needed for the CTPL application? // check parameters $required_params = [ - '', + 'client_type', + 'first_name', + 'last_name', + 'corporate_name', + 'address_number', + 'address_barangay', + 'address_city', + 'address_province', + 'zipcode', + 'mobile_number', + 'email_address', + 'make', + 'model', + 'series', + 'color', + 'plate_number', + 'mv_file_number', + 'motor_number', + 'serial_chassis', + 'year_model', + 'mv_type_id', + 'body_type', + 'is_public', + 'line', ]; // check required parameters and api key @@ -48,17 +70,18 @@ class InsuranceAPIController extends Controller implements LoggedController // create client data $client_data = new ClientData(); - // TODO: set values for client data + $this->setClientData($req, $client_data); // check if client data values are valid - $error_mesage = $ins_validator->validateClientData($client_data); + $error_message = $ins_validator->validateClientData($client_data); if ($error_message != null) { + error_log('client data values are not valid ' . $error_message); // return error message $res->setError(true) ->setErrorMessage($error_message); - return $res; + return $res->getReturnResponse(); } $result = $insurance->processApplication($client_data); @@ -71,7 +94,7 @@ class InsuranceAPIController extends Controller implements LoggedController $res->setError(true) ->setErrorMessage($message); - return $res; + return $res->getReturnResponse(); } // return data portion of result received from insurance api @@ -82,6 +105,42 @@ class InsuranceAPIController extends Controller implements LoggedController return $res->getReturnResponse(); } + protected function setClientData(Request $req, ClientData $client_data) + { + // set client info part + $client_data->setClientType($req->request->get('client_type', '')) + ->setFirstName($req->request->get('first_name', '')) + ->setMiddleName($req->request->get('middle_name', '')) + ->setSurname($req->request->get('last_name', '')) + ->setCorporateName($req->request->get('corporate_name', '')); + + // set the client contact info part + $client_data->setAddressNumber($req->request->get('address_number', '')) + ->setAddressStreet($req->request->get('address_street', '')) + ->setAddressBuilding($req->request->get('address_building', '')) + ->setAddressBarangay($req->request->get('address_barangay', '')) + ->setAddressCity($req->request->get('address_city', '')) + ->setAddressProvince($req->request->get('address_province', '')) + ->setZipcode($req->request->get('zipcode', 0)) + ->setMobileNumber($req->request->get('mobile_number', '')) + ->setEmailAddress($req->request->get('email_address', '')); + + // set the car info part + $client_data->setMake($req->request->get('make', '')) + ->setModel($req->request->get('model', '')) + ->setSeries($req->request->get('series', '')) + ->setColor($req->request->get('color', '')) + ->setPlateNumber($req->request->get('plate_number', '')) + ->setMvFileNumber($req->request->get('mv_file_number', '')) + ->setMotorNumber($req->request->get('motor_number', '')) + ->setSerialChassis($req->request->get('serial_chassis', '')) + ->setYearModel($req->request->get('year_model', 0)) + ->setMvTypeID($req->request->get('mv_type_id', 0)) + ->setBodyType($req->request->get('body_type', '')) + ->setLine($req->request->get('line', '')) + ->setPublic($req->request->get('is_public', false)); + } + protected function checkMissingParameters(Request $req, $params = []) { $missing = []; diff --git a/src/Service/InsuranceDataValidator.php b/src/Service/InsuranceDataValidator.php index 33a01ddd..74b4ea32 100644 --- a/src/Service/InsuranceDataValidator.php +++ b/src/Service/InsuranceDataValidator.php @@ -57,6 +57,8 @@ class InsuranceDataValidator return $error; } + // TODO: should we check for the format of corporate name if client type is Individual? + // number address is required if (empty($client_data->getAddressNumber())) { @@ -173,7 +175,7 @@ class InsuranceDataValidator } // vehicle year model is required - if (empty($client_data->getYearModel())) + if ($client_data->getYearModel() == 0) { $error = 'Vehicle year model is required.'; return $error; @@ -222,6 +224,7 @@ class InsuranceDataValidator // check line type and flag_public combination // if line type is MCOC/private motorcycle, flag_public should be false // if line type is LCOC/public motorcycle, flag_public should be true + // TODO: should we check other combinations? Like PCOC/private car and flag_public is true? if ($client_data->getLine() == LineType::PRIVATE_MOTORCYCLE) { if ($client_data->isPublic()) -- 2.43.5 From 1cd52e264fbdc2ad22865a6211e14b69835e5963 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 16 Jan 2023 09:36:36 +0000 Subject: [PATCH 11/11] Remove debug message. #728 --- src/Controller/Insurance/InsuranceAPIController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Controller/Insurance/InsuranceAPIController.php b/src/Controller/Insurance/InsuranceAPIController.php index c1f2e0bc..258b10ac 100644 --- a/src/Controller/Insurance/InsuranceAPIController.php +++ b/src/Controller/Insurance/InsuranceAPIController.php @@ -76,7 +76,6 @@ class InsuranceAPIController extends Controller implements LoggedController $error_message = $ins_validator->validateClientData($client_data); if ($error_message != null) { - error_log('client data values are not valid ' . $error_message); // return error message $res->setError(true) ->setErrorMessage($error_message); -- 2.43.5