diff --git a/app/Http/Controllers/ExpenseCategoryController.php b/app/Http/Controllers/ExpenseCategoryController.php new file mode 100644 index 000000000000..d054a66f17f1 --- /dev/null +++ b/app/Http/Controllers/ExpenseCategoryController.php @@ -0,0 +1,435 @@ +base_repo = $base_repo; + } + + /** + * @OA\Get( + * path="/api/v1/expense_categories", + * operationId="getExpenseCategorys", + * tags={"expense_categories"}, + * summary="Gets a list of expense_categories", + * description="Lists tax rates", + * @OA\Parameter(ref="#/components/parameters/index"), + * @OA\Response( + * response=200, + * description="A list of expense_categories", + * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), + * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), + * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), + * @OA\JsonContent(ref="#/components/schemas/ExpenseCategory"), + * ), + * @OA\Response( + * response=422, + * description="Validation error", + * @OA\JsonContent(ref="#/components/schemas/ValidationError"), + + * ), + * @OA\Response( + * response="default", + * description="Unexpected Error", + * @OA\JsonContent(ref="#/components/schemas/Error"), + * ), + * ) + * + * + * Display a listing of the resource. + * + * @return \Illuminate\Http\Response + */ + public function index() + { + $expense_categories = ExpenseCategory::scope(); + + return $this->listResponse($expense_categories); + } + + /** + * Show the form for creating a new resource. + * + * @return \Illuminate\Http\Response + * + * + * + * @OA\Get( + * path="/api/v1/expense_categories/create", + * operationId="getExpenseCategoryCreate", + * tags={"expense_categories"}, + * summary="Gets a new blank Expens Category object", + * description="Returns a blank object with default values", + * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), + * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), + * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), + * @OA\Response( + * response=200, + * description="A blank Expens Category object", + * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), + * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), + * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), + * @OA\JsonContent(ref="#/components/schemas/ExpenseCategory"), + * ), + * @OA\Response( + * response=422, + * description="Validation error", + * @OA\JsonContent(ref="#/components/schemas/ValidationError"), + * + * ), + * @OA\Response( + * response="default", + * description="Unexpected Error", + * @OA\JsonContent(ref="#/components/schemas/Error"), + * ), + * ) + */ + public function create(CreateExpenseCategoryRequest $request) + { + $tax_rate = ExpenseCategoryFactory::create(auth()->user()->company()->id, auth()->user()->id); + + return $this->itemResponse($tax_rate); + } + + /** + * Store a newly created resource in storage. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\Response + */ + public function store(StoreExpenseCategoryRequest $request) + { + $tax_rate = ExpenseCategoryFactory::create(auth()->user()->company()->id, auth()->user()->id); + $tax_rate->fill($request->all()); + $tax_rate->save(); + + return $this->itemResponse($tax_rate); + } + + /** + * Display the specified resource. + * + * @param int $id + * @return \Illuminate\Http\Response + * + * + * @OA\Get( + * path="/api/v1/expense_categories/{id}", + * operationId="showExpenseCategory", + * tags={"expense_categories"}, + * summary="Shows a Expens Category", + * description="Displays an ExpenseCategory by id", + * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), + * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), + * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), + * @OA\Parameter( + * name="id", + * in="path", + * description="The ExpenseCategory Hashed ID", + * example="D2J234DFA", + * required=true, + * @OA\Schema( + * type="string", + * format="string", + * ), + * ), + * @OA\Response( + * response=200, + * description="Returns the Expens Category object", + * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), + * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), + * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), + * @OA\JsonContent(ref="#/components/schemas/ExpenseCategory"), + * ), + * @OA\Response( + * response=422, + * description="Validation error", + * @OA\JsonContent(ref="#/components/schemas/ValidationError"), + * + * ), + * @OA\Response( + * response="default", + * description="Unexpected Error", + * @OA\JsonContent(ref="#/components/schemas/Error"), + * ), + * ) + */ + public function show(ShowExpenseCategoryRequest $request, ExpenseCategory $tax_rate) + { + return $this->itemResponse($tax_rate); + } + + /** + * Show the form for editing the specified resource. + * + * @param int $id + * @return \Illuminate\Http\Response + * + * + * @OA\Get( + * path="/api/v1/expense_categories/{id}/edit", + * operationId="editExpenseCategory", + * tags={"expense_categories"}, + * summary="Shows a Expens Category for editting", + * description="Displays a Expens Category by id", + * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), + * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), + * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), + * @OA\Parameter( + * name="id", + * in="path", + * description="The ExpenseCategory Hashed ID", + * example="D2J234DFA", + * required=true, + * @OA\Schema( + * type="string", + * format="string", + * ), + * ), + * @OA\Response( + * response=200, + * description="Returns the Expens Category object", + * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), + * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), + * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), + * @OA\JsonContent(ref="#/components/schemas/ExpenseCategory"), + * ), + * @OA\Response( + * response=422, + * description="Validation error", + * @OA\JsonContent(ref="#/components/schemas/ValidationError"), + * + * ), + * @OA\Response( + * response="default", + * description="Unexpected Error", + * @OA\JsonContent(ref="#/components/schemas/Error"), + * ), + * ) + */ + public function edit(EditExpenseCategoryRequest $request, ExpenseCategory $tax_rate) + { + return $this->itemResponse($tax_rate); + } + + /** + * Update the specified resource in storage. + * + * @param \Illuminate\Http\Request $request + * @param App\Models\Client $client + * @return \Illuminate\Http\Response + * + * + * + * @OA\Put( + * path="/api/v1/expense_categories/{id}", + * operationId="updateExpenseCategory", + * tags={"expense_categories"}, + * summary="Updates a tax rate", + * description="Handles the updating of a tax rate by id", + * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), + * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), + * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), + * @OA\Parameter( + * name="id", + * in="path", + * description="The ExpenseCategory Hashed ID", + * example="D2J234DFA", + * required=true, + * @OA\Schema( + * type="string", + * format="string", + * ), + * ), + * @OA\Response( + * response=200, + * description="Returns the ExpenseCategory object", + * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), + * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), + * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), + * @OA\JsonContent(ref="#/components/schemas/ExpenseCategory"), + * ), + * @OA\Response( + * response=422, + * description="Validation error", + * @OA\JsonContent(ref="#/components/schemas/ValidationError"), + * + * ), + * @OA\Response( + * response="default", + * description="Unexpected Error", + * @OA\JsonContent(ref="#/components/schemas/Error"), + * ), + * ) + */ + public function update(UpdateExpenseCategoryRequest $request, ExpenseCategory $tax_rate) + { + $tax_rate->fill($request->all()); + $tax_rate->save(); + + return $this->itemResponse($tax_rate); + } + + /** + * Remove the specified resource from storage. + * + * @param int $id + * @return \Illuminate\Http\Response + * + * + * @OA\Delete( + * path="/api/v1/expense_categories/{id}", + * operationId="deleteExpenseCategory", + * tags={"expense_categories"}, + * summary="Deletes a ExpenseCategory", + * description="Handles the deletion of an ExpenseCategory by id", + * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), + * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), + * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), + * @OA\Parameter( + * name="id", + * in="path", + * description="The ExpenseCategory Hashed ID", + * example="D2J234DFA", + * required=true, + * @OA\Schema( + * type="string", + * format="string", + * ), + * ), + * @OA\Response( + * response=200, + * description="Returns a HTTP status", + * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), + * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), + * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), + * ), + * @OA\Response( + * response=422, + * description="Validation error", + * @OA\JsonContent(ref="#/components/schemas/ValidationError"), + * + * ), + * @OA\Response( + * response="default", + * description="Unexpected Error", + * @OA\JsonContent(ref="#/components/schemas/Error"), + * ), + * ) + */ + public function destroy(DestroyExpenseCategoryRequest $request, ExpenseCategory $tax_rate) + { + $tax_rate->is_deleted = true; + $tax_rate->save(); + $tax_rate->delete(); + + return $this->itemResponse($tax_rate); + } + + /** + * Perform bulk actions on the list view. + * + * @param BulkExpenseCategoryRequest $request + * @return \Illuminate\Http\Response + * + * + * @OA\Post( + * path="/api/v1/expense_categories/bulk", + * operationId="bulkExpenseCategorys", + * tags={"expense_categories"}, + * summary="Performs bulk actions on an array of ExpenseCategorys", + * description="", + * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), + * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), + * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), + * @OA\Parameter(ref="#/components/parameters/index"), + * @OA\RequestBody( + * description="Expens Categorys", + * required=true, + * @OA\MediaType( + * mediaType="application/json", + * @OA\Schema( + * type="array", + * @OA\Items( + * type="integer", + * description="Array of hashed IDs to be bulk 'actioned", + * example="[0,1,2,3]", + * ), + * ) + * ) + * ), + * @OA\Response( + * response=200, + * description="The ExpenseCategory List response", + * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), + * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), + * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), + * @OA\JsonContent(ref="#/components/schemas/Webhook"), + * ), + * @OA\Response( + * response=422, + * description="Validation error", + * @OA\JsonContent(ref="#/components/schemas/ValidationError"), + * ), + * @OA\Response( + * response="default", + * description="Unexpected Error", + * @OA\JsonContent(ref="#/components/schemas/Error"), + * ), + * ) + */ + public function bulk() + { + $action = request()->input('action'); + + $ids = request()->input('ids'); + + $expense_categories = ExpenseCategory::withTrashed()->find($this->transformKeys($ids)); + + $expense_categories->each(function ($tax_rate, $key) use ($action) { + if (auth()->user()->can('edit', $tax_rate)) { + $this->base_repo->{$action}($tax_rate); + } + }); + + return $this->listResponse(ExpenseCategory::withTrashed()->whereIn('id', $this->transformKeys($ids))); + } +} diff --git a/app/Http/Controllers/OpenAPI/ExpenseCategorySchema.php b/app/Http/Controllers/OpenAPI/ExpenseCategorySchema.php new file mode 100644 index 000000000000..f42ce8143905 --- /dev/null +++ b/app/Http/Controllers/OpenAPI/ExpenseCategorySchema.php @@ -0,0 +1,13 @@ +user()->->isAdmin(); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return []; + } +} diff --git a/app/Http/Requests/ExpenseCategory/CreateExpenseCategoryRequest.php b/app/Http/Requests/ExpenseCategory/CreateExpenseCategoryRequest.php new file mode 100644 index 000000000000..615a5d6b439a --- /dev/null +++ b/app/Http/Requests/ExpenseCategory/CreateExpenseCategoryRequest.php @@ -0,0 +1,28 @@ +user()->can('create', ExpenseCategory::class); + } +} diff --git a/app/Http/Requests/ExpenseCategory/DestroyExpenseCategoryRequest.php b/app/Http/Requests/ExpenseCategory/DestroyExpenseCategoryRequest.php new file mode 100644 index 000000000000..126bb2e1ead3 --- /dev/null +++ b/app/Http/Requests/ExpenseCategory/DestroyExpenseCategoryRequest.php @@ -0,0 +1,28 @@ +user()->can('edit', $this->expense_category); + } +} diff --git a/app/Http/Requests/ExpenseCategory/EditExpenseCategoryRequest.php b/app/Http/Requests/ExpenseCategory/EditExpenseCategoryRequest.php new file mode 100644 index 000000000000..6046b3d8c1df --- /dev/null +++ b/app/Http/Requests/ExpenseCategory/EditExpenseCategoryRequest.php @@ -0,0 +1,38 @@ +user()->can('edit', $this->expense_category); + } + + // public function prepareForValidation() + // { + // $input = $this->all(); + + // //$input['id'] = $this->encodePrimaryKey($input['id']); + + // $this->replace($input); + + // } +} diff --git a/app/Http/Requests/ExpenseCategory/ShowExpenseCategoryRequest.php b/app/Http/Requests/ExpenseCategory/ShowExpenseCategoryRequest.php new file mode 100644 index 000000000000..7f8cf1413994 --- /dev/null +++ b/app/Http/Requests/ExpenseCategory/ShowExpenseCategoryRequest.php @@ -0,0 +1,28 @@ +user()->can('view', $this->expense_category); + } +} diff --git a/app/Http/Requests/ExpenseCategory/StoreExpenseCategoryRequest.php b/app/Http/Requests/ExpenseCategory/StoreExpenseCategoryRequest.php new file mode 100644 index 000000000000..51e017e8b079 --- /dev/null +++ b/app/Http/Requests/ExpenseCategory/StoreExpenseCategoryRequest.php @@ -0,0 +1,61 @@ +user()->can('create', ExpenseCategory::class); + } + + public function rules() + { + $rules = []; + $rules['name'] = 'unique:expense_categories,name,'.$this->id.',id,company_id,'.$this->company_id;; + + + return $rules; + } + + protected function prepareForValidation() + { + // $input = $this->all(); + + + // $this->replace($input); + } + + // public function messages() + // { + // return [ + // 'unique' => ctrans('validation.unique', ['attribute' => 'email']), + // //'required' => trans('validation.required', ['attribute' => 'email']), + // 'contacts.*.email.required' => ctrans('validation.email', ['attribute' => 'email']), + // ]; + // } +} diff --git a/app/Http/Requests/ExpenseCategory/UpdateExpenseCategoryRequest.php b/app/Http/Requests/ExpenseCategory/UpdateExpenseCategoryRequest.php new file mode 100644 index 000000000000..f84f5a5ba392 --- /dev/null +++ b/app/Http/Requests/ExpenseCategory/UpdateExpenseCategoryRequest.php @@ -0,0 +1,64 @@ +user()->can('edit', $this->expense_category); + } + + public function rules() + { + /* Ensure we have a client name, and that all emails are unique*/ + $rules = []; + + if ($this->input('number')) { + $rules['name'] = 'unique:expense_categories,name,'.$this->id.',id,company_id,'.$this->expense_category->name; + } + + return $rules; + } + + // public function messages() + // { + // return [ + // 'unique' => ctrans('validation.unique', ['attribute' => 'email']), + // 'email' => ctrans('validation.email', ['attribute' => 'email']), + // 'name.required' => ctrans('validation.required', ['attribute' => 'name']), + // 'required' => ctrans('validation.required', ['attribute' => 'email']), + // ]; + // } + + protected function prepareForValidation() + { + $input = $this->all(); + + $this->replace($input); + } +} diff --git a/app/Policies/ExpenseCategoryPolicy.php b/app/Policies/ExpenseCategoryPolicy.php new file mode 100644 index 000000000000..37529ece0486 --- /dev/null +++ b/app/Policies/ExpenseCategoryPolicy.php @@ -0,0 +1,32 @@ +isAdmin() || $user->hasPermission('create_expense_categories') || $user->hasPermission('create_all'); + } +} diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 793e49d37bcb..d0c8d2978134 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -20,6 +20,7 @@ use App\Models\Credit; use App\Models\Design; use App\Models\Document; use App\Models\Expense; +use App\Models\ExpenseCategory; use App\Models\GroupSetting; use App\Models\Invoice; use App\Models\Payment; @@ -42,6 +43,7 @@ use App\Policies\CompanyTokenPolicy; use App\Policies\CreditPolicy; use App\Policies\DesignPolicy; use App\Policies\DocumentPolicy; +use App\Policies\ExpenseCategoryPolicy; use App\Policies\ExpensePolicy; use App\Policies\GroupSettingPolicy; use App\Policies\InvoicePolicy; @@ -78,6 +80,7 @@ class AuthServiceProvider extends ServiceProvider Design::class => DesignPolicy::class, Document::class => DocumentPolicy::class, Expense::class => ExpensePolicy::class, + ExpenseCategory::class => ExpenseCategoryPolicy::class, GroupSetting::class => GroupSettingPolicy::class, Invoice::class => InvoicePolicy::class, Payment::class => PaymentPolicy::class, diff --git a/app/Transformers/ExpenseTransformer.php b/app/Transformers/ExpenseTransformer.php index 8dcde2bfd23d..89a00c33e3a1 100644 --- a/app/Transformers/ExpenseTransformer.php +++ b/app/Transformers/ExpenseTransformer.php @@ -58,7 +58,7 @@ class ExpenseTransformer extends EntityTransformer 'bank_id' => (string) $expense->bank_id ?: '', 'invoice_currency_id' => (string) $expense->invoice_currency_id ?: '', 'expense_currency_id' => (string) $expense->expense_currency_id ?: '', - 'invoice_category_id' => (string) $expense->invoice_category_id ?: '', + 'category_id' => (string) $expense->category_id ?: '', 'payment_type_id' => (string) $expense->payment_type_id ?: '', 'recurring_expense_id' => (string) $expense->recurring_expense_id ?: '', 'is_deleted' => (bool) $expense->is_deleted, diff --git a/database/factories/ExpenseCategoryFactory.php b/database/factories/ExpenseCategoryFactory.php new file mode 100644 index 000000000000..38a1c07aa7fd --- /dev/null +++ b/database/factories/ExpenseCategoryFactory.php @@ -0,0 +1,37 @@ + $this->faker->text(10), + ]; + } +} diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index b4aa48dde235..edd0af5e83f9 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -1244,11 +1244,12 @@ class CreateUsersTable extends Migration $table->increments('id'); $table->unsignedInteger('user_id'); $table->unsignedInteger('company_id')->index(); + $table->string('name')->nullable(); $table->timestamps(6); $table->softDeletes(); - $table->string('name')->nullable(); $table->index(['company_id', 'deleted_at']); + $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade')->onUpdate('cascade'); }); Schema::create('expenses', function (Blueprint $table) { diff --git a/database/migrations/2020_10_12_204517_project_number_column.php b/database/migrations/2020_10_12_204517_project_number_column.php index 9bf2f86cfc9f..1a771af99000 100644 --- a/database/migrations/2020_10_12_204517_project_number_column.php +++ b/database/migrations/2020_10_12_204517_project_number_column.php @@ -16,6 +16,10 @@ class ProjectNumberColumn extends Migration Schema::table('projects', function($table){ $table->string('number')->nullable(); }); + + Schema::table('expenses', function ($t){ + $t->renameColumn('expense_date', 'date'); + }); } /** diff --git a/routes/api.php b/routes/api.php index fe48c007897f..711dcc25088e 100644 --- a/routes/api.php +++ b/routes/api.php @@ -69,6 +69,10 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a Route::post('expenses/bulk', 'ExpenseController@bulk')->name('expenses.bulk'); + Route::resource('expense_categories', 'ExpenseCategoryController'); // name = (expense_categories. index / create / show / update / destroy / edit + + Route::post('expense_categories/bulk', 'ExpenseCategoryController@bulk')->name('expense_categories.bulk'); + Route::resource('tasks', 'TaskController'); // name = (tasks. index / create / show / update / destroy / edit Route::post('tasks/bulk', 'TaskController@bulk')->name('tasks.bulk'); diff --git a/tests/Feature/ExpenseCategoryApiTest.php b/tests/Feature/ExpenseCategoryApiTest.php new file mode 100644 index 000000000000..1b4c28123e51 --- /dev/null +++ b/tests/Feature/ExpenseCategoryApiTest.php @@ -0,0 +1,152 @@ +makeTestData(); + + Session::start(); + + $this->faker = \Faker\Factory::create(); + + Model::reguard(); + } + + public function testExpenseCategoryPost() + { + $data = [ + 'public_notes' => $this->faker->firstName, + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/expense_categories', $data); + + $response->assertStatus(200); + } + + public function testExpenseCategoryPut() + { + $data = [ + 'public_notes' => $this->faker->firstName, + 'id_number' => 'Coolio', + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->put('/api/v1/expense_categories/'.$this->encodePrimaryKey($this->expense_category->id), $data); + + $response->assertStatus(200); + } + + public function testExpenseCategoryGet() + { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/expense_categories/'.$this->encodePrimaryKey($this->expense_category->id)); + + $response->assertStatus(200); + } + + public function testExpenseCategoryNotArchived() + { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/expense_categories/'.$this->encodePrimaryKey($this->expense_category->id)); + + $arr = $response->json(); + + $this->assertEquals(0, $arr['data']['archived_at']); + } + + public function testExpenseCategoryArchived() + { + $data = [ + 'ids' => [$this->encodePrimaryKey($this->expense_category->id)], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/expense_categories/bulk?action=archive', $data); + + $arr = $response->json(); + + $this->assertNotNull($arr['data'][0]['archived_at']); + } + + public function testExpenseCategoryRestored() + { + $data = [ + 'ids' => [$this->encodePrimaryKey($this->expense_category->id)], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/expense_categories/bulk?action=restore', $data); + + $arr = $response->json(); + + $this->assertEquals(0, $arr['data'][0]['archived_at']); + } + + public function testExpenseCategoryDeleted() + { + $data = [ + 'ids' => [$this->encodePrimaryKey($this->expense_category->id)], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/expense_categories/bulk?action=delete', $data); + + $arr = $response->json(); + + $this->assertTrue($arr['data'][0]['is_deleted']); + } +} diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index 87972001b69a..3c36bf758ac9 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -32,6 +32,7 @@ use App\Models\CompanyGateway; use App\Models\CompanyToken; use App\Models\Credit; use App\Models\Expense; +use App\Models\ExpenseCategory; use App\Models\GroupSetting; use App\Models\Invoice; use App\Models\InvoiceInvitation; @@ -80,6 +81,8 @@ trait MockAccountData public $task; + public $expense_category; + public function makeTestData() { @@ -225,6 +228,11 @@ trait MockAccountData 'company_id' => $this->company->id, ]); + $this->expense_category = ExpenseCategory::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + ]); + $gs = new GroupSetting; $gs->name = 'Test'; $gs->company_id = $this->client->company_id;