Refactor a bit

This commit is contained in:
Matthew Shillam 2018-10-24 17:50:09 +01:00
parent 2f4e89944c
commit f7d3d225e4
8 changed files with 90 additions and 33 deletions

1
.gitignore vendored
View File

@ -13,3 +13,4 @@ yarn-error.log
.env .env
.phpunit.result.cache .phpunit.result.cache
.DS_Store .DS_Store
public/get.php

View File

@ -8,6 +8,7 @@ use Illuminate\Http\Request;
use App\Mail\BackupWarning; use App\Mail\BackupWarning;
use App\Mail\BackupSuccess; use App\Mail\BackupSuccess;
use App\Mail\BackupError; use App\Mail\BackupError;
use App\Mail\BackupDebug;
use Carbon\Carbon; use Carbon\Carbon;
use App\Schedule; use App\Schedule;
use App\Backup; use App\Backup;
@ -16,18 +17,6 @@ use App\Client;
class BackupController extends Controller class BackupController extends Controller
{ {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function test()
{
$created_at = Backup::all()-> pluck('created_at');
return $created_at;
}
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
* *
@ -36,9 +25,18 @@ class BackupController extends Controller
*/ */
public function store($client_id, $schedule_name, Request $request) public function store($client_id, $schedule_name, Request $request)
{ {
$client = Client::where('id', '=', $client_id)->first();
$current_time = Carbon::now()->toDateTimeString(); $current_time = Carbon::now()->toDateTimeString();
$response = json_decode( file_get_contents('php://input') ); $response = json_decode( file_get_contents('php://input') );
$data = $response->Data; if( isset($response->Data->ParsedResult)){
$data = $response->Data;
}
else{
Mail::to('matthew@shillam.me.uk')
->send(new BackupDebug($client, $response));
return response()->json(['error' => 'A Problem with the data provided from Duplicati!'], 403);
}
$schedule = Schedule::updateOrCreate( $schedule = Schedule::updateOrCreate(
['client_id' => $client_id, 'name' => $schedule_name], ['client_id' => $client_id, 'name' => $schedule_name],
@ -77,30 +75,28 @@ class BackupController extends Controller
'version' => $data->Version 'version' => $data->Version
)); ));
// notify client by email. /**
$client = Client::where('id', '=', $client_id)->first(); * notify client by email
*/
if($backup->status == "Success") if($backup->status == "Success")
{ {
Mail::to($client->email) Mail::to($client->email)
->cc(['matthew@shillam.me.uk','j.kendrick@dentalsupportuk.com']) ->send(new BackupSuccess($backup, $client));
->send(new BackupSuccess($backup));
} }
elseif($backup->status == "Warning") elseif($backup->status == "Warning")
{ {
Mail::to($client->email) Mail::to($client->email)
->cc(['matthew@shillam.me.uk','j.kendrick@dentalsupportuk.com']) ->send(new BackupWarning($backup, $client));
->send(new BackupWarning($backup));
} }
elseif($backup->status == "Error") elseif($backup->status == "Error")
{ {
Mail::to($client->email) Mail::to($client->email)
->cc(['matthew@shillam.me.uk','j.kendrick@dentalsupportuk.com']) ->send(new BackupError($backup, $client));
->send(new BackupError($backup));
} }
else{ else{
Mail::to('matthew@shillam.me.uk') Mail::to('matthew@shillam.me.uk')
->send(new BackupError($backup)); ->send(new BackupDebug($client, $response));
return response()->json(['error' => 'A Problem with the data provided from Duplicati!'], 403);
} }
} }

View File

@ -17,8 +17,8 @@ class ScheduleResource extends JsonResource
// return parent::toArray($request); // return parent::toArray($request);
return [ return [
'id' => $this->id, 'id' => $this->id,
'name' => $this->client_name, 'name' => $this->name,
'last_backup_status' => $this->access_key, 'last_backup_status' => $this->last_backup_status,
'client' => $this->client->client_name, 'client' => $this->client->client_name,
'client_id' => $this->client->id, 'client_id' => $this->client->id,
]; ];

