Skip to content

Commit

Permalink
Merge pull request #86 from SGS-Optimisation/main
Browse files Browse the repository at this point in the history
fixes Silvanite#58 flagged rule notifications
  • Loading branch information
yayann authored Aug 24, 2021
2 parents d4f6162 + 8bfee3d commit b0e36c1
Show file tree
Hide file tree
Showing 22 changed files with 445 additions and 9 deletions.
54 changes: 54 additions & 0 deletions app/Console/Commands/FlaggedRulesReminder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Console\Commands;

use App\Features\Rules\FlaggedCollector;
use App\Models\User;
use App\Notifications\FlaggedRulesReminderNotification;
use Illuminate\Console\Command;

class FlaggedRulesReminder extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'rules:flagged:remind';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Send reminders about flagged rules to their contributors';

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

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$flagged_collector = (new FlaggedCollector)->handle();

foreach($flagged_collector->users_rules_dict as $user_id => $user_rules_list) {
/** @var User $user */
$user = $user_rules_list['user'];
$this->info('notifying ' . $user->name);

$user->notify(new FlaggedRulesReminderNotification($user_rules_list['rules']));

}
}
}
5 changes: 5 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console;

use App\Console\Commands\FlaggedRulesReminder;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Laravel\Nova\Trix\PruneStaleAttachments;
Expand Down Expand Up @@ -30,6 +31,10 @@ protected function schedule(Schedule $schedule)
//(new PruneStaleAttachments)();
})->daily();

$schedule->call(function() {
(new FlaggedRulesReminder)();
})->weeklyOn(1, '8:00');

$schedule->command('cache:clear')->lastDayOfMonth();
$schedule->command('cache:warmup')->hourly();
}
Expand Down
38 changes: 38 additions & 0 deletions app/Features/Rules/FlaggedCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php


namespace App\Features\Rules;


use App\Features\BaseFeature;
use App\Models\Rule;

