API APP DOCTOR

This commit is contained in:
ivan-sim
2024-04-23 16:28:59 +07:00
parent 2cf00ac927
commit 1cb3b2d3f0
15 changed files with 811 additions and 10 deletions

View File

@@ -0,0 +1,264 @@
<?php
namespace Modules\Linksehat\Http\Controllers\Api\Doctor;
use App\Http\Controllers\Controller;
use App\Models\User;
use Crypt;
use Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Modules\Internal\Emails\SendVerifyEmail;
use Modules\Internal\Events\ForgetPassword;
use Illuminate\Support\Facades\Validator;
use Modules\HospitalPortal\Helpers\ApiResponse;
use App\Helpers\Helper;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\DB;
class AuthDoctorController extends Controller
{
public function login(Request $request)
{
$data = [
'email' => $request->email,
'password' => $request->password
];
$validator = Validator::make($request->all(), [
'email' => 'required|email',
'password' => 'required'
], [
'email.required' => trans('validation.required',['attribute' => 'Email']),
'email.email' => trans('validation.email'),
'password.required' => trans('validation.required',['attribute' => 'Password']),
]);
if ($validator->fails())
{
return ApiResponse::apiResponse('Bad Request', $data, $validator->errors(), 400);
}
else
{
$user = User::where('email', $request->email)->first();
if (!$user) {
return ApiResponse::apiResponse('Not Found', $data, trans('message.not_found'), 404);
}
if (!Hash::check($request->password, $user->password)) {
return ApiResponse::apiResponse('Bad Request', $data, trans('message.password'), 400);
}
$res_data = [
'user' => $user,
'token' => $user->createToken('app')->plainTextToken
];
return ApiResponse::apiResponse("Success", $res_data, trans('message.success'), 200);
}
}
public function logout(Request $request)
{
$request->user()->tokens()->delete();
return ApiResponse::apiResponse('Success', [], trans('message.logout'), 200);
}
public function forgotPassword(Request $request)
{
$data = [
'email' => $request->email,
];
$validator = Validator::make($request->all(), [
'email' => 'required|email',
], [
'email.required' => trans('validation.required',['attribute' => 'Email']),
'email.email' => trans('validation.email'),
]);
if ($validator->fails())
{
return ApiResponse::apiResponse('Bad Request', $data, $validator->errors(), 400);
}
else
{
$user = User::where('email', $request->email)->first();
if (!$user) {
return ApiResponse::apiResponse('Not Found', $data, trans('message.not_found'), 404);
}
//send email
// Insert data notifications
$emailTo = $request->email;
$dataNotif = [
'user_id' => $user->id,
'email' => $emailTo,
'title' => 'Forgot Password',
'description' => 'Request forgot password from App Doctor',
'type' => 1,
'isUnRead' => true,
'created_by' => auth()->check() ? auth()->user()->id : null,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
];
$sendNotif = Helper::insertNotification($dataNotif);
//Insert data password reset
$token = mt_rand(100000, 999999); // Menghasilkan angka acak antara 100000 dan 999999
$p_resets = DB::table('password_resets')
->insert([
'email' => $request->email,
'token' => $token,
'created_at' => date('Y-m-d H:i:s'),
]);
// Send Email after insert notifications
if($sendNotif && $p_resets)
{
//send to alarm
$nameTo = 'User';
$dataEmail = [
'email' => $emailTo,
'name' => $nameTo,
'subject' => 'Request Forgot Password from App Doctor Date '. date('Y-m-d H:i:s'),
'body' => View::make('email/forgot_password', ['token' => $token])->render(),
];
Helper::sendEmail($dataEmail);
$res = DB::table('password_resets')
->where('email', '=', $request->email)
->where('token', '=', $token)
->get();
return ApiResponse::apiResponse("Success", $res, trans('message.success'), 200);
}
else
{
return ApiResponse::apiResponse("Internal Server Error", $data, trans('message.server_error'), 500);
}
}
}
public function verifCode(Request $request)
{
$data = [
'email' => $request->email,
'token' => $request->token,
];
$validator = Validator::make($request->all(), [
'email' => 'required|email',
'token' => 'required|numeric',
], [
'email.required' => trans('validation.required',['attribute' => 'Email']),
'email.email' => trans('validation.email'),
'token.required' => trans('validation.required',['attribute' => 'Token']),
]);
if ($validator->fails())
{
return ApiResponse::apiResponse('Bad Request', $data, $validator->errors(), 400);
}
else
{
//Check Time
$check = DB::table('password_resets')
->where('email', '=', $request->email)
->where('token', '=', $request->token)
->select('created_at')
->first();
if($check)
{
$created_at = strtotime($check->created_at); // Konversi string waktu ke UNIX timestamp
$now = time(); // Waktu sekarang dalam UNIX timestamp
// Hitung selisih waktu dalam menit
$diffInMinutes = ($now - $created_at) / 60;
if ($diffInMinutes > 60) {
return ApiResponse::apiResponse('Not Found', $data, trans('message.token_expired'), 404);
} else {
// Lanjutkan dengan proses pemulihan kata sandi
return ApiResponse::apiResponse("Success", $data, trans('message.success'), 200);
}
}
else
{
return ApiResponse::apiResponse('Not Found', $data, trans('message.not_found'), 404);
}
}
}
public function resetPassword(Request $request)
{
$data = [
'email' => $request->email,
'token' => $request->token,
'new_password' => $request->new_password
];
$validator = Validator::make($request->all(), [
'email' => 'required|email',
'token' => 'required|numeric',
'new_password' => [
'required',
'min:8',
'regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/'
]
], [
'email.required' => trans('validation.required',['attribute' => 'Email']),
'email.email' => trans('validation.email'),
'token.required' => trans('validation.required',['attribute' => 'Token']),
'new_password.required' => trans('validation.required',['attribute' => 'New Password']),
'new_password.min' => trans('validation.min',['attribute' => 'New Password']),
'new_password.regex' => trans('validation.regex',['attribute' => 'New Password']),
]);
if ($validator->fails())
{
return ApiResponse::apiResponse('Bad Request', $data, $validator->errors(), 400);
}
else
{
//Check Time
$check = DB::table('password_resets')
->where('email', '=', $request->email)
->where('token', '=', $request->token)
->select('created_at')
->first();
if($check)
{
$created_at = strtotime($check->created_at); // Konversi string waktu ke UNIX timestamp
$now = time(); // Waktu sekarang dalam UNIX timestamp
// Hitung selisih waktu dalam menit
$diffInMinutes = ($now - $created_at) / 60;
if ($diffInMinutes > 60) {
return ApiResponse::apiResponse('Not Found', $data, trans('message.token_expired'), 404);
} else {
// Lanjutkan dengan proses pemulihan kata sandi
$user = User::where('email', $request->email)->first();
if ($user)
{
$newPassword = Hash::make($request->new_password);
$user->password = $newPassword;
$user->save();
return ApiResponse::apiResponse("Success", $data, trans('message.success'), 200);
}
else
{
return ApiResponse::apiResponse('Not Found', $data, trans('message.token_expired'), 404);
}
}
}
else
{
return ApiResponse::apiResponse('Not Found', $data, trans('message.not_found'), 404);
}
}
}
}

