mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 13:50:31 +00:00
add server to monorepo.
This commit is contained in:
73
packages/server/src/jobs/ComputeItemCost.ts
Normal file
73
packages/server/src/jobs/ComputeItemCost.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { Container } from 'typedi';
|
||||
import events from '@/subscribers/events';
|
||||
import InventoryService from '@/services/Inventory/Inventory';
|
||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||
import {
|
||||
IComputeItemCostJobCompletedPayload,
|
||||
IComputeItemCostJobStartedPayload,
|
||||
} from '@/interfaces';
|
||||
|
||||
export default class ComputeItemCostJob {
|
||||
agenda: any;
|
||||
eventPublisher: EventPublisher;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param agenda
|
||||
*/
|
||||
constructor(agenda) {
|
||||
this.agenda = agenda;
|
||||
this.eventPublisher = Container.get(EventPublisher);
|
||||
|
||||
agenda.define(
|
||||
'compute-item-cost',
|
||||
{ priority: 'high', concurrency: 1 },
|
||||
this.handler.bind(this)
|
||||
);
|
||||
this.agenda.on('start:compute-item-cost', this.onJobStart.bind(this));
|
||||
this.agenda.on(
|
||||
'complete:compute-item-cost',
|
||||
this.onJobCompleted.bind(this)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The job handler.
|
||||
*/
|
||||
public async handler(job, done: Function): Promise<void> {
|
||||
const { startingDate, itemId, tenantId } = job.attrs.data;
|
||||
const inventoryService = Container.get(InventoryService);
|
||||
|
||||
try {
|
||||
await inventoryService.computeItemCost(tenantId, startingDate, itemId);
|
||||
done();
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the job started.
|
||||
*/
|
||||
async onJobStart(job) {
|
||||
const { startingDate, itemId, tenantId } = job.attrs.data;
|
||||
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.inventory.onComputeItemCostJobStarted,
|
||||
{ startingDate, itemId, tenantId } as IComputeItemCostJobStartedPayload
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle job complete items cost finished.
|
||||
* @param {Job} job -
|
||||
*/
|
||||
async onJobCompleted(job) {
|
||||
const { startingDate, itemId, tenantId } = job.attrs.data;
|
||||
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.inventory.onComputeItemCostJobCompleted,
|
||||
{ startingDate, itemId, tenantId } as IComputeItemCostJobCompletedPayload
|
||||
);
|
||||
}
|
||||
}
|
||||
34
packages/server/src/jobs/MailNotificationSubscribeEnd.ts
Normal file
34
packages/server/src/jobs/MailNotificationSubscribeEnd.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import Container from 'typedi';
|
||||
import SubscriptionService from '@/services/Subscription/Subscription';
|
||||
|
||||
export default class MailNotificationSubscribeEnd {
|
||||
/**
|
||||
* Job handler.
|
||||
* @param {Job} job -
|
||||
*/
|
||||
handler(job) {
|
||||
const { tenantId, phoneNumber, remainingDays } = job.attrs.data;
|
||||
|
||||
const subscriptionService = Container.get(SubscriptionService);
|
||||
const Logger = Container.get('logger');
|
||||
|
||||
Logger.info(
|
||||
`Send mail notification subscription end soon - started: ${job.attrs.data}`
|
||||
);
|
||||
|
||||
try {
|
||||
subscriptionService.mailMessages.sendRemainingTrialPeriod(
|
||||
phoneNumber,
|
||||
remainingDays
|
||||
);
|
||||
Logger.info(
|
||||
`Send mail notification subscription end soon - finished: ${job.attrs.data}`
|
||||
);
|
||||
} catch (error) {
|
||||
Logger.info(
|
||||
`Send mail notification subscription end soon - failed: ${job.attrs.data}, error: ${e}`
|
||||
);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
34
packages/server/src/jobs/MailNotificationTrialEnd.ts
Normal file
34
packages/server/src/jobs/MailNotificationTrialEnd.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import Container from 'typedi';
|
||||
import SubscriptionService from '@/services/Subscription/Subscription';
|
||||
|
||||
export default class MailNotificationTrialEnd {
|
||||
/**
|
||||
*
|
||||
* @param {Job} job -
|
||||
*/
|
||||
handler(job) {
|
||||
const { tenantId, phoneNumber, remainingDays } = job.attrs.data;
|
||||
|
||||
const subscriptionService = Container.get(SubscriptionService);
|
||||
const Logger = Container.get('logger');
|
||||
|
||||
Logger.info(
|
||||
`Send mail notification subscription end soon - started: ${job.attrs.data}`
|
||||
);
|
||||
|
||||
try {
|
||||
subscriptionService.mailMessages.sendRemainingTrialPeriod(
|
||||
phoneNumber,
|
||||
remainingDays
|
||||
);
|
||||
Logger.info(
|
||||
`Send mail notification subscription end soon - finished: ${job.attrs.data}`
|
||||
);
|
||||
} catch (error) {
|
||||
Logger.info(
|
||||
`Send mail notification subscription end soon - failed: ${job.attrs.data}, error: ${e}`
|
||||
);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
34
packages/server/src/jobs/OrganizationSetup.ts
Normal file
34
packages/server/src/jobs/OrganizationSetup.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { Container } from 'typedi';
|
||||
import OrganizationService from '@/services/Organization/OrganizationService';
|
||||
|
||||
export default class OrganizationSetupJob {
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
constructor(agenda) {
|
||||
agenda.define(
|
||||
'organization-setup',
|
||||
{ priority: 'high', concurrency: 1 },
|
||||
this.handler
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle job action.
|
||||
*/
|
||||
async handler(job, done: Function): Promise<void> {
|
||||
const { tenantId, buildDTO, authorizedUser, _id } = job.attrs.data;
|
||||
const organizationService = Container.get(OrganizationService);
|
||||
|
||||
try {
|
||||
await organizationService.build(tenantId, buildDTO, authorizedUser);
|
||||
done();
|
||||
} catch (e) {
|
||||
// Unlock build status of the tenant.
|
||||
await organizationService.revertBuildRunJob(tenantId, _id);
|
||||
|
||||
console.error(e);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
31
packages/server/src/jobs/OrganizationUpgrade.ts
Normal file
31
packages/server/src/jobs/OrganizationUpgrade.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Container } from 'typedi';
|
||||
import OrganizationUpgrade from '@/services/Organization/OrganizationUpgrade';
|
||||
|
||||
export default class OrganizationUpgradeJob {
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
constructor(agenda) {
|
||||
agenda.define(
|
||||
'organization-upgrade',
|
||||
{ priority: 'high', concurrency: 1 },
|
||||
this.handler
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle job action.
|
||||
*/
|
||||
async handler(job, done: Function): Promise<void> {
|
||||
const { tenantId, _id } = job.attrs.data;
|
||||
const organizationUpgrade = Container.get(OrganizationUpgrade);
|
||||
|
||||
try {
|
||||
await organizationUpgrade.upgradeJob(tenantId);
|
||||
done();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
39
packages/server/src/jobs/ResetPasswordMail.ts
Normal file
39
packages/server/src/jobs/ResetPasswordMail.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { Container, Inject } from 'typedi';
|
||||
import AuthenticationService from '@/services/Authentication';
|
||||
|
||||
export default class WelcomeEmailJob {
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param {Agenda} agenda
|
||||
*/
|
||||
constructor(agenda) {
|
||||
agenda.define(
|
||||
'reset-password-mail',
|
||||
{ priority: 'high' },
|
||||
this.handler.bind(this),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle send welcome mail job.
|
||||
* @param {Job} job
|
||||
* @param {Function} done
|
||||
*/
|
||||
public async handler(job, done: Function): Promise<void> {
|
||||
const { data } = job.attrs;
|
||||
const { user, token } = data;
|
||||
const Logger = Container.get('logger');
|
||||
const authService = Container.get(AuthenticationService);
|
||||
|
||||
Logger.info(`[send_reset_password] started.`, { data });
|
||||
|
||||
try {
|
||||
await authService.mailMessages.sendResetPasswordMessage(user, token);
|
||||
Logger.info(`[send_reset_password] finished.`, { data });
|
||||
done()
|
||||
} catch (error) {
|
||||
Logger.error(`[send_reset_password] error.`, { data, error });
|
||||
done(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
28
packages/server/src/jobs/SMSNotificationSubscribeEnd.ts
Normal file
28
packages/server/src/jobs/SMSNotificationSubscribeEnd.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import Container from 'typedi';
|
||||
import SubscriptionService from '@/services/Subscription/Subscription';
|
||||
|
||||
export default class SMSNotificationSubscribeEnd {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Job}job
|
||||
*/
|
||||
handler(job) {
|
||||
const { tenantId, phoneNumber, remainingDays } = job.attrs.data;
|
||||
|
||||
const subscriptionService = Container.get(SubscriptionService);
|
||||
const Logger = Container.get('logger');
|
||||
|
||||
Logger.info(`Send SMS notification subscription end soon - started: ${job.attrs.data}`);
|
||||
|
||||
try {
|
||||
subscriptionService.smsMessages.sendRemainingSubscriptionPeriod(
|
||||
phoneNumber, remainingDays,
|
||||
);
|
||||
Logger.info(`Send SMS notification subscription end soon - finished: ${job.attrs.data}`);
|
||||
} catch(error) {
|
||||
Logger.info(`Send SMS notification subscription end soon - failed: ${job.attrs.data}, error: ${e}`);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
28
packages/server/src/jobs/SMSNotificationTrialEnd.ts
Normal file
28
packages/server/src/jobs/SMSNotificationTrialEnd.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import Container from 'typedi';
|
||||
import SubscriptionService from '@/services/Subscription/Subscription';
|
||||
|
||||
export default class SMSNotificationTrialEnd {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Job}job
|
||||
*/
|
||||
handler(job) {
|
||||
const { tenantId, phoneNumber, remainingDays } = job.attrs.data;
|
||||
|
||||
const subscriptionService = Container.get(SubscriptionService);
|
||||
const Logger = Container.get('logger');
|
||||
|
||||
Logger.info(`Send notification subscription end soon - started: ${job.attrs.data}`);
|
||||
|
||||
try {
|
||||
subscriptionService.smsMessages.sendRemainingTrialPeriod(
|
||||
phoneNumber, remainingDays,
|
||||
);
|
||||
Logger.info(`Send notification subscription end soon - finished: ${job.attrs.data}`);
|
||||
} catch(error) {
|
||||
Logger.info(`Send notification subscription end soon - failed: ${job.attrs.data}, error: ${e}`);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
packages/server/src/jobs/SendLicenseEmail.ts
Normal file
33
packages/server/src/jobs/SendLicenseEmail.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Container } from 'typedi';
|
||||
import LicenseService from '@/services/Payment/License';
|
||||
|
||||
export default class SendLicenseViaEmailJob {
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param agenda
|
||||
*/
|
||||
constructor(agenda) {
|
||||
agenda.define(
|
||||
'send-license-via-email',
|
||||
{ priority: 'high', concurrency: 1, },
|
||||
this.handler,
|
||||
);
|
||||
}
|
||||
|
||||
public async handler(job, done: Function): Promise<void> {
|
||||
const Logger = Container.get('logger');
|
||||
const licenseService = Container.get(LicenseService);
|
||||
const { email, licenseCode } = job.attrs.data;
|
||||
|
||||
Logger.info(`[send_license_via_mail] started: ${job.attrs.data}`);
|
||||
|
||||
try {
|
||||
await licenseService.mailMessages.sendMailLicense(licenseCode, email);
|
||||
Logger.info(`[send_license_via_mail] completed: ${job.attrs.data}`);
|
||||
done();
|
||||
} catch(e) {
|
||||
Logger.error(`[send_license_via_mail] ${job.attrs.data}, error: ${e}`);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
packages/server/src/jobs/SendLicensePhone.ts
Normal file
33
packages/server/src/jobs/SendLicensePhone.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Container } from 'typedi';
|
||||
import LicenseService from '@/services/Payment/License';
|
||||
|
||||
export default class SendLicenseViaPhoneJob {
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
constructor(agenda) {
|
||||
agenda.define(
|
||||
'send-license-via-phone',
|
||||
{ priority: 'high', concurrency: 1, },
|
||||
this.handler,
|
||||
);
|
||||
}
|
||||
|
||||
public async handler(job, done: Function): Promise<void> {
|
||||
const { phoneNumber, licenseCode } = job.attrs.data;
|
||||
|
||||
const Logger = Container.get('logger');
|
||||
const licenseService = Container.get(LicenseService);
|
||||
|
||||
Logger.debug(`Send license via phone number - started: ${job.attrs.data}`);
|
||||
|
||||
try {
|
||||
await licenseService.smsMessages.sendLicenseSMSMessage(phoneNumber, licenseCode);
|
||||
Logger.debug(`Send license via phone number - completed: ${job.attrs.data}`);
|
||||
done();
|
||||
} catch(e) {
|
||||
Logger.error(`Send license via phone number: ${job.attrs.data}, error: ${e}`);
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
22
packages/server/src/jobs/SmsNotification.ts
Normal file
22
packages/server/src/jobs/SmsNotification.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Container } from 'typedi';
|
||||
|
||||
export default class SmsNotification {
|
||||
constructor(agenda) {
|
||||
agenda.define('sms-notification', { priority: 'high' }, this.handler);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Job}job
|
||||
*/
|
||||
async handler(job) {
|
||||
const { message, to } = job.attrs.data;
|
||||
const smsClient = Container.get('SMSClient');
|
||||
|
||||
try {
|
||||
await smsClient.sendMessage(to, message);
|
||||
} catch (error) {
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
45
packages/server/src/jobs/UserInviteMail.ts
Normal file
45
packages/server/src/jobs/UserInviteMail.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Container, Inject } from 'typedi';
|
||||
import InviteUserService from '@/services/InviteUsers/AcceptInviteUser';
|
||||
|
||||
export default class UserInviteMailJob {
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param {Agenda} agenda
|
||||
*/
|
||||
constructor(agenda) {
|
||||
agenda.define(
|
||||
'user-invite-mail',
|
||||
{ priority: 'high' },
|
||||
this.handler.bind(this)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle invite user job.
|
||||
* @param {Job} job
|
||||
* @param {Function} done
|
||||
*/
|
||||
public async handler(job, done: Function): Promise<void> {
|
||||
const { invite, authorizedUser, tenantId } = job.attrs.data;
|
||||
|
||||
const Logger = Container.get('logger');
|
||||
const inviteUsersService = Container.get(InviteUserService);
|
||||
|
||||
Logger.info(`Send invite user mail - started: ${job.attrs.data}`);
|
||||
|
||||
try {
|
||||
await inviteUsersService.mailMessages.sendInviteMail(
|
||||
tenantId,
|
||||
authorizedUser,
|
||||
invite
|
||||
);
|
||||
Logger.info(`Send invite user mail - finished: ${job.attrs.data}`);
|
||||
done();
|
||||
} catch (error) {
|
||||
Logger.info(
|
||||
`Send invite user mail - error: ${job.attrs.data}, error: ${error}`
|
||||
);
|
||||
done(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
35
packages/server/src/jobs/WelcomeSMS.ts
Normal file
35
packages/server/src/jobs/WelcomeSMS.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { Container, Inject } from 'typedi';
|
||||
import AuthenticationService from '@/services/Authentication';
|
||||
|
||||
export default class WelcomeSMSJob {
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param {Agenda} agenda
|
||||
*/
|
||||
constructor(agenda) {
|
||||
agenda.define('welcome-sms', { priority: 'high' }, this.handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle send welcome mail job.
|
||||
* @param {Job} job
|
||||
* @param {Function} done
|
||||
*/
|
||||
public async handler(job, done: Function): Promise<void> {
|
||||
const { tenant, user } = job.attrs.data;
|
||||
|
||||
const Logger = Container.get('logger');
|
||||
const authService = Container.get(AuthenticationService);
|
||||
|
||||
Logger.info(`[welcome_sms] started: ${job.attrs.data}`);
|
||||
|
||||
try {
|
||||
await authService.smsMessages.sendWelcomeMessage(tenant, user);
|
||||
Logger.info(`[welcome_sms] finished`, { tenant, user });
|
||||
done();
|
||||
} catch (error) {
|
||||
Logger.info(`[welcome_sms] error`, { error, tenant, user });
|
||||
done(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
39
packages/server/src/jobs/welcomeEmail.ts
Normal file
39
packages/server/src/jobs/welcomeEmail.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { Container } from 'typedi';
|
||||
import AuthenticationService from '@/services/Authentication';
|
||||
|
||||
export default class WelcomeEmailJob {
|
||||
/**
|
||||
* Constructor method.
|
||||
* @param {Agenda} agenda -
|
||||
*/
|
||||
constructor(agenda) {
|
||||
// Welcome mail and SMS message.
|
||||
agenda.define(
|
||||
'welcome-email',
|
||||
{ priority: 'high' },
|
||||
this.handler.bind(this),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle send welcome mail job.
|
||||
* @param {Job} job
|
||||
* @param {Function} done
|
||||
*/
|
||||
public async handler(job, done: Function): Promise<void> {
|
||||
const { organizationId, user } = job.attrs.data;
|
||||
const Logger: any = Container.get('logger');
|
||||
const authService = Container.get(AuthenticationService);
|
||||
|
||||
Logger.info(`[welcome_mail] started: ${job.attrs.data}`);
|
||||
|
||||
try {
|
||||
await authService.mailMessages.sendWelcomeMessage(user, organizationId);
|
||||
Logger.info(`[welcome_mail] finished: ${job.attrs.data}`);
|
||||
done();
|
||||
} catch (error) {
|
||||
Logger.error(`[welcome_mail] error: ${job.attrs.data}, error: ${error}`);
|
||||
done(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
50
packages/server/src/jobs/writeInvoicesJEntries.ts
Normal file
50
packages/server/src/jobs/writeInvoicesJEntries.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { Container } from 'typedi';
|
||||
import events from '@/subscribers/events';
|
||||
import SalesInvoicesCost from '@/services/Sales/SalesInvoicesCost';
|
||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||
|
||||
export default class WriteInvoicesJournalEntries {
|
||||
eventPublisher: EventPublisher;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
constructor(agenda) {
|
||||
const eventName = 'rewrite-invoices-journal-entries';
|
||||
this.eventPublisher = Container.get(EventPublisher);
|
||||
|
||||
agenda.define(
|
||||
eventName,
|
||||
{ priority: 'normal', concurrency: 1 },
|
||||
this.handler.bind(this)
|
||||
);
|
||||
agenda.on(`complete:${eventName}`, this.onJobCompleted.bind(this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the job execuation.
|
||||
*/
|
||||
public async handler(job, done: Function): Promise<void> {
|
||||
const { startingDate, tenantId } = job.attrs.data;
|
||||
const salesInvoicesCost = Container.get(SalesInvoicesCost);
|
||||
|
||||
try {
|
||||
await salesInvoicesCost.writeCostLotsGLEntries(tenantId, startingDate);
|
||||
done();
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the job complete.
|
||||
*/
|
||||
async onJobCompleted(job) {
|
||||
const { startingDate, itemId, tenantId } = job.attrs.data;
|
||||
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.inventory.onInventoryCostEntriesWritten,
|
||||
{ startingDate, itemId, tenantId }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user