-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #455 from range-of-motion/support-creating-of-recu…
…rrings-in-spa-prototype Support creating of recurrings in SPA prototype
- Loading branch information
Showing
4 changed files
with
118 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace App\Enums; | ||
|
||
enum RecurringInterval: string | ||
{ | ||
case daily = 'daily'; | ||
case weekly = 'weekly'; | ||
case biweekly = 'biweekly'; | ||
case monthly = 'monthly'; | ||
case yearly = 'yearly'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Enums\RecurringInterval; | ||
use App\Http\Controllers\Controller; | ||
use App\Jobs\ProcessRecurrings; | ||
use App\Models\Recurring; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Validation\Rule; | ||
|
||
class RecurringController extends Controller | ||
{ | ||
public function store(Request $request) | ||
{ | ||
/** @var ApiKey $apiKey */ | ||
$apiKey = $request->get('apiKey'); | ||
|
||
$space = $apiKey->user->spaces()->first(); | ||
|
||
$request->validate([ | ||
'type' => ['required', 'in:earning,spending'], | ||
'interval' => ['required', Rule::enum(RecurringInterval::class)], | ||
'day' => ['required', 'regex:/\b(0?[1-9]|[12][0-9]|3[01])\b/'], | ||
'start' => ['date', 'date_format:Y-m-d'], | ||
'end' => ['nullable', 'date', 'date_format:Y-m-d'], | ||
'tag_id' => ['nullable', 'exists:tags,id'], // TODO: CHECK IF TAG BELONGS TO USER | ||
'description' => ['required', 'max:255'], | ||
'amount' => ['required', 'regex:/^\d*(\.\d{1,2})?$/'], | ||
]); | ||
|
||
Recurring::create([ | ||
'space_id' => $space->id, | ||
'type' => $request->input('type'), | ||
'interval' => $request->input('interval'), | ||
'day' => (int) ltrim($request->input('day'), 0), | ||
'starts_on' => $request->input('start'), | ||
'ends_on' => $request->input('end'), | ||
'tag_id' => $request->input('tag_id'), | ||
'description' => $request->input('description'), | ||
'amount' => (int) ($request->input('amount') * 100), | ||
'currency_id' => $space->currency_id, | ||
]); | ||
|
||
ProcessRecurrings::dispatch(); | ||
|
||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters