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.

67 lines
2.5 KiB

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Business;
  4. use App\Models\Workflow;
  5. use Illuminate\Http\Request;
  6. class WorkflowController extends Controller
  7. {
  8. public function store($business, Request $request)
  9. {
  10. permit('businessWorkFlows');
  11. Workflow::create($request->merge(['business_id' => $business])->except('_business_info'));
  12. // $statuses = collect($validatedData['statuses'])->map(function($status) use ($workflow, $business) {
  13. // $status['business_id'] = $business;
  14. // $status['workflow_id'] = $workflow->id;
  15. // return $status;
  16. // });
  17. // $statuses = $workflow->statuses()->createMany($statuses->toArray());
  18. return Business::info($business, true);
  19. }
  20. public function update($business, $workflow, Request $request)
  21. {
  22. permit('businessWorkFlows');
  23. $workflowModel = Workflow::findOrFail($workflow);
  24. $workflowModel->update($request->except('_business_info'));
  25. return Business::info($business, true);
  26. }
  27. public function syncStatus($business, $workflowModel)
  28. {
  29. $old_statuses_name = array_keys(collect(\request('_business_info')['workflows'][$workflowModel->id]['statuses'])->keyBy('name')->toArray());
  30. $new_statuses_name = array_keys(collect(\request('statuses'))->keyBy('name')->toArray());
  31. $removed_statuses_name = array_diff(array_merge($old_statuses_name, $new_statuses_name), $new_statuses_name);
  32. foreach ($removed_statuses_name as $status_name) {
  33. //delete all statuses that removed name's from request->statuses
  34. $workflowModel->statuses()->where('name', $status_name)->first()->delete();
  35. }
  36. foreach (request('statuses') as $status) {
  37. //sync another statuses
  38. $workflowModel->statuses()
  39. ->updateOrCreate(
  40. ['name' => $status['name'], 'business_id' => $business, 'workflow_id' => $workflowModel->id],
  41. ['state' => $status['state'], 'order' => $status['order']]
  42. );
  43. }
  44. return $workflowModel;
  45. }
  46. public function delete($business, $workflow)
  47. {
  48. permit('businessWorkFlows');
  49. $workflow = Workflow::findOrFail($workflow);
  50. foreach ($workflow->statuses as $status) {
  51. //delete all statuses related to this workflow
  52. $status->delete();
  53. }
  54. $workflow->delete();
  55. return Business::info($business, true);
  56. }
  57. }