Skip to content

feat: notifications for new engagements (resolves #2562) #2604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function create(array $input): User
}
}

if ($input['context'] === UserContext::Individual->value) {
$input['notification_settings'] = ['engagements' => '1'];
}

Validator::make(
$input,
[
Expand All @@ -63,6 +67,7 @@ public function create(array $input): User
'locale' => ['required', Rule::in(config('locales.supported'))],
'accepted_privacy_policy' => 'accepted',
'accepted_terms_of_service' => 'accepted',
'notification_settings.engagements' => 'nullable|boolean',
],
[
'accepted_privacy_policy.accepted' => __('You must agree to the privacy policy.'),
Expand All @@ -88,6 +93,7 @@ public function create(array $input): User
'extra_attributes' => $input['extra_attributes'] ?? null,
'accepted_privacy_policy_at' => now(),
'accepted_terms_of_service_at' => now(),
'notification_settings' => $input['notification_settings'] ?? null,
]);
}
}
150 changes: 150 additions & 0 deletions app/Console/Commands/MigrateSettingsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

namespace App\Console\Commands;

use App\Models\Organization;
use App\Models\User;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Isolatable;

use function Termwind\render;

class MigrateSettingsData extends Command implements Isolatable
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:migrate-settings-data
{--list : lists out available migrations}
{--from=1.6.0 : when running all migrations, indicate which version the application is being migrated from. Previous migrations will be skipped.}
{--migration= : a specific migration to run}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Migrates settings data that is not performed by database migrations; such as modifying the contents of database fields.';

protected $migrations = [
'EnableEngagementNotifications' => [
'version' => '1.7.0',
'handler' => 'enableEngagementNotificationsMigration',
'description' => 'Replaces older format of notifications_settings with only ["engagements" => "1"]. Setting the engagement notifications on be default. If the notifications_settings contains a valid engagements setting, then no changes are made.',
],
];

/**
* Execute the console command.
*/
public function handle()

Check warning on line 43 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L43

Added line #L43 was not covered by tests
{
$verbose = $this->options()['verbose'];

Check failure on line 45 in app/Console/Commands/MigrateSettingsData.php

View workflow job for this annotation

GitHub Actions / php-analyze (8.4)

Offset 'verbose' does not exist on array{list: bool, from: string|null, migration: string|null, isolated: string|false|null}.

Check warning on line 45 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L45

Added line #L45 was not covered by tests

if ($this->options()['list']) {

Check warning on line 47 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L47

Added line #L47 was not covered by tests

$this->listMigrations($this->migrations);

Check warning on line 49 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L49

Added line #L49 was not covered by tests

return 0;

Check warning on line 51 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L51

Added line #L51 was not covered by tests
}

if ($this->options()['migration']) {

Check warning on line 54 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L54

Added line #L54 was not covered by tests
try {
$migration = $this->migrations[$this->options()['migration']];
} catch (Exception $e) {
$this->fail('Could not find migration: '.$this->options()['migration']);

Check warning on line 58 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L56-L58

Added lines #L56 - L58 were not covered by tests
}

if (isset($migration)) {
$handler = $migration['handler'];
$this->$handler();

Check warning on line 63 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L61-L63

Added lines #L61 - L63 were not covered by tests

return 0;

Check warning on line 65 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L65

Added line #L65 was not covered by tests
}
}

$this->runMigrations($this->migrations, $this->options()['from'], $verbose);

Check warning on line 69 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L69

Added line #L69 was not covered by tests
}

public function listMigrations($migrations)

Check warning on line 72 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L72

Added line #L72 was not covered by tests
{
$definitions = '';

Check warning on line 74 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L74

Added line #L74 was not covered by tests

foreach ($migrations as $name => $migration) {
$version = $migration['version'];
$description = $migration['description'];
$definitions .= "<dt>$name</dt><dd>Version added: $version</dd><dd class=\"pb-1\">$description</dd>";

Check warning on line 79 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L76-L79

Added lines #L76 - L79 were not covered by tests
}

render(<<<HTML
<dl>
$definitions

Check warning on line 84 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L82-L84

Added lines #L82 - L84 were not covered by tests
</dl>
HTML);

Check warning on line 86 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L86

Added line #L86 was not covered by tests
}

public function runMigrations($migrations, $from = '1.6.0', $verbose = false)

Check warning on line 89 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L89

