Skip to content

Commit

Permalink
feat: Expand Sorting Options for Jobs (#499)
Browse files Browse the repository at this point in the history
* add sort by salary

* misc changes to salary sort

* add view counter and sorting

* add Most Applied sorting

* misc optimisation

* only keep salary sort options

---------

Co-authored-by: Arpit Khandelwal <[email protected]>
  • Loading branch information
Arpit-Khandelwal and basedD3v authored Oct 16, 2024
1 parent 0304925 commit f186265
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/lib/constant/jobs.constant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export enum SortByEnums {
POSTEDAT_ASC = 'postedat_asc',
POSTEDAT_DESC = 'postedat_desc',
MAXSALARY_ASC = 'maxsalary_asc',
MAXSALARY_DESC = 'maxsalary_desc',
}
export const filters = {
salaryRange: [
Expand Down Expand Up @@ -43,4 +45,14 @@ export const jobSorting = [
label: 'Oldest Jobs',
value: 'postedat_asc',
},
{
id: 3,
label: 'Lowest Salary',
value: 'maxsalary_asc',
},
{
id: 4,
label: 'Highest Salary',
value: 'maxsalary_desc',
},
];
3 changes: 2 additions & 1 deletion src/lib/validators/jobs.validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from 'zod';
import { WorkMode, EmployementType, Currency } from '@prisma/client';
import { SortByEnums } from '../constant/jobs.constant';

export const JobPostSchema = z
.object({
Expand Down Expand Up @@ -133,7 +134,7 @@ export const JobQuerySchema = z.object({
}
return val;
}),
sortby: z.enum(['postedat_asc', 'postedat_desc']).default('postedat_desc'),
sortby: z.nativeEnum(SortByEnums).default(SortByEnums.POSTEDAT_ASC),
page: z.coerce
.number({ message: 'page must be a number' })
.optional()
Expand Down
8 changes: 5 additions & 3 deletions src/services/jobs.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ export function getJobFilters({
};
const sortFieldMapping: { [key: string]: string } = {
postedat: 'postedAt',
maxsalary: 'maxSalary',
};
const [sort, sortOrder] = sortby.split('_');
const orderBy: Prisma.JobOrderByWithAggregationInput = {
...(sortby && { [sortFieldMapping[sort]]: sortOrder }),
};
const orderBy: Prisma.JobOrderByWithAggregationInput = sortby
? { [sortFieldMapping[sort]]: sortOrder }
: {};

const pagination = {
skip: 0,
take: limit || JOBS_PER_PAGE,
Expand Down

0 comments on commit f186265

Please sign in to comment.