diff --git a/app/Http/Controllers/Api/V1/Me/MyCollectionsController.php b/app/Http/Controllers/Api/V1/Me/MyCollectionsController.php index fc9b961..b1788b3 100644 --- a/app/Http/Controllers/Api/V1/Me/MyCollectionsController.php +++ b/app/Http/Controllers/Api/V1/Me/MyCollectionsController.php @@ -23,15 +23,19 @@ class MyCollectionsController extends ApiController ]); $household = Household::where('head_user_id', $request->user()->id)->first(); - if (! $household) { - return $this->ok([], 'No household yet'); - } $perPage = (int) ($data['per_page'] ?? 25); $logs = CollectionLog::query() ->with(['qrCode:id,serial', 'dropOffPoint:id,name,uuid']) - ->where('household_id', $household->id) + ->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())) diff --git a/app/Notifications/WasteCollected.php b/app/Notifications/WasteCollected.php new file mode 100644 index 0000000..4b500bf --- /dev/null +++ b/app/Notifications/WasteCollected.php @@ -0,0 +1,57 @@ +log->scanned_at?->format('h:i A') ?? now()->format('h:i A'); + $wasteTypeStr = $this->log->waste_type ? strtolower($this->log->waste_type) . ' ' : ''; + + return [ + 'type' => 'waste.collected', + 'collection_log_id' => $this->log->id, + 'message' => "Your {$wasteTypeStr}waste was successfully collected at {$time}.", + 'action_label' => 'View History', + 'action_url' => '/collections', + ]; + } + + public function toSms(mixed $notifiable): array + { + $time = $this->log->scanned_at?->format('h:i A') ?? now()->format('h:i A'); + $wasteTypeStr = $this->log->waste_type ? strtolower($this->log->waste_type) . ' ' : ''; + + return [ + 'to' => $notifiable->phone, + 'message' => "Verde: Your {$wasteTypeStr}waste was successfully collected at {$time}.", + ]; + } + + public function toPush(mixed $notifiable): array + { + $time = $this->log->scanned_at?->format('h:i A') ?? now()->format('h:i A'); + $wasteTypeStr = $this->log->waste_type ? strtolower($this->log->waste_type) . ' ' : ''; + + return [ + 'title' => 'Waste Collected', + 'body' => "Your {$wasteTypeStr}waste was successfully collected at {$time}.", + 'data' => [ + 'type' => 'waste.collected', + 'collection_log_id' => (string) $this->log->id, + ], + ]; + } +} diff --git a/app/Services/Scan/ScanService.php b/app/Services/Scan/ScanService.php index 1d58268..37086f1 100644 --- a/app/Services/Scan/ScanService.php +++ b/app/Services/Scan/ScanService.php @@ -123,6 +123,15 @@ class ScanService $this->qrAllocator->notifyBalanceIfLow($code->household); } + // Notify the resident about the collection + $notifiable = $code->household?->head ?? $code->user; + if ($notifiable) { + \Illuminate\Support\Facades\Notification::send( + $notifiable, + new \App\Notifications\WasteCollected($log) + ); + } + return ScanResult::accepted($code->fresh(), $log); }); }