From bc9cf2a74f8fe31895d52f4b75e38b7f4a290bed Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Thu, 10 Jan 2019 02:26:42 +0800 Subject: [PATCH] Modify claim warranty api call #171 --- catalyst/api-bundle/Command/TestCommand.php | 10 +++++- config/routes/warranty_api.yaml | 2 +- src/Controller/CAPI/WarrantyController.php | 37 ++++++++++++++++++--- src/Entity/Warranty.php | 20 ++++++++++- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/catalyst/api-bundle/Command/TestCommand.php b/catalyst/api-bundle/Command/TestCommand.php index 5b19d182..12bf1b6f 100644 --- a/catalyst/api-bundle/Command/TestCommand.php +++ b/catalyst/api-bundle/Command/TestCommand.php @@ -63,9 +63,17 @@ class TestCommand extends Command // warranty find $api->get('/capi/warranties/' . $serial); + */ // warranty claim - $api->post('/capi/warranties/' . $serial . '/claim'); + $id = 86811; + $serial = 'TEST001'; + $params = [ + 'serial' => $serial, + ]; + $api->post('/capi/warranties/' . $id . '/claim', $params); + + /* // plate warranty $api->get('/capi/plates/' . $plate_num . '/warranties'); diff --git a/config/routes/warranty_api.yaml b/config/routes/warranty_api.yaml index 0aed61c5..baf3643d 100644 --- a/config/routes/warranty_api.yaml +++ b/config/routes/warranty_api.yaml @@ -56,7 +56,7 @@ capi_warranty_register: # claim warranty capi_warranty_claim: - path: /capi/warranties/{serial}/claim + path: /capi/warranties/{id}/claim controller: App\Controller\CAPI\WarrantyController::claim methods: [POST] diff --git a/src/Controller/CAPI/WarrantyController.php b/src/Controller/CAPI/WarrantyController.php index 7a92eaff..f6f23929 100644 --- a/src/Controller/CAPI/WarrantyController.php +++ b/src/Controller/CAPI/WarrantyController.php @@ -194,23 +194,52 @@ class WarrantyController extends APIController return new APIResponse(true, 'Warranty registered.', $data); } - public function claim(Request $req, EntityManagerInterface $em, $serial) + public function claim(Request $req, EntityManagerInterface $em, $id) { - $clean_serial = $this->cleanSerial($serial); - $warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $clean_serial]); + // required parameters + $params = [ + 'serial', + ]; + $msg = $this->checkRequiredParameters($req, $params); + if ($msg) + return new APIResponse(false, $msg); // no warranty + $warr = $em->getRepository(Warranty::class)->find($id); if ($warr == null) - return new APIResponse(false, 'No warranty found with that serial number.', null, 404); + return new APIResponse(false, 'No warranty found with that id.', null, 404); // warranty is not active if (!$warr->canClaim()) return new APIResponse(false, 'Warranty is not active.'); + + // check if new serial has been used + $serial = $req->request->get('serial'); + $clean_serial = $this->cleanSerial($serial); + $check_warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $clean_serial]); + if ($check_warr != null) + return new APIResponse(false, 'Serial for replacement has already been used.'); + // set status to claim $warr->setStatus(WarrantyStatus::CLAIMED) ->setDateClaim(new DateTime()); + // make replacement warranty + $new_warr = new Warranty(); + $new_warr->setSerial($clean_serial) + ->setWarrantyClass($warr->getWarrantyClass()) + ->setPlateNumber($warr->getPlateNumber()) + ->setBatteryModel($warr->getBatteryModel()) + ->setBatterySize($warr->getBatterySize()) + ->setDatePurchase($warr->getDatePurchase()) + ->setDateClaim(null) + ->setDateExpire($warr->getDateExpire()) + ->setClaimedFrom($warr); + + $em->persist($new_warr); + + $em->flush(); // TODO: claim log diff --git a/src/Entity/Warranty.php b/src/Entity/Warranty.php index 480e7251..ca565aa4 100644 --- a/src/Entity/Warranty.php +++ b/src/Entity/Warranty.php @@ -89,6 +89,13 @@ class Warranty */ protected $date_claim; + // claimed from + /** + * @ORM\OneToOne(targetEntity="Warranty") + * @ORM\JoinColumn(name="claim_id", referencedColumnName="id", nullable=true) + */ + protected $claim_from; + public function __construct() { $this->date_create = new DateTime(); @@ -250,8 +257,19 @@ class Warranty return true; if ($this->status == WarrantyStatus::CLAIMED) - return true; + return false; return false; } + + public function setClaimedFrom($claim_from) + { + $this->claim_form = $claim_from; + return $this; + } + + public function getClaimedFrom() + { + return $this->claim_from; + } }