@tmlmobilidade/interfaces
Version:
This package provides SDK-style connectors for interacting with databases (e.g., stops, plans, rides, alerts) and external providers (e.g., authentication, storage). It simplifies data access and integration across projects.
117 lines (116 loc) • 4.57 kB
JavaScript
/* * */
import { MongoCollectionClass } from '../../mongo-collection.js';
import { UpdateUserSchema, UserSchema } from '@tmlmobilidade/types';
import { AsyncSingletonProxy } from '@tmlmobilidade/utils';
class UsersClass extends MongoCollectionClass {
static _instance;
createSchema = UserSchema;
updateSchema = UpdateUserSchema;
constructor() {
super();
}
static async getInstance() {
if (!UsersClass._instance) {
const instance = new UsersClass();
await instance.connect();
UsersClass._instance = instance;
}
return UsersClass._instance;
}
/**
* Finds a user document by its email.
*
* @param email - The email of the user to find
* @param includePasswordHash - Whether to include the password hash in the result
* @returns A promise that resolves to the matching user document or null if not found
*/
async findByEmail(email, includePasswordHash = false) {
const user = await this.mongoCollection.findOne({ email });
if (!user) {
return null;
}
return includePasswordHash ? user : this.deletePasswordHash(user);
}
/**
* Finds a document by its ID.
*
* @param id - The ID of the document to find
* @param includePasswordHash - Whether to include the password hash in the result
* @returns A promise that resolves to the matching document or null if not found
*/
async findById(id, options, includePasswordHash = false) {
const user = await this.mongoCollection.findOne({ _id: id }, options);
if (!user) {
return null;
}
return includePasswordHash ? user : this.deletePasswordHash(user);
}
/**
* Finds users by their organization code
*
* @param code - The code of the organization to find users for
* @param includePasswordHash - Whether to include the password hash in the result
* @returns A promise that resolves to the matching user documents or null if not found
*/
async findByOrganization(id, includePasswordHash = false) {
const users = await this.mongoCollection.find({ organization_ids: { $in: [id] } }).toArray();
return includePasswordHash ? users : users.map(user => this.deletePasswordHash(user));
}
/**
* Finds a user by their role
*
* @param role - The role of the user to find
* @returns A promise that resolves to the matching user document or null if not found
*/
async findByRole(role, includePasswordHash = false) {
const users = await this.mongoCollection.find({ role_ids: { $in: [role] } }).toArray();
return includePasswordHash ? users : users.map(user => this.deletePasswordHash(user));
}
/**
* Finds multiple documents matching the filter criteria with optional pagination and sorting.
*
* @param filter - (Optional) filter criteria to match documents
* @param perPage - (Optional) number of documents per page for pagination
* @param page - (Optional) page number for pagination
* @param sort - (Optional) sort specification
* @returns A promise that resolves to an array of matching documents
*/
async findMany(filter, perPage, page, sort) {
const query = this.mongoCollection.find(filter ?? {});
if (perPage)
query.limit(perPage);
if (page && perPage)
query.skip(perPage * (page - 1));
if (sort)
query.sort(sort);
const users = await query.toArray();
return users.map(user => this.deletePasswordHash(user));
}
async findOne(filter) {
const user = await this.mongoCollection.findOne(filter);
if (!user) {
return null;
}
return this.deletePasswordHash(user);
}
getCollectionIndexes() {
return [
{ background: true, key: { email: 1 }, unique: true },
{ background: true, key: { 'profile.first_name': 1, 'profile.last_name': 1 } },
{ background: true, key: { session_ids: 1 } },
{ background: true, key: { role_ids: 1 } },
];
}
getCollectionName() {
return 'users';
}
getEnvName() {
return 'TML_INTERFACE_AUTH';
}
deletePasswordHash(user) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { password_hash, ...userWithoutPassword } = user;
return userWithoutPassword;
}
}
export const users = AsyncSingletonProxy(UsersClass);