Skip to content
This repository has been archived by the owner on Feb 8, 2025. It is now read-only.

Commit

Permalink
fix: fix search function to accept null criteria as empty query string (
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Jun 30, 2021
1 parent 07901c2 commit bae676b
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 181 deletions.
24 changes: 10 additions & 14 deletions app/Http/ViewHelpers/Adminland/AdminExpenseViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public static function employees($company): ?Collection
* Search all employees matching a given criteria.
*
* @param Company $company
* @param string $criteria
* @param string|null $criteria
* @return Collection
*/
public static function search(Company $company, string $criteria): Collection
public static function search(Company $company, ?string $criteria): Collection
{
$employees = $company->employees()
return $company->employees()
->select('id', 'first_name', 'last_name', 'avatar_file_id')
->notLocked()
->where(function ($query) use ($criteria) {
Expand All @@ -78,16 +78,12 @@ public static function search(Company $company, string $criteria): Collection
->where('can_manage_expenses', false)
->orderBy('last_name', 'asc')
->take(10)
->get();

$employeesCollection = collect([]);
foreach ($employees as $employee) {
$employeesCollection->push([
'id' => $employee->id,
'name' => $employee->name,
]);
}

return $employeesCollection;
->get()
->map(function ($employee) {
return [
'id' => $employee->id,
'name' => $employee->name,
];
});
}
}
7 changes: 3 additions & 4 deletions app/Http/ViewHelpers/Adminland/AdminSoftwareViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,15 @@ public static function seats($employees, Company $company): Collection
*
* @param Software $software
* @param Company $company
* @param string $criteria
* @param string|null $criteria
* @return Collection
*/
public static function getPotentialEmployees(Software $software, Company $company, string $criteria): Collection
public static function getPotentialEmployees(Software $software, Company $company, ?string $criteria): Collection
{
// get list of employees who have the software
$employees = $software->employees()
->select('id')
->pluck('id')
->toArray();
->pluck('id');

$potentialEmployees = $company->employees()
->select('id', 'first_name', 'last_name', 'avatar_file_id')
Expand Down
26 changes: 11 additions & 15 deletions app/Http/ViewHelpers/Company/Group/GroupCreateViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class GroupCreateViewHelper
* Search all potential members for the group.
*
* @param Company $company
* @param string $criteria
* @param string|null $criteria
* @return Collection
*/
public static function search(Company $company, string $criteria): Collection
public static function search(Company $company, ?string $criteria): Collection
{
$potentialEmployees = $company->employees()
return $company->employees()
->select('id', 'first_name', 'last_name', 'avatar_file_id')
->notLocked()
->where(function ($query) use ($criteria) {
Expand All @@ -28,17 +28,13 @@ public static function search(Company $company, string $criteria): Collection
})
->orderBy('last_name', 'asc')
->take(10)
->get();

$employeesCollection = collect([]);
foreach ($potentialEmployees as $employee) {
$employeesCollection->push([
'id' => $employee->id,
'name' => $employee->name,
'avatar' => ImageHelper::getAvatar($employee, 23),
]);
}

return $employeesCollection;
->get()
->map(function ($employee) {
return [
'id' => $employee->id,
'name' => $employee->name,
'avatar' => ImageHelper::getAvatar($employee, 23),
];
});
}
}
29 changes: 12 additions & 17 deletions app/Http/ViewHelpers/Company/Group/GroupMeetingsViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,16 @@ public static function show(Meeting $meeting, Company $company): array
*
* @param Meeting $meeting
* @param Company $company
* @param string $criteria
* @param string|null $criteria
* @return Collection
*/
public static function potentialGuests(Meeting $meeting, Company $company, string $criteria): Collection
public static function potentialGuests(Meeting $meeting, Company $company, ?string $criteria): Collection
{
$members = $meeting->employees()
->select('id', 'first_name', 'last_name')
->pluck('id')
->toArray();
->select('id')
->pluck('id');

$potentialGuests = $company->employees()
return $company->employees()
->select('id', 'first_name', 'last_name')
->notLocked()
->whereNotIn('id', $members)
Expand All @@ -149,17 +148,13 @@ public static function potentialGuests(Meeting $meeting, Company $company, strin
})
->orderBy('last_name', 'asc')
->take(10)
->get();

$employeesCollection = collect([]);
foreach ($potentialGuests as $employee) {
$employeesCollection->push([
'id' => $employee->id,
'name' => $employee->name,
]);
}

return $employeesCollection;
->get()
->map(function ($employee) {
return [
'id' => $employee->id,
'name' => $employee->name,
];
});
}

/**
Expand Down
4 changes: 1 addition & 3 deletions app/Http/ViewHelpers/Company/HR/CompanyHRViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ public static function eCoffees(Company $company): ?array
$allMatchesInCompany = DB::table('e_coffees')
->where('company_id', $company->id)
->select('id')
->get()
->pluck('id')
->toArray();
->pluck('id');

// list all matches for each ecoffee sessions
$eCoffeeMatchesParticipated = DB::table('e_coffee_matches')
Expand Down
26 changes: 11 additions & 15 deletions app/Http/ViewHelpers/Company/Project/ProjectDecisionsViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public static function decisions(Project $project, Employee $employee): Collecti
* Search all employees matching a given criteria.
*
* @param Company $company
* @param string $criteria
* @param string|null $criteria
* @return Collection
*/
public static function searchDeciders(Company $company, string $criteria): Collection
public static function searchDeciders(Company $company, ?string $criteria): Collection
{
$employees = $company->employees()
return $company->employees()
->select('id', 'first_name', 'last_name', 'avatar_file_id')
->notLocked()
->where(function ($query) use ($criteria) {
Expand All @@ -73,17 +73,13 @@ public static function searchDeciders(Company $company, string $criteria): Colle
})
->orderBy('last_name', 'asc')
->take(10)
->get();

$employeesCollection = collect([]);
foreach ($employees as $employee) {
$employeesCollection->push([
'id' => $employee->id,
'name' => $employee->name,
'avatar' => ImageHelper::getAvatar($employee, 23),
]);
}

return $employeesCollection;
->get()
->map(function ($employee) {
return [
'id' => $employee->id,
'name' => $employee->name,
'avatar' => ImageHelper::getAvatar($employee, 23),
];
});
}
}
26 changes: 11 additions & 15 deletions app/Http/ViewHelpers/Company/Project/ProjectViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ public static function info(Project $project): array
* Search all employees matching a given criteria.
*
* @param Company $company
* @param string $criteria
* @param string|null $criteria
* @return Collection
*/
public static function searchProjectLead(Company $company, string $criteria): Collection
public static function searchProjectLead(Company $company, ?string $criteria): Collection
{
$employees = $company->employees()
return $company->employees()
->select('id', 'first_name', 'last_name', 'avatar_file_id', 'email')
->notLocked()
->where(function ($query) use ($criteria) {
Expand All @@ -229,17 +229,13 @@ public static function searchProjectLead(Company $company, string $criteria): Co
})
->orderBy('last_name', 'asc')
->take(10)
->get();

$employeesCollection = collect([]);
foreach ($employees as $employee) {
$employeesCollection->push([
'id' => $employee->id,
'name' => $employee->name,
'avatar' => ImageHelper::getAvatar($employee, 23),
]);
}

return $employeesCollection;
->get()
->map(function ($employee) {
return [
'id' => $employee->id,
'name' => $employee->name,
'avatar' => ImageHelper::getAvatar($employee, 23),
];
});
}
}
4 changes: 1 addition & 3 deletions app/Http/ViewHelpers/Dashboard/DashboardHRViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ public static function employeesWithoutManagersWithPendingTimesheets(Company $co
$listOfEmployeesWithManagers = DB::table('direct_reports')
->where('company_id', $company->id)
->select('employee_id')
->get()
->pluck('employee_id')
->toArray();
->pluck('employee_id');

$timesheetsWithUniqueEmployees = $timesheets->unique('employee_id');
$timesheetsWithUniqueEmployees = $timesheetsWithUniqueEmployees->whereNotIn('employee_id', $listOfEmployeesWithManagers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public static function timesheetApprovalsForEmployeesWithoutManagers(Company $co
$listOfEmployeesWithManagers = DB::table('direct_reports')
->where('company_id', $company->id)
->select('employee_id')
->get()
->pluck('employee_id')
->toArray();
->pluck('employee_id');

$timesheetsWithUniqueEmployees = $timesheets->unique('employee_id');
$timesheetsWithUniqueEmployees = $timesheetsWithUniqueEmployees->whereNotIn('employee_id', $listOfEmployeesWithManagers);
Expand Down
41 changes: 19 additions & 22 deletions app/Http/ViewHelpers/Employee/EmployeeHierarchyViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ class EmployeeHierarchyViewHelper
*
* @param Company $company
* @param Employee $employee
* @param string $criteria
* @param string|null $criteria
* @return Collection
*/
public static function search(Company $company, Employee $employee, string $criteria): Collection
public static function search(Company $company, Employee $employee, ?string $criteria): Collection
{
$potentialEmployees = $company->employees()
// remove the existing managers of this employee from the list
$existingManagersForTheEmployee = $employee->getListOfManagers()
->pluck('id');

// remove the existing direct reports of this employee from the list
$existingDirectReportsForTheEmployee = $employee->getListOfDirectReports()
->pluck('id');

return $company->employees()
->select('id', 'first_name', 'last_name')
->notLocked()
->where(function ($query) use ($criteria) {
Expand All @@ -27,26 +35,15 @@ public static function search(Company $company, Employee $employee, string $crit
->orWhere('email', 'LIKE', '%'.$criteria.'%');
})
->where('id', '!=', $employee->id)
->whereNotIn('id', $existingManagersForTheEmployee->merge($existingDirectReportsForTheEmployee))
->orderBy('last_name', 'asc')
->take(10)
->get();

// remove the existing managers of this employee from the list
$existingManagersForTheEmployee = $employee->getListOfManagers();
$potentialEmployees = $potentialEmployees->diff($existingManagersForTheEmployee);

// remove the existing direct reports of this employee from the list
$existingDirectReportsForTheEmployee = $employee->getListOfDirectReports();
$potentialEmployees = $potentialEmployees->diff($existingDirectReportsForTheEmployee);

$employeesCollection = collect([]);
foreach ($potentialEmployees as $employee) {
$employeesCollection->push([
'id' => $employee->id,
'name' => $employee->name,
]);
}

return $employeesCollection;
->get()
->map(function ($employee) {
return [
'id' => $employee->id,
'name' => $employee->name,
];
});
}
}
3 changes: 1 addition & 2 deletions app/Http/ViewHelpers/Employee/EmployeeSkillViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public static function search(Company $company, Employee $employee, ?string $cri

$employeeSkills = $employee->skills()
->select('id')
->pluck('id')
->toArray();
->pluck('id');

return $company->skills()
->select('id', 'name')
Expand Down
35 changes: 15 additions & 20 deletions app/Http/ViewHelpers/Team/TeamMembersViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,33 @@ class TeamMembersViewHelper
*
* @param Company $company
* @param Team $team
* @param string $criteria
* @param string|null $criteria
* @return Collection
*/
public static function searchPotentialTeamMembers(Company $company, Team $team, string $criteria): Collection
public static function searchPotentialTeamMembers(Company $company, Team $team, ?string $criteria): Collection
{
$potentialEmployees = $company->employees()
$employeesInTeam = $team->employees()
->select('id')
->pluck('id');

return $company->employees()
->select('id', 'first_name', 'last_name', 'email')
->notLocked()
->where(function ($query) use ($criteria) {
$query->where('first_name', 'LIKE', '%'.$criteria.'%')
->orWhere('last_name', 'LIKE', '%'.$criteria.'%')
->orWhere('email', 'LIKE', '%'.$criteria.'%');
})
->whereNotIn('id', $employeesInTeam)
->orderBy('last_name', 'asc')
->take(10)
->get();

$employeesInTeam = $team->employees()
->select('id', 'first_name', 'last_name')
->get();

$potentialEmployees = $potentialEmployees->diff($employeesInTeam);

$employeesCollection = collect([]);
foreach ($potentialEmployees as $employee) {
$employeesCollection->push([
'id' => $employee->id,
'name' => $employee->name,
]);
}

return $employeesCollection;
->get()
->map(function ($employee) {
return [
'id' => $employee->id,
'name' => $employee->name,
];
});
}

/**
Expand Down
Loading

0 comments on commit bae676b

Please sign in to comment.