View File

@@ -0,0 +1,175 @@
<?php
namespace Modules\Linksehat\Http\Controllers\Api\Doctor;
use App\Http\Controllers\Controller;
use App\Models\User;
use Crypt;
use Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Modules\Internal\Emails\SendVerifyEmail;
use Modules\Internal\Events\ForgetPassword;
use Illuminate\Support\Facades\Validator;
use Modules\HospitalPortal\Helpers\ApiResponse;
use App\Helpers\Helper;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\DB;
class ProfileDoctorController extends Controller
{
public function getProfile()
{
$data = [
'user_id' => auth()->check() ? auth()->user()->id : null,
];
$user_id = auth()->check() ? auth()->user()->id : null;
//Get data Profile
$dataProfile = DB::table('users')
->join('persons','persons.id', '=', 'users.person_id')
->leftJoin('person_educations','person_educations.person_id', '=', 'persons.id')
->leftJoin('practitioners','practitioners.person_id', '=', 'persons.id')
->leftJoin('practitioner_roles','practitioner_roles.practitioner_id', '=', 'practitioners.id')
->where('users.id', '=', $user_id)
->select(
'persons.name',
DB::raw('
"Pediatrics" AS specialist
'),
DB::raw('
"4" AS rating
'),
'persons.name AS full_name',
'persons.birth_date as date_of_birth',
'persons.gender',
'persons.phone AS mobile_number',
'persons.email',
'practitioners.str_number',
'practitioners.exp_date_str',
'practitioner_roles.sip_number',
'practitioner_roles.exp_date_sip'
)
->first();
//Name
$dataName = [
'name' => $dataProfile->name,
'specialist' => $dataProfile->specialist,
'rating' => $dataProfile->rating
];
$res_data['dataName'] = $dataName;
// Basic
$dataProfileBasic = [
'full_name' => $dataProfile->full_name,
'date_of_birth' => $dataProfile->date_of_birth ? date('d M Y', strtotime($dataProfile->date_of_birth)) : '',
'gender' => $dataProfile->gender
];
$res_data['dataProfileBasic'] = $dataProfileBasic;
//Contact
$dataProfileContact = [
'mobile_number' => $dataProfile->mobile_number,
'email' => $dataProfile->email
];
$res_data['dataProfileContact'] = $dataProfileContact;
//Education
$dataEdu = DB::table('users')
->join('persons','persons.id', '=', 'users.person_id')
->leftJoin('person_educations','person_educations.person_id', '=', 'persons.id')
->where('users.id', '=', $user_id)
->select(
'person_educations.level_id',
'person_educations.name',
'person_educations.start_date',
'person_educations.end_date',
)
->get();
$dataEducations = [];
foreach($dataEdu as $val)
{
$dataEducations[] = [
'level_id' => $val->level_id,
'name' => $val->name,
'start_date' => date('d/m/Y', strtotime($val->start_date)),
'end_date' => date('d/m/Y', strtotime($val->end_date)),
];
}
$res_data['dataEducations'] = $dataEducations;
//Work Experience
$dataWork = DB::table('users')
->join('persons','persons.id', '=', 'users.person_id')
->leftJoin('practitioners','practitioners.person_id', '=', 'persons.id')
->leftJoin('practitioner_roles','practitioner_roles.practitioner_id', '=', 'practitioners.id')
->leftJoin('organizations','organizations.id', '=', 'practitioner_roles.organization_id')
->where('users.id', '=', $user_id)
->select(
'organizations.name',
'practitioner_roles.period_start',
'practitioner_roles.period_end',
)
->get();
$dataWorkExperience = [];
foreach ($dataWork as $val)
{
$dataWorkExperience[] = [
'name' => $val->name ? $val->name : '',
'period' => $this->fWorkExperience($val->period_start, $val->period_end)
];
}
$res_data['dataWorkExperience'] = $dataWorkExperience;
//STR
$dataStr = [
'str_number' => $dataProfile->str_number,
'exp_date_str' => $dataProfile->exp_date_str ? date('d M Y', strtotime($dataProfile->exp_date_str)) : ''
];
$res_data['dataStr'] = $dataStr;
//SIP
$dataSip = [
'sip_number' => $dataProfile->sip_number,
'exp_date_sip' => $dataProfile->exp_date_sip ? date('d M Y', strtotime($dataProfile->exp_date_sip)) : ''
];
$res_data['dataSip'] = $dataSip;
return ApiResponse::apiResponse("Success", $res_data, trans('message.success'), 200);
}
public function fWorkExperience($start, $end)
{
$startDateString = $start; // Tanggal dan waktu awal
$endDateString = $end ; // Tanggal dan waktu akhir
// Mengubah string tanggal ke timestamp UNIX
$startTime = strtotime($startDateString);
$endTime = strtotime($endDateString);
// Menghitung selisih waktu dalam detik
$timeDifference = $endTime - $startTime;
// Menghitung jumlah tahun, bulan, dan hari dari selisih waktu
$years = floor($timeDifference / (365 * 24 * 60 * 60));
$months = floor(($timeDifference - ($years * 365 * 24 * 60 * 60)) / (30 * 24 * 60 * 60));
$days = floor(($timeDifference - ($years * 365 * 24 * 60 * 60) - ($months * 30 * 24 * 60 * 60)) / (24 * 60 * 60));
// Formatkan hasilnya
$experience = '';
if ($years > 0) {
$experience .= $years . ' years ';
}
if ($months > 0) {
$experience .= $months . ' months ';
}
if ($days > 0) {
$experience .= $days . ' days';
}
return $experience;
}
}