Skip to content

Commit 83fe6d9

Browse files
author
Jose Ganora
committed
#WN-118# Fixed system of user logs to save the activty. Added fields on the user logs. Code status, language, region and event type.
1 parent 662826a commit 83fe6d9

File tree

5 files changed

+95
-40
lines changed

5 files changed

+95
-40
lines changed

app/Classes/Repositories/RegionRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function findBySlug($orgId, $slug)
3333
{
3434
$slug = strtolower($slug);
3535
$slug = str_replace(' ', '-', $slug);
36-
return $this->regModel->where('slug', $slug)->where('organisation_id', $orgId)->first();
36+
return $this->regModel->where('slug', $slug)->where('organisation_id', $orgId)->firstOrFail();;
3737
}
3838

3939
public function mapTranslationInput($regionId, $language_code, $data)

app/Http/Controllers/WhatNowController.php

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -179,37 +179,54 @@ public function deleteById($id)
179179
public function getFeed(WhatNowFeed $feed, $code)
180180
{
181181
try {
182-
$org = $this->orgRepo->findByCountryCode(strtoupper($code));
183-
} catch (\Exception $e) {
184-
Log::error('Organisation not found', ['message' => $e->getMessage()]);
185-
186-
return response(null, 404);
187-
}
188-
$feed->setOrganisation($org);
189-
190-
$regName = $this->request->query('region', null);
191-
if ($regName) {
192182
try {
193-
$reg = $this->regionRepo->findBySlug($org->id, $regName);
194-
$feed->setRegion($reg);
183+
$org = $this->orgRepo->findByCountryCode(strtoupper($code));
195184
} catch (\Exception $e) {
196-
Log::error('Region not found', ['message' => $e->getMessage()]);
185+
Log::error('Organisation not found', ['message' => $e->getMessage()]);
186+
$this->changeLogStatus(404);
187+
return response()->json(['message' => 'Organisation not found'], 404);
188+
}
189+
$feed->setOrganisation($org);
190+
191+
$regName = $this->request->query('region', null);
192+
if ($regName) {
193+
try {
194+
$reg = $this->regionRepo->findBySlug($org->id, $regName);
195+
$feed->setRegion($reg);
196+
} catch (\Exception $e) {
197+
Log::error('Region not found', ['message' => $e->getMessage()]);
198+
$this->changeLogStatus(404);
199+
return response()->json(['message' => 'Region not found'], 404);
200+
}
197201
}
198-
}
199202

200-
$langParam = $this->request->query('language', null);
201-
$langHeader = $this->request->header('Accept-Language', null);
203+
$langParam = $this->request->query('language', null);
204+
$langHeader = $this->request->header('Accept-Language', null);
202205

203-
if ($langParam) {
204-
$feed->setLanguage($langParam);
205-
} elseif ($langHeader) {
206-
$feed->setLanguage(locale_accept_from_http($langHeader));
207-
}
206+
if ($langParam) {
207+
$feed->setLanguage($langParam);
208+
} elseif ($langHeader) {
209+
$feed->setLanguage(locale_accept_from_http($langHeader));
210+
}
208211

209-
$feed->setEventTypeFilter($this->request->query('eventType', null));
210-
$feed->loadData();
212+
$feed->setEventTypeFilter($this->request->query('eventType', null));
213+
$feed->loadData();
214+
$data = $feed->getResponseData();
215+
if(empty($data)){
216+
$this->changeLogStatus(204);
217+
}
218+
return response()->json(['data' => $data]);
219+
}catch(\Exception $e){
220+
$this->changeLogStatus(500);
221+
return response()->json(['message' => $e], 500);
222+
}
223+
}
211224

212-
return response()->json(['data' => $feed->getResponseData()], 200);
225+
protected function changeLogStatus($status){
226+
if(isset($this->request->usageLog)){
227+
$this->request->usageLog->code_status = $status;
228+
$this->request->usageLog->save();
229+
}
213230
}
214231

215232
/**

app/Http/Middleware/ApiAuthMiddleware.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Middleware;
44

55
use App\Models\Application;
6+
use App\Models\UsageLog;
67
use Carbon\Carbon;
78
use Closure;
89
use Illuminate\Support\Facades\Log;
@@ -23,20 +24,17 @@ public function handle($request, Closure $next)
2324
if (! $application) {
2425
return parent::handle($request, $next);
2526
}
26-
27-
// Configure payload for logging API usage data
28-
$event = [
29-
'type' => 'APIRequestEvent',
30-
'payload' => [
31-
'app_id' => $application->id,
32-
'endpoint' => $request->path(),
33-
'method' => $request->method(),
34-
'timestamp' => Carbon::now()->toDateTimeString(),
35-
],
36-
];
37-
38-
// Log request to CloudWatch
39-
Log::channel('cloudwatch_access')->info(json_encode($event));
27+
$usageLog = new UsageLog;
28+
$usageLog->application_id = $application->id;
29+
$usageLog->method = $request->method();
30+
$usageLog->endpoint = $request->path();
31+
$usageLog->timestamp = Carbon::now()->toDateTimeString();
32+
$usageLog->code_status = 200;
33+
$usageLog->language = $request->input('language', false) ? $request->input('language', null) : $request->header('Accept-Language', null);
34+
$usageLog->region = $request->input('region', null);
35+
$usageLog->event_type = $request->input('eventType', null);
36+
$usageLog->save();
37+
$request->usageLog=$usageLog;
4038

4139
return $next($request);
4240
}

app/Models/UsageLog.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
class UsageLog extends Model
88
{
9-
protected $connection = 'stats_mysql'; // This model uses a different database connection for the stats DB
109

1110
protected $table = 'usage_logs';
11+
12+
public $timestamps = false;
13+
1214
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class AddCodeStatusOnUsageLogsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('usage_logs', function (Blueprint $table) {
17+
$table->integer('code_status')->nullable();
18+
$table->string('language',10)->nullable();
19+
$table->string('region',45)->nullable();
20+
$table->string('event_type',45)->nullable();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*
27+
* @return void
28+
*/
29+
public function down()
30+
{
31+
Schema::table('usage_logs', function (Blueprint $table) {
32+
$table->dropColumn('code_status');
33+
$table->dropColumn('language');
34+
$table->dropColumn('region');
35+
$table->dropColumn('event_type');
36+
});
37+
}
38+
}

0 commit comments

Comments
 (0)