diff --git a/app/Http/Controllers/SystemLogController.php b/app/Http/Controllers/SystemLogController.php index ae4e83ae461b..1b7a0b5e6ff9 100644 --- a/app/Http/Controllers/SystemLogController.php +++ b/app/Http/Controllers/SystemLogController.php @@ -8,7 +8,7 @@ use App\Transformers\SystemLogTransformer; use App\Utils\Traits\MakesHash; use Illuminate\Http\Request; -class SystemLogController extends Controller +class SystemLogController extends BaseController { use MakesHash; diff --git a/app/Models/SystemLog.php b/app/Models/SystemLog.php index 490a4c104b17..25d082952709 100644 --- a/app/Models/SystemLog.php +++ b/app/Models/SystemLog.php @@ -11,10 +11,26 @@ namespace App\Models; +use App\Models\Filterable; +use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\SoftDeletes; class SystemLog extends Model { + use Filterable; + use SoftDeletes; + use MakesHash; + + protected $casts = [ + 'updated_at' => 'timestamp', + 'created_at' => 'timestamp', + 'deleted_at' => 'timestamp', + 'log' => 'array' + ]; + + protected $dateFormat = 'Y-m-d H:i:s.u'; + /* Category IDs */ const CATEGORY_GATEWAY_RESPONSE = 1; const CATEGORY_MAIL = 2; @@ -51,10 +67,6 @@ class SystemLog extends Model 'type_id', ]; - protected $casts = [ - 'log' => 'array' - ]; - public function resolveRouteBinding($value) { if (is_numeric($value)) { @@ -64,4 +76,14 @@ class SystemLog extends Model return $this ->where('id', $this->decodePrimaryKey($value))->firstOrFail(); } + + /* + V2 type of scope + */ + public function scopeCompany($query) + { + $query->where('company_id', auth()->user()->companyId()); + + return $query; + } } diff --git a/database/migrations/2020_08_18_140557_add_is_public_to_documents_table.php b/database/migrations/2020_08_18_140557_add_is_public_to_documents_table.php index 1079ab6c688b..784db3ae356f 100644 --- a/database/migrations/2020_08_18_140557_add_is_public_to_documents_table.php +++ b/database/migrations/2020_08_18_140557_add_is_public_to_documents_table.php @@ -31,6 +31,11 @@ class AddIsPublicToDocumentsTable extends Migration $table->text('meta')->nullable(); }); + Schema::table('system_logs', function (Blueprint $table) { + $table->softDeletes('deleted_at', 6); + }); + + } /** diff --git a/tests/Feature/SystemLogApiTest.php b/tests/Feature/SystemLogApiTest.php new file mode 100644 index 000000000000..4eeada3ed084 --- /dev/null +++ b/tests/Feature/SystemLogApiTest.php @@ -0,0 +1,127 @@ +makeTestData(); + + } + + + public function testSystemLogRoutes() + { + + $sl = [ + 'client_id' => $this->client->id, + 'company_id' => $this->company->id, + 'user_id' => $this->client->user_id, + 'log' => 'thelog', + 'category_id' => 1, + 'event_id' => 1, + 'type_id' => 1, + ]; + + SystemLog::create($sl); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token + ])->get('/api/v1/system_logs'); + + + $response->assertStatus(200); + + $arr = $response->json(); + + $this->assertTrue(count($arr['data']) >=1); + + $hashed_id = $arr['data'][0]['id']; + + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token + ])->get('/api/v1/system_logs/' . $hashed_id); + + $response->assertStatus(200); + + $arr = $response->json(); + + $this->assertEquals($hashed_id, $arr['data']['id']); + + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token + ])->put('/api/v1/system_logs/' . $hashed_id, $sl)->assertStatus(400); + + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token + ])->delete('/api/v1/system_logs/' . $hashed_id)->assertStatus(400); + } + + public function testStoreRouteFails() + { + + $sl = [ + 'client_id' => $this->client->id, + 'company_id' => $this->company->id, + 'user_id' => $this->client->user_id, + 'log' => 'thelog', + 'category_id' => 1, + 'event_id' => 1, + 'type_id' => 1, + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token + ])->post('/api/v1/system_logs', $sl)->assertStatus(400); + + } + + public function testCreateRouteFails() + { + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token + ])->get('/api/v1/system_logs/create')->assertStatus(400); + + } + +}