Files
webstatement/app/Http/Requests/BalanceSummaryRequest.php
Daeng Deni Mardaeni e53b522f77 feat(API): standarisasi response API dengan ResponseCode enum dan penambahan struktur meta
- Menambahkan ResponseCode enum untuk standarisasi semua response API.
- Integrasi meta data: nomor rekening, periode, request_id, dan reference_code.
- Memperbarui validasi input dengan response code standar (INVALID_FIELD).
- Struktur response dibuat konsisten untuk success dan error.
- Logging diperkuat untuk debugging dan monitoring.
2025-08-27 17:07:57 +07:00

110 lines
3.3 KiB
PHP

<?php
namespace Modules\Webstatement\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Log;
class BalanceSummaryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
// Sesuaikan dengan authorization logic jika diperlukan
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'account_number' => [
'required',
'string',
'max:50',
'regex:/^[A-Z0-9-]+$/i' // Hanya alphanumeric dan dash
],
'start_date' => [
'required',
'date_format:Y-m-d',
'before_or_equal:end_date',
'after_or_equal:1900-01-01',
'before_or_equal:today'
],
'end_date' => [
'required',
'date_format:Y-m-d',
'after_or_equal:start_date',
'after_or_equal:1900-01-01',
'before_or_equal:today'
],
];
}
/**
* Get custom messages for validator errors.
*
* @return array<string, string>
*/
public function messages(): array
{
return [
'account_number.required' => 'Nomor rekening wajib diisi.',
'account_number.string' => 'Nomor rekening harus berupa teks.',
'account_number.max' => 'Nomor rekening maksimal :max karakter.',
'account_number.regex' => 'Nomor rekening hanya boleh mengandung huruf, angka, dan strip.',
'start_date.required' => 'Tanggal awal wajib diisi.',
'start_date.date_format' => 'Format tanggal awal harus YYYY-MM-DD.',
'start_date.before_or_equal' => 'Tanggal awal harus sebelum atau sama dengan tanggal akhir.',
'end_date.required' => 'Tanggal akhir wajib diisi.',
'end_date.date_format' => 'Format tanggal akhir harus YYYY-MM-DD.',
'end_date.after_or_equal' => 'Tanggal akhir harus sesudah atau sama dengan tanggal awal.',
];
}
/**
* Handle a failed validation attempt.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
protected function failedValidation($validator)
{
$errors = $validator->errors();
$firstError = $errors->first();
throw new \Illuminate\Http\Exceptions\HttpResponseException(
response()->json(
\Modules\Webstatement\Enums\ResponseCode::INVALID_FIELD->toResponse(
null,
$firstError
),
\Modules\Webstatement\Enums\ResponseCode::INVALID_FIELD->getHttpStatus()
)
);
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation(): void
{
Log::info('Balance summary request received', [
'input' => $this->all(),
'ip' => $this->ip(),
'user_agent' => $this->userAgent()
]);
}
}