mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
fix: issues in organization build.
This commit is contained in:
@@ -2,8 +2,13 @@ import { Container, Inject } from 'typedi';
|
||||
import AuthenticationService from '@/services/Authentication';
|
||||
|
||||
export default class WelcomeSMSJob {
|
||||
@Inject()
|
||||
authService: AuthenticationService;
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param {Agenda} agenda
|
||||
*/
|
||||
constructor(agenda) {
|
||||
agenda.define('welcome-sms', { priority: 'high' }, this.handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle send welcome mail job.
|
||||
@@ -11,17 +16,19 @@ export default class WelcomeSMSJob {
|
||||
* @param {Function} done
|
||||
*/
|
||||
public async handler(job, done: Function): Promise<void> {
|
||||
const { email, organizationName, firstName } = job.attrs.data;
|
||||
const Logger = Container.get('logger');
|
||||
const { tenant, user } = job.attrs.data;
|
||||
|
||||
const Logger = Container.get('logger');
|
||||
const authService = Container.get(AuthenticationService);
|
||||
|
||||
Logger.info(`[welcome_sms] started: ${job.attrs.data}`);
|
||||
|
||||
Logger.info(`Send welcome SMS message - started: ${job.attrs.data}`);
|
||||
|
||||
try {
|
||||
await this.authService.smsMessages.sendWelcomeMessage();
|
||||
Logger.info(`Send welcome SMS message - finished: ${job.attrs.data}`);
|
||||
done()
|
||||
await authService.smsMessages.sendWelcomeMessage(tenant, user);
|
||||
Logger.info(`[welcome_sms] finished`, { tenant, user });
|
||||
done();
|
||||
} catch (error) {
|
||||
Logger.info(`Send welcome SMS message - error: ${job.attrs.data}, error: ${error}`);
|
||||
Logger.info(`[welcome_sms] error`, { error, tenant, user });
|
||||
done(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,9 +11,6 @@ export default ({ mongoConnection, knex }) => {
|
||||
const smsClientInstance = SmsClientLoader();
|
||||
const dbManager = dbManagerFactory();
|
||||
|
||||
Container.set('agenda', agendaInstance);
|
||||
LoggerInstance.info('Agenda has been injected into container');
|
||||
|
||||
Container.set('logger', LoggerInstance)
|
||||
LoggerInstance.info('Logger instance has been injected into container');
|
||||
|
||||
@@ -29,6 +26,9 @@ export default ({ mongoConnection, knex }) => {
|
||||
Container.set('dbManager', dbManager);
|
||||
LoggerInstance.info('Database manager has been injected into container.');
|
||||
|
||||
Container.set('agenda', agendaInstance);
|
||||
LoggerInstance.info('Agenda has been injected into container');
|
||||
|
||||
return { agenda: agendaInstance };
|
||||
} catch (e) {
|
||||
LoggerInstance.error('Error on dependency injector loader: %o', e);
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
// Here we import all events.
|
||||
import '@/subscribers/authentication';
|
||||
|
||||
import '@/subscribers/organization';
|
||||
|
||||
@@ -15,12 +15,7 @@ import UserInviteMailJob from '@/jobs/UserInviteMail';
|
||||
export default ({ agenda }: { agenda: Agenda }) => {
|
||||
new WelcomeEmailJob(agenda);
|
||||
new ResetPasswordMailJob(agenda);
|
||||
|
||||
agenda.define(
|
||||
'welcome-sms',
|
||||
{ priority: 'high' },
|
||||
new WelcomeSMSJob().handler
|
||||
);
|
||||
new WelcomeSMSJob(agenda);
|
||||
|
||||
// User invite mail.
|
||||
agenda.define(
|
||||
|
||||
@@ -4,7 +4,6 @@ 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 {
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
import { Service } from "typedi";
|
||||
import { Service, Inject } from "typedi";
|
||||
import { ISystemUser, ITenant } from "@/interfaces";
|
||||
|
||||
@Service()
|
||||
export default class AuthenticationSMSMessages {
|
||||
@Inject('SMSClient')
|
||||
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.`
|
||||
/**
|
||||
* Sends welcome sms message.
|
||||
* @param {ITenant} tenant
|
||||
* @param {ISystemUser} user
|
||||
*/
|
||||
sendWelcomeMessage(tenant: ITenant, user: ISystemUser) {
|
||||
const message: string = `Hi ${user.firstName}, Welcome to Bigcapital, You've joined the new workspace, if you need any help please don't hesitate to contact us.`
|
||||
|
||||
|
||||
return this.smsClient.sendMessage(user.phoneNumber, message);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,19 @@
|
||||
import { Service, Inject, Container } from 'typedi';
|
||||
import { Tenant } from '@/system/models';
|
||||
import { Tenant, SystemUser } from '@/system/models';
|
||||
import TenantsManager from '@/system/TenantsManager';
|
||||
import { ServiceError } from '@/exceptions';
|
||||
import { ITenant } from '@/interfaces';
|
||||
import {
|
||||
EventDispatcher,
|
||||
EventDispatcherInterface,
|
||||
} from '@/decorators/eventDispatcher';
|
||||
import events from '@/subscribers/events';
|
||||
|
||||
@Service()
|
||||
export default class OrganizationService {
|
||||
@EventDispatcher()
|
||||
eventDispatcher: EventDispatcherInterface;
|
||||
|
||||
@Inject()
|
||||
tenantsManager: TenantsManager;
|
||||
|
||||
@@ -35,8 +43,18 @@ export default class OrganizationService {
|
||||
|
||||
this.logger.info('[tenant_db_build] mark tenant as initialized.', { tenant });
|
||||
await tenant.$query().update({ initialized: true });
|
||||
|
||||
// Retrieve the tenant system user.
|
||||
const user = await SystemUser.query().findOne('tenant_id', tenant.id);
|
||||
|
||||
// Throws `onOrganizationBuild` event.
|
||||
this.eventDispatcher.dispatch(events.organization.build, { tenant, user });
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws error in case the given tenant is undefined.
|
||||
* @param {ITenant} tenant
|
||||
*/
|
||||
private throwIfTenantNotExists(tenant: ITenant) {
|
||||
if (!tenant) {
|
||||
this.logger.info('[tenant_db_build] organization id not found.');
|
||||
@@ -44,13 +62,13 @@ export default class OrganizationService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws error in case the given tenant is already initialized.
|
||||
* @param {ITenant} tenant
|
||||
*/
|
||||
private throwIfTenantInitizalized(tenant: ITenant) {
|
||||
if (tenant.initialized) {
|
||||
throw new ServiceError('tenant_initialized');
|
||||
}
|
||||
}
|
||||
|
||||
destroy() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -11,15 +11,14 @@ export default class EasySMSClient implements SMSClientInterface {
|
||||
* @param {string} message
|
||||
*/
|
||||
send(to: string, message: string) {
|
||||
console.log(config);
|
||||
const API_KEY = config.easySMSGateway.api_key;
|
||||
const params = `action=send-sms&api_key=${API_KEY}=&to=${to}&sms=${message}&unicode=1`;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
axios.get(`https://easysms.devs.ly/sms/api?${params}`)
|
||||
.then((response) => {
|
||||
if (response.code === 'ok') { resolve(); }
|
||||
else { reject(); }
|
||||
if (response.data.code === 'ok') { resolve(); }
|
||||
else { reject(response.data); }
|
||||
})
|
||||
.catch((error) => { reject(error) });
|
||||
});
|
||||
|
||||
@@ -12,5 +12,9 @@ export default {
|
||||
acceptInvite: 'onUserAcceptInvite',
|
||||
sendInvite: 'onUserSendInvite',
|
||||
checkInvite: 'onUserCheckInvite'
|
||||
},
|
||||
|
||||
organization: {
|
||||
build: 'onOrganizationBuild',
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,10 @@ import events from '@/subscribers/events';
|
||||
export class OrganizationSubscriber {
|
||||
|
||||
@On(events.organization.build)
|
||||
public onBuild(payload) {
|
||||
public async onBuild(payload) {
|
||||
const { tenant, user } = payload;
|
||||
const agenda = Container.get('agenda');
|
||||
|
||||
agenda.now('welcome-sms', {
|
||||
email, organizationName, firstName,
|
||||
});
|
||||
await agenda.now('welcome-sms', { tenant, user });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user