refactor(nestjs): loops module

This commit is contained in:
Ahmed Bouhuolia
2025-04-03 20:03:55 +02:00
parent 0a2ac4ee56
commit 503d0016ea
4 changed files with 66 additions and 1 deletions

View File

@@ -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: [

View File

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

View File

@@ -0,0 +1,7 @@
import { Module } from '@nestjs/common';
import { LoopsEventsSubscriber } from './LoopsEvents.subscriber';
@Module({
providers: [LoopsEventsSubscriber],
})
export class LoopsModule {}

View File

@@ -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);
}
}