Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReportingUserDetailsRequest
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace App\Actions;
4
5use App\DTO\ReportingUserDetailsRequestDTO;
6use App\Http\Models\Auth\User;
7use App\Http\Models\HubspotProperties;
8use App\Http\Resources\UserDetailsReportResource;
9use Carbon\Carbon;
10use MongoDB\BSON\UTCDateTime;
11
12class ReportingUserDetailsRequest
13{
14    /**
15     * Execute the action to get user details.
16     *
17     * @return array
18     */
19    public function execute(ReportingUserDetailsRequestDTO $filter)
20    {
21        $start_date = $filter->fromDate;
22        $end_date = $filter->toDate;
23
24        // Parse dates or set defaults
25        if ($start_date && $end_date) {
26            $start_date = Carbon::parse($start_date);
27            $end_date = Carbon::parse($end_date);
28        } else {
29            $start_date = Carbon::now()->subMonths(12)->startOfMonth();
30            $end_date = Carbon::now()->endOfMonth();
31        }
32
33        $start_date = Carbon::now()->subMonths(12)->startOfMonth();
34        $end_date = Carbon::now()->endOfMonth();
35
36        // Convert start_date and end_date to UTCDateTime once to reuse
37        $start = new UTCDateTime($start_date->getTimestamp() * 1000);
38        $end = new UTCDateTime($end_date->getTimestamp() * 1000);
39
40        // Fetch users with trashed records included
41        $users = User::withTrashed()
42            ->select([
43                'id',
44                'first_name',
45                'last_name',
46                'company_group_id',
47                'company_id',
48                'deleted_at',
49            ])
50            ->whereBetween('created_at', [$start, $end])
51            ->get();
52
53        $ids = $users->pluck('id')->toArray();
54
55        // Fetch properties within the date range using the previously fetched IDs
56        $properties = HubspotProperties::whereIn('flymsg_id', $ids)
57            ->whereBetween('created_at', [$start, $end])
58            ->get();
59
60        // Convert to resource collection
61        return UserDetailsReportResource::collection($properties);
62    }
63}