bugfix: a new flow added to re-enable disabled staffs.
This commit is contained in:
@@ -80,6 +80,19 @@ export class StaffController {
|
|||||||
return this.staffService.updateMember(req.user.id, organizationId, membershipId, dto);
|
return this.staffService.updateMember(req.user.id, organizationId, membershipId, dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Patch('members/:membershipId/enable')
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@ApiOperation({
|
||||||
|
summary: 'Re-enable a disabled staff member (uses one plan seat; no new invitation)',
|
||||||
|
})
|
||||||
|
enableMember(
|
||||||
|
@Req() req: { user: { id: string; organizationId?: string } },
|
||||||
|
@Param('membershipId') membershipId: string,
|
||||||
|
) {
|
||||||
|
const organizationId = this.staffService.getOrganizationIdFromUser(req.user);
|
||||||
|
return this.staffService.enableMember(req.user.id, organizationId, membershipId);
|
||||||
|
}
|
||||||
|
|
||||||
@Patch('members/:membershipId/disable')
|
@Patch('members/:membershipId/disable')
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@ApiOperation({
|
@ApiOperation({
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import * as bcrypt from 'bcrypt';
|
import * as bcrypt from 'bcrypt';
|
||||||
import { createHash, randomBytes } from 'crypto';
|
import { createHash, randomBytes } from 'crypto';
|
||||||
|
import { Prisma } from '@prisma/client';
|
||||||
import { PrismaService } from '../../../prisma/prisma.service';
|
import { PrismaService } from '../../../prisma/prisma.service';
|
||||||
import { AcceptStaffInviteDto } from './dto/accept-staff-invite.dto';
|
import { AcceptStaffInviteDto } from './dto/accept-staff-invite.dto';
|
||||||
import { isUnlimitedSeats, normalizeTabPermissions } from '../../common/permissions';
|
import { isUnlimitedSeats, normalizeTabPermissions } from '../../common/permissions';
|
||||||
@@ -399,6 +400,50 @@ export class StaffService {
|
|||||||
return { success: true, message: 'Member updated' };
|
return { success: true, message: 'Member updated' };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async enableMember(actorUserId: string, organizationId: string, membershipId: string) {
|
||||||
|
const actor = await this.getActorMembership(actorUserId, organizationId);
|
||||||
|
if (!actor || !this.canEditStaff(actor)) {
|
||||||
|
throw new ForbiddenException('You cannot manage staff');
|
||||||
|
}
|
||||||
|
|
||||||
|
const target = await this.prisma.membership.findFirst({
|
||||||
|
where: { id: membershipId, organizationId },
|
||||||
|
include: {
|
||||||
|
invitations: { orderBy: { createdAt: 'desc' }, take: 1 },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!target) {
|
||||||
|
throw new NotFoundException('Member not found');
|
||||||
|
}
|
||||||
|
if (target.isOwner) {
|
||||||
|
throw new ForbiddenException('Cannot enable the organization owner');
|
||||||
|
}
|
||||||
|
if (target.isActive) {
|
||||||
|
throw new BadRequestException('This member is already active');
|
||||||
|
}
|
||||||
|
|
||||||
|
const invitation = target.invitations[0];
|
||||||
|
if (invitation && !invitation.acceptedAt) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
'This member has not completed their invitation yet. Share the invite link instead.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.prisma.$transaction(async (tx) => {
|
||||||
|
await this.assertOrganizationHasAvailableSeat(organizationId, tx);
|
||||||
|
await tx.membership.update({
|
||||||
|
where: { id: membershipId },
|
||||||
|
data: { isActive: true },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message: 'Member enabled. They can sign in to this organization again.',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async disableMember(actorUserId: string, organizationId: string, membershipId: string) {
|
async disableMember(actorUserId: string, organizationId: string, membershipId: string) {
|
||||||
const actor = await this.getActorMembership(actorUserId, organizationId);
|
const actor = await this.getActorMembership(actorUserId, organizationId);
|
||||||
if (!actor || !this.canEditStaff(actor)) {
|
if (!actor || !this.canEditStaff(actor)) {
|
||||||
@@ -459,6 +504,37 @@ export class StaffService {
|
|||||||
return { success: true, message: 'Member removed' };
|
return { success: true, message: 'Member removed' };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async assertOrganizationHasAvailableSeat(
|
||||||
|
organizationId: string,
|
||||||
|
db: Prisma.TransactionClient | PrismaService = this.prisma,
|
||||||
|
) {
|
||||||
|
const org = await db.organization.findUnique({
|
||||||
|
where: { id: organizationId },
|
||||||
|
include: { plan: true },
|
||||||
|
});
|
||||||
|
if (!org) {
|
||||||
|
throw new NotFoundException('Organization not found');
|
||||||
|
}
|
||||||
|
if (!org.plan) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
'This organization has no active subscription. Please choose a plan before adding staff.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const maxUsers = org.plan.maxUsers;
|
||||||
|
const seatsUsed = await db.membership.count({
|
||||||
|
where: {
|
||||||
|
organizationId,
|
||||||
|
OR: [{ isOwner: true }, { isActive: true }],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!isUnlimitedSeats(maxUsers) && seatsUsed >= maxUsers) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
`Your plan allows ${maxUsers} team members. Free a seat by disabling another member or upgrade your plan.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async getActorMembership(userId: string, organizationId: string) {
|
private async getActorMembership(userId: string, organizationId: string) {
|
||||||
return this.prisma.membership.findFirst({
|
return this.prisma.membership.findFirst({
|
||||||
where: { userId, organizationId },
|
where: { userId, organizationId },
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import {
|
|||||||
formatAccessSummary,
|
formatAccessSummary,
|
||||||
type FeaturePermState,
|
type FeaturePermState,
|
||||||
} from '../../../components/staff/staff-permission-form';
|
} from '../../../components/staff/staff-permission-form';
|
||||||
import { Pencil, Trash2, Copy, Check, X, UserX } from 'lucide-react';
|
import { Pencil, Trash2, Copy, Check, X, UserX, UserCheck } from 'lucide-react';
|
||||||
import { DialogCloseButton } from '@/components/ui/shared/DialogCloseButton';
|
import { DialogCloseButton } from '@/components/ui/shared/DialogCloseButton';
|
||||||
import { useAuth } from '@/lib/hooks/useAuth';
|
import { useAuth } from '@/lib/hooks/useAuth';
|
||||||
import { staffApi, type StaffMemberDto } from '@/lib/api/staff';
|
import { staffApi, type StaffMemberDto } from '@/lib/api/staff';
|
||||||
@@ -67,6 +67,10 @@ function canDisableStaff(member: StaffMemberDto): boolean {
|
|||||||
return !member.isOwner && member.isActive;
|
return !member.isOwner && member.isActive;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function canEnableStaff(member: StaffMemberDto): boolean {
|
||||||
|
return !member.isOwner && member.invitationStatus === 'DISABLED';
|
||||||
|
}
|
||||||
|
|
||||||
function PermissionGrid({
|
function PermissionGrid({
|
||||||
state,
|
state,
|
||||||
onChange,
|
onChange,
|
||||||
@@ -161,6 +165,8 @@ export default function StaffPage() {
|
|||||||
const [editLoading, setEditLoading] = useState(false);
|
const [editLoading, setEditLoading] = useState(false);
|
||||||
const [disableTarget, setDisableTarget] = useState<StaffMemberDto | null>(null);
|
const [disableTarget, setDisableTarget] = useState<StaffMemberDto | null>(null);
|
||||||
const [disablingMembershipId, setDisablingMembershipId] = useState<string | null>(null);
|
const [disablingMembershipId, setDisablingMembershipId] = useState<string | null>(null);
|
||||||
|
const [enableTarget, setEnableTarget] = useState<StaffMemberDto | null>(null);
|
||||||
|
const [enablingMembershipId, setEnablingMembershipId] = useState<string | null>(null);
|
||||||
|
|
||||||
const canEdit = useMemo(() => canEditStaff(currentOrganization), [currentOrganization]);
|
const canEdit = useMemo(() => canEditStaff(currentOrganization), [currentOrganization]);
|
||||||
const hasActivePlan = Boolean(currentOrganization?.plan);
|
const hasActivePlan = Boolean(currentOrganization?.plan);
|
||||||
@@ -170,6 +176,12 @@ export default function StaffPage() {
|
|||||||
return seats.used >= seats.limit;
|
return seats.used >= seats.limit;
|
||||||
}, [seats]);
|
}, [seats]);
|
||||||
|
|
||||||
|
const hasAvailableSeat = useMemo(() => {
|
||||||
|
if (!seats || seats.unlimited) return true;
|
||||||
|
if (seats.limit == null) return true;
|
||||||
|
return seats.used < seats.limit;
|
||||||
|
}, [seats]);
|
||||||
|
|
||||||
const load = useCallback(async () => {
|
const load = useCallback(async () => {
|
||||||
toast.setError('');
|
toast.setError('');
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
@@ -352,6 +364,23 @@ export default function StaffPage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function confirmEnableMember() {
|
||||||
|
if (!enableTarget || !canEnableStaff(enableTarget) || !hasAvailableSeat) return;
|
||||||
|
|
||||||
|
setEnablingMembershipId(enableTarget.id);
|
||||||
|
toast.setError('');
|
||||||
|
try {
|
||||||
|
await staffApi.enableMember(enableTarget.id);
|
||||||
|
toast.showSuccess(`${enableTarget.name} was enabled and can sign in again.`);
|
||||||
|
setEnableTarget(null);
|
||||||
|
await load();
|
||||||
|
} catch (e) {
|
||||||
|
toast.showError(formatApiErrorMessage(e, 'Failed to enable member.'));
|
||||||
|
} finally {
|
||||||
|
setEnablingMembershipId(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!currentOrganization || !canViewStaff(currentOrganization)) {
|
if (!currentOrganization || !canViewStaff(currentOrganization)) {
|
||||||
return (
|
return (
|
||||||
<p className="text-sm text-text-secondary">Redirecting…</p>
|
<p className="text-sm text-text-secondary">Redirecting…</p>
|
||||||
@@ -547,6 +576,25 @@ export default function StaffPage() {
|
|||||||
)}
|
)}
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
|
{canEnableStaff(m) && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={`p-2 rounded-md ${
|
||||||
|
canEdit
|
||||||
|
? 'text-text-secondary hover:bg-background-card/80 hover:text-primary'
|
||||||
|
: 'text-text-muted opacity-50 cursor-not-allowed'
|
||||||
|
}`}
|
||||||
|
aria-label="Enable member"
|
||||||
|
disabled={!canEdit || enablingMembershipId === m.id}
|
||||||
|
title="Enable member (uses a seat)"
|
||||||
|
onClick={() => {
|
||||||
|
if (!canEdit) return;
|
||||||
|
setEnableTarget(m);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<UserCheck className="w-4 h-4" />
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
{canDisableStaff(m) && (
|
{canDisableStaff(m) && (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
@@ -660,6 +708,66 @@ export default function StaffPage() {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{enableTarget && (
|
||||||
|
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/55">
|
||||||
|
<div
|
||||||
|
className="surface-card w-full max-w-md p-5 space-y-4 shadow-xl"
|
||||||
|
role="dialog"
|
||||||
|
aria-modal="true"
|
||||||
|
aria-labelledby="enable-staff-title"
|
||||||
|
>
|
||||||
|
<div className="flex items-start justify-between gap-2">
|
||||||
|
<h2 id="enable-staff-title" className="text-lg font-semibold text-text-primary pr-2">
|
||||||
|
Enable team member
|
||||||
|
</h2>
|
||||||
|
<DialogCloseButton
|
||||||
|
onClick={() => {
|
||||||
|
if (enablingMembershipId) return;
|
||||||
|
setEnableTarget(null);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<p className="text-sm text-text-secondary">
|
||||||
|
Enable <span className="font-medium text-text-primary">{enableTarget.name}</span> (
|
||||||
|
{enableTarget.email})?
|
||||||
|
</p>
|
||||||
|
<ul className="text-sm text-text-secondary space-y-2 list-disc pl-5">
|
||||||
|
<li>They can sign in to this organization again with their existing account.</li>
|
||||||
|
<li>No new invitation is sent and no data was removed while they were disabled.</li>
|
||||||
|
<li>
|
||||||
|
Enabling uses <span className="text-text-primary font-medium">one seat</span> on your
|
||||||
|
plan.
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
{!hasAvailableSeat && (
|
||||||
|
<p className="text-sm text-amber-600 dark:text-amber-400">
|
||||||
|
No seats are available. Disable another member or upgrade your plan before enabling
|
||||||
|
this person.
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
<div className="flex justify-end gap-2 pt-1">
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
disabled={Boolean(enablingMembershipId)}
|
||||||
|
onClick={() => setEnableTarget(null)}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="primary"
|
||||||
|
isLoading={enablingMembershipId === enableTarget.id}
|
||||||
|
disabled={Boolean(enablingMembershipId) || !hasAvailableSeat}
|
||||||
|
onClick={() => void confirmEnableMember()}
|
||||||
|
>
|
||||||
|
Enable member
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
{disableTarget && (
|
{disableTarget && (
|
||||||
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/55">
|
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/55">
|
||||||
<div
|
<div
|
||||||
|
|||||||
@@ -8,10 +8,18 @@ import { Input } from '@/components/ui/shared/Input';
|
|||||||
import { Button } from '@/components/ui/shared/Button';
|
import { Button } from '@/components/ui/shared/Button';
|
||||||
|
|
||||||
export function OrganizationSelectorContent() {
|
export function OrganizationSelectorContent() {
|
||||||
const { organizations, selectOrganization, createOrganization, isLoading, error, clearError } = useAuth();
|
const {
|
||||||
|
organizations,
|
||||||
|
currentOrganization,
|
||||||
|
selectOrganization,
|
||||||
|
createOrganization,
|
||||||
|
isLoading,
|
||||||
|
error,
|
||||||
|
clearError,
|
||||||
|
} = useAuth();
|
||||||
const canCreateOrganization = useMemo(
|
const canCreateOrganization = useMemo(
|
||||||
() => canUserCreateOrganization(organizations),
|
() => canCreateOrganizationFromCurrentOrg(currentOrganization),
|
||||||
[organizations],
|
[currentOrganization],
|
||||||
);
|
);
|
||||||
const [isCreateOpen, setIsCreateOpen] = useState(false);
|
const [isCreateOpen, setIsCreateOpen] = useState(false);
|
||||||
const [organizationName, setOrganizationName] = useState('');
|
const [organizationName, setOrganizationName] = useState('');
|
||||||
|
|||||||
@@ -107,6 +107,13 @@ export const staffApi = {
|
|||||||
return response.data;
|
return response.data;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
enableMember: async (
|
||||||
|
membershipId: string,
|
||||||
|
): Promise<{ success: boolean; message: string }> => {
|
||||||
|
const response = await apiClient.patch(`/staff/members/${membershipId}/enable`);
|
||||||
|
return response.data;
|
||||||
|
},
|
||||||
|
|
||||||
removeMember: async (
|
removeMember: async (
|
||||||
membershipId: string,
|
membershipId: string,
|
||||||
): Promise<{ success: boolean; message: string }> => {
|
): Promise<{ success: boolean; message: string }> => {
|
||||||
|
|||||||
Reference in New Issue
Block a user