From 472ba842ebe72ef2f5797dea397adab2a39f17e7 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Mon, 8 Aug 2016 17:45:37 +0300 Subject: [PATCH] Working on the bot --- app/Http/Controllers/BotController.php | 3 +- app/Http/Requests/EntityRequest.php | 5 +++ app/Http/routes.php | 4 +- app/Models/EntityModel.php | 4 +- app/Ninja/Intents/BaseIntent.php | 36 +++++++++++++--- app/Ninja/Intents/CreateInvoiceIntent.php | 7 +++- .../Intents/CreateInvoiceItemsIntent.php | 41 +++++++++++++++++++ app/Ninja/Repositories/ClientRepository.php | 4 +- app/Ninja/Transformers/InvoiceTransformer.php | 2 +- 9 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 app/Ninja/Intents/CreateInvoiceItemsIntent.php diff --git a/app/Http/Controllers/BotController.php b/app/Http/Controllers/BotController.php index 149268346728..b84527c63f95 100644 --- a/app/Http/Controllers/BotController.php +++ b/app/Http/Controllers/BotController.php @@ -11,7 +11,7 @@ class BotController extends Controller public function handleMessage($platform) { $to = '29:1C-OsU7OWBEDOYJhQUsDkYHmycOwOq9QOg5FVTwRX9ts'; - $message = 'invoice acme client for 2 tickets'; + $message = 'add 8 tickets'; //$message = view('bots.skype.message', ['message' => $message])->render(); //return $this->sendResponse($to, $message); @@ -93,6 +93,7 @@ class BotController extends Controller ]; $data = '{ eTag: "*", data: "' . addslashes(json_encode($data)) . '" }'; + //$data = '{ eTag: "*", data: "" }'; var_dump($data); $response = CurlUtils::post($url, $data, $headers); diff --git a/app/Http/Requests/EntityRequest.php b/app/Http/Requests/EntityRequest.php index b93aed3b495c..6bed19d44198 100644 --- a/app/Http/Requests/EntityRequest.php +++ b/app/Http/Requests/EntityRequest.php @@ -44,6 +44,11 @@ class EntityRequest extends Request { return $this->entity; } + public function setEntity($entity) + { + $this->entity = $entity; + } + public function authorize() { if ($this->entity()) { diff --git a/app/Http/routes.php b/app/Http/routes.php index 549cdbf3cd78..402d8dd4c33c 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -353,7 +353,7 @@ if (!defined('CONTACT_EMAIL')) { define('ENTITY_CONTACT', 'contact'); define('ENTITY_INVOICE', 'invoice'); define('ENTITY_DOCUMENT', 'document'); - define('ENTITY_INVOICE_ITEMS', 'invoice_items'); + define('ENTITY_INVOICE_ITEM', 'invoice_item'); define('ENTITY_INVITATION', 'invitation'); define('ENTITY_RECURRING_INVOICE', 'recurring_invoice'); define('ENTITY_PAYMENT', 'payment'); @@ -627,7 +627,7 @@ if (!defined('CONTACT_EMAIL')) { define('EMAIL_MARKUP_URL', env('EMAIL_MARKUP_URL', 'https://developers.google.com/gmail/markup')); define('OFX_HOME_URL', env('OFX_HOME_URL', 'http://www.ofxhome.com/index.php/home/directory/all')); define('GOOGLE_ANALYITCS_URL', env('GOOGLE_ANALYITCS_URL', 'https://www.google-analytics.com/collect')); - + define('MSBOT_LOGIN_URL', 'https://login.microsoftonline.com/common/oauth2/v2.0/token'); define('MSBOT_LUIS_URL', 'https://api.projectoxford.ai/luis/v1/application'); define('SKYPE_API_URL', 'https://apis.skype.com/v3'); diff --git a/app/Models/EntityModel.php b/app/Models/EntityModel.php index fb6dffd793af..04d0a003df6f 100644 --- a/app/Models/EntityModel.php +++ b/app/Models/EntityModel.php @@ -202,9 +202,10 @@ class EntityModel extends Eloquent * @param $entityType * @return bool|string */ - public static function validate($data, $entityType, $action = 'create') + public static function validate($data, $entityType, $entity = false) { // Use the API request if it exists + $action = $entity ? 'update' : 'create'; $requestClass = sprintf('App\\Http\\Requests\\%s%sAPIRequest', ucwords($action), ucwords($entityType)); if ( ! class_exists($requestClass)) { $requestClass = sprintf('App\\Http\\Requests\\%s%sRequest', ucwords($action), ucwords($entityType)); @@ -212,6 +213,7 @@ class EntityModel extends Eloquent $request = new $requestClass(); $request->setUserResolver(function() { return Auth::user(); }); + $request->setEntity($entity); $request->replace($data); if ( ! $request->authorize()) { diff --git a/app/Ninja/Intents/BaseIntent.php b/app/Ninja/Intents/BaseIntent.php index 46a7f56ad55f..7712692c72a1 100644 --- a/app/Ninja/Intents/BaseIntent.php +++ b/app/Ninja/Intents/BaseIntent.php @@ -38,15 +38,25 @@ class BaseIntent // do nothing by default } - public function addState($entities) + public function setState($entityType, $entities) { - var_dump($this->state); - if (isset($this->state->current)) { - $this->state->previous = $this->state->current; + $state = $this->state; + + if (isset($state->current->$entityType)) { + if ( ! isset($state->previous)) { + $state->previous = new stdClass; + } + + $state->previous->$entityType = $state->current->$entityType; } + if ( ! isset($state->current)) { + $state->current = new stdClass; + } - $this->state->current = $entities; + if ($entities) { + $state->current->$entityType = $entities; + } } public function getState() @@ -54,6 +64,22 @@ class BaseIntent return $this->state; } + public function getCurrentState($entityType = false, $first = false) + { + $current = $this->state->current; + $value = $entityType ? $current->$entityType : $current; + + if ($value) { + if ($first && count($value)) { + return $value[0]; + } else { + return $value; + } + } else { + return []; + } + } + protected function parseClient() { $clientRepo = app('App\Ninja\Repositories\ClientRepository'); diff --git a/app/Ninja/Intents/CreateInvoiceIntent.php b/app/Ninja/Intents/CreateInvoiceIntent.php index 603f60fe0d65..59ae3809db1c 100644 --- a/app/Ninja/Intents/CreateInvoiceIntent.php +++ b/app/Ninja/Intents/CreateInvoiceIntent.php @@ -31,8 +31,13 @@ class CreateInvoiceIntent extends BaseIntent } $invoice = $invoiceRepo->save($data); + $invoiceItemIds = array_map(function($item) { + return $item['public_id']; + }, $invoice->invoice_items->toArray()); - $this->addState([$invoice->entityKey()]); + $this->setState(ENTITY_CLIENT, [$client->public_id]); + $this->setState(ENTITY_INVOICE, [$invoice->public_id]); + $this->setState(ENTITY_INVOICE_ITEM, $invoiceItemIds); return view('bots.skype.invoice', [ 'invoice' => $invoice diff --git a/app/Ninja/Intents/CreateInvoiceItemsIntent.php b/app/Ninja/Intents/CreateInvoiceItemsIntent.php new file mode 100644 index 000000000000..2b97bcac1230 --- /dev/null +++ b/app/Ninja/Intents/CreateInvoiceItemsIntent.php @@ -0,0 +1,41 @@ +getCurrentState(ENTITY_INVOICE, true); + $invoice = Invoice::scope($invoiceId)->first(); + + $invoiceItems = $this->parseInvoiceItems(); + $data = [ + 'invoice_items' => $invoiceItems + ]; + + $valid = EntityModel::validate($data, ENTITY_INVOICE, $invoice); + + if ($valid !== true) { + return view('bots.skype.message', [ + 'message' => $valid + ])->render(); + } + + $invoice = $invoiceRepo->save($data, $invoice); + + $invoiceItems = array_slice($invoice->invoice_items->toArray(), count($invoiceItems) * -1); + $invoiceItemIds = array_map(function($item) { + return $item['public_id']; + }, $invoiceItems); + + $this->setState(ENTITY_INVOICE_ITEM, [$invoiceItemId]); + + return view('bots.skype.invoice', [ + 'invoice' => $invoice + ])->render(); + } +} diff --git a/app/Ninja/Repositories/ClientRepository.php b/app/Ninja/Repositories/ClientRepository.php index ae67f918ab7d..4b81a7f1ba2e 100644 --- a/app/Ninja/Repositories/ClientRepository.php +++ b/app/Ninja/Repositories/ClientRepository.php @@ -141,7 +141,7 @@ class ClientRepository extends BaseRepository $max = 0; $clientId = 0; - $clients = Client::scope()->get(['id', 'name']); + $clients = Client::scope()->get(['id', 'name', 'public_id']); foreach ($clients as $client) { if ( ! $client->name) { @@ -157,7 +157,7 @@ class ClientRepository extends BaseRepository } } - $contacts = Contact::scope()->get(['client_id', 'first_name', 'last_name']); + $contacts = Contact::scope()->get(['client_id', 'first_name', 'last_name', 'public_id']); foreach ($contacts as $contact) { if ( ! $contact->getFullName()) { diff --git a/app/Ninja/Transformers/InvoiceTransformer.php b/app/Ninja/Transformers/InvoiceTransformer.php index ab22fcb27773..62b997c15eba 100644 --- a/app/Ninja/Transformers/InvoiceTransformer.php +++ b/app/Ninja/Transformers/InvoiceTransformer.php @@ -40,7 +40,7 @@ class InvoiceTransformer extends EntityTransformer public function includeInvoiceItems(Invoice $invoice) { $transformer = new InvoiceItemTransformer($this->account, $this->serializer); - return $this->includeCollection($invoice->invoice_items, $transformer, ENTITY_INVOICE_ITEMS); + return $this->includeCollection($invoice->invoice_items, $transformer, ENTITY_INVOICE_ITEM); } public function includeInvitations(Invoice $invoice)