@takentrade/takentrade-libs
Version:
TakeNTrade shared libraries
219 lines (218 loc) • 8.92 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationClient = void 0;
const common_1 = require("@nestjs/common");
const notification_enum_1 = require("./notification.enum");
const nats_1 = require("../nats");
const class_validator_1 = require("class-validator");
let NotificationClient = class NotificationClient {
natsClient;
constructor(natsClient) {
this.natsClient = natsClient;
}
validatePhoneNumber(phone) {
if (!(0, class_validator_1.isPhoneNumber)(phone)) {
throw new common_1.BadRequestException('Invalid phone number');
}
}
validateEmailAddress(email) {
if (!(0, class_validator_1.isEmail)(email)) {
throw new common_1.BadRequestException('Invalid email address');
}
}
async sendSms(to, message) {
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.NOTIFICATION_TEXT, {
to: to.slice(1),
message,
});
}
/**
* This utility method sends an OTP message via the SMS channel.
*
* @param to - The recipient phone number
* @param message - The OTP message
* @deprecated
*/
async sendSmsOTP(to, message) {
this.natsClient.emit(`notification.sms.otp`, {
to: to.slice(1),
message,
});
}
async sendCustomMail(to, options) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.NOTIFICATION_MAIL, {
to,
...options,
});
}
async sendEmailVerificationMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.EMAIL_VERIFICATION_EMAIL, {
to,
data,
});
}
async sendPasswordResetMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.PASSWORD_RESET_MAIL, {
to,
data,
});
}
async sendCreditTransactionMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.TRANSANCTION_CREDIT_MAIL, { to, data });
}
async sendDebitTransactionMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.TRANSANCTION_DEBIT_MAIL, { to, data });
}
async sendAccountFreezeMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.ACCOUNT_FREEZE_MAIL, {
to,
data,
});
}
async sendAccountStatementMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.ACCOUNT_STATEMENT_MAIL, { to, data });
}
async sendAccountUnfreezeMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.ACCOUNT_UNFREEZE_MAIL, { to, data });
}
async sendAdminNewLoanMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.ADMIN_NEW_LOAN_MAIL, {
to,
data,
});
}
async sendAjoCancellationMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.AJO_CANCELLATION_MAIL, { to, data });
}
async sendAjoContributionMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.AJO_CONTRIBUTION_MAIL, { to, data });
}
async sendAjoInvitationMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.AJO_INVITATION_MAIL, {
to,
data,
});
}
async sendAjoMembershipMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.AJO_MEMBERSHIP_MAIL, {
to,
data,
});
}
async sendContactMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.CONTACT_MAIL, {
to,
data,
});
}
async sendForgetPinOTPMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.FORGET_PIN_OTP_MAIL, {
to,
data,
});
}
async sendInvestmentCancellationMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.INVESTMENT_CANCELLATION_MAIL, { to, data });
}
async sendLoanApprovedMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.LOAN_APPROVED_MAIL, {
to,
data,
});
}
async sendLoanRepaymentFailedMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.LOAN_REPAYMENT_FAILED_MAIL, { to, data });
}
async sendLoanRepaymentSuccessMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.LOAN_REPAYMENT_SUCCESS_MAIL, { to, data });
}
async sendNewAjoMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.NEW_AJO_MAIL, {
to,
data,
});
}
async sendNewInvestmentMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.NEW_INVESTMENT_MAIL, {
to,
data,
});
}
async sendWelcomeMail(to, data) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.WELCOME_MAIL, {
to,
data,
});
}
/**
* Sends generated OTP to a recipient
* @param to - recipient phone number or email
* @param data - OTP data
* @param channel - OTP notification channel. Defaults to sms.
*/
async sendOTP(to, data, channel = notification_enum_1.OTPNotificationChannel.sms) {
if (channel == notification_enum_1.OTPNotificationChannel.mail) {
this.validateEmailAddress(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.OTP_MAIL, {
to,
...data,
});
}
else if (channel == notification_enum_1.OTPNotificationChannel.sms) {
this.validatePhoneNumber(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.NOTIFICATION_OTP_TEXT, { to, ...data });
}
else if (channel == notification_enum_1.OTPNotificationChannel.voice) {
this.validatePhoneNumber(to);
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.NOTIFICATION_OTP_VOICE, { to, ...data });
}
else {
throw new common_1.BadRequestException('Invalid OTP channel');
}
}
/**
* Sends push notification
*/
async sendPush(input) {
if (!input.tokens?.length && !input.userId) {
throw new common_1.BadRequestException('You must provide a device tokens array or a userId');
}
this.natsClient.emit(notification_enum_1.NotificationSubcribedNATSSubject.NOTIFICATION_PUSH, input);
}
};
exports.NotificationClient = NotificationClient;
exports.NotificationClient = NotificationClient = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(nats_1.NATS_CLIENT))
], NotificationClient);