Skip to content

Commit

Permalink
Implement Total number endpoint to return the total number of filtere…
Browse files Browse the repository at this point in the history
…d fields (#66)

* Updated the function that returns the total number of postings to handle filtered postings and also separate filter object creation to prevent the repetition

* Updated total number endpoints to handle filtered postings
  • Loading branch information
ama-cantabile authored May 22, 2024
1 parent a10deb1 commit c852578
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 29 deletions.
18 changes: 15 additions & 3 deletions src/app/api/job-posting/asylum-refugees/total-posts/route.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
18 changes: 15 additions & 3 deletions src/app/api/job-posting/disabled/total-posts/route.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
18 changes: 15 additions & 3 deletions src/app/api/job-posting/indigenous/total-posts/route.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
18 changes: 15 additions & 3 deletions src/app/api/job-posting/newcomers/total-posts/route.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
36 changes: 22 additions & 14 deletions src/app/api/job-posting/siteRequestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,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;
}
Expand Down Expand Up @@ -106,18 +110,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);

let query = Posting.find({ ...siteCriteria, ...filterObject });

Expand Down Expand Up @@ -165,3 +158,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;
}, {});
}
18 changes: 15 additions & 3 deletions src/app/api/job-posting/students/total-posts/route.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit c852578

Please sign in to comment.