Skip to content

Commit

Permalink
Added Filtering APIs for Admin Panel and Job Posting Front-Ends in Ba…
Browse files Browse the repository at this point in the history
…ck-End (#44)

* Update featchJobPostings function to handle filtering and added parseSortCriteria and parsefilterCriteria functions to handle the parsing

* Added filtering feature for Admin panel side

* Added filter feature for job posting front-ends

* Updated filtering functions based on the changes made in URL patterns

* Refactored the code to use fetching functions from siteRequestUtils instead of having own fetching function and changed the 404 status code to 204

* Changed 404 status code to 204

* Changed 404 status code to 204

* Updated filtering handling in job posting front-ends and changed the 404 status code to 204
  • Loading branch information
ama-cantabile authored May 17, 2024
1 parent 039bdaf commit 4377224
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 73 deletions.
22 changes: 16 additions & 6 deletions src/app/api/job-posting/asylum-refugees/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
fetchJobPostings,
handleError,
checkFieldExist,
parseSortCriteria,
parseFilterCriteria,
} from '../siteRequestUtils';

export async function GET(req) {
Expand All @@ -14,7 +16,13 @@ export async function GET(req) {
// Extract pagination parameters
const { skip, pageSize } = getPaginationParams(req);
const sortBy = req.nextUrl.searchParams.get('sort');
const sortCriteria = sortBy ? JSON.parse(sortBy) : null;
const etFilters = req.nextUrl.searchParams.getAll('et');
const pFilters = req.nextUrl.searchParams.getAll('p');

// Parse sort and filter criteria
const sortCriteria = await parseSortCriteria(sortBy);
// Parse filter criteria
const filterCriteria = await parseFilterCriteria(etFilters, pFilters);

// Check if the requested sort field exists
if (sortCriteria != null && !(await checkFieldExist(sortCriteria))) {
Expand All @@ -29,15 +37,17 @@ export async function GET(req) {
const jobPostings = await fetchJobPostings(
siteCriteria,
sortCriteria,
filterCriteria,
skip,
pageSize
);

if (jobPostings.length === 0) {
return NextResponse.json(
{ message: 'Not Found - No job postings found on this page' },
{ status: 404 }
);
// Check if job postings were found
if (jobPostings.length < 1) {
return new Response(null, {
status: 204,
statusText: 'No job postings were found',
});
}

// Return success response with the paginated job postings
Expand Down
9 changes: 5 additions & 4 deletions src/app/api/job-posting/asylum-refugees/total-posts/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ export async function GET() {

let jobPostings = await getTotalNumberOfPostings(siteCriteria);

// Check if job postings were found
if (jobPostings < 1) {
return NextResponse.json(
{ message: 'Not Found - No job postings found on this page' },
{ status: 404 }
);
return new Response(null, {
status: 204,
statusText: 'No job postings were found',
});
}

return NextResponse.json({ jobPostings }, { status: 200 });
Expand Down
13 changes: 5 additions & 8 deletions src/app/api/job-posting/by-id/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ export async function GET(req) {
const jobPostings = await Posting.find({ _id: jobPostingId });

// Check if job postings were found
if (jobPostings.length === 0) {
return NextResponse.json(
{
message:
'Not Found - No job postings were found associated with the provided ID',
},
{ status: 404 }
);
if (jobPostings.length < 1) {
return new Response(null, {
status: 204,
statusText: 'No job postings were found',
});
}

return NextResponse.json({ jobPostings }, { status: 200 });
Expand Down
22 changes: 16 additions & 6 deletions src/app/api/job-posting/disabled/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
fetchJobPostings,
handleError,
checkFieldExist,
parseSortCriteria,
parseFilterCriteria,
} from '../siteRequestUtils';

export async function GET(req) {
Expand All @@ -14,7 +16,13 @@ export async function GET(req) {
// Extract pagination parameters
const { skip, pageSize } = getPaginationParams(req);
const sortBy = req.nextUrl.searchParams.get('sort');
const sortCriteria = sortBy ? JSON.parse(sortBy) : null;
const etFilters = req.nextUrl.searchParams.getAll('et');
const pFilters = req.nextUrl.searchParams.getAll('p');

// Parse sort and filter criteria
const sortCriteria = await parseSortCriteria(sortBy);
// Parse filter criteria
const filterCriteria = await parseFilterCriteria(etFilters, pFilters);

// Check if the requested sort field exists
if (sortCriteria != null && !(await checkFieldExist(sortCriteria))) {
Expand All @@ -29,15 +37,17 @@ export async function GET(req) {
const jobPostings = await fetchJobPostings(
siteCriteria,
sortCriteria,
filterCriteria,
skip,
pageSize
);

if (jobPostings.length === 0) {
return NextResponse.json(
{ message: 'Not Found - No job postings found on this page' },
{ status: 404 }
);
// Check if job postings were found
if (jobPostings.length < 1) {
return new Response(null, {
status: 204,
statusText: 'No job postings were found',
});
}

// Return success response with the paginated job postings
Expand Down
9 changes: 5 additions & 4 deletions src/app/api/job-posting/disabled/total-posts/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ export async function GET() {

let jobPostings = await getTotalNumberOfPostings(siteCriteria);

// Check if job postings were found
if (jobPostings < 1) {
return NextResponse.json(
{ message: 'Not Found - No job postings found on this page' },
{ status: 404 }
);
return new Response(null, {
status: 204,
statusText: 'No job postings were found',
});
}

return NextResponse.json({ jobPostings }, { status: 200 });
Expand Down
22 changes: 16 additions & 6 deletions src/app/api/job-posting/indigenous/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
fetchJobPostings,
handleError,
checkFieldExist,
parseSortCriteria,
parseFilterCriteria,
} from '../siteRequestUtils';

export async function GET(req) {
Expand All @@ -14,7 +16,13 @@ export async function GET(req) {
// Extract pagination parameters
const { skip, pageSize } = getPaginationParams(req);
const sortBy = req.nextUrl.searchParams.get('sort');
const sortCriteria = sortBy ? JSON.parse(sortBy) : null;
const etFilters = req.nextUrl.searchParams.getAll('et');
const pFilters = req.nextUrl.searchParams.getAll('p');

// Parse sort and filter criteria
const sortCriteria = await parseSortCriteria(sortBy);
// Parse filter criteria
const filterCriteria = await parseFilterCriteria(etFilters, pFilters);

// Check if the requested sort field exists
if (sortCriteria != null && !(await checkFieldExist(sortCriteria))) {
Expand All @@ -29,15 +37,17 @@ export async function GET(req) {
const jobPostings = await fetchJobPostings(
siteCriteria,
sortCriteria,
filterCriteria,
skip,
pageSize
);

if (jobPostings.length === 0) {
return NextResponse.json(
{ message: 'Not Found - No job postings found on this page' },
{ status: 404 }
);
// Check if job postings were found
if (jobPostings.length < 1) {
return new Response(null, {
status: 204,
statusText: 'No job postings were found',
});
}

// Return success response with the paginated job postings
Expand Down
9 changes: 5 additions & 4 deletions src/app/api/job-posting/indigenous/total-posts/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ export async function GET() {

let jobPostings = await getTotalNumberOfPostings(siteCriteria);

// Check if job postings were found
if (jobPostings < 1) {
return NextResponse.json(
{ message: 'Not Found - No job postings found on this page' },
{ status: 404 }
);
return new Response(null, {
status: 204,
statusText: 'No job postings were found',
});
}

return NextResponse.json({ jobPostings }, { status: 200 });
Expand Down
22 changes: 16 additions & 6 deletions src/app/api/job-posting/newcomers/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
fetchJobPostings,
handleError,
checkFieldExist,
parseSortCriteria,
parseFilterCriteria,
} from '../siteRequestUtils';

export async function GET(req) {
Expand All @@ -14,7 +16,13 @@ export async function GET(req) {
// Extract pagination parameters
const { skip, pageSize } = getPaginationParams(req);
const sortBy = req.nextUrl.searchParams.get('sort');
const sortCriteria = sortBy ? JSON.parse(sortBy) : null;
const etFilters = req.nextUrl.searchParams.getAll('et');
const pFilters = req.nextUrl.searchParams.getAll('p');

// Parse sort and filter criteria
const sortCriteria = await parseSortCriteria(sortBy);
// Parse filter criteria
const filterCriteria = await parseFilterCriteria(etFilters, pFilters);

// Check if the requested sort field exists
if (sortCriteria != null && !(await checkFieldExist(sortCriteria))) {
Expand All @@ -29,15 +37,17 @@ export async function GET(req) {
const jobPostings = await fetchJobPostings(
siteCriteria,
sortCriteria,
filterCriteria,
skip,
pageSize
);

if (jobPostings.length === 0) {
return NextResponse.json(
{ message: 'Not Found - No job postings found on this page' },
{ status: 404 }
);
// Check if job postings were found
if (jobPostings.length < 1) {
return new Response(null, {
status: 204,
statusText: 'No job postings were found',
});
}

// Return success response with the paginated job postings
Expand Down
9 changes: 5 additions & 4 deletions src/app/api/job-posting/newcomers/total-posts/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ export async function GET() {

let jobPostings = await getTotalNumberOfPostings(siteCriteria);

// Check if job postings were found
if (jobPostings < 1) {
return NextResponse.json(
{ message: 'Not Found - No job postings found on this page' },
{ status: 404 }
);
return new Response(null, {
status: 204,
statusText: 'No job postings were found',
});
}

return NextResponse.json({ jobPostings }, { status: 200 });
Expand Down
45 changes: 31 additions & 14 deletions src/app/api/job-posting/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ import { NextResponse } from 'next/server';
import { connectMongoDB } from '@/libs/mongodb';
import posting from '@/app/api/posting';
import mongoose from 'mongoose';
import { checkFieldExist } from '@/app/api/job-posting/siteRequestUtils';
import {
fetchJobPostings,
checkFieldExist,
parseSortCriteria,
parseFilterCriteria,
} from '@/app/api/job-posting/siteRequestUtils';

export async function GET(req) {
try {
const email = req.nextUrl.searchParams.get('email');
const sortBy = req.nextUrl.searchParams.get('sort');
const sortCriteria = sortBy ? JSON.parse(sortBy) : null;
const etFilters = req.nextUrl.searchParams.getAll('et');
const pFilters = req.nextUrl.searchParams.getAll('p');

// Parse sort and filter criteria
const sortCriteria = await parseSortCriteria(sortBy);
// Parse filter criteria
const filterCriteria = await parseFilterCriteria(etFilters, pFilters);

if (!email) {
return NextResponse.json(
Expand All @@ -28,20 +39,26 @@ export async function GET(req) {

await connectMongoDB();

const Posting =
mongoose.models.posting || mongoose.model('posting', posting);

const jobPostings = await Posting.find({ email }).sort(sortCriteria);
const siteCriteria = { email };
const skip = 0;
const pageSize = 0;

// Query job postings with pagination and sort criteria if provided
const jobPostings = await fetchJobPostings(
siteCriteria,
sortCriteria,
filterCriteria,
skip,
pageSize
);

// Check if job postings were found
if (jobPostings.length === 0) {
return NextResponse.json(
{
message:
'Not Found - No job postings were found associated with the provided email',
},
{ status: 404 }
);
if (jobPostings.length < 1) {
return new Response(null, {
status: 204,
statusText:
'No job postings were found associated with the provided email',
});
}

return NextResponse.json({ jobPostings }, { status: 200 });
Expand Down
Loading

0 comments on commit 4377224

Please sign in to comment.