- Menambahkan logika baru di `ResponseCode` untuk memisahkan struktur response sukses dan error. - Field `errors` sekarang berada di root level untuk response validasi error. - Meta response error lebih ringkas, hanya menyertakan `generated_at` dan `request_id`. - Pesan error menampilkan seluruh daftar error, bukan hanya error pertama. - Konsistensi format response memudahkan integrasi dengan frontend. - Field `account_number` dan `period` dihapus dari meta pada response error. - Memberikan request_id otomatis pada setiap response untuk keperluan tracking. - Semua endpoint yang menggunakan ResponseCode enum otomatis mengikuti format baru.
110 lines
3.3 KiB
PHP
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();
|
|
|
|
throw new \Illuminate\Http\Exceptions\HttpResponseException(
|
|
response()->json(
|
|
\Modules\Webstatement\Enums\ResponseCode::INVALID_FIELD->toResponse(
|
|
['errors' => $errors->all()],
|
|
'Field tertentu tidak sesuai aturan'
|
|
),
|
|
\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()
|
|
]);
|
|
}
|
|
}
|