mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
chore: make code re-usable
This commit is contained in:
@ -1,9 +1,8 @@
|
||||
import { ORGANISATION_MEMBER_ROLE_PERMISSIONS_MAP } from '@documenso/lib/constants/organisations';
|
||||
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||
import { deletedAccountServiceAccount } from '@documenso/lib/server-only/user/service-accounts/deleted-account';
|
||||
import { handleDocumentOwnershipOnDeletion } from '@documenso/lib/server-only/document/move-documents-to-owner-or-service';
|
||||
import { buildOrganisationWhereQuery } from '@documenso/lib/utils/organisations';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
import { DocumentStatus } from '@documenso/prisma/client';
|
||||
|
||||
import { authenticatedProcedure } from '../trpc';
|
||||
import {
|
||||
@ -18,8 +17,6 @@ export const deleteOrganisationRoute = authenticatedProcedure
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { organisationId } = input;
|
||||
const { user } = ctx;
|
||||
const serviceAccount = await deletedAccountServiceAccount();
|
||||
const serviceAccountTeam = serviceAccount.ownedOrganisations[0].teams[0];
|
||||
|
||||
ctx.logger.info({
|
||||
input: {
|
||||
@ -59,66 +56,13 @@ export const deleteOrganisationRoute = authenticatedProcedure
|
||||
});
|
||||
}
|
||||
|
||||
const organisationOwner = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: organisation.owner.id,
|
||||
},
|
||||
include: {
|
||||
ownedOrganisations: {
|
||||
include: {
|
||||
teams: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const documentIds = organisation.teams.flatMap((team) => team.documents.map((doc) => doc.id));
|
||||
|
||||
if (documentIds.length > 0) {
|
||||
await prisma.document.deleteMany({
|
||||
where: {
|
||||
id: {
|
||||
in: documentIds,
|
||||
},
|
||||
status: DocumentStatus.DRAFT,
|
||||
},
|
||||
if (documentIds && documentIds.length > 0) {
|
||||
await handleDocumentOwnershipOnDeletion({
|
||||
documentIds,
|
||||
organisationOwnerId: organisation.owner.id,
|
||||
});
|
||||
|
||||
if (organisationOwner && organisationOwner.ownedOrganisations.length > 0) {
|
||||
const ownerPersonalTeam = organisationOwner.ownedOrganisations[0].teams[0];
|
||||
|
||||
await prisma.document.updateMany({
|
||||
where: {
|
||||
id: {
|
||||
in: documentIds,
|
||||
},
|
||||
status: {
|
||||
not: DocumentStatus.DRAFT,
|
||||
},
|
||||
},
|
||||
data: {
|
||||
userId: organisationOwner.id,
|
||||
teamId: ownerPersonalTeam.id,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
});
|
||||
} else {
|
||||
await prisma.document.updateMany({
|
||||
where: {
|
||||
id: {
|
||||
in: documentIds,
|
||||
},
|
||||
status: {
|
||||
not: DocumentStatus.DRAFT,
|
||||
},
|
||||
},
|
||||
data: {
|
||||
userId: serviceAccount.id,
|
||||
teamId: serviceAccountTeam.id,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await prisma.organisation.delete({
|
||||
|
||||
Reference in New Issue
Block a user