json( LoanOrder::query() ->where('user_id', auth()->id()) ->where('source', OrderRepo::MOBILE_DEVICE) ->latest() ->get() ->map(fn (LoanOrder $loanOrder) => [ 'id' => $loanOrder->id, 'loan_type' => $loanOrder->loanType?->name, 'loan_amount' => $loanOrder->loan_amount, 'created_at' => $loanOrder->created_at, ]) ); } /** * SAVE* Loan order. * * `Karz sargytlary save`. Example bar, online panelkadan gorup hem bilersin. Update store daky yaly, yone oz ugratyan zadyn update bolyar, eger ugratmasaň, üýtgemez. */ public function store(LoanOrderStoreRequest $request): JsonResponse { Log::channel('form_logs')->info('loan-order-store-request', $request->all()); $data = $request->validated(); $months = DateHelperRepository::monthsAsNumber(); $years = DateHelperRepository::yearsUntil(); $data['card_month'] = indexByValue($request->card_month, $months); $data['card_year'] = indexByValue($request->card_year, $years); $data['guarantor_card_month'] = indexByValue($request->guarantor_card_month, $months); $data['guarantor_card_year'] = indexByValue($request->guarantor_card_year, $years); $data['guarantor_2_card_month'] = indexByValue($request->guarantor_2_card_month, $months); $data['guarantor_2_card_year'] = indexByValue($request->guarantor_2_card_year, $years); LoanOrder::forceCreate([ ...$data, ...[ 'user_id' => auth()->id(), 'status' => OrderRepo::PENDING, 'source' => OrderRepo::MOBILE_DEVICE, ], ...$this->uploadedFiles($request), ]); return response()->json([ 'message' => __('Successfully created'), ], 201); } /** * Upload files * * @return array */ public function uploadedFiles(Request $request): array { $files = []; foreach (['passport_one', 'passport_two', 'passport_three', 'passport_four'] as $field) { if ($request->hasFile($field)) { $files[$field] = Str::after($request->file($field)->store('public'), 'public/'); } } return $files; } /** * SHOW* Loan order * * `Karz sargytlary show by id`. ID ugradyp alyan route -da. Base App Enum-lardan peydalan. Panelkadan gor. */ public function show(LoanOrder $loanOrder): JsonResponse { if ($loanOrder->user_id != auth()->id()) { return response()->json(status: 403); } return response()->json(new LoanOrderShowResource($loanOrder)); } /** * UPDATE* Loan order * * `Karz sargytlary update`. ID ugradyp `route`-da update edip bilyan. Base App Enum-lardan peydalan. Panelkadan gor. */ public function update(LoanOrderUpdateRequest $request, LoanOrder $loanOrder): JsonResponse { Log::channel('form_logs')->info('loan-order-update-request', $request->all()); $data = $request->all(); $months = DateHelperRepository::monthsAsNumber(); $years = DateHelperRepository::yearsUntil(); if ($request->filled('card_month')) { $data['card_month'] = indexByValue($request->card_month, $months); } if ($request->filled('card_year')) { $data['card_year'] = indexByValue($request->card_year, $years); } if ($request->filled('guarantor_card_month')) { $data['guarantor_card_month'] = indexByValue($request->guarantor_card_month, $months); } if ($request->filled('guarantor_card_year')) { $data['guarantor_card_year'] = indexByValue($request->guarantor_card_year, $years); } if ($request->filled('guarantor_2_card_month')) { $data['guarantor_2_card_month'] = indexByValue($request->guarantor_2_card_month, $months); } if ($request->filled('guarantor_2_card_year')) { $data['guarantor_2_card_year'] = indexByValue($request->guarantor_2_card_year, $years); } $data = array_merge($request->all(), $this->uploadedFiles($request)); Model::unguarded(function () use ($loanOrder, $data) { $loanOrder->update($data); }); return response()->json([ 'message' => __('Successfully updates'), ]); } /** * DELETE* loan order resource */ public function destroy(LoanOrder $loanOrder): JsonResponse { if ($loanOrder->user_id != auth()->id()) { return response()->json(status: 403); } $loanOrder->delete(); return response()->json(); } }