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

@@ -8,7 +8,7 @@ import { requireUser } from '@kit/supabase/require-user';
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
import { CreateTeamSchema } from '../../schema/create-team.schema';
import { CreateTeamAccountService } from '../services/create-team-account.service';
import { createCreateTeamAccountService } from '../services/create-team-account.service';
const TEAM_ACCOUNTS_HOME_PATH = z
.string({
@@ -23,7 +23,6 @@ export async function createOrganizationAccountAction(
const { name: accountName } = CreateTeamSchema.parse(params);
const client = getSupabaseServerActionClient();
const service = new CreateTeamAccountService(client);
const auth = await requireUser(client);
if (auth.error) {
@@ -31,6 +30,7 @@ export async function createOrganizationAccountAction(
}
const userId = auth.data.id;
const service = createCreateTeamAccountService(client);
const { data, error } = await service.createNewOrganizationAccount({
name: accountName,

View File

@@ -9,7 +9,7 @@ import { requireUser } from '@kit/supabase/require-user';
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
import { DeleteTeamAccountSchema } from '../../schema/delete-team-account.schema';
import { DeleteTeamAccountService } from '../services/delete-team-account.service';
import { createDeleteTeamAccountService } from '../services/delete-team-account.service';
export async function deleteTeamAccountAction(formData: FormData) {
const params = DeleteTeamAccountSchema.parse(
@@ -27,7 +27,7 @@ export async function deleteTeamAccountAction(formData: FormData) {
await assertUserPermissionsToDeleteTeamAccount(client, params.accountId);
// Get the Supabase client and create a new service instance.
const service = new DeleteTeamAccountService();
const service = createDeleteTeamAccountService();
// Delete the team account and all associated data.
await service.deleteTeamAccount(

View File

@@ -7,7 +7,7 @@ import { requireUser } from '@kit/supabase/require-user';
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
import { LeaveTeamAccountSchema } from '../../schema/leave-team-account.schema';
import { LeaveTeamAccountService } from '../services/leave-team-account.service';
import { createLeaveTeamAccountService } from '../services/leave-team-account.service';
export async function leaveTeamAccountAction(formData: FormData) {
const body = Object.fromEntries(formData.entries());
@@ -20,7 +20,7 @@ export async function leaveTeamAccountAction(formData: FormData) {
throw new Error('Authentication required');
}
const service = new LeaveTeamAccountService(
const service = createLeaveTeamAccountService(
getSupabaseServerActionClient({ admin: true }),
);

View File

@@ -16,8 +16,8 @@ import { DeleteInvitationSchema } from '../../schema/delete-invitation.schema';
import { InviteMembersSchema } from '../../schema/invite-members.schema';
import { RenewInvitationSchema } from '../../schema/renew-invitation.schema';
import { UpdateInvitationSchema } from '../../schema/update-invitation.schema';
import { AccountInvitationsService } from '../services/account-invitations.service';
import { AccountPerSeatBillingService } from '../services/account-per-seat-billing.service';
import { createAccountInvitationsService } from '../services/account-invitations.service';
import { createAccountPerSeatBillingService } from '../services/account-per-seat-billing.service';
/**
* Creates invitations for inviting members.
@@ -34,8 +34,10 @@ export async function createInvitationsAction(params: {
invitations: params.invitations,
});
const service = new AccountInvitationsService(client);
// Create the service
const service = createAccountInvitationsService(client);
// send invitations
await service.sendInvitations({
invitations,
accountSlug: params.accountSlug,
@@ -43,7 +45,9 @@ export async function createInvitationsAction(params: {
revalidateMemberPage();
return { success: true };
return {
success: true,
};
}
/**
@@ -66,8 +70,9 @@ export async function deleteInvitationAction(
throw new Error(`Authentication required`);
}
const service = new AccountInvitationsService(client);
const service = createAccountInvitationsService(client);
// Delete the invitation
await service.deleteInvitation(invitation);
revalidateMemberPage();
@@ -83,7 +88,7 @@ export async function updateInvitationAction(
await assertSession(client);
const service = new AccountInvitationsService(client);
const service = createAccountInvitationsService(client);
await service.updateInvitation(invitation);
@@ -99,10 +104,12 @@ export async function acceptInvitationAction(data: FormData) {
Object.fromEntries(data),
);
const accountPerSeatBillingService = new AccountPerSeatBillingService(client);
// Ensure the user is authenticated
const user = await assertSession(client);
const service = new AccountInvitationsService(client);
// create the services
const perSeatBillingService = createAccountPerSeatBillingService(client);
const service = createAccountInvitationsService(client);
// Accept the invitation
const accountId = await service.acceptInvitationToTeam(
@@ -113,7 +120,13 @@ export async function acceptInvitationAction(data: FormData) {
},
);
await accountPerSeatBillingService.increaseSeats(accountId);
// If the account ID is not present, throw an error
if (!accountId) {
throw new Error('Failed to accept invitation');
}
// Increase the seats for the account
await perSeatBillingService.increaseSeats(accountId);
return redirect(nextPath);
}
@@ -126,13 +139,16 @@ export async function renewInvitationAction(
await assertSession(client);
const service = new AccountInvitationsService(client);
const service = createAccountInvitationsService(client);
// Renew the invitation
await service.renewInvitation(invitationId);
revalidateMemberPage();
return { success: true };
return {
success: true,
};
}
async function assertSession(client: SupabaseClient<Database>) {

View File

@@ -12,7 +12,7 @@ import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-clie
import { RemoveMemberSchema } from '../../schema/remove-member.schema';
import { TransferOwnershipConfirmationSchema } from '../../schema/transfer-ownership-confirmation.schema';
import { UpdateMemberRoleSchema } from '../../schema/update-member-role.schema';
import { AccountMembersService } from '../services/account-members.service';
import { createAccountMembersService } from '../services/account-members.service';
export async function removeMemberFromAccountAction(
params: z.infer<typeof RemoveMemberSchema>,
@@ -26,7 +26,7 @@ export async function removeMemberFromAccountAction(
const { accountId, userId } = RemoveMemberSchema.parse(params);
const service = new AccountMembersService(client);
const service = createAccountMembersService(client);
await service.removeMemberFromAccount({
accountId,
@@ -46,7 +46,7 @@ export async function updateMemberRoleAction(
await assertSession(client);
const service = new AccountMembersService(client);
const service = createAccountMembersService(client);
const adminClient = getSupabaseServerActionClient({ admin: true });
// update the role of the member
@@ -87,7 +87,7 @@ export async function transferOwnershipAction(
);
}
const service = new AccountMembersService(client);
const service = createAccountMembersService(client);
// at this point, the user is authenticated and is the owner of the account
// so we proceed with the transfer of ownership with admin privileges
@@ -105,7 +105,9 @@ export async function transferOwnershipAction(
// revalidate all pages that depend on the account
revalidatePath('/home/[account]', 'layout');
return { success: true };
return {
success: true,
};
}
async function assertSession(client: SupabaseClient<Database>) {