Merge branch '251-edit-warranty-via-api' into 'master'

Resolve "Edit warranty via api"

Closes #251

See merge request jankstudio/resq!296
This commit is contained in:
Korina Cordero 2019-08-27 04:41:31 +00:00
commit a93801d933
4 changed files with 112 additions and 4 deletions

View file

@ -65,6 +65,21 @@ class TestAPICommand extends Command
// warranty find
$api->get('/capi/warranties/' . $serial);
// warranty update
$id = 86811;
$params = [
'serial' => $serial,
'plate_number' => $plate_num,
'warranty_class' => 'private',
'sku' => 'WMEB24CB-CPN00-LX',
'date_purchase' => '20181001',
'date_expire' => '20191001',
'first_name' => 'First',
'last_name' => 'Last',
'mobile_number' => '123456789111',
];
$api->post('/capi/warranties/'. $id, $params);
// warranty claim
$id = 86811;
$serial = 'AJ34LJADR12134LKJL5';

View file

@ -12,6 +12,8 @@ access_keys:
label: Register Battery
- id: warranty.claim
label: Claim
- id: warranty.update
label: Update
- id: warranty.cancel
label: Cancel
- id: warranty.delete

View file

@ -84,6 +84,12 @@ capi_warranty_get_all:
controller: App\Controller\CAPI\WarrantyController::getAll
methods: [GET]
# edit warranty
capi_warranty_update:
path: /capi/warranties/{id}
controller: App\Controller\CAPI\WarrantyController::update
methods: [POST]
# cancel warranty
capi_warranty_cancel:
path: /capi/warranties/{id}/cancel
@ -96,8 +102,6 @@ capi_warranty_delete:
controller: App\Controller\CAPI\WarrantyController::delete
methods: [POST]
# customer vehicle api
# find customer vehicle by id

View file

@ -316,6 +316,95 @@ class WarrantyController extends APIController
return new APIResponse(true, 'Warranties loaded.', $data);
}
public function update(Request $req, EntityManagerInterface $em, $id)
{
$this->denyAccessUnlessGranted('warranty.update', null, 'No access.');
// find warranty
$warr = $em->getRepository(Warranty::class)->find($id);
if ($warr == null)
return new APIResponse(false, 'No warranty found with that id.', null, 404);
// required parameters
$params = [
'serial',
'warranty_class',
'plate_number',
'date_expire',
'date_purchase',
'sku',
];
$msg = $this->checkRequiredParameters($req, $params);
error_log('msg - ' . $msg);
if ($msg)
return new APIResponse(false, $msg);
// TODO: refactor this since this snippet is the same for register
$serial = $req->request->get('serial');
$date_expire_string = $req->request->get('date_expire');
$date_pur_string = $req->request->get('date_purchase');
$warr_class = $req->request->get('warranty_class');
$plate = $req->request->get('plate_number');
$sku = $req->request->get('sku');
$fname = $req->request->get('first_name', null);
$lname = $req->request->get('last_name', null);
$mnum = $req->request->get('mobile_number', null);
// wrong date expire format
$date_expire = DateTime::createFromFormat('Ymd', $date_expire_string);
if ($date_expire === false)
return new APIResponse(false, 'Wrong date format: date_expire.');
// wrong date purchase format
$date_pur = DateTime::createFromFormat('Ymd', $date_pur_string);
if ($date_pur === false)
return new APIResponse(false, 'Wrong date format: date_purchase.');
// valid warranty class
if (!WarrantyClass::validate($warr_class))
return new APIResponse(false, 'Invalid warranty class.');
// plate number
$plate = Warranty::cleanPlateNumber($plate);
if (!$plate)
return new APIResponse(false, 'Invalid plate number.');
// battery
$batt = $em->getRepository(SAPBattery::class)->find($sku);
if ($batt == null)
return new APIResponse(false, 'Invalid battery SKU.');
$warr->setSerial($serial)
->setWarrantyClass($warr_class)
->setPlateNumber($plate)
->setFirstName($fname)
->setLastName($lname)
->setMobileNumber($mnum)
->setSAPBattery($batt)
->setDatePurchase($date_pur)
->setDateClaim(null)
->setDateExpire($date_expire);
try
{
$em->persist($warr);
$em->flush();
}
catch (UniqueConstraintViolationException $e)
{
return new APIResponse(false, 'Duplicate serial encountered.');
}
// data
$data = [
'warranty' => $this->generateWarrantyData($warr),
];
return new APIResponse(true, 'Warranty updated.', $data);
}
public function cancel(Request $req, EntityManagerInterface $em, $id)
{
$this->denyAccessUnlessGranted('warranty.cancel', null, 'No access.');
@ -323,9 +412,7 @@ class WarrantyController extends APIController
// find warranty
$warr = $em->getRepository(Warranty::class)->find($id);
if ($warr == null)
{
return new APIResponse(false, 'No warranty found with that id.', null, 404);
}
if ($warr->getStatus() == WarrantyStatus::CANCELLED)
{