diff --git a/.gitignore b/.gitignore index 0151b51..ccdd96f 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ yarn-error.log .env .phpunit.result.cache .DS_Store +public/get.php diff --git a/app/Http/Controllers/API/v1/BackupController.php b/app/Http/Controllers/API/v1/BackupController.php index a15fc1c..e64495e 100644 --- a/app/Http/Controllers/API/v1/BackupController.php +++ b/app/Http/Controllers/API/v1/BackupController.php @@ -8,25 +8,14 @@ use Illuminate\Http\Request; use App\Mail\BackupWarning; use App\Mail\BackupSuccess; use App\Mail\BackupError; +use App\Mail\BackupDebug; use Carbon\Carbon; use App\Schedule; use App\Backup; use App\Client; 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. @@ -36,9 +25,18 @@ class BackupController extends Controller */ public function store($client_id, $schedule_name, Request $request) { + $client = Client::where('id', '=', $client_id)->first(); $current_time = Carbon::now()->toDateTimeString(); + $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( ['client_id' => $client_id, 'name' => $schedule_name], @@ -77,30 +75,28 @@ class BackupController extends Controller 'version' => $data->Version )); - // notify client by email. - $client = Client::where('id', '=', $client_id)->first(); - + /** + * notify client by email + */ if($backup->status == "Success") { - Mail::to($client->email) - ->cc(['matthew@shillam.me.uk','j.kendrick@dentalsupportuk.com']) - ->send(new BackupSuccess($backup)); + Mail::to($client->email) + ->send(new BackupSuccess($backup, $client)); } elseif($backup->status == "Warning") { Mail::to($client->email) - ->cc(['matthew@shillam.me.uk','j.kendrick@dentalsupportuk.com']) - ->send(new BackupWarning($backup)); + ->send(new BackupWarning($backup, $client)); } elseif($backup->status == "Error") { Mail::to($client->email) - ->cc(['matthew@shillam.me.uk','j.kendrick@dentalsupportuk.com']) - ->send(new BackupError($backup)); + ->send(new BackupError($backup, $client)); } else{ 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); } } diff --git a/app/Http/Resources/ScheduleResource.php b/app/Http/Resources/ScheduleResource.php index 1d45d87..2c5b9f4 100644 --- a/app/Http/Resources/ScheduleResource.php +++ b/app/Http/Resources/ScheduleResource.php @@ -17,8 +17,8 @@ class ScheduleResource extends JsonResource // return parent::toArray($request); return [ 'id' => $this->id, - 'name' => $this->client_name, - 'last_backup_status' => $this->access_key, + 'name' => $this->name, + 'last_backup_status' => $this->last_backup_status, 'client' => $this->client->client_name, 'client_id' => $this->client->id, ]; diff --git a/app/Mail/BackupDebug.php b/app/Mail/BackupDebug.php new file mode 100644 index 0000000..7619ac6 --- /dev/null +++ b/app/Mail/BackupDebug.php @@ -0,0 +1,36 @@ +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]); + } +} diff --git a/app/Mail/BackupError.php b/app/Mail/BackupError.php index 23d135b..981960c 100644 --- a/app/Mail/BackupError.php +++ b/app/Mail/BackupError.php @@ -17,9 +17,10 @@ class BackupError extends Mailable * * @return void */ - public function __construct(Backup $backup) + public function __construct(Backup $backup, $client) { $this->backup = $backup; + $this->client = $client; } /** @@ -30,7 +31,8 @@ class BackupError extends Mailable public function build() { 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]); } } diff --git a/app/Mail/BackupSuccess.php b/app/Mail/BackupSuccess.php index 0102f49..6f9c065 100644 --- a/app/Mail/BackupSuccess.php +++ b/app/Mail/BackupSuccess.php @@ -17,9 +17,10 @@ class BackupSuccess extends Mailable * * @return void */ - public function __construct(Backup $backup) + public function __construct(Backup $backup, $client) { $this->backup = $backup; + $this->client = $client; } /** @@ -30,7 +31,8 @@ class BackupSuccess extends Mailable public function build() { 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]); } } diff --git a/app/Mail/BackupWarning.php b/app/Mail/BackupWarning.php index fff1767..9e5c1a1 100644 --- a/app/Mail/BackupWarning.php +++ b/app/Mail/BackupWarning.php @@ -17,9 +17,10 @@ class BackupWarning extends Mailable * * @return void */ - public function __construct(Backup $backup) + public function __construct(Backup $backup, $client) { $this->backup = $backup; + $this->client = $client; } /** @@ -30,7 +31,8 @@ class BackupWarning extends Mailable public function build() { 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]); } } diff --git a/resources/views/mail/backup/debug.blade.php b/resources/views/mail/backup/debug.blade.php new file mode 100644 index 0000000..2cf0e3d --- /dev/null +++ b/resources/views/mail/backup/debug.blade.php @@ -0,0 +1,18 @@ +@component('mail::message') +