Last active
June 21, 2017 16:54
-
-
Save atejandro/ca56bb045fc9c402f3ceae5b00e521b0 to your computer and use it in GitHub Desktop.
Gist for Rappi [Alejandro Ardila Schickler]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Specifies all the error types this particular service has. | |
abstract class ServerError | |
{ | |
const Undefined = 0; | |
const AlreadyExists = 1; | |
const InvalidArgument = 2; | |
const NotFound = 3; | |
} | |
//Shows the phone type. | |
abstract class Mobile | |
{ | |
const Android = 0; | |
const IPhone = 1; | |
} | |
//Shows in which status the delivery is. | |
abstract class DeliveryStatus | |
{ | |
const Open = 0; | |
const Ordered = 1; | |
const Assigned = 2; | |
//.... | |
//.... | |
const Delivered = 6; | |
} | |
//Specifies a delivery entity. | |
class Delivery { | |
// Private fields ... | |
public function has_assigned_driver(){ | |
return ($this->driver_id != NULL); | |
} | |
// other functions .... | |
} | |
//Holds all the push notifiaction logic | |
class Push { | |
//Private fields .... | |
//Factory method for generating push notifications depending on the mobile phone type. | |
public static function make($message, $phone, $uuid, $detail) { | |
if($phone == Mobile::Android){ | |
$this->android($uuid, $message, 1, 'default', 'Open', array($detail)); | |
} else if ($phone == Mobile::IPhone) { | |
$this->ios($uuid, $message, 1, 'honk.wav', 'Open', array($detail)); | |
} else { | |
throw new Exception('undefined cellphone'); | |
} | |
} | |
//other functions ... | |
} | |
//Confirms a delivery to the user using push notifications. | |
public function post_confirm() { | |
$delivery_id = Input::get('service_id'); | |
$driver_id = Input::get('driver_id'); | |
$delivery = Delivery::find($delivery_id); | |
if(!empty($delivery)) { | |
if($delivery->status_id == DeliveryStatus::Delivered){ | |
return error(ServerError::InvalidArgument); | |
} | |
if(!$delivery->has_assigned_driver() || $delivery->status_id == DeliveryStatus::Ordered){ | |
assign_driver_to_delivery($driver_id, $delivery_id); | |
send_push_notification($delivery); | |
} | |
} else { | |
return error(ServerError::NotFound); | |
} | |
} | |
//Assigns a driver to the ordered delivery | |
function assign_driver_to_delivery($driver_id, $delivery_id){ | |
$driver = Driver::find($driver_id); | |
$delivery = Delivery::update($delivery_id, array( | |
'driver_id' => $driver_id, | |
'status_id' => DeliveryStatus::Assigned, | |
'car_id' => $driver->car_id)); | |
Driver::update($driver_id, array('available' => false)); | |
} | |
//Returns a server response. | |
function error($serverError){ | |
//log error using framework's logger | |
return Response::json(array('error' => $serverError)) | |
} | |
//Sends a push notification to the client. | |
function send_push_notification($delivery) { | |
$pushMessage = 'Tu servicio ha sido confirmado!'; | |
$uuid = $delivery->user->uuid; | |
$deliveryId = $delivery->id; | |
if(empty($uuid)){ | |
return error(ServerError::Undefined); | |
} | |
try{ | |
Push::make($pushMessage, $delivery->user->type, $uuid, $deliveryId); | |
} catch(Exception $e){ | |
//log exception using framework's logger | |
return error(ServerError::Undefined); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment