From c62afee10386de125d2077bd9bb048f10a6b46f7 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Wed, 17 Jul 2024 15:53:49 +0200 Subject: [PATCH 1/2] feat: send signup event to Loops --- packages/server/src/config/index.ts | 4 ++ packages/server/src/loaders/eventEmitter.ts | 4 ++ .../services/Loops/LoopsEventsSubscriber.ts | 47 +++++++++++++++++++ packages/server/src/subscribers/events.ts | 7 +++ 4 files changed, 62 insertions(+) create mode 100644 packages/server/src/services/Loops/LoopsEventsSubscriber.ts diff --git a/packages/server/src/config/index.ts b/packages/server/src/config/index.ts index 8172ddace8..b8a862343d 100644 --- a/packages/server/src/config/index.ts +++ b/packages/server/src/config/index.ts @@ -237,4 +237,8 @@ module.exports = { endpoint: process.env.S3_ENDPOINT, bucket: process.env.S3_BUCKET || 'bigcapital-documents', }, + + loops: { + apiKey: process.env.LOOPS_API_KEY, + }, }; diff --git a/packages/server/src/loaders/eventEmitter.ts b/packages/server/src/loaders/eventEmitter.ts index 8595bed553..9e2ae276e0 100644 --- a/packages/server/src/loaders/eventEmitter.ts +++ b/packages/server/src/loaders/eventEmitter.ts @@ -113,6 +113,7 @@ import { UnlinkBankRuleOnDeleteBankRule } from '@/services/Banking/Rules/events/ import { DecrementUncategorizedTransactionOnMatching } from '@/services/Banking/Matching/events/DecrementUncategorizedTransactionsOnMatch'; import { DecrementUncategorizedTransactionOnExclude } from '@/services/Banking/Exclude/events/DecrementUncategorizedTransactionOnExclude'; import { DecrementUncategorizedTransactionOnCategorize } from '@/services/Cashflow/subscribers/DecrementUncategorizedTransactionOnCategorize'; +import { LoopsEventsSubscriber } from '@/services/Loops/LoopsEventsSubscriber'; export default () => { return new EventPublisher(); @@ -274,5 +275,8 @@ export const susbcribers = () => { // Plaid RecognizeSyncedBankTranasctions, + + // Loops + LoopsEventsSubscriber ]; }; diff --git a/packages/server/src/services/Loops/LoopsEventsSubscriber.ts b/packages/server/src/services/Loops/LoopsEventsSubscriber.ts new file mode 100644 index 0000000000..ae212b0363 --- /dev/null +++ b/packages/server/src/services/Loops/LoopsEventsSubscriber.ts @@ -0,0 +1,47 @@ +import axios from 'axios'; +import config from '@/config'; +import { IAuthSignUpVerifiedEventPayload } from '@/interfaces'; +import events from '@/subscribers/events'; +import { SystemUser } from '@/system/models'; + +export class LoopsEventsSubscriber { + /** + * Constructor method. + */ + public attach(bus) { + bus.subscribe( + events.auth.signUpConfirmed, + this.triggerEventOnSignupVerified.bind(this) + ); + } + + /** + * Once the user verified sends the event to the Loops. + * @param {IAuthSignUpVerifiedEventPayload} param0 + */ + public async triggerEventOnSignupVerified({ + email, + userId, + }: IAuthSignUpVerifiedEventPayload) { + const user = await SystemUser.query().findById(userId); + + const options = { + method: 'POST', + url: 'https://app.loops.so/api/v1/events/send', + headers: { + Authorization: `Bearer ${config.loops.apiKey}`, + 'Content-Type': 'application/json', + }, + data: { + email, + userId, + firstName: user.firstName, + lastName: user.lastName, + eventName: 'USER_VERIFIED', + eventProperties: {}, + mailingLists: {}, + }, + }; + await axios(options); + } +} diff --git a/packages/server/src/subscribers/events.ts b/packages/server/src/subscribers/events.ts index 711dbce35d..e90aeb3095 100644 --- a/packages/server/src/subscribers/events.ts +++ b/packages/server/src/subscribers/events.ts @@ -40,6 +40,13 @@ export default { baseCurrencyUpdated: 'onOrganizationBaseCurrencyUpdated', }, + /** + * User subscription events. + */ + subscription: { + onSubscribed: 'onOrganizationSubscribed', + }, + /** * Tenants managment service. */ From 77079c463304cb0a6c100ae64bf1e5c2201fa798 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Wed, 17 Jul 2024 15:55:37 +0200 Subject: [PATCH 2/2] feat: fix --- packages/server/src/services/Loops/LoopsEventsSubscriber.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/server/src/services/Loops/LoopsEventsSubscriber.ts b/packages/server/src/services/Loops/LoopsEventsSubscriber.ts index ae212b0363..33fe56e1a8 100644 --- a/packages/server/src/services/Loops/LoopsEventsSubscriber.ts +++ b/packages/server/src/services/Loops/LoopsEventsSubscriber.ts @@ -23,6 +23,10 @@ export class LoopsEventsSubscriber { email, userId, }: IAuthSignUpVerifiedEventPayload) { + // Can't continue since the Loops the api key is not configured. + if (!config.loops.apiKey) { + return; + } const user = await SystemUser.query().findById(userId); const options = {