Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
deniskorbakov committed Oct 27, 2024
2 parents c8de365 + d9410de commit 9794f21
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

TELEGRAM_BOT_TOKEN=
TELEGRAM_SECRET_KEY=
35 changes: 35 additions & 0 deletions app/Http/Controllers/Api/TelegramController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class TelegramController
{
public function syncTelegram(Request $request): JsonResponse
{
$request->validate([
'telegram_id' => 'required|string',
'telegram_username' => 'required|string',
'secret_key' => 'required|string',
]);

if ($request->secret_key !== env('TELEGRAM_SECRET_KEY')) {
return response()->json(['message' => 'error'], 403);
}

$success = (bool) User::query()
->where('telegram_username', $request->post('telegram_username'))
->update([
'telegram_id' => $request->post('telegram_id'),
]);

if ($success) {
return response()->json(['message' => 'success']);
} else {
return response()->json(['message' => 'error'], 404);
}
}
}
4 changes: 3 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class User extends Authenticatable implements FilamentUser
'about',
'image',
'is_admin',
'is_confirmed'
'is_confirmed',
'telegram_username',
'telegram_id',
];

public function challenges(): BelongsToMany
Expand Down
2 changes: 2 additions & 0 deletions database/migrations/2024_10_25_213720_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function up(): void
$table->string('image')->nullable();
$table->boolean('is_admin')->default(false);
$table->boolean('is_confirmed')->default(false);
$table->string('telegram_username')->unique()->nullable();
$table->string('telegram_id')->unique()->nullable();
$table->rememberToken();
$table->timestamps();
});
Expand Down
3 changes: 3 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\TeamController;
use App\Http\Controllers\Api\TelegramController;
use App\Http\Controllers\Api\UserController;
use Illuminate\Support\Facades\Route;

Expand All @@ -27,3 +28,5 @@
Route::get('/{id}/achievements', [TeamController::class, 'achievements'])->name('teams.achievements');
});
});

Route::post('sync-telegram', [TelegramController::class, 'syncTelegram'])->name('auth.syncTelegram');

0 comments on commit 9794f21

Please sign in to comment.