Added line #L89 was not covered by tests
{
$from = str_starts_with($from, 'v') || str_starts_with($from, 'V') ? substr($from, 1) : $from;
$migrationRunCount = 0;

Check warning on line 92 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L91-L92

Added lines #L91 - L92 were not covered by tests

foreach ($migrations as $name => $migration) {
if (version_compare($from, $migration['version'], '<')) {
if ($verbose) {
$this->line("<fg=cyan>Run migration - $name</>");

Check warning on line 97 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L94-L97

Added lines #L94 - L97 were not covered by tests
}
$handler = $migration['handler'];
$this->$handler($verbose);
$migrationRunCount++;
} elseif ($verbose) {
$this->comment("Skipped migration - $name");

Check warning on line 103 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L99-L103

Added lines #L99 - L103 were not covered by tests
}

if ($verbose) {
$this->newLine();

Check warning on line 107 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L106-L107

Added lines #L106 - L107 were not covered by tests
}
}

$this->line('<options=bold;fg=green>Completed '.$migrationRunCount.'</>');
$this->line('<options=bold;fg=yellow>Skipped '.(count($migrations) - $migrationRunCount).'</>');

Check warning on line 112 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L111-L112

Added lines #L111 - L112 were not covered by tests
}

// Migrations

public function enableEngagementNotificationsMigration($verbose = false)

Check warning on line 117 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L117

Added line #L117 was not covered by tests
{
$updatedNotificationSettings = ['engagements' => '1'];

Check warning on line 119 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L119

Added line #L119 was not covered by tests

if ($verbose) {
$this->info(' - Migrating engagement notifications for users');

Check warning on line 122 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L121-L122

Added lines #L121 - L122 were not covered by tests
}

$users = User::where('context', 'individual')->whereNull('notification_settings->engagements')
->get();

Check warning on line 126 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L125-L126

Added lines #L125 - L126 were not covered by tests

$users->each(function ($user) use ($updatedNotificationSettings) {
$user->notification_settings = $updatedNotificationSettings;

Check failure on line 129 in app/Console/Commands/MigrateSettingsData.php

View workflow job for this annotation

GitHub Actions / php-analyze (8.4)

Property App\Models\User::$notification_settings (Spatie\SchemalessAttributes\SchemalessAttributes) does not accept array<string, string>.
$user->save();
});

Check warning on line 131 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L128-L131

Added lines #L128 - L131 were not covered by tests

if ($verbose) {
$this->info(' - Migrated '.$users->count().' users');
$this->info(' - Migrating engagement notification settings for Organizations');

Check warning on line 135 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L133-L135

Added lines #L133 - L135 were not covered by tests
}

$orgs = Organization::whereNull('notification_settings->engagements')
->get();

Check warning on line 139 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L138-L139

Added lines #L138 - L139 were not covered by tests

$orgs->each(function ($organization) use ($updatedNotificationSettings) {
$organization->notification_settings = $updatedNotificationSettings;

Check failure on line 142 in app/Console/Commands/MigrateSettingsData.php

View workflow job for this annotation

GitHub Actions / php-analyze (8.4)

Property App\Models\Organization::$notification_settings (Spatie\SchemalessAttributes\SchemalessAttributes) does not accept array<string, string>.
$organization->save();
});

Check warning on line 144 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L141-L144

Added lines #L141 - L144 were not covered by tests

if ($verbose) {
$this->info(' - Migrated '.$orgs->count().' Organizations');

Check warning on line 147 in app/Console/Commands/MigrateSettingsData.php

View check run for this annotation

Codecov / codecov/patch

app/Console/Commands/MigrateSettingsData.php#L146-L147

Added lines #L146 - L147 were not covered by tests
}
}
}
17 changes: 17 additions & 0 deletions app/Enums/YesNo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Enums;

