[WIP] Fix Person
This commit is contained in:
@@ -19,9 +19,15 @@ class PersonController extends Controller
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$personModel = Person::query()->with('avatar')->where('owner_user_id', auth()->user()->id)->get();
|
||||
$user = auth()->user();
|
||||
$user->load(['person', 'person.families', 'person.avatar']);
|
||||
|
||||
return $personModel;
|
||||
$families = $user->person->families;
|
||||
$currentUserPerson = $user->person;
|
||||
unset($currentUserPerson->families);
|
||||
$families->prepend($currentUserPerson);
|
||||
|
||||
return PersonResource::collection($families);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,12 +35,37 @@ class PersonController extends Controller
|
||||
* @param Request $request
|
||||
* @return Renderable
|
||||
*/
|
||||
public function store()
|
||||
public function store(Request $request)
|
||||
{
|
||||
$personModel = Person::query()->create([
|
||||
'owner_user_id' => auth()->user()->id,
|
||||
'created_by' => auth()->user()->id,
|
||||
$request->validate([]);
|
||||
|
||||
$personData = $request->only([
|
||||
'owner_user_id',
|
||||
'nik',
|
||||
'name_prefix',
|
||||
'name',
|
||||
'name_suffix',
|
||||
'phone',
|
||||
'email',
|
||||
'gender',
|
||||
'birth_date',
|
||||
'birth_place',
|
||||
'citizenship',
|
||||
'current_employment',
|
||||
'last_education',
|
||||
'religion',
|
||||
'blood_type',
|
||||
'is_deceased',
|
||||
'deceased_at',
|
||||
'marital_status',
|
||||
'main_address_id',
|
||||
'domicile_address_id',
|
||||
]);
|
||||
$personData = array_merge($personData, [
|
||||
'owner_user_id' => auth()->user()->id,
|
||||
]);
|
||||
|
||||
$personModel = Person::query()->create($personData);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
@@ -71,7 +102,29 @@ class PersonController extends Controller
|
||||
*/
|
||||
public function update(PersonRequest $request, Person $person)
|
||||
{
|
||||
$person->update($request->validated());
|
||||
$personData = $request->only([
|
||||
'owner_user_id',
|
||||
'nik',
|
||||
'name_prefix',
|
||||
'name',
|
||||
'name_suffix',
|
||||
'phone',
|
||||
'email',
|
||||
'gender',
|
||||
'birth_date',
|
||||
'birth_place',
|
||||
'citizenship',
|
||||
'current_employment',
|
||||
'last_education',
|
||||
'religion',
|
||||
'blood_type',
|
||||
'is_deceased',
|
||||
'deceased_at',
|
||||
'marital_status',
|
||||
'main_address_id',
|
||||
'domicile_address_id',
|
||||
]);
|
||||
$person->update($personData);
|
||||
|
||||
if ($request->hasFile('user_avatar')) {
|
||||
$pathFileAvatar = File::storeFile('avatar', $person->id, $request->file('user_avatar'));
|
||||
|
||||
@@ -16,10 +16,6 @@ class PersonResource extends JsonResource
|
||||
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,
|
||||
@@ -29,12 +25,13 @@ class PersonResource extends JsonResource
|
||||
'blood_type' => $this->blood_type,
|
||||
'weight' => $this->weight,
|
||||
'height' => $this->height,
|
||||
'relation_with_owner' => $this->relation_with_owner,
|
||||
'relation_with_owner' => ($this->user && $this->user->person_id == $this->id) ? 'Self' : $this->pivot->relation_with_owner ?? null,
|
||||
'marital_status' => $this->marital_status,
|
||||
'last_education' => $this->last_education,
|
||||
'current_employment' => $this->current_employment,
|
||||
'nik' => $this->nik,
|
||||
'religion' => $this->religion,
|
||||
'avatar_url' => $this->avatar->url ?? asset('images/default-doctor-avatar.png'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,10 @@ class File extends Model
|
||||
'path',
|
||||
];
|
||||
|
||||
public $appends = [
|
||||
'url'
|
||||
];
|
||||
|
||||
public static $file_directories = [
|
||||
'import-temp' => 'import-temp/',
|
||||
'avatar' => 'user-avatar/',
|
||||
@@ -41,6 +45,11 @@ class File extends Model
|
||||
return $type . '-' . $id . '-' . Str::random(10);
|
||||
}
|
||||
|
||||
public function getUrlAttribute()
|
||||
{
|
||||
return url($this->path);
|
||||
}
|
||||
|
||||
public static function storeFile($type, $id, $file)
|
||||
{
|
||||
$fileName = self::getFileName($type, $id);
|
||||
|
||||
@@ -88,8 +88,18 @@ class Person extends Model
|
||||
return $this->morphOne(File::class, 'fileable')->where('type', 'avatar')->latestOfMany();
|
||||
}
|
||||
|
||||
public function families()
|
||||
{
|
||||
return $this->belongsToMany(Person::class, 'family_relations', 'owner_id', 'person_id')->withPivot(['relation_with_owner']);
|
||||
}
|
||||
|
||||
public function familyOwner()
|
||||
{
|
||||
return $this->hasOne(Family::class, 'person_id');
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->hasOne(User::class, 'person_id');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,10 @@ class User extends Authenticatable
|
||||
'otp_created_at' => 'datetime'
|
||||
];
|
||||
|
||||
public $with = [
|
||||
'metas'
|
||||
];
|
||||
|
||||
public $appends = [
|
||||
'meta'
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user