36
app/Mail/BackupDebug.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace App\Mail;
use App\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class BackupDebug extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($client, $response)
{
$this->client = $client;
$this->response = $response;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('reports@mydentalbackup.co.uk')
->subject('** MAILING-PROBLEM ** for : ' . $this->client->client_name)
->markdown('mail.backup.debug', ['response' => $this->response, 'client' => $this->client]);
}
}

View File

@ -17,9 +17,10 @@ class BackupError extends Mailable
* *
* @return void * @return void
*/ */
public function __construct(Backup $backup) public function __construct(Backup $backup, $client)
{ {
$this->backup = $backup; $this->backup = $backup;
$this->client = $client;
} }
/** /**
@ -30,7 +31,8 @@ class BackupError extends Mailable
public function build() public function build()
{ {
return $this->from('reports@mydentalbackup.co.uk') return $this->from('reports@mydentalbackup.co.uk')
->subject('ERROR Your backup ( ' . $this->backup->schedule_name . ' ) has an error!') ->cc('reports@mydentalbackup.co.uk')
->subject('Backup ( ' . $this->backup->schedule_name . ' ) Error! for : ' . $this->client->client_name)
->markdown('mail.backup.error', ['backup' => $this->backup]); ->markdown('mail.backup.error', ['backup' => $this->backup]);
} }
} }

View File

@ -17,9 +17,10 @@ class BackupSuccess extends Mailable
* *
* @return void * @return void
*/ */
public function __construct(Backup $backup) public function __construct(Backup $backup, $client)
{ {
$this->backup = $backup; $this->backup = $backup;
$this->client = $client;
} }
/** /**
@ -30,7 +31,8 @@ class BackupSuccess extends Mailable
public function build() public function build()
{ {
return $this->from('reports@mydentalbackup.co.uk') return $this->from('reports@mydentalbackup.co.uk')
->subject('SUCCESS Your backup ( ' . $this->backup->schedule_name . ' ) completed successfully') ->cc('reports@mydentalbackup.co.uk')
->subject('Backup ( ' . $this->backup->schedule_name . ' ) Success! for : ' . $this->client->client_name . '')
->markdown('mail.backup.success', ['backup' => $this->backup]); ->markdown('mail.backup.success', ['backup' => $this->backup]);
} }
} }

View File

@ -17,9 +17,10 @@ class BackupWarning extends Mailable
* *
* @return void * @return void
*/ */
public function __construct(Backup $backup) public function __construct(Backup $backup, $client)
{ {
$this->backup = $backup; $this->backup = $backup;
$this->client = $client;
} }
/** /**
@ -30,7 +31,8 @@ class BackupWarning extends Mailable
public function build() public function build()
{ {
return $this->from('reports@mydentalbackup.co.uk') return $this->from('reports@mydentalbackup.co.uk')
->subject('WARNING Your backup ( ' . $this->backup->schedule_name . ' ) has warnings') ->cc('reports@mydentalbackup.co.uk')
->subject('Backup ( ' . $this->backup->schedule_name . ' ) Warning! for : ' . $this->client->client_name . '')
->markdown('mail.backup.warning', ['backup' => $this->backup]); ->markdown('mail.backup.warning', ['backup' => $this->backup]);
} }
} }

View File

@ -0,0 +1,18 @@
@component('mail::message')
<h4 class="error">Error!</h4>
@component('mail::panel')
No mail was sent to users for ( {{$client->client_name}} ) has some errors! See log below...
@endcomponent
<div class="dark-box">
<h1 style="text-align:center;font-weight:normal;color:white;">Full variable data logs..</h1>
</div>
@component('mail::panel')
<?php var_dump($response); ?>
@endcomponent
Regards,<br>
{{ config('app.name') }} - auto notification bot.
@endcomponent