mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
feat: auto subscribe to free plan once signup on community version.
This commit is contained in:
@@ -96,9 +96,7 @@ export class LemonSqueezyWebhooks {
|
||||
if (webhookEvent === 'subscription_created') {
|
||||
await this.subscriptionService.newSubscribtion(
|
||||
tenantId,
|
||||
'pro-yearly',
|
||||
'year',
|
||||
1
|
||||
'early-adaptor',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,13 +15,17 @@ export class Subscription {
|
||||
public async newSubscribtion(
|
||||
tenantId: number,
|
||||
planSlug: string,
|
||||
invoiceInterval: string,
|
||||
invoicePeriod: number,
|
||||
subscriptionSlug: string = 'main'
|
||||
) {
|
||||
const tenant = await Tenant.query().findById(tenantId).throwIfNotFound();
|
||||
const plan = await Plan.query().findOne('slug', planSlug).throwIfNotFound();
|
||||
|
||||
const isFree = plan.price === 0;
|
||||
|
||||
// Take the invoice interval and period from the given plan.
|
||||
const invoiceInterval = plan.invoiceInternal;
|
||||
const invoicePeriod = isFree ? Infinity : plan.invoicePeriod;
|
||||
|
||||
const subscription = await tenant
|
||||
.$relatedQuery('subscriptions')
|
||||
.modify('subscriptionBySlug', subscriptionSlug)
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import moment from 'moment';
|
||||
import moment, { type unitOfTime } from 'moment';
|
||||
|
||||
export default class SubscriptionPeriod {
|
||||
start: Date;
|
||||
end: Date;
|
||||
interval: string;
|
||||
count: number;
|
||||
private start: Date;
|
||||
private end: Date;
|
||||
private interval: string;
|
||||
private count: number;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
@@ -12,7 +12,11 @@ export default class SubscriptionPeriod {
|
||||
* @param {number} count -
|
||||
* @param {Date} start -
|
||||
*/
|
||||
constructor(interval: string = 'month', count: number, start?: Date) {
|
||||
constructor(
|
||||
interval: unitOfTime.DurationConstructor = 'month',
|
||||
count: number,
|
||||
start?: Date
|
||||
) {
|
||||
this.interval = interval;
|
||||
this.count = count;
|
||||
this.start = start;
|
||||
@@ -20,7 +24,11 @@ export default class SubscriptionPeriod {
|
||||
if (!start) {
|
||||
this.start = moment().toDate();
|
||||
}
|
||||
this.end = moment(start).add(count, interval).toDate();
|
||||
if (count === Infinity) {
|
||||
this.end = null;
|
||||
} else {
|
||||
this.end = moment(start).add(count, interval).toDate();
|
||||
}
|
||||
}
|
||||
|
||||
getStartDate() {
|
||||
@@ -36,6 +44,6 @@ export default class SubscriptionPeriod {
|
||||
}
|
||||
|
||||
getIntervalCount() {
|
||||
return this.interval;
|
||||
return this.count;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { IAuthSignedUpEventPayload } from '@/interfaces';
|
||||
import events from '@/subscribers/events';
|
||||
import config from '@/config';
|
||||
import { Subscription } from '../Subscription';
|
||||
import { Inject, Service } from 'typedi';
|
||||
|
||||
@Service()
|
||||
export class SubscribeFreeOnSignupCommunity {
|
||||
@Inject()
|
||||
private subscriptionService: Subscription;
|
||||
|
||||
/**
|
||||
* Attaches events with handlers.
|
||||
*/
|
||||
public attach = (bus) => {
|
||||
bus.subscribe(
|
||||
events.auth.signUp,
|
||||
this.subscribeFreeOnSigupCommunity.bind(this)
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new free subscription once the user signup if the app is self-hosted.
|
||||
* @param {IAuthSignedUpEventPayload}
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
private async subscribeFreeOnSigupCommunity({
|
||||
signupDTO,
|
||||
tenant,
|
||||
user,
|
||||
}: IAuthSignedUpEventPayload) {
|
||||
if (config.hostedOnBigcapitalCloud) return null;
|
||||
|
||||
await this.subscriptionService.newSubscribtion(tenant.id, 'free');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user