Files
triggerdotdev--trigger.dev/apps/webapp/app/presenters/SelectBestProjectPresenter.server.ts
Matt Aitken 9ae0ca64af Fix edge cases with deleting last org/project (#892)
* If you have an org with no projects, it displays in the project dropdown with a “New project” button

* When creating a new org disable the button whilst it’s doing the request

* If an org already had any deleted projects it couldn’t be deleted…

* When selecting the best project, factor in deleted ones

* Don’t show the cancel button when creating a new org if there are no non-deleted projects

* If a project has already been deleted just return
2024-02-09 11:05:28 +00:00

51 lines
1.4 KiB
TypeScript

import { PrismaClient } from "@trigger.dev/database";
import { prisma } from "~/db.server";
import { getCurrentProjectId } from "~/services/currentProject.server";
export class SelectBestProjectPresenter {
#prismaClient: PrismaClient;
constructor(prismaClient: PrismaClient = prisma) {
this.#prismaClient = prismaClient;
}
public async call({ userId, request }: { userId: string; request: Request }) {
//try get current project from cookie
const projectId = await getCurrentProjectId(request);
if (projectId) {
const project = await this.#prismaClient.project.findUnique({
where: { id: projectId, deletedAt: null, organization: { members: { some: { userId } } } },
include: { organization: true },
});
if (project) {
return { project, organization: project.organization };
}
}
//failing that, we pick the project with the most jobs
const projects = await this.#prismaClient.project.findMany({
include: {
organization: true,
},
where: {
deletedAt: null,
organization: {
members: { some: { userId } },
},
},
orderBy: {
jobs: {
_count: "desc",
},
},
take: 1,
});
if (projects.length === 0) {
throw new Response("Not Found", { status: 404 });
}
return { project: projects[0], organization: projects[0].organization };
}
}