This is an API developed to manage measurements for customers across different service types. The application uses Node.js, TypeScript, Prisma, Express.js, and other technologies to provide a robust and scalable backend.
- 📏 React Precision Application - Interface for registering gas and water measurements
- Node.js: Runtime environment for JavaScript on the backend.
- TypeScript: Superset of JavaScript that adds static typing to the code.
- Express.js: Minimalist web framework for Node.js.
- Prisma: ORM that simplifies database access.
- MySQL: Database used during development.
- ESLint: Linting tool to keep the code clean and standardized.
- Jest: Testing framework used to ensure code quality.
- Mocha: Used for additional testing.
- Google Generative AI: Integrated to analyze meter images and extract numerical values from measurements.
- Swagger: Integrated to generate documentation for the routes.
The project follows a modular structure to facilitate maintenance and scalability. The main folders and files are:
src/
: Contains the source code of the application.controllers/
: Control logic where requests are processed.db/
: Generates a Prisma instance for connecting to the database throughout the application.exceptions/
: Creates custom exceptions to handle errors during application execution.interfaces/
: Defines interfaces and types to handle function parameters and returns.middlewares/
: Middlewares for validations and treatments.models/
: Logic for connecting to the database.services/
: Service layer that interacts with Prisma and performs business operations.routes/
: Definition of the API routes.utils/
: Utility functions, such as image manipulation and interaction with the Google Generative AI API.tests/
: Automated tests to validate functionalities.
- List Measurements: Allows listing all measurements for a specific customer, filtering by measurement type.
- Image Management: Images of measurements are saved and retrieved through temporary URLs, using Base64.
- Parameter Validation: Middleware to validate input parameters, ensuring request integrity.
- Image Analysis with Google Generative AI: The API analyzes images of measurements and extracts the consumption value shown.
- Node.js
- Docker (optional for development environment)
-
Clone the repository:
git clone https://github.com/SamuelRocha91/apiMeasureWaterAndGas.git cd apiMeasureWaterAndGas
-
Install the dependencies:
npm install
-
Configure the environment variables:
- Create a
.env
file with the necessary configurations. - Example:
DATABASE_URL="file:./dev.db" GEMINI_API_KEY="your_google_api_key" HOST="http://localhost:3000"
- Create a
-
Run the database migrations:
npx prisma migrate dev
-
Start the server:
npm run dev
You can run the project using Docker. To do this, execute:
docker-compose up --build
start
: Starts the application.dev
: Starts the application in development mode.build
: Compiles TypeScript code to JavaScript.lint
: Runs ESLint to check code compliance.lint:fix
: Runs ESLint and automatically fixes issues.prisma:generate
: Generates Prisma types.prisma:migrate
: Runs database migrations.prisma:seed
: Populates the database with initial data.docker
: Installs dependencies, generates Prisma types, runs migrations, and starts the server using Nodemon.test
: Runs all tests using Mocha and Jest.
Utility functions to save and generate URLs for images:
saveBase64Image
: Saves a Base64 image to a file on the server.getImageUrl
: Generates a temporary URL to access the image.extractMimeType
: Extracts the MIME type from a Base64 image.extractSize
: Calculates the size of a Base64 image.
The function checkMeasureValue
uses Google Generative AI to analyze meter images and extract the consumption value.
async function checkMeasureValue(mime: string, base64: string): Promise<number> {
const result = await model.generateContent([
{
inlineData: {
mimeType: mime,
data: base64
}
},
{ text: PROMPT }
]);
return Number(result.response.text().match(/\d+/)[0]);
}
This function is used to ensure that the measurement value is accurately extracted from the provided image.