Skip to content

Commit

Permalink
add log statements
Browse files Browse the repository at this point in the history
  • Loading branch information
simlarsen committed Nov 3, 2023
1 parent 19b0a1f commit 0b71c8b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
34 changes: 33 additions & 1 deletion CommonServer/Services/BillingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,12 @@ export class BillingService extends BaseService {
meteredSubscriptionId: string;
trialEndsAt?: Date | undefined;
}> {
logger.info('Changing plan');
logger.info(data);

if (!this.isBillingEnabled()) {
logger.info('Billing not enabled');

throw new BadDataException(
'Billing is not enabled for this server.'
);
Expand All @@ -460,26 +465,45 @@ export class BillingService extends BaseService {
const subscription: Stripe.Response<Stripe.Subscription> =
await this.stripe.subscriptions.retrieve(data.subscriptionId);

logger.info('Subscription');
logger.info(subscription);

if (!subscription) {
logger.info('Subscription not found');
throw new BadDataException('Subscription not found');
}

logger.info('Subscription status');
logger.info(subscription.status);

const paymentMethods: Array<PaymentMethod> =
await this.getPaymentMethods(subscription.customer.toString());

logger.info('Payment methods');
logger.info(paymentMethods);

if (paymentMethods.length === 0) {
logger.info('No payment methods');

throw new BadDataException(
'No payment methods added. Please add your card to this project to change your plan'
);
}

logger.info('Cancelling subscriptions');
logger.info(data.subscriptionId);
await this.cancelSubscription(data.subscriptionId);

logger.info('Cancelling metered subscriptions');
logger.info(data.meteredSubscriptionId);
await this.cancelSubscription(data.meteredSubscriptionId);

if (data.endTrialAt && !OneUptimeDate.isInTheFuture(data.endTrialAt)) {
data.endTrialAt = undefined;
}

logger.info('Subscribing to plan');

const subscribeToPlan: {
subscriptionId: string;
meteredSubscriptionId: string;
Expand All @@ -496,11 +520,19 @@ export class BillingService extends BaseService {
promoCode: undefined,
});

return {
logger.info('Subscribed to plan');

const value: {
subscriptionId: string;
meteredSubscriptionId: string;
trialEndsAt?: Date | undefined;
} = {
subscriptionId: subscribeToPlan.subscriptionId,
meteredSubscriptionId: subscribeToPlan.meteredSubscriptionId,
trialEndsAt: subscribeToPlan.trialEndsAt || undefined,
};

return value;
}

public async deletePaymentMethod(
Expand Down
38 changes: 38 additions & 0 deletions CommonServer/Services/ProjectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ export class Service extends DatabaseService<Model> {
project.paymentProviderPlanId !==
updateBy.data.paymentProviderPlanId
) {
logger.info('Changing plan for project ' + project.id);

const plan: SubscriptionPlan | undefined =
SubscriptionPlan.getSubscriptionPlanById(
updateBy.data.paymentProviderPlanId! as string,
Expand All @@ -306,13 +308,29 @@ export class Service extends DatabaseService<Model> {
throw new BadDataException('Invalid plan');
}

logger.info(
'Changing plan for project ' +
project.id?.toString() +
' to ' +
plan.getName()
);

if (!project.paymentProviderSubscriptionSeats) {
project.paymentProviderSubscriptionSeats =
await TeamMemberService.getUniqueTeamMemberCountInProject(
project.id!
);
}

logger.info(
'Changing plan for project ' +
project.id?.toString() +
' to ' +
plan.getName() +
' with seats ' +
project.paymentProviderSubscriptionSeats
);

const subscription: {
subscriptionId: string;
meteredSubscriptionId: string;
Expand All @@ -333,6 +351,16 @@ export class Service extends DatabaseService<Model> {
endTrialAt: project.trialEndsAt,
});

logger.info(
'Changing plan for project ' +
project.id?.toString() +
' to ' +
plan.getName() +
' with seats ' +
project.paymentProviderSubscriptionSeats +
' completed.'
);

await this.updateOneById({
id: new ObjectID(updateBy.query._id! as string),
data: {
Expand All @@ -350,6 +378,16 @@ export class Service extends DatabaseService<Model> {
ignoreHooks: true,
},
});

logger.info(
'Changing plan for project ' +
project.id?.toString() +
' to ' +
plan.getName() +
' with seats ' +
project.paymentProviderSubscriptionSeats +
' completed and project updated.'
);
}
}
}
Expand Down

0 comments on commit 0b71c8b

Please sign in to comment.