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 120ccff + 7a11701 commit facc2f1
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@ VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

TELEGRAM_BOT_TOKEN=
TELEGRAM_SECRET_KEY=

FRONTEND_URL=https://localhost:3000
14 changes: 14 additions & 0 deletions app/Filament/Pages/Dashboard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Filament\Pages;

use Filament\Pages\BasePage;
use Filament\Panel;

class Dashboard extends BasePage
{
public function panel(Panel $panel): Panel
{
return $panel->pages([]);
}
}
11 changes: 8 additions & 3 deletions app/Filament/Resources/ChallengeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,26 @@ public static function form(Form $form): Form
])
->disabled(
function ($record) {
if ($record === null) {
return false;
}
return $record->teams->count() > 0 || $record->users->count() > 0;
}
)
->required()
->maxWidth('sm'),
Forms\Components\Textarea::make('description')
->label('Описание')
->required()
->rows(5),
Forms\Components\Section::make()->schema([
DateTimePicker::make('start_date')
->label('Дата начала')
->required()
->default(now()),
DateTimePicker::make('end_date')
->label('Дата окончания')
->nullable(),
->required(),
])->columns(),
Forms\Components\Textarea::make('result')
->label('Результаты завершения челленджа')
Expand All @@ -84,7 +89,7 @@ function ($record) {
->visibility('public')
->maxWidth('xs')
->label('Изображение'),
Forms\Components\Section::make()
$record !== null ? Forms\Components\Section::make()
->schema([
$record->type === ChallengeType::PERSONAL->value ? Forms\Components\Select::make( 'users')
->label('Участники челленджа')
Expand All @@ -96,7 +101,7 @@ function ($record) {
->relationship('teams', 'name')
->preload()
->multiple(),
])->columns(2),
])->columns(2) : new Forms\Components\Section(),
])->columns(1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Filament\Resources\ChallengeResource\Pages;

use App\Filament\Resources\ChallengeResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;

class CreateChallenge extends CreateRecord
Expand Down
14 changes: 14 additions & 0 deletions app/Filament/Resources/ChallengeResource/Pages/EditChallenge.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Filament\Resources\ChallengeResource\Pages;

use App\Filament\Resources\ChallengeResource;
use App\Http\Controllers\Api\TelegramController;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

Expand All @@ -19,6 +20,19 @@ protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
Actions\Action::make('Отправить уведомления')
->action('sendNotification')
->color('success'),
];
}

public function sendNotification(TelegramController $controller): void
{
$record = $this->form->getRecord();

$url = env('FRONTEND_URL') . "/challenges/{$record->id}";
$message = "Создан новый челлендж: {$record->name}! \nСкорее присоединяйся по ссылке {$url}";

$controller->sendMessageForAll($message);
}
}
51 changes: 51 additions & 0 deletions app/Filament/Widgets/StatsOverview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Filament\Widgets;

use App\Models\User;
use Carbon\Carbon;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Illuminate\Support\Facades\DB;

class StatsOverview extends BaseWidget
{
protected function getStats(): array
{
return [
Stat::make('Новых пользователей за этот месяц',
User::query()->where('created_at', '>=', Carbon::now()->startOfMonth())->count())
->chart(self::getCountUsersRegisteredForLastMonth())
->color('success'),
Stat::make(
'Активных пользователей',
User::query()->where('is_confirmed', true)->count()
),
Stat::make(
'Всего пользователей',
User::query()->count()
),
];
}

private function getCountUsersRegisteredForLastMonth(): array
{
$results = [];
$startTime = Carbon::now()->startOfMonth();
$daysLeft = $startTime->diffInDays(Carbon::now());

for ($i = 0; $i < $daysLeft; $i++) {
$endTime = $startTime->copy()->addDay();
$count = DB::table('users')
->whereBetween('created_at', [
$startTime,
$endTime,
])
->count();
$results[] = $count;
$startTime = $endTime;
}

return $results;
}
}
30 changes: 30 additions & 0 deletions app/Http/Controllers/Api/TelegramController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,34 @@ public function syncTelegram(Request $request): JsonResponse
return response()->json(['message' => 'error'], 404);
}
}

public function sendMessageForAll(string $message)
{
$users = User::query()
->whereNotNull('telegram_id')
->get();

foreach ($users as $user) {
$this->sendMessage($user->telegram_id, $message);
}
}

public function sendMessage(string $telegramId, string $message)
{
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_URL => 'https://api.telegram.org/bot' . env('TELEGRAM_BOT_TOKEN') . '/sendMessage',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 10,
CURLOPT_POSTFIELDS => array(
'chat_id' => $telegramId,
'text' => $message,
),
)
);
curl_exec($ch);
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"laravel/framework": "^11.9",
"laravel/sanctum": "^4.0",
"laravel/tinker": "^2.9",
"spatie/laravel-data": "^4.11"
"spatie/laravel-data": "^4.11",
"ext-curl": "*"
},
"require-dev": {
"fakerphp/faker": "^1.23",
Expand Down
34 changes: 34 additions & 0 deletions config/cors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Cross-Origin Resource Sharing (CORS) Configuration
|--------------------------------------------------------------------------
|
| Here you may configure your settings for cross-origin resource sharing
| or "CORS". This determines what cross-origin operations may execute
| in web browsers. You are free to adjust these settings as needed.
|
| To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
|
*/

'paths' => ['api/*', 'sanctum/csrf-cookie'],

'allowed_methods' => ['*'],

'allowed_origins' => ['*'],

'allowed_origins_patterns' => [],

'allowed_headers' => ['*'],

'exposed_headers' => [],

'max_age' => 0,

'supports_credentials' => false,

];

0 comments on commit facc2f1

Please sign in to comment.