class FlaggedCollector extends BaseFeature
{
public $users_rules_dict;

public function handle()
{
$rules = Rule::isFlagged()->with(['contributors'])->get();
$users_rules_dict = [];

foreach ($rules as $rule) {
foreach ($rule->contributors as $contributor) {
if (!isset($users_rules_dict[$contributor->id])) {
$users_rules_dict[$contributor->id] = [
'user' => $contributor,
'rules' => [],
];
}

$users_rules_dict[$contributor->id]['rules'][] = $rule;

}
}

$this->users_rules_dict = $users_rules_dict;

return $this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace App\Listeners\Rules;

use App\Events\Rules\Flagged;
use App\Notifications\FlaggedRuleNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Notification;

class NotifyRuleEditors
class NotifyRuleContributor implements ShouldQueue
{
/**
* Create the event listener.
Expand All @@ -26,6 +28,6 @@ public function __construct()
*/
public function handle(Flagged $event)
{
//
Notification::send($event->rule->contributors, new FlaggedRuleNotification($event->rule));
}
}
13 changes: 11 additions & 2 deletions app/Listeners/Rules/NotifyTeamOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace App\Listeners\Rules;

use App\Events\Rules\Flagged;
use App\Notifications\FlaggedRuleNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Notification;

class NotifyTeamOwner
class NotifyTeamOwner implements ShouldQueue
{
/**
* Create the event listener.
Expand All @@ -26,6 +28,13 @@ public function __construct()
*/
public function handle(Flagged $event)
{
//
Notification::send(
$event->rule->contributorTeams()
->with('owner')
->get()
->pluck('owner')
->whereNotIn('email', $event->rule->contributors->pluck('email')),
new FlaggedRuleNotification($event->rule)
);
}
}
2 changes: 2 additions & 0 deletions app/Models/ClientAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
* @property-read int|null $child_taxonomies_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Taxonomy[] $root_taxonomies
* @property-read int|null $root_taxonomies_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Team[] $teams
* @property-read int|null $teams_count
*/
class ClientAccount extends Model
{
Expand Down
14 changes: 14 additions & 0 deletions app/Models/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@
* @method static Builder|Rule whereNotState(string $column, $states)
* @method static Builder|Rule whereState($value)
* @method static Builder|Rule forClient(\App\Models\ClientAccount $clientAccount)
* @property-read mixed $dag_id
* @property-read \Illuminate\Database\Eloquent\Collection|\Altek\Accountant\Models\Ledger[] $ledgers
* @property-read int|null $ledgers_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Team[] $teams
* @property-read int|null $teams_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\User[] $users
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\User[] $contributors
* @property-read int|null $users_count
*/
class Rule extends Model implements Recordable
{
Expand Down Expand Up @@ -273,4 +281,10 @@ public function getDagIdAttribute()
{
return str_pad($this->id, 6, '0', STR_PAD_LEFT) . 'D';
}


public function getUrlAttribute()
{
return route('pm.client-account.rules.edit', [$this->clientAccount->slug, $this->id]);
}
}
9 changes: 9 additions & 0 deletions app/Models/RuleTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
*
* @property int $id
* @property int $taxonomy_id
* @property int $rule_id
* @property int $term_id
* @method static \Illuminate\Database\Eloquent\Builder|RuleTerm newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|RuleTerm newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|RuleTerm query()
* @method static \Illuminate\Database\Eloquent\Builder|RuleTerm whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|RuleTerm whereRuleId($value)
* @method static \Illuminate\Database\Eloquent\Builder|RuleTerm whereTermId($value)
* @mixin \Eloquent
*/
class RuleTerm extends Pivot // implements Recordable
{
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
* @property-read int|null $mappings_count
* @method static \Database\Factories\TaxonomyFactory factory(...$parameters)
* @method static Builder|Taxonomy children()
* @property-read \Illuminate\Database\Eloquent\Collection|\Altek\Accountant\Models\Ledger[] $ledgers
* @property-read int|null $ledgers_count
*/
class Taxonomy extends Model implements Recordable
{
Expand Down
4 changes: 4 additions & 0 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
* @property int $client_account_id
* @method static \Illuminate\Database\Eloquent\Builder|Team whereClientAccountId($value)
* @method static Builder|Team personal($personal = true)
* @property string|null $region
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\User[] $rules
* @property-read int|null $rules_count
* @method static Builder|Team whereRegion($value)
*/
class Team extends JetstreamTeam
{
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* @property-read int|null $rules_count
* @method static \Illuminate\Database\Eloquent\Builder|Term whereDeletedAt($value)
* @method static \Database\Factories\TermFactory factory(...$parameters)
* @property-read \Illuminate\Database\Eloquent\Collection|\Altek\Accountant\Models\Ledger[] $ledgers
* @property-read int|null $ledgers_count
*/
class Term extends Model implements Recordable
{
Expand Down
2 changes: 2 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
* @method static \Illuminate\Database\Eloquent\Builder|User whereMobilePhone($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereOfficeLocation($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereSurname($value)
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Rule[] $rules
* @property-read int|null $rules_count
*/
class User extends Authenticatable implements Identifiable
{
Expand Down
76 changes: 76 additions & 0 deletions app/Notifications/FlaggedRuleNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Notifications;

use App\Models\Rule;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;

class FlaggedRuleNotification extends Notification
{
use Queueable;

/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(public Rule $rule)
{

}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
$rule = $this->rule;
$last_reason_key = array_key_last($rule->metadata['flag_reason']);
$flag_reason = $rule->metadata['flag_reason'][$last_reason_key];

$subject = 'Rule flagged: '.Str::limit($rule->name, 20);

return (new MailMessage)
->greeting('Hello '.$notifiable->given_name)
->subject($subject)
->line('The following flagged rule needs your attention:')
->line(new HtmlString(
sprintf('<p><a href="%s">%s</a><br>The flag reason was: "%s" (%s on %s)</p><br>',
$rule->url, '['.$rule->dag_id.'] '.$rule->name,
$flag_reason['reason'], $flag_reason['user'], $flag_reason['date']
)
))->salutation(new HtmlString('Regards,<br>The Dagobah Team'));
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
Loading

0 comments on commit b0e36c1

Please sign in to comment.