enum YesNo: string
{
case Yes = '1';
case No = '0';

public static function labels(): array
{
return [
'1' => __('Yes'),
'0' => __('No'),
];
}
}
17 changes: 17 additions & 0 deletions app/Http/Controllers/EngagementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use App\Models\Project;
use App\Models\User;
use App\Notifications\AccessNeedsFacilitationRequested;
use App\Notifications\EngagementAdded;
use App\Notifications\JoinedEngagement;
use App\Notifications\LeftEngagement;
use App\Notifications\OrganizationAddedToEngagement;
Expand All @@ -47,6 +48,7 @@
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification as FacadesNotification;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Notification;
Expand Down Expand Up @@ -422,6 +424,21 @@ public function update(UpdateEngagementRequest $request, Engagement $engagement)
if ($engagement->fresh()->isPublishable()) {
$engagement->update(['published_at' => now()]);
flash(__('Your engagement has been published.'), 'success|'.__('Your engagement has been published.', [], 'en'));

if ($engagement->recruitment === EngagementRecruitment::OpenCall->value) {
$users = User::where('context', 'individual')->withNotificationSettings('engagements', '1')->get();
FacadesNotification::send($users, new EngagementAdded($engagement));
}

$projectable = $engagement->project->projectable;

$otherOrgs = Organization::when($projectable instanceof Organization, fn ($query) => $query->whereNot(fn ($query) => $query->where('id', $projectable->id)))
->withNotificationSettings('engagements', '1')
->get();

if ($otherOrgs->count()) {
FacadesNotification::send($otherOrgs, new EngagementAdded($engagement));
}
}
} else {
flash(__('Your engagement has been updated.'), 'success|'.__('Your engagement has been updated.', [], 'en'));
Expand Down
6 changes: 2 additions & 4 deletions app/Http/Controllers/IndividualController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Enums\IndividualRole;
use App\Enums\MeetingType;
use App\Enums\ProvinceOrTerritory;
use App\Enums\YesNo;
use App\Http\Requests\DestroyIndividualRequest;
use App\Http\Requests\SaveIndividualRolesRequest;
use App\Http\Requests\UpdateIndividualCommunicationAndConsultationPreferencesRequest;
Expand Down Expand Up @@ -137,10 +138,7 @@ public function edit(Individual $individual): View
'indigenousIdentities' => Options::forModels(Identity::query()->whereJsonContains('clusters', IdentityCluster::Indigenous))->toArray(),
'languages' => Options::forArray(get_available_languages(true))->nullable(__('Choose a language…'))->toArray(),
'livedExperiences' => Options::forModels(Identity::query()->whereJsonContains('clusters', IdentityCluster::LivedExperience)->withoutGlobalScope(ReachableIdentityScope::class))->toArray(),
'yesNoOptions' => Options::forArray([
'1' => __('Yes'),
'0' => __('No'),
])->toArray(),
'yesNoOptions' => Options::forEnum(YesNo::class)->toArray(),
'communityConnectorHasLivedExperience' => Options::forEnum(CommunityConnectorHasLivedExperience::class)->toArray(),
'contactPeople' => Options::forEnum(ContactPerson::class)->toArray(),
'meetingTypes' => Options::forEnum(MeetingType::class)->toArray(),
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/OrganizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Enums\ProvinceOrTerritory;
use App\Enums\StaffHaveLivedExperience;
use App\Enums\TeamRole;
use App\Enums\YesNo;
use App\Http\Requests\DestroyOrganizationRequest;
use App\Http\Requests\SaveOrganizationRolesRequest;
use App\Http\Requests\StoreOrganizationLanguagesRequest;
Expand Down Expand Up @@ -82,6 +83,8 @@ public function store(StoreOrganizationRequest $request): RedirectResponse

$data['languages'] = get_supported_locales(false);

$data['notification_settings'] = ['engagements' => '1'];

$organization = Organization::create($data);

session()->forget('type');
Expand Down Expand Up @@ -183,10 +186,7 @@ public function edit(Organization $organization): View
'indigenousIdentities' => Options::forModels(Identity::query()->whereJsonContains('clusters', IdentityCluster::Indigenous))->toArray(),
'languages' => Options::forArray(get_available_languages(true))->nullable(__('Choose a language…'))->toArray(),
'livedExperiences' => Options::forModels(Identity::query()->whereJsonContains('clusters', IdentityCluster::LivedExperience)->withoutGlobalScope(ReachableIdentityScope::class))->toArray(),
'yesNoOptions' => Options::forArray([
'1' => __('Yes'),
'0' => __('No'),
])->toArray(),
'yesNoOptions' => Options::forEnum(YesNo::class)->toArray(),
'staffHaveLivedExperience' => Options::forEnum(StaffHaveLivedExperience::class)->toArray(),
]);
}
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Enums\ProvinceOrTerritory;
use App\Enums\TeamRole;
use App\Enums\Theme;
use App\Enums\YesNo;
use App\Http\Requests\UpdateAccessNeedsRequest;
use App\Http\Requests\UpdateAreasOfInterestRequest;
use App\Http\Requests\UpdateCommunicationAndConsultationPreferencesRequest;
Expand Down Expand Up @@ -357,6 +358,7 @@ public function editNotificationPreferences(): View
'organizationNotificationChannels' => Options::forEnum(OrganizationNotificationChannel::class)->toArray(),
'projectNotificationTypes' => Options::forArray($projectNotificationTypes)->toArray(),
'engagementNotificationTypes' => Options::forArray($engagementNotificationTypes)->toArray(),
'yesNoOptions' => Options::forEnum(YesNo::class)->toArray(),
]);
}

Expand Down
Loading
Loading