From 077ead96126dfa7ba014a916bab4fd58a8d931cb Mon Sep 17 00:00:00 2001 From: Miguel Nogueira Date: Fri, 9 Oct 2020 22:27:36 +0100 Subject: [PATCH] RSM-5 Made Vacancies easily linkable to Teams --- .idea/hrm-mcserver.iml | 11 +++ .idea/php.xml | 11 ++- app/Http/Controllers/TeamController.php | 70 ++++++++++++++++--- app/Vacancy.php | 28 ++++++++ composer.json | 1 + composer.lock | 57 ++++++++++++++- .../views/dashboard/teams/edit-team.blade.php | 36 ++++++---- routes/web.php | 20 +++--- 8 files changed, 200 insertions(+), 34 deletions(-) diff --git a/.idea/hrm-mcserver.iml b/.idea/hrm-mcserver.iml index 1d4160c..b9d240f 100644 --- a/.idea/hrm-mcserver.iml +++ b/.idea/hrm-mcserver.iml @@ -2,11 +2,14 @@ + + + @@ -27,6 +30,7 @@ + @@ -67,6 +71,7 @@ + @@ -83,12 +88,16 @@ + + + + @@ -107,6 +116,8 @@ + + diff --git a/.idea/php.xml b/.idea/php.xml index 9cee5da..6c0e8ad 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -142,9 +142,18 @@ + + + + + + + + + - + diff --git a/app/Http/Controllers/TeamController.php b/app/Http/Controllers/TeamController.php index 6743f20..2276bf6 100644 --- a/app/Http/Controllers/TeamController.php +++ b/app/Http/Controllers/TeamController.php @@ -16,6 +16,7 @@ use Mpociot\Teamwork\Exceptions\UserNotInTeamException; use Mpociot\Teamwork\Facades\Teamwork; use Mpociot\Teamwork\TeamInvite; + class TeamController extends Controller { /** @@ -82,7 +83,7 @@ class TeamController extends Controller return view('dashboard.teams.edit-team') ->with('team', $team) ->with('users', User::all()) - ->with('vacancies', Vacancy::all()); + ->with('vacancies', Vacancy::with('teams')->get()->all()); } /** @@ -115,7 +116,7 @@ class TeamController extends Controller } public function invite(SendInviteRequest $request, Team $team) - { + { $user = User::findOrFail($request->user); if (!$team->openJoin) @@ -126,29 +127,29 @@ class TeamController extends Controller Teamwork::inviteToTeam($user, $team, function(TeamInvite $invite) use ($user) { Mail::to($user)->send(new InviteToTeam($invite)); }); - + $request->session()->flash('success', 'Invite sent! They can now accept or deny it.'); } - else + else { $request->session()->flash('error', 'This user has already been invited.'); } - + } else { $request->session()->flash('error', 'You can\'t invite users to public teams.'); } - return redirect()->back(); + return redirect()->back(); } public function processInviteAction(Request $request, $action, $token) { - + switch($action) { case 'accept': @@ -168,7 +169,7 @@ class TeamController extends Controller break; case 'deny': - + $invite = Teamwork::getInviteFromDenyToken($token); if ($invite && $invite->user->is(Auth::user())) @@ -182,10 +183,10 @@ class TeamController extends Controller } break; - + default: $request->session()->flash('error', 'Sorry, but the invite URL you followed was malformed. Try asking for another invite, or submit a bug report.'); - + } @@ -209,4 +210,53 @@ class TeamController extends Controller return redirect()->back(); } + + + // Since it's a separate form, we shouldn't use the same update method + public function assignVacancies(Request $request, Team $team) + { + // P.S. To future developers + // This method gave me a lot of trouble lol. It's hard to write code when you're half asleep. + // There may be an n+1 query in the view and I don't think there's a way to avoid that without writing a lot of extra code. + + $requestVacancies = $request->assocVacancies; + $currentVacancies = $team->vacancies->pluck('id')->all(); + + if (is_null($requestVacancies)) + { + + foreach ($team->vacancies as $vacancy) + { + $team->vacancies()->detach($vacancy->id); + } + + $request->session()->flash('success', 'Removed all vacancy associations.'); + return redirect()->back(); + } + + $vacancyDiff = array_diff($requestVacancies, $currentVacancies); + $deselectedDiff = array_diff($currentVacancies, $requestVacancies); + + + if (!empty($vacancyDiff) || !empty($deselectedDiff)) + { + foreach ($vacancyDiff as $selectedVacancy) + { + $team->vacancies()->attach($selectedVacancy); + } + + foreach ($deselectedDiff as $deselectedVacancy) + { + $team->vacancies()->detach($deselectedVacancy); + } + } + else + { + $team->vacancies()->attach($requestVacancies); + } + + $request->session()->flash('success', 'Assignments changed successfully.'); + return redirect()->back(); + + } } diff --git a/app/Vacancy.php b/app/Vacancy.php index 3308acf..e205074 100644 --- a/app/Vacancy.php +++ b/app/Vacancy.php @@ -80,4 +80,32 @@ class Vacancy extends Model } + + /** + * Check if the Modal is attached to the $checkingTeam Model + * + * @param Team $checkingTeam The mdoel you want to check against + * @return boolean Whether the models are attached + */ + public function hasTeam(Team $checkingTeam): bool + { + $myTeams = $this->teams; + + if (empty($myTeams)) + { + // no associated teams + return false; + } + + foreach($myTeams as $team) + { + if ($team->id === $checkingTeam->id) + { + return true; + } + } + + return false; + } + } diff --git a/composer.json b/composer.json index bdc00e1..8950634 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ "ext-imagick": "*", "ext-json": "*", "arcanedev/log-viewer": "^8.0", + "awssat/discord-notification-channel": "^1.4", "doctrine/dbal": "^2.10", "fideloper/proxy": "^4.2", "fruitcake/laravel-cors": "^1.0", diff --git a/composer.lock b/composer.lock index a3ea849..95545bf 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "db269f8f20690fd92cc9b51dbbca3fc5", + "content-hash": "b91b3c69e28100abbffdb9bb256025fd", "packages": [ { "name": "almasaeed2010/adminlte", @@ -225,6 +225,61 @@ ], "time": "2019-12-24T22:41:47+00:00" }, + { + "name": "awssat/discord-notification-channel", + "version": "1.4.0", + "source": { + "type": "git", + "url": "https://github.com/awssat/discord-notification-channel.git", + "reference": "514df4bb5db48f624658240d6b1f9b8ee468a46f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/awssat/discord-notification-channel/zipball/514df4bb5db48f624658240d6b1f9b8ee468a46f", + "reference": "514df4bb5db48f624658240d6b1f9b8ee468a46f", + "shasum": "" + }, + "require": { + "guzzlehttp/guzzle": "^6.0|^7.0", + "illuminate/notifications": "~5.8.0|^6.0|^7.0|^8.0", + "laravel/slack-notification-channel": "^2.0", + "php": "^7.1.3" + }, + "require-dev": { + "mockery/mockery": "^1.0", + "phpunit/phpunit": "^7.0|^8.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Awssat\\Notifications\\DiscordChannelServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Awssat\\Notifications\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bader Almutairi", + "email": "phpfalcon@gmail.com" + } + ], + "description": "Discord Notification Channel for laravel.", + "keywords": [ + "discord", + "laravel", + "notifications" + ], + "time": "2020-09-09T20:42:43+00:00" + }, { "name": "bacon/bacon-qr-code", "version": "2.0.2", diff --git a/resources/views/dashboard/teams/edit-team.blade.php b/resources/views/dashboard/teams/edit-team.blade.php index 169f4a0..ae9990a 100644 --- a/resources/views/dashboard/teams/edit-team.blade.php +++ b/resources/views/dashboard/teams/edit-team.blade.php @@ -60,13 +60,13 @@ @foreach ($team->users as $teammate) - + {{ $teammate->id }} {{ $teammate->name }} @foreach ($teammate->roles as $teammate_role) - + {{ $teammate_role->name }} @endforeach @@ -123,21 +123,21 @@
- +
@csrf @method('PATCH')
- + - + Markdown supported - +
@@ -147,11 +147,11 @@
- +
- +
@@ -200,4 +210,4 @@ -@stop \ No newline at end of file +@stop diff --git a/routes/web.php b/routes/web.php index 095d535..4467c7f 100644 --- a/routes/web.php +++ b/routes/web.php @@ -60,22 +60,24 @@ Route::group(['prefix' => LaravelLocalization::setLocale(), 'middleware' => [ 'l ->name('directory'); + + Route::resource('teams', TeamController::class); + - Route::post('teams/{team}/invites/send', [TeamController::class, 'invite']) + Route::post('teams/{team}/invites/send', [TeamController::class, 'invite']) ->name('sendInvite'); - Route::get('teams/{team}/switch', [TeamController::class, 'switchTeam']) + Route::get('teams/{team}/switch', [TeamController::class, 'switchTeam']) ->name('switchTeam'); + Route::patch('teams/{team}/vacancies/update', [TeamController::class, 'assignVacancies']) + ->name('assignVacancies'); + + Route::get('teams/invites/{action}/{token}', [TeamController::class, 'processInviteAction']) ->name('processInvite'); - - - // WARNING: This is a resource, might not work under laravel 8. - Route::resource('teams', TeamController::class); - - + Route::group(['prefix' => '/applications'], function (){ @@ -251,7 +253,7 @@ Route::group(['prefix' => LaravelLocalization::setLocale(), 'middleware' => [ 'l Route::delete('forms/destroy/{form}', [FormController::class, 'destroy']) ->name('destroyForm'); - Route::get('forms', [FormController::class, 'showFormBuilder']) + Route::get('forms', [FormController::class, 'index']) ->name('showForms'); Route::get('forms/preview/{form}', [FormController::class, 'preview'])