[offers][fix] Fix the sort and filter checks in list offers API

pull/402/head
BryannYeap 2 years ago
parent 283333e1ee
commit 111b078147

@ -35,34 +35,6 @@ const COMPANIES = [
}, },
]; ];
const OFFER_PROFILES = [
{
id: 'cl91v97ex000109mt7fka5rto',
profileName: 'battery-horse-stable-cow',
editToken: 'cl91ulmhg000009l86o45aspt',
},
{
id: 'cl91v9iw2000209mtautgdnxq',
profileName: 'house-zebra-fast-giraffe',
editToken: 'cl91umigc000109l80f1tcqe8',
},
{
id: 'cl91v9m3y000309mt1ctw55wi',
profileName: 'keyboard-mouse-lazy-cat',
editToken: 'cl91ummoa000209l87q2b8hl7',
},
{
id: 'cl91v9p09000409mt5rvoasf1',
profileName: 'router-hen-bright-pig',
editToken: 'cl91umqa3000309l87jyefe9k',
},
{
id: 'cl91v9uda000509mt5i5fez3v',
profileName: 'screen-ant-dirty-bird',
editToken: 'cl91umuj9000409l87ez85vmg',
},
];
async function main() { async function main() {
console.log('Seeding started...'); console.log('Seeding started...');
await Promise.all([ await Promise.all([
@ -73,13 +45,6 @@ async function main() {
create: company, create: company,
}); });
}), }),
OFFER_PROFILES.map(async (offerProfile) => {
await prisma.offersProfile.upsert({
where: { profileName: offerProfile.profileName },
update: offerProfile,
create: offerProfile,
});
}),
]); ]);
console.log('Seeding completed.'); console.log('Seeding completed.');
} }

@ -77,7 +77,7 @@ function Test() {
message: 'wassup bro', message: 'wassup bro',
profileId: 'cl9efyn9p004ww3u42mjgl1vn', profileId: 'cl9efyn9p004ww3u42mjgl1vn',
replyingToId: 'cl9el4xj10001w3w21o3p2iny', replyingToId: 'cl9el4xj10001w3w21o3p2iny',
userId: 'cl9ehvpng0000w3ec2mpx0bdd' userId: 'cl9ehvpng0000w3ec2mpx0bdd',
}); });
}; };
@ -218,7 +218,6 @@ function Test() {
}, },
); );
// Console.log(replies.data?.data)
const deleteMutation = trpc.useMutation(['offers.profile.delete']); const deleteMutation = trpc.useMutation(['offers.profile.delete']);
const handleDelete = (id: string) => { const handleDelete = (id: string) => {

@ -9,8 +9,8 @@ function Test() {
limit: 100, limit: 100,
location: 'Singapore, Singapore', location: 'Singapore, Singapore',
offset: 0, offset: 0,
sortBy: '-totalYoe', sortBy: '+totalCompensation',
yoeCategory: 2, yoeCategory: 1,
}, },
]); ]);

@ -43,7 +43,7 @@ export const offersRouter = createRouter().query('list', {
limit: z.number().positive(), limit: z.number().positive(),
location: z.string(), location: z.string(),
offset: z.number().nonnegative(), offset: z.number().nonnegative(),
salaryMax: z.number().nullish(), salaryMax: z.number().nonnegative().nullish(),
salaryMin: z.number().nonnegative().nullish(), salaryMin: z.number().nonnegative().nullish(),
sortBy: z.string().regex(createSortByValidationRegex()).nullish(), sortBy: z.string().regex(createSortByValidationRegex()).nullish(),
title: z.string().nullish(), title: z.string().nullish(),
@ -154,38 +154,47 @@ export const offersRouter = createRouter().query('list', {
data = data.filter((offer) => { data = data.filter((offer) => {
let validRecord = true; let validRecord = true;
if (input.companyId) { if (input.companyId && input.companyId.length !== 0) {
validRecord = validRecord && offer.company.id === input.companyId; validRecord = validRecord && offer.company.id === input.companyId;
} }
if (input.title) { if (input.title && input.title.length !== 0) {
validRecord = validRecord =
validRecord && validRecord &&
(offer.offersFullTime?.title === input.title || (offer.offersFullTime?.title === input.title ||
offer.offersIntern?.title === input.title); offer.offersIntern?.title === input.title);
} }
if (input.dateStart && input.dateEnd) { if (
input.dateStart &&
input.dateEnd &&
input.dateStart.getTime() <= input.dateEnd.getTime()
) {
validRecord = validRecord =
validRecord && validRecord &&
offer.monthYearReceived.getTime() >= input.dateStart.getTime() && offer.monthYearReceived.getTime() >= input.dateStart.getTime() &&
offer.monthYearReceived.getTime() <= input.dateEnd.getTime(); offer.monthYearReceived.getTime() <= input.dateEnd.getTime();
} }
if (input.salaryMin && input.salaryMax) { if (input.salaryMin != null || input.salaryMax != null) {
const salary = offer.offersFullTime?.totalCompensation.value const salary = offer.offersFullTime?.totalCompensation.value
? offer.offersFullTime?.totalCompensation.value ? offer.offersFullTime?.totalCompensation.value
: offer.offersIntern?.monthlySalary.value; : offer.offersIntern?.monthlySalary.value;
if (!salary) { if (salary == null) {
throw new TRPCError({ throw new TRPCError({
code: 'NOT_FOUND', code: 'NOT_FOUND',
message: 'Total Compensation or Salary not found', message: 'Total Compensation or Salary not found',
}); });
} }
validRecord = if (input.salaryMin != null) {
validRecord && salary >= input.salaryMin && salary <= input.salaryMax; validRecord = validRecord && salary >= input.salaryMin;
}
if (input.salaryMax != null) {
validRecord = validRecord && salary <= input.salaryMax;
}
} }
return validRecord; return validRecord;
@ -221,7 +230,7 @@ export const offersRouter = createRouter().query('list', {
? offer2.offersFullTime?.totalCompensation.value ? offer2.offersFullTime?.totalCompensation.value
: offer2.offersIntern?.monthlySalary.value; : offer2.offersIntern?.monthlySalary.value;
if (!salary1 || !salary2) { if (salary1 == null || salary2 == null) {
throw new TRPCError({ throw new TRPCError({
code: 'NOT_FOUND', code: 'NOT_FOUND',
message: 'Total Compensation or Salary not found', message: 'Total Compensation or Salary not found',
@ -235,7 +244,7 @@ export const offersRouter = createRouter().query('list', {
const yoe1 = offer1.profile.background?.totalYoe; const yoe1 = offer1.profile.background?.totalYoe;
const yoe2 = offer2.profile.background?.totalYoe; const yoe2 = offer2.profile.background?.totalYoe;
if (!yoe1 || !yoe2) { if (yoe1 == null || yoe2 == null) {
throw new TRPCError({ throw new TRPCError({
code: 'NOT_FOUND', code: 'NOT_FOUND',
message: 'Total years of experience not found', message: 'Total years of experience not found',
@ -267,7 +276,7 @@ export const offersRouter = createRouter().query('list', {
? offer2.offersFullTime?.totalCompensation.value ? offer2.offersFullTime?.totalCompensation.value
: offer2.offersIntern?.monthlySalary.value; : offer2.offersIntern?.monthlySalary.value;
if (!salary1 || !salary2) { if (salary1 == null || salary2 == null) {
throw new TRPCError({ throw new TRPCError({
code: 'NOT_FOUND', code: 'NOT_FOUND',
message: 'Total Compensation or Salary not found', message: 'Total Compensation or Salary not found',
@ -281,7 +290,7 @@ export const offersRouter = createRouter().query('list', {
const yoe1 = offer1.profile.background?.totalYoe; const yoe1 = offer1.profile.background?.totalYoe;
const yoe2 = offer2.profile.background?.totalYoe; const yoe2 = offer2.profile.background?.totalYoe;
if (!yoe1 || !yoe2) { if (yoe1 == null || yoe2 == null) {
throw new TRPCError({ throw new TRPCError({
code: 'NOT_FOUND', code: 'NOT_FOUND',
message: 'Total years of experience not found', message: 'Total years of experience not found',

Loading…
Cancel
Save