Skip to content

Commit

Permalink
api group lock added
Browse files Browse the repository at this point in the history
  • Loading branch information
hafijul233 committed Oct 20, 2023
1 parent 0bfe871 commit eed98ed
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 7 deletions.
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

Route::middleware(config('fintech.auth.middleware'))->group(function () {
Route::apiResource('users', \Fintech\Auth\Http\Controllers\UserController::class);
Route::post('users/{user}/restore', [\Fintech\Auth\Http\Controllers\UserController::class, 'restore'])->name('users.restore');

Route::apiResource('roles', \Fintech\Auth\Http\Controllers\RoleController::class);
Route::post('roles/{role}/restore', [\Fintech\Auth\Http\Controllers\RoleController::class, 'restore'])->name('roles.restore');
Expand Down
1 change: 0 additions & 1 deletion src/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Fintech\Auth\Http\Resources\UserCollection;
use Fintech\Auth\Http\Resources\UserResource;
use Fintech\Core\Exceptions\DeleteOperationException;
use Fintech\Core\Exceptions\ResourceNotFoundException;
use Fintech\Core\Exceptions\RestoreOperationException;
use Fintech\Core\Exceptions\StoreOperationException;
use Fintech\Core\Exceptions\UpdateOperationException;
Expand Down
69 changes: 68 additions & 1 deletion src/Http/Resources/UserCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fintech\Auth\Http\Resources;

use Fintech\Core\Facades\Core;
use Fintech\Core\Supports\Constant;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
Expand All @@ -16,7 +17,73 @@ class UserCollection extends ResourceCollection
*/
public function toArray($request)
{
return parent::toArray($request);
return $this->collection->map(function ($user) {
$data = [
'parent_id' => $user->parent_id ?? null,
'parent_name' => ($user->parent) ? $user->parent->name : null,
'name' => $user->name ?? null,
'mobile' => $user->mobile ?? null,
'email' => $user->email ?? null,
'login_id' => $user->login_id ?? null,
'wrong_password' => $user->wrong_password ?? null,
'wrong_pin' => $user->wrong_pin ?? null,
'status' => $user->status ?? null,
'language' => $user->language ?? null,
'currency' => $user->currency ?? null,
'app_version' => $user->app_version ?? null,
'remember_token' => $user->remember_token ?? null,
'fcm_token' => $user->fcm_token ?? null,
'roles' => ($user->roles) ? $user->roles : [],
'links' => $user->links,
'created_at' => $user->created_at,
'updated_at' => $user->updated_at,
];

$profile = $user->profile;

$profile_data = [
'profile_photo' => $user->getFirstMediaUrl('profile_data'),
'user_profile_data' => $profile->user_profile_data ?? null,
'id_type' => $profile->id_type ?? null,
'id_no' => $profile->id_no ?? null,
'id_issue_country' => $profile->id_issue_country ?? null,
'id_expired_at' => $profile->id_expired_at ?? null,
'id_issue_at' => $profile->id_issue_at ?? null,
'id_no_duplicate' => $profile->id_no_duplicate ?? null,
'date_of_birth' => $profile->date_of_birth ?? null,
'address' => $profile->permanent_address ?? null,
'city_id' => $profile->city_id ?? null,
'city_name' => null,
'state_id' => $profile->state_id ?? null,
'state_name' => null,
'country_id' => $profile->country_id ?? null,
'country_name' => null,
'post_code' => $profile->post_code ?? null,
'present_address' => $profile->present_address ?? null,
'present_city_id' => $profile->present_city_id ?? null,
'present_city_name' => null,
'present_state_id' => $profile->present_state_id ?? null,
'present_state_name' => null,
'present_country_id' => $profile->present_country_id ?? null,
'present_country_name' => null,
'present_post_code' => $profile->present_post_code ?? null,
'blacklisted' => $profile->blacklisted ?? null,
'metadata_available' => Core::packageExists('MetaData')
];

if (class_exists(\Fintech\MetaData\Facades\MetaData::class)) {

$profile_data['city_name'] = $profile->city?->name ?? null;
$profile_data['state_name'] = $profile->state?->name ?? null;
$profile_data['country_name'] = $profile->country?->name ?? null;
$profile_data['present_city_name'] = $profile->presentCity?->name ?? null;
$profile_data['present_state_name'] = $profile->presentState?->name ?? null;
$profile_data['present_country_name'] = $profile->presentCountry?->name ?? null;
}

return array_merge($data, $profile_data);

})->toArray();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Models/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Fintech\Auth\Models;

use Fintech\Auth\Traits\MetaDataRelations;
use Fintech\Core\Traits\AuditableTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Fintech\Auth\Traits\MetaDataRelations;

class Profile extends Model
{
use \Fintech\Core\Traits\AuditableTrait;
use AuditableTrait;
use SoftDeletes;
use MetaDataRelations;

Expand Down
69 changes: 68 additions & 1 deletion src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@
namespace Fintech\Auth\Models;

use Fintech\Core\Traits\AuditableTrait;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Image\Manipulations;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\Permission\Traits\HasRoles;

/**
* Class User
* @package Fintech\Auth\Models
* @method getTeamIdFromToken()
*/
class User extends Authenticatable
class User extends Authenticatable implements HasMedia

Check failure on line 23 in src/Models/User.php

View workflow job for this annotation

GitHub Actions / P8.1 - L10.* - prefer-stable - ubuntu-latest

Trait "Spatie\MediaLibrary\InteractsWithMedia" not found
{
use HasApiTokens;
use HasRoles;
use AuditableTrait;
use SoftDeletes;
use Notifiable;
use InteractsWithMedia;

/*
|--------------------------------------------------------------------------
Expand All @@ -42,6 +48,10 @@ class User extends Authenticatable

protected $hidden = ['creator_id', 'editor_id', 'destroyer_id', 'restorer_id'];

protected $appends = ['links'];

protected $files = ['profile_photo'];

/*
|--------------------------------------------------------------------------
| FUNCTIONS
Expand All @@ -60,12 +70,44 @@ public function authField()

}

public function registerMediaConversions(Media $media = null): void
{
$this
->addMediaConversion('preview')
->fit(Manipulations::FIT_CROP, 300, 300)
->nonQueued();
}

public function registerMediaCollections(): void
{
$this
->addMediaCollection('profile_photo')
->acceptsMimeTypes(['image/jpeg', 'image/png', 'image/gif', 'image/jpg', 'image/svg+xml'])
->useFallbackUrl('/images/anonymous-user.jpg')
->useFallbackPath(storage_path('/app/public/images/anonymous-user.jpg'))
->useFallbackUrl('/images/anonymous-user.jpg', 'thumb')
->useFallbackPath(storage_path('/app/public/images/anonymous-user.jpg'), 'thumb')
->useDisk('public')
->singleFile()
->registerMediaConversions(function (Media $media) {
$this
->addMediaConversion('thumb')
->width(128)
->height(128);
});
}

/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/

public function parent(): BelongsTo
{
return $this->belongsTo(self::class, 'parent_id', 'id');
}

public function profile(): HasOne
{
return $this->hasOne(config('fintech.auth.user_profile_model'));
Expand All @@ -83,6 +125,31 @@ public function profile(): HasOne
|--------------------------------------------------------------------------
*/

/**
* return all resource link for this object
*
* @return array[]
*/
public function getLinksAttribute()
{
$primaryKey = $this->getKey();

$links = [
'show' => action_link(route('auth.users.show', $primaryKey), __('core::messages.action.show'), 'get'),
'update' => action_link(route('auth.users.update', $primaryKey), __('core::messages.action.update'), 'put'),
'destroy' => action_link(route('auth.users.destroy', $primaryKey), __('core::messages.action.destroy'), 'delete'),
'restore' => action_link(route('auth.users.restore', $primaryKey), __('core::messages.action.restore'), 'post'),
];

if ($this->getAttribute('deleted_at') == null) {
unset($links['restore']);
} else {
unset($links['destroy']);
}

return $links;
}

/*
|--------------------------------------------------------------------------
| MUTATORS
Expand Down
1 change: 0 additions & 1 deletion tests/Feature/PermissionTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use Illuminate\Support\Str;

use function Pest\Laravel\deleteJson;
use function Pest\Laravel\getJson;
use function Pest\Laravel\postJson;
Expand Down
1 change: 0 additions & 1 deletion tests/Feature/RoleTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use Illuminate\Support\Str;

use function Pest\Laravel\deleteJson;
use function Pest\Laravel\getJson;
use function Pest\Laravel\postJson;
Expand Down

0 comments on commit eed98ed

Please sign in to comment.