|
1 | 1 | import { |
| 2 | + checkServiceAccess, |
2 | 3 | createMount, |
3 | 4 | deleteMount, |
4 | 5 | findApplicationById, |
| 6 | + findComposeById, |
| 7 | + findMariadbById, |
| 8 | + findMongoById, |
5 | 9 | findMountById, |
6 | 10 | findMountOrganizationId, |
| 11 | + findMountsByApplicationId, |
| 12 | + findMySqlById, |
| 13 | + findPostgresById, |
| 14 | + findRedisById, |
7 | 15 | getServiceContainer, |
8 | 16 | updateMount, |
9 | 17 | } from "@dokploy/server"; |
| 18 | +import type { ServiceType } from "@dokploy/server/db/schema/mount"; |
10 | 19 | import { TRPCError } from "@trpc/server"; |
11 | 20 | import { z } from "zod"; |
12 | 21 | import { |
13 | 22 | apiCreateMount, |
| 23 | + apiFindMountByApplicationId, |
14 | 24 | apiFindOneMount, |
15 | 25 | apiRemoveMount, |
16 | 26 | apiUpdateMount, |
17 | 27 | } from "@/server/db/schema"; |
18 | 28 | import { createTRPCRouter, protectedProcedure } from "../trpc"; |
19 | 29 |
|
| 30 | +async function getServiceOrganizationId( |
| 31 | + serviceId: string, |
| 32 | + serviceType: ServiceType, |
| 33 | +): Promise<string | null> { |
| 34 | + switch (serviceType) { |
| 35 | + case "application": { |
| 36 | + const app = await findApplicationById(serviceId); |
| 37 | + return app?.environment?.project?.organizationId ?? null; |
| 38 | + } |
| 39 | + case "postgres": { |
| 40 | + const postgres = await findPostgresById(serviceId); |
| 41 | + return postgres?.environment?.project?.organizationId ?? null; |
| 42 | + } |
| 43 | + case "mariadb": { |
| 44 | + const mariadb = await findMariadbById(serviceId); |
| 45 | + return mariadb?.environment?.project?.organizationId ?? null; |
| 46 | + } |
| 47 | + case "mongo": { |
| 48 | + const mongo = await findMongoById(serviceId); |
| 49 | + return mongo?.environment?.project?.organizationId ?? null; |
| 50 | + } |
| 51 | + case "mysql": { |
| 52 | + const mysql = await findMySqlById(serviceId); |
| 53 | + return mysql?.environment?.project?.organizationId ?? null; |
| 54 | + } |
| 55 | + case "redis": { |
| 56 | + const redis = await findRedisById(serviceId); |
| 57 | + return redis?.environment?.project?.organizationId ?? null; |
| 58 | + } |
| 59 | + case "compose": { |
| 60 | + const compose = await findComposeById(serviceId); |
| 61 | + return compose?.environment?.project?.organizationId ?? null; |
| 62 | + } |
| 63 | + default: |
| 64 | + return null; |
| 65 | + } |
| 66 | +} |
| 67 | + |
20 | 68 | export const mountRouter = createTRPCRouter({ |
21 | 69 | create: protectedProcedure |
22 | 70 | .input(apiCreateMount) |
@@ -71,4 +119,35 @@ export const mountRouter = createTRPCRouter({ |
71 | 119 | ); |
72 | 120 | return mounts; |
73 | 121 | }), |
| 122 | + listByServiceId: protectedProcedure |
| 123 | + .input(apiFindMountByApplicationId) |
| 124 | + .query(async ({ input, ctx }) => { |
| 125 | + console.log("input", input); |
| 126 | + if (ctx.user.role === "member") { |
| 127 | + await checkServiceAccess( |
| 128 | + ctx.user.id, |
| 129 | + input.serviceId, |
| 130 | + ctx.session.activeOrganizationId, |
| 131 | + "access", |
| 132 | + ); |
| 133 | + } |
| 134 | + const organizationId = await getServiceOrganizationId( |
| 135 | + input.serviceId, |
| 136 | + input.serviceType, |
| 137 | + ); |
| 138 | + if ( |
| 139 | + organizationId === null || |
| 140 | + organizationId !== ctx.session.activeOrganizationId |
| 141 | + ) { |
| 142 | + throw new TRPCError({ |
| 143 | + code: "UNAUTHORIZED", |
| 144 | + message: |
| 145 | + "You are not authorized to access this service or it does not exist", |
| 146 | + }); |
| 147 | + } |
| 148 | + return await findMountsByApplicationId( |
| 149 | + input.serviceId, |
| 150 | + input.serviceType, |
| 151 | + ); |
| 152 | + }), |
74 | 153 | }); |
0 commit comments