Welcome to the Inkspiff Demo API Repository! This project is a robust and efficient TypeScript-based backend service meticulously designed to handle contact-related operations. Leveraging the power of Prisma as an ORM, it ensures seamless database interactions. Adhering to the principles of MVC architecture, the codebase maintains a clean separation of concerns, promoting maintainability and scalability.
A well-structured and type-safe API for managing contacts, built with modern technologies.
- Features
- Technologies Used
- Getting Started
- API Endpoints
- Code Structure
- Contributing
- License
- Support
- Contact Identification: Identify contacts by email or phone number with intelligent linking.
- Contact Retrieval: Retrieve all contacts based on flexible search criteria.
- Secure Contact Clearing: Clear all contacts securely with password protection.
- Robust Validation: Comprehensive input validation to ensure data integrity.
- Error Handling: Centralized error handling for consistent API responses.
- Database Management: Utilizes Prisma for efficient database schema management and migrations.
- TypeScript: For enhanced type safety and modern JavaScript features.
- Node.js: As the runtime environment, providing scalability and performance.
- Express: For building a RESTful API with middleware support.
- Prisma: As the ORM, simplifying database interactions and schema management.
- CORS: Cross-Origin Resource Sharing for secure API access from different domains.
- dotenv: Zero-dependency module that loads environment variables from a
.envfile.
Follow these steps to set up and run the project locally:
-
Clone the repository:
git clone https://github.com/Inkspiff/inkspiff-demo-api-repo.git cd inkspiff-demo-api-repo -
Install dependencies:
npm install
-
Set up your environment variables:
Create a
.envfile in the root directory and configure the following variables:DATABASE_URL=your_database_url CLEAR_PASSWORD=your_secure_passwordNote: Ensure your
DATABASE_URLpoints to a valid PostgreSQL database. -
Run database migrations:
npx prisma migrate dev --name init
-
Start the application:
npm start
The server will start on
http://localhost:3000.
- POST
/identify- Request Body:
{ "email": "string", "phoneNumber": "string" } - Description: Identifies a contact based on email or phone number, linking them if necessary.
- Response:
{ "primaryContactId": 123, "emails": ["primary@example.com", "secondary@example.com"], "phoneNumbers": ["123-456-7890", "098-765-4321"], "secondaryContactIds": [456, 789] }
- Request Body:
- GET
/identify- Query Parameters:
email,phoneNumber - Description: Retrieves all contacts matching the provided email or phone number.
- Response:
[ { "id": 1, "phoneNumber": "123-456-7890", "email": "test@example.com", "linkedId": null, "linkPrecedence": "primary", "createdAt": "2024-01-01T00:00:00.000Z", "updatedAt": "2024-01-01T00:00:00.000Z", "deletedAt": null } ]
- Query Parameters:
- DELETE
/identify- Query Parameters:
email,phoneNumber,password - Description: Clears all contacts matching the criteria if the correct password is provided.
- Response:
{ "message": "All contacts have been cleared" }
- Query Parameters:
The project is organized into the following directories:
controllers/: Contains the logic for handling incoming requests and generating responses.services/: Contains the business logic and interacts with the database through Prisma.routes/: Defines the API endpoints and their corresponding controller functions.config/: Configuration files, including the Prisma client setup.prisma/: Contains the Prisma schema for database modeling and migrations.utils/: Utility functions such as custom error classes and validation helpers.
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export default prisma;export const identify = async (req: any, res: any) => {
try {
const { email, phoneNumber } = req.body;
const result = await identifyContact(email || null, phoneNumber || null);
res.json({ contact: result });
} catch (error: any) {
return res.status(400).json({ error: error.message });
}
};model Contact {
id Int @id @default(autoincrement())
phoneNumber String?
email String?
linkedId Int?
linkPrecedence LinkPrecedence @default(primary)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
deletedAt DateTime?
}
enum LinkPrecedence {
primary
secondary
}Contributions are highly welcome! If you have suggestions for improvements or bug fixes, please:
- Fork the repository.
- Create a new branch for your feature or fix.
- Submit a pull request with a clear description of your changes.
This project is licensed under the MIT License. See the LICENSE file for details.
For any questions, issues, or support requests, please open an issue on the GitHub repository.
Feel free to explore the code and contribute to the project! We appreciate your interest and support.