Improve owner transfer process and member sorting

Extended the account ownership transfer tests and implemented several updates. This includes transferring the ownership only to an existing account member, sorting team members based on role hierarchy and whether a member is the primary owner. In the permissions check, prevented non-members from creating invitations and enhanced the styling of role badges depending on if they are custom or not.
This commit is contained in:
giancarlo
2024-04-20 20:33:19 +08:00
parent 4195697b54
commit a55655a61a
8 changed files with 172 additions and 32 deletions

View File

@@ -75,15 +75,27 @@ export function AccountMembersTable({
currentRoleHierarchy: userRoleHierarchy,
});
const filteredMembers = members.filter((member) => {
const searchString = search.toLowerCase();
const displayName = member.name ?? member.email.split('@')[0];
const filteredMembers = members
.filter((member) => {
const searchString = search.toLowerCase();
const displayName = member.name ?? member.email.split('@')[0];
return (
displayName.includes(searchString) ||
member.role.toLowerCase().includes(searchString)
);
});
return (
displayName.includes(searchString) ||
member.role.toLowerCase().includes(searchString)
);
})
.sort((prev, next) => {
if (prev.primary_owner_user_id === prev.user_id) {
return -1;
}
if (prev.role_hierarchy_level < next.role_hierarchy_level) {
return -1;
}
return 1;
});
return (
<div className={'flex flex-col space-y-2'}>

View File

@@ -5,13 +5,15 @@ import { Trans } from '@kit/ui/trans';
type Role = string;
const roles = {
owner: '',
member:
'bg-blue-50 hover:bg-blue-50 text-blue-500 dark:bg-blue-500/10 dark:hover:bg-blue-500/10',
};
const roleClassNameBuilder = cva('font-medium capitalize shadow-none', {
variants: {
role: {
owner: '',
member:
'bg-blue-50 hover:bg-blue-50 text-blue-500 dark:bg-blue-500/10 dark:hover:bg-blue-500/10',
},
role: roles,
},
});
@@ -20,9 +22,10 @@ export const RoleBadge: React.FC<{
}> = ({ role }) => {
// @ts-expect-error: hard to type this since users can add custom roles
const className = roleClassNameBuilder({ role });
const isCustom = !(role in roles);
return (
<Badge className={className}>
<Badge className={className} variant={isCustom ? 'outline' : 'default'}>
<span data-test={'member-role-badge'}>
<Trans i18nKey={`common:roles.${role}.label`} defaults={role} />
</span>

View File

@@ -87,17 +87,20 @@ export async function transferOwnershipAction(
);
}
const service = new AccountMembersService(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
const service = new AccountMembersService(
getSupabaseServerActionClient({ admin: true }),
);
const adminClient = getSupabaseServerActionClient({ admin: true });
await service.transferOwnership({
accountId,
userId,
confirmation: params.confirmation,
});
await service.transferOwnership(
{
accountId,
userId,
confirmation: params.confirmation,
},
adminClient,
);
// revalidate all pages that depend on the account
revalidatePath('/home/[account]', 'layout');

View File

@@ -125,6 +125,7 @@ export class AccountMembersService {
async transferOwnership(
params: z.infer<typeof TransferOwnershipConfirmationSchema>,
adminClient: SupabaseClient<Database>,
) {
const logger = await getLogger();
@@ -135,7 +136,7 @@ export class AccountMembersService {
logger.info(ctx, `Transferring ownership of account...`);
const { data, error } = await this.client.rpc(
const { data, error } = await adminClient.rpc(
'transfer_team_account_ownership',
{
target_account_id: params.accountId,