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.

60 lines (59 loc) 1.97 kB
/* * */ import { MongoCollectionClass } from '../../mongo-collection.js'; import { HttpException, HttpStatus } from '@tmlmobilidade/lib'; import { OrganizationSchema, UpdateOrganizationSchema } from '@tmlmobilidade/types'; import { AsyncSingletonProxy } from '@tmlmobilidade/utils'; /* * */ class OrganizationsClass extends MongoCollectionClass { static _instance; createSchema = OrganizationSchema; updateSchema = UpdateOrganizationSchema; constructor() { super(); } static async getInstance() { if (!OrganizationsClass._instance) { const instance = new OrganizationsClass(); await instance.connect(); OrganizationsClass._instance = instance; } return OrganizationsClass._instance; } /** * Finds an organization by its code * * @param code - The code of the organization to find * @returns A promise that resolves to the matching organization document or null if not found */ async findByCode(code) { return this.mongoCollection.findOne({ code }); } /** * Updates an organization by its code * * @param code - The code of the organization to update * @param fields - The fields to update * @returns A promise that resolves to the result of the update operation */ async updateByCode(code, fields) { return this.mongoCollection.updateOne({ code }, { $set: fields }); } /** * Disable Update Many */ async updateMany() { throw new HttpException(HttpStatus.METHOD_NOT_ALLOWED, 'Method not allowed for organizations'); } getCollectionIndexes() { return [ { background: true, key: { name: 1 }, unique: true }, ]; } getCollectionName() { return 'organizations'; } getEnvName() { return 'TML_INTERFACE_ORGANIZATIONS'; } } export const organizations = AsyncSingletonProxy(OrganizationsClass);