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..33fe56e1a8 --- /dev/null +++ b/packages/server/src/services/Loops/LoopsEventsSubscriber.ts @@ -0,0 +1,51 @@ +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) { + // 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 = { + 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. */