diff --git a/src/lib/constant/jobs.constant.ts b/src/lib/constant/jobs.constant.ts index 484ffdf3..3cc972ac 100644 --- a/src/lib/constant/jobs.constant.ts +++ b/src/lib/constant/jobs.constant.ts @@ -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: [ @@ -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', + }, ]; diff --git a/src/lib/validators/jobs.validator.ts b/src/lib/validators/jobs.validator.ts index 978b8326..5fa3abf6 100644 --- a/src/lib/validators/jobs.validator.ts +++ b/src/lib/validators/jobs.validator.ts @@ -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({ @@ -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() diff --git a/src/services/jobs.services.ts b/src/services/jobs.services.ts index 7534850a..c815cae5 100644 --- a/src/services/jobs.services.ts +++ b/src/services/jobs.services.ts @@ -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,