Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 56 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
JoshController | |
0.00% |
0 / 56 |
|
0.00% |
0 / 1 |
380 | |
0.00% |
0 / 1 |
getJoshConversions | |
0.00% |
0 / 56 |
|
0.00% |
0 / 1 |
380 |
1 | <?php |
2 | |
3 | namespace App\Http\Controllers\api; |
4 | |
5 | use DB; |
6 | use App\Http\Controllers\Controller; |
7 | use Illuminate\Http\Request; |
8 | use Illuminate\Http\JsonResponse; |
9 | use Illuminate\Database\Eloquent\Builder; |
10 | |
11 | use App\Models\Conversion; |
12 | use App\Models\ConversionNote; |
13 | use App\Models\ConversionDigest; |
14 | use App\Models\ConversionStatus; |
15 | use App\Models\GoogleAdsCampaign; |
16 | use App\Models\GoogleAdsCustomer; |
17 | use App\Models\ConversionCurrency; |
18 | use App\Models\ConversionTransaction; |
19 | |
20 | use App\Libs\SqlFormatter; |
21 | |
22 | use Carbon\Carbon; |
23 | |
24 | use App\Traits\GoogleTrait; |
25 | |
26 | |
27 | class JoshController extends BaseController |
28 | { |
29 | use GoogleTrait; |
30 | |
31 | /** |
32 | * Get conversions for josh |
33 | * |
34 | * |
35 | */ |
36 | public function getJoshConversions(Request $request):JsonResponse |
37 | { |
38 | /** |
39 | * Get pagination data from request |
40 | */ |
41 | $paginationDataResult = $this->__validateAndExtractPaginationData($request); |
42 | if (!is_array($paginationDataResult)) { |
43 | return $paginationDataResult; |
44 | } |
45 | $size = $paginationDataResult['size']; |
46 | |
47 | /** |
48 | * Get filters, order and sort data from request |
49 | */ |
50 | $order = in_array($request->query('order'), ['campaign', 'customer', 'gclid', 'fbclid']) ? $request->query('order') : 'customer'; |
51 | $sort = in_array(strtolower($request->query('sort')), ['asc', 'desc']) ? $request->query('sort') : 'desc'; |
52 | $status = $request->query('status', null); |
53 | $filter_campaign_id = $request->query('campaign_id', null); |
54 | $filter_customer_id = $request->query('customer_id', null); |
55 | |
56 | /** |
57 | * Function to yield start and end dates |
58 | * |
59 | * @param Request $request |
60 | * @return Generator |
61 | */ |
62 | $getStartEndDates = function (Request $request) { |
63 | // default start and end dates |
64 | $startDate = (string)Carbon::createFromFormat('Y-m-d', '1980-01-01')->format('Y-m-d').' 00:00:01'; |
65 | $endDate = (string)Carbon::now()->addDay()->format('Y-m-d').' 23:59:59'; |
66 | |
67 | // use provided dates if present and valid, otherwise use defaults |
68 | $start = $request->query('start', null); |
69 | $end = $request->query('end', null); |
70 | |
71 | // validate and assign start date if non null |
72 | if (!is_null($start) && $this->__validateYYYYMMDD($start)) { |
73 | $startDate = (string)Carbon::createFromFormat('Y-m-d', $start)->format('Y-m-d').' 00:00:01'; |
74 | } |
75 | |
76 | // validate and assign end date if non null |
77 | if (!is_null($end) && $this->__validateYYYYMMDD($end)) { |
78 | $endDate = (string)Carbon::createFromFormat('Y-m-d', $end)->format('Y-m-d').' 23:59:59'; |
79 | } |
80 | |
81 | yield $startDate; |
82 | yield $endDate; |
83 | }; // getStartEndDates |
84 | |
85 | /** |
86 | * Get start and end dates |
87 | */ |
88 | $startEndDate = $getStartEndDates($request); |
89 | $startDate = $startEndDate->current(); |
90 | $startEndDate->next(); |
91 | $endDate = $startEndDate->current(); |
92 | |
93 | /** |
94 | * Function to optionally filter query by status id from status name |
95 | * |
96 | * @param Builder $conversionsPage The builder to add the status filter to |
97 | * @param Request $request |
98 | * @return Builder |
99 | */ |
100 | $filterByStatus = function (Builder $conversionsPage, Request $request) { |
101 | try { |
102 | $statusId = ConversionStatus::where('name', '=', $request->query('status'))->first()->id; |
103 | return $statusId ? $conversionsPage->where('status_id', '=', $statusId) : $conversionsPage; |
104 | } catch (\Exception $e) { |
105 | return $conversionsPage; |
106 | } |
107 | }; //filterByStatus |
108 | |
109 | /** |
110 | * Base query |
111 | */ |
112 | $conversionsPage = ConversionDigest::leftJoin('google_ads_campaigns', 'google_ads_campaigns.campaign_id', '=', 'cmpid') |
113 | ->select( |
114 | 'conversions.id', |
115 | 'conversions.gclid', |
116 | 'conversions.fbclid', |
117 | 'conversions.website_url', |
118 | 'conversions.cmpid', |
119 | 'conversions.created_at', |
120 | 'conversions.status_id', |
121 | 'google_ads_customers.customer_id', |
122 | 'google_ads_customers.customer_descriptive_name', |
123 | 'google_ads_campaigns.campaign_id', |
124 | 'google_ads_campaigns.campaign_name' |
125 | ) |
126 | ->leftJoin('google_ads_customers', 'google_ads_customers.customer_id', '=', 'google_ads_campaigns.customer_id') |
127 | ->where('conversions.created_at', '>', $startDate) |
128 | ->where('conversions.created_at', '<', $endDate); |
129 | |
130 | /** |
131 | * Optional filters |
132 | */ |
133 | $conversionsPage = $filter_customer_id ? $conversionsPage->where('google_ads_customers.customer_id', '=', $filter_customer_id) : $conversionsPage; |
134 | $conversionsPage = $filter_campaign_id ? $conversionsPage->where('google_ads_campaigns.campaign_id', '=', $filter_campaign_id) : $conversionsPage; |
135 | $conversionsPage = $request->query('gclid') ? $conversionsPage->where('gclid', '=', $request->query('gclid')) : $conversionsPage; |
136 | $conversionsPage = $request->query('fbclid') ? $conversionsPage->where('fbclid', '=', $request->query('fbclid')) : $conversionsPage; |
137 | |
138 | /** |
139 | * Optional filter by status name |
140 | */ |
141 | $conversionsPage = $filterByStatus($conversionsPage, $request); |
142 | |
143 | /** |
144 | * Order and sort |
145 | */ |
146 | switch ($order) { |
147 | // sort by campaign name |
148 | case 'campaign': |
149 | $conversionsPage->orderBy('google_ads_campaigns.campaign_name', $sort); |
150 | break; |
151 | |
152 | // sort by customer name |
153 | case 'customer': |
154 | $conversionsPage->orderBy('google_ads_customers.customer_descriptive_name', $sort); |
155 | break; |
156 | |
157 | // sort by click ids |
158 | case 'gclid': |
159 | case 'fbclid': |
160 | $conversionsPage->orderBy($order, $sort); |
161 | break; |
162 | |
163 | // default sort is creation date descending |
164 | default: |
165 | $conversionsPage->orderBy('conversions.created_at', $sort); |
166 | break; |
167 | } |
168 | |
169 | /** |
170 | * Page and items |
171 | */ |
172 | $conversionsPage = $conversionsPage->paginate($size); |
173 | $conversionsItems = $conversionsPage->items(); |
174 | |
175 | /** |
176 | * Build pagination hateoas |
177 | */ |
178 | $paginationHateoas = $this->createPaginationFromOrm($conversionsPage); |
179 | |
180 | /** |
181 | * HTTP 200 |
182 | */ |
183 | return $this->sendResponse(200, $conversionsItems, $paginationHateoas); |
184 | |
185 | } // getJoshConversions |
186 | } |