first commit
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import WapServerList from "../WapServer/WapServer";
|
||||
import { SendBotMessageParams } from "../WapServer/WapServer.Interface";
|
||||
import { INotificationsAdapter, NotificationDataWap } from "./Notifications.Interface";
|
||||
|
||||
export class NotificationsWhatsappBotAdapter implements INotificationsAdapter<NotificationDataWap> {
|
||||
async send(data: NotificationDataWap): Promise<void> {
|
||||
|
||||
if (!data.phoneNumber) {
|
||||
throw new Error("La notificación de Whatsapp no se ha enviado. No se ha especificado el número de contacto.");
|
||||
}
|
||||
|
||||
const sendData: SendBotMessageParams = {
|
||||
number: data.phoneNumber,
|
||||
message: data.message,
|
||||
companyId: data.companyId,
|
||||
sessionUser: data.sessionUser
|
||||
}
|
||||
|
||||
await WapServerList.sendMessage(sendData);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { INotificationsAdapter, NotificationDataEmail } from "./Notifications.Interface";
|
||||
import nodemailer, { SentMessageInfo } from "nodemailer";
|
||||
|
||||
export class NotificationsNodemailerAdapter implements INotificationsAdapter<NotificationDataEmail> {
|
||||
async send(data: NotificationDataEmail): Promise<void> {
|
||||
const mailTransporter = nodemailer.createTransport({
|
||||
service: "gmail",
|
||||
secure: true,
|
||||
port: 465,
|
||||
auth: {
|
||||
user: process.env.GMAIL_EMAIL,
|
||||
pass: process.env.GMAIL_PASSWORD,
|
||||
},
|
||||
});
|
||||
|
||||
const mailOptions = {
|
||||
from: process.env.GMAIL_EMAIL,
|
||||
to: data.email,
|
||||
subject: data.subject,
|
||||
text: data.message,
|
||||
};
|
||||
|
||||
await mailTransporter.sendMail(
|
||||
mailOptions,
|
||||
(error: Error | null, info: SentMessageInfo) => {
|
||||
if (error) {
|
||||
return console.log(error);
|
||||
}
|
||||
console.log("Correo enviado: " + info.response);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { io } from "../../index";
|
||||
import SystemNotificationsList from "../SystemNotifications/SystemNotification";
|
||||
import { CreateSystemNotificationParams } from "../SystemNotifications/SystemNotification.Interface";
|
||||
import { INotificationsAdapter, NotificationDataSystem } from "./Notifications.Interface";
|
||||
|
||||
export class NotificationsSystemAdapter implements INotificationsAdapter<NotificationDataSystem> {
|
||||
async send(data: NotificationDataSystem): Promise<void> {
|
||||
const sendData: CreateSystemNotificationParams = {
|
||||
userId: data.userId,
|
||||
subject: data.subject,
|
||||
conversationId: data.conversationId,
|
||||
message: data.message,
|
||||
type: data.type,
|
||||
code: data.code,
|
||||
};
|
||||
|
||||
const notif = await SystemNotificationsList.createNotification(sendData);
|
||||
|
||||
if (notif.userId) {
|
||||
const countUnreadNotifications = await SystemNotificationsList.finUnreadNotifications({
|
||||
userId: notif.userId,
|
||||
sessionUser: notif.userId,
|
||||
});
|
||||
|
||||
io.to(`user:${data.userId}`).emit("new_system_notification", countUnreadNotifications);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import axios from "axios";
|
||||
import { INotificationsAdapter, NotificationDataEmail } from "./Notifications.Interface";
|
||||
|
||||
export class NotificationsDonWebAdapter implements INotificationsAdapter<NotificationDataEmail> {
|
||||
async send(data: NotificationDataEmail): Promise<void> {
|
||||
const dataEmail = {
|
||||
from: "norepply@turnosxpress.com.ar",
|
||||
to: data.email,
|
||||
subject: data.subject,
|
||||
html: data.message,
|
||||
};
|
||||
|
||||
const apiConfig = {
|
||||
method: "post",
|
||||
maxBodyLength: Infinity,
|
||||
url: "https://api.envialosimple.email/api/v1/mail/send",
|
||||
headers: {
|
||||
Authorization: `Bearer ${process.env.DONWEB_MAIL_API_KEY}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
data: dataEmail,
|
||||
};
|
||||
|
||||
await axios(apiConfig);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
export enum NOTIFICATION_TYPES {
|
||||
EMAIL = "EMAIL",
|
||||
WAP = "WAP",
|
||||
SYSTEM = "SYSTEM",
|
||||
}
|
||||
|
||||
export type NotificationDataEmail = {
|
||||
email: string;
|
||||
subject: string;
|
||||
message: string;
|
||||
};
|
||||
|
||||
export type NotificationDataWap = {
|
||||
phoneNumber: string;
|
||||
message: string;
|
||||
companyId: string;
|
||||
sessionUser: string;
|
||||
};
|
||||
|
||||
export type NotificationDataSystem = {
|
||||
userId: string;
|
||||
conversationId?: string;
|
||||
subject: string;
|
||||
message: string;
|
||||
type?: string;
|
||||
code?: string;
|
||||
};
|
||||
|
||||
export interface INotificationsAdapter<T> {
|
||||
send: (data: T) => Promise<void>;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import {
|
||||
NotificationDataEmail,
|
||||
NotificationDataSystem,
|
||||
NotificationDataWap,
|
||||
} from "./Notifications.Interface";
|
||||
|
||||
import { NotificationsWhatsappBotAdapter } from "./Notifications.Adapter.Bot";
|
||||
import { NotificationsSystemAdapter } from "./Notifications.Adapter.System";
|
||||
import { NotificationsDonWebAdapter } from "./Notifications.Adapter.donweb";
|
||||
|
||||
class NotificationsMngr {
|
||||
//notificationsNodeMailer: NotificationsNodemailerAdapter;
|
||||
notificationsDonWeb: NotificationsDonWebAdapter;
|
||||
notificationsBot: NotificationsWhatsappBotAdapter;
|
||||
notificationsSystem: NotificationsSystemAdapter;
|
||||
constructor() {
|
||||
//this.notificationsNodeMailer = new NotificationsNodemailerAdapter();
|
||||
this.notificationsDonWeb = new NotificationsDonWebAdapter();
|
||||
this.notificationsBot = new NotificationsWhatsappBotAdapter();
|
||||
this.notificationsSystem = new NotificationsSystemAdapter();
|
||||
}
|
||||
|
||||
async sendEmail(data: NotificationDataEmail): Promise<void> {
|
||||
//await this.notificationsNodeMailer.send(data);
|
||||
await this.notificationsDonWeb.send(data);
|
||||
}
|
||||
|
||||
async sendWap(data: NotificationDataWap): Promise<void> {
|
||||
await this.notificationsBot.send(data);
|
||||
}
|
||||
|
||||
async sendSystemNotification(data: NotificationDataSystem): Promise<void> {
|
||||
await this.notificationsSystem.send(data);
|
||||
}
|
||||
}
|
||||
|
||||
export const NotificationsManager = new NotificationsMngr();
|
||||
Reference in New Issue
Block a user