Lae/app/Jobs/Module/WorkOrder/WorkOrder.php

68 lines
1.9 KiB
PHP
Raw Normal View History

2022-08-19 15:27:57 +00:00
<?php
2022-11-16 02:29:50 +00:00
namespace App\Jobs\Module\WorkOrder;
2022-08-19 15:27:57 +00:00
2022-09-22 06:00:03 +00:00
use App\Events\UserEvent;
2022-11-06 11:28:22 +00:00
use App\Models\WorkOrder\WorkOrder as WorkOrderWorkOrder;
2022-08-19 15:27:57 +00:00
use Illuminate\Bus\Queueable;
2022-11-06 11:28:22 +00:00
use Illuminate\Contracts\Queue\ShouldQueue;
2022-08-19 15:27:57 +00:00
use Illuminate\Queue\InteractsWithQueue;
2022-11-06 11:28:22 +00:00
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
2022-09-08 16:12:02 +00:00
// use Illuminate\Contracts\Queue\ShouldBeUnique;
2022-08-19 15:27:57 +00:00
class WorkOrder implements ShouldQueue
{
2022-09-08 16:12:02 +00:00
use InteractsWithQueue, Queueable, SerializesModels;
2022-08-19 15:27:57 +00:00
protected $workOrder, $type;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(WorkOrderWorkOrder $workOrder, $type = 'post')
2022-08-19 15:27:57 +00:00
{
//
$this->workOrder = $workOrder;
$this->type = $type;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$this->workOrder->load(['module']);
2022-08-19 15:27:57 +00:00
if ($this->type == 'put') {
2022-11-30 07:57:31 +00:00
$response = $this->workOrder->module->http()->put('work-orders/' . $this->workOrder->id, $this->workOrder->toArray());
2022-11-19 03:06:02 +00:00
} else if ($this->type == 'delete') {
2022-11-30 07:57:31 +00:00
$response = $this->workOrder->module->http()->delete('work-orders/' . $this->workOrder->id);
2022-11-19 03:06:02 +00:00
if ($response->successful()) {
$this->workOrder->delete();
}
2022-08-19 15:27:57 +00:00
} else {
2022-11-30 07:57:31 +00:00
$response = $this->workOrder->module->http()->post('work-orders', $this->workOrder->toArray());
2022-08-19 15:27:57 +00:00
}
if (!$response->successful()) {
$this->workOrder->update([
'status' => 'error'
]);
2022-09-22 06:00:03 +00:00
} else {
2022-11-19 03:06:02 +00:00
if ($this->type == 'delete') {
broadcast(new UserEvent($this->workOrder->user_id, 'work-order.deleted', $this->workOrder));
} else {
broadcast(new UserEvent($this->workOrder->user_id, 'work-order.updated', $this->workOrder));
}
2022-08-19 15:27:57 +00:00
}
}
}