| 1 | import { randomUUID } from "crypto"; |
| 2 | import { and, eq |
| 3 | import { z } |
| 4 | import { ORPCError } |
| 5 | import { db } |
| 6 | import { servers, subusers |
| 7 | import { recordActivity } |
| 8 | import { protectedProcedure } |
| 9 | |
| 10 | export const subusersRouter = |
| 11 | list: protectedProcedure |
| 12 | .input(z.object |
| 13 | .handler(async ( |
| 14 | const server = |
| 15 | where: eq(servers |
| 16 | }); |
| 17 | if (!server) |
| 18 | if (server.userId |
| 19 | throw new ORPCError( |
| 20 | } |
| 21 | |
| 22 | return db.query |
| 23 | where: eq(subusers |
| 24 | with: { user: true |
| 25 | }); |
| 26 | }), |
| 27 | |
| 28 | create: protectedProcedure |
| 29 | .input( |
| 30 | z.object({ |
| 31 | serverId: z.string |
| 32 | email: z.string |
| 33 | permissions: z.array |
| 34 | }), |
| 35 | ) |
| 36 | .handler(async ( |
| 37 | const server = |
| 38 | where: eq(servers |
| 39 | }); |
| 40 | if (!server) |
| 41 | if (server.userId |
| 42 | throw new ORPCError( |
| 43 | } |
| 44 | |
| 45 | const targetUser = |
| 46 | where: eq(user |
| 47 | }); |
| 48 | if (!targetUser) |
| 49 | throw new ORPCError( |
| 50 | } |
| 51 | if (targetUser.id |
| 52 | throw new ORPCError( |
| 53 | } |
| 54 | |
| 55 | const existing = |
| 56 | where: and(eq |
| 57 | }); |
| 58 | if (existing) { |
| 59 | throw new ORPCError( |
| 60 | } |
| 61 | |
| 62 | const id = |
| 63 | await db.insert |
| 64 | id, |
| 65 | userId: targetUser.id |
| 66 | serverId: input.serverId |
| 67 | permissions: JSON.stringify |
| 68 | }); |
| 69 | |
| 70 | recordActivity({ |
| 71 | eventType: "user:subuser-create", |
| 72 | userId: context.session |
| 73 | serverId: input.serverId |
| 74 | ip: context.ip |
| 75 | properties: { targetEmail: input |
| 76 | }); |
| 77 | |
| 78 | return db.query |
| 79 | }), |
| 80 | |
| 81 | update: protectedProcedure |
| 82 | .input( |
| 83 | z.object({ |
| 84 | serverId: z.string |
| 85 | userId: z.string |
| 86 | permissions: z.array |
| 87 | }), |
| 88 | ) |
| 89 | .handler(async ( |
| 90 | const server = |
| 91 | where: eq(servers |
| 92 | }); |
| 93 | if (!server) |
| 94 | if (server.userId |
| 95 | throw new ORPCError( |
| 96 | } |
| 97 | |
| 98 | await db |
| 99 | .update(subusers) |
| 100 | .set({ permissions: |
| 101 | .where( |
| 102 | and(eq(subusers |
| 103 | ); |
| 104 | |
| 105 | recordActivity({ |
| 106 | eventType: "user:subuser-update", |
| 107 | userId: context.session |
| 108 | serverId: input.serverId |
| 109 | ip: context.ip |
| 110 | properties: { targetUserId: input |
| 111 | }); |
| 112 | }), |
| 113 | |
| 114 | delete: protectedProcedure |
| 115 | .input(z.object |
| 116 | .handler(async ( |
| 117 | const server = |
| 118 | where: eq(servers |
| 119 | }); |
| 120 | if (!server) |
| 121 | if (server.userId |
| 122 | throw new ORPCError( |
| 123 | } |
| 124 | |
| 125 | await db |
| 126 | .delete(subusers) |
| 127 | .where( |
| 128 | and(eq(subusers |
| 129 | ); |
| 130 | |
| 131 | recordActivity({ |
| 132 | eventType: "user:subuser-delete", |
| 133 | userId: context.session |
| 134 | serverId: input.serverId |
| 135 | ip: context.ip |
| 136 | properties: { targetUserId: input |
| 137 | }); |
| 138 | }), |
| 139 | }; |