62 lines
2.2 KiB
PHP
62 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1\Me;
|
|
|
|
use App\Http\Controllers\Api\V1\ApiController;
|
|
use App\Models\CollectionLog;
|
|
use App\Models\Household;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class MyCollectionsController extends ApiController
|
|
{
|
|
/**
|
|
* Resident's QR collection history. Filters by date range.
|
|
*/
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'from' => ['nullable', 'date'],
|
|
'to' => ['nullable', 'date'],
|
|
'per_page' => ['nullable', 'integer', 'min:1', 'max:100'],
|
|
]);
|
|
|
|
$household = Household::where('head_user_id', $request->user()->id)->first();
|
|
|
|
$perPage = (int) ($data['per_page'] ?? 25);
|
|
|
|
$logs = CollectionLog::query()
|
|
->with(['qrCode:id,serial', 'dropOffPoint:id,name,uuid'])
|
|
->where(function ($query) use ($household, $request) {
|
|
if ($household) {
|
|
$query->where('household_id', $household->id);
|
|
}
|
|
$query->orWhereHas('qrCode', function ($q) use ($request) {
|
|
$q->where('assigned_to_user_id', $request->user()->id);
|
|
});
|
|
})
|
|
->where('verification_status', CollectionLog::STATUS_VALID)
|
|
->when($data['from'] ?? null, fn ($q, $from) => $q->where('scanned_at', '>=', Carbon::parse($from)->startOfDay()))
|
|
->when($data['to'] ?? null, fn ($q, $to) => $q->where('scanned_at', '<=', Carbon::parse($to)->endOfDay()))
|
|
->orderByDesc('scanned_at')
|
|
->paginate($perPage);
|
|
|
|
$items = $logs->getCollection()->map(fn (CollectionLog $l) => [
|
|
'id' => $l->id,
|
|
'serial' => $l->qrCode?->serial,
|
|
'scanned_at' => $l->scanned_at?->toIso8601String(),
|
|
'weight_kg' => $l->weight_kg,
|
|
'waste_type' => $l->waste_type,
|
|
'drop_off_point' => $l->dropOffPoint?->name,
|
|
])->all();
|
|
|
|
return $this->ok($items, null, [
|
|
'page' => $logs->currentPage(),
|
|
'per_page' => $logs->perPage(),
|
|
'total' => $logs->total(),
|
|
'last_page' => $logs->lastPage(),
|
|
]);
|
|
}
|
|
}
|