mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-06-15 20:25:23 +02:00
119 lines
3.5 KiB
TypeScript
119 lines
3.5 KiB
TypeScript
import {
|
|
findApplicationById,
|
|
findPreviewDeploymentById,
|
|
findPreviewDeploymentsByApplicationId,
|
|
IS_CLOUD,
|
|
removePreviewDeployment,
|
|
} from "@dokploy/server";
|
|
import { TRPCError } from "@trpc/server";
|
|
import { z } from "zod";
|
|
import { apiFindAllByApplication } from "@/server/db/schema";
|
|
import type { DeploymentJob } from "@/server/queues/queue-types";
|
|
import { myQueue } from "@/server/queues/queueSetup";
|
|
import { deploy } from "@/server/utils/deploy";
|
|
import { createTRPCRouter, protectedProcedure } from "../trpc";
|
|
|
|
export const previewDeploymentRouter = createTRPCRouter({
|
|
all: protectedProcedure
|
|
.input(apiFindAllByApplication)
|
|
.query(async ({ input, ctx }) => {
|
|
const application = await findApplicationById(input.applicationId);
|
|
if (
|
|
application.environment.project.organizationId !==
|
|
ctx.session.activeOrganizationId
|
|
) {
|
|
throw new TRPCError({
|
|
code: "UNAUTHORIZED",
|
|
message: "You are not authorized to access this application",
|
|
});
|
|
}
|
|
return await findPreviewDeploymentsByApplicationId(input.applicationId);
|
|
}),
|
|
delete: protectedProcedure
|
|
.input(z.object({ previewDeploymentId: z.string() }))
|
|
.mutation(async ({ input, ctx }) => {
|
|
const previewDeployment = await findPreviewDeploymentById(
|
|
input.previewDeploymentId,
|
|
);
|
|
if (
|
|
previewDeployment.application.environment.project.organizationId !==
|
|
ctx.session.activeOrganizationId
|
|
) {
|
|
throw new TRPCError({
|
|
code: "UNAUTHORIZED",
|
|
message: "You are not authorized to delete this preview deployment",
|
|
});
|
|
}
|
|
await removePreviewDeployment(input.previewDeploymentId);
|
|
return true;
|
|
}),
|
|
one: protectedProcedure
|
|
.input(z.object({ previewDeploymentId: z.string() }))
|
|
.query(async ({ input, ctx }) => {
|
|
const previewDeployment = await findPreviewDeploymentById(
|
|
input.previewDeploymentId,
|
|
);
|
|
if (
|
|
previewDeployment.application.environment.project.organizationId !==
|
|
ctx.session.activeOrganizationId
|
|
) {
|
|
throw new TRPCError({
|
|
code: "UNAUTHORIZED",
|
|
message: "You are not authorized to access this preview deployment",
|
|
});
|
|
}
|
|
return previewDeployment;
|
|
}),
|
|
redeploy: protectedProcedure
|
|
.input(
|
|
z.object({
|
|
previewDeploymentId: z.string(),
|
|
title: z.string().optional(),
|
|
description: z.string().optional(),
|
|
}),
|
|
)
|
|
.mutation(async ({ input, ctx }) => {
|
|
const previewDeployment = await findPreviewDeploymentById(
|
|
input.previewDeploymentId,
|
|
);
|
|
if (
|
|
previewDeployment.application.environment.project.organizationId !==
|
|
ctx.session.activeOrganizationId
|
|
) {
|
|
throw new TRPCError({
|
|
code: "UNAUTHORIZED",
|
|
message: "You are not authorized to redeploy this preview deployment",
|
|
});
|
|
}
|
|
const application = await findApplicationById(
|
|
previewDeployment.applicationId,
|
|
);
|
|
const jobData: DeploymentJob = {
|
|
applicationId: previewDeployment.applicationId,
|
|
titleLog: input.title || "Rebuild Preview Deployment",
|
|
descriptionLog: input.description || "",
|
|
type: "redeploy",
|
|
applicationType: "application-preview",
|
|
previewDeploymentId: input.previewDeploymentId,
|
|
server: !!application.serverId,
|
|
};
|
|
|
|
if (IS_CLOUD && application.serverId) {
|
|
jobData.serverId = application.serverId;
|
|
deploy(jobData).catch((error) => {
|
|
console.error("Background deployment failed:", error);
|
|
});
|
|
return true;
|
|
}
|
|
await myQueue.add(
|
|
"deployments",
|
|
{ ...jobData },
|
|
{
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
},
|
|
);
|
|
return true;
|
|
}),
|
|
});
|