From 503d0016ea8dce474892f995bfc0e0d7d1988e7e Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Thu, 3 Apr 2025 20:03:55 +0200 Subject: [PATCH] refactor(nestjs): loops module --- .../server-nest/src/modules/App/App.module.ts | 2 + .../src/modules/Auth/Auth.controller.ts | 2 +- .../src/modules/Loops/Loops.module.ts | 7 +++ .../modules/Loops/LoopsEvents.subscriber.ts | 56 +++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 packages/server-nest/src/modules/Loops/Loops.module.ts create mode 100644 packages/server-nest/src/modules/Loops/LoopsEvents.subscriber.ts diff --git a/packages/server-nest/src/modules/App/App.module.ts b/packages/server-nest/src/modules/App/App.module.ts index 0be07bc05..62ca63bcd 100644 --- a/packages/server-nest/src/modules/App/App.module.ts +++ b/packages/server-nest/src/modules/App/App.module.ts @@ -76,6 +76,7 @@ import { TenantDBManagerModule } from '../TenantDBManager/TenantDBManager.module import { PaymentServicesModule } from '../PaymentServices/PaymentServices.module'; import { AuthModule } from '../Auth/Auth.module'; import { TenancyModule } from '../Tenancy/Tenancy.module'; +import { LoopsModule } from '../Loops/Loops.module'; @Module({ imports: [ @@ -187,6 +188,7 @@ import { TenancyModule } from '../Tenancy/Tenancy.module'; OrganizationModule, TenantDBManagerModule, PaymentServicesModule, + LoopsModule ], controllers: [AppController], providers: [ diff --git a/packages/server-nest/src/modules/Auth/Auth.controller.ts b/packages/server-nest/src/modules/Auth/Auth.controller.ts index 5e57fb392..bf1bb6798 100644 --- a/packages/server-nest/src/modules/Auth/Auth.controller.ts +++ b/packages/server-nest/src/modules/Auth/Auth.controller.ts @@ -17,8 +17,8 @@ import { LocalAuthGuard } from './guards/local.guard'; import { JwtService } from '@nestjs/jwt'; import { AuthSigninService } from './commands/AuthSignin.service'; -@ApiTags('Auth') @Controller('/auth') +@ApiTags('Auth') @PublicRoute() export class AuthController { constructor( diff --git a/packages/server-nest/src/modules/Loops/Loops.module.ts b/packages/server-nest/src/modules/Loops/Loops.module.ts new file mode 100644 index 000000000..190b8a242 --- /dev/null +++ b/packages/server-nest/src/modules/Loops/Loops.module.ts @@ -0,0 +1,7 @@ +import { Module } from '@nestjs/common'; +import { LoopsEventsSubscriber } from './LoopsEvents.subscriber'; + +@Module({ + providers: [LoopsEventsSubscriber], +}) +export class LoopsModule {} diff --git a/packages/server-nest/src/modules/Loops/LoopsEvents.subscriber.ts b/packages/server-nest/src/modules/Loops/LoopsEvents.subscriber.ts new file mode 100644 index 000000000..7d19f311a --- /dev/null +++ b/packages/server-nest/src/modules/Loops/LoopsEvents.subscriber.ts @@ -0,0 +1,56 @@ +import axios from 'axios'; +import { OnEvent } from '@nestjs/event-emitter'; +import { Inject, Injectable } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { IAuthSignUpVerifiedEventPayload } from '../Auth/Auth.interfaces'; +import { SystemUser } from '../System/models/SystemUser'; +import { events } from '@/common/events/events'; + +@Injectable() +export class LoopsEventsSubscriber { + + constructor( + private readonly configService: ConfigService, + + @Inject(SystemUser.name) + private readonly systemUserModel: typeof SystemUser + ) { + + } + /** + * Once the user verified sends the event to the Loops. + * @param {IAuthSignUpVerifiedEventPayload} param0 + */ + @OnEvent(events.auth.signUpConfirmed) + public async triggerEventOnSignupVerified({ + email, + userId, + }: IAuthSignUpVerifiedEventPayload) { + const apiKey = this.configService.get('loops.apiKey'); + + // Can't continue since the Loops the api key is not configured. + if (!apiKey) { + return; + } + const user = await this.systemUserModel.query().findById(userId); + + const options = { + method: 'POST', + url: 'https://app.loops.so/api/v1/events/send', + headers: { + Authorization: `Bearer ${apiKey}`, + 'Content-Type': 'application/json', + }, + data: { + email, + userId, + firstName: user.firstName, + lastName: user.lastName, + eventName: 'USER_VERIFIED', + eventProperties: {}, + mailingLists: {}, + }, + }; + await axios(options); + } +}