Repository: novuhq/novu
File: libs/dal/src/repositories/notification-template/notification-template.repository.ts (TypeScript)

Code before:
import { BaseRepository } from '../base-repository';
import { EnvironmentRepository } from '../environment';
import { NotificationTemplateDBModel, NotificationTemplateEntity } from './notification-template.entity';
import { NotificationTemplate } from './notification-template.schema';

type NotificationTemplateQuery = FilterQuery<NotificationTemplateDBModel> & EnforceEnvOrOrgIds;

export class NotificationTemplateRepository extends BaseRepository<
  NotificationTemplateDBModel,
  NotificationTemplateEntity,
  EnforceEnvOrOrgIds
> {
  private notificationTemplate: SoftDeleteModel;
  private environmentRepository = new EnvironmentRepository();

  constructor() {
    super(NotificationTemplate, NotificationTemplateEntity);
    this.notificationTemplate = NotificationTemplate;
  }

  async findPublishable(environmentId: string, organizationId: string): Promise<NotificationTemplateEntity[]> {
    const items = await this.MongooseModel.find({
      _environmentId: environmentId,
      _organizationId: organizationId,
      origin: { $in: [ResourceOriginEnum.NOVU_CLOUD] },
      type: { $in: [ResourceTypeEnum.BRIDGE] },
    })
      .select({ _id: 1, name: 1, 'triggers.identifier': 1, updatedAt: 1, _updatedBy: 1, _environmentId: 1 })
      .populate('updatedBy', '_id firstName lastName externalId');

    return this.mapEntities(items);
  }
  async findByTriggerIdentifier(environmentId: string, identifier: string, session?: ClientSession | null) {
    const requestQuery: NotificationTemplateQuery = {
      _environmentId: environmentId,
      'triggers.identifier': identifier,
    };

    const query = this.MongooseModel.findOne(requestQuery, undefined, { session })
      .populate('steps.template')
      .populate('updatedBy');

    const item = await query;

    return this.mapEntity(item);
  }

  async findAllByTriggerIdentifier(
    environmentId: string,
    identifier: string,
    session?: ClientSession | null

Review comment (performance):
I'm not entirely sure, but I think doing origin: ResourceOriginEnum.NOVU_CLOUD and type: ResourceTypeEnum.BRIDGE without the $in might be slightly more optimized

Code after:
import { BaseRepository } from '../base-repository';
import { EnvironmentRepository } from '../environment';
import { NotificationTemplateDBModel, NotificationTemplateEntity } from './notification-template.entity';
import { NotificationTemplate } from './notification-template.schema';

type NotificationTemplateQuery = FilterQuery<NotificationTemplateDBModel> & EnforceEnvOrOrgIds;

export class NotificationTemplateRepository extends BaseRepository<
  NotificationTemplateDBModel,
  NotificationTemplateEntity,
  EnforceEnvOrOrgIds
> {
  private notificationTemplate: SoftDeleteModel;
  private environmentRepository = new EnvironmentRepository();

  constructor() {
    super(NotificationTemplate, NotificationTemplateEntity);
    this.notificationTemplate = NotificationTemplate;
  }

  async findPublishable(environmentId: string, organizationId: string): Promise<NotificationTemplateEntity[]> {
    const items = await this.MongooseModel.find({
      _environmentId: environmentId,
      _organizationId: organizationId,
      type: ResourceTypeEnum.BRIDGE,
      origin: ResourceOriginEnum.NOVU_CLOUD,
    })
      .select({ _id: 1, name: 1, 'triggers.identifier': 1, updatedAt: 1, _updatedBy: 1, _environmentId: 1 })
      .populate('updatedBy', '_id firstName lastName externalId');

    return this.mapEntities(items);
  }
  async findByTriggerIdentifier(environmentId: string, identifier: string, session?: ClientSession | null) {
    const requestQuery: NotificationTemplateQuery = {
      _environmentId: environmentId,
      'triggers.identifier': identifier,
    };

    const query = this.MongooseModel.findOne(requestQuery, undefined, { session })
      .populate('steps.template')
      .populate('updatedBy');

    const item = await query;

    return this.mapEntity(item);
  }

  async findAllByTriggerIdentifier(
    environmentId: string,
    identifier: string,
    session?: ClientSession | null