-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to add members to project (#301)
- Loading branch information
Showing
23 changed files
with
990 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
app/Http/Controllers/Company/Project/ProjectMembersController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Company\Project; | ||
|
||
use Carbon\Carbon; | ||
use Inertia\Inertia; | ||
use Inertia\Response; | ||
use App\Helpers\DateHelper; | ||
use Illuminate\Http\Request; | ||
use App\Helpers\InstanceHelper; | ||
use App\Models\Company\Project; | ||
use Illuminate\Http\JsonResponse; | ||
use App\Helpers\NotificationHelper; | ||
use App\Http\Controllers\Controller; | ||
use App\Services\Company\Project\AddEmployeeToProject; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use App\Http\ViewHelpers\Project\ProjectMembersViewHelper; | ||
use App\Services\Company\Project\RemoveEmployeeFromProject; | ||
|
||
class ProjectMembersController extends Controller | ||
{ | ||
/** | ||
* Display the list of members in the project. | ||
* | ||
* @param Request $request | ||
* @param int $companyId | ||
* @param int $projectId | ||
*/ | ||
public function index(Request $request, int $companyId, int $projectId) | ||
{ | ||
$company = InstanceHelper::getLoggedCompany(); | ||
|
||
try { | ||
$project = Project::where('company_id', $company->id) | ||
->with('employees') | ||
->findOrFail($projectId); | ||
} catch (ModelNotFoundException $e) { | ||
return redirect('home'); | ||
} | ||
|
||
return Inertia::render('Project/Members/Index', [ | ||
'tab' => 'members', | ||
'project' => ProjectMembersViewHelper::info($project), | ||
'members' => ProjectMembersViewHelper::members($project), | ||
'notifications' => NotificationHelper::getNotifications(InstanceHelper::getLoggedEmployee()), | ||
]); | ||
} | ||
|
||
/** | ||
* Returns all potential members, displayed in the Add member modal. | ||
* | ||
* @param Request $request | ||
* @param int $companyId | ||
* @param int $projectId | ||
* @return JsonResponse | ||
*/ | ||
public function search(Request $request, int $companyId, int $projectId): JsonResponse | ||
{ | ||
$company = InstanceHelper::getLoggedCompany(); | ||
|
||
try { | ||
$project = Project::where('company_id', $company->id) | ||
->with('employees') | ||
->findOrFail($projectId); | ||
} catch (ModelNotFoundException $e) { | ||
return redirect('home'); | ||
} | ||
|
||
$potentialMembers = ProjectMembersViewHelper::potentialMembers($project); | ||
|
||
return response()->json([ | ||
'data' => $potentialMembers, | ||
], 200); | ||
} | ||
|
||
/** | ||
* Add an employee to the project. | ||
* | ||
* @param Request $request | ||
* @param int $companyId | ||
* @param int $projectId | ||
* @return JsonResponse | ||
*/ | ||
public function store(Request $request, int $companyId, int $projectId): JsonResponse | ||
{ | ||
$loggedEmployee = InstanceHelper::getLoggedEmployee(); | ||
$loggedCompany = InstanceHelper::getLoggedCompany(); | ||
|
||
$data = [ | ||
'company_id' => $loggedCompany->id, | ||
'author_id' => $loggedEmployee->id, | ||
'project_id' => $projectId, | ||
'employee_id' => $request->input('employee.value'), | ||
'role' => $request->input('role'), | ||
]; | ||
|
||
$employee = (new AddEmployeeToProject)->execute($data); | ||
|
||
return response()->json([ | ||
'data' => [ | ||
'id' => $employee->id, | ||
'name' => $employee->name, | ||
'avatar' => $employee->avatar, | ||
'role' => $request->input('role'), | ||
'added_at' => DateHelper::formatDate(Carbon::now()), | ||
'position' => (! $employee->position) ? null : [ | ||
'id' => $employee->position->id, | ||
'title' => $employee->position->title, | ||
], | ||
'url' => route('employees.show', [ | ||
'company' => $loggedCompany, | ||
'employee' => $employee, | ||
]), | ||
], | ||
], 201); | ||
} | ||
|
||
/** | ||
* Remove an employee from the project. | ||
* | ||
* @param Request $request | ||
* @param int $companyId | ||
* @param int $projectId | ||
* @return JsonResponse | ||
*/ | ||
public function remove(Request $request, int $companyId, int $projectId): JsonResponse | ||
{ | ||
$loggedEmployee = InstanceHelper::getLoggedEmployee(); | ||
$loggedCompany = InstanceHelper::getLoggedCompany(); | ||
|
||
$data = [ | ||
'company_id' => $loggedCompany->id, | ||
'author_id' => $loggedEmployee->id, | ||
'project_id' => $projectId, | ||
'employee_id' => $request->input('employee'), | ||
]; | ||
|
||
(new RemoveEmployeeFromProject)->execute($data); | ||
|
||
return response()->json([ | ||
'data' => [ | ||
'id' => $request->input('employee.value'), | ||
], | ||
], 201); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
app/Http/ViewHelpers/Project/ProjectMembersViewHelper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace App\Http\ViewHelpers\Project; | ||
|
||
use App\Helpers\DateHelper; | ||
use App\Models\Company\Project; | ||
|
||
class ProjectMembersViewHelper | ||
{ | ||
/** | ||
* Array containing the information all the members in the project, and all | ||
* the roles. | ||
* | ||
* @param Project $project | ||
* @return array | ||
*/ | ||
public static function members(Project $project): array | ||
{ | ||
$members = $project->employees() | ||
->where('locked', false) | ||
->with('position') | ||
->orderBy('pivot_created_at', 'desc') | ||
->get(); | ||
|
||
$membersCollection = collect([]); | ||
$roles = collect([]); | ||
foreach ($members as $member) { | ||
$membersCollection->push([ | ||
'id' => $member->id, | ||
'name' => $member->name, | ||
'avatar' => $member->avatar, | ||
'role' => $member->pivot->role, | ||
'added_at' => DateHelper::formatDate($member->pivot->created_at), | ||
'position' => (! $member->position) ? null : [ | ||
'id' => $member->position->id, | ||
'title' => $member->position->title, | ||
], | ||
'url' => route('employees.show', [ | ||
'company' => $project->company_id, | ||
'employee' => $member, | ||
]), | ||
]); | ||
|
||
if ($member->pivot->role) { | ||
$roles->push([ | ||
'id' => $member->id, | ||
'role' => $member->pivot->role, | ||
]); | ||
} | ||
} | ||
|
||
// filter the unique roles in the collection | ||
$roles = $roles->unique('role')->sortBy('role'); | ||
|
||
return [ | ||
'members' => $membersCollection, | ||
'roles' => $roles, | ||
]; | ||
} | ||
|
||
/** | ||
* Array containing the information about the project itself. | ||
* | ||
* @param Project $project | ||
* @return array | ||
*/ | ||
public static function info(Project $project): array | ||
{ | ||
return [ | ||
'id' => $project->id, | ||
'name' => $project->name, | ||
'code' => $project->code, | ||
'summary' => $project->summary, | ||
]; | ||
} | ||
|
||
/** | ||
* Returns the potential employees that can be assigned as members. | ||
* This filters out the current members of the project (doh). | ||
* It also contains all the current roles currently used in the project. | ||
* | ||
* @param Project $project | ||
* @return array | ||
*/ | ||
public static function potentialMembers(Project $project): array | ||
{ | ||
$company = $project->company; | ||
$employees = $company->employees() | ||
->where('locked', false) | ||
->get(); | ||
|
||
$currentMembers = $project->employees; | ||
|
||
$potentialMembers = $employees->diff($currentMembers); | ||
|
||
$potentialMembersCollection = collect([]); | ||
foreach ($potentialMembers as $potential) { | ||
$potentialMembersCollection->push([ | ||
'value' => $potential->id, | ||
'label' => $potential->name, | ||
]); | ||
} | ||
|
||
return [ | ||
'potential_members' => $potentialMembersCollection, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.