Description
Create a POST Applicant API endpoint that creates a new applicant in the database with their basic information and application details.
Technical Details
1. Create the Action Function
Create a new function in packages/internal-models/src/db/actions/Applicant.ts that will:
- Take a CreateApplicantRequest as input and return an ApplicantResponse
- Connect to the database using dbConnect()
- Create a new applicant with the provided data
- Set default values for status and statusUpdatedAt
- Return the created applicant
2. Create the API Route
Create a new file at apps/applicant-tracking/src/app/api/applicants/route.ts that will:
- Import necessary dependencies from internal-models and next/server
- Create a POST function that handles the HTTP request
- Parse and validate the request body using Zod
- Call the createApplicant action function
- Return appropriate HTTP responses with status codes
Teachables
Action Function
The action function is responsible for:
- Database operations (connecting and creating records)
- Business logic (setting default values)
- Data transformation (converting between request and response types)
- Error handling for database operations
API Route
The API route is responsible for:
- HTTP request handling (parsing body, headers)
- Input validation using Zod schemas
- Calling the appropriate action function
- Returning HTTP responses with proper status codes
- Error handling for HTTP-level issues
Data Flow
- Client sends HTTP POST request with applicant data
- API route receives request and validates data
- If valid, calls action function
- Action function connects to database and creates record
- Action function returns created applicant
- API route returns HTTP response with applicant ID
Error Handling
- API level: Handles HTTP errors (400 for invalid input, 500 for server errors)
- Action level: Handles database errors
- Both levels: Provide meaningful error messages
Testing Steps
- Make a POST api call using postman with the url
localhost:3000/api/applicants
- Set the Content-Type header to
application/json
- Include a JSON body with the required fields:
{
"firstName": "John",
"lastName": "Doe",
"netid": "jdoe",
"term": "Fall 2024",
"application": "507f1f77bcf86cd799439011" // Form submission ID
}
-
Verify the response:
- Status code should be 201 for successful creation
- Response body should contain the new applicant's ID
-
Test error cases:
- Missing required fields (should return 400)
- Invalid term format (should return 400)
- Invalid application ID (should return 400)
- Database connection error (should return 500)
-
Verify the applicant was created in the database with:
- All provided information matches the request
- Status is set to 'Pending Review'
- statusUpdatedAt is set to current timestamp
Description
Create a POST Applicant API endpoint that creates a new applicant in the database with their basic information and application details.
Technical Details
1. Create the Action Function
Create a new function in
packages/internal-models/src/db/actions/Applicant.tsthat will:2. Create the API Route
Create a new file at
apps/applicant-tracking/src/app/api/applicants/route.tsthat will:Teachables
Action Function
The action function is responsible for:
API Route
The API route is responsible for:
Data Flow
Error Handling
Testing Steps
localhost:3000/api/applicantsapplication/json{ "firstName": "John", "lastName": "Doe", "netid": "jdoe", "term": "Fall 2024", "application": "507f1f77bcf86cd799439011" // Form submission ID }Verify the response:
Test error cases:
Verify the applicant was created in the database with: