mirror of
https://github.com/documenso/documenso.git
synced 2026-07-22 16:03:39 +10:00
138d663c25
Merge origin/main into feat/external-2fa-codes. Resolve formatting conflicts caused by biome rollout; preserve both feature streams: PR's external 2FA token + signing-session 2FA proof additions plus main's RateLimit/RecipientExpired/signingReminders/date-auto-insert. In complete-document-with-token.ts, drop the duplicate early field-fetching block introduced when main moved that logic later with date auto-insert support; keep the EXTERNAL_TWO_FACTOR_AUTH check using derivedRecipientActionAuth.
102 lines
2.7 KiB
TypeScript
102 lines
2.7 KiB
TypeScript
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
|
import { prisma } from '@documenso/prisma';
|
|
import { OrganisationGroupType } from '@prisma/client';
|
|
|
|
import { adminProcedure } from '../trpc';
|
|
import { ZDeleteAdminTeamMemberRequestSchema, ZDeleteAdminTeamMemberResponseSchema } from './delete-team-member.types';
|
|
|
|
export const deleteAdminTeamMemberRoute = adminProcedure
|
|
.input(ZDeleteAdminTeamMemberRequestSchema)
|
|
.output(ZDeleteAdminTeamMemberResponseSchema)
|
|
.mutation(async ({ ctx, input }) => {
|
|
const { teamId, memberId } = input;
|
|
|
|
ctx.logger.info({
|
|
input: {
|
|
teamId,
|
|
memberId,
|
|
},
|
|
});
|
|
|
|
const team = await prisma.team.findUnique({
|
|
where: {
|
|
id: teamId,
|
|
},
|
|
include: {
|
|
organisation: {
|
|
select: {
|
|
ownerUserId: true,
|
|
},
|
|
},
|
|
teamGroups: {
|
|
where: {
|
|
organisationGroup: {
|
|
type: OrganisationGroupType.INTERNAL_TEAM,
|
|
organisationGroupMembers: {
|
|
some: {
|
|
organisationMember: {
|
|
id: memberId,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!team) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
message: 'Team not found',
|
|
});
|
|
}
|
|
|
|
const teamGroupToRemoveMemberFrom = team.teamGroups[0];
|
|
|
|
if (!teamGroupToRemoveMemberFrom) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
message: 'Member is not directly assigned to this team. Inherited members cannot be removed here.',
|
|
});
|
|
}
|
|
|
|
const member = await prisma.organisationMember.findUnique({
|
|
where: {
|
|
id: memberId,
|
|
},
|
|
select: {
|
|
userId: true,
|
|
},
|
|
});
|
|
|
|
if (!member) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
|
message: 'Member not found',
|
|
});
|
|
}
|
|
|
|
await prisma.$transaction(async (tx) => {
|
|
// Removing a user from a single team drops their INTERNAL_TEAM
|
|
// OrganisationGroupMember link, but Envelope rows they authored in this
|
|
// team still point at their userId. Reassign to the org owner so those
|
|
// envelopes remain reachable after the member loses team access.
|
|
await tx.envelope.updateMany({
|
|
where: {
|
|
userId: member.userId,
|
|
teamId,
|
|
},
|
|
data: {
|
|
userId: team.organisation.ownerUserId,
|
|
},
|
|
});
|
|
|
|
await tx.organisationGroupMember.delete({
|
|
where: {
|
|
organisationMemberId_groupId: {
|
|
organisationMemberId: memberId,
|
|
groupId: teamGroupToRemoveMemberFrom.organisationGroupId,
|
|
},
|
|
},
|
|
});
|
|
});
|
|
});
|