Implement household, QR sale, team helpers assignment, and helper endpoints
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Http\Requests\Team\StoreCollectionTeamRequest;
|
||||
use App\Http\Resources\CollectionTeamResource;
|
||||
use App\Models\CollectionTeam;
|
||||
use App\Models\TeamMember;
|
||||
use App\Models\User;
|
||||
use App\Services\Team\TeamConflictDetector;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -49,9 +50,20 @@ class AdminTeamController extends ApiController
|
||||
public function store(StoreCollectionTeamRequest $request): JsonResponse
|
||||
{
|
||||
$data = $request->validated();
|
||||
$helperIds = $data['helper_ids'] ?? [];
|
||||
$override = (bool) ($data['override_conflicts'] ?? false);
|
||||
unset($data['helper_ids'], $data['override_conflicts']);
|
||||
|
||||
if (! isset($data['helper_count']) && isset($data['helper_ids'])) {
|
||||
$data['helper_count'] = count($data['helper_ids']);
|
||||
$data['helper_assignment_mode'] = 'manual';
|
||||
}
|
||||
|
||||
$helperIds = $this->resolveHelpers($data);
|
||||
$expectedCount = (int) ($data['helper_count'] ?? 0);
|
||||
if ($expectedCount > 0 && count($helperIds) < $expectedCount) {
|
||||
return $this->fail('Not enough available helpers found.', [
|
||||
'helper_count' => ['Only ' . count($helperIds) . ' available helper(s) found.']
|
||||
], 422);
|
||||
}
|
||||
|
||||
if (! $override) {
|
||||
$conflicts = $this->conflicts->check(
|
||||
@@ -65,6 +77,8 @@ class AdminTeamController extends ApiController
|
||||
}
|
||||
}
|
||||
|
||||
unset($data['helper_ids'], $data['override_conflicts'], $data['helper_count'], $data['helper_assignment_mode']);
|
||||
|
||||
$team = DB::transaction(function () use ($data, $helperIds) {
|
||||
$team = CollectionTeam::create($data);
|
||||
|
||||
@@ -99,16 +113,29 @@ class AdminTeamController extends ApiController
|
||||
public function update(StoreCollectionTeamRequest $request, CollectionTeam $team): JsonResponse
|
||||
{
|
||||
$data = $request->validated();
|
||||
$helperIds = $data['helper_ids'] ?? null;
|
||||
$override = (bool) ($data['override_conflicts'] ?? false);
|
||||
unset($data['helper_ids'], $data['override_conflicts']);
|
||||
|
||||
$helperIds = null;
|
||||
if (array_key_exists('helper_count', $data) || array_key_exists('helper_ids', $data)) {
|
||||
if (! isset($data['helper_count']) && isset($data['helper_ids'])) {
|
||||
$data['helper_count'] = count($data['helper_ids']);
|
||||
$data['helper_assignment_mode'] = 'manual';
|
||||
}
|
||||
$helperIds = $this->resolveHelpers($data, $team->id);
|
||||
$expectedCount = (int) ($data['helper_count'] ?? 0);
|
||||
if ($expectedCount > 0 && count($helperIds) < $expectedCount) {
|
||||
return $this->fail('Not enough available helpers found.', [
|
||||
'helper_count' => ['Only ' . count($helperIds) . ' available helper(s) found.']
|
||||
], 422);
|
||||
}
|
||||
}
|
||||
|
||||
if (! $override) {
|
||||
$conflicts = $this->conflicts->check(
|
||||
$data['driver_id'] ?? $team->driver_id,
|
||||
$data['scanner_id'] ?? $team->scanner_id,
|
||||
$data['truck_id'] ?? $team->truck_id,
|
||||
$helperIds ?? [],
|
||||
$helperIds ?? $team->helpers()->pluck('user_id')->toArray(),
|
||||
$team->id,
|
||||
);
|
||||
if (! empty($conflicts)) {
|
||||
@@ -116,6 +143,8 @@ class AdminTeamController extends ApiController
|
||||
}
|
||||
}
|
||||
|
||||
unset($data['helper_ids'], $data['override_conflicts'], $data['helper_count'], $data['helper_assignment_mode']);
|
||||
|
||||
DB::transaction(function () use ($team, $data, $helperIds) {
|
||||
$team->update($data);
|
||||
|
||||
@@ -141,6 +170,40 @@ class AdminTeamController extends ApiController
|
||||
);
|
||||
}
|
||||
|
||||
protected function resolveHelpers(array $data, ?int $excludeTeamId = null): array
|
||||
{
|
||||
$mode = $data['helper_assignment_mode'] ?? 'manual';
|
||||
$count = (int) ($data['helper_count'] ?? 0);
|
||||
|
||||
if ($count <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($mode === 'manual') {
|
||||
return $data['helper_ids'] ?? [];
|
||||
}
|
||||
|
||||
$override = (bool) ($data['override_conflicts'] ?? false);
|
||||
$query = User::where('role', User::ROLE_HELPER)->where('status', User::STATUS_ACTIVE);
|
||||
|
||||
if (! $override) {
|
||||
$today = now()->toDateString();
|
||||
$assignedHelperIds = TeamMember::query()
|
||||
->where('role_in_team', TeamMember::ROLE_HELPER)
|
||||
->whereDate('assigned_from', '<=', $today)
|
||||
->where(function ($q) use ($today) {
|
||||
$q->whereNull('assigned_until')->orWhereDate('assigned_until', '>=', $today);
|
||||
})
|
||||
->when($excludeTeamId, fn ($q, $id) => $q->where('team_id', '!=', $id))
|
||||
->pluck('user_id')
|
||||
->toArray();
|
||||
|
||||
$query->whereNotIn('id', $assignedHelperIds);
|
||||
}
|
||||
|
||||
return $query->inRandomOrder()->take($count)->pluck('id')->toArray();
|
||||
}
|
||||
|
||||
public function destroy(CollectionTeam $team): JsonResponse
|
||||
{
|
||||
$team->delete();
|
||||
|
||||
Reference in New Issue
Block a user