feat: pause, resume main subscription

This commit is contained in:
Ahmed Bouhuolia
2024-07-27 16:55:56 +02:00
parent 998e6de211
commit db634cbb79
17 changed files with 646 additions and 1 deletions

View File

@@ -0,0 +1,47 @@
import { Inject, Service } from 'typedi';
import { cancelSubscription } from '@lemonsqueezy/lemonsqueezy.js';
import { configureLemonSqueezy } from './utils';
import { PlanSubscription } from '@/system/models';
import { ServiceError } from '@/exceptions';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
import { ERRORS, IOrganizationSubscriptionCanceled } from './types';
@Service()
export class LemonCancelSubscription {
@Inject()
private eventPublisher: EventPublisher;
/**
* Cancels the subscription of the given tenant.
* @param {number} tenantId
* @param {number} subscriptionId
* @returns {Promise<void>}
*/
public async cancelSubscription(tenantId: number) {
configureLemonSqueezy();
const subscription = await PlanSubscription.query().findOne({
tenantId,
slug: 'main',
});
if (!subscription) {
throw new ServiceError(ERRORS.SUBSCRIPTION_ID_NOT_ASSOCIATED_TO_TENANT);
}
const lemonSusbcriptionId = subscription.lemonSubscriptionId;
const subscriptionId = subscription.id;
const cancelledSub = await cancelSubscription(lemonSusbcriptionId);
if (cancelledSub.error) {
throw new Error(cancelledSub.error.message);
}
await PlanSubscription.query().findById(subscriptionId).patch({
canceledAt: new Date(),
});
// Triggers `onSubscriptionCanceled` event.
await this.eventPublisher.emitAsync(
events.subscription.onSubscriptionCanceled,
{ tenantId, subscriptionId } as IOrganizationSubscriptionCanceled
);
}
}

View File

@@ -0,0 +1,47 @@
import { Inject, Service } from 'typedi';
import { updateSubscription } from '@lemonsqueezy/lemonsqueezy.js';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import { ServiceError } from '@/exceptions';
import { PlanSubscription } from '@/system/models';
import { configureLemonSqueezy } from './utils';
import events from '@/subscribers/events';
import { IOrganizationSubscriptionChanged } from './types';
@Service()
export class LemonChangeSubscriptionPlan {
@Inject()
private eventPublisher: EventPublisher;
/**
* Changes the given organization subscription plan.
* @param {number} tenantId - Tenant id.
* @param {number} newVariantId - New variant id.
* @returns {Promise<void>}
*/
public async changeSubscriptionPlan(tenantId: number, newVariantId: number) {
configureLemonSqueezy();
const subscription = await PlanSubscription.query().findOne({
tenantId,
slug: 'main',
});
const lemonSubscriptionId = subscription.lemonSubscriptionId;
// Send request to Lemon Squeezy to change the subscription.
const updatedSub = await updateSubscription(lemonSubscriptionId, {
variantId: newVariantId,
});
if (updatedSub.error) {
throw new ServiceError('SOMETHING_WENT_WRONG');
}
// Triggers `onSubscriptionPlanChanged` event.
await this.eventPublisher.emitAsync(
events.subscription.onSubscriptionPlanChanged,
{
tenantId,
lemonSubscriptionId,
newVariantId,
} as IOrganizationSubscriptionChanged
);
}
}

View File

@@ -0,0 +1,48 @@
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';
import { ERRORS, IOrganizationSubscriptionResumed } from './types';
import { updateSubscription } from '@lemonsqueezy/lemonsqueezy.js';
@Service()
export class LemonResumeSubscription {
@Inject()
private eventPublisher: EventPublisher;
/**
* Resumes the main subscription of the given tenant.
* @param {number} tenantId -
* @returns {Promise<void>}
*/
public async resumeSubscription(tenantId: number) {
configureLemonSqueezy();
const subscription = await PlanSubscription.query().findOne({
tenantId,
slug: 'main',
});
if (!subscription) {
throw new ServiceError(ERRORS.SUBSCRIPTION_ID_NOT_ASSOCIATED_TO_TENANT);
}
const subscriptionId = subscription.id;
const lemonSubscriptionId = subscription.lemonSubscriptionId;
const returnedSub = await updateSubscription(lemonSubscriptionId, {
cancelled: false,
});
if (returnedSub.error) {
throw new ServiceError('');
}
// Update the subscription of the organization.
await PlanSubscription.query().findById(subscriptionId).patch({
canceledAt: null,
});
// Triggers `onSubscriptionCanceled` event.
await this.eventPublisher.emitAsync(
events.subscription.onSubscriptionResumed,
{ tenantId, subscriptionId } as IOrganizationSubscriptionResumed
);
}
}

View File

@@ -0,0 +1,48 @@
import { Inject, Service } from 'typedi';
import { LemonCancelSubscription } from './LemonCancelSubscription';
import { LemonChangeSubscriptionPlan } from './LemonChangeSubscriptionPlan';
import { LemonResumeSubscription } from './LemonResumeSubscription';
@Service()
export class SubscriptionApplication {
@Inject()
private cancelSubscriptionService: LemonCancelSubscription;
@Inject()
private resumeSubscriptionService: LemonResumeSubscription;
@Inject()
private changeSubscriptionPlanService: LemonChangeSubscriptionPlan;
/**
* Cancels the subscription of the given tenant.
* @param {number} tenantId
* @param {string} id
* @returns {Promise<void>}
*/
public cancelSubscription(tenantId: number, id: string) {
return this.cancelSubscriptionService.cancelSubscription(tenantId, id);
}
/**
* Resumes the subscription of the given tenant.
* @param {number} tenantId
* @returns {Promise<void>}
*/
public resumeSubscription(tenantId: number) {
return this.resumeSubscriptionService.resumeSubscription(tenantId);
}
/**
* Changes the given organization subscription plan.
* @param {number} tenantId
* @param {number} newVariantId
* @returns {Promise<void>}
*/
public changeSubscriptionPlan(tenantId: number, newVariantId: number) {
return this.changeSubscriptionPlanService.changeSubscriptionPlan(
tenantId,
newVariantId
);
}
}

View File

@@ -0,0 +1,20 @@
export const ERRORS = {
SUBSCRIPTION_ID_NOT_ASSOCIATED_TO_TENANT:
'SUBSCRIPTION_ID_NOT_ASSOCIATED_TO_TENANT',
};
export interface IOrganizationSubscriptionChanged {
tenantId: number;
lemonSubscriptionId: string;
newVariantId: number;
}
export interface IOrganizationSubscriptionCanceled {
tenantId: number;
subscriptionId: string;
}
export interface IOrganizationSubscriptionResumed {
tenantId: number;
subscriptionId: number;
}