From 3ab38e69fcb8dc859e428fe2225821c157901cf8 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 16 Aug 2023 16:03:30 +0200 Subject: [PATCH] feat: send internal notification to discord --- app/Jobs/CleanupInstanceStuffsJob.php | 1 + app/Jobs/SendConfirmationForWaitlistJob.php | 1 + .../Internal/GeneralNotification.php | 27 +++++++++++++++++++ bootstrap/helpers/shared.php | 21 +++++++++++---- routes/webhooks.php | 7 ++++- 5 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 app/Notifications/Internal/GeneralNotification.php diff --git a/app/Jobs/CleanupInstanceStuffsJob.php b/app/Jobs/CleanupInstanceStuffsJob.php index 7b3be1a44..e67a562bd 100644 --- a/app/Jobs/CleanupInstanceStuffsJob.php +++ b/app/Jobs/CleanupInstanceStuffsJob.php @@ -29,6 +29,7 @@ class CleanupInstanceStuffsJob implements ShouldQueue, ShouldBeUnique try { $this->cleanup_waitlist(); } catch (\Exception $e) { + send_internal_notification('CleanupInstanceStuffsJob failed with error: ' . $e->getMessage()); ray($e->getMessage()); } } diff --git a/app/Jobs/SendConfirmationForWaitlistJob.php b/app/Jobs/SendConfirmationForWaitlistJob.php index 31098e180..5c1518fc6 100755 --- a/app/Jobs/SendConfirmationForWaitlistJob.php +++ b/app/Jobs/SendConfirmationForWaitlistJob.php @@ -52,6 +52,7 @@ class SendConfirmationForWaitlistJob implements ShouldQueue ->html((string) $mail->render()) ); } catch (\Throwable $th) { + send_internal_notification('SendConfirmationForWaitlistJob failed with error: ' . $th->getMessage()); ray($th->getMessage()); throw $th; } diff --git a/app/Notifications/Internal/GeneralNotification.php b/app/Notifications/Internal/GeneralNotification.php new file mode 100644 index 000000000..ee13a6cc2 --- /dev/null +++ b/app/Notifications/Internal/GeneralNotification.php @@ -0,0 +1,27 @@ +message; + } +} diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index 65fe454ab..2d315020f 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -1,6 +1,9 @@ user()?->isInstanceAdmin(); } -function general_error_handler(Throwable|null $err = null, $that = null, $isJson = false, $customErrorMessage = null): mixed +function general_error_handler(Throwable | null $err = null, $that = null, $isJson = false, $customErrorMessage = null): mixed { try { ray('ERROR OCCURRED: ' . $err->getMessage()); @@ -47,9 +49,9 @@ function general_error_handler(Throwable|null $err = null, $that = null, $isJson } else { throw new Exception($customErrorMessage ?? $err->errorInfo[2]); } - } elseif($err instanceof TooManyRequestsException){ + } elseif ($err instanceof TooManyRequestsException) { throw new Exception($customErrorMessage ?? "Too many requests. Please try again in {$err->secondsUntilAvailable} seconds."); - }else { + } else { throw new Exception($customErrorMessage ?? $err->getMessage()); } } catch (Throwable $error) { @@ -104,7 +106,7 @@ function is_transactional_emails_active(): bool return data_get(InstanceSettings::get(), 'smtp_enabled'); } -function set_transanctional_email_settings(InstanceSettings|null $settings = null): void +function set_transanctional_email_settings(InstanceSettings | null $settings = null): void { if (!$settings) { $settings = InstanceSettings::get(); @@ -194,3 +196,12 @@ function validate_cron_expression($expression_to_validate): bool } return $isValid; } +function send_internal_notification(string $message): void +{ + try { + $team = Team::find(0); + $team->notify(new GeneralNotification('👀 Internal notifications: ' . $message)); + } catch (\Throwable $th) { + ray($th->getMessage()); + } +} diff --git a/routes/webhooks.php b/routes/webhooks.php index 77167caee..6c66ed706 100644 --- a/routes/webhooks.php +++ b/routes/webhooks.php @@ -174,7 +174,6 @@ Route::post('/source/github/events', function () { return general_error_handler(err: $e); } }); - Route::get('/waitlist/confirm', function () { $email = request()->get('email'); $confirmation_code = request()->get('confirmation_code'); @@ -184,6 +183,7 @@ Route::get('/waitlist/confirm', function () { if ($found && !$found->verified && $found->created_at > now()->subMinutes(config('constants.waitlist.confirmation_valid_for_minutes'))) { $found->verified = true; $found->save(); + send_internal_notification('Waitlist confirmed: ' . $email); return 'Thank you for confirming your email address. We will notify you when you are next in line.'; } return redirect()->route('dashboard'); @@ -199,6 +199,7 @@ Route::get('/waitlist/cancel', function () { $found = Waitlist::where('uuid', $confirmation_code)->where('email', $email)->first(); if ($found && !$found->verified) { $found->delete(); + send_internal_notification('Waitlist cancelled: ' . $email); return 'Your email address has been removed from the waitlist.'; } return redirect()->route('dashboard'); @@ -250,6 +251,7 @@ Route::post('/payments/events', function () { case 'subscription_updated': case 'subscription_resumed': case 'subscription_unpaused': + send_internal_notification('Subscription created or updated: ' . $subscription_id . ' for team ' . $team_id . ' with status ' . $status); $subscription = Subscription::updateOrCreate([ 'team_id' => $team_id, ], [ @@ -271,6 +273,7 @@ Route::post('/payments/events', function () { case 'subscription_expired': $subscription = Subscription::where('team_id', $team_id)->where('lemon_order_id', $order_id)->first(); if ($subscription) { + send_internal_notification('Subscription cancelled or paused: ' . $subscription_id . ' for team ' . $team_id . ' with status ' . $status); $subscription->update([ 'lemon_status' => $status, 'lemon_trial_ends_at' => $trial_ends_at, @@ -281,11 +284,13 @@ Route::post('/payments/events', function () { } break; } + $webhook->update([ 'status' => 'success', ]); } catch (Exception $e) { ray($e->getMessage()); + send_internal_notification('Subscription webhook failed: ' . $e->getMessage()); $webhook->update([ 'status' => 'failed', 'failure_reason' => $e->getMessage(),