From 98137d15743c5363a8622242e6a22404e473f2a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Drunen?= Date: Sat, 9 Dec 2023 00:11:43 +0100 Subject: [PATCH] Support creating of recurrings in SPA prototype --- app/Enums/RecurringInterval.php | 12 ++++ .../Controllers/Api/RecurringController.php | 49 +++++++++++++ .../prototype/screens/Transactions/Create.vue | 71 ++++++++++++++----- routes/api.php | 4 ++ 4 files changed, 118 insertions(+), 18 deletions(-) create mode 100644 app/Enums/RecurringInterval.php create mode 100644 app/Http/Controllers/Api/RecurringController.php diff --git a/app/Enums/RecurringInterval.php b/app/Enums/RecurringInterval.php new file mode 100644 index 00000000..de02d6ae --- /dev/null +++ b/app/Enums/RecurringInterval.php @@ -0,0 +1,12 @@ +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 []; + } +} diff --git a/resources/assets/js/prototype/screens/Transactions/Create.vue b/resources/assets/js/prototype/screens/Transactions/Create.vue index f9dfd8d6..e4357987 100644 --- a/resources/assets/js/prototype/screens/Transactions/Create.vue +++ b/resources/assets/js/prototype/screens/Transactions/Create.vue @@ -10,9 +10,11 @@ const tags = ref([]); const type = ref('earning'); const tagId = ref(null); -const happened_on = ref(new Date().toJSON().slice(0, 10)); +const happenedOn = ref(new Date().toJSON().slice(0, 10)); const description = ref(''); const amount = ref(10.00); +const isRecurring = ref(false); +const recurringInterval = ref('monthly'); const fetchTags = () => { fetch('/api/tags', { headers: { 'api-key': localStorage.getItem('api_key') } }) @@ -23,23 +25,45 @@ const fetchTags = () => { }; const create = () => { - axios.post('/api/transactions', { - type: type.value, - tag_id: tagId.value, - happened_on: happened_on.value, - description: description.value, - amount: amount.value, - }, { - headers: { - 'api-key': localStorage.getItem('api_key'), - }, - }) - .then(() => { - router.push({ name: 'transactions.index' }); + if (isRecurring.value === true) { + axios.post('/api/recurrings', { + type: type.value, + interval: recurringInterval.value, + day: happenedOn.value.split('-')[2], + start: happenedOn.value, + tag_id: tagId.value, + description: description.value, + amount: amount.value, + }, { + headers: { + 'api-key': localStorage.getItem('api_key'), + }, }) - .catch(() => { - alert('Something went wrong'); - }); + .then(() => { + router.push({ name: 'transactions.index' }); + }) + .catch(() => { + alert('Something went wrong'); + }); + } else { + axios.post('/api/transactions', { + type: type.value, + tag_id: tagId.value, + happened_on: happenedOn.value, + description: description.value, + amount: amount.value, + }, { + headers: { + 'api-key': localStorage.getItem('api_key'), + }, + }) + .then(() => { + router.push({ name: 'transactions.index' }); + }) + .catch(() => { + alert('Something went wrong'); + }); + } }; fetchTags(); @@ -63,7 +87,7 @@ fetchTags();
- +
@@ -73,6 +97,17 @@ fetchTags();
+
+ + +
+
+ + + + + +
diff --git a/routes/api.php b/routes/api.php index 45b215b1..d9e3b0ee 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,6 +1,7 @@ only(['index', 'store']); + Route::resource('recurrings', RecurringController::class) + ->only(['store']); + Route::resource('tags', TagController::class) ->only(['index']);