Compare commits
2 Commits
d49c43f209
...
ceb4f36ba1
| Author | SHA1 | Date | |
|---|---|---|---|
| ceb4f36ba1 | |||
| b3784b77b1 |
@ -3,36 +3,33 @@
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Requests\StoreUserRequest;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Throwable;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function registerUser(StoreUserRequest $request)
|
||||
{
|
||||
$data = $request->validated();
|
||||
$data['password'] = Str::random(8);
|
||||
|
||||
$validated = $request->validated();
|
||||
$validated['is_admin'] = false;
|
||||
$validated['password'] = Str::random(8);
|
||||
$validated['status'] = true;
|
||||
try {
|
||||
$user = User::create($validated);
|
||||
}catch (Throwable $exception){
|
||||
$user = User::create($data);
|
||||
} catch (Throwable $exception) {
|
||||
return response()
|
||||
->json($exception);
|
||||
->json($exception);
|
||||
}
|
||||
|
||||
|
||||
return response()
|
||||
->json(['message' => 'Successfully created user ' . $user->firstname]);
|
||||
->json(['message' => 'Successfully created user ' . $user->firstname]);
|
||||
}
|
||||
|
||||
public function getUser(Request $request, User $user)
|
||||
{
|
||||
return response()
|
||||
->json(["status" => true]);
|
||||
->json(['status' => true]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Log;
|
||||
use App\Models\Quote;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
@ -12,6 +13,7 @@ class WebHookController extends Controller
|
||||
{
|
||||
public function webHookSend(string $payload)
|
||||
{
|
||||
// TODO: move this to a helper class so we can reuse code between API and FE
|
||||
$data = ['text' => $payload];
|
||||
$response = Http::post(config('bot.webhook'), $data);
|
||||
}
|
||||
@ -19,17 +21,30 @@ class WebHookController extends Controller
|
||||
public function sendQuote(Request $request)
|
||||
{
|
||||
$quote = $request->input('quote');
|
||||
|
||||
if (empty($quote)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'content' => "Quote sent. {$quote}",
|
||||
]);
|
||||
|
||||
$this->webHookSend($quote);
|
||||
}
|
||||
|
||||
public function sendRandomQuote()
|
||||
{
|
||||
$quote = Quote::inRandomOrder()->first()->quote;
|
||||
$this->webHookSend($quote);
|
||||
$quote = Quote::inRandomOrder()->first();
|
||||
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'quote_id' => $quote->id,
|
||||
'content' => 'Quote sent.',
|
||||
]);
|
||||
|
||||
$this->webHookSend($quote->quote);
|
||||
}
|
||||
|
||||
public function test()
|
||||
|
||||
18
app/Models/Log.php
Normal file
18
app/Models/Log.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @mixin IdeHelperLog
|
||||
*/
|
||||
class Log extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'quote_id',
|
||||
'requested_quote_id',
|
||||
'content',
|
||||
];
|
||||
}
|
||||
@ -14,11 +14,37 @@ class Quote extends Model
|
||||
|
||||
public function request(): void
|
||||
{
|
||||
// Send the notification
|
||||
// Send the quote
|
||||
// If success, add it to the transactions
|
||||
}
|
||||
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'quote_id' => $this->id,
|
||||
'content' => 'Quote requested.'
|
||||
]);
|
||||
}
|
||||
protected $fillable = [
|
||||
'quote',
|
||||
];
|
||||
|
||||
public static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
self::created(function ($model) {
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'quote_id' => $model->id,
|
||||
'content' => 'Quote created.'
|
||||
]);
|
||||
});
|
||||
|
||||
self::deleted(function ($model) {
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'quote_id' => $model->id,
|
||||
'content' => 'Quote deleted.'
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,7 +19,12 @@ class RequestedQuote extends Model
|
||||
|
||||
public function approve(): void
|
||||
{
|
||||
// TODO: we'll probs want to log who approved this quote
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'requested_quote_id' => $this->id,
|
||||
'content' => 'Quote approved.'
|
||||
]);
|
||||
|
||||
Quote::create([
|
||||
'quote' => $this->quote,
|
||||
]);
|
||||
@ -29,7 +34,11 @@ class RequestedQuote extends Model
|
||||
|
||||
public function reject(): void
|
||||
{
|
||||
// TODO: we'll probs want to log who reject this quote
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'requested_quote_id' => $this->id,
|
||||
'content' => 'Quote rejected.'
|
||||
]);
|
||||
|
||||
$this->delete();
|
||||
}
|
||||
|
||||
@ -14,6 +14,16 @@ class User extends Authenticatable
|
||||
{
|
||||
use HasApiTokens, HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The model's default values for attributes.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $attributes = [
|
||||
'is_admin' => false,
|
||||
'status' => true,
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
@ -46,6 +56,23 @@ class User extends Authenticatable
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $casts = [
|
||||
'is_admin' => 'boolean',
|
||||
'status' => 'boolean',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
|
||||
public static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
// These will only ever get created by the api/bot.
|
||||
// If we want to add something like by whom later on,
|
||||
// we can just append it to the content e.g auth()?->user()?->id ?? 'API'.
|
||||
self::created(function ($model) {
|
||||
Log::create([
|
||||
'user_id' => $model->id,
|
||||
'content' => 'User created.'
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
28
database/migrations/2023_10_10_200426_create_logs_table.php
Normal file
28
database/migrations/2023_10_10_200426_create_logs_table.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Quote;
|
||||
use App\Models\RequestedQuote;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('logs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignIdFor(User::class)->nullable();
|
||||
$table->foreignIdFor(Quote::class)->nullable();
|
||||
$table->foreignIdFor(RequestedQuote::class)->nullable();
|
||||
$table->string('content');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('logs');
|
||||
}
|
||||
};
|
||||
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Log;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Attributes\Rule;
|
||||
@ -14,13 +15,18 @@ new #[Layout('layouts.app')] class extends Component
|
||||
{
|
||||
$validated = $this->validate();
|
||||
|
||||
// TODO: move this to a helper class so we can reuse code between API and FE
|
||||
$response = Http::post(config('bot.webhook'), ['text' => $validated['quote']]);
|
||||
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'content' => "Quote sent. {$validated['quote']}"
|
||||
]);
|
||||
|
||||
$this->quote = '';
|
||||
}
|
||||
}; ?>
|
||||
|
||||
|
||||
<div class="px-4 sm:px-6 lg:px-8">
|
||||
<form wire:submit="sendQuote">
|
||||
<x-text-input
|
||||
@ -32,6 +38,8 @@ new #[Layout('layouts.app')] class extends Component
|
||||
<x-input-error :messages="$errors->get('quote')" class="mt-2"/>
|
||||
<x-primary-button wire:loading.attr="disabled"
|
||||
wire:loading.class="opacity-50"
|
||||
class="mt-4">{{ __('Send') }}</x-primary-button>
|
||||
class="mt-4">
|
||||
{{ __('Send') }}
|
||||
</x-primary-button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Log;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Illuminate\Auth\Events\Lockout;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
@ -39,6 +40,11 @@ new #[Layout('layouts.guest')] class extends Component
|
||||
|
||||
session()->regenerate();
|
||||
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'content' => "Quote sent. {$quote}"
|
||||
]);
|
||||
|
||||
$this->redirect(
|
||||
session('url.intended', RouteServiceProvider::HOME),
|
||||
navigate: true
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Log;
|
||||
use Illuminate\Auth\Events\PasswordReset;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Password;
|
||||
@ -58,6 +59,11 @@ new #[Layout('layouts.guest')] class extends Component
|
||||
|
||||
session()->flash('status', __($status));
|
||||
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'content' => "Quote sent. {$quote}"
|
||||
]);
|
||||
|
||||
$this->redirectRoute('login', navigate: true);
|
||||
}
|
||||
}; ?>
|
||||
|
||||
@ -1,77 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Livewire\Attributes\Rule;
|
||||
use Livewire\Volt\Component;
|
||||
|
||||
new class extends Component
|
||||
{
|
||||
#[Rule(['required', 'string', 'current_password'])]
|
||||
public string $password = '';
|
||||
|
||||
public function deleteUser(): void
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
tap(auth()->user(), fn () => auth()->logout())->delete();
|
||||
|
||||
session()->invalidate();
|
||||
session()->regenerateToken();
|
||||
|
||||
$this->redirect('/', navigate: true);
|
||||
}
|
||||
}; ?>
|
||||
|
||||
<section class="space-y-6">
|
||||
<header>
|
||||
<h2 class="text-lg font-medium text-nexi-black dark:text-gray-100">
|
||||
{{ __('Delete Account') }}
|
||||
</h2>
|
||||
|
||||
<p class="mt-1 text-sm text-gray-600 dark:text-gray-400">
|
||||
{{ __('Once your account is deleted, all of its resources and data will be permanently deleted. Before deleting your account, please download any data or information that you wish to retain.') }}
|
||||
</p>
|
||||
</header>
|
||||
|
||||
<x-danger-button
|
||||
x-data=""
|
||||
x-on:click.prevent="$dispatch('open-modal', 'confirm-user-deletion')"
|
||||
>{{ __('Delete Account') }}</x-danger-button>
|
||||
|
||||
<x-modal name="confirm-user-deletion" :show="$errors->isNotEmpty()" focusable>
|
||||
<form wire:submit="deleteUser" class="p-6">
|
||||
|
||||
<h2 class="text-lg font-medium text-nexi-black dark:text-gray-100">
|
||||
{{ __('Are you sure you want to delete your account?') }}
|
||||
</h2>
|
||||
|
||||
<p class="mt-1 text-sm text-gray-600 dark:text-gray-400">
|
||||
{{ __('Once your account is deleted, all of its resources and data will be permanently deleted. Please enter your password to confirm you would like to permanently delete your account.') }}
|
||||
</p>
|
||||
|
||||
<div class="mt-6">
|
||||
<x-input-label for="password" value="{{ __('Password') }}" class="sr-only" />
|
||||
|
||||
<x-text-input
|
||||
wire:model="password"
|
||||
id="password"
|
||||
name="password"
|
||||
type="password"
|
||||
class="mt-1 block w-3/4"
|
||||
placeholder="{{ __('Password') }}"
|
||||
/>
|
||||
|
||||
<x-input-error :messages="$errors->get('password')" class="mt-2" />
|
||||
</div>
|
||||
|
||||
<div class="mt-6 flex justify-end">
|
||||
<x-secondary-button x-on:click="$dispatch('close')">
|
||||
{{ __('Cancel') }}
|
||||
</x-secondary-button>
|
||||
|
||||
<x-danger-button class="ml-3">
|
||||
{{ __('Delete Account') }}
|
||||
</x-danger-button>
|
||||
</div>
|
||||
</form>
|
||||
</x-modal>
|
||||
</section>
|
||||
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Log;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rules\Password;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
@ -28,6 +29,11 @@ new class extends Component
|
||||
'password' => Hash::make($validated['password']),
|
||||
]);
|
||||
|
||||
Log::create([
|
||||
'user_id' => auth()?->user()?->id,
|
||||
'content' => "Quote sent. {$quote}"
|
||||
]);
|
||||
|
||||
$this->reset('current_password', 'password', 'password_confirmation');
|
||||
|
||||
$this->dispatch('password-updated');
|
||||
|
||||
@ -1,82 +0,0 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Livewire\Volt\Component;
|
||||
|
||||
new class extends Component
|
||||
{
|
||||
public string $firstname = '';
|
||||
public string $lastname = '';
|
||||
public string $email = '';
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->firstname = auth()->user()->firstname;
|
||||
$this->lastname = auth()->user()->lastname;
|
||||
$this->email = auth()->user()->email;
|
||||
}
|
||||
|
||||
public function updateProfileInformation(): void
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
$validated = $this->validate([
|
||||
'firstname' => ['required', 'string', 'max:255'],
|
||||
'lastname' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'email', 'max:255', Rule::unique(User::class)->ignore($user->id)],
|
||||
]);
|
||||
|
||||
$user->fill($validated);
|
||||
|
||||
if ($user->isDirty('email')) {
|
||||
$user->email_verified_at = null;
|
||||
}
|
||||
|
||||
$user->save();
|
||||
|
||||
$this->dispatch('profile-updated', name: $user->name);
|
||||
}
|
||||
}; ?>
|
||||
|
||||
<section>
|
||||
<header>
|
||||
<h2 class="text-lg font-medium text-nexi-black dark:text-gray-100">
|
||||
{{ __('Profile Information') }}
|
||||
</h2>
|
||||
|
||||
<p class="mt-1 text-sm text-gray-600 dark:text-gray-400">
|
||||
{{ __("Update your account's profile information and email address.") }}
|
||||
</p>
|
||||
</header>
|
||||
|
||||
<form wire:submit="updateProfileInformation" class="mt-6 space-y-6">
|
||||
<div>
|
||||
<x-input-label for="firstname" :value="__('FirstName')" />
|
||||
<x-text-input wire:model="firstname" id="firstname" name="firstname" type="text" class="mt-1 block w-full" required autofocus autocomplete="first" />
|
||||
<x-input-error class="mt-2" :messages="$errors->get('firstname')" />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<x-input-label for="lastname" :value="__('lastname')" />
|
||||
<x-text-input wire:model="lastname" id="lastname" name="lastname" type="text" class="mt-1 block w-full" required autofocus autocomplete="last" />
|
||||
<x-input-error class="mt-2" :messages="$errors->get('lastname')" />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<x-input-label for="email" :value="__('Email')" />
|
||||
<x-text-input wire:model="email" id="email" name="email" type="email" class="mt-1 block w-full" required autocomplete="username" />
|
||||
<x-input-error class="mt-2" :messages="$errors->get('email')" />
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<x-primary-button wire:loading.attr="disabled" wire:loading.class="opacity-50">{{ __('Save') }}</x-primary-button>
|
||||
|
||||
<x-action-message class="mr-3" on="profile-updated">
|
||||
{{ __('Saved.') }}
|
||||
</x-action-message>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
@ -7,23 +7,11 @@
|
||||
|
||||
<div class="py-12">
|
||||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8 space-y-6">
|
||||
{{-- <div class="p-4 sm:p-8 shadow sm:rounded-lg dark:shadow-zinc-600">--}}
|
||||
{{-- <div class="max-w-xl">--}}
|
||||
{{-- <livewire:profile.update-profile-information-form />--}}
|
||||
{{-- </div>--}}
|
||||
{{-- </div>--}}
|
||||
|
||||
<div class="p-4 sm:p-8 shadow sm:rounded-lg dark:shadow-zinc-600">
|
||||
<div class="max-w-xl">
|
||||
<livewire:profile.update-password-form />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{-- <div class="p-4 sm:p-8 shadow sm:rounded-lg dark:shadow-zinc-600">--}}
|
||||
{{-- <div class="max-w-xl">--}}
|
||||
{{-- <livewire:profile.delete-user-form />--}}
|
||||
{{-- </div>--}}
|
||||
{{-- </div>--}}
|
||||
</div>
|
||||
</div>
|
||||
</x-app-layout>
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\Api\WebHookController;
|
||||
use App\Http\Controllers\Api\UserController;
|
||||
use App\Http\Controllers\Api\WebHookController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user