- feat: remove unnecessary migrations, controllers and models files.

- feat: metable store
- feat: metable store with settings store.
- feat: settings middleware to auto-save and load.
- feat: DI db manager to master container.
- feat: write some logs to sale invoices.
This commit is contained in:
Ahmed Bouhuolia
2020-09-03 16:51:48 +02:00
parent abefba22ee
commit 9ee7ed89ec
98 changed files with 1697 additions and 2052 deletions

View File

@@ -0,0 +1,35 @@
import { Service } from "typedi";
@Service()
export default class AuthenticationMailMesssages {
sendWelcomeMessage() {
const Logger = Container.get('logger');
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,
});
const mailOptions = {
to: 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();
});
}
sendResetPasswordMessage() {
}
}

View File

@@ -0,0 +1,13 @@
import { Service } from "typedi";
@Service()
export default class AuthenticationSMSMessages {
smsClient: any;
sendWelcomeMessage() {
const message: string = `Hi ${firstName}, Welcome to Bigcapital, You've joined the new workspace,
if you need any help please don't hesitate to contact us.`
}
}

View File

@@ -3,8 +3,8 @@ import JWT from 'jsonwebtoken';
import uniqid from 'uniqid';
import { omit } from 'lodash';
import {
EventDispatcher
EventDispatcherInterface
EventDispatcher,
EventDispatcherInterface,
} from '@/decorators/eventDispatcher';
import {
SystemUser,
@@ -22,6 +22,8 @@ import { hashPassword } from '@/utils';
import { ServiceError, ServiceErrors } from "@/exceptions";
import config from '@/../config/config';
import events from '@/subscribers/events';
import AuthenticationMailMessages from '@/services/Authentication/AuthenticationMailMessages';
import AuthenticationSMSMessages from '@/services/Authentication/AuthenticationSMSMessages';
@Service()
export default class AuthenticationService {
@@ -34,6 +36,12 @@ export default class AuthenticationService {
@EventDispatcher()
eventDispatcher: EventDispatcherInterface;
@Inject()
smsMessages: AuthenticationSMSMessages;
@Inject()
mailMessages: AuthenticationMailMessages;
/**
* Signin and generates JWT token.
* @throws {ServiceError}
@@ -70,6 +78,7 @@ export default class AuthenticationService {
this.logger.info('[login] Logging success.', { user, token });
// Triggers `onLogin` event.
this.eventDispatcher.dispatch(events.auth.login, {
emailOrPhone, password,
});
@@ -191,6 +200,7 @@ export default class AuthenticationService {
const passwordReset = await PasswordReset.query().insert({ email, token });
const user = await SystemUser.query().findOne('email', email);
// Triggers `onSendResetPassword` event.
this.eventDispatcher.dispatch(events.auth.sendResetPassword, { user, token });
return passwordReset;
@@ -225,25 +235,26 @@ export default class AuthenticationService {
// Delete the reset password token.
await PasswordReset.query().where('email', user.email).delete();
this.eventDispatcher.dispatch(events.auth.sendResetPassword, { user, token, password });
// Triggers `onResetPassword` event.
this.eventDispatcher.dispatch(events.auth.resetPassword, { user, token, password });
this.logger.info('[reset_password] reset password success.');
}
/**
* Generates JWT token for the given user.
* @param {IUser} user
* @param {ISystemUser} user
* @return {string} token
*/
generateToken(user: IUser): string {
generateToken(user: ISystemUser): string {
const today = new Date();
const exp = new Date(today);
exp.setDate(today.getDate() + 60);
this.logger.silly(`Sign JWT for userId: ${user._id}`);
this.logger.silly(`Sign JWT for userId: ${user.id}`);
return JWT.sign(
{
_id: user._id, // We are gonna use this in the middleware 'isAuth'
id: user.id, // We are gonna use this in the middleware 'isAuth'
exp: exp.getTime() / 1000,
},
config.jwtSecret,