Skip to main content

UserService Class

The UserService class provides methods for interacting with user data stored in MongoDB. It uses Mongoose for database operations and is injected with the User model from the DELTA_DISPATCH database. Source: src/app/user/user.service.ts

Constructor

The service is initialized with dependency injection:
constructor(
  @InjectModel(User.name, DELTA_DISPATCH_DB_NAME)
  private readonly userModel: Model<UserDocument>,
) { }
userModel
Model<UserDocument>
Mongoose model for User collection, injected from the DELTA_DISPATCH database connection

Methods

findOneDni()

Finds a single user by their DNI (national identity document number). Source: src/app/user/user.service.ts:14
async findOneDni(dni: number): Promise<UserDocument | null>

Parameters

dni
number
required
The user’s DNI number to search for

Returns

result
UserDocument | null
Returns the user document if found, or null if no user exists with the given DNI

Example

import { UserService } from './user.service';

// Inject the service
constructor(private readonly userService: UserService) {}

// Find user by DNI
async authenticateUser(dni: number) {
  const user = await this.userService.findOneDni(dni);
  
  if (!user) {
    throw new Error('User not found');
  }
  
  return {
    id: user._id,
    name: user.user_fullname,
    role: user.user_role,
    isAdmin: user.user_admin,
  };
}

Implementation Details

The method uses Mongoose’s findOne() query to search for a user where user_dni matches the provided DNI:
return this.userModel.findOne({ user_dni: dni }).exec();

User Schema

The User model is defined in src/app/user/schema/user.schema.ts and includes the following key fields:

Authentication Fields

user_dni
number
User’s national identity document number (used for authentication)
user_username
string
Username for login
user_password
string
Hashed password
user_email
string
User’s email address

Profile Fields

user_name
string
User’s first name
user_lastname
string
User’s last name
user_fullname
string
User’s full name
user_cellphone
number
User’s cellphone number
user_photo
string
URL or path to user’s photo
user_color
string
Color associated with the user (for UI purposes)

Authorization Fields

user_role
string
default:"Operador"
User’s role in the system
user_position
string
default:"Empleado"
User’s job position
user_isroot
boolean
Whether the user has root privileges
user_admin
boolean
Whether the user has admin privileges
user_is_inspector
boolean
Whether the user is an inspector (for cadastre module)

Module Access Permissions

web_seguridad_ciudadana
boolean
Access to citizen security module
web_catastro
boolean
Access to cadastre module
web_rentas
boolean
Access to revenue module
web_inspecciones
boolean
Access to inspections module
fichas
boolean
Access to forms/records module

State Fields

user_state
boolean
default:"true"
Whether the user account is active
user_available
boolean
default:"true"
Whether the user is currently available
user_date_deactivation
string
Date when the user was deactivated

Additional Fields

workgroup_name
string
Name of the workgroup the user belongs to
notification_token
string
Token for push notifications
user_code
number
Internal user code
user_creation_date
Date
When the user account was created
user_latest_update
Date
When the user account was last updated

MongoDB Connection

The UserService connects to MongoDB using the DELTA_DISPATCH_DB_NAME connection name. This connection is configured through the DatabaseModule:
DatabaseModule.forDeltaDispatchApplication(process.env.MONGO_URI)
See Database Configuration for more details on the multi-database setup.

Build docs developers (and LLMs) love