parent
957b07b3eb
commit
720d2c4e7b
@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Toby\Infrastructure\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Toby\Domain\UserVacationStatsRetriever;
|
||||||
|
use Toby\Domain\VacationRequestStatesRetriever;
|
||||||
|
use Toby\Eloquent\Helpers\YearPeriodRetriever;
|
||||||
|
use Toby\Eloquent\Models\User;
|
||||||
|
use Toby\Infrastructure\Http\Controllers\Controller;
|
||||||
|
use Toby\Infrastructure\Http\Requests\Api\CalculateUserUnavailableDaysRequest;
|
||||||
|
|
||||||
|
class CalculateUserUnavailableDaysController extends Controller
|
||||||
|
{
|
||||||
|
public function __invoke(
|
||||||
|
CalculateUserUnavailableDaysRequest $request,
|
||||||
|
UserVacationStatsRetriever $vacationStatsRetriever,
|
||||||
|
YearPeriodRetriever $yearPeriodRetriever,
|
||||||
|
): JsonResponse {
|
||||||
|
/** @var User $user */
|
||||||
|
$user = User::query()->find($request->get("user"));
|
||||||
|
$yearPeriod = $yearPeriodRetriever->selected();
|
||||||
|
|
||||||
|
$holidays = $yearPeriod->holidays()->pluck("date");
|
||||||
|
$vacationDays = $user->vacations()
|
||||||
|
->where("year_period_id", $yearPeriod->id)
|
||||||
|
->whereRelation(
|
||||||
|
"vacationRequest",
|
||||||
|
fn(Builder $query) => $query->noStates(VacationRequestStatesRetriever::failedStates()),
|
||||||
|
)
|
||||||
|
->pluck("date");
|
||||||
|
|
||||||
|
return new JsonResponse([
|
||||||
|
...$holidays->map(fn(Carbon $date) => $date->toDateString()),
|
||||||
|
...$vacationDays->map(fn(Carbon $date) => $date->toDateString()),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace Toby\Infrastructure\Http\Controllers\Api;
|
namespace Toby\Infrastructure\Http\Controllers\Api;
|
||||||
|
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
use Toby\Domain\VacationDaysCalculator;
|
use Toby\Domain\VacationDaysCalculator;
|
||||||
use Toby\Infrastructure\Http\Controllers\Controller;
|
use Toby\Infrastructure\Http\Controllers\Controller;
|
||||||
use Toby\Infrastructure\Http\Requests\Api\CalculateVacationDaysRequest;
|
use Toby\Infrastructure\Http\Requests\Api\CalculateVacationDaysRequest;
|
||||||
@ -15,6 +16,6 @@ class CalculateVacationDaysController extends Controller
|
|||||||
{
|
{
|
||||||
$days = $calculator->calculateDays($request->yearPeriod(), $request->from(), $request->to());
|
$days = $calculator->calculateDays($request->yearPeriod(), $request->from(), $request->to());
|
||||||
|
|
||||||
return new JsonResponse($days->all());
|
return new JsonResponse($days->map(fn(Carbon $day) => $day->toDateString())->all());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Toby\Infrastructure\Http\Requests\Api;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class CalculateUserUnavailableDaysRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
"user" => ["required", "exists:users,id"],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -332,7 +332,6 @@ import axios from 'axios'
|
|||||||
import useCurrentYearPeriodInfo from '@/Composables/yearPeriodInfo'
|
import useCurrentYearPeriodInfo from '@/Composables/yearPeriodInfo'
|
||||||
import VacationChart from '@/Shared/VacationChart'
|
import VacationChart from '@/Shared/VacationChart'
|
||||||
|
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
auth: Object,
|
auth: Object,
|
||||||
users: Object,
|
users: Object,
|
||||||
@ -362,26 +361,24 @@ const stats = ref({
|
|||||||
|
|
||||||
const { minDate, maxDate } = useCurrentYearPeriodInfo()
|
const { minDate, maxDate } = useCurrentYearPeriodInfo()
|
||||||
|
|
||||||
const disableDates = [
|
const weekends = date => (date.getDay() === 0 || date.getDay() === 6)
|
||||||
date => (date.getDay() === 0 || date.getDay() === 6),
|
|
||||||
]
|
|
||||||
|
|
||||||
const fromInputConfig = reactive({
|
const fromInputConfig = reactive({
|
||||||
minDate,
|
minDate,
|
||||||
maxDate,
|
maxDate,
|
||||||
disable: disableDates,
|
disable: [weekends],
|
||||||
})
|
})
|
||||||
|
|
||||||
const toInputConfig = reactive({
|
const toInputConfig = reactive({
|
||||||
minDate,
|
minDate,
|
||||||
maxDate,
|
maxDate,
|
||||||
disable: disableDates,
|
disable: [weekends],
|
||||||
})
|
})
|
||||||
|
|
||||||
watch(() => form.user, async user => {
|
watch(() => form.user, user => {
|
||||||
const res = await axios.post('/api/calculate-vacations-stats', { user: user.id })
|
resetForm()
|
||||||
|
refreshVacationStats(user)
|
||||||
stats.value = res.data
|
refreshUnavailableDays(user)
|
||||||
}, { immediate: true })
|
}, { immediate: true })
|
||||||
|
|
||||||
function createForm() {
|
function createForm() {
|
||||||
@ -395,15 +392,30 @@ function createForm() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onFromChange(selectedDates, dateStr) {
|
function onFromChange(selectedDates, dateStr) {
|
||||||
|
if (form.to === null) {
|
||||||
form.to = dateStr
|
form.to = dateStr
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
refreshEstimatedDays(form.from, form.to)
|
refreshEstimatedDays(form.from, form.to)
|
||||||
}
|
}
|
||||||
|
|
||||||
function onToChange() {
|
function onToChange(selectedDates, dateStr) {
|
||||||
|
if (form.from === null) {
|
||||||
|
form.from = dateStr
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
refreshEstimatedDays(form.from, form.to)
|
refreshEstimatedDays(form.from, form.to)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resetForm() {
|
||||||
|
form.reset('type', 'to', 'from', 'comment', 'flowSkipped')
|
||||||
|
form.clearErrors()
|
||||||
|
estimatedDays.value = []
|
||||||
|
}
|
||||||
|
|
||||||
async function refreshEstimatedDays(from, to) {
|
async function refreshEstimatedDays(from, to) {
|
||||||
if (from && to) {
|
if (from && to) {
|
||||||
const res = await axios.post('/api/calculate-vacation-days', { from, to })
|
const res = await axios.post('/api/calculate-vacation-days', { from, to })
|
||||||
@ -412,4 +424,25 @@ async function refreshEstimatedDays(from, to) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function refreshVacationStats(user) {
|
||||||
|
const res = await axios.post('/api/calculate-vacation-stats', { user: user.id })
|
||||||
|
|
||||||
|
stats.value = res.data
|
||||||
|
}
|
||||||
|
|
||||||
|
async function refreshUnavailableDays(user) {
|
||||||
|
const res = await axios.post('/api/calculate-unavailable-days', { user: user.id })
|
||||||
|
const unavailableDays = res.data
|
||||||
|
|
||||||
|
fromInputConfig.disable = [
|
||||||
|
weekends,
|
||||||
|
...unavailableDays,
|
||||||
|
]
|
||||||
|
|
||||||
|
toInputConfig.disable = [
|
||||||
|
weekends,
|
||||||
|
...unavailableDays,
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
@ -3,10 +3,12 @@
|
|||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use Toby\Infrastructure\Http\Controllers\Api\CalculateUserUnavailableDaysController;
|
||||||
use Toby\Infrastructure\Http\Controllers\Api\CalculateUserVacationStatsController;
|
use Toby\Infrastructure\Http\Controllers\Api\CalculateUserVacationStatsController;
|
||||||
use Toby\Infrastructure\Http\Controllers\Api\CalculateVacationDaysController;
|
use Toby\Infrastructure\Http\Controllers\Api\CalculateVacationDaysController;
|
||||||
|
|
||||||
Route::middleware("auth:sanctum")->group(function (): void {
|
Route::middleware("auth:sanctum")->group(function (): void {
|
||||||
Route::post("calculate-vacation-days", CalculateVacationDaysController::class);
|
Route::post("calculate-vacation-days", CalculateVacationDaysController::class);
|
||||||
Route::post("calculate-vacations-stats", CalculateUserVacationStatsController::class);
|
Route::post("calculate-vacation-stats", CalculateUserVacationStatsController::class);
|
||||||
|
Route::post("calculate-unavailable-days", CalculateUserUnavailableDaysController::class);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user