| 1 | import { and, count, eq, like, or } from "drizzle-orm"; |
| 2 | import { z } |
| 3 | import { db } |
| 4 | import { user, session |
| 5 | import { recordActivity } |
| 6 | import { notifyAdmins } |
| 7 | import { adminProcedure, protectedProcedure |
| 8 | |
| 9 | export const usersRouter = |
| 10 | getSelf: protectedProcedure.handler |
| 11 | return db.query |
| 12 | where: eq(user |
| 13 | columns: { |
| 14 | id: true, |
| 15 | name: true, |
| 16 | email: true, |
| 17 | image: true, |
| 18 | role: true, |
| 19 | createdAt: true, |
| 20 | twoFactorEnabled: true, |
| 21 | billingName: true, |
| 22 | billingAddressLine1: true, |
| 23 | billingAddressLine2: true, |
| 24 | billingCity: true, |
| 25 | billingState: true, |
| 26 | billingPostalCode: true, |
| 27 | billingCountry: true, |
| 28 | vatNumber: true, |
| 29 | vatCountry: true, |
| 30 | locale: true, |
| 31 | }, |
| 32 | }); |
| 33 | }), |
| 34 | |
| 35 | updateProfile: protectedProcedure |
| 36 | .input(z.object |
| 37 | .handler(async ( |
| 38 | await db.update |
| 39 | }), |
| 40 | |
| 41 | updateBilling: protectedProcedure |
| 42 | .input( |
| 43 | z.object({ |
| 44 | billingName: z.string |
| 45 | billingAddressLine1: z.string |
| 46 | billingAddressLine2: z.string |
| 47 | billingCity: z.string |
| 48 | billingState: z.string |
| 49 | billingPostalCode: z.string |
| 50 | billingCountry: z.string |
| 51 | vatNumber: z.string |
| 52 | vatCountry: z.string |
| 53 | }), |
| 54 | ) |
| 55 | .handler(async ( |
| 56 | await db |
| 57 | .update(user) |
| 58 | .set({ |
| 59 | billingName: input.billingName |
| 60 | billingAddressLine1: input.billingAddressLine1 |
| 61 | billingAddressLine2: input.billingAddressLine2 |
| 62 | billingCity: input.billingCity |
| 63 | billingState: input.billingState |
| 64 | billingPostalCode: input.billingPostalCode |
| 65 | billingCountry: input.billingCountry |
| 66 | vatNumber: input.vatNumber |
| 67 | vatCountry: input.vatCountry |
| 68 | }) |
| 69 | .where(eq(user |
| 70 | }), |
| 71 | |
| 72 | updateLocale: protectedProcedure |
| 73 | .input(z.object |
| 74 | .handler(async ( |
| 75 | await db.update |
| 76 | }), |
| 77 | |
| 78 | listSessions: protectedProcedure.handler |
| 79 | return db.query |
| 80 | where: eq(session |
| 81 | columns: { |
| 82 | id: true, |
| 83 | token: true, |
| 84 | createdAt: true, |
| 85 | expiresAt: true, |
| 86 | ipAddress: true, |
| 87 | userAgent: true, |
| 88 | }, |
| 89 | orderBy: (s, |
| 90 | }); |
| 91 | }), |
| 92 | |
| 93 | revokeSession: protectedProcedure |
| 94 | .input(z.object |
| 95 | .handler(async ( |
| 96 | await db |
| 97 | .delete(session) |
| 98 | .where( |
| 99 | and( |
| 100 | eq(session.token |
| 101 | eq(session.userId |
| 102 | ), |
| 103 | ); |
| 104 | }), |
| 105 | |
| 106 | search: adminProcedure |
| 107 | .input(z.object |
| 108 | .handler(async ( |
| 109 | const q = |
| 110 | return db.query |
| 111 | where: or(like |
| 112 | columns: { id: true |
| 113 | limit: 10, |
| 114 | }); |
| 115 | }), |
| 116 | |
| 117 | list: adminProcedure |
| 118 | .input( |
| 119 | z.object({ |
| 120 | page: z.number |
| 121 | search: z.string |
| 122 | }), |
| 123 | ) |
| 124 | .handler(async ( |
| 125 | const pageSize = |
| 126 | const offset = |
| 127 | |
| 128 | const whereClause = |
| 129 | ? or(like( |
| 130 | : undefined; |
| 131 | |
| 132 | const [users, |
| 133 | db.query.user |
| 134 | where: whereClause, |
| 135 | columns: { |
| 136 | id: true, |
| 137 | name: true, |
| 138 | email: true, |
| 139 | image: true, |
| 140 | role: true, |
| 141 | banned: true, |
| 142 | banReason: true, |
| 143 | banExpires: true, |
| 144 | createdAt: true, |
| 145 | }, |
| 146 | limit: pageSize, |
| 147 | offset, |
| 148 | orderBy: (u, |
| 149 | }), |
| 150 | db.select({ |
| 151 | ]); |
| 152 | |
| 153 | return { users, |
| 154 | }), |
| 155 | |
| 156 | adminGet: adminProcedure |
| 157 | .input(z.object |
| 158 | .handler(async ( |
| 159 | const [userData, |
| 160 | db.query.user |
| 161 | where: eq(user |
| 162 | columns: { |
| 163 | id: true, |
| 164 | name: true, |
| 165 | email: true, |
| 166 | image: true, |
| 167 | role: true, |
| 168 | banned: true, |
| 169 | banReason: true, |
| 170 | banExpires: true, |
| 171 | createdAt: true, |
| 172 | twoFactorEnabled: true, |
| 173 | billingName: true, |
| 174 | billingAddressLine1: true, |
| 175 | billingAddressLine2: true, |
| 176 | billingCity: true, |
| 177 | billingState: true, |
| 178 | billingPostalCode: true, |
| 179 | billingCountry: true, |
| 180 | vatNumber: true, |
| 181 | vatCountry: true, |
| 182 | }, |
| 183 | }), |
| 184 | db.select({ |
| 185 | ]); |
| 186 | |
| 187 | if (!userData) |
| 188 | return { ...userData |
| 189 | }), |
| 190 | |
| 191 | adminGetSessions: adminProcedure |
| 192 | .input(z.object |
| 193 | .handler(async ( |
| 194 | return db.query |
| 195 | where: eq(session |
| 196 | columns: { |
| 197 | id: true, |
| 198 | token: true, |
| 199 | createdAt: true, |
| 200 | expiresAt: true, |
| 201 | ipAddress: true, |
| 202 | userAgent: true, |
| 203 | }, |
| 204 | orderBy: (s, |
| 205 | }); |
| 206 | }), |
| 207 | |
| 208 | adminGetServers: adminProcedure |
| 209 | .input(z.object |
| 210 | .handler(async ( |
| 211 | const pageSize = |
| 212 | const offset = |
| 213 | |
| 214 | const [userServers, |
| 215 | db |
| 216 | .select({ |
| 217 | id: servers.id |
| 218 | uuid: servers.uuid |
| 219 | name: servers.name |
| 220 | suspended: servers.suspended |
| 221 | createdAt: servers.createdAt |
| 222 | nodeName: nodes.name |
| 223 | allocationIp: nodeAllocations.ip |
| 224 | allocationPort: nodeAllocations.port |
| 225 | }) |
| 226 | .from(servers) |
| 227 | .leftJoin(nodes, |
| 228 | .leftJoin(nodeAllocations, |
| 229 | .where(eq(servers |
| 230 | .limit(pageSize) |
| 231 | .offset(offset) |
| 232 | .orderBy(servers.createdAt |
| 233 | db.select({ |
| 234 | ]); |
| 235 | |
| 236 | return { servers: |
| 237 | }), |
| 238 | |
| 239 | setRole: adminProcedure |
| 240 | .input(z.object |
| 241 | .handler(async ( |
| 242 | if (input.userId |
| 243 | throw new Error( |
| 244 | } |
| 245 | await db.update |
| 246 | recordActivity({ eventType: |
| 247 | }), |
| 248 | |
| 249 | ban: adminProcedure |
| 250 | .input( |
| 251 | z.object({ |
| 252 | userId: z.string |
| 253 | reason: z.string |
| 254 | expiresAt: z.string |
| 255 | }), |
| 256 | ) |
| 257 | .handler(async ( |
| 258 | if (input.userId |
| 259 | throw new Error( |
| 260 | } |
| 261 | await db |
| 262 | .update(user) |
| 263 | .set({ |
| 264 | banned: true, |
| 265 | banReason: input.reason |
| 266 | banExpires: input.expiresAt |
| 267 | }) |
| 268 | .where(eq(user |
| 269 | recordActivity({ eventType: |
| 270 | }), |
| 271 | |
| 272 | unban: adminProcedure |
| 273 | .input(z.object |
| 274 | .handler(async ( |
| 275 | await db |
| 276 | .update(user) |
| 277 | .set({ banned: |
| 278 | .where(eq(user |
| 279 | recordActivity({ eventType: |
| 280 | }), |
| 281 | |
| 282 | delete: adminProcedure |
| 283 | .input(z.object |
| 284 | .handler(async ( |
| 285 | if (input.userId |
| 286 | throw new Error( |
| 287 | } |
| 288 | const target = |
| 289 | where: eq(user |
| 290 | columns: { email: true |
| 291 | }); |
| 292 | await db.delete |
| 293 | recordActivity({ eventType: |
| 294 | if (target) notifyAdmins |
| 295 | }), |
| 296 | }; |