Skip to content

AugustoC01/sistema-facturacion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

25 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿงพ Sistema de Facturaciรณn โ€” Backend API

RESTful API backend for a billing and invoicing management system. Built with Node.js and Express, powered by Firebase Firestore as the database. It provides full CRUD operations for products, clients, suppliers, categories, sales, employees, and daily revenue reports.

๐Ÿ”— Live Demo: https://sistema-facturacion-front-roan.vercel.app/


Table of Contents


Tech Stack

Technology Purpose
Node.js Runtime environment
Express Web framework
Firebase Admin SDK Firestore database & authentication
Nodemailer Email service (password recovery)
Bcrypt Password hashing
Helmet HTTP security headers
CORS Cross-origin resource sharing
express-rate-limit Rate limiting (demo mode)
Docker Containerization
GitHub Actions CI/CD pipeline

Features

  • ๐Ÿ” Session-based authentication with secure HTTP-only cookies
  • ๐Ÿ“ฆ Full CRUD for products, clients, suppliers, categories, and sales
  • ๐Ÿ‘ฅ Employee management with role-based access
  • ๐Ÿ“Š Daily revenue reports with date range filtering and payment method breakdown
  • ๐Ÿ“ง Password recovery via email
  • ๐Ÿ›ก๏ธ Security hardened with Helmet, CORS whitelist, and rate limiting
  • ๐ŸŽฎ Demo mode with configurable request limits
  • ๐Ÿณ Docker-ready with non-root user and automated deployments

Getting Started

Prerequisites

  • Node.js v18 or higher
  • npm v9 or higher
  • A Firebase project with Firestore enabled
  • A Firebase Service Account Key (JSON file)

Installation

# Clone the repository
git clone https://github.com/AugustoC01/sistema-facturacion-back.git
cd sistema-facturacion-back

# Install dependencies
npm install

Environment Variables

Create a .env.local file in the root directory based on the .example.env template:

# API Configuration
PORT = 8080
CORS_ORIGINS = https://yourdomain.com
NODE_ENV = development
APP_MODE = production
MAX_LIMIT = 30

# Firebase
GOOGLE_APPLICATION_CREDENTIALS = ./serviceAccountKey.json

# Nodemailer (Gmail)
SENDER = your-email@gmail.com
PASS = your-app-password
Variable Description
PORT Server port (default: 8080)
CORS_ORIGINS Comma-separated list of allowed origins
NODE_ENV development or production โ€” dev mode auto-allows localhost:5173
APP_MODE Set to demo to enable rate limiting
MAX_LIMIT Max daily requests allowed in demo mode (default: 30)
GOOGLE_APPLICATION_CREDENTIALS Path to your Firebase service account JSON file
SENDER Email address used for sending password recovery emails
PASS App password for the sender email account

Running Locally

npm run dev

The server will start on http://localhost:<PORT> with hot-reload via Nodemon.

Running with Docker

# Build the image
docker build -t sistema-facturacion-back .

# Run the container
docker run -p 8080:8080 \
  --env-file .env \
  -v /path/to/serviceAccountKey.json:/app/secrets/serviceAccountKey.json \
  sistema-facturacion-back

Project Structure

src/
โ”œโ”€โ”€ index.js              # App entry point & middleware setup
โ”œโ”€โ”€ config.js             # Environment config & CORS options
โ”œโ”€โ”€ controllers/          # Request handlers
โ”‚   โ”œโ”€โ”€ authController.js
โ”‚   โ”œโ”€โ”€ categoryController.js
โ”‚   โ”œโ”€โ”€ clientController.js
โ”‚   โ”œโ”€โ”€ employeeController.js
โ”‚   โ”œโ”€โ”€ productController.js
โ”‚   โ”œโ”€โ”€ reportsController.js
โ”‚   โ”œโ”€โ”€ saleController.js
โ”‚   โ””โ”€โ”€ supplierController.js
โ”œโ”€โ”€ routes/               # Route definitions
โ”‚   โ”œโ”€โ”€ router.js         # Main router (mounts all sub-routes)
โ”‚   โ”œโ”€โ”€ authRoute.js
โ”‚   โ”œโ”€โ”€ categoryRoute.js
โ”‚   โ”œโ”€โ”€ clientRoute.js
โ”‚   โ”œโ”€โ”€ employeeRoute.js
โ”‚   โ”œโ”€โ”€ productRoute.js
โ”‚   โ”œโ”€โ”€ reportRoute.js
โ”‚   โ”œโ”€โ”€ saleRoute.js
โ”‚   โ””โ”€โ”€ supplierRoute.js
โ”œโ”€โ”€ middleware/
โ”‚   โ”œโ”€โ”€ userAuth.js       # Session authentication middleware
โ”‚   โ””โ”€โ”€ demoRateLimiter.js
โ”œโ”€โ”€ service/
โ”‚   โ”œโ”€โ”€ firebase.js       # Firebase Admin SDK initialization
โ”‚   โ”œโ”€โ”€ db.js             # Generic Firestore CRUD operations
โ”‚   โ””โ”€โ”€ employeeService.js
โ””โ”€โ”€ utils/
    โ”œโ”€โ”€ bcrypt.js          # Password hashing helpers
    โ”œโ”€โ”€ idGenerator.js     # Unique ID generation (nanoid)
    โ”œโ”€โ”€ nodemailer.js      # Email sending
    โ””โ”€โ”€ objectUtils.js

API Documentation

โš ๏ธ All endpoints except /user/* require an active session cookie (sessionId). Unauthenticated requests will receive a 401 Unauthorized response.

Base URL: http://localhost:8080


Authentication

Method Endpoint Description
POST /user/signup Register a new employee
POST /user/login Log in and receive a session cookie
POST /user/logout Log out and clear the session
POST /user/forgotPassword Send a password recovery email
Request / Response Details

POST /user/signup

{
  "name": "John Doe",
  "email": "john@example.com",
  "password": "securepassword"
}

POST /user/login

{
  "email": "john@example.com",
  "password": "securepassword"
}

โœ… On success, a sessionId cookie is set automatically.

POST /user/logout

No body required. Clears the session cookie.

POST /user/forgotPassword

{
  "email": "john@example.com"
}

๐Ÿ“ง A new auto-generated password will be sent to the provided email.


Employees

๐Ÿ”’ Requires authentication

Method Endpoint Description
GET /employees Get all employees
GET /employees/:id Get employee by ID
PUT /employees/:id Update employee
DELETE /employees/:id Delete employee

Categories

๐Ÿ”’ Requires authentication

Method Endpoint Description
GET /categories Get all categories
GET /categories/:id Get category by ID
POST /categories Create a new category
PUT /categories/:id Update a category
DELETE /categories/:id Delete a category

Products

๐Ÿ”’ Requires authentication

Method Endpoint Description
GET /products Get all products
GET /products/:id Get product by ID
POST /products Create a new product
PUT /products/:id Update a product
DELETE /products/:id Delete a product

Filtering & Queries:

Method Endpoint Description
GET /products/category/:categoryId Get products by category
GET /products/supplier/:supplierId Get products by supplier
GET /products/stock/enabled Get products in stock
GET /products/stock/disabled Get out-of-stock products
GET /products/stock/:quantity Get products with stock below a quantity
GET /products/higherPrice/:price Get products above a price
GET /products/lowerPrice/:price Get products below a price

Field Operations:

Method Endpoint Description
DELETE /products/remove/:id/field Delete a specific field from a product

Suppliers

๐Ÿ”’ Requires authentication

Method Endpoint Description
GET /suppliers Get all suppliers
GET /suppliers/:id Get supplier by ID
POST /suppliers Create a new supplier
PUT /suppliers/:id Update a supplier
DELETE /suppliers/:id Delete a supplier

Clients

๐Ÿ”’ Requires authentication

Method Endpoint Description
GET /clients Get all clients
GET /clients/:id Get client by ID
POST /clients Create a new client
PUT /clients/:id Update a client
DELETE /clients/:id Delete a client

Sales

๐Ÿ”’ Requires authentication

Method Endpoint Description
GET /sales Get all sales
GET /sales/:id Get sale by ID
POST /sales Create a new sale
PUT /sales/:id Update a sale
DELETE /sales/:id Delete a sale

๐Ÿ“Š Creating or deleting a sale automatically updates the daily revenue report.


Reports

๐Ÿ”’ Requires authentication

Method Endpoint Description
GET /reports Get all daily reports
GET /reports/days/:begin/:end Get total revenue between two dates

Date format for the range query: DD-MM-YYYY (e.g., /reports/days/01-01-2025/31-01-2025)


Deployment

This project includes a GitHub Actions workflow that automatically deploys to a VPS on every push to main. The pipeline connects via SSH and runs a deployment script on the server.

Required GitHub Secrets:

  • VPS_HOST โ€” Server IP/hostname
  • VPS_USER โ€” SSH username
  • SSH_PRIVATE_KEY โ€” Private SSH key

Roadmap

  • Expand employees management features
  • Expand providers/suppliers management
  • Add more analytics and reporting capabilities

License

This project is licensed under the ISC license.


Made with โค๏ธ by Augusto

About

Production-ready Node.js backend containerized with Docker and deployed on a VPS, featuring secure configuration, environment management, and scalable architecture.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages