| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Agent; |
| 6 |
use FluentSupport\App\Models\AgentGroup; |
| 7 |
use FluentSupport\App\Models\TagPivot; |
| 8 |
use FluentSupport\Framework\Http\Request\Request; |
| 9 |
|
| 10 |
class AgentGroupController extends Controller |
| 11 |
{ |
| 12 |
public function index(Request $request) |
| 13 |
{ |
| 14 |
$search = $request->getSafe('search', 'sanitize_text_field'); |
| 15 |
|
| 16 |
$groups = AgentGroup::orderBy('id', 'DESC') |
| 17 |
->searchBy($search) |
| 18 |
->paginate(); |
| 19 |
|
| 20 |
$groupIds = $groups->pluck('id')->toArray(); |
| 21 |
|
| 22 |
$agentCounts = TagPivot::where('source_type', 'agent_group') |
| 23 |
->whereIn('tag_id', $groupIds) |
| 24 |
->selectRaw('tag_id, COUNT(*) as agents_count') |
| 25 |
->groupBy('tag_id') |
| 26 |
->pluck('agents_count', 'tag_id'); |
| 27 |
|
| 28 |
foreach ($groups as $group) { |
| 29 |
$group->agents_count = $agentCounts[$group->id] ?? 0; |
| 30 |
} |
| 31 |
|
| 32 |
return [ |
| 33 |
'groups' => $groups |
| 34 |
]; |
| 35 |
} |
| 36 |
|
| 37 |
public function get($group_id) |
| 38 |
{ |
| 39 |
$group = AgentGroup::findOrFail($group_id); |
| 40 |
$group->agent_ids = TagPivot::where('tag_id', $group->id) |
| 41 |
->where('source_type', 'agent_group') |
| 42 |
->pluck('source_id') |
| 43 |
->toArray(); |
| 44 |
|
| 45 |
$agents = Agent::select(['id', 'first_name', 'last_name']) |
| 46 |
->where('person_type', 'agent') |
| 47 |
->get(); |
| 48 |
|
| 49 |
foreach ($agents as $agent) { |
| 50 |
$agent->id = strval($agent->id); |
| 51 |
} |
| 52 |
|
| 53 |
return [ |
| 54 |
'group' => $group, |
| 55 |
'agents' => $agents |
| 56 |
]; |
| 57 |
} |
| 58 |
|
| 59 |
public function create(Request $request) |
| 60 |
{ |
| 61 |
$this->validate($request->all(), [ |
| 62 |
'title' => 'required|string' |
| 63 |
]); |
| 64 |
|
| 65 |
$data = $this->sanitizeGroupPayload($request); |
| 66 |
|
| 67 |
$settings = []; |
| 68 |
if ($data['is_default']) { |
| 69 |
$this->clearDefaultGroup(); |
| 70 |
$settings['is_default'] = true; |
| 71 |
} |
| 72 |
|
| 73 |
$group = AgentGroup::create([ |
| 74 |
'title' => $data['title'], |
| 75 |
'description' => $data['description'], |
| 76 |
'settings' => $settings, |
| 77 |
]); |
| 78 |
|
| 79 |
if (!empty($data['agent_ids'])) { |
| 80 |
$this->syncAgents($group, $data['agent_ids']); |
| 81 |
} |
| 82 |
|
| 83 |
return [ |
| 84 |
'message' => __('Agent group has been created', 'fluent-support'), |
| 85 |
'group' => $group |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
public function update(Request $request, $group_id) |
| 90 |
{ |
| 91 |
$this->validate($request->all(), [ |
| 92 |
'title' => 'required|string' |
| 93 |
]); |
| 94 |
|
| 95 |
$data = $this->sanitizeGroupPayload($request); |
| 96 |
$group = AgentGroup::findOrFail($group_id); |
| 97 |
|
| 98 |
$settings = $group->settings ?: []; |
| 99 |
if ($data['is_default']) { |
| 100 |
$this->clearDefaultGroup($group->id); |
| 101 |
$settings['is_default'] = true; |
| 102 |
} else { |
| 103 |
$settings['is_default'] = false; |
| 104 |
} |
| 105 |
|
| 106 |
$group->title = $data['title']; |
| 107 |
$group->description = $data['description']; |
| 108 |
$group->settings = $settings; |
| 109 |
$group->save(); |
| 110 |
|
| 111 |
if ($request->has('agent_ids')) { |
| 112 |
$this->syncAgents($group, $data['agent_ids']); |
| 113 |
} |
| 114 |
|
| 115 |
return [ |
| 116 |
'message' => __('Agent group has been updated', 'fluent-support'), |
| 117 |
'group' => $group |
| 118 |
]; |
| 119 |
} |
| 120 |
|
| 121 |
public function delete(Request $request, $group_id) |
| 122 |
{ |
| 123 |
$group = AgentGroup::findOrFail($group_id); |
| 124 |
|
| 125 |
$fallbackGroupId = intval($request->get('fallback_group_id')); |
| 126 |
|
| 127 |
if (!$fallbackGroupId || $fallbackGroupId === intval($group_id)) { |
| 128 |
return $this->sendError([ |
| 129 |
'message' => __('A valid fallback group is required', 'fluent-support') |
| 130 |
], 422); |
| 131 |
} |
| 132 |
|
| 133 |
$fallbackGroup = AgentGroup::find($fallbackGroupId); |
| 134 |
|
| 135 |
if (!$fallbackGroup) { |
| 136 |
return $this->sendError([ |
| 137 |
'message' => __('The selected fallback group does not exist', 'fluent-support') |
| 138 |
], 422); |
| 139 |
} |
| 140 |
|
| 141 |
// Get agent IDs from the group being deleted |
| 142 |
$agentIds = TagPivot::where('tag_id', $group_id) |
| 143 |
->where('source_type', 'agent_group') |
| 144 |
->pluck('source_id') |
| 145 |
->toArray(); |
| 146 |
|
| 147 |
// Get agent IDs already in the fallback group |
| 148 |
$existingAgentIds = TagPivot::where('tag_id', $fallbackGroupId) |
| 149 |
->where('source_type', 'agent_group') |
| 150 |
->pluck('source_id') |
| 151 |
->toArray(); |
| 152 |
|
| 153 |
// Move agents that aren't already in the fallback group |
| 154 |
$newAgentIds = array_diff($agentIds, $existingAgentIds); |
| 155 |
|
| 156 |
foreach ($newAgentIds as $agentId) { |
| 157 |
TagPivot::create([ |
| 158 |
'tag_id' => $fallbackGroupId, |
| 159 |
'source_id' => $agentId, |
| 160 |
'source_type' => 'agent_group', |
| 161 |
]); |
| 162 |
} |
| 163 |
|
| 164 |
// Delete old pivot rows and the group |
| 165 |
TagPivot::where('tag_id', $group_id) |
| 166 |
->where('source_type', 'agent_group') |
| 167 |
->delete(); |
| 168 |
|
| 169 |
$group->delete(); |
| 170 |
|
| 171 |
return [ |
| 172 |
'message' => __('Agent group has been deleted and agents have been reassigned', 'fluent-support') |
| 173 |
]; |
| 174 |
} |
| 175 |
|
| 176 |
private function sanitizeGroupPayload(Request $request) |
| 177 |
{ |
| 178 |
return [ |
| 179 |
'title' => $request->getSafe('title', 'sanitize_text_field'), |
| 180 |
'description' => $request->getSafe('description', 'sanitize_textarea_field'), |
| 181 |
'is_default' => rest_sanitize_boolean($request->get('is_default', false)), |
| 182 |
'agent_ids' => is_array($request->get('agent_ids')) ? array_map('intval', $request->get('agent_ids')) : [], |
| 183 |
]; |
| 184 |
} |
| 185 |
|
| 186 |
private function syncAgents($group, $agentIds) |
| 187 |
{ |
| 188 |
TagPivot::where('tag_id', $group->id) |
| 189 |
->where('source_type', 'agent_group') |
| 190 |
->delete(); |
| 191 |
|
| 192 |
$agentIds = array_map('intval', array_filter((array) $agentIds)); |
| 193 |
|
| 194 |
foreach ($agentIds as $agentId) { |
| 195 |
TagPivot::create([ |
| 196 |
'tag_id' => $group->id, |
| 197 |
'source_id' => $agentId, |
| 198 |
'source_type' => 'agent_group', |
| 199 |
]); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
private function clearDefaultGroup($exceptGroupId = null) |
| 204 |
{ |
| 205 |
$groups = AgentGroup::whereNotNull('settings')->get()->filter(function ($group) use ($exceptGroupId) { |
| 206 |
if ($exceptGroupId && $group->id == $exceptGroupId) { |
| 207 |
return false; |
| 208 |
} |
| 209 |
return !empty($group->settings['is_default']); |
| 210 |
}); |
| 211 |
|
| 212 |
foreach ($groups as $group) { |
| 213 |
$settings = $group->settings; |
| 214 |
$settings['is_default'] = false; |
| 215 |
$group->settings = $settings; |
| 216 |
$group->save(); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|