mirror of
https://github.com/documenso/documenso.git
synced 2026-07-26 01:45:08 +10:00
feat: admin member role updates (#2093)
This commit is contained in:
@@ -0,0 +1,218 @@
|
|||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
|
import { useLingui } from '@lingui/react/macro';
|
||||||
|
import { Trans } from '@lingui/react/macro';
|
||||||
|
import { OrganisationMemberRole } from '@prisma/client';
|
||||||
|
import type * as DialogPrimitive from '@radix-ui/react-dialog';
|
||||||
|
import { useForm } from 'react-hook-form';
|
||||||
|
import { useNavigate } from 'react-router';
|
||||||
|
import { match } from 'ts-pattern';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import { getHighestOrganisationRoleInGroup } from '@documenso/lib/utils/organisations';
|
||||||
|
import { trpc } from '@documenso/trpc/react';
|
||||||
|
import type { TGetAdminOrganisationResponse } from '@documenso/trpc/server/admin-router/get-admin-organisation.types';
|
||||||
|
import { Button } from '@documenso/ui/primitives/button';
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogDescription,
|
||||||
|
DialogFooter,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
DialogTrigger,
|
||||||
|
} from '@documenso/ui/primitives/dialog';
|
||||||
|
import {
|
||||||
|
Form,
|
||||||
|
FormControl,
|
||||||
|
FormField,
|
||||||
|
FormItem,
|
||||||
|
FormLabel,
|
||||||
|
FormMessage,
|
||||||
|
} from '@documenso/ui/primitives/form/form';
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
} from '@documenso/ui/primitives/select';
|
||||||
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
||||||
|
|
||||||
|
export type AdminOrganisationMemberUpdateDialogProps = {
|
||||||
|
trigger?: React.ReactNode;
|
||||||
|
organisationId: string;
|
||||||
|
organisationMember: TGetAdminOrganisationResponse['members'][number];
|
||||||
|
isOwner: boolean;
|
||||||
|
} & Omit<DialogPrimitive.DialogProps, 'children'>;
|
||||||
|
|
||||||
|
const ZUpdateOrganisationMemberFormSchema = z.object({
|
||||||
|
role: z.enum(['OWNER', 'ADMIN', 'MANAGER', 'MEMBER']),
|
||||||
|
});
|
||||||
|
|
||||||
|
type ZUpdateOrganisationMemberSchema = z.infer<typeof ZUpdateOrganisationMemberFormSchema>;
|
||||||
|
|
||||||
|
export const AdminOrganisationMemberUpdateDialog = ({
|
||||||
|
trigger,
|
||||||
|
organisationId,
|
||||||
|
organisationMember,
|
||||||
|
isOwner,
|
||||||
|
...props
|
||||||
|
}: AdminOrganisationMemberUpdateDialogProps) => {
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
|
||||||
|
const { t } = useLingui();
|
||||||
|
const { toast } = useToast();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
// Determine the current role value for the form
|
||||||
|
const currentRoleValue = isOwner
|
||||||
|
? 'OWNER'
|
||||||
|
: getHighestOrganisationRoleInGroup(
|
||||||
|
organisationMember.organisationGroupMembers.map((ogm) => ogm.group),
|
||||||
|
);
|
||||||
|
const organisationMemberName = organisationMember.user.name ?? organisationMember.user.email;
|
||||||
|
|
||||||
|
const form = useForm<ZUpdateOrganisationMemberSchema>({
|
||||||
|
resolver: zodResolver(ZUpdateOrganisationMemberFormSchema),
|
||||||
|
defaultValues: {
|
||||||
|
role: currentRoleValue,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const { mutateAsync: updateOrganisationMemberRole } =
|
||||||
|
trpc.admin.organisationMember.updateRole.useMutation();
|
||||||
|
|
||||||
|
const onFormSubmit = async ({ role }: ZUpdateOrganisationMemberSchema) => {
|
||||||
|
try {
|
||||||
|
await updateOrganisationMemberRole({
|
||||||
|
organisationId,
|
||||||
|
userId: organisationMember.userId,
|
||||||
|
role,
|
||||||
|
});
|
||||||
|
|
||||||
|
const roleLabel = match(role)
|
||||||
|
.with('OWNER', () => t`Owner`)
|
||||||
|
.with(OrganisationMemberRole.ADMIN, () => t`Admin`)
|
||||||
|
.with(OrganisationMemberRole.MANAGER, () => t`Manager`)
|
||||||
|
.with(OrganisationMemberRole.MEMBER, () => t`Member`)
|
||||||
|
.exhaustive();
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: t`Success`,
|
||||||
|
description:
|
||||||
|
role === 'OWNER'
|
||||||
|
? t`Ownership transferred to ${organisationMemberName}.`
|
||||||
|
: t`Updated ${organisationMemberName} to ${roleLabel}.`,
|
||||||
|
duration: 5000,
|
||||||
|
});
|
||||||
|
|
||||||
|
setOpen(false);
|
||||||
|
|
||||||
|
// Refresh the page to show updated data
|
||||||
|
await navigate(0);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
|
||||||
|
toast({
|
||||||
|
title: t`An unknown error occurred`,
|
||||||
|
description: t`We encountered an unknown error while attempting to update this organisation member. Please try again later.`,
|
||||||
|
variant: 'destructive',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!open) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
form.reset({
|
||||||
|
role: currentRoleValue,
|
||||||
|
});
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [open, currentRoleValue, form]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog
|
||||||
|
{...props}
|
||||||
|
open={open}
|
||||||
|
onOpenChange={(value) => !form.formState.isSubmitting && setOpen(value)}
|
||||||
|
>
|
||||||
|
<DialogTrigger onClick={(e) => e.stopPropagation()} asChild>
|
||||||
|
{trigger ?? (
|
||||||
|
<Button variant="secondary">
|
||||||
|
<Trans>Update role</Trans>
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</DialogTrigger>
|
||||||
|
|
||||||
|
<DialogContent position="center">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>
|
||||||
|
<Trans>Update organisation member</Trans>
|
||||||
|
</DialogTitle>
|
||||||
|
|
||||||
|
<DialogDescription className="mt-4">
|
||||||
|
<Trans>
|
||||||
|
You are currently updating{' '}
|
||||||
|
<span className="font-bold">{organisationMemberName}.</span>
|
||||||
|
</Trans>
|
||||||
|
</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
|
||||||
|
<Form {...form}>
|
||||||
|
<form onSubmit={form.handleSubmit(onFormSubmit)}>
|
||||||
|
<fieldset className="flex h-full flex-col" disabled={form.formState.isSubmitting}>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="role"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem className="w-full">
|
||||||
|
<FormLabel required>
|
||||||
|
<Trans>Role</Trans>
|
||||||
|
</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<Select {...field} onValueChange={field.onChange}>
|
||||||
|
<SelectTrigger className="text-muted-foreground">
|
||||||
|
<SelectValue />
|
||||||
|
</SelectTrigger>
|
||||||
|
|
||||||
|
<SelectContent className="w-full" position="popper">
|
||||||
|
<SelectItem value="OWNER">
|
||||||
|
<Trans>Owner</Trans>
|
||||||
|
</SelectItem>
|
||||||
|
<SelectItem value={OrganisationMemberRole.ADMIN}>
|
||||||
|
<Trans>Admin</Trans>
|
||||||
|
</SelectItem>
|
||||||
|
<SelectItem value={OrganisationMemberRole.MANAGER}>
|
||||||
|
<Trans>Manager</Trans>
|
||||||
|
</SelectItem>
|
||||||
|
<SelectItem value={OrganisationMemberRole.MEMBER}>
|
||||||
|
<Trans>Member</Trans>
|
||||||
|
</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<DialogFooter className="mt-4">
|
||||||
|
<Button type="button" variant="secondary" onClick={() => setOpen(false)}>
|
||||||
|
<Trans>Cancel</Trans>
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<Button type="submit" loading={form.formState.isSubmitting}>
|
||||||
|
<Trans>Update</Trans>
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</fieldset>
|
||||||
|
</form>
|
||||||
|
</Form>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -34,6 +34,7 @@ import { Input } from '@documenso/ui/primitives/input';
|
|||||||
import { Tooltip, TooltipContent, TooltipTrigger } from '@documenso/ui/primitives/tooltip';
|
import { Tooltip, TooltipContent, TooltipTrigger } from '@documenso/ui/primitives/tooltip';
|
||||||
import { useToast } from '@documenso/ui/primitives/use-toast';
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
||||||
|
|
||||||
|
import { AdminOrganisationMemberUpdateDialog } from '~/components/dialogs/admin-organisation-member-update-dialog';
|
||||||
import { GenericErrorLayout } from '~/components/general/generic-error-layout';
|
import { GenericErrorLayout } from '~/components/general/generic-error-layout';
|
||||||
import { SettingsHeader } from '~/components/general/settings-header';
|
import { SettingsHeader } from '~/components/general/settings-header';
|
||||||
|
|
||||||
@@ -71,23 +72,6 @@ export default function OrganisationGroupSettingsPage({ params }: Route.Componen
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const { mutateAsync: promoteToOwner, isPending: isPromotingToOwner } =
|
|
||||||
trpc.admin.organisationMember.promoteToOwner.useMutation({
|
|
||||||
onSuccess: () => {
|
|
||||||
toast({
|
|
||||||
title: t`Success`,
|
|
||||||
description: t`Member promoted to owner successfully`,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
onError: () => {
|
|
||||||
toast({
|
|
||||||
title: t`Error`,
|
|
||||||
description: t`We couldn't promote the member to owner. Please try again.`,
|
|
||||||
variant: 'destructive',
|
|
||||||
});
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const teamsColumns = useMemo(() => {
|
const teamsColumns = useMemo(() => {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
@@ -120,23 +104,24 @@ export default function OrganisationGroupSettingsPage({ params }: Route.Componen
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
header: t`Actions`,
|
header: t`Actions`,
|
||||||
cell: ({ row }) => (
|
cell: ({ row }) => {
|
||||||
<div className="flex justify-end space-x-2">
|
const isOwner = row.original.userId === organisation?.ownerUserId;
|
||||||
<Button
|
|
||||||
variant="outline"
|
return (
|
||||||
disabled={row.original.userId === organisation?.ownerUserId}
|
<div className="flex justify-end space-x-2">
|
||||||
loading={isPromotingToOwner}
|
<AdminOrganisationMemberUpdateDialog
|
||||||
onClick={async () =>
|
trigger={
|
||||||
promoteToOwner({
|
<Button variant="outline">
|
||||||
organisationId,
|
<Trans>Update role</Trans>
|
||||||
userId: row.original.userId,
|
</Button>
|
||||||
})
|
}
|
||||||
}
|
organisationId={organisationId}
|
||||||
>
|
organisationMember={row.original}
|
||||||
<Trans>Promote to owner</Trans>
|
isOwner={isOwner}
|
||||||
</Button>
|
/>
|
||||||
</div>
|
</div>
|
||||||
),
|
);
|
||||||
|
},
|
||||||
},
|
},
|
||||||
] satisfies DataTableColumnDef<TGetAdminOrganisationResponse['members'][number]>[];
|
] satisfies DataTableColumnDef<TGetAdminOrganisationResponse['members'][number]>[];
|
||||||
}, [organisation]);
|
}, [organisation]);
|
||||||
|
|||||||
+156
-47
@@ -68,15 +68,29 @@ test('[ADMIN]: promote member to owner', async ({ page }) => {
|
|||||||
// Test promoting a MEMBER to owner
|
// Test promoting a MEMBER to owner
|
||||||
const memberRow = page.getByRole('row', { name: memberUser.email });
|
const memberRow = page.getByRole('row', { name: memberUser.email });
|
||||||
|
|
||||||
// Find and click the "Promote to owner" button for the member
|
// Find and click the "Update role" button for the member
|
||||||
const promoteButton = memberRow.getByRole('button', { name: 'Promote to owner' });
|
const updateRoleButton = memberRow.getByRole('button', {
|
||||||
await expect(promoteButton).toBeVisible();
|
name: 'Update role',
|
||||||
await expect(promoteButton).not.toBeDisabled();
|
});
|
||||||
|
await expect(updateRoleButton).toBeVisible();
|
||||||
|
await expect(updateRoleButton).not.toBeDisabled();
|
||||||
|
|
||||||
await promoteButton.click();
|
await updateRoleButton.click();
|
||||||
|
|
||||||
// Verify success toast appears
|
// Wait for dialog to open and select Owner role
|
||||||
await expect(page.getByText('Member promoted to owner successfully').first()).toBeVisible();
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Find and click the select trigger - it's a button with role="combobox"
|
||||||
|
await page.getByRole('dialog').locator('button[role="combobox"]').click();
|
||||||
|
|
||||||
|
// Select "Owner" from the dropdown options
|
||||||
|
await page.getByRole('option', { name: 'Owner' }).click();
|
||||||
|
|
||||||
|
// Click Update button
|
||||||
|
await page.getByRole('dialog').getByRole('button', { name: 'Update' }).click();
|
||||||
|
|
||||||
|
// Wait for dialog to close (indicates success)
|
||||||
|
await expect(page.getByRole('dialog')).not.toBeVisible({ timeout: 10_000 });
|
||||||
|
|
||||||
// Reload the page to see the changes
|
// Reload the page to see the changes
|
||||||
await page.reload();
|
await page.reload();
|
||||||
@@ -89,12 +103,18 @@ test('[ADMIN]: promote member to owner', async ({ page }) => {
|
|||||||
const previousOwnerRow = page.getByRole('row', { name: ownerUser.email });
|
const previousOwnerRow = page.getByRole('row', { name: ownerUser.email });
|
||||||
await expect(previousOwnerRow.getByRole('status').filter({ hasText: 'Owner' })).not.toBeVisible();
|
await expect(previousOwnerRow.getByRole('status').filter({ hasText: 'Owner' })).not.toBeVisible();
|
||||||
|
|
||||||
// Verify that the promote button is now disabled for the new owner
|
// Verify that the Update role button exists for the new owner and shows Owner as current role
|
||||||
const newOwnerPromoteButton = newOwnerRow.getByRole('button', { name: 'Promote to owner' });
|
const newOwnerUpdateButton = newOwnerRow.getByRole('button', {
|
||||||
await expect(newOwnerPromoteButton).toBeDisabled();
|
name: 'Update role',
|
||||||
|
});
|
||||||
|
await expect(newOwnerUpdateButton).toBeVisible();
|
||||||
|
|
||||||
// Test that we can't promote the current owner (button should be disabled)
|
// Verify clicking it shows the dialog with Owner already selected
|
||||||
await expect(newOwnerPromoteButton).toHaveAttribute('disabled');
|
await newOwnerUpdateButton.click();
|
||||||
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Close the dialog without making changes
|
||||||
|
await page.getByRole('button', { name: 'Cancel' }).click();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('[ADMIN]: promote manager to owner', async ({ page }) => {
|
test('[ADMIN]: promote manager to owner', async ({ page }) => {
|
||||||
@@ -130,10 +150,26 @@ test('[ADMIN]: promote manager to owner', async ({ page }) => {
|
|||||||
|
|
||||||
// Promote the manager to owner
|
// Promote the manager to owner
|
||||||
const managerRow = page.getByRole('row', { name: managerUser.email });
|
const managerRow = page.getByRole('row', { name: managerUser.email });
|
||||||
const promoteButton = managerRow.getByRole('button', { name: 'Promote to owner' });
|
const updateRoleButton = managerRow.getByRole('button', {
|
||||||
|
name: 'Update role',
|
||||||
|
});
|
||||||
|
|
||||||
await promoteButton.click();
|
await updateRoleButton.click();
|
||||||
await expect(page.getByText('Member promoted to owner successfully').first()).toBeVisible();
|
|
||||||
|
// Wait for dialog to open and select Owner role
|
||||||
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Find and click the select trigger - it's a button with role="combobox"
|
||||||
|
await page.getByRole('dialog').locator('button[role="combobox"]').click();
|
||||||
|
|
||||||
|
// Select "Owner" from the dropdown options
|
||||||
|
await page.getByRole('option', { name: 'Owner' }).click();
|
||||||
|
|
||||||
|
// Click Update button
|
||||||
|
await page.getByRole('dialog').getByRole('button', { name: 'Update' }).click();
|
||||||
|
|
||||||
|
// Wait for dialog to close (indicates success)
|
||||||
|
await expect(page.getByRole('dialog')).not.toBeVisible({ timeout: 10_000 });
|
||||||
|
|
||||||
// Reload and verify the change
|
// Reload and verify the change
|
||||||
await page.reload();
|
await page.reload();
|
||||||
@@ -173,14 +209,27 @@ test('[ADMIN]: promote admin member to owner', async ({ page }) => {
|
|||||||
|
|
||||||
// Promote the admin member to owner
|
// Promote the admin member to owner
|
||||||
const adminMemberRow = page.getByRole('row', { name: adminMemberUser.email });
|
const adminMemberRow = page.getByRole('row', { name: adminMemberUser.email });
|
||||||
const promoteButton = adminMemberRow.getByRole('button', { name: 'Promote to owner' });
|
const updateRoleButton = adminMemberRow.getByRole('button', {
|
||||||
|
name: 'Update role',
|
||||||
await promoteButton.click();
|
|
||||||
|
|
||||||
await expect(page.getByText('Member promoted to owner successfully').first()).toBeVisible({
|
|
||||||
timeout: 10_000,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await updateRoleButton.click();
|
||||||
|
|
||||||
|
// Wait for dialog to open and select Owner role
|
||||||
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Find and click the select trigger - it's a button with role="combobox"
|
||||||
|
await page.getByRole('dialog').locator('button[role="combobox"]').click();
|
||||||
|
|
||||||
|
// Select "Owner" from the dropdown options
|
||||||
|
await page.getByRole('option', { name: 'Owner' }).click();
|
||||||
|
|
||||||
|
// Click Update button
|
||||||
|
await page.getByRole('dialog').getByRole('button', { name: 'Update' }).click();
|
||||||
|
|
||||||
|
// Wait for dialog to close (indicates success)
|
||||||
|
await expect(page.getByRole('dialog')).not.toBeVisible({ timeout: 10_000 });
|
||||||
|
|
||||||
// Reload and verify the change
|
// Reload and verify the change
|
||||||
await page.reload();
|
await page.reload();
|
||||||
await expect(adminMemberRow.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
await expect(adminMemberRow.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
||||||
@@ -249,11 +298,25 @@ test('[ADMIN]: verify role hierarchy after promotion', async ({ page }) => {
|
|||||||
await expect(memberRow.getByRole('status').filter({ hasText: 'Owner' })).not.toBeVisible();
|
await expect(memberRow.getByRole('status').filter({ hasText: 'Owner' })).not.toBeVisible();
|
||||||
|
|
||||||
// Promote member to owner
|
// Promote member to owner
|
||||||
const promoteButton = memberRow.getByRole('button', { name: 'Promote to owner' });
|
const updateRoleButton = memberRow.getByRole('button', {
|
||||||
await promoteButton.click();
|
name: 'Update role',
|
||||||
await expect(page.getByText('Member promoted to owner successfully').first()).toBeVisible({
|
|
||||||
timeout: 10_000,
|
|
||||||
});
|
});
|
||||||
|
await updateRoleButton.click();
|
||||||
|
|
||||||
|
// Wait for dialog to open and select Owner role
|
||||||
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Find and click the select trigger - it's a button with role="combobox"
|
||||||
|
await page.getByRole('dialog').locator('button[role="combobox"]').click();
|
||||||
|
|
||||||
|
// Select "Owner" from the dropdown options
|
||||||
|
await page.getByRole('option', { name: 'Owner' }).click();
|
||||||
|
|
||||||
|
// Click Update button
|
||||||
|
await page.getByRole('dialog').getByRole('button', { name: 'Update' }).click();
|
||||||
|
|
||||||
|
// Wait for dialog to close (indicates success)
|
||||||
|
await expect(page.getByRole('dialog')).not.toBeVisible({ timeout: 10_000 });
|
||||||
|
|
||||||
// Reload page to see updated state
|
// Reload page to see updated state
|
||||||
await page.reload();
|
await page.reload();
|
||||||
@@ -262,9 +325,11 @@ test('[ADMIN]: verify role hierarchy after promotion', async ({ page }) => {
|
|||||||
memberRow = page.getByRole('row', { name: memberUser.email });
|
memberRow = page.getByRole('row', { name: memberUser.email });
|
||||||
await expect(memberRow.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
await expect(memberRow.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
||||||
|
|
||||||
// Verify the promote button is now disabled for the new owner
|
// Verify the Update role button exists and shows Owner as current role
|
||||||
const newOwnerPromoteButton = memberRow.getByRole('button', { name: 'Promote to owner' });
|
const newOwnerUpdateButton = memberRow.getByRole('button', {
|
||||||
await expect(newOwnerPromoteButton).toBeDisabled();
|
name: 'Update role',
|
||||||
|
});
|
||||||
|
await expect(newOwnerUpdateButton).toBeVisible();
|
||||||
|
|
||||||
// Sign in as the newly promoted user to verify they have owner permissions
|
// Sign in as the newly promoted user to verify they have owner permissions
|
||||||
await apiSignin({
|
await apiSignin({
|
||||||
@@ -336,28 +401,56 @@ test('[ADMIN]: multiple promotions in sequence', async ({ page }) => {
|
|||||||
|
|
||||||
// First promotion: Member 1 becomes owner
|
// First promotion: Member 1 becomes owner
|
||||||
let member1Row = page.getByRole('row', { name: member1User.email });
|
let member1Row = page.getByRole('row', { name: member1User.email });
|
||||||
let promoteButton1 = member1Row.getByRole('button', { name: 'Promote to owner' });
|
let updateRoleButton1 = member1Row.getByRole('button', {
|
||||||
await promoteButton1.click();
|
name: 'Update role',
|
||||||
await expect(page.getByText('Member promoted to owner successfully').first()).toBeVisible({
|
|
||||||
timeout: 10_000,
|
|
||||||
});
|
});
|
||||||
|
await updateRoleButton1.click();
|
||||||
|
|
||||||
|
// Wait for dialog to open and select Owner role
|
||||||
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Find and click the select trigger - it's a button with role="combobox"
|
||||||
|
await page.getByRole('dialog').locator('button[role="combobox"]').click();
|
||||||
|
|
||||||
|
// Select "Owner" from the dropdown options
|
||||||
|
await page.getByRole('option', { name: 'Owner' }).click();
|
||||||
|
|
||||||
|
// Click Update button
|
||||||
|
await page.getByRole('dialog').getByRole('button', { name: 'Update' }).click();
|
||||||
|
|
||||||
|
// Wait for dialog to close (indicates success)
|
||||||
|
await expect(page.getByRole('dialog')).not.toBeVisible({ timeout: 10_000 });
|
||||||
|
|
||||||
await page.reload();
|
await page.reload();
|
||||||
|
|
||||||
// Verify Member 1 is now owner and button is disabled
|
// Verify Member 1 is now owner
|
||||||
member1Row = page.getByRole('row', { name: member1User.email });
|
member1Row = page.getByRole('row', { name: member1User.email });
|
||||||
await expect(member1Row.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
await expect(member1Row.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
||||||
promoteButton1 = member1Row.getByRole('button', { name: 'Promote to owner' });
|
updateRoleButton1 = member1Row.getByRole('button', { name: 'Update role' });
|
||||||
await expect(promoteButton1).toBeDisabled();
|
await expect(updateRoleButton1).toBeVisible();
|
||||||
|
|
||||||
// Second promotion: Member 2 becomes the new owner
|
// Second promotion: Member 2 becomes the new owner
|
||||||
const member2Row = page.getByRole('row', { name: member2User.email });
|
const member2Row = page.getByRole('row', { name: member2User.email });
|
||||||
const promoteButton2 = member2Row.getByRole('button', { name: 'Promote to owner' });
|
const updateRoleButton2 = member2Row.getByRole('button', {
|
||||||
await expect(promoteButton2).not.toBeDisabled();
|
name: 'Update role',
|
||||||
await promoteButton2.click();
|
|
||||||
await expect(page.getByText('Member promoted to owner successfully').first()).toBeVisible({
|
|
||||||
timeout: 10_000,
|
|
||||||
});
|
});
|
||||||
|
await expect(updateRoleButton2).toBeVisible();
|
||||||
|
await updateRoleButton2.click();
|
||||||
|
|
||||||
|
// Wait for dialog to open and select Owner role
|
||||||
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Find and click the select trigger - it's a button with role="combobox"
|
||||||
|
await page.getByRole('dialog').locator('button[role="combobox"]').click();
|
||||||
|
|
||||||
|
// Select "Owner" from the dropdown options
|
||||||
|
await page.getByRole('option', { name: 'Owner' }).click();
|
||||||
|
|
||||||
|
// Click Update button
|
||||||
|
await page.getByRole('dialog').getByRole('button', { name: 'Update' }).click();
|
||||||
|
|
||||||
|
// Wait for dialog to close (indicates success)
|
||||||
|
await expect(page.getByRole('dialog')).not.toBeVisible({ timeout: 10_000 });
|
||||||
|
|
||||||
await page.reload();
|
await page.reload();
|
||||||
|
|
||||||
@@ -365,9 +458,11 @@ test('[ADMIN]: multiple promotions in sequence', async ({ page }) => {
|
|||||||
await expect(member2Row.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
await expect(member2Row.getByRole('status').filter({ hasText: 'Owner' })).toBeVisible();
|
||||||
await expect(member1Row.getByRole('status').filter({ hasText: 'Owner' })).not.toBeVisible();
|
await expect(member1Row.getByRole('status').filter({ hasText: 'Owner' })).not.toBeVisible();
|
||||||
|
|
||||||
// Verify Member 1's promote button is now enabled again
|
// Verify Member 1's Update role button is still visible
|
||||||
const newPromoteButton1 = member1Row.getByRole('button', { name: 'Promote to owner' });
|
const newUpdateButton1 = member1Row.getByRole('button', {
|
||||||
await expect(newPromoteButton1).not.toBeDisabled();
|
name: 'Update role',
|
||||||
|
});
|
||||||
|
await expect(newUpdateButton1).toBeVisible();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('[ADMIN]: verify organisation access after ownership change', async ({ page }) => {
|
test('[ADMIN]: verify organisation access after ownership change', async ({ page }) => {
|
||||||
@@ -402,11 +497,25 @@ test('[ADMIN]: verify organisation access after ownership change', async ({ page
|
|||||||
});
|
});
|
||||||
|
|
||||||
const memberRow = page.getByRole('row', { name: memberUser.email });
|
const memberRow = page.getByRole('row', { name: memberUser.email });
|
||||||
const promoteButton = memberRow.getByRole('button', { name: 'Promote to owner' });
|
const updateRoleButton = memberRow.getByRole('button', {
|
||||||
await promoteButton.click();
|
name: 'Update role',
|
||||||
await expect(page.getByText('Member promoted to owner successfully').first()).toBeVisible({
|
|
||||||
timeout: 10_000,
|
|
||||||
});
|
});
|
||||||
|
await updateRoleButton.click();
|
||||||
|
|
||||||
|
// Wait for dialog to open and select Owner role
|
||||||
|
await expect(page.getByRole('dialog')).toBeVisible();
|
||||||
|
|
||||||
|
// Find and click the select trigger - it's a button with role="combobox"
|
||||||
|
await page.getByRole('dialog').locator('button[role="combobox"]').click();
|
||||||
|
|
||||||
|
// Select "Owner" from the dropdown options
|
||||||
|
await page.getByRole('option', { name: 'Owner' }).click();
|
||||||
|
|
||||||
|
// Click Update button
|
||||||
|
await page.getByRole('dialog').getByRole('button', { name: 'Update' }).click();
|
||||||
|
|
||||||
|
// Wait for dialog to close (indicates success)
|
||||||
|
await expect(page.getByRole('dialog')).not.toBeVisible({ timeout: 10_000 });
|
||||||
|
|
||||||
// Test that the new owner can access organisation settings
|
// Test that the new owner can access organisation settings
|
||||||
await apiSignin({
|
await apiSignin({
|
||||||
@@ -39,6 +39,11 @@ export const getAdminOrganisation = async ({ organisationId }: GetOrganisationOp
|
|||||||
teams: true,
|
teams: true,
|
||||||
members: {
|
members: {
|
||||||
include: {
|
include: {
|
||||||
|
organisationGroupMembers: {
|
||||||
|
include: {
|
||||||
|
group: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
user: {
|
user: {
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import { z } from 'zod';
|
|||||||
import { ZOrganisationSchema } from '@documenso/lib/types/organisation';
|
import { ZOrganisationSchema } from '@documenso/lib/types/organisation';
|
||||||
import OrganisationClaimSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationClaimSchema';
|
import OrganisationClaimSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationClaimSchema';
|
||||||
import OrganisationGlobalSettingsSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationGlobalSettingsSchema';
|
import OrganisationGlobalSettingsSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationGlobalSettingsSchema';
|
||||||
|
import OrganisationGroupMemberSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationGroupMemberSchema';
|
||||||
|
import OrganisationGroupSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationGroupSchema';
|
||||||
import OrganisationMemberSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationMemberSchema';
|
import OrganisationMemberSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationMemberSchema';
|
||||||
import SubscriptionSchema from '@documenso/prisma/generated/zod/modelSchema/SubscriptionSchema';
|
import SubscriptionSchema from '@documenso/prisma/generated/zod/modelSchema/SubscriptionSchema';
|
||||||
import TeamSchema from '@documenso/prisma/generated/zod/modelSchema/TeamSchema';
|
import TeamSchema from '@documenso/prisma/generated/zod/modelSchema/TeamSchema';
|
||||||
@@ -30,6 +32,18 @@ export const ZGetAdminOrganisationResponseSchema = ZOrganisationSchema.extend({
|
|||||||
email: true,
|
email: true,
|
||||||
name: true,
|
name: true,
|
||||||
}),
|
}),
|
||||||
|
organisationGroupMembers: z.array(
|
||||||
|
OrganisationGroupMemberSchema.pick({
|
||||||
|
id: true,
|
||||||
|
groupId: true,
|
||||||
|
}).extend({
|
||||||
|
group: OrganisationGroupSchema.pick({
|
||||||
|
id: true,
|
||||||
|
type: true,
|
||||||
|
organisationRole: true,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
),
|
||||||
}).array(),
|
}).array(),
|
||||||
subscription: SubscriptionSchema.nullable(),
|
subscription: SubscriptionSchema.nullable(),
|
||||||
organisationClaim: OrganisationClaimSchema,
|
organisationClaim: OrganisationClaimSchema,
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import { promoteMemberToOwnerRoute } from './promote-member-to-owner';
|
|||||||
import { resealDocumentRoute } from './reseal-document';
|
import { resealDocumentRoute } from './reseal-document';
|
||||||
import { resetTwoFactorRoute } from './reset-two-factor-authentication';
|
import { resetTwoFactorRoute } from './reset-two-factor-authentication';
|
||||||
import { updateAdminOrganisationRoute } from './update-admin-organisation';
|
import { updateAdminOrganisationRoute } from './update-admin-organisation';
|
||||||
|
import { updateOrganisationMemberRoleRoute } from './update-organisation-member-role';
|
||||||
import { updateRecipientRoute } from './update-recipient';
|
import { updateRecipientRoute } from './update-recipient';
|
||||||
import { updateSiteSettingRoute } from './update-site-setting';
|
import { updateSiteSettingRoute } from './update-site-setting';
|
||||||
import { updateSubscriptionClaimRoute } from './update-subscription-claim';
|
import { updateSubscriptionClaimRoute } from './update-subscription-claim';
|
||||||
@@ -31,6 +32,7 @@ export const adminRouter = router({
|
|||||||
},
|
},
|
||||||
organisationMember: {
|
organisationMember: {
|
||||||
promoteToOwner: promoteMemberToOwnerRoute,
|
promoteToOwner: promoteMemberToOwnerRoute,
|
||||||
|
updateRole: updateOrganisationMemberRoleRoute,
|
||||||
},
|
},
|
||||||
claims: {
|
claims: {
|
||||||
find: findSubscriptionClaimsRoute,
|
find: findSubscriptionClaimsRoute,
|
||||||
|
|||||||
@@ -0,0 +1,220 @@
|
|||||||
|
import { OrganisationGroupType, OrganisationMemberRole } from '@prisma/client';
|
||||||
|
|
||||||
|
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||||
|
import { generateDatabaseId } from '@documenso/lib/universal/id';
|
||||||
|
import { getHighestOrganisationRoleInGroup } from '@documenso/lib/utils/organisations';
|
||||||
|
import { prisma } from '@documenso/prisma';
|
||||||
|
|
||||||
|
import { adminProcedure } from '../trpc';
|
||||||
|
import {
|
||||||
|
ZUpdateOrganisationMemberRoleRequestSchema,
|
||||||
|
ZUpdateOrganisationMemberRoleResponseSchema,
|
||||||
|
} from './update-organisation-member-role.types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Admin mutation to update organisation member role or transfer ownership.
|
||||||
|
*
|
||||||
|
* This mutation handles two scenarios:
|
||||||
|
* 1. When role='OWNER': Transfers organisation ownership and promotes to ADMIN
|
||||||
|
* 2. When role=ADMIN/MANAGER/MEMBER: Updates group membership
|
||||||
|
*
|
||||||
|
* Admin privileges bypass normal hierarchy restrictions.
|
||||||
|
*/
|
||||||
|
export const updateOrganisationMemberRoleRoute = adminProcedure
|
||||||
|
.input(ZUpdateOrganisationMemberRoleRequestSchema)
|
||||||
|
.output(ZUpdateOrganisationMemberRoleResponseSchema)
|
||||||
|
.mutation(async ({ input, ctx }) => {
|
||||||
|
const { organisationId, userId, role } = input;
|
||||||
|
|
||||||
|
ctx.logger.info({
|
||||||
|
input: {
|
||||||
|
organisationId,
|
||||||
|
userId,
|
||||||
|
role,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const organisation = await prisma.organisation.findUnique({
|
||||||
|
where: {
|
||||||
|
id: organisationId,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
groups: {
|
||||||
|
where: {
|
||||||
|
type: OrganisationGroupType.INTERNAL_ORGANISATION,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
members: {
|
||||||
|
where: {
|
||||||
|
userId,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
organisationGroupMembers: {
|
||||||
|
include: {
|
||||||
|
group: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!organisation) {
|
||||||
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||||
|
message: 'Organisation not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const [member] = organisation.members;
|
||||||
|
|
||||||
|
if (!member) {
|
||||||
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||||
|
message: 'User is not a member of this organisation',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentOrganisationRole = getHighestOrganisationRoleInGroup(
|
||||||
|
member.organisationGroupMembers.flatMap((member) => member.group),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (role === 'OWNER') {
|
||||||
|
if (organisation.ownerUserId === userId) {
|
||||||
|
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
||||||
|
message: 'User is already the owner of this organisation',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentMemberGroup = organisation.groups.find(
|
||||||
|
(group) => group.organisationRole === currentOrganisationRole,
|
||||||
|
);
|
||||||
|
|
||||||
|
const adminGroup = organisation.groups.find(
|
||||||
|
(group) => group.organisationRole === OrganisationMemberRole.ADMIN,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!currentMemberGroup) {
|
||||||
|
ctx.logger.error({
|
||||||
|
message: '[CRITICAL]: Missing internal group',
|
||||||
|
organisationId,
|
||||||
|
userId,
|
||||||
|
role: currentOrganisationRole,
|
||||||
|
});
|
||||||
|
|
||||||
|
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
||||||
|
message: 'Current member group not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!adminGroup) {
|
||||||
|
ctx.logger.error({
|
||||||
|
message: '[CRITICAL]: Missing internal group',
|
||||||
|
organisationId,
|
||||||
|
userId,
|
||||||
|
targetRole: 'ADMIN',
|
||||||
|
});
|
||||||
|
|
||||||
|
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
||||||
|
message: 'Admin group not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await prisma.$transaction(async (tx) => {
|
||||||
|
await tx.organisation.update({
|
||||||
|
where: {
|
||||||
|
id: organisationId,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
ownerUserId: userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (currentOrganisationRole !== OrganisationMemberRole.ADMIN) {
|
||||||
|
await tx.organisationGroupMember.delete({
|
||||||
|
where: {
|
||||||
|
organisationMemberId_groupId: {
|
||||||
|
organisationMemberId: member.id,
|
||||||
|
groupId: currentMemberGroup.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await tx.organisationGroupMember.create({
|
||||||
|
data: {
|
||||||
|
id: generateDatabaseId('group_member'),
|
||||||
|
organisationMemberId: member.id,
|
||||||
|
groupId: adminGroup.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const targetRole = role as OrganisationMemberRole;
|
||||||
|
|
||||||
|
if (currentOrganisationRole === targetRole) {
|
||||||
|
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
||||||
|
message: 'User already has this role',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userId === organisation.ownerUserId && targetRole !== OrganisationMemberRole.ADMIN) {
|
||||||
|
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
||||||
|
message: 'Organisation owner must be an admin. Transfer ownership first.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentMemberGroup = organisation.groups.find(
|
||||||
|
(group) => group.organisationRole === currentOrganisationRole,
|
||||||
|
);
|
||||||
|
|
||||||
|
const newMemberGroup = organisation.groups.find(
|
||||||
|
(group) => group.organisationRole === targetRole,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!currentMemberGroup) {
|
||||||
|
ctx.logger.error({
|
||||||
|
message: '[CRITICAL]: Missing internal group',
|
||||||
|
organisationId,
|
||||||
|
userId,
|
||||||
|
role: currentOrganisationRole,
|
||||||
|
});
|
||||||
|
|
||||||
|
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
||||||
|
message: 'Current member group not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!newMemberGroup) {
|
||||||
|
ctx.logger.error({
|
||||||
|
message: '[CRITICAL]: Missing internal group',
|
||||||
|
organisationId,
|
||||||
|
userId,
|
||||||
|
targetRole,
|
||||||
|
});
|
||||||
|
|
||||||
|
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
||||||
|
message: 'New member group not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await prisma.$transaction(async (tx) => {
|
||||||
|
await tx.organisationGroupMember.delete({
|
||||||
|
where: {
|
||||||
|
organisationMemberId_groupId: {
|
||||||
|
organisationMemberId: member.id,
|
||||||
|
groupId: currentMemberGroup.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await tx.organisationGroupMember.create({
|
||||||
|
data: {
|
||||||
|
id: generateDatabaseId('group_member'),
|
||||||
|
organisationMemberId: member.id,
|
||||||
|
groupId: newMemberGroup.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import { OrganisationMemberRole } from '@prisma/client';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Admin-only role selection that includes OWNER as a special case.
|
||||||
|
* OWNER is not a database role but triggers ownership transfer.
|
||||||
|
*/
|
||||||
|
export const ZAdminRoleSelection = z.enum([
|
||||||
|
'OWNER',
|
||||||
|
OrganisationMemberRole.ADMIN,
|
||||||
|
OrganisationMemberRole.MANAGER,
|
||||||
|
OrganisationMemberRole.MEMBER,
|
||||||
|
]);
|
||||||
|
|
||||||
|
export type TAdminRoleSelection = z.infer<typeof ZAdminRoleSelection>;
|
||||||
|
|
||||||
|
export const ZUpdateOrganisationMemberRoleRequestSchema = z.object({
|
||||||
|
organisationId: z.string().min(1),
|
||||||
|
userId: z.number().min(1),
|
||||||
|
role: ZAdminRoleSelection,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const ZUpdateOrganisationMemberRoleResponseSchema = z.void();
|
||||||
|
|
||||||
|
export type TUpdateOrganisationMemberRoleRequest = z.infer<
|
||||||
|
typeof ZUpdateOrganisationMemberRoleRequestSchema
|
||||||
|
>;
|
||||||
|
export type TUpdateOrganisationMemberRoleResponse = z.infer<
|
||||||
|
typeof ZUpdateOrganisationMemberRoleResponseSchema
|
||||||
|
>;
|
||||||
Reference in New Issue
Block a user