Skip to content
Snippets Groups Projects
Select Git revision
  • 968c119efed4e7501b0df83d020cf6cd7cd70e82
  • dev default protected
  • renovate/ghcr.io-browserless-chromium-2.x
  • 168-pro-connect
  • renovate/major-nest-monorepo
  • renovate/luxon-3.x
  • renovate/gouvfr-anct-timetable-to-osm-opening-hours-2.x
  • renovate/major-typescript-eslint-monorepo
  • renovate/npm-11.x
  • renovate/mysql-9.x
  • renovate/mongo-express-1.x
  • renovate/major-jest-monorepo
  • renovate/tsconfig-paths-4.x
  • renovate/jest-junit-16.x
  • renovate/express-5.x
  • renovate/elastic-elasticsearch-8.x
  • renovate/ghost-5.x
  • renovate/elasticsearch-7.x
  • renovate/devdependencies-(non-major)
  • 722-envsubst-client-side-conf
  • master protected
  • v4.0.3
  • v4.0.1
  • v4.0.0
  • v3.4.3
  • v3.4.2
  • v3.4.1
  • v3.4.0
  • v3.3.1
  • v3.3.0
  • v3.2.0
  • v3.1.0
  • v3.0.1
  • v3.0.0
  • v2.5.0
  • v2.4.2
  • v2.4.1
  • v2.4.0
  • v2.3.2
  • v2.3.1
  • v2.3.0
41 results

newsletter.service.ts

Blame
  • To find the state of this project's repository at the time of any of these versions, check out the tags.
    newsletter.service.ts 1.75 KiB
    import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
    import { InjectModel } from '@nestjs/mongoose';
    import { Model } from 'mongoose';
    import { INewsletterSubscription } from './interface/newsletter-subscription.interface';
    import { NewsletterSubscription, NewsletterSubscriptionDocument } from './newsletter-subscription.schema';
    
    @Injectable()
    export class NewsletterService {
      constructor(
        @InjectModel(NewsletterSubscription.name) private newsletterSubscriptionModel: Model<INewsletterSubscription>
      ) {}
    
      public async newsletterSubscribe(email: string): Promise<NewsletterSubscription> {
        const existingEmail = await this.findOne(email);
        if (existingEmail) {
          throw new HttpException('Email already exists', HttpStatus.BAD_REQUEST);
        }
        await this.newsletterSubscriptionModel.create({ email: email });
        return this.findOne(email);
      }
    
      public async newsletterUnsubscribe(email: string): Promise<NewsletterSubscription> {
        const subscription = await this.findOne(email);
        if (!subscription) {
          throw new HttpException('Invalid email', HttpStatus.BAD_REQUEST);
        }
        return subscription.deleteOne();
      }
    
      public async findOne(mail: string): Promise<INewsletterSubscription | undefined> {
        return this.newsletterSubscriptionModel.findOne({ email: mail });
      }
    
      public async searchNewsletterSubscription(searchString: string): Promise<NewsletterSubscriptionDocument[]> {
        return this.newsletterSubscriptionModel.find({ email: new RegExp(searchString, 'i') });
      }
    
      public async countNewsletterSubscriptions(): Promise<number> {
        return this.newsletterSubscriptionModel.countDocuments({});
      }
    
      public async findAll(): Promise<NewsletterSubscription[]> {
        return this.newsletterSubscriptionModel.find();
      }
    }