Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 0 additions & 91 deletions src/controllers/gupshup-webhook.controller.ts

This file was deleted.

155 changes: 0 additions & 155 deletions src/controllers/meta-cloud-whatsapp-webhook.controller.ts

This file was deleted.

65 changes: 32 additions & 33 deletions src/factories/whatsapp.factory.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
import { Injectable, Logger } from "@nestjs/common";
import { Inject, Injectable, Logger } from "@nestjs/common";
import { ConfigType } from "@nestjs/config";
import { ModuleRef } from "@nestjs/core";
import { SolidRegistry } from "src/helpers/solid-registry";
import { IWhatsAppTransport } from "src/interfaces";
import { SettingService } from "src/services/setting.service";
import type { SolidCoreSetting } from "src/services/settings/default-settings-provider.service";

function norm(s?: string) {
return s?.trim().toLowerCase();
}

// This factory will be use to return a mail service instance, using the configured environment variables
@Injectable()
export class WhatsAppFactory {
private readonly logger = new Logger(WhatsAppFactory.name);

constructor(
private readonly moduleRef: ModuleRef,
private readonly solidRegistry: SolidRegistry,
private readonly settingService: SettingService,
) {}

getWhatsappService(name?: string): IWhatsAppTransport {
const providerKey =
name ||
this.settingService.getConfigValue<SolidCoreSetting>("whatsappProvider");

if (!providerKey) {
throw new Error("Unable to resolve whatsapp provider");
}

const whatsappProviders = this.solidRegistry.getWhatsappProviders();

if (!whatsappProviders.length) {
throw new Error("No whatsapp providers are registered.");
}

const whatsappServiceProvider = whatsappProviders.find((provider) =>
provider.name?.toLowerCase().includes(providerKey.toLowerCase()),
);

if (!whatsappServiceProvider) {
throw new Error(`WhatsApp provider '${providerKey}' not found`);
private readonly logger = new Logger(this.constructor.name);
constructor(
private readonly moduleRef: ModuleRef, // Use the module ref to dynamically resolve the mail service
private readonly solidRegistry: SolidRegistry,
private readonly settingService: SettingService,
) { }

getWhatsappService(name: string = null): IWhatsAppTransport {
// This is the default provider
const whatsappServiceName = name || this.settingService.getConfigValue<SolidCoreSetting>("whatsappProvider");
if (!whatsappServiceName) {
throw new Error("Unable to resolve whatsapp provider")
}
const whatsappProviders = this.solidRegistry.getWhatsappProviders();

// Return the instance which matches the whatsappServiceName
if (!whatsappProviders.length) {
// throw new Error("No mail providers are registered.");
this.logger.error("No whatsapp providers are registered.");
}

const whatsappServiceProvider = whatsappProviders.find(provider => provider.name === whatsappServiceName);

return whatsappServiceProvider.instance as IWhatsAppTransport;
}

return whatsappServiceProvider.instance as IWhatsAppTransport;
}
}
}
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ export * from './services/solid-introspect.service'
export * from './services/user.service'
export * from './services/view-metadata.service'
export * from './services/whatsapp/Msg91WhatsappService' //rename
export * from './services/whatsapp/GupshupOtpWhatsappService'
export * from './services/setting.service'
export * from './services/encryption.service'
export * from './services/info.service'
Expand Down
55 changes: 6 additions & 49 deletions src/listeners/user-registration.listener.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,14 @@

import { User } from "../entities/user.entity";
import { OnEvent } from "@nestjs/event-emitter";
import { Injectable, Logger } from "@nestjs/common";
import { EventDetails, EventType } from "../interfaces";
import { WhatsAppFactory } from "src/factories/whatsapp.factory";

@Injectable()
export class UserRegistrationListener {
private readonly logger = new Logger(UserRegistrationListener.name);

constructor(private readonly whatsAppFactory: WhatsAppFactory) {}

@OnEvent(EventType.USER_REGISTERED)
async handleUserRegistration(event: EventDetails<User>) {
this.logger.log(`User registered with details: ${JSON.stringify(event.payload)}`);

const notifyTo = process.env.WHATSAPP_EVENT_NOTIFY_TO;
if (!notifyTo) {
this.logger.debug("WHATSAPP_EVENT_NOTIFY_TO not set. Skipping registration WhatsApp notification.");
return;
}

try {
const whatsappService = this.whatsAppFactory.getWhatsappService();
const username = event.payload?.username || "User";
const userId = event.payload?.id || "N/A";

await whatsappService.sendWhatsAppMessage(
notifyTo,
"registration_event",
{
payload: {
channel: "whatsapp",
source: process.env.COMMON_GUPSHUP_WHATSAPP_SOURCE,
destination: notifyTo,
"src.name": process.env.COMMON_GUPSHUP_APP_NAME || "solidx",
message: {
type: "text",
text: `New user registered: ${username} (id: ${userId})`,
},
},
},
);

this.logger.log(`Sent registration WhatsApp notification to ${notifyTo}`);
} catch (error: any) {
const status = error?.response?.status;
const responseData = error?.response?.data;
const errorMessage = error?.message;
const stack = error?.stack;

this.logger.error(
`Failed to send registration WhatsApp notification to ${notifyTo}. status=${status ?? "unknown"}, response=${typeof responseData === "object" ? JSON.stringify(responseData) : responseData}, message=${errorMessage}, stack=${stack}`,
);
private logger = new Logger(UserRegistrationListener.name);
@OnEvent(EventType.USER_REGISTERED)
handleUserRegistration(event: EventDetails<User>) {
this.logger.log(`User registered with details: ${JSON.stringify(event.payload)}`);
}
}
}
}
Loading