From bf39daba780aafada9da706996b56a5cce818bb1 Mon Sep 17 00:00:00 2001 From: amamc Date: Tue, 21 May 2024 12:26:50 -0700 Subject: [PATCH 1/2] Updated the function that returns the total number of postings to handle filtered postings and also separate filter object creation to prevent the repetition --- src/app/api/job-posting/siteRequestUtils.js | 36 +++++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/app/api/job-posting/siteRequestUtils.js b/src/app/api/job-posting/siteRequestUtils.js index 5a31d35..c493fda 100644 --- a/src/app/api/job-posting/siteRequestUtils.js +++ b/src/app/api/job-posting/siteRequestUtils.js @@ -9,11 +9,15 @@ const employmentSubTypeMap = { }; // Function to get the total number of job postings -export async function getTotalNumberOfPostings(siteCriteria) { +export async function getTotalNumberOfPostings(siteCriteria, filterCriteria) { await connectMongoDB(); const Posting = mongoose.models.posting || mongoose.model('posting', posting); - const totalNumberOfPostings = await Posting.countDocuments(siteCriteria); + const filterObject = await createFilterObject(filterCriteria); + const totalNumberOfPostings = await Posting.countDocuments({ + ...siteCriteria, + ...filterObject, + }); return totalNumberOfPostings; } @@ -99,18 +103,7 @@ export async function fetchJobPostings( await connectMongoDB(); const Posting = mongoose.models.posting || mongoose.model('posting', posting); - - const filterObject = filterCriteria.reduce((acc, filter) => { - // Check if the key already exists in the accumulator - if (Object.prototype.hasOwnProperty.call(acc, Object.keys(filter)[0])) { - // If the key exists, push the value to an array - acc[Object.keys(filter)[0]].push(Object.values(filter)[0]); - } else { - // If the key doesn't exist, initialize it as an array with the value - acc[Object.keys(filter)[0]] = [Object.values(filter)[0]]; - } - return acc; - }, {}); + const filterObject = await createFilterObject(filterCriteria); // Query job postings with pagination const jobPostings = await Posting.find({ ...siteCriteria, ...filterObject }) @@ -146,3 +139,18 @@ export async function parseFilterCriteria(etFilters, pFilters) { return [...etFilterCriteria, ...pFilterCriteria]; } + +// Function to create a filter object +export async function createFilterObject(filterCriteria) { + return filterCriteria.reduce((acc, filter) => { + // Check if the key already exists in the accumulator + if (Object.prototype.hasOwnProperty.call(acc, Object.keys(filter)[0])) { + // If the key exists, push the value to an array + acc[Object.keys(filter)[0]].push(Object.values(filter)[0]); + } else { + // If the key doesn't exist, initialize it as an array with the value + acc[Object.keys(filter)[0]] = [Object.values(filter)[0]]; + } + return acc; + }, {}); +} From f5dc2f957c28e5e189829237c634573ced88449a Mon Sep 17 00:00:00 2001 From: amamc Date: Tue, 21 May 2024 12:28:00 -0700 Subject: [PATCH 2/2] Updated total number endpoints to handle filtered postings --- .../asylum-refugees/total-posts/route.js | 18 +++++++++++++++--- .../job-posting/disabled/total-posts/route.js | 18 +++++++++++++++--- .../indigenous/total-posts/route.js | 18 +++++++++++++++--- .../job-posting/newcomers/total-posts/route.js | 18 +++++++++++++++--- .../job-posting/students/total-posts/route.js | 18 +++++++++++++++--- 5 files changed, 75 insertions(+), 15 deletions(-) diff --git a/src/app/api/job-posting/asylum-refugees/total-posts/route.js b/src/app/api/job-posting/asylum-refugees/total-posts/route.js index b943943..c84e99e 100644 --- a/src/app/api/job-posting/asylum-refugees/total-posts/route.js +++ b/src/app/api/job-posting/asylum-refugees/total-posts/route.js @@ -1,11 +1,23 @@ import { NextResponse } from 'next/server'; -import { getTotalNumberOfPostings, handleError } from '../../siteRequestUtils'; +import { + getTotalNumberOfPostings, + parseFilterCriteria, + handleError, +} from '../../siteRequestUtils'; -export async function GET() { +export async function GET(req) { try { const siteCriteria = { site5: true }; + const etFilters = req.nextUrl.searchParams.getAll('et'); + const pFilters = req.nextUrl.searchParams.getAll('p'); - let jobPostings = await getTotalNumberOfPostings(siteCriteria); + // Parse filter criteria + const filterCriteria = await parseFilterCriteria(etFilters, pFilters); + + let jobPostings = await getTotalNumberOfPostings( + siteCriteria, + filterCriteria + ); // Check if job postings were found if (jobPostings < 1) { diff --git a/src/app/api/job-posting/disabled/total-posts/route.js b/src/app/api/job-posting/disabled/total-posts/route.js index 1a2e0a9..86e5e24 100644 --- a/src/app/api/job-posting/disabled/total-posts/route.js +++ b/src/app/api/job-posting/disabled/total-posts/route.js @@ -1,11 +1,23 @@ import { NextResponse } from 'next/server'; -import { getTotalNumberOfPostings, handleError } from '../../siteRequestUtils'; +import { + getTotalNumberOfPostings, + parseFilterCriteria, + handleError, +} from '../../siteRequestUtils'; -export async function GET() { +export async function GET(req) { try { const siteCriteria = { site3: true }; + const etFilters = req.nextUrl.searchParams.getAll('et'); + const pFilters = req.nextUrl.searchParams.getAll('p'); - let jobPostings = await getTotalNumberOfPostings(siteCriteria); + // Parse filter criteria + const filterCriteria = await parseFilterCriteria(etFilters, pFilters); + + let jobPostings = await getTotalNumberOfPostings( + siteCriteria, + filterCriteria + ); // Check if job postings were found if (jobPostings < 1) { diff --git a/src/app/api/job-posting/indigenous/total-posts/route.js b/src/app/api/job-posting/indigenous/total-posts/route.js index a91c3a9..3945ca0 100644 --- a/src/app/api/job-posting/indigenous/total-posts/route.js +++ b/src/app/api/job-posting/indigenous/total-posts/route.js @@ -1,11 +1,23 @@ import { NextResponse } from 'next/server'; -import { getTotalNumberOfPostings, handleError } from '../../siteRequestUtils'; +import { + getTotalNumberOfPostings, + parseFilterCriteria, + handleError, +} from '../../siteRequestUtils'; -export async function GET() { +export async function GET(req) { try { const siteCriteria = { site1: true }; + const etFilters = req.nextUrl.searchParams.getAll('et'); + const pFilters = req.nextUrl.searchParams.getAll('p'); - let jobPostings = await getTotalNumberOfPostings(siteCriteria); + // Parse filter criteria + const filterCriteria = await parseFilterCriteria(etFilters, pFilters); + + let jobPostings = await getTotalNumberOfPostings( + siteCriteria, + filterCriteria + ); // Check if job postings were found if (jobPostings < 1) { diff --git a/src/app/api/job-posting/newcomers/total-posts/route.js b/src/app/api/job-posting/newcomers/total-posts/route.js index f6dd966..32744f7 100644 --- a/src/app/api/job-posting/newcomers/total-posts/route.js +++ b/src/app/api/job-posting/newcomers/total-posts/route.js @@ -1,11 +1,23 @@ import { NextResponse } from 'next/server'; -import { getTotalNumberOfPostings, handleError } from '../../siteRequestUtils'; +import { + getTotalNumberOfPostings, + parseFilterCriteria, + handleError, +} from '../../siteRequestUtils'; -export async function GET() { +export async function GET(req) { try { const siteCriteria = { site2: true }; + const etFilters = req.nextUrl.searchParams.getAll('et'); + const pFilters = req.nextUrl.searchParams.getAll('p'); - let jobPostings = await getTotalNumberOfPostings(siteCriteria); + // Parse filter criteria + const filterCriteria = await parseFilterCriteria(etFilters, pFilters); + + let jobPostings = await getTotalNumberOfPostings( + siteCriteria, + filterCriteria + ); // Check if job postings were found if (jobPostings < 1) { diff --git a/src/app/api/job-posting/students/total-posts/route.js b/src/app/api/job-posting/students/total-posts/route.js index ec0634d..c0a4d71 100644 --- a/src/app/api/job-posting/students/total-posts/route.js +++ b/src/app/api/job-posting/students/total-posts/route.js @@ -1,11 +1,23 @@ import { NextResponse } from 'next/server'; -import { getTotalNumberOfPostings, handleError } from '../../siteRequestUtils'; +import { + getTotalNumberOfPostings, + parseFilterCriteria, + handleError, +} from '../../siteRequestUtils'; -export async function GET() { +export async function GET(req) { try { const siteCriteria = { site4: true }; + const etFilters = req.nextUrl.searchParams.getAll('et'); + const pFilters = req.nextUrl.searchParams.getAll('p'); - let jobPostings = await getTotalNumberOfPostings(siteCriteria); + // Parse filter criteria + const filterCriteria = await parseFilterCriteria(etFilters, pFilters); + + let jobPostings = await getTotalNumberOfPostings( + siteCriteria, + filterCriteria + ); // Check if job postings were found if (jobPostings < 1) {