mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 22:30:31 +00:00
feat: wip billing page
This commit is contained in:
@@ -6,6 +6,165 @@ export class GetSubscriptionsTransformer extends Transformer {
|
||||
* @returns {Array}
|
||||
*/
|
||||
public includeAttributes = (): string[] => {
|
||||
return [];
|
||||
return [
|
||||
'canceledAtFormatted',
|
||||
'cancelsAtFormatted',
|
||||
'trialStartsAtFormatted',
|
||||
'trialEndsAtFormatted',
|
||||
'statusFormatted',
|
||||
'planName',
|
||||
'planSlug',
|
||||
'planPrice',
|
||||
'planPriceCurrency',
|
||||
'planPriceFormatted',
|
||||
'planPeriod',
|
||||
'lemonUrls',
|
||||
];
|
||||
};
|
||||
|
||||
/**
|
||||
* Exclude attributes.
|
||||
* @returns {string[]}
|
||||
*/
|
||||
public excludeAttributes = (): string[] => {
|
||||
return ['id', 'plan'];
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the canceled at formatted.
|
||||
* @param subscription
|
||||
* @returns {string}
|
||||
*/
|
||||
public canceledAtFormatted = (subscription) => {
|
||||
return subscription.canceledAt
|
||||
? this.formatDate(subscription.canceledAt)
|
||||
: null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the cancels at formatted.
|
||||
* @param subscription
|
||||
* @returns {string}
|
||||
*/
|
||||
public cancelsAtFormatted = (subscription) => {
|
||||
return subscription.cancelsAt
|
||||
? this.formatDate(subscription.cancelsAt)
|
||||
: null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the trial starts at formatted date.
|
||||
* @returns {string}
|
||||
*/
|
||||
public trialStartsAtFormatted = (subscription) => {
|
||||
return subscription.trialStartsAt
|
||||
? this.formatDate(subscription.trialStartsAt)
|
||||
: null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the trial ends at formatted date.
|
||||
* @returns {string}
|
||||
*/
|
||||
public trialEndsAtFormatted = (subscription) => {
|
||||
return subscription.trialEndsAt
|
||||
? this.formatDate(subscription.trialEndsAt)
|
||||
: null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the Lemon subscription metadata.
|
||||
* @param subscription
|
||||
* @returns
|
||||
*/
|
||||
public lemonSubscription = (subscription) => {
|
||||
return (
|
||||
this.options.lemonSubscriptions[subscription.lemonSubscriptionId] || null
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the formatted subscription status.
|
||||
* @param subscription
|
||||
* @returns {string}
|
||||
*/
|
||||
public statusFormatted = (subscription) => {
|
||||
const pairs = {
|
||||
canceled: 'Canceled',
|
||||
active: 'Active',
|
||||
inactive: 'Inactive',
|
||||
expired: 'Expired',
|
||||
on_trial: 'On Trial',
|
||||
};
|
||||
return pairs[subscription.status] || '';
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the subscription plan name.
|
||||
* @param subscription
|
||||
* @returns {string}
|
||||
*/
|
||||
public planName(subscription) {
|
||||
return subscription.plan?.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the subscription plan slug.
|
||||
* @param subscription
|
||||
* @returns {string}
|
||||
*/
|
||||
public planSlug(subscription) {
|
||||
return subscription.plan?.slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the subscription plan price.
|
||||
* @param subscription
|
||||
* @returns {number}
|
||||
*/
|
||||
public planPrice(subscription) {
|
||||
return subscription.plan?.price;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the subscription plan price currency.
|
||||
* @param subscription
|
||||
* @returns {string}
|
||||
*/
|
||||
public planPriceCurrency(subscription) {
|
||||
return subscription.plan?.currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the subscription plan formatted price.
|
||||
* @param subscription
|
||||
* @returns {string}
|
||||
*/
|
||||
public planPriceFormatted(subscription) {
|
||||
return this.formatMoney(subscription.plan?.price, {
|
||||
currencyCode: subscription.plan?.currency,
|
||||
precision: 0
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the subscription plan period.
|
||||
* @param subscription
|
||||
* @returns {string}
|
||||
*/
|
||||
public planPeriod(subscription) {
|
||||
return subscription?.plan?.period;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the subscription Lemon Urls.
|
||||
* @param subscription
|
||||
* @returns
|
||||
*/
|
||||
public lemonUrls = (subscription) => {
|
||||
const lemonSusbcription = this.lemonSubscription(subscription);
|
||||
console.log(lemonSusbcription);
|
||||
|
||||
return lemonSusbcription?.data?.attributes?.urls;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
|
||||
import events from '@/subscribers/events';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { configureLemonSqueezy } from './utils';
|
||||
import { PlanSubscription } from '@/system/models';
|
||||
import { ServiceError } from '@/exceptions';
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { getSubscription } from '@lemonsqueezy/lemonsqueezy.js';
|
||||
import { PromisePool } from '@supercharge/promise-pool';
|
||||
import { PlanSubscription } from '@/system/models';
|
||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
||||
import { GetSubscriptionsTransformer } from './GetSubscriptionsTransformer';
|
||||
import { configureLemonSqueezy } from './utils';
|
||||
import { fromPairs } from 'lodash';
|
||||
|
||||
@Service()
|
||||
export default class SubscriptionService {
|
||||
@@ -13,14 +17,34 @@ export default class SubscriptionService {
|
||||
* @param {number} tenantId
|
||||
*/
|
||||
public async getSubscriptions(tenantId: number) {
|
||||
const subscriptions = await PlanSubscription.query().where(
|
||||
'tenant_id',
|
||||
tenantId
|
||||
configureLemonSqueezy();
|
||||
|
||||
const subscriptions = await PlanSubscription.query()
|
||||
.where('tenant_id', tenantId)
|
||||
.withGraphFetched('plan');
|
||||
|
||||
const lemonSubscriptionsResult = await PromisePool.withConcurrency(1)
|
||||
.for(subscriptions)
|
||||
.process(async (subscription, index, pool) => {
|
||||
if (subscription.lemonSubscriptionId) {
|
||||
const res = await getSubscription(subscription.lemonSubscriptionId);
|
||||
|
||||
if (res.error) {
|
||||
return;
|
||||
}
|
||||
return [subscription.lemonSubscriptionId, res.data];
|
||||
}
|
||||
});
|
||||
const lemonSubscriptions = fromPairs(
|
||||
lemonSubscriptionsResult?.results.filter((result) => !!result[1])
|
||||
);
|
||||
return this.transformer.transform(
|
||||
tenantId,
|
||||
subscriptions,
|
||||
new GetSubscriptionsTransformer()
|
||||
new GetSubscriptionsTransformer(),
|
||||
{
|
||||
lemonSubscriptions,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
exports.up = function (knex) {
|
||||
return knex.schema.table('subscription_plan_subscriptions', (table) => {
|
||||
table.dateTime('trial_starts_at').nullable();
|
||||
table.dateTime('trial_ends_at').nullable();
|
||||
});
|
||||
};
|
||||
|
||||
exports.down = function (knex) {
|
||||
return knex.schema.table('subscription_plan_subscriptions', (table) => {
|
||||
table.dropColumn('trial_starts_at').nullable();
|
||||
table.dropColumn('trial_ends_at').nullable();
|
||||
});
|
||||
};
|
||||
@@ -5,7 +5,16 @@ import SubscriptionPeriod from '@/services/Subscription/SubscriptionPeriod';
|
||||
|
||||
export default class PlanSubscription extends mixin(SystemModel) {
|
||||
lemonSubscriptionId: number;
|
||||
|
||||
|
||||
canceledAt: Date;
|
||||
cancelsAt: Date;
|
||||
|
||||
trialStartsAt: Date;
|
||||
trialEndsAt: Date;
|
||||
|
||||
endsAt: Date;
|
||||
startsAt: Date;
|
||||
|
||||
/**
|
||||
* Table name.
|
||||
*/
|
||||
@@ -24,7 +33,7 @@ export default class PlanSubscription extends mixin(SystemModel) {
|
||||
* Defined virtual attributes.
|
||||
*/
|
||||
static get virtualAttributes() {
|
||||
return ['active', 'inactive', 'ended', 'onTrial'];
|
||||
return ['active', 'inactive', 'ended', 'canceled', 'onTrial', 'status'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,7 +49,7 @@ export default class PlanSubscription extends mixin(SystemModel) {
|
||||
builder.where('trial_ends_at', '>', now);
|
||||
},
|
||||
|
||||
inactiveSubscriptions() {
|
||||
inactiveSubscriptions(builder) {
|
||||
builder.modify('endedTrial');
|
||||
builder.modify('endedPeriod');
|
||||
},
|
||||
@@ -100,35 +109,80 @@ export default class PlanSubscription extends mixin(SystemModel) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if subscription is active.
|
||||
* Check if the subscription is expired.
|
||||
* Expired mens the user his lost the right to use the product.
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
public expired() {
|
||||
return this.ended() && !this.onTrial();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if paid subscription is active.
|
||||
* @return {Boolean}
|
||||
*/
|
||||
active() {
|
||||
return !this.ended() || this.onTrial();
|
||||
public active() {
|
||||
return (
|
||||
!this.canceled() && !this.onTrial() && !this.ended() && this.started()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if subscription is inactive.
|
||||
* @return {Boolean}
|
||||
*/
|
||||
inactive() {
|
||||
public inactive() {
|
||||
return !this.active();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if subscription period has ended.
|
||||
* Check if paid subscription period has ended.
|
||||
* @return {Boolean}
|
||||
*/
|
||||
ended() {
|
||||
public ended() {
|
||||
return this.endsAt ? moment().isAfter(this.endsAt) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the paid subscription has started.
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
public started() {
|
||||
return this.startsAt ? moment().isAfter(this.startsAt) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if subscription is currently on trial.
|
||||
* @return {Boolean}
|
||||
*/
|
||||
onTrial() {
|
||||
return this.trailEndsAt ? moment().isAfter(this.trailEndsAt) : false;
|
||||
public onTrial() {
|
||||
return this.trialEndsAt ? moment().isBefore(this.trialEndsAt) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the subscription is canceled.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public canceled() {
|
||||
return (
|
||||
this.canceledAt ||
|
||||
(this.cancelsAt && moment().isAfter(this.cancelsAt)) ||
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the subscription status.
|
||||
* @returns {string}
|
||||
*/
|
||||
public status() {
|
||||
return this.canceled()
|
||||
? 'canceled'
|
||||
: this.onTrial()
|
||||
? 'on_trial'
|
||||
: this.active()
|
||||
? 'active'
|
||||
: 'inactive';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,7 +197,7 @@ export default class PlanSubscription extends mixin(SystemModel) {
|
||||
const period = new SubscriptionPeriod(
|
||||
invoiceInterval,
|
||||
invoicePeriod,
|
||||
start,
|
||||
start
|
||||
);
|
||||
|
||||
const startsAt = period.getStartDate();
|
||||
@@ -159,7 +213,7 @@ export default class PlanSubscription extends mixin(SystemModel) {
|
||||
renew(invoiceInterval, invoicePeriod) {
|
||||
const { startsAt, endsAt } = PlanSubscription.setNewPeriod(
|
||||
invoiceInterval,
|
||||
invoicePeriod,
|
||||
invoicePeriod
|
||||
);
|
||||
return this.$query().update({ startsAt, endsAt });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user