refactor(dokploy): prevent to create projects when the user doesn't have servers buyed

This commit is contained in:
Mauricio Siu
2024-10-25 23:44:39 -06:00
parent 1f208f5f5b
commit 742e5a244d

View File

@@ -20,10 +20,12 @@ import { and, desc, eq, sql } from "drizzle-orm";
import type { AnyPgColumn } from "drizzle-orm/pg-core"; import type { AnyPgColumn } from "drizzle-orm/pg-core";
import { import {
IS_CLOUD,
addNewProject, addNewProject,
checkProjectAccess, checkProjectAccess,
createProject, createProject,
deleteProject, deleteProject,
findAdminById,
findProjectById, findProjectById,
findUserByAuthId, findUserByAuthId,
updateProjectById, updateProjectById,
@@ -38,6 +40,15 @@ export const projectRouter = createTRPCRouter({
await checkProjectAccess(ctx.user.authId, "create"); await checkProjectAccess(ctx.user.authId, "create");
} }
const admin = await findAdminById(ctx.user.adminId);
if (admin.serversQuantity === 0 && IS_CLOUD) {
throw new TRPCError({
code: "NOT_FOUND",
message: "No servers available, Please subscribe to a plan",
});
}
const project = await createProject(input, ctx.user.adminId); const project = await createProject(input, ctx.user.adminId);
if (ctx.user.rol === "user") { if (ctx.user.rol === "user") {
await addNewProject(ctx.user.authId, project.projectId); await addNewProject(ctx.user.authId, project.projectId);
@@ -47,7 +58,7 @@ export const projectRouter = createTRPCRouter({
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
code: "BAD_REQUEST", code: "BAD_REQUEST",
message: "Error to create the project", message: `Error to create the project: ${error instanceof Error ? error.message : error}`,
cause: error, cause: error,
}); });
} }
@@ -128,9 +139,9 @@ export const projectRouter = createTRPCRouter({
)})`, )})`,
with: { with: {
applications: { applications: {
where: buildServiceFilter( where: and(
applications.applicationId, buildServiceFilter(applications.applicationId, accesedServices),
accesedServices, eq(projects.adminId, ctx.user.adminId),
), ),
with: { domains: true }, with: { domains: true },
}, },
@@ -230,5 +241,5 @@ function buildServiceFilter(fieldName: AnyPgColumn, accesedServices: string[]) {
accesedServices.map((serviceId) => sql`${serviceId}`), accesedServices.map((serviceId) => sql`${serviceId}`),
sql`, `, sql`, `,
)})` )})`
: sql`1 = 0`; // Always false condition : sql`1 = 0`;
} }