Refactored classes according to new convention

This commit is contained in:
giancarlo
2024-04-23 00:10:12 +08:00
parent 70da6ef1fa
commit 17e0781581
30 changed files with 351 additions and 131 deletions

View File

@@ -12,7 +12,17 @@ import { DeleteInvitationSchema } from '../../schema/delete-invitation.schema';
import { InviteMembersSchema } from '../../schema/invite-members.schema';
import { UpdateInvitationSchema } from '../../schema/update-invitation.schema';
export class AccountInvitationsService {
export function createAccountInvitationsService(
client: SupabaseClient<Database>,
) {
return new AccountInvitationsService(client);
}
/**
* @name AccountInvitationsService
* @description Service for managing account invitations.
*/
class AccountInvitationsService {
private readonly namespace = 'invitations';
constructor(private readonly client: SupabaseClient<Database>) {}
@@ -50,6 +60,11 @@ export class AccountInvitationsService {
return data;
}
/**
* @name updateInvitation
* @param params
* @description Updates an invitation in the database.
*/
async updateInvitation(params: z.infer<typeof UpdateInvitationSchema>) {
const logger = await getLogger();

View File

@@ -10,13 +10,22 @@ import { Database } from '@kit/supabase/database';
import { RemoveMemberSchema } from '../../schema/remove-member.schema';
import { TransferOwnershipConfirmationSchema } from '../../schema/transfer-ownership-confirmation.schema';
import { UpdateMemberRoleSchema } from '../../schema/update-member-role.schema';
import { AccountPerSeatBillingService } from './account-per-seat-billing.service';
import { createAccountPerSeatBillingService } from './account-per-seat-billing.service';
export class AccountMembersService {
export function createAccountMembersService(client: SupabaseClient<Database>) {
return new AccountMembersService(client);
}
class AccountMembersService {
private readonly namespace = 'account-members';
constructor(private readonly client: SupabaseClient<Database>) {}
/**
* @name removeMemberFromAccount
* @description Removes a member from an account.
* @param params
*/
async removeMemberFromAccount(params: z.infer<typeof RemoveMemberSchema>) {
const logger = await getLogger();
@@ -52,13 +61,19 @@ export class AccountMembersService {
`Successfully removed member from account. Verifying seat count...`,
);
const service = new AccountPerSeatBillingService(this.client);
const service = createAccountPerSeatBillingService(this.client);
await service.decreaseSeats(params.accountId);
return data;
}
/**
* @name updateMemberRole
* @description Updates the role of a member in an account.
* @param params
* @param adminClient
*/
async updateMemberRole(
params: z.infer<typeof UpdateMemberRoleSchema>,
adminClient: SupabaseClient<Database>,
@@ -123,6 +138,12 @@ export class AccountMembersService {
return data;
}
/**
* @name transferOwnership
* @description Transfers ownership of an account to another user.
* @param params
* @param adminClient
*/
async transferOwnership(
params: z.infer<typeof TransferOwnershipConfirmationSchema>,
adminClient: SupabaseClient<Database>,

View File

@@ -2,15 +2,30 @@ import 'server-only';
import { SupabaseClient } from '@supabase/supabase-js';
import { BillingGatewayService } from '@kit/billing-gateway';
import { createBillingGatewayService } from '@kit/billing-gateway';
import { getLogger } from '@kit/shared/logger';
import { Database } from '@kit/supabase/database';
export class AccountPerSeatBillingService {
export function createAccountPerSeatBillingService(
client: SupabaseClient<Database>,
) {
return new AccountPerSeatBillingService(client);
}
/**
* @name AccountPerSeatBillingService
* @description Service for managing per-seat billing for accounts.
*/
class AccountPerSeatBillingService {
private readonly namespace = 'accounts.per-seat-billing';
constructor(private readonly client: SupabaseClient<Database>) {}
/**
* @name getPerSeatSubscriptionItem
* @description Retrieves the per-seat subscription item for an account.
* @param accountId
*/
async getPerSeatSubscriptionItem(accountId: string) {
const logger = await getLogger();
const ctx = { accountId, name: this.namespace };
@@ -66,6 +81,11 @@ export class AccountPerSeatBillingService {
return data;
}
/**
* @name increaseSeats
* @description Increases the number of seats for an account.
* @param accountId
*/
async increaseSeats(accountId: string) {
const logger = await getLogger();
const subscription = await this.getPerSeatSubscriptionItem(accountId);
@@ -82,7 +102,7 @@ export class AccountPerSeatBillingService {
return;
}
const billingGateway = new BillingGatewayService(subscription.provider);
const billingGateway = createBillingGatewayService(subscription.provider);
const ctx = {
name: this.namespace,
@@ -133,6 +153,11 @@ export class AccountPerSeatBillingService {
await Promise.all(promises);
}
/**
* @name decreaseSeats
* @description Decreases the number of seats for an account.
* @param accountId
*/
async decreaseSeats(accountId: string) {
const logger = await getLogger();
const subscription = await this.getPerSeatSubscriptionItem(accountId);
@@ -157,7 +182,7 @@ export class AccountPerSeatBillingService {
logger.info(ctx, `Decreasing seats for account ${accountId}...`);
const billingGateway = new BillingGatewayService(subscription.provider);
const billingGateway = createBillingGatewayService(subscription.provider);
const promises = subscriptionItems.map(async (item) => {
try {

View File

@@ -5,7 +5,13 @@ import { SupabaseClient } from '@supabase/supabase-js';
import { getLogger } from '@kit/shared/logger';
import { Database } from '@kit/supabase/database';
export class CreateTeamAccountService {
export function createCreateTeamAccountService(
client: SupabaseClient<Database>,
) {
return new CreateTeamAccountService(client);
}
class CreateTeamAccountService {
private readonly namespace = 'accounts.create-team-account';
constructor(private readonly client: SupabaseClient<Database>) {}

View File

@@ -5,7 +5,11 @@ import { SupabaseClient } from '@supabase/supabase-js';
import { getLogger } from '@kit/shared/logger';
import { Database } from '@kit/supabase/database';
export class DeleteTeamAccountService {
export function createDeleteTeamAccountService() {
return new DeleteTeamAccountService();
}
class DeleteTeamAccountService {
private readonly namespace = 'accounts.delete-team-account';
/**

View File

@@ -12,11 +12,26 @@ const Schema = z.object({
userId: z.string().uuid(),
});
export class LeaveTeamAccountService {
export function createLeaveTeamAccountService(
client: SupabaseClient<Database>,
) {
return new LeaveTeamAccountService(client);
}
/**
* @name LeaveTeamAccountService
* @description Service for leaving a team account.
*/
class LeaveTeamAccountService {
private readonly namespace = 'leave-team-account';
constructor(private readonly adminClient: SupabaseClient<Database>) {}
/**
* @name leaveTeamAccount
* @description Leave a team account
* @param params
*/
async leaveTeamAccount(params: z.infer<typeof Schema>) {
const logger = await getLogger();

View File

@@ -26,7 +26,13 @@ const env = z
emailSender,
});
export class AccountInvitationsWebhookService {
export function createAccountInvitationsWebhookService(
client: SupabaseClient<Database>,
) {
return new AccountInvitationsWebhookService(client);
}
class AccountInvitationsWebhookService {
private namespace = 'accounts.invitations.webhook';
constructor(private readonly adminClient: SupabaseClient<Database>) {}

View File

@@ -5,7 +5,11 @@ import { Database } from '@kit/supabase/database';
type Account = Database['public']['Tables']['accounts']['Row'];
export class AccountWebhooksService {
export function createAccountWebhooksService() {
return new AccountWebhooksService();
}
class AccountWebhooksService {
private readonly namespace = 'accounts.webhooks';
async handleAccountDeletedWebhook(account: Account) {