mirror of
https://github.com/documenso/documenso.git
synced 2025-11-12 07:43:16 +10:00
81 lines
2.3 KiB
TypeScript
81 lines
2.3 KiB
TypeScript
import { syncMemberCountWithStripeSeatPlan } from '@documenso/ee/server-only/stripe/update-subscription-item-quantity';
|
|
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
|
import { jobs } from '@documenso/lib/jobs/client';
|
|
import { validateIfSubscriptionIsRequired } from '@documenso/lib/utils/billing';
|
|
import { buildOrganisationWhereQuery } from '@documenso/lib/utils/organisations';
|
|
import { prisma } from '@documenso/prisma';
|
|
import { OrganisationMemberInviteStatus } from '@documenso/prisma/client';
|
|
|
|
import { authenticatedProcedure } from '../trpc';
|
|
import {
|
|
ZLeaveOrganisationRequestSchema,
|
|
ZLeaveOrganisationResponseSchema,
|
|
} from './leave-organisation.types';
|
|
|
|
export const leaveOrganisationRoute = authenticatedProcedure
|
|
.input(ZLeaveOrganisationRequestSchema)
|
|
.output(ZLeaveOrganisationResponseSchema)
|
|
.mutation(async ({ ctx, input }) => {
|
|
const { organisationId } = input;
|
|
const userId = ctx.user.id;
|
|
|
|
ctx.logger.info({
|
|
input: {
|
|
organisationId,
|
|
},
|
|
});
|
|
|
|
const organisation = await prisma.organisation.findFirst({
|
|
where: buildOrganisationWhereQuery({ organisationId, userId }),
|
|
include: {
|
|
organisationClaim: true,
|
|
subscription: true,
|
|
invites: {
|
|
where: {
|
|
status: OrganisationMemberInviteStatus.PENDING,
|
|
},
|
|
select: {
|
|
id: true,
|
|
},
|
|
},
|
|
members: {
|
|
select: {
|
|
id: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!organisation) {
|
|
throw new AppError(AppErrorCode.NOT_FOUND);
|
|
}
|
|
|
|
const { organisationClaim } = organisation;
|
|
|
|
const subscription = validateIfSubscriptionIsRequired(organisation.subscription);
|
|
|
|
const inviteCount = organisation.invites.length;
|
|
const newMemberCount = organisation.members.length + inviteCount - 1;
|
|
|
|
if (subscription) {
|
|
await syncMemberCountWithStripeSeatPlan(subscription, organisationClaim, newMemberCount);
|
|
}
|
|
|
|
await prisma.organisationMember.delete({
|
|
where: {
|
|
userId_organisationId: {
|
|
userId,
|
|
organisationId,
|
|
},
|
|
},
|
|
});
|
|
|
|
await jobs.triggerJob({
|
|
name: 'send.organisation-member-left.email',
|
|
payload: {
|
|
organisationId: organisation.id,
|
|
memberUserId: userId,
|
|
},
|
|
});
|
|
});
|