UNPKG

@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.

110 lines (109 loc) 4.18 kB
/* * */ import { MongoCollectionClass } from '../../common/mongo-collection.js'; import { CreateUserSchema, PermissionCatalog, UpdateUserSchema } from '@tmlmobilidade/types'; import { asyncSingletonProxy } from '@tmlmobilidade/utils'; /* * */ class UsersClass extends MongoCollectionClass { static _instance; createSchema = CreateUserSchema; updateSchema = UpdateUserSchema; constructor() { super(); } static async getInstance() { if (!UsersClass._instance) { const instance = new UsersClass(); await instance.connect(); UsersClass._instance = instance; } return UsersClass._instance; } async findByEmail(email, options) { const foundUser = await this.mongoCollection.findOne({ email: { $eq: email } }); if (!foundUser) return null; if (options?.includeUnsafeProperties) return foundUser; return this.sanitizeUser(foundUser); } async findById(id, options) { const foundUser = await this.mongoCollection.findOne({ _id: id }, options); if (!foundUser) return null; if (options?.includeUnsafeProperties) return foundUser; return this.sanitizeUser(foundUser); } /** * Finds users by their organization code. * @param code The code of the organization to find users for. * @param includeUnsafeProperties 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) { const foundUsers = await this.mongoCollection.find({ organization_id: { $in: [id] } }).toArray(); return foundUsers.map(item => this.sanitizeUser(item)); } /** * 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) { const foundUsers = await this.mongoCollection.find({ role_ids: { $in: [role] } }).toArray(); return foundUsers.map(item => this.sanitizeUser(item)); } /** * 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, options) { const foundUsers = await this.mongoCollection.find(filter ?? {}, options).toArray(); return foundUsers.map(item => this.sanitizeUser(item)); } /** * Finds a single document matching the filter criteria. * @param filter Filter criteria to match the document. * @returns A promise that resolves to the matching document or null if not found. */ async findOne(filter) { const foundUser = await this.mongoCollection.findOne(filter); if (!foundUser) return null; return this.sanitizeUser(foundUser); } 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 'DATABASE_URI'; } sanitizeUser(user) { return { ...user, password_hash: null, permissions: PermissionCatalog.sanitize(user.permissions), session_ids: null, verification_token_ids: null, }; } } /* * */ /** * @deprecated This class is deprecated and will be removed in the future. * Use `@tmlmobilidade/go-interfaces-go-db` instead. */ export const users = asyncSingletonProxy(UsersClass);