Add Notification Token

This commit is contained in:
R
2022-11-22 10:19:37 +07:00
parent 9a0032ae17
commit 05ae09ab4c
6 changed files with 223 additions and 0 deletions

View File

@@ -0,0 +1,106 @@
<?php
namespace Modules\Linksehat\Http\Controllers\Api;
use App\Helpers\Helper;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Symfony\Component\HttpFoundation\Response;
class NotificationTokenController extends Controller
{
/**
* Display a listing of the resource.
* @return Renderable
*/
public function index()
{
return view('linksehat::index');
}
/**
* Show the form for creating a new resource.
* @return Renderable
*/
public function create()
{
return view('linksehat::create');
}
/**
* Store a newly created resource in storage.
* @param Request $request
* @return Renderable
*/
public function store(Request $request)
{
$request->validate([
'origin' => 'required|in:linksehat-app,linksehat-web,linksehat-ios,linkmedis-app,linkmedis-web,linkmedis-ios',
'type' => 'required',
'token' => 'required',
]);
$user = auth()->user();
$user->notificationTokens()->updateOrCreate([
'type' => $request->type,
'token' => $request->token
], [
'origin' => $request->origin,
'type' => $request->type,
'token' => $request->token,
'status' => 'active'
]);
return Helper::responseJson(data: ['tokens' => $user->notificationTokens], message: 'Token Berhasil Ditambah');
}
/**
* Show the specified resource.
* @param int $id
* @return Renderable
*/
public function show($id)
{
return view('linksehat::show');
}
/**
* Show the form for editing the specified resource.
* @param int $id
* @return Renderable
*/
public function edit($id)
{
return view('linksehat::edit');
}
/**
* Update the specified resource in storage.
* @param Request $request
* @param int $id
* @return Renderable
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
* @param int $id
* @return Renderable
*/
public function destroy($id)
{
$user = auth()->user();
$notificationToken = $user->notificationTokens()->findOrFail($id);
if ($notificationToken->delete())
{
return Helper::responseJson(data: ['tokens' => $user->notificationTokens], message: 'Token Berhasil Dihapus');
}
return Helper::responseJson(data: [], message: 'Something Went Wrong', statusCode: Response::HTTP_NOT_MODIFIED);
}
}

View File

@@ -5,6 +5,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\NotificationTokenController;
use Modules\Linksehat\Http\Controllers\Api\PersonController;
use Modules\Linksehat\Http\Controllers\Api\ProfileController;
use Modules\Linksehat\Http\Controllers\Api\SearchController;
@@ -60,6 +61,8 @@ 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::post('notification-tokens/delete/{id}', [NotificationTokenController::class, 'destroy'])->name('profile.delete.token');
Route::post('notification-tokens', [NotificationTokenController::class, 'store'])->name('profile.store.token');
Route::apiResource('appointment', AppointmentController::class);
Route::apiResource('families', PersonController::class)->except(['destroy']);
});

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class NotificationToken extends Model
{
use HasFactory;
protected $fillable = [
'origin',
'type',
'token',
'status',
];
protected $hidden = [
'notifiabletoken_type',
'notifiabletoken_id',
'created_at',
'updated_at'
];
public function notifiabletoken()
{
return $this->morphTo();
}
}

View File

@@ -84,4 +84,9 @@ class User extends Authenticatable
{
return $this->hasMany(Person::class, 'owner_user_id');
}
public function notificationTokens()
{
return $this->morphMany(NotificationToken::class, 'notifiabletoken');
}
}

View File

@@ -0,0 +1,36 @@
<?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('notification_tokens', function (Blueprint $table) {
$table->id();
$table->morphs('notifiabletoken', 'notifiabletoken');
$table->string('origin');
$table->string('type');
$table->string('token');
$table->string('status');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('notification_tokens');
}
};

View File

@@ -0,0 +1,43 @@
<?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('api_logs', function (Blueprint $table) {
$table->id();
$table->string('type')->comment('in or out');
$table->string('context')->nullable();
$table->string('target')->comment('url or path that got hit');
$table->text('request');
$table->text('response')->nullable();
$table->timestamps();
$table->softDeletes();
$table->unsignedBigInteger('created_by')->nullable()->index();
$table->unsignedBigInteger('updated_by')->nullable()->index();
$table->unsignedBigInteger('deleted_by')->nullable()->index();
$table->index('token');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('api_logs');
}
};