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
Reference Examples:
- See
packages/internal-models/src/db/actions/Applicant.ts for other applicant actions like getApplicants() and deleteApplicant()
- See
apps/form-builder/src/server/actions/forms.ts for the createForm() function as a similar example
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
Reference Examples:
- See
apps/applicant-tracking/src/app/api/applicants/[applicantId]/route.ts for the DELETE endpoint implementation
- See
apps/form-builder/src/app/api/forms/route.ts for the POST endpoint implementation
- See
apps/form-builder/src/app/api/forms/[formId]/formSubmissions/route.ts for a more complex POST implementation
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
Additional Resources
Schema and Model References
- See
packages/internal-models/src/types/applicant.ts for the Zod schema definitions
- See
packages/internal-models/src/db/models/Applicant.ts for the Mongoose model definition
API Service References
- See
apps/applicant-tracking/src/services/api/applicant.ts for how the API is consumed by the frontend
- See
apps/applicant-tracking/src/utils/constants/urls.ts for API endpoint URL definitions
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:Reference Examples:
packages/internal-models/src/db/actions/Applicant.tsfor other applicant actions likegetApplicants()anddeleteApplicant()apps/form-builder/src/server/actions/forms.tsfor thecreateForm()function as a similar example2. Create the API Route
Create a new file at
apps/applicant-tracking/src/app/api/applicants/route.tsthat will:Reference Examples:
apps/applicant-tracking/src/app/api/applicants/[applicantId]/route.tsfor the DELETE endpoint implementationapps/form-builder/src/app/api/forms/route.tsfor the POST endpoint implementationapps/form-builder/src/app/api/forms/[formId]/formSubmissions/route.tsfor a more complex POST implementationTeachables
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:
Additional Resources
Schema and Model References
packages/internal-models/src/types/applicant.tsfor the Zod schema definitionspackages/internal-models/src/db/models/Applicant.tsfor the Mongoose model definitionAPI Service References
apps/applicant-tracking/src/services/api/applicant.tsfor how the API is consumed by the frontendapps/applicant-tracking/src/utils/constants/urls.tsfor API endpoint URL definitions