Skip to content

Commit

Permalink
Merge pull request #443 from range-of-motion/improve-validation-for-s…
Browse files Browse the repository at this point in the history
…toring-transactions-through-api

Improve validation for storing transactions through API
  • Loading branch information
range-of-motion authored Nov 19, 2023
2 parents 7f20642 + dfcf6d2 commit a615fd9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
11 changes: 7 additions & 4 deletions app/Http/Controllers/Api/TransactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ public function store(Request $request)
$spaceId = $apiKey->user->spaces()->first()->id;

$request->validate([
'type' => 'in:earning,spending',
// TODO
'type' => ['required', 'in:earning,spending'],
// 'tag_id' => ['nullable', 'exists:tags,id'], // TODO CHECK IF TAG BELONGS TO USER
'happened_on' => ['required', 'date', 'date_format:Y-m-d'],
'description' => ['required', 'max:255'],
'amount' => ['required', 'regex:/^\d*(\.\d{1,2})?$/'],
]);

if ($request->input('type') === 'earning') {
Expand All @@ -46,7 +49,7 @@ public function store(Request $request)
'recurring_id' => null, // TODO
'happened_on' => $request->input('happened_on'),
'description' => $request->input('description'),
'amount' => $request->input('amount')
'amount' => (int) ($request->input('amount') * 100),
]);
}

Expand All @@ -58,7 +61,7 @@ public function store(Request $request)
'tag_id' => null, // TODO
'happened_on' => $request->input('happened_on'),
'description' => $request->input('description'),
'amount' => $request->input('amount')
'amount' => (int) ($request->input('amount') * 100),
]);
}

Expand Down
6 changes: 4 additions & 2 deletions resources/assets/js/prototype/screens/Transactions/Create.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<script setup>
import axios from 'axios';
import { ref } from 'vue';
import { getCurrentInstance, ref } from 'vue';
import Navigation from '../../components/Navigation.vue';
const router = getCurrentInstance().proxy.$router;
const type = ref('earning');
const happened_on = ref('2023-11-02');
const description = ref('');
Expand All @@ -21,7 +23,7 @@ const create = () => {
},
})
.then(() => {
alert('wtf, dat werkte');
router.push({ name: 'transactions.index' });
})
.catch(() => {
alert('Something went wrong');
Expand Down
50 changes: 50 additions & 0 deletions tests/Feature/Api/TransactionControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Tests\Feature\Api;

use App\Models\ApiKey;
use App\Models\Earning;
use App\Models\Space;
use App\Models\User;
use Tests\TestCase;

class TransactionControllerTest extends TestCase
{
public function testStoreEndpoint(): void
{
$user = User::factory()
->create();

$space = Space::factory()
->create();

$user->spaces()->attach($space->id);

$apiKey = ApiKey::factory()
->create(['user_id' => $user->id]);

$response = $this->postJson(
'/api/transactions',
[
'type' => 'earning',
'happened_on' => '2021-01-01',
'description' => 'Helping grandma',
'amount' => 10.50,
],
[
'api-key' => $apiKey->token,
],
);

$response->assertStatus(200);

$this->assertDatabaseHas(
Earning::class,
[
'happened_on' => '2021-01-01',
'description' => 'Helping grandma',
'amount' => 1050,
],
);
}
}

0 comments on commit a615fd9

Please sign in to comment.