Emergency Management API Documentation
Introduction
This document provides detailed information about the Emergency Management API. The
API enables CRUD operations on emergency entities including user and authentication
handling. Below is a breakdown of the available endpoints and their functionalities.
Base URL
/admin/emergency
Endpoints
1. Create Emergency
Method: POST
URL: /emergencySignup
Description:
Allows an admin to create a new emergency record.
Request Body:
"type": "hospital",
"name": "City Hospital",
"phone": "1234567890",
"address": "123 Main St",
"number": "102",
"latitude": 29.9792,
"longitude": 31.1342 }
Validation Rules:
• type: Required, must be one of the predefined valid types.
• name: Required, must not be empty.
• phone: Required, numeric, must not be empty.
• address: Optional.
• number: Required, numeric.
• latitude: Optional, numeric.
• longitude: Optional, numeric.
Response:
• Success (201):
"status": "success",
"msg": "Emergency created successfully.",
"data": {
"id": "<generated-id>",
"type": "hospital",
"name": "City Hospital",
"phone": "1234567890",
"address": "123 Main St",
"number": "102",
"latitude": 29.9792,
"longitude": 31.1342
• Validation Error (400):
"status": "failed",
"errors": [{ "msg": "Phone must be numeric." }]
2. Get All Emergencies
Method: GET
URL: /emergency
Description:
Fetches all emergency records.
Response:
• Success (200):
{
"status": "success",
"data": [
{
"id": "1",
"type": "hospital",
"name": "City Hospital",
"phone": "1234567890",
"address": "123 Main St",
"number": "102",
"latitude": 29.9792,
"longitude": 31.1342
} ] }
• No Data Found (404):
{
"status": "failed",
"errors": [{ "msg": "No data found." }]
}
3. Get Emergency by ID
Fetches details of a specific emergency by its unique ID.
• Method: GET
• Endpoint: /api/emergency/:id
• Parameters:
1. - id: Unique identifier for the emergency.
Sample Response:
{
"status": "success",
"data": [ { "id": "123", "name": "Fire Brigade", "type": "fire", ... } ]
}
• Not Found (404):
"status": "failed",
"errors": [{ "msg": "Emergency not found." }]
4. Update Emergency
Method: PUT
URL: /emergency/:id
Description:
Updates an existing emergency record. Validates that all provided fields are unique and that
the body contains new data to update.
Request Body:
"type": "hospital",
"name": "City Hospital",
"phone": "1234567890"
Validation Rules:
• Checks for uniqueness of:
o type
o name
o phone
o address
o latitude
o longitude
o number
• Rejects empty updates.
Response:
• Success (200):
{
"status": "success",
"msg": "Emergency updated successfully.",
"data": {
"type": "hospital",
"name": "City Hospital",
"phone": "1234567890",
"updatedAt": "<timestamp>"
}
}
• No Data to Update (200):
{
"status": "success",
"msg": "No new data to update."
}
• Validation Error (400):
"status": "failed",
"errors": [
{ "msg": "Update failed due to uniqueness constraints." }
5. Delete Emergency
Method: DELETE
URL: /emergency/:id
Description:
Deletes an emergency record by its ID.
Response:
• Success (200):
"status": "success",
"msg": "Emergency with ID 1 deleted successfully."
• Not Found (404):
"status": "failed",
"errors": [{ "msg": "Emergency not found." }] }
• Error During Deletion (500):
"status": "failed",
"errors": [{ "msg": "An error occurred while deleting the emergency." }]
Error Handling
• Validation Errors: Each request is validated against specific rules, and validation
errors are returned in a structured format with error messages.
• 404 Errors: Returned when requested data is not found.
• 500 Errors: Returned for unexpected server errors.
Notes
1. All endpoints are protected and require authentication using the admin token.
2. Use the adminProtect middleware for routes to ensure authorization.
3. Fields like email, phone, and number must be unique across all records.