From 05ae09ab4c56c24e905dd2e604cb03e818f409dc Mon Sep 17 00:00:00 2001 From: R Date: Tue, 22 Nov 2022 10:19:37 +0700 Subject: [PATCH] Add Notification Token --- .../Api/NotificationTokenController.php | 106 ++++++++++++++++++ Modules/Linksehat/Routes/api.php | 3 + app/Models/NotificationToken.php | 30 +++++ app/Models/User.php | 5 + ...83926_create_notification_tokens_table.php | 36 ++++++ ...022_11_22_093749_create_api_logs_table.php | 43 +++++++ 6 files changed, 223 insertions(+) create mode 100644 Modules/Linksehat/Http/Controllers/Api/NotificationTokenController.php create mode 100644 app/Models/NotificationToken.php create mode 100644 database/migrations/2022_11_22_083926_create_notification_tokens_table.php create mode 100644 database/migrations/2022_11_22_093749_create_api_logs_table.php diff --git a/Modules/Linksehat/Http/Controllers/Api/NotificationTokenController.php b/Modules/Linksehat/Http/Controllers/Api/NotificationTokenController.php new file mode 100644 index 00000000..e419a108 --- /dev/null +++ b/Modules/Linksehat/Http/Controllers/Api/NotificationTokenController.php @@ -0,0 +1,106 @@ +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); + } +} diff --git a/Modules/Linksehat/Routes/api.php b/Modules/Linksehat/Routes/api.php index c39a2a3b..cc4a1082 100755 --- a/Modules/Linksehat/Routes/api.php +++ b/Modules/Linksehat/Routes/api.php @@ -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']); }); diff --git a/app/Models/NotificationToken.php b/app/Models/NotificationToken.php new file mode 100644 index 00000000..7979b36d --- /dev/null +++ b/app/Models/NotificationToken.php @@ -0,0 +1,30 @@ +morphTo(); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index d13f367c..fd6398e7 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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'); + } } diff --git a/database/migrations/2022_11_22_083926_create_notification_tokens_table.php b/database/migrations/2022_11_22_083926_create_notification_tokens_table.php new file mode 100644 index 00000000..28d3c1a0 --- /dev/null +++ b/database/migrations/2022_11_22_083926_create_notification_tokens_table.php @@ -0,0 +1,36 @@ +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'); + } +}; diff --git a/database/migrations/2022_11_22_093749_create_api_logs_table.php b/database/migrations/2022_11_22_093749_create_api_logs_table.php new file mode 100644 index 00000000..c7333322 --- /dev/null +++ b/database/migrations/2022_11_22_093749_create_api_logs_table.php @@ -0,0 +1,43 @@ +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'); + } +};