You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

71 lines
2.5 KiB

<?php
namespace App\Http\Controllers;
use App\Business;
use App\Status;
use App\Workflow;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class WorkflowController extends Controller
{
public function store($business, Request $request)
{
permit('businessWorkFlows');
Workflow::create($request->merge(['business_id' => $business])->except('_business_info'));
// $statuses = collect($validatedData['statuses'])->map(function($status) use ($workflow, $business) {
// $status['business_id'] = $business;
// $status['workflow_id'] = $workflow->id;
// return $status;
// });
// $statuses = $workflow->statuses()->createMany($statuses->toArray());
return Business::info($business, true);
}
public function update($business, $workflow, Request $request)
{
permit('businessWorkFlows');
$workflowModel = Workflow::findOrFail($workflow);
$workflowModel->update($request->except('_business_info'));
return Business::info($business, true);
}
public function syncStatus($business, $workflowModel)
{
$old_statuses_name = array_keys(collect(\request('_business_info')['workflows'][$workflowModel->id]['statuses'])->keyBy('name')->toArray());
$new_statuses_name = array_keys(collect(\request('statuses'))->keyBy('name')->toArray());
$removed_statuses_name = array_diff(array_merge($old_statuses_name, $new_statuses_name), $new_statuses_name);
foreach ($removed_statuses_name as $status_name) {
//delete all statuses that removed name's from request->statuses
$workflowModel->statuses()->where('name', $status_name)->first()->delete();
}
foreach (request('statuses') as $status) {
//sync another statuses
$workflowModel->statuses()
->updateOrCreate(
['name' => $status['name'], 'business_id' => $business, 'workflow_id' => $workflowModel->id],
['state' => $status['state'], 'order' => $status['order']]
);
}
return $workflowModel;
}
public function delete($business, $workflow)
{
permit('businessWorkFlows');
$workflow = Workflow::findOrFail($workflow);
foreach ($workflow->statuses as $status) {
//delete all statuses related to this workflow
$status->delete();
}
$workflow->delete();
return Business::info($business, true);
}
}