Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion apps/admin-dashboard/app/routes/api.companies.search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ export async function loader({ request }: LoaderFunctionArgs) {
const search = url.searchParams.get('search') || '';

const { companies } = await listCompanies({
includeCompaniesWithoutEmployeesOrOpportunities: true,
orderBy: 'most_employees',
pagination: {
limit: 100,
limit: 50,
page: 1,
},
select: ['companies.id', 'companies.name'],
Expand Down
25 changes: 17 additions & 8 deletions packages/core/src/modules/employment/queries/list-companies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { type PaginationSearchParams } from '@/shared/types';

type ListCompaniesOptions<Selection> = {
includeCompaniesWithoutEmployeesOrOpportunities?: boolean;
orderBy: ListCompaniesOrderBy;
pagination: PaginationSearchParams;
select: Selection[];
Expand All @@ -18,7 +19,13 @@ type ListCompaniesOptions<Selection> = {

export async function listCompanies<
Selection extends SelectExpression<DB, 'companies'>,
>({ orderBy, pagination, select, where }: ListCompaniesOptions<Selection>) {
>({
includeCompaniesWithoutEmployeesOrOpportunities = false,
orderBy,
pagination,
select,
where,
}: ListCompaniesOptions<Selection>) {
const query = db
.selectFrom('companies')
.leftJoin('opportunities', (join) => {
Expand All @@ -33,13 +40,15 @@ export async function listCompanies<
'workExperiences.id'
)
.where('workExperiences.deletedAt', 'is', null)
.where((eb) => {
// We only want to return companies that have at least one employee (past
// or present) or opportunity.
return eb.or([
eb('opportunities.companyId', 'is not', null),
eb('workExperiences.companyId', 'is not', null),
]);
.$if(!includeCompaniesWithoutEmployeesOrOpportunities, (qb) => {
// We only want to return companies that have at least one employee
// (past or present) or opportunity.
return qb.where((eb) => {
return eb.or([
eb('opportunities.companyId', 'is not', null),
eb('workExperiences.companyId', 'is not', null),
]);
});
})
.$if(!!where.search, (qb) => {
const { search } = where;
Expand Down
Loading