From c1bdc53730eacc0b2158e65cfdf7495ee2730856 Mon Sep 17 00:00:00 2001 From: Peirong <35712975+peironggg@users.noreply.github.com> Date: Tue, 8 Nov 2022 14:26:15 +0800 Subject: [PATCH] [resumes][feat] show all resumes when no filters selected (#535) --- .../router/resumes/resumes-resume-router.ts | 30 +++++----- .../resumes/resumes-resume-user-router.ts | 55 ++++++++++-------- .../portal/src/utils/resumes/resumeFilters.ts | 57 +------------------ ...etFilterCounts.ts => resumePrismaUtils.ts} | 18 ++++++ 4 files changed, 68 insertions(+), 92 deletions(-) rename apps/portal/src/utils/resumes/{resumeGetFilterCounts.ts => resumePrismaUtils.ts} (69%) diff --git a/apps/portal/src/server/router/resumes/resumes-resume-router.ts b/apps/portal/src/server/router/resumes/resumes-resume-router.ts index 54f4e099..330f9198 100644 --- a/apps/portal/src/server/router/resumes/resumes-resume-router.ts +++ b/apps/portal/src/server/router/resumes/resumes-resume-router.ts @@ -2,7 +2,10 @@ import { z } from 'zod'; import { Vote } from '@prisma/client'; import type { FilterCounts } from '~/utils/resumes/resumeFilters'; -import { resumeGetFilterCounts } from '~/utils/resumes/resumeGetFilterCounts'; +import { + getWhereClauseFilters, + resumeGetFilterCounts, +} from '~/utils/resumes/resumePrismaUtils'; import { createRouter } from '../context'; @@ -79,10 +82,12 @@ export const resumesRouter = createRouter() skip: isTop10 ? 0 : skip, take, where: { - experience: { in: experienceFilters }, + ...getWhereClauseFilters( + experienceFilters, + roleFilters, + locationFilters, + ), isResolved: isUnreviewed ? false : {}, - locationId: { in: locationFilters }, - role: { in: roleFilters }, title: { contains: searchValue, mode: 'insensitive' }, }, }); @@ -111,10 +116,12 @@ export const resumesRouter = createRouter() } else { totalRecords = await ctx.prisma.resumesResume.count({ where: { - experience: { in: experienceFilters }, + ...getWhereClauseFilters( + experienceFilters, + roleFilters, + locationFilters, + ), isResolved: isUnreviewed ? false : {}, - locationId: { in: locationFilters }, - role: { in: roleFilters }, title: { contains: searchValue, mode: 'insensitive' }, }, }); @@ -126,9 +133,8 @@ export const resumesRouter = createRouter() }, by: ['role'], where: { - experience: { in: experienceFilters }, + ...getWhereClauseFilters(experienceFilters, [], locationFilters), isResolved: isUnreviewed ? false : {}, - locationId: { in: locationFilters }, title: { contains: searchValue, mode: 'insensitive' }, }, }); @@ -145,8 +151,7 @@ export const resumesRouter = createRouter() by: ['experience'], where: { isResolved: isUnreviewed ? false : {}, - locationId: { in: locationFilters }, - role: { in: roleFilters }, + ...getWhereClauseFilters([], roleFilters, locationFilters), title: { contains: searchValue, mode: 'insensitive' }, }, }); @@ -160,9 +165,8 @@ export const resumesRouter = createRouter() }, by: ['locationId'], where: { - experience: { in: experienceFilters }, + ...getWhereClauseFilters(experienceFilters, roleFilters, []), isResolved: isUnreviewed ? false : {}, - role: { in: roleFilters }, title: { contains: searchValue, mode: 'insensitive' }, }, }); diff --git a/apps/portal/src/server/router/resumes/resumes-resume-user-router.ts b/apps/portal/src/server/router/resumes/resumes-resume-user-router.ts index bdd6d81c..1f522b33 100644 --- a/apps/portal/src/server/router/resumes/resumes-resume-user-router.ts +++ b/apps/portal/src/server/router/resumes/resumes-resume-user-router.ts @@ -1,7 +1,10 @@ import { z } from 'zod'; import type { FilterCounts } from '~/utils/resumes/resumeFilters'; -import { resumeGetFilterCounts } from '~/utils/resumes/resumeGetFilterCounts'; +import { + getWhereClauseFilters, + resumeGetFilterCounts, +} from '~/utils/resumes/resumePrismaUtils'; import { createProtectedRouter } from '../context'; @@ -141,10 +144,12 @@ export const resumesResumeUserRouter = createProtectedRouter() take, where: { resume: { - experience: { in: experienceFilters }, + ...getWhereClauseFilters( + experienceFilters, + roleFilters, + locationFilters, + ), isResolved: isUnreviewed ? false : {}, - locationId: { in: locationFilters }, - role: { in: roleFilters }, title: { contains: searchValue, mode: 'insensitive' }, }, userId, @@ -177,10 +182,12 @@ export const resumesResumeUserRouter = createProtectedRouter() totalRecords = await ctx.prisma.resumesStar.count({ where: { resume: { - experience: { in: experienceFilters }, + ...getWhereClauseFilters( + experienceFilters, + roleFilters, + locationFilters, + ), isResolved: isUnreviewed ? false : {}, - locationId: { in: locationFilters }, - role: { in: roleFilters }, title: { contains: searchValue, mode: 'insensitive' }, }, userId, @@ -193,9 +200,8 @@ export const resumesResumeUserRouter = createProtectedRouter() }, by: ['role'], where: { - experience: { in: experienceFilters }, + ...getWhereClauseFilters(experienceFilters, [], locationFilters), isResolved: isUnreviewed ? false : {}, - locationId: { in: locationFilters }, stars: { some: { userId, @@ -214,9 +220,8 @@ export const resumesResumeUserRouter = createProtectedRouter() }, by: ['experience'], where: { + ...getWhereClauseFilters([], roleFilters, locationFilters), isResolved: isUnreviewed ? false : {}, - locationId: { in: locationFilters }, - role: { in: roleFilters }, stars: { some: { userId, @@ -235,9 +240,8 @@ export const resumesResumeUserRouter = createProtectedRouter() }, by: ['locationId'], where: { - experience: { in: experienceFilters }, + ...getWhereClauseFilters(experienceFilters, roleFilters, []), isResolved: isUnreviewed ? false : {}, - role: { in: roleFilters }, stars: { some: { userId, @@ -328,10 +332,12 @@ export const resumesResumeUserRouter = createProtectedRouter() skip: isTop10 ? 0 : skip, take, where: { - experience: { in: experienceFilters }, + ...getWhereClauseFilters( + experienceFilters, + roleFilters, + locationFilters, + ), isResolved: isUnreviewed ? false : {}, - locationId: { in: locationFilters }, - role: { in: roleFilters }, title: { contains: searchValue, mode: 'insensitive' }, userId, }, @@ -361,10 +367,12 @@ export const resumesResumeUserRouter = createProtectedRouter() } else { totalRecords = await ctx.prisma.resumesResume.count({ where: { - experience: { in: experienceFilters }, + ...getWhereClauseFilters( + experienceFilters, + roleFilters, + locationFilters, + ), isResolved: isUnreviewed ? false : {}, - locationId: { in: locationFilters }, - role: { in: roleFilters }, title: { contains: searchValue, mode: 'insensitive' }, userId, }, @@ -376,9 +384,8 @@ export const resumesResumeUserRouter = createProtectedRouter() }, by: ['role'], where: { - experience: { in: experienceFilters }, + ...getWhereClauseFilters(experienceFilters, [], locationFilters), isResolved: isUnreviewed ? false : {}, - locationId: { in: locationFilters }, title: { contains: searchValue, mode: 'insensitive' }, userId, }, @@ -393,9 +400,8 @@ export const resumesResumeUserRouter = createProtectedRouter() }, by: ['experience'], where: { + ...getWhereClauseFilters([], roleFilters, locationFilters), isResolved: isUnreviewed ? false : {}, - locationId: { in: locationFilters }, - role: { in: roleFilters }, title: { contains: searchValue, mode: 'insensitive' }, userId, }, @@ -410,9 +416,8 @@ export const resumesResumeUserRouter = createProtectedRouter() }, by: ['locationId'], where: { - experience: { in: experienceFilters }, + ...getWhereClauseFilters(experienceFilters, roleFilters, []), isResolved: isUnreviewed ? false : {}, - role: { in: roleFilters }, title: { contains: searchValue, mode: 'insensitive' }, userId, }, diff --git a/apps/portal/src/utils/resumes/resumeFilters.ts b/apps/portal/src/utils/resumes/resumeFilters.ts index 4ff02371..6bc79fe4 100644 --- a/apps/portal/src/utils/resumes/resumeFilters.ts +++ b/apps/portal/src/utils/resumes/resumeFilters.ts @@ -71,24 +71,6 @@ export const SORT_OPTIONS: Array> = [ { label: 'Most Comments', value: 'mostComments' }, ]; -const INITIAL_ROLES_VALUES: Array = [ - 'software-engineer', - 'back-end-engineer', - 'front-end-engineer', - 'full-stack-engineer', - 'ios-engineer', - 'android-engineer', - 'data-engineer', -]; -export const INITIAL_ROLES: Array = INITIAL_ROLES_VALUES.map( - (value) => - getTypeaheadOption('role', value) ?? { - id: value, - label: value, - value, - }, -); - export const EXPERIENCES: Array = [ { id: 'internship', @@ -112,45 +94,12 @@ export const EXPERIENCES: Array = [ }, ]; -export const INITIAL_LOCATIONS: Array = [ - { - id: '196', - label: 'Singapore', - value: '196', - }, - { - id: '101', - label: 'India', - value: '101', - }, - { - id: '231', - label: 'United States', - value: '231', - }, - { - id: '230', - label: 'United Kingdom', - value: '230', - }, - { - id: '102', - label: 'Indonesia', - value: '102', - }, - { - id: '44', - label: 'China', - value: '44', - }, -]; - export const INITIAL_FILTER_STATE: FilterState = { - experience: EXPERIENCES, + experience: [], isTop10: false, isUnreviewed: true, - location: INITIAL_LOCATIONS, - role: INITIAL_ROLES, + location: [], + role: [], }; export const SHORTCUTS: Array = [ diff --git a/apps/portal/src/utils/resumes/resumeGetFilterCounts.ts b/apps/portal/src/utils/resumes/resumePrismaUtils.ts similarity index 69% rename from apps/portal/src/utils/resumes/resumeGetFilterCounts.ts rename to apps/portal/src/utils/resumes/resumePrismaUtils.ts index 896155ca..0cee397f 100644 --- a/apps/portal/src/utils/resumes/resumeGetFilterCounts.ts +++ b/apps/portal/src/utils/resumes/resumePrismaUtils.ts @@ -37,3 +37,21 @@ export function resumeGetFilterCounts(data: Array) { role: roleCounts, }; } + +export const getWhereClauseFilters = ( + experienceFilters: Array, + roleFilters: Array, + locationFilters: Array, +) => { + return { + ...(experienceFilters.length > 0 && { + experience: { in: experienceFilters }, + }), + ...(roleFilters.length > 0 && { + role: { in: roleFilters }, + }), + ...(locationFilters.length > 0 && { + locationId: { in: locationFilters }, + }), + }; +};