Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Total number endpoint to return the total number of filtered fields #66

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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;
}
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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;
}, {});
}
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
Loading