Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Vero-Ventures/job-bank into…
Browse files Browse the repository at this point in the history
… feature/admin-panel-UI
  • Loading branch information
mai-vu committed May 17, 2024
2 parents 0066c35 + a1e015b commit ab6e6f9
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/app/api/job-posting/email-sent/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { NextResponse } from 'next/server';
import { connectMongoDB } from '@/libs/mongodb';
import posting from '@/app/api/posting';
import mongoose from 'mongoose';
import {
getEmailAddressesWithSentField,
handleError,
} from '@/app/api/job-posting/siteRequestUtils';

export async function GET(req) {
try {
let emailAddresses = await getEmailAddressesWithSentField(
req.nextUrl.searchParams
);

return NextResponse.json({ emailAddresses }, { status: 200 });
} catch (error) {
// Handle errors
return handleError(error);
}
}

export async function PATCH(req) {
try {
const { email, sent } = await req.json();

if (!email) {
return NextResponse.json(
{ message: 'Bad Request - Email parameter is required' },
{ status: 400 }
);
}

await connectMongoDB();

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

// Update documents that match the email criteria
const updatedJobPostings = await Posting.updateMany(
{ email },
{ $set: { sent: sent !== undefined ? sent : true } } // Update "sent" to specified value or default to true
);

// Check if any documents were updated
if (updatedJobPostings.nModified === 0) {
return NextResponse.json(
{ message: 'No job postings were updated' },
{ status: 404 }
);
}

return NextResponse.json(
{ message: 'Job postings updated successfully' },
{ status: 200 }
);
} catch (error) {
console.log('Error updating job postings:', error);

// Check error status and return appropriate response
if (error instanceof SyntaxError || error instanceof TypeError) {
// Malformed or invalid request
return NextResponse.json(
{ message: 'Bad Request - The request is malformed or invalid' },
{ status: 400 }
);
} else if (error.name === 'UnauthorizedError') {
// Unauthorized
return NextResponse.json(
{
message:
'Unauthorized - The client is not authorized to perform the operation',
},
{ status: 401 }
);
} else if (error.name === 'NotFoundError') {
// Not Found
return NextResponse.json(
{
message:
'Not Found - No job postings were found for the provided email',
},
{ status: 404 }
);
} else {
// Other server error
return NextResponse.json(
{ message: 'Internal Server Error' },
{ status: 500 }
);
}
}
}
52 changes: 52 additions & 0 deletions src/app/api/job-posting/siteRequestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,58 @@ import { connectMongoDB } from '@/libs/mongodb';
import posting from '@/app/api/posting';
import mongoose from 'mongoose';

// Function to get the total number of job postings
export async function getTotalNumberOfPostings(siteCriteria) {
await connectMongoDB();

const Posting = mongoose.models.posting || mongoose.model('posting', posting);
const totalNumberOfPostings = await Posting.countDocuments(siteCriteria);

return totalNumberOfPostings;
}

// Function to get email addresses with the 'sent' field
export async function getEmailAddressesWithSentField(params) {
const sortBy = params.get('sort');
const sortCriteria = sortBy ? JSON.parse(sortBy) : null;

await connectMongoDB();

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

// Define the aggregation pipeline to perform operations on the documents
let aggregationPipeline = [
{
// Group documents by the 'email' field, and for each group,
// create a new field 'sent' containing the value of the 'sent' field
$group: {
_id: '$email',
sent: { $first: '$sent' }, // Include the 'sent' field from the first document in each group
},
},
{
// Project stage: Reshape the documents to include only the 'email' and 'sent' fields,
// while replacing the '_id' field name to 'email'
$project: {
email: '$_id', // Rename '_id' to 'email'
sent: 1,
_id: 0,
},
},
];

if (sortCriteria) {
aggregationPipeline.push({
$sort: { sent: sortCriteria },
});
}

// Execute the aggregation pipeline
let emailAddresses = await Posting.aggregate(aggregationPipeline);

return emailAddresses;
}

// Function to extract pagination parameters
export function getPaginationParams(req) {
const pageSize = 25;
Expand Down

0 comments on commit ab6e6f9

Please sign in to comment.