1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

Add activities to API

This commit is contained in:
David Bomba 2019-09-27 14:31:13 +10:00
parent 1e025dcbe1
commit 441d52523d
9 changed files with 170 additions and 1 deletions

View File

@ -0,0 +1,47 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Controllers;
use App\Models\Activity;
use App\Transformers\ActivityTransformer;
use Illuminate\Http\Request;
class ActivityController extends BaseController
{
protected $entity_type = Activity::class;
protected $entity_transformer = ActivityTransformer::class;
public function __construct()
{
parent::__construct();
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$activities = Activity::whereCompanyId(auth()->user()->company()->id)
->orderBy('created_at', 'DESC')
->take(50);
return $this->listResponse($activities);
}
}

View File

@ -11,12 +11,13 @@
namespace App\Http\Controllers;
use App\Models\User;
use App\Transformers\ArraySerializer;
use App\Transformers\EntityTransformer;
use App\Utils\Statics;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Request as Input;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request as Input;
use League\Fractal\Manager;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection;
@ -145,6 +146,16 @@ class BaseController extends Controller
$query->with($includes);
if (auth()->user()->cannot('view_'.$this->entity_type))
{
if ($this->entity_type == User::class) {
$query->where('id', '=', auth()->user()->id);
} else {
$query->where('user_id', '=', auth()->user()->id);
}
}
$data = $this->createCollection($query, $transformer, $this->entity_type);
return $this->response($data);

View File

@ -0,0 +1,30 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Requests\Activity;
use App\Http\Requests\Request;
use App\Models\Activity;
class ShowActivityRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize() : bool
{
return auth()->user()->can('view', Activity::class);
}
}

View File

@ -130,4 +130,8 @@ class Activity extends StaticModel
return $this->belongsTo(Expense::class)->withTrashed();
}
public function company()
{
return $this->belongsTo(Company::class)->withTrashed();
}
}

View File

@ -22,6 +22,8 @@ class StaticModel extends Model
'deleted_at' => 'timestamp',
];
protected $dateFormat = 'Y-m-d H:i:s.u';
public function getIdAttribute()
{
return (string)$this->attributes['id'];

View File

@ -0,0 +1,24 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Policies;
use App\Models\User;
/**
* Class ActivityPolicy
* @package App\Policies
*/
class ActivityPolicy extends EntityPolicy
{
}

View File

@ -11,6 +11,7 @@
namespace App\Providers;
use App\Models\Activity;
use App\Models\Client;
use App\Models\Company;
use App\Models\Invoice;
@ -20,6 +21,7 @@ use App\Models\Quote;
use App\Models\RecurringInvoice;
use App\Models\RecurringQuote;
use App\Models\User;
use App\Policies\ActivityPolicy;
use App\Policies\ClientPolicy;
use App\Policies\CompanyPolicy;
use App\Policies\InvoicePolicy;
@ -41,6 +43,7 @@ class AuthServiceProvider extends ServiceProvider
* @var array
*/
protected $policies = [
Activity::class => ActivityPolicy::class,
Client::class => ClientPolicy::class,
Company::class => CompanyPolicy::class,
Product::class => ProductPolicy::class,

View File

@ -0,0 +1,46 @@
<?php
namespace App\Transformers;
use App\Models\Activity;
use App\Utils\Traits\MakesHash;
class ActivityTransformer extends EntityTransformer
{
use MakesHash;
protected $defaultIncludes = [];
/**
* @var array
*/
protected $availableIncludes = [];
/**
* @param Activity $activity
*
* @return array
*/
public function transform(Activity $activity)
{
return [
'id' => (string) $this->encodePrimaryKey($activity->id),
'activity_type_id' => (string) $activity->activity_type_id,
'client_id' => $activity->client ? (string) $activity->client->id : '',
'company_id' => $activity->company ? (string) $activity->company->id : '',
'user_id' => (string) $activity->user_id,
'invoice_id' => $activity->invoice ? (string) $activity->invoice->id : '',
'payment_id' => $activity->payment ? (string) $activity->payment->id : '',
'credit_id' => $activity->credit ? (string) $activity->credit->id : '',
'updated_at' => $activity->updated_at,
'expense_id' => $activity->expense_id ? (string) $activity->expense->id : '',
'is_system' => (bool) $activity->is_system,
'contact_id' => $activity->contact_id ? (string) $activity->contact->id : '',
'task_id' => $activity->task_id ? (string) $activity->task->id : '',
'notes' => $activity->notes ? (string) $activity->notes : '',
'ip' => (string) $activity->ip,
];
}
}

View File

@ -34,6 +34,8 @@ Route::group(['api_secret_check','domain_db'], function () {
Route::group(['middleware' => ['api_db','api_secret_check','token_auth'], 'prefix' =>'api/v1', 'as' => 'api.'], function () {
Route::resource('activities', 'ActivityController'); // name = (clients. index / create / show / update / destroy / edit
Route::resource('clients', 'ClientController'); // name = (clients. index / create / show / update / destroy / edit
Route::post('clients/bulk', 'ClientController@bulk')->name('clients.bulk');