api person create & update & add family

This commit is contained in:
Muhammad Fajar
2022-11-01 14:41:59 +07:00
parent a34fb34180
commit ba0023544f
10 changed files with 345 additions and 132 deletions

View File

@@ -29,6 +29,16 @@ class AuthController extends Controller
'otp' => rand(1000, 9999),
'otp_created_at' => now()
]);
$person = Person::query()->create([
'owner_user_id' => $user->id,
'email' => $request->phone_or_email,
'created_by' => $user->id,
]);
User::query()->find($user->id)->update([
'person_id' => $person->id
]);
} else {
$user = User::updateOrCreate([
'phone' => $request->phone_or_email
@@ -37,6 +47,16 @@ class AuthController extends Controller
'otp' => rand(1000, 9999),
'otp_created_at' => now()
]);
$person = Person::query()->create([
'owner_user_id' => $user->id,
'phone' => $request->phone_or_email,
'created_by' => $user->id,
]);
User::query()->find($user->id)->update([
'person_id' => $person->id
]);
}
if (!$user) {
@@ -57,6 +77,7 @@ class AuthController extends Controller
return response()->json([
'message' => 'OTP Terkirim',
'data' => [
'otp' => $user->otp,
'otp_valid_until' => $user->otp_created_at->addMinutes(config('linksehat.otp_valid_minutes'))
]
]);
@@ -181,16 +202,11 @@ class AuthController extends Controller
return Socialite::driver($provider)->redirect();
}
public function handleSocialLoginCallback(Request $request, $provider)
public function handleSocialLoginCallback($provider)
{
// get the provider's user. (In the provider server)
$providerUser = Socialite::driver($provider)->user();
$providerUser = Socialite::driver($provider)->stateless()->user();
$user = User::query()->firstWhere('email', $providerUser->email);
// check if access token exists etc..
// search for a user in our server with the specified provider id and provider name
$user = User::where('email', $providerUser->email)->first();
// // if there is no record with these data, create a new user
if (!$user) {
$user = User::query()->create([
'email' => $providerUser->email,
@@ -200,6 +216,7 @@ class AuthController extends Controller
'owner_user_id' => $user->id,
'name' => $providerUser->name,
'email' => $providerUser->email,
'created_by' => $user->id,
]);
User::query()->find($user->id)->update([
@@ -207,7 +224,6 @@ class AuthController extends Controller
]);
}
// // return the token for usage
return response([
'message' => 'Selamat Datang',
'user' => UserProfileResource::make($user),

View File

@@ -2,11 +2,14 @@
namespace Modules\Linksehat\Http\Controllers\Api;
use App\Models\PractitionerRole;
use App\Models\File;
use App\Models\Person;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Linksehat\Transformers\Speciality\SpecialityResource;
use Modules\Linksehat\Http\Requests\PersonRequest;
use Modules\Linksehat\Transformers\Person\PersonResource;
use Symfony\Component\HttpFoundation\Response;
class PersonController extends Controller
{
@@ -14,69 +17,11 @@ class PersonController extends Controller
* Display a listing of the resource.
* @return Renderable
*/
public function index(Request $request)
public function index()
{
$organizationId = $request->organization_id;
$specialityId = $request->speciality_id;
$personModel = Person::query()->with('avatar')->where('owner_user_id', auth()->user()->id)->get();
if (empty($organizationId) || empty($specialityId)) {
$messageorganizationId = !empty($organizationId) ? ' ' : ' organization_id or ';
$messageSpecialityId = !empty($specialityId) ? ' ' : 'speciality_id';
abort(400, 'missing parameter' . $messageorganizationId . $messageSpecialityId);
}
$doctors = PractitionerRole::query()->with(['practitioner.person', 'speciality'])->where('organization_id', $organizationId)->where('speciality_id', $specialityId)->get();
foreach ($doctors as $key => $doctor) {
$specialisName = $doctor->speciality->name;
}
// Price belum ke ambil
return response()->json([
'status' => true,
'statusCode' => 200,
'message' => 'Data Berhasil di ambil',
'data' => [
'title' => 'Spesialis ' . $specialisName,
'doctors' => SpecialityResource::collection($doctors)
]
]);
}
public function searchSpecialityOrPerson(Request $request)
{
$doctors = PractitionerRole::query()
->with(['practitioner.person', 'speciality'])
->whereHas('practitioner.person', function ($query) use ($request) {
$query->where('name', 'LIKE', "%{$request->value}%");
})
// ->whereHas('speciality', function ($query) use ($request) {
// $query->where('name', 'LIKE', "%{$request->value}%");
// })
->get();
return $doctors;
// Price belum ke ambil
// return response()->json([
// 'status' => true,
// 'statusCode' => 200,
// 'message' => 'Data Berhasil di ambil',
// 'data' => [
// 'title' => 'Spesialis ' . $specialisName,
// 'doctors' => SpecialityResource::collection($doctors)
// ]
// ]);
}
/**
* Show the form for creating a new resource.
* @return Renderable
*/
public function create()
{
//
return $personModel;
}
/**
@@ -84,9 +29,21 @@ class PersonController extends Controller
* @param Request $request
* @return Renderable
*/
public function store(Request $request)
public function store()
{
//
$personModel = Person::query()->create([
'owner_user_id' => auth()->user()->id,
'created_by' => auth()->user()->id,
]);
return response()->json([
'status' => 'success',
'statusCode' => Response::HTTP_CREATED,
'message' => 'Data Berhasil di buat',
'data' => [
'person' => $personModel,
],
], Response::HTTP_CREATED);
}
/**
@@ -94,19 +51,16 @@ class PersonController extends Controller
* @param int $id
* @return Renderable
*/
public function show(Request $request, $id)
public function show(Person $person)
{
//
}
/**
* Show the form for editing the specified resource.
* @param int $id
* @return Renderable
*/
public function edit($id)
{
//
return response()->json([
'status' => 'success',
'statusCode' => Response::HTTP_OK,
'message' => 'Data Person dengan nama ' . $person->name . ' berhasil di ambil',
'data' => [
'person' => new PersonResource($person),
],
]);
}
/**
@@ -115,9 +69,55 @@ class PersonController extends Controller
* @param int $id
* @return Renderable
*/
public function update(Request $request, $id)
public function update(PersonRequest $request, Person $person)
{
//
$person->update($request->validated());
if ($request->hasFile('user_avatar')) {
$pathFileAvatar = File::storeFile('avatar', $person->id, $request->file('user_avatar'));
$person->files()->updateOrCreate([
'type' => 'avatar',
'name' => File::getFileName('avatar', $person->id, $request->file('user_avatar')),
'extension' => $request->file('user_avatar')->getClientOriginalExtension(),
'path' => $pathFileAvatar,
'created_by' => auth()->user()->id,
'updated_by' => auth()->user()->id,
]);
}
if ($request->hasFile('verification_file')) {
$pathFileVerification = File::storeFile('dataDiri', $person->id, $request->file('verification_file'));
$person->files()->updateOrCreate([
'type' => 'dataDiri',
'name' => File::getFileName('dataDiri', $person->id, $request->file('verification_file')),
'extension' => $request->file('verification_file')->getClientOriginalExtension(),
'path' => $pathFileVerification,
'created_by' => auth()->user()->id,
'updated_by' => auth()->user()->id,
]);
}
if ($request->has('relation_with_owner')) {
$person->familyOwner()->updateOrCreate([
'owner_id' => auth()->user()->person_id,
'person_id' => $person->id,
], [
'owner_id' => auth()->user()->person_id,
'relation_with_owner' => $request->relation_with_owner,
'person_id' => $person->id,
'created_by' => auth()->user()->id,
'updated_by' => auth()->user()->id,
]);
}
return response()->json([
'status' => 'success',
'statusCode' => Response::HTTP_OK,
'message' => 'Data Berhasil di update',
'data' => [
'person' => $person,
],
]);
}
/**

View File

@@ -0,0 +1,65 @@
<?php
namespace Modules\Linksehat\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PersonRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'name' => 'string',
'birth_place' => 'string',
'birth_date' => 'date',
'gender' => 'string',
'phone' => 'numeric',
'email' => 'email',
'blood_type' => 'string',
'weight' => 'string',
'height' => 'string',
'relation_with_owner' => 'string', // relasi family table
'marital_status' => 'string',
'religion' => 'string',
'last_education' => 'string',
'current_employment' => 'string',
'nik' => 'string|min:16|max:16',
'updated_by' => 'integer',
'user_avatar' => 'file', // relasi file table
'verification_file' => 'file', // relasi file table
'is_ktp' => 'boolean',
'citizenship' => 'string',
];
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation()
{
$citizenship = $this->is_ktp ? 'wni' : 'wna';
$this->merge([
'birth_date' => empty($this->birth_date) ? $this->birth_date . " 00:00:00" : $this->birth_date,
'updated_by' => auth()->user()->id,
'citizenship' => $citizenship ? $citizenship : NULL
]);
}
}

View File

@@ -4,6 +4,7 @@ use Modules\Linksehat\Http\Controllers\Api\AuthController;
use Modules\Linksehat\Http\Controllers\Api\DashboardController;
use Modules\Linksehat\Http\Controllers\Api\DoctorController;
use Modules\Linksehat\Http\Controllers\Api\HospitalController;
use Modules\Linksehat\Http\Controllers\Api\PersonController;
use Modules\Linksehat\Http\Controllers\Api\ProfileController;
use Modules\Linksehat\Http\Controllers\Api\SpecialityController;
@@ -51,5 +52,6 @@ Route::prefix('linksehat')->group(function () {
Route::middleware('auth:sanctum')->group(function () {
Route::get('profile', [ProfileController::class, 'index'])->name('profile');
Route::post('profile', [ProfileController::class, 'update'])->name('profile.update');
Route::apiResource('persons', PersonController::class)->except(['destroy']);
});
});

View File

@@ -0,0 +1,40 @@
<?php
namespace Modules\Linksehat\Transformers\Person;
use App\Helpers\Helper;
use Illuminate\Http\Resources\Json\JsonResource;
class PersonResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'avatar' => [
'title' => $this->avatar->name ?? '',
'url' => url($this->avatar->path) ?? '',
],
'id' => $this->id,
'name' => $this->name,
'birth_place' => $this->birth_place,
'gender' => $this->gender,
'phone' => $this->phone,
'email' => $this->email,
'blood_type' => $this->blood_type,
'weight' => $this->weight,
'height' => $this->height,
'relation_with_owner' => $this->relation_with_owner,
'marital_status' => $this->marital_status,
'last_education' => $this->last_education,
'current_employment' => $this->current_employment,
'nik' => $this->nik,
'religion' => $this->religion,
];
}
}

22
app/Models/Family.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Family extends Model
{
use HasFactory;
protected $table = 'family_relations';
protected $fillable = [
'owner_id',
'relation_with_owner',
'person_id',
'created_by',
'updated_by',
'deleted_by',
];
}

View File

@@ -10,7 +10,7 @@ use Illuminate\Support\Str;
class File extends Model
{
use HasFactory;
protected $fillable = [
'fileable_type',
'fileable_id',
@@ -20,12 +20,10 @@ class File extends Model
'path',
];
protected $appends = [
'url'
];
public static $file_directories = [
'import-temp' => 'import-temp/',
'avatar' => 'user-avatar/',
'dataDiri' => 'data-diri/'
];
public function fileable()
@@ -33,29 +31,23 @@ class File extends Model
return $this->morphTo();
}
public function getUrlAttribute()
{
return Storage::url($this->path);
}
public function getDirectory($type)
public static function getDirectory($type)
{
return self::$file_directories[$type] ?? 'any';
}
public static function getFileName($type, $id, $file)
public static function getFileName($type, $id)
{
$extension = $file->getClientOriginalExtension();
return $type . '-' . $id .'-'.Str::random(10).'.'.$extension;
return $type . '-' . $id . '-' . Str::random(10);
}
public static function storeFile($type, $id, $file)
{
$fileName = self::getFileName($type, $id, $file);
$fileName = self::getFileName($type, $id);
$directory = self::getDirectory($type);
$path = $directory . $fileName;
$file->storeAs($directory, $fileName);
$extension = $file->getClientOriginalExtension();
$path = $directory . $fileName . '.' . $extension;
$file->storeAs('public/' . $directory, $fileName . '.' . $extension);
return $path;
}
}

View File

@@ -10,20 +10,33 @@ class Person extends Model
use HasFactory;
protected $table = 'persons';
protected $fillable = [
'owner_user_id',
'nik',
'name_prefix',
'name',
'name_suffix',
'phone',
'email',
'gender',
'birth_date',
'birth_place',
'citizenship',
'current_employment',
'last_education',
'religion',
'blood_type',
'weight',
'height',
'is_deceased',
'deceased_at',
'marital_status',
'main_address_id',
'domicile_address_id',
'created_by',
'updated_by',
'deleted_by'
];
public function getFullNameAttribute()
@@ -31,11 +44,11 @@ class Person extends Model
$arr = [];
if (!empty($this->name_prefix)) {
$arr[] = $this->name_prefix;
}
}
$arr[] = $this->name;
if (!empty($this->name_suffix)) {
$arr[] = $this->name_suffix;
}
}
return implode(' ', $arr);
}
@@ -44,17 +57,17 @@ class Person extends Model
{
return $this->morphMany(Address::class, 'addressable');
}
public function currentAddress()
{
return $this->belongsTo(Address::class, 'main_address_id');
}
public function domicileAddress()
{
return $this->belongsTo(Address::class, 'main_address_id');
}
public function metas()
{
return $this->morphMany(Meta::class, 'metaable');
@@ -65,26 +78,18 @@ class Person extends Model
return $this->belongsTo(User::class, 'owner_user_id');
}
public function makeLmsApiData()
public function files()
{
return [
'name' => $this->name,
'birth_date' => $this->birth_date,
'birth_place' => $this->birth_place,
'gender' => $this->gender,
'blood_type' => $this->blood_type,
'religion' => $this->religion,
'last_education' => $this->last_education,
'current_employment' => $this->current_employment,
'marital_status' => $this->marital_status,
'nik' => $this->nik,
'citizenship' => $this->citizenship,
'address' => $this->currentAddress->text ?? null,
'city_name' => $this->currentAddress->city->name ?? null,
'domicile_address' => $this->domicileAddress->text ?? null,
'domicile_city_name' => $this->domicileAddress->city->name ?? null,
'email' => $this->email,
'phone' => $this->phone,
];
return $this->morphMany(File::class, 'fileable');
}
public function avatar()
{
return $this->morphOne(File::class, 'fileable')->where('type', 'avatar')->latestOfMany();
}
public function familyOwner()
{
return $this->hasOne(Family::class, 'person_id');
}
}

View File

@@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('family_relations', function (Blueprint $table) {
$table->id();
$table->string('relation_with_owner');
$table->timestamps();
$table->softDeletes();
$table->foreignId('created_by')->nullable();
$table->foreignId('updated_by')->nullable();
$table->foreignId('deleted_by')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('family_relations');
}
};

View File

@@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('family_relations', function (Blueprint $table) {
$table->foreignId('owner_id')->nullable()->after('id');
$table->foreignId('person_id')->nullable()->after('relation_with_owner');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('family_relations', function (Blueprint $table) {
$table->dropColumn('owner_id');
$table->dropColumn('person_id');
});
}
};