Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/fredy-rizo/despacho-backend/llms.txt

Use this file to discover all available pages before exploring further.

Despacho Backend uses Mongoose ODM to define and interact with two core collections in MongoDB: LawyerUser, which represents authenticated lawyer accounts, and FormReserve, which holds client appointment reservation requests. Both schemas use Mongoose’s built-in timestamps option to automatically track document creation and modification times. Understanding these models is essential for working with the API’s request and response shapes.
Both schemas are created with { timestamps: true }. Mongoose automatically adds and manages the createdAt and updatedAt fields on every document — you do not need to set these manually when creating or updating records.

LawyerUser model

The LawyerUser model (src/models/LawyerUser.js) represents a law firm staff member who can log in and manage client reservations. A new LawyerUser is created via POST /api/lawyer/user/create and receives the default role of Admin on registration.

Fields

token
String
default:"\"\""
Stores the most recently issued JWT for this user. Updated on every successful login via LawyerUser.updateOne(). Defaults to an empty string before the first login.
email
String
required
The lawyer’s unique email address. Used as the login identifier. Stored as provided (the controller calls .toLowerCase() during lookup to ensure case-insensitive matching).
password
String
required
The bcrypt-hashed password. The raw password is never storedencryptPassword() hashes it at cost factor 10 before the document is saved.
role
Array<{ name: String, value: String }>
An array of role objects. Each object has a human-readable name and a numeric string value. Defaults to [{ name: "Admin", value: "1" }] on account creation. See the Roles section for the full role reference.
createdAt
Date
Automatically set by Mongoose when the document is first inserted.
updatedAt
Date
Automatically updated by Mongoose every time the document is modified.

Schema source

// src/models/LawyerUser.js
import mongoose from "mongoose";
import bcrypt from "bcrypt";
const { Schema } = mongoose;

const LawyerUserSchema = new Schema(
  {
    token: { type: String, default: "" },
    email: String,
    password: String,
    role: [{ name: String, value: String }],
  },
  { timestamps: true }
);

export const encryptPassword = async (password) => {
  const pass = password.toString();
  return await bcrypt.hash(pass, parseInt(10));
};

export const comparePassword = async (password, receivePassword) => {
  return await bcrypt.compare(password, receivePassword);
};

/*
  role
  1 → Admin
  2 → SuperAdmin
*/

export const LawyerUser = mongoose.model("LawyerUser", LawyerUserSchema);

Helper functions


FormReserve model

The FormReserve model (src/models/FormReserve.js) represents a single appointment request submitted by a prospective client. Reservations begin with reservation_accepted: false and are updated to true when a lawyer confirms the appointment.

Fields

full_name
String
required
The client’s full name as submitted in the reservation form.
email
String
required
The client’s email address. Used for contact and notification purposes.
phone
Number
required
The client’s phone number stored as a numeric value.
required_service
String (enum)
required
The legal service area the client is requesting. Must be one of the six allowed values — any other string will fail Mongoose validation. Defaults to "" if omitted (though the controller enforces this field as required).Allowed values:
  • "Derecho civil"
  • "Derecho familiar"
  • "Derecho mercantil"
  • "Derecho penal"
  • "Derecho laboral"
  • "Derecho inmobiliario"
preferred_date
String
required
The client’s requested appointment date. Stored as a string (e.g., "2024-03-15").
preferred_hour
String
required
The client’s requested appointment time. Stored as a string (e.g., "10:00").
additional_message
String
An optional free-text field for any extra context or notes the client wants to include with their request.
reservation_accepted
Boolean
default:"false"
Tracks whether a lawyer has accepted and confirmed the reservation. Set to true when the lawyer calls POST /api/form/reserve/accept/:reserveId/reserve. Drives the filtering logic in the listing endpoints.
createdAt
Date
Automatically set by Mongoose when the document is first inserted.
updatedAt
Date
Automatically updated by Mongoose every time the document is modified.

Schema source

// src/models/FormReserve.js
import mongoose from "mongoose";
const { Schema } = mongoose;

const FormReserveSchema = new Schema(
  {
    full_name: String,
    email: String,
    phone: Number,
    required_service: {
      type: String,
      enum: [
        "Derecho civil",
        "Derecho familiar",
        "Derecho mercantil",
        "Derecho penal",
        "Derecho laboral",
        "Derecho inmobiliario",
      ],
      default: "",
    },
    preferred_date: String,
    preferred_hour: String,
    additional_message: String,
    reservation_accepted: { type: Boolean, default: false },
  },
  { timestamps: true }
);

export const FormReserve = mongoose.model("FormReserve", FormReserveSchema);

Allowed values for required_service

ValueArea of Law
Derecho civilCivil law
Derecho familiarFamily law
Derecho mercantilCommercial / mercantile law
Derecho penalCriminal law
Derecho laboralLabor / employment law
Derecho inmobiliarioReal estate law

MongoDB connection

Database connectivity is handled by the MongooseDB() function in src/database/DB/mongooseDB.js. It reads the connection string from the MONGODB_URL environment variable (defined in src/config.js via dotenv) and establishes the connection using mongoose.connect().
// src/database/DB/mongooseDB.js
import mongoose from "mongoose";
import dotenv from "dotenv";

dotenv.config();

const DB_URI = process.env.MONGODB_URL;
mongoose.set("strictQuery", true);

export const MongooseDB = () => {
  mongoose
    .connect(DB_URI, {})
    .then(() => console.log("Connected to Mongoose ✅"))
    .catch(() => console.log("Error connecting to Mongoose ❌"));
};
strictQuery: true is set globally, which means Mongoose will filter out any query fields that are not defined in the schema rather than passing them through to MongoDB. This prevents accidental queries against undefined fields.
Set the MONGODB_URL environment variable in a .env file at the project root (e.g., MONGODB_URL=mongodb+srv://user:pass@cluster.mongodb.net/despacho). The config.js module loads it automatically via dotenv.config() when the server starts.

Build docs developers (and LLMs) love