diff --git a/app/Console/Commands/Optimise/Optimise.php b/app/Console/Commands/Optimise/Optimise.php index 3445d5c..1b20eab 100644 --- a/app/Console/Commands/Optimise/Optimise.php +++ b/app/Console/Commands/Optimise/Optimise.php @@ -159,8 +159,11 @@ public function optimise() $solver = $solver->solve(); $this->solver = $solver; }catch(OptimiseException $e) { + if(!$e->isEmpty()) + \Event::fire(new ErrorEvent($this->company, $e->getMessage())); throw $e; }catch (\Exception $e) { + //TODO use the correct exceptions to avoid to share private data \Event::fire(new ErrorEvent($this->company, $e->getMessage())); throw new OptimiseException('Optimising error', 0, $e); //TODO catch specif exception @@ -425,6 +428,7 @@ public function save() try { $this->saveMeetings($this->solver); $this->saveEmployeesMeetings($this->solver); + //TODO use the correct exceptions to avoid to share private data } catch (\Exception $e) { \Event::fire(new ErrorEvent($this->company, $e->getMessage())); throw new OptimiseException('Optimising error', 0, $e); diff --git a/app/Console/Commands/Optimise/OptimiseCommand.php b/app/Console/Commands/Optimise/OptimiseCommand.php index 1ba480b..4bbc548 100644 --- a/app/Console/Commands/Optimise/OptimiseCommand.php +++ b/app/Console/Commands/Optimise/OptimiseCommand.php @@ -74,13 +74,17 @@ private function makeForeground(Company $company) try { (new Optimise($company, $this->schedule, $this->laravel))->optimise()->save(); $this->info('Optimisation ' . $company->id . ' completed'); - }catch(OptimiseException $e){ - if($e->isEmpty()) - $this->warn('Company ' . $company->id . ' has no sufficient data'); - else - $this->error('Error during optimisation of company ' . $company->id .': '. $e->getMessage()); - }catch(\Exception $e) { - $this->error('Error during optimisation of company ' . $company->id .': '. $e->getMessage()); + }catch(OptimiseException $e) { + if ($e->isEmpty()) { + $mex = 'Company ' . $company->id . ' doesn\'t have sufficient data'; + $this->warn($mex); + \Log::info($mex); + } else { + $mex = 'Error during optimisation of company ' . $company->id . ': ' . $e->getMessage(); + $this->error($mex); + //TODO log cause, but don't send it to the user + //\Log::error($mex); //already logged in listener + } } } diff --git a/app/Console/Commands/SyncCaldav/Sync.php b/app/Console/Commands/SyncCaldav/Sync.php index ce6c953..b695349 100644 --- a/app/Console/Commands/SyncCaldav/Sync.php +++ b/app/Console/Commands/SyncCaldav/Sync.php @@ -112,7 +112,7 @@ private function getEvents() $calendars = $caldavClient->findCalendars(); if(!isset($calendars[$this->calendar->calendar_name])) throw new CaldavException("calendar inserted doesn't exist"); - $caldavClient->setCalendar($calendars[$this->calendar->calendar_name]);//TODO error if the calendar name is wrong + $caldavClient->setCalendar($calendars[$this->calendar->calendar_name]); /** * 26 hours before to avoid tiemezone problems and dst problems * 30 days after diff --git a/app/Console/Commands/SyncCaldav/SyncCaldav.php b/app/Console/Commands/SyncCaldav/SyncCaldav.php index 8fba14a..2d3826a 100644 --- a/app/Console/Commands/SyncCaldav/SyncCaldav.php +++ b/app/Console/Commands/SyncCaldav/SyncCaldav.php @@ -73,6 +73,7 @@ private function makeForeground(Caldav $calendar) { $this->info('Sync calendar ' . $calendar->calendar_id . ' started'); (new Sync($calendar))->sync(); + //TODO show errors as warning $this->info('Sync calendar ' . $calendar->calendar_id . ' completed'); } diff --git a/app/Listeners/Caldav/OkListener.php b/app/Listeners/Caldav/OkListener.php index 52ea2dd..f6b31a5 100644 --- a/app/Listeners/Caldav/OkListener.php +++ b/app/Listeners/Caldav/OkListener.php @@ -26,6 +26,7 @@ public function handle(OkEvent $event) { // $calendar = $event->getCalendar(); + \Log::info('caldav (calendar id = ' . $calendar->calendar_id . ') correctly synchronized'); //$calendar = $event->getCalendar()->fresh(); $calendar->sync_errors = ''; $calendar->save(); diff --git a/app/Listeners/Optimise/ErrorListener.php b/app/Listeners/Optimise/ErrorListener.php index ff5a8eb..400d02c 100644 --- a/app/Listeners/Optimise/ErrorListener.php +++ b/app/Listeners/Optimise/ErrorListener.php @@ -25,7 +25,7 @@ public function __construct() public function handle(ErrorEvent $event) { // - \Log::info('problems during optimise (company id = ' . $event->getCompany()->id . '): ' . $event->getError()); + \Log::error('problems during optimise (company id = ' . $event->getCompany()->id . '): ' . $event->getError()); $company = $event->getCompany(); //$company = $event->getCompany()->fresh(); self::sendEmail($company->email, $event->getError()); diff --git a/app/Listeners/Optimise/OkListener.php b/app/Listeners/Optimise/OkListener.php index 23ad20f..ac19357 100644 --- a/app/Listeners/Optimise/OkListener.php +++ b/app/Listeners/Optimise/OkListener.php @@ -30,7 +30,9 @@ public function handle(OkEvent $event) //send email to company self::sendCompanyEmail($company->email); //send emails to employees - $employees = $company->employees()->with('meetings')->get(); + $employees = $company->employees()->with(['meetings'=>function($query){ + $query->where('start_time', '>=', new \DateTime()); + }])->get(); foreach ($employees as $employee) self::sendEmployeeEmail($employee->email, $employee->meetings); } diff --git a/resources/views/emails/optimise/error.blade.php b/resources/views/emails/optimise/error.blade.php index 245b521..4b398e6 100644 --- a/resources/views/emails/optimise/error.blade.php +++ b/resources/views/emails/optimise/error.blade.php @@ -1,2 +1,3 @@ The following error occurred during the meeting optimisation:
-{{$error}} \ No newline at end of file +{{$error}} +if you need help don't hesitate to contact us \ No newline at end of file diff --git a/tests/ConsoleTests/Optimise/OptimiseCommandTest.php b/tests/ConsoleTests/Optimise/OptimiseCommandTest.php index 658fb51..8899b69 100644 --- a/tests/ConsoleTests/Optimise/OptimiseCommandTest.php +++ b/tests/ConsoleTests/Optimise/OptimiseCommandTest.php @@ -41,4 +41,14 @@ public function testSimpleModel() $this->assertEquals(1, $meetingShort->employees->count()); $this->assertEquals(2, $meetingLong->employees->count()); } + + public function testAll() + { + if (!$this->doConsole()) + return; + + //create data + $status = \Artisan::call('optimise:meetings'); + $this->assertEquals(0, $status); + } } diff --git a/tests/ConsoleTests/Optimise/OptimiseTest.php b/tests/ConsoleTests/Optimise/OptimiseTest.php index 4583d65..a42afe4 100644 --- a/tests/ConsoleTests/Optimise/OptimiseTest.php +++ b/tests/ConsoleTests/Optimise/OptimiseTest.php @@ -95,7 +95,14 @@ public function testNoData() return; $company = factory(\plunner\Company::class)->create(); $optimise = new Optimise($company, new Schedule(), \App::getInstance()); - $this->setExpectedException('\plunner\Console\Commands\Optimise\OptimiseException'); - $optimise->optimise(); + $exception = null; + try { + $optimise->optimise(); + }catch(\plunner\Console\Commands\Optimise\OptimiseException $e) + { + $exception = $e; + } + $this->assertNotNull($exception); + $this->assertTrue($exception->isEmpty()); } }