Refactor team accounts feature and improve form validation

Updated several components within the team accounts feature to use more specific schema definitions and provide clearer form validation. Several schema files were renamed to better reflect their usage and additional properties were included. The commit also introduces handling for a new accountId property, which provides more accurate user account tracking. Autocomplete was turned off on deletion actions for better security. Changes related to account ownership transfer were also made, including transaction logging and error handling improvements.
This commit is contained in:
giancarlo
2024-03-29 17:39:35 +08:00
parent 9e06d420bd
commit af908ae685
17 changed files with 995 additions and 887 deletions

View File

@@ -1,13 +1,21 @@
import { SupabaseClient } from '@supabase/supabase-js';
import 'server-only';
import { z } from 'zod';
import { Logger } from '@kit/shared/logger';
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';
export class AccountMembersService {
private readonly namespace = 'account-members';
constructor(private readonly client: SupabaseClient<Database>) {}
async removeMemberFromAccount(params: { accountId: string; userId: string }) {
async removeMemberFromAccount(params: z.infer<typeof RemoveMemberSchema>) {
const { data, error } = await this.client
.from('accounts_memberships')
.delete()
@@ -23,11 +31,7 @@ export class AccountMembersService {
return data;
}
async updateMemberRole(params: {
accountId: string;
userId: string;
role: string;
}) {
async updateMemberRole(params: z.infer<typeof UpdateMemberRoleSchema>) {
const { data, error } = await this.client
.from('accounts_memberships')
.update({
@@ -45,21 +49,35 @@ export class AccountMembersService {
return data;
}
async transferOwnership(params: { accountId: string; userId: string }) {
const { data, error } = await this.client
.from('accounts')
.update({
primary_owner_user_id: params.userId,
})
.match({
id: params.accountId,
user_id: params.userId,
});
async transferOwnership(
params: z.infer<typeof TransferOwnershipConfirmationSchema>,
) {
const ctx = {
namespace: this.namespace,
...params,
};
Logger.info(ctx, `Transferring ownership of account`);
const { data, error } = await this.client.rpc(
'transfer_team_account_ownership',
{
target_account_id: params.accountId,
new_owner_id: params.userId,
},
);
if (error) {
Logger.error(
{ ...ctx, error },
`Failed to transfer ownership of account`,
);
throw error;
}
Logger.info(ctx, `Successfully transferred ownership of account`);
return data;
}
}