mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
fix: issues in authentication mail messages.
This commit is contained in:
@@ -1,35 +1,78 @@
|
||||
import { Service } from "typedi";
|
||||
import fs from 'fs';
|
||||
import { Service, Container } from "typedi";
|
||||
import Mustache from 'mustache';
|
||||
import path from 'path';
|
||||
import { ISystemUser } from '@/interfaces';
|
||||
import config from '@/../config/config';
|
||||
import { ISystemUser } from 'src/interfaces';
|
||||
|
||||
@Service()
|
||||
export default class AuthenticationMailMesssages {
|
||||
|
||||
sendWelcomeMessage() {
|
||||
const Logger = Container.get('logger');
|
||||
/**
|
||||
* Sends welcome message.
|
||||
* @param {ISystemUser} user - The system user.
|
||||
* @param {string} organizationName -
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
sendWelcomeMessage(user: ISystemUser, organizationName: string): Promise<void> {
|
||||
const Mail = Container.get('mail');
|
||||
|
||||
|
||||
const filePath = path.join(global.rootPath, 'views/mail/Welcome.html');
|
||||
const template = fs.readFileSync(filePath, 'utf8');
|
||||
const rendered = Mustache.render(template, {
|
||||
email, organizationName, firstName,
|
||||
email: user.email,
|
||||
firstName: user.firstName,
|
||||
organizationName,
|
||||
});
|
||||
const mailOptions = {
|
||||
to: email,
|
||||
to: user.email,
|
||||
from: `${process.env.MAIL_FROM_NAME} ${process.env.MAIL_FROM_ADDRESS}`,
|
||||
subject: 'Welcome to Bigcapital',
|
||||
html: rendered,
|
||||
};
|
||||
Mail.sendMail(mailOptions, (error) => {
|
||||
if (error) {
|
||||
Logger.error('Failed send welcome mail', { error, form });
|
||||
done(error);
|
||||
return;
|
||||
}
|
||||
Logger.info('User has been sent welcome email successfuly.', { form });
|
||||
done();
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
Mail.sendMail(mailOptions, (error) => {
|
||||
if (error) {
|
||||
resolve(error);
|
||||
return;
|
||||
}
|
||||
reject();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
sendResetPasswordMessage() {
|
||||
/**
|
||||
* Sends reset password message.
|
||||
*
|
||||
* @param {ISystemUser} user - The system user.
|
||||
* @param {string} token - Reset password token.
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
sendResetPasswordMessage(user: ISystemUser, token: string): Promise<void> {
|
||||
const Mail = Container.get('mail');
|
||||
|
||||
const filePath = path.join(global.rootPath, 'views/mail/ResetPassword.html');
|
||||
const template = fs.readFileSync(filePath, 'utf8');
|
||||
const rendered = Mustache.render(template, {
|
||||
resetPasswordUrl: `${config.baseURL}/reset/${token}`,
|
||||
first_name: user.firstName,
|
||||
last_name: user.lastName,
|
||||
contact_us_email: config.contactUsMail,
|
||||
});
|
||||
const mailOptions = {
|
||||
to: user.email,
|
||||
from: `${process.env.MAIL_FROM_NAME} ${process.env.MAIL_FROM_ADDRESS}`,
|
||||
subject: 'Bigcapital - Password Reset',
|
||||
html: rendered,
|
||||
};
|
||||
return new Promise((resolve, reject) => {
|
||||
Mail.sendMail(mailOptions, (error) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -65,7 +65,7 @@ export default class AuthenticationService {
|
||||
|
||||
this.logger.info('[login] check password validation.');
|
||||
if (!user.verifyPassword(password)) {
|
||||
throw new ServiceError('password_invalid');
|
||||
throw new ServiceError('invalid_password');
|
||||
}
|
||||
|
||||
if (!user.active) {
|
||||
@@ -133,8 +133,10 @@ export default class AuthenticationService {
|
||||
tenant_id: tenant.id,
|
||||
});
|
||||
|
||||
this.eventDispatcher.dispatch(events.auth.register, { registerDTO });
|
||||
|
||||
// Triggers `onRegister` event.
|
||||
this.eventDispatcher.dispatch(events.auth.register, {
|
||||
registerDTO, user: registeredUser
|
||||
});
|
||||
return registeredUser;
|
||||
}
|
||||
|
||||
@@ -194,9 +196,12 @@ export default class AuthenticationService {
|
||||
await this.validateEmailExistance(email);
|
||||
|
||||
// Delete all stored tokens of reset password that associate to the give email.
|
||||
this.logger.info('[send_reset_password] trying to delete all tokens by email.');
|
||||
await PasswordReset.query().where('email', email).delete();
|
||||
|
||||
const token = uniqid();
|
||||
|
||||
this.logger.info('[send_reset_password] insert the generated token.');
|
||||
const passwordReset = await PasswordReset.query().insert({ email, token });
|
||||
const user = await SystemUser.query().findOne('email', email);
|
||||
|
||||
|
||||
@@ -2,7 +2,11 @@ import { Service } from "typedi";
|
||||
|
||||
@Service()
|
||||
export default class SubscriptionMailMessages {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param phoneNumber
|
||||
* @param remainingDays
|
||||
*/
|
||||
public async sendRemainingSubscriptionPeriod(phoneNumber: string, remainingDays: number) {
|
||||
const message: string = `
|
||||
Your remaining subscription is ${remainingDays} days,
|
||||
@@ -11,6 +15,11 @@ export default class SubscriptionMailMessages {
|
||||
this.smsClient.sendMessage(phoneNumber, message);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param phoneNumber
|
||||
* @param remainingDays
|
||||
*/
|
||||
public async sendRemainingTrialPeriod(phoneNumber: string, remainingDays: number) {
|
||||
const message: string = `
|
||||
Your remaining free trial is ${remainingDays} days,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { Inject } from 'typedi';
|
||||
import { Tenant, Plan } from '@/system/models';
|
||||
import { IPaymentContext } from '@/interfaces';
|
||||
import { NotAllowedChangeSubscriptionPlan } from '@/exceptions';
|
||||
@@ -5,6 +6,9 @@ import { NotAllowedChangeSubscriptionPlan } from '@/exceptions';
|
||||
export default class Subscription<PaymentModel> {
|
||||
paymentContext: IPaymentContext|null;
|
||||
|
||||
@Inject('logger')
|
||||
logger: any;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param {IPaymentContext}
|
||||
|
||||
Reference in New Issue
Block a user