Files
triggerdotdev--trigger.dev/apps/webapp/app/services/deleteOrganization.server.ts
T
Matt Aitken 6cf86d5916 Deleting v2 dashboard/api (#1628)
* Delete v2 Stripe routes

* Delete v2 billing/usage pages

* Delete v2 integration pages

* Delete v2 project pages

* Deleted a load of components and services

* Deleted a load more components, presenters and services

* Deleted another 100 files or so…

* Removed old v2 paths

* Removed named icons from form titles

* Removed more string icons

* Delete NamedIcon

* Fixed some type errors

* Delete endpointApi

* Removed v2 from core/sdk

* Post merge fixes

* added explicit return types

* using the new sdk export without v3

* Delete old v2 file

* Added explicit return types because TS was complaining…

* Don’t export RuntimeEnvironmentType from two core files. Was causing TS issue

* Fix for removal of NamedIcon in new route

* Removed strange eslintrc rule

* Use the new redis client

---------

Co-authored-by: James Ritchie <james@trigger.dev>
2025-03-08 14:53:06 +00:00

87 lines
2.6 KiB
TypeScript

import { DateFormatter } from "@internationalized/date";
import { PrismaClient } from "@trigger.dev/database";
import { prisma } from "~/db.server";
import { featuresForRequest } from "~/features.server";
import { DeleteProjectService } from "./deleteProject.server";
import { getCurrentPlan } from "./platform.v3.server";
export class DeleteOrganizationService {
#prismaClient: PrismaClient;
constructor(prismaClient: PrismaClient = prisma) {
this.#prismaClient = prismaClient;
}
public async call({
organizationSlug,
userId,
request,
}: {
organizationSlug: string;
userId: string;
request: Request;
}) {
const organization = await this.#prismaClient.organization.findFirst({
include: {
projects: true,
members: true,
},
where: {
slug: organizationSlug,
members: { some: { userId: userId } },
},
});
if (!organization) {
throw new Error("Organization not found");
}
if (organization.deletedAt) {
throw new Error("Organization already deleted");
}
//Check if they have an active subscription
const { isManagedCloud } = featuresForRequest(request);
const currentPlan = isManagedCloud ? await getCurrentPlan(organization.id) : undefined;
if (currentPlan && currentPlan.v3Subscription && currentPlan.v3Subscription.isPaying) {
//they've cancelled and that date hasn't passed yet
if (
currentPlan.v3Subscription.canceledAt &&
new Date(currentPlan.v3Subscription.canceledAt) > new Date()
) {
//a dateformatter that produces results like "Jan 1 2024"
const dateFormatter = new DateFormatter("en-us", {
year: "numeric",
month: "short",
day: "numeric",
});
throw new Error(
`This Organization has a canceled subscription. You can delete it when the cancelation date (${dateFormatter.format(
new Date(currentPlan.v3Subscription.canceledAt)
)}) is in the past.`
);
}
throw new Error("You can't delete an Organization that has an active subscription");
}
// loop through the projects and delete them
const projectDeleteService = new DeleteProjectService();
for (const project of organization.projects) {
await projectDeleteService.call({ projectId: project.id, userId });
}
//mark the organization as deleted
await this.#prismaClient.organization.update({
where: {
id: organization.id,
},
data: {
runsEnabled: false,
deletedAt: new Date(),
},
});
}
}