Add update warranty method to WarrantyController for third party. #251
This commit is contained in:
parent
74e1e329a7
commit
af60b0db71
4 changed files with 117 additions and 2 deletions
|
|
@ -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';
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@ access_keys:
|
|||
label: Register Battery
|
||||
- id: warranty.claim
|
||||
label: Claim
|
||||
- id: warranty.update
|
||||
label: Update
|
||||
- id: batterybrand
|
||||
label: Battery Brand Access
|
||||
acls:
|
||||
|
|
|
|||
|
|
@ -84,7 +84,11 @@ 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]
|
||||
|
||||
# customer vehicle api
|
||||
|
||||
|
|
|
|||
|
|
@ -60,7 +60,6 @@ class WarrantyController extends APIController
|
|||
'status' => (string) $warr->getStatus(),
|
||||
'date_create' => (string) $warr->getDateCreate()->format('YmdHis'),
|
||||
'date_purchase' => (string) $warr->getDatePurchase()->format('Ymd'),
|
||||
'date_expire' => (string) $warr->getDateExpire()->format('Ymd'),
|
||||
'flag_activated' => (boolean) $warr->isActivated(),
|
||||
];
|
||||
|
||||
|
|
@ -70,6 +69,12 @@ class WarrantyController extends APIController
|
|||
else
|
||||
$data['date_claim'] = (string) $warr->getDateClaim()->format('Ymd');
|
||||
|
||||
$date_expire = $warr->getDateExpire();
|
||||
if ($date_expire == null)
|
||||
$data['date_expire'] = null;
|
||||
else
|
||||
$data['date_expire'] = (string) $warr->getDateExpire()->format('Ymd');
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
|
@ -310,4 +315,93 @@ 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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue