Refactor and improve billing module
The billing module has been refined and enhanced to include deeper validation and detailing of billing plans and products. The checkout session creation process was revised to handle more complex scenarios, incorporating better parsing and validation. Additional validations were added for the plan and product schemas, improving product details extraction, and rearranging of module exports was made for better organization. The code refactor allows easier future modifications and upgrades for recurring and one-time payments with nuanced product configurations.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import { BillingGatewayService } from '@kit/billing-gateway';
|
||||
import { Logger } from '@kit/shared/logger';
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
/**
|
||||
@@ -11,12 +13,95 @@ import { Database } from '@kit/supabase/database';
|
||||
* const accountsService = new AccountsService(client);
|
||||
*/
|
||||
export class PersonalAccountsService {
|
||||
private namespace = 'account';
|
||||
|
||||
constructor(private readonly client: SupabaseClient<Database>) {}
|
||||
|
||||
/**
|
||||
* @name deletePersonalAccount
|
||||
* Delete personal account of a user.
|
||||
* This will delete the user from the authentication provider and cancel all subscriptions.
|
||||
*/
|
||||
async deletePersonalAccount(
|
||||
adminClient: SupabaseClient<Database>,
|
||||
params: { userId: string },
|
||||
) {
|
||||
return adminClient.auth.admin.deleteUser(params.userId);
|
||||
Logger.info(
|
||||
{ userId: params.userId, name: this.namespace },
|
||||
'User requested deletion. Processing...',
|
||||
);
|
||||
|
||||
try {
|
||||
await adminClient.auth.admin.deleteUser(params.userId);
|
||||
} catch (error) {
|
||||
Logger.error(
|
||||
{
|
||||
userId: params.userId,
|
||||
error,
|
||||
name: this.namespace,
|
||||
},
|
||||
'Error deleting user',
|
||||
);
|
||||
|
||||
throw new Error('Error deleting user');
|
||||
}
|
||||
|
||||
try {
|
||||
await this.cancelAllUserSubscriptions(params.userId);
|
||||
} catch (error) {
|
||||
Logger.error({
|
||||
userId: params.userId,
|
||||
error,
|
||||
name: this.namespace,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async cancelAllUserSubscriptions(userId: string) {
|
||||
Logger.info(
|
||||
{
|
||||
userId,
|
||||
name: this.namespace,
|
||||
},
|
||||
'Cancelling all subscriptions for user...',
|
||||
);
|
||||
|
||||
const { data: subscriptions } = await this.client
|
||||
.from('subscriptions')
|
||||
.select('*')
|
||||
.eq('account_id', userId);
|
||||
|
||||
const cancellationRequests = [];
|
||||
|
||||
Logger.info(
|
||||
{
|
||||
userId,
|
||||
subscriptions: subscriptions?.length ?? 0,
|
||||
name: this.namespace,
|
||||
},
|
||||
'Cancelling subscriptions...',
|
||||
);
|
||||
|
||||
for (const subscription of subscriptions ?? []) {
|
||||
const gateway = new BillingGatewayService(subscription.billing_provider);
|
||||
|
||||
cancellationRequests.push(
|
||||
gateway.cancelSubscription({
|
||||
subscriptionId: subscription.id,
|
||||
invoiceNow: true,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
await Promise.all(cancellationRequests);
|
||||
|
||||
Logger.info(
|
||||
{
|
||||
userId,
|
||||
subscriptions: subscriptions?.length ?? 0,
|
||||
name: this.namespace,
|
||||
},
|
||||
'Subscriptions cancelled successfully',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user