Skip to content

Commit

Permalink
Add bugsnag support for errors (#20)
Browse files Browse the repository at this point in the history
* Update composer dependencies and add bugsnag

* Update config

* Also make mysql storage persistant...

* Update the channels some and the alerts modal. Still have to fix the listener to handle the newer data tho.

* Finally figure out how we're going to support webhooks, slack, and discord.

* Save the .env vars for how we handle slack/discord
  • Loading branch information
Austin Kregel authored Jul 27, 2020
1 parent 53f5aa2 commit b57fd59
Show file tree
Hide file tree
Showing 20 changed files with 323 additions and 172 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ PLAID_CLIENT_ID=

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

SLACK_WEBHOOK_URL=
DISCORD_WEBHOOK_URL=

34 changes: 6 additions & 28 deletions app/Console/Commands/GenerateChannelsAndAlertsFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,54 +34,32 @@ class GenerateChannelsAndAlertsFile extends Command
*/
protected $description = 'Build channels and alerts.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->writeToDisk('js/channels.js', [
[
'type' => SlackWebhookChannel::class,
'name' => 'Slack',
'service' => 'webhook',
'type' => SlackWebhookChannel::class,
],
[
'type' => DiscordChannel::class,
'name' => 'Discord',
'service' => 'webhook',
'type' => DiscordChannel::class,
],
[
'type' => WebhookChannel::class,
'name' => 'Webhook',
'service' => 'webhook',
'type' => WebhookChannel::class,
],
[
'type' => MailChannel::class,
'name' => 'Email',
'service' => 'email',
'type' => MailChannel::class,
],
[
'type' => NexmoSmsChannel::class,
'name' => 'Nexmo',
'service' => 'sms'
'type' => NexmoSmsChannel::class,
],
[
// BroadcastChannel::class
'type' => DatabaseChannel::class,
'name' => 'In-site notification',
'service' => 'in-site'
'type' => DatabaseChannel::class,
],
]);

Expand Down
6 changes: 6 additions & 0 deletions app/Http/Controllers/Api/AbstractResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Http\Controllers\Api;

use App\FailedJob;
use App\Http\Controllers\Controller;
use App\Models\Transaction;
use Exception;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
Expand Down Expand Up @@ -37,6 +39,10 @@ public function index(IndexRequest $request, AbstractEloquentModel $model)
->allowedIncludes($model->getAbstractAllowedRelationships())
->allowedSorts($model->getAbstractAllowedSorts());

if (!in_array(get_class($model), [FailedJob::class, Transaction::class])) {
$query->where('user_id', auth()->id());
}

return $this->json($action->execute($query));
}

Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Api/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public function index(IndexRequest $request)
Filter::scope('q')
]))
->allowedIncludes($model->getAbstractAllowedRelationships())
->allowedSorts($model->getAbstractAllowedSorts());
->allowedSorts($model->getAbstractAllowedSorts())
->whereIn('access_token_id', $request->user()->accessTokens->map->id);

return $this->json($action->execute($query));
}
Expand All @@ -56,6 +57,7 @@ public function show(ViewRequest $request, Account $model)
->allowedFilters($model->getAbstractAllowedFilters())
->allowedIncludes($model->getAbstractAllowedRelationships())
->allowedSorts($model->getAbstractAllowedSorts())
->whereIn('access_token_id', $request->user()->accessTokens->map->id)
->find($model->id);

if (empty($result)) {
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Api/AlertController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function index(IndexRequest $request)
Filter::scope('q')
]))
->allowedIncludes($model->getAbstractAllowedRelationships())
->allowedSorts($model->getAbstractAllowedSorts());
->allowedSorts($model->getAbstractAllowedSorts())
->where('user_id', auth()->id());

return $this->json($action->execute($query));
}
Expand All @@ -54,6 +55,7 @@ public function show(ViewRequest $request, Alert $model)
->allowedFilters($model->getAbstractAllowedFilters())
->allowedIncludes($model->getAbstractAllowedRelationships())
->allowedSorts($model->getAbstractAllowedSorts())
->where('user_id', auth()->id())
->find($model->id);

if (empty($result)) {
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Api/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function index(IndexRequest $request)
Filter::scope('q')
]))
->allowedIncludes($model->getAbstractAllowedRelationships())
->allowedSorts($model->getAbstractAllowedSorts());
->allowedSorts($model->getAbstractAllowedSorts())
->where('user_id', auth()->id());

return $this->json($action->execute($query));
}
Expand All @@ -63,6 +64,7 @@ public function show(ViewRequest $request, Tag $model)
->allowedFilters($model->getAbstractAllowedFilters())
->allowedIncludes($model->getAbstractAllowedRelationships())
->allowedSorts($model->getAbstractAllowedSorts())
->where('user_id', auth()->id())
->find($model->id);

if (empty($result)) {
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/Api/TransactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public function index(IndexRequest $request)
Filter::scope('q')
]))
->allowedIncludes($model->getAbstractAllowedRelationships())
->allowedSorts($model->getAbstractAllowedSorts());
->allowedSorts($model->getAbstractAllowedSorts())
->whereHas('account', function ($query) use ($request) {
$query->whereIn('access_token_id', $request->user()->accessTokens->map->id);
});

return $this->json($action->execute($query));
}
Expand Down
9 changes: 7 additions & 2 deletions app/Listeners/CreateDefaultAlertsForUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ class CreateDefaultAlertsForUser
TransactionCreated::class,
],
'channels' => [
MailChannel::class,
DatabaseChannel::class,
]
],
[
'name' => 'Bill paid!',
'title' => 'You just paid your {{ transaction.name }} {{ tag.name }}!',
'title' => 'You just paid your {{ transaction.name }} {{ tag.name.en }}!',
'body' => 'This time around, you paid ${{ transaction.amount }}.',
'conditions' => [
[
Expand All @@ -53,7 +54,7 @@ class CreateDefaultAlertsForUser
],
[
'name' => 'Subscription paid!',
'title' => 'You just paid your {{ transaction.name }} {{ tag.name }}!',
'title' => 'You just paid your {{ transaction.name }} {{ tag.name.en }}!',
'body' => 'This time around, you paid ${{ transaction.amount }}.',
'conditions' => [
[
Expand Down Expand Up @@ -93,6 +94,10 @@ public function handle(Registered $event)
$conditions = $alertInfo['conditions'];
unset($alertInfo['conditions']);

if (isset($alertInfo['channels'][MailChannel::class]) && isset($alertInfo['channels'][MailChannel::class]['fields']['to'])) {
$alertInfo['channels'][MailChannel::class]['fields']['to'] = $user->email;
}

/** @var Alert $alert */
$alert = $user->alerts()->create($alertInfo);
foreach ($conditions as $condition) {
Expand Down
9 changes: 5 additions & 4 deletions app/Notifications/AlertNotiffication.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ protected function render($string, $data)

public function routeNotificationForDiscord()
{
return $this->alertLog->alert->messaging_service_channel;
return config('services.discord_webhook_url');
}

public function routeNotificationForSlack($notification)
{
return $this->alertLog->alert->webhook_url;
return config('services.slack_webhook_url');
}

public function toMail($notifiable)
Expand All @@ -80,9 +80,10 @@ public function toSlack($notifiable)
->warning();
}

public function toDiscord()
public function toDiscord($notifiable)
{
return new DiscordMessage(sprintf('We saw you were charged $%s by %s to your account %s', $this->alertLog->transaction->amount, $this->alertLog->transaction->name, $this->alertLog->transaction->account->name));
return (new DiscordMessage)
->body(sprintf('We saw you were charged $%s by %s to your account %s', $this->alertLog->transaction->amount, $this->alertLog->transaction->name, $this->alertLog->transaction->account->name));
}

public function toWebhook($notifiable)
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"require": {
"php": "^7.4",
"barryvdh/laravel-debugbar": "^3.3",
"bugsnag/bugsnag-laravel": "^2.0",
"doctrine/dbal": "^2.9",
"fideloper/proxy": "^4.2",
"firebase/php-jwt": "^5.0",
Expand Down
Loading

0 comments on commit b57fd59

Please sign